Main menu:

Site search

 

November 2017
M T W T F S S
« Oct    
 12345
6789101112
13141516171819
20212223242526
27282930  

Categories

Archives

Links:

Going fullscreen in Cocoa - Part I

For a project I’m working on I thought it would be nice to give the user the possibility to use the full screen (e.g. full screen editing in iPhoto, slideshow in Preview, …). As usual this turned out to be a bit less evident than I thought it would be.

The effect I wanted to achieve is as follows. When the user selects the “Go Fullscreen” menu item, the screen would fade out to black and then fade in with the key window now occupying the full screen. Selecting “Go Fullscreen” again would fade out the screen to black and then fade in with the key window having its original size again. A small sample project with the solution presented in part I can be found at the end of this blog post.

First approach:

The NSView class has the following two methods which look very promising:

- (BOOL)enterFullScreenMode:(NSScreen *)screen withOptions:(NSDictionary *)options

and

- (void)exitFullScreenModeWithOptions:(NSDictionary *)options

The possible values that can be given to the options parameter is unfortunately somewhat limited (especially in Leopard 10.5). The biggest drawback in my opinion is that you can’t animate how the view takes over the full screen (e.g. through a fading effect like I wanted to achieve, growing/shrinking the view to/from fullscreen, …). So I need some extra code to fade in and out.
The following code snippet presents the ‘core’ of going full screen and returning back.

if ([aView isInFullScreenMode] == NO)
{
    [self fadeOut];
    [aView enterFullScreenMode:[NSScreen mainScreen]
                   withOptions:nil];
    [self fadeIn];
}
else
{
    [self fadeOut];
    [aView exitFullScreenModeWithOptions:nil];
    [self fadeIn];
} /* end if */

Apple calls this the kiosk mode and has recently released some extra info which might be useful if you’re using Snow Leopard. This documentation can be found here.

Fading the screen

Apple has some documentation on how to achieve the fading effect I wanted and it can be found at this URL. The code is quite simple as can be seen here.

CGDisplayFadeReservationToken    token;  /* Fade out/in token */

...

- (void)fadeOut
{
    CGDisplayErr    err;

    err = CGAcquireDisplayFadeReservation(kCGMaxDisplayReservationInterval,
                                          &token);
    if (err == kCGErrorSuccess)
    {
        CGDisplayFade(token,
                      1.5,
                      kCGDisplayBlendNormal,
                      kCGDisplayBlendSolidColor,
                      0,
                      0,
                      0,
                      TRUE);
    } /* end if */
} /* end fadeOut */

- (void)fadeIn
{
    CGDisplayErr    err;

    err = CGDisplayFade(token,
                        1.5,
                        kCGDisplayBlendSolidColor,
                        kCGDisplayBlendNormal,
                        0,
                        0,
                        0,
                        TRUE);
    if (err == kCGErrorSuccess)
    {
        CGReleaseDisplayFadeReservation(token);
    } /* end if */
} /* end fadeIn */

Drawbacks of this approach

As it turns out, there are a few drawbacks with the combination of both techniques which made this a less than perfect solution for me:

    The enterFullScreenMode and enterFullScreenMode methods as they are implemented in Leopard (OS X 10.5) hide the dock and menubar for you but no means to show the menubar again when the mouse hits the top of the screen (Snow Leopard fixes this).

    CGDisplayFade() unfortunately fades all displays connected to your Mac, which isn’t what I wanted (only the screen where the window is located should go through the fade in/out cycle).

I made a small project that displays a photo of Monument Valley I took the week after WWDC ‘09. Using command-F you can toggle back and forth to full screen mode via a fade out to black and fade in again.

In part II, I’ll present another approach on how to achieve the wanted effect in an attempt to avoid the above mentioned drawbacks.

Here is project using the above code: gofullscreen-part-i.zip

Write a comment

You need to login to post comments!