4
\$\begingroup\$

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?

\$\endgroup\$
3
  • \$\begingroup\$ Off the top of my head: can any of it be done in interface builder? \$\endgroup\$
    – Stuart
    Jan 27, 2011 at 17:43
  • \$\begingroup\$ Nope, I don't think so. \$\endgroup\$
    – Seb Jachec
    Jan 27, 2011 at 18:02
  • \$\begingroup\$ 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? \$\endgroup\$
    – Hack Saw
    Jan 27, 2011 at 18:24

3 Answers 3

4
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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 \$\endgroup\$
    – Seb Jachec
    Jan 27, 2011 at 18:04
  • \$\begingroup\$ Yeah, that'd be nice. \$\endgroup\$
    – Hack Saw
    Jan 27, 2011 at 18:22
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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];
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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