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) { | |
Robert Sesek
2012/12/28 21:09:40
Use ObjC BOOL since this is used as a value for fu
Avi (use Gerrit)
2012/12/28 21:31:52
Done.
| |
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))) { | |
31 return true; | |
32 } | |
33 | |
34 if (options & NSApplicationPresentationFullScreen) | |
35 return true; | |
36 | |
37 return false; | |
38 } | |
39 | |
40 } // namespace | |
11 | 41 |
12 @interface FullScreenMonitor : NSObject { | 42 @interface FullScreenMonitor : NSObject { |
13 @private | 43 @private |
14 BOOL fullScreen_; | 44 BOOL fullScreen_; |
15 EventHandlerRef eventHandler_; | |
16 } | 45 } |
17 | 46 |
18 @property (nonatomic, getter=isFullScreen) BOOL fullScreen; | 47 @property (nonatomic, getter=isFullScreen) BOOL fullScreen; |
19 | 48 |
20 @end | 49 @end |
21 | 50 |
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 | 51 @implementation FullScreenMonitor |
46 | 52 |
47 @synthesize fullScreen = fullScreen_; | 53 @synthesize fullScreen = fullScreen_; |
48 | 54 |
49 - (id)init { | 55 - (id)init { |
50 if ((self = [super init])) { | 56 if ((self = [super init])) { |
51 // Check if the user is in presentation mode initially. | 57 [NSApp gtm_addObserver:self |
52 SystemUIMode currentMode; | 58 forKeyPath:@"currentSystemPresentationOptions" |
53 GetSystemUIMode(¤tMode, NULL); | 59 selector:@selector(observeNotification:) |
54 fullScreen_ = currentMode == kUIModeAllHidden; | 60 userInfo:nil |
55 | 61 options:NSKeyValueObservingOptionNew | |
56 // Register a Carbon event to receive the notification about the login | 62 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 } | 63 } |
71 return self; | 64 return self; |
72 } | 65 } |
73 | 66 |
74 - (void)dealloc { | 67 - (void)dealloc { |
75 if (eventHandler_) | 68 [NSApp gtm_removeObserver:self |
76 RemoveEventHandler(eventHandler_); | 69 forKeyPath:@"currentSystemPresentationOptions" |
70 selector:@selector(observeNotification:)]; | |
77 [super dealloc]; | 71 [super dealloc]; |
78 } | 72 } |
79 | 73 |
74 - (void)observeNotification:(GTMKeyValueChangeNotification*)notification { | |
75 NSDictionary* change = [notification change]; | |
76 NSApplicationPresentationOptions options = | |
77 [[change objectForKey:NSKeyValueChangeNewKey] integerValue]; | |
78 [self setFullScreen:AreOptionsFullScreen(options)]; | |
79 } | |
80 | |
80 @end | 81 @end |
81 | 82 |
82 static FullScreenMonitor* g_fullScreenMonitor = nil; | 83 static FullScreenMonitor* g_fullScreenMonitor = nil; |
83 | 84 |
84 void InitFullScreenMonitor() { | 85 void InitFullScreenMonitor() { |
85 if (!g_fullScreenMonitor) | 86 if (!g_fullScreenMonitor) |
86 g_fullScreenMonitor = [[FullScreenMonitor alloc] init]; | 87 g_fullScreenMonitor = [[FullScreenMonitor alloc] init]; |
87 } | 88 } |
88 | 89 |
89 void StopFullScreenMonitor() { | 90 void StopFullScreenMonitor() { |
90 [g_fullScreenMonitor release]; | 91 [g_fullScreenMonitor release]; |
91 g_fullScreenMonitor = nil; | 92 g_fullScreenMonitor = nil; |
92 } | 93 } |
93 | 94 |
94 bool IsFullScreenMode() { | 95 bool IsFullScreenMode() { |
95 // Check if the main display has been captured (game in particular). | 96 // Check if the main display has been captured (by games in particular). |
96 if (CGDisplayIsCaptured(CGMainDisplayID())) | 97 if (CGDisplayIsCaptured(CGMainDisplayID())) |
97 return true; | 98 return true; |
98 | 99 |
99 return [g_fullScreenMonitor isFullScreen]; | 100 return [g_fullScreenMonitor isFullScreen]; |
100 } | 101 } |
OLD | NEW |