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. 4 5#import "ui/base/cocoa/fullscreen_window_manager.h" 6 7namespace { 8 9// Get the screen with the menu bar. 10NSScreen* GetMenuBarScreen() { 11 // Documentation in NSScreen says that the first object in 12 // +[NSScreen screens] is the menu bar screen. 13 NSArray *screens = [NSScreen screens]; 14 if ([screens count]) 15 return [screens objectAtIndex:0]; 16 return nil; 17} 18 19// Get the screen with the dock. 20NSScreen* GetDockScreen() { 21 NSArray *screens = [NSScreen screens]; 22 NSUInteger count = [screens count]; 23 if (count == 0) 24 return NULL; 25 if (count == 1) 26 return [screens objectAtIndex:0]; 27 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])) 34 return screen; 35 } 36 return [screens objectAtIndex:0]; 37} 38 39} // namespace 40 41@interface FullscreenWindowManager() 42- (void)onScreenChanged:(NSNotification*)note; 43- (void)update; 44@end 45 46@implementation FullscreenWindowManager 47 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] 55 addObserver:self 56 selector:@selector(onScreenChanged:) 57 name:NSApplicationDidChangeScreenParametersNotification 58 object:NSApp]; 59 } 60 return self; 61} 62 63- (void)dealloc { 64 [[NSNotificationCenter defaultCenter] removeObserver:self]; 65 [self exitFullscreenMode]; 66 [super dealloc]; 67} 68 69- (void)enterFullscreenMode { 70 if (fullscreenActive_) 71 return; 72 fullscreenActive_ = true; 73 [self update]; 74} 75 76- (void)exitFullscreenMode { 77 if (!fullscreenActive_) 78 return; 79 fullscreenActive_ = false; 80 [self update]; 81} 82 83- (void)onScreenChanged:(NSNotification*)note { 84 [self update]; 85} 86 87- (void)update { 88 if (![[NSScreen screens] containsObject:desiredScreen_]) 89 desiredScreen_.reset([[window_ screen] retain]); 90 91 base::mac::FullScreenMode newMode; 92 if (!fullscreenActive_) 93 newMode = base::mac::kFullScreenModeNormal; 94 else if (desiredScreen_ == GetMenuBarScreen()) 95 newMode = base::mac::kFullScreenModeHideAll; 96 else if (desiredScreen_ == GetDockScreen()) 97 newMode = base::mac::kFullScreenModeHideDock; 98 else 99 newMode = base::mac::kFullScreenModeNormal; 100 101 if (fullscreenMode_ != newMode) { 102 if (fullscreenMode_ != base::mac::kFullScreenModeNormal) 103 base::mac::ReleaseFullScreen(fullscreenMode_); 104 if (newMode != base::mac::kFullScreenModeNormal) 105 base::mac::RequestFullScreen(newMode); 106 fullscreenMode_ = newMode; 107 } 108 109 if (fullscreenActive_) 110 [window_ setFrame:[desiredScreen_ frame] display:YES]; 111} 112 113@end 114