1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ui/base/cocoa/fullscreen_window_manager.h"
9 // Get the screen with the menu bar.
10 NSScreen* GetMenuBarScreen() {
11 // Documentation in NSScreen says that the first object in
12 // +[NSScreen screens] is the menu bar screen.
13 NSArray *screens = [NSScreen screens];
15 return [screens objectAtIndex:0];
19 // Get the screen with the dock.
20 NSScreen* GetDockScreen() {
21 NSArray *screens = [NSScreen screens];
22 NSUInteger count = [screens count];
26 return [screens objectAtIndex:0];
28 for (NSUInteger i = 1; i < count; ++i) {
29 NSScreen* screen = [screens objectAtIndex:i];
30 // This screen is not the menu bar screen since it's not index 0. Therefore,
31 // the only reason that the frame would not match the visible frame is if
32 // the dock is on the screen.
33 if (!NSEqualRects([screen frame], [screen visibleFrame]))
36 return [screens objectAtIndex:0];
41 @interface FullscreenWindowManager()
42 - (void)onScreenChanged:(NSNotification*)note;
46 @implementation FullscreenWindowManager
48 - (id)initWithWindow:(NSWindow*)window
49 desiredScreen:(NSScreen*)desiredScreen {
50 if ((self = [super init])) {
51 window_.reset([window retain]);
52 desiredScreen_.reset([desiredScreen retain]);
53 fullscreenMode_ = base::mac::kFullScreenModeNormal;
54 [[NSNotificationCenter defaultCenter]
56 selector:@selector(onScreenChanged:)
57 name:NSApplicationDidChangeScreenParametersNotification
64 [[NSNotificationCenter defaultCenter] removeObserver:self];
65 [self exitFullscreenMode];
69 - (void)enterFullscreenMode {
70 if (fullscreenActive_)
72 fullscreenActive_ = true;
76 - (void)exitFullscreenMode {
77 if (!fullscreenActive_)
79 fullscreenActive_ = false;
83 - (void)onScreenChanged:(NSNotification*)note {
88 // From OS X 10.10, NSApplicationDidChangeScreenParametersNotification is sent
89 // when displaying a fullscreen window, which should normally only be sent if
90 // the monitor resolution has changed or new display is detected.
91 if (![[NSScreen screens] containsObject:desiredScreen_])
92 desiredScreen_.reset([[window_ screen] retain]);
94 base::mac::FullScreenMode newMode;
95 if (!fullscreenActive_)
96 newMode = base::mac::kFullScreenModeNormal;
97 else if ([desiredScreen_ isEqual:GetMenuBarScreen()])
98 newMode = base::mac::kFullScreenModeHideAll;
99 else if ([desiredScreen_ isEqual:GetDockScreen()])
100 newMode = base::mac::kFullScreenModeHideDock;
102 newMode = base::mac::kFullScreenModeNormal;
104 if (fullscreenMode_ != newMode) {
105 if (fullscreenMode_ != base::mac::kFullScreenModeNormal)
106 base::mac::ReleaseFullScreen(fullscreenMode_);
107 if (newMode != base::mac::kFullScreenModeNormal)
108 base::mac::RequestFullScreen(newMode);
109 fullscreenMode_ = newMode;
112 if (fullscreenActive_)
113 [window_ setFrame:[desiredScreen_ frame] display:YES];