13

I guess I should check if [NSApplication presentationOptions] contains NSFullScreenModeApplicationPresentationOptions, but how do I achieve that?

EDIT: using [NSApplication presentationOptions] doesn't work as in my document-based app there might be some documents in fullscreen and others not. I'm now looking for another solution. I'm wondering why there isn't a property called [NSWindow isFullscreen] or something like that.

Nickkk
  • 2,261
  • 1
  • 25
  • 34

4 Answers4

32

I was just looking for a solution myself and based on Matthieu's answer I created a category on NSWindow that works fine for me.

@interface NSWindow (FullScreen)

- (BOOL)mn_isFullScreen;

@end

@implementation NSWindow (FullScreen)

- (BOOL)mn_isFullScreen
{
    return (([self styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask);
}

@end
Markus Müller-Simhofer
  • 3,391
  • 1
  • 28
  • 32
  • 1
    NSFullScreenWindowMask is deprecated now. Use NSWindowStyleMaskFullScreen i.e. return (([self styleMask] & NSWindowStyleMaskFullScreen) != 0); – w0mbat Aug 11 '20 at 20:08
11

You need to use an & bitwise operator to test that that option is being used. Not tested but probably something like this:

- (BOOL) inFullScreenMode {
    NSApplicationPresentationOptions opts = [[NSApplication sharedApplication ] presentationOptions];
    if ( opts & NSApplicationPresentationFullScreen) {
       return YES;
    }
    return NO;
}

To see if any of your windows are in full screen mode simply check the style mask of the window.

NSUInteger masks = [someNSWindow styleMask]
if ( masks & NSFullScreenWindowMask) {
 // Do something
}
Matthieu Cormier
  • 2,185
  • 1
  • 21
  • 32
  • Thank you, this is exactly what I needed. Just one correction: use `NSApp` instead of `NSApplication`, otherwise you will get a warning. Oh... and of course `- (BOOL) inFullScreenMode()` should be written as `- (BOOL) inFullScreenMode`. – Nickkk Jul 27 '11 at 10:07
  • Unfortunately, now there is another problem: in my document-based app, using the above solution won't work as some of the documents might be fullscreen and others not. Any other solution? – Nickkk Jul 29 '11 at 19:54
  • Simply check the style masks of all your windows. I've added some sample code. – Matthieu Cormier Jul 31 '11 at 12:55
4

For Swift 3.0

if let window = NSApp.mainWindow {
    let isWindowFullscreen = window.styleMask.contains(NSFullScreenWindowMask)
}

Obviously, for the original question, you'd replace NSApp.mainWindow with whichever document window you're wanting to check.

John Nesbitt
  • 8,984
  • 1
  • 14
  • 7
1

The way I handled it in pre-10.7 (where neither NSApplicationPresentationFullScreen nor NSFullScreenWindowMask was available) was to check

if ([mainWindow frame].size.height == [[mainWindow screen] frame].size.height)
{
    // window is fullscreen
}

And this piece of really old code seem to still work not only on "Lion" but also on today's - at the time of writing 10.14.x - OS.

Andrew_STOP_RU_WAR_IN_UA
  • 9,318
  • 5
  • 65
  • 101
silverdr
  • 1,978
  • 2
  • 22
  • 27
  • screen.frame is nil in case of fullscreen :) So it will not work – Andrew_STOP_RU_WAR_IN_UA Jan 07 '22 at 06:59
  • Except that the dcoumentation says "it is nil when the window is offscreen", not when it is fullscreen. And yes - as I mentioned - this old piece of code still works today. https://developer.apple.com/documentation/appkit/nswindow/1419232-screen?language=objc – silverdr Jan 07 '22 at 19:06
  • I don't know what is "offscreen" means. But just run the code and check what is placed in this frame when your app is fullscreened. – Andrew_STOP_RU_WAR_IN_UA Jan 07 '22 at 22:23
  • steps to reproduce: 1: place button that print's to console "is fullscreened: (value)" in to the safe area of window. 2. run app and fullscreen it; 3. place mouse to the top of screen (menu line did not appear) and press the button. It will write to console that app is not fullscreened even if it is. Resolution: this code does not work properly. – Andrew_STOP_RU_WAR_IN_UA Jan 09 '22 at 01:40
  • Which is unfortunately again not true. To make it easier here is a ready xcode project you can verify your resolution with: https://cld.silverdr.com/s/oLcWeLXw3kjcSXW – silverdr Apr 17 '22 at 09:20