OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "chrome/browser/fullscreen.h" | 5 #import "chrome/browser/fullscreen.h" |
6 | 6 |
7 #import <Carbon/Carbon.h> | |
8 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
9 | 8 |
10 #import "base/logging.h" | 9 #import "base/logging.h" |
10 #import "third_party/GTM/Foundation/GTMNSObject+KeyValueObserving.h" | |
11 | |
12 // Replicate specific 10.7 SDK declarations for building with prior SDKs. | |
13 #if !defined(MAC_OS_X_VERSION_10_7) || \ | |
14 MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_7 | |
15 | |
16 enum { | |
17 NSApplicationPresentationFullScreen = 1 << 10 | |
18 }; | |
19 | |
20 #endif // MAC_OS_X_VERSION_10_7 | |
21 | |
22 namespace { | |
23 | |
24 bool AreOptionsFullScreen(NSApplicationPresentationOptions options) { | |
25 // If both dock and menu bar are hidden, that is the equivalent of the Carbon | |
26 // SystemUIMode (or Info.plist's LSUIPresentationMode) kUIModeAllHidden. | |
27 if (((options & NSApplicationPresentationHideDock) || | |
28 (options & NSApplicationPresentationAutoHideDock)) && | |
29 ((options & NSApplicationPresentationHideMenuBar) || | |
30 (options & NSApplicationPresentationAutoHideMenuBar))) | |
Robert Sesek
2012/12/28 20:18:54
nit: needs {} since the condition is multi-line
Avi (use Gerrit)
2012/12/28 21:02:44
Done.
| |
31 return true; | |
32 | |
33 if (options & NSApplicationPresentationFullScreen) | |
34 return true; | |
35 | |
36 return false; | |
37 } | |
38 | |
39 } // namespace | |
11 | 40 |
12 @interface FullScreenMonitor : NSObject { | 41 @interface FullScreenMonitor : NSObject { |
13 @private | 42 @private |
14 BOOL fullScreen_; | 43 BOOL fullScreen_; |
15 EventHandlerRef eventHandler_; | |
16 } | 44 } |
17 | 45 |
18 @property (nonatomic, getter=isFullScreen) BOOL fullScreen; | 46 @property (nonatomic, getter=isFullScreen) BOOL fullScreen; |
19 | 47 |
20 @end | 48 @end |
21 | 49 |
22 static OSStatus handleAppEvent(EventHandlerCallRef myHandler, | |
23 EventRef event, | |
24 void* userData) { | |
25 DCHECK(userData); | |
26 | |
27 FullScreenMonitor* fullScreenMonitor = | |
28 reinterpret_cast<FullScreenMonitor*>(userData); | |
29 | |
30 UInt32 mode = 0; | |
31 OSStatus status = GetEventParameter(event, | |
32 kEventParamSystemUIMode, | |
33 typeUInt32, | |
34 NULL, | |
35 sizeof(UInt32), | |
36 NULL, | |
37 &mode); | |
38 if (status != noErr) | |
39 return status; | |
40 BOOL isFullScreenMode = mode == kUIModeAllHidden; | |
41 [fullScreenMonitor setFullScreen:isFullScreenMode]; | |
42 return noErr; | |
43 } | |
44 | |
45 @implementation FullScreenMonitor | 50 @implementation FullScreenMonitor |
46 | 51 |
47 @synthesize fullScreen = fullScreen_; | 52 @synthesize fullScreen = fullScreen_; |
48 | 53 |
49 - (id)init { | 54 - (id)init { |
50 if ((self = [super init])) { | 55 if ((self = [super init])) { |
51 // Check if the user is in presentation mode initially. | 56 [NSApp gtm_addObserver:self |
52 SystemUIMode currentMode; | 57 forKeyPath:@"currentSystemPresentationOptions" |
53 GetSystemUIMode(¤tMode, NULL); | 58 selector:@selector(observeNotification:) |
54 fullScreen_ = currentMode == kUIModeAllHidden; | 59 userInfo:nil |
55 | 60 options:NSKeyValueObservingOptionNew | |
56 // Register a Carbon event to receive the notification about the login | 61 NSKeyValueObservingOptionInitial]; |
57 // session's UI mode change. | |
58 EventTypeSpec events[] = | |
59 {{ kEventClassApplication, kEventAppSystemUIModeChanged }}; | |
60 OSStatus status = InstallApplicationEventHandler( | |
61 NewEventHandlerUPP(handleAppEvent), | |
62 GetEventTypeCount(events), | |
63 events, | |
64 self, | |
65 &eventHandler_); | |
66 if (status) { | |
67 [self release]; | |
68 self = nil; | |
69 } | |
70 } | 62 } |
71 return self; | 63 return self; |
72 } | 64 } |
73 | 65 |
74 - (void)dealloc { | 66 - (void)dealloc { |
75 if (eventHandler_) | 67 [NSApp gtm_removeObserver:self |
76 RemoveEventHandler(eventHandler_); | 68 forKeyPath:@"currentSystemPresentationOptions" |
69 selector:@selector(observeNotification:)]; | |
77 [super dealloc]; | 70 [super dealloc]; |
78 } | 71 } |
79 | 72 |
73 - (void)observeNotification:(GTMKeyValueChangeNotification*)notification { | |
74 NSDictionary* change = [notification change]; | |
75 NSApplicationPresentationOptions options = | |
76 [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; | |
77 [self setFullScreen:AreOptionsFullScreen(options)]; | |
78 } | |
79 | |
80 @end | 80 @end |
81 | 81 |
82 static FullScreenMonitor* g_fullScreenMonitor = nil; | 82 static FullScreenMonitor* g_fullScreenMonitor = nil; |
83 | 83 |
84 void InitFullScreenMonitor() { | 84 void InitFullScreenMonitor() { |
85 if (!g_fullScreenMonitor) | 85 if (!g_fullScreenMonitor) |
86 g_fullScreenMonitor = [[FullScreenMonitor alloc] init]; | 86 g_fullScreenMonitor = [[FullScreenMonitor alloc] init]; |
87 } | 87 } |
88 | 88 |
89 void StopFullScreenMonitor() { | 89 void StopFullScreenMonitor() { |
90 [g_fullScreenMonitor release]; | 90 [g_fullScreenMonitor release]; |
91 g_fullScreenMonitor = nil; | 91 g_fullScreenMonitor = nil; |
92 } | 92 } |
93 | 93 |
94 bool IsFullScreenMode() { | 94 bool IsFullScreenMode() { |
95 // Check if the main display has been captured (game in particular). | 95 // Check if the main display has been captured (by games in particular). |
96 if (CGDisplayIsCaptured(CGMainDisplayID())) | 96 if (CGDisplayIsCaptured(CGMainDisplayID())) |
97 return true; | 97 return true; |
98 | 98 |
99 return [g_fullScreenMonitor isFullScreen]; | 99 return [g_fullScreenMonitor isFullScreen]; |
100 } | 100 } |
OLD | NEW |