OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ui/cocoa/apps/app_menu_controller_mac.h" | 5 #import "chrome/browser/ui/cocoa/apps/app_menu_controller_mac.h" |
6 | 6 |
7 #include "apps/app_shim/extension_app_shim_handler_mac.h" | 7 #include "apps/app_shim/extension_app_shim_handler_mac.h" |
8 #include "apps/shell_window.h" | 8 #include "apps/shell_window.h" |
9 #include "apps/shell_window_registry.h" | 9 #include "apps/shell_window_registry.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
11 #include "base/strings/utf_string_conversions.h" | |
11 #import "chrome/browser/ui/cocoa/apps/native_app_window_cocoa.h" | 12 #import "chrome/browser/ui/cocoa/apps/native_app_window_cocoa.h" |
12 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
14 #include "grit/generated_resources.h" | |
13 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
14 #include "ui/base/l10n/l10n_util_mac.h" | 16 #include "ui/base/l10n/l10n_util_mac.h" |
15 | 17 |
16 @interface AppMenuController () | 18 @interface AppMenuController () |
17 // Construct the NSMenuItems for apps. | 19 // Construct the NSMenuItems for apps. |
18 - (void)buildAppMenuItems; | 20 - (void)buildAppMenuItems; |
19 // Register for NSWindow notifications. | 21 // Register for NSWindow notifications. |
20 - (void)registerEventHandlers; | 22 - (void)registerEventHandlers; |
21 // If the window is an app window, add or remove menu items. | 23 // If the window is an app window, add or remove menu items. |
22 - (void)windowMainStatusChanged:(NSNotification*)notification; | 24 - (void)windowMainStatusChanged:(NSNotification*)notification; |
23 // Add menu items for an app and hide Chrome menu items. | 25 // Add menu items for an app and hide Chrome menu items. |
24 - (void)addMenuItems:(const extensions::Extension*)app; | 26 - (void)addMenuItems:(const extensions::Extension*)app; |
25 // If the window belongs to the currently focused app, remove the menu items and | 27 // If the window belongs to the currently focused app, remove the menu items and |
26 // unhide Chrome menu items. | 28 // unhide Chrome menu items. |
27 - (void)removeMenuItems:(NSString*)appId; | 29 - (void)removeMenuItems:(NSString*)appId; |
30 - (void)quitCurrentPlatformApp; | |
28 @end | 31 @end |
29 | 32 |
30 @implementation AppMenuController | 33 @implementation AppMenuController |
31 | 34 |
32 - (id)init { | 35 - (id)init { |
33 if ((self = [super init])) { | 36 if ((self = [super init])) { |
34 [self buildAppMenuItems]; | 37 [self buildAppMenuItems]; |
35 [self registerEventHandlers]; | 38 [self registerEventHandlers]; |
36 } | 39 } |
37 return self; | 40 return self; |
38 } | 41 } |
39 | 42 |
40 - (void)dealloc { | 43 - (void)dealloc { |
41 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 44 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
42 [super dealloc]; | 45 [super dealloc]; |
43 } | 46 } |
44 | 47 |
45 - (void)buildAppMenuItems { | 48 - (void)buildAppMenuItems { |
49 quitChromeItem_ = [[[[[NSApp mainMenu] itemAtIndex:0] submenu] itemArray] | |
tapted
2013/08/22 04:40:06
maybe chromeMenuQuitItem_ - keeps it consistent wi
jackhou1
2013/08/22 06:41:01
Done.
| |
50 lastObject]; | |
tapted
2013/08/22 04:40:06
This might be a bit fragile. I think we need to lo
jackhou1
2013/08/22 06:41:01
Done.
| |
51 | |
46 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" | 52 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" |
47 action:nil | 53 action:nil |
48 keyEquivalent:@""]); | 54 keyEquivalent:@""]); |
49 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); | 55 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); |
56 [appMenu setAutoenablesItems:NO]; | |
57 NSMenuItem* appQuitItem = [appMenu addItemWithTitle:@"" | |
tapted
2013/08/22 04:40:06
nit: maybe appMenuQuitItem?
jackhou1
2013/08/22 06:41:01
Done.
| |
58 action:nil | |
59 keyEquivalent:@""]; | |
60 [appQuitItem setTarget:self]; | |
61 [appQuitItem setEnabled:YES]; | |
tapted
2013/08/22 04:40:06
is this line needed?
jackhou1
2013/08/22 06:41:01
Apparently not.
| |
50 [appMenuItem_ setSubmenu:appMenu]; | 62 [appMenuItem_ setSubmenu:appMenu]; |
51 } | 63 } |
52 | 64 |
53 - (void)registerEventHandlers { | 65 - (void)registerEventHandlers { |
54 [[NSNotificationCenter defaultCenter] | 66 [[NSNotificationCenter defaultCenter] |
55 addObserver:self | 67 addObserver:self |
56 selector:@selector(windowMainStatusChanged:) | 68 selector:@selector(windowMainStatusChanged:) |
57 name:NSWindowDidBecomeMainNotification | 69 name:NSWindowDidBecomeMainNotification |
58 object:nil]; | 70 object:nil]; |
59 | 71 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 - (void)addMenuItems:(const extensions::Extension*)app { | 108 - (void)addMenuItems:(const extensions::Extension*)app { |
97 NSString* appId = base::SysUTF8ToNSString(app->id()); | 109 NSString* appId = base::SysUTF8ToNSString(app->id()); |
98 NSString* title = base::SysUTF8ToNSString(app->name()); | 110 NSString* title = base::SysUTF8ToNSString(app->name()); |
99 | 111 |
100 if ([appId_ isEqualToString:appId]) | 112 if ([appId_ isEqualToString:appId]) |
101 return; | 113 return; |
102 | 114 |
103 [self removeMenuItems:appId_]; | 115 [self removeMenuItems:appId_]; |
104 appId_.reset([appId copy]); | 116 appId_.reset([appId copy]); |
105 | 117 |
118 // Hide Chrome menu items. | |
106 NSMenu* mainMenu = [NSApp mainMenu]; | 119 NSMenu* mainMenu = [NSApp mainMenu]; |
107 for (NSMenuItem* item in [mainMenu itemArray]) | 120 for (NSMenuItem* item in [mainMenu itemArray]) |
108 [item setHidden:YES]; | 121 [item setHidden:YES]; |
109 | 122 |
123 // The action on the existing "Quit Chrome" item needs to be changed. | |
124 // This is so that the "Quit <app>" item to the app's menu can have the same | |
125 // keyboard shortcut. | |
126 [quitChromeItem_ setAction:@selector(quitCurrentPlatformApp)]; | |
tapted
2013/08/22 04:40:06
With the current code, can they both stay as termi
jackhou1
2013/08/22 06:41:01
Yeah, I don't really like it either. I was hoping
tapted
2013/08/22 07:20:15
Ahhhh - clicking. What if you just remove the cond
| |
127 | |
128 NSString* quit_localized_string = | |
tapted
2013/08/22 04:40:06
maybe localizedQuitApp ? (should be camel case, si
jackhou1
2013/08/22 06:41:01
Done.
| |
129 l10n_util::GetNSStringF(IDS_EXIT_MAC, base::UTF8ToUTF16(app->name())); | |
130 NSMenuItem* appQuitItem = [[[appMenuItem_ submenu] itemArray] lastObject]; | |
131 [appQuitItem setTitle:quit_localized_string]; | |
132 [appQuitItem setAction:[quitChromeItem_ action]]; | |
133 [appQuitItem setKeyEquivalent:[quitChromeItem_ keyEquivalent]]; | |
tapted
2013/08/22 04:40:06
should you also setKeyEquivalentModifierMask: ?
jackhou1
2013/08/22 06:41:01
Done.
| |
134 | |
110 [appMenuItem_ setTitle:appId]; | 135 [appMenuItem_ setTitle:appId]; |
111 [[appMenuItem_ submenu] setTitle:title]; | 136 [[appMenuItem_ submenu] setTitle:title]; |
112 [mainMenu addItem:appMenuItem_]; | 137 [mainMenu addItem:appMenuItem_]; |
113 } | 138 } |
114 | 139 |
115 - (void)removeMenuItems:(NSString*)appId { | 140 - (void)removeMenuItems:(NSString*)appId { |
116 if (![appId_ isEqualToString:appId]) | 141 if (![appId_ isEqualToString:appId]) |
117 return; | 142 return; |
118 | 143 |
119 appId_.reset(); | 144 appId_.reset(); |
120 | 145 |
121 NSMenu* mainMenu = [NSApp mainMenu]; | 146 NSMenu* mainMenu = [NSApp mainMenu]; |
122 [mainMenu removeItem:appMenuItem_]; | 147 [mainMenu removeItem:appMenuItem_]; |
123 | 148 |
124 // Restore the Chrome main menu bar. | 149 // Restore the Chrome main menu bar. |
125 for (NSMenuItem* item in [mainMenu itemArray]) | 150 for (NSMenuItem* item in [mainMenu itemArray]) |
126 [item setHidden:NO]; | 151 [item setHidden:NO]; |
152 | |
153 [quitChromeItem_ setAction:@selector(terminate:)]; | |
154 } | |
155 | |
156 // If the currently focused window belongs to a platform app, quit the app. | |
tapted
2013/08/22 04:40:06
move this comment to the declaration? (if we keep
jackhou1
2013/08/22 06:41:01
Done.
| |
157 - (void)quitCurrentPlatformApp { | |
158 apps::ShellWindow* shellWindow = | |
159 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( | |
160 [NSApp keyWindow]); | |
161 if (shellWindow) | |
162 apps::ExtensionAppShimHandler::QuitAppForWindow(shellWindow); | |
127 } | 163 } |
128 | 164 |
129 @end | 165 @end |
OLD | NEW |