12

I'm searching for the "best" way of creating a fullscreen overlay under Mac OS X. I want to create a transparent or semi-transparent overlay, which cares about mouse events and shows other input/output elements.

This overlay should be above every other GUI items (like the CMD-Tab overlay).

Do you know how to do it effectively? At the moment I'm playing around with this kind of code:

int windowLevel = CGShieldingWindowLevel();
NSRect windowRect = [[NSScreen mainScreen] frame];
NSWindow *overlayWindow = [[NSWindow alloc] initWithContentRect:windowRect
                                          styleMask:NSBorderlessWindowMask
                                            backing:NSBackingStoreBuffered
                                              defer:NO
                                             screen:[NSScreen mainScreen]];

[overlayWindow setReleasedWhenClosed:YES];
[overlayWindow setLevel:windowLevel];
[overlayWindow setBackgroundColor:[NSColor colorWithCalibratedRed:0.0
                                                          green:0.0
                                                           blue:0.0
                                                          alpha:0.5]];
[overlayWindow setAlphaValue:1.0];
[overlayWindow setOpaque:NO];
[overlayWindow setIgnoresMouseEvents:NO];
[overlayWindow makeKeyAndOrderFront:nil];

…and it works fine but I've got no options to initiate any kind of animations like slowly increasing the transparency (slowly dimming the screen) etc.

Although I'm not understanding how to put this window in the background, without releasing it and let it pop up time to time.

So is there a better or "standard" way to do it?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
tamasgal
  • 24,826
  • 18
  • 96
  • 135
  • You will have to use custom drawing and/or a timer to implement animations. You have the part about creating an overlay down, you are just struggling with drawing on it. So, your question isn't with creating an overlay, it's with drawing on a view. – Alex Nichol Nov 25 '11 at 18:18
  • 3
    `[NSScreen mainScreen]` isn't what you probably think it is: It's the screen with the active (main) window on it, which may be a secondary screen. If you want the screen that has the menu bar on it, that's `[[NSScreen screens] objectAtIndex:0]`. (*Everybody* makes this mistake at least once.) – Peter Hosey Nov 26 '11 at 07:59
  • This sounds like it could be a bad idea, if not implemented properly: If another application that wants to track mouse movements were to use this method, it would conflict with yours. – titaniumdecoy Nov 26 '11 at 08:01

1 Answers1

6

You can use NSViewAnimation. Yes, it works on windows, too.

Your animation's target should be the window, and its effect should be fade-in or fade-out, depending on whether you're showing or hiding it. Leave out the frame keys, since you probably don't want to move or resize the window.

Of course, you should leave out the makeKeyAndOrderFront: message, since you'll be ordering it front with the fade-in effect.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • OK thanks! I'll try it. So in fact, there is no other (better) way to create a fullscreen overlay? – tamasgal Nov 26 '11 at 14:17
  • 2
    @septi: Not an overlay, no. Making a full-screen borderless window at a relevant window level is the way to do it. – Peter Hosey Nov 26 '11 at 16:10