Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

This is some of the code I have:

[window setLevel:kCGScreenSaverWindowLevel];
[window setOpaque:NO];
[window setStyleMask:0];
[window setBackgroundColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.3]];
[window setAlphaValue:0];

[window setFrame:[window frameRectForContentRect:[[window screen] frame]] display:YES animate:YES];

[window makeKeyAndOrderFront:self];
[[window animator] setAlphaValue:1.0];

I was just wondering if there was any way to compact it, all of those commands to my window. Any ways to improve it too?

share|improve this question

5 Answers

up vote 4 down vote accepted

This is a highly readable style, and simple. You might be able to make a loop and run through the list in some fashion, but it's unlikely to actually lower complexity, just shift it around a bit.

share|improve this answer
 
It's just that for example in AppleScript (One of the first things I learnt) you can just do a 'tell' block... like "Tell myWindow" and then list all of the commands. It saves typing out 'window' over and over again –  magikseb Jan 27 '11 at 18:04
 
Yeah, that'd be nice. –  Hack Saw Jan 27 '11 at 18:22

Off the top of my head: can any of it be done in interface builder?

share|improve this answer
 
Nope, I don't think so. –  magikseb Jan 27 '11 at 18:02
5  
I'm pretty sure a number of these properties can be set in IB. I think the meat of the question is do you actually need to create this window programatically, or could you load it from a NIB? –  Hack Saw Jan 27 '11 at 18:24

If you are calling this code more than once, I would do this:

In the implementation file:

#import "ThisClass.h"

@interface ThisClass() {}
    - (void)doWindowStuff;

@end

@implementation ThisClass

- (void)doWindowStuff
{
    window.level = kCGScreenSaverWindowLevel;
    [window setOpaque:NO];
    window.styleMask = 0;
    window.backgroundColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.3];
    window.alphaValue = 0;

    [window setFrame:[window frameRectForContentRect:window.screen.frame] display:YES animate:YES];

    [window makeKeyAndOrderFront:self];
    [window.animator setAlphaValue:1.0];
}

- (void)someOtherMethods
{
    // other code
    [self doWindowStuff];
}

Note: (from experience) beware interface builder... setting some things in IB can make code harder to troubleshoot and/or read.

share|improve this answer

Most Objective-C methods return the target object rather than void, so this might work:

[[[window setLevel:kCGScreenSaverWindowLevel] setOpaque:NO] setStyleMask:0];

and so on...

share|improve this answer
 
I tried that out but I get an error: "Void value not ignored as it ought to be. –  magikseb Jan 27 '11 at 18:38
1  
Probably doable - but it's horrible style IMO –  Abizern May 28 '11 at 16:15
1  
This is untrue. Setters always return void. –  Adam Ko Aug 25 '11 at 6:01

Depending on how you feel about the use of Objective-C 2.0 properties you can you can:

window.level = kCGScreenSaverWindowLevel;
[window setOpaque:NO];
window.styleMask = 0;
window.backgroundColor = [NSColor colorWithCalibratedWhite:0.0 alpha:0.3];
window.alphaValue = 0;

[window setFrame:[window frameRectForContentRect:window.screen.frame] display:YES animate:YES];

[window makeKeyAndOrderFront:self];
[window.animator setAlphaValue:1.0];

I had programmed in C# for many years before taking up Objective-C and I still find the bracket notation hard to read at times. For my own readability I would would also introduce a variable for the new frame.

NSRect newFrame = [window frameRectForContentRect:window.screen.frame];
[window setFrame:newFrame display:YES animate:YES];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.