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_shim_menu_controller_mac.h" | 5 #import "chrome/browser/ui/cocoa/apps/app_shim_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 AppShimMenuController () | 18 @interface AppShimMenuController () |
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 // If the currently focused window belongs to a platform app, quit the app. |
| 31 - (void)quitCurrentPlatformApp; |
28 @end | 32 @end |
29 | 33 |
30 @implementation AppShimMenuController | 34 @implementation AppShimMenuController |
31 | 35 |
32 - (id)init { | 36 - (id)init { |
33 if ((self = [super init])) { | 37 if ((self = [super init])) { |
34 [self buildAppMenuItems]; | 38 [self buildAppMenuItems]; |
35 [self registerEventHandlers]; | 39 [self registerEventHandlers]; |
36 } | 40 } |
37 return self; | 41 return self; |
38 } | 42 } |
39 | 43 |
40 - (void)dealloc { | 44 - (void)dealloc { |
41 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 45 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
42 [super dealloc]; | 46 [super dealloc]; |
43 } | 47 } |
44 | 48 |
45 - (void)buildAppMenuItems { | 49 - (void)buildAppMenuItems { |
| 50 // Find the "Quit Chrome" menu item. |
| 51 NSMenu* chromeMenu = [[[NSApp mainMenu] itemAtIndex:0] submenu]; |
| 52 for (NSMenuItem* item in [chromeMenu itemArray]) { |
| 53 if ([item action] == @selector(terminate:)) { |
| 54 chromeMenuQuitItem_.reset([item retain]); |
| 55 break; |
| 56 } |
| 57 } |
| 58 DCHECK(chromeMenuQuitItem_); |
| 59 |
46 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" | 60 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" |
47 action:nil | 61 action:nil |
48 keyEquivalent:@""]); | 62 keyEquivalent:@""]); |
49 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); | 63 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); |
| 64 [appMenu setAutoenablesItems:NO]; |
| 65 NSMenuItem* appMenuQuitItem = |
| 66 [appMenu addItemWithTitle:@"" |
| 67 action:@selector(quitCurrentPlatformApp) |
| 68 keyEquivalent:@"q"]; |
| 69 [appMenuQuitItem setKeyEquivalentModifierMask: |
| 70 [chromeMenuQuitItem_ keyEquivalentModifierMask]]; |
| 71 [appMenuQuitItem setTarget:self]; |
50 [appMenuItem_ setSubmenu:appMenu]; | 72 [appMenuItem_ setSubmenu:appMenu]; |
51 } | 73 } |
52 | 74 |
53 - (void)registerEventHandlers { | 75 - (void)registerEventHandlers { |
54 [[NSNotificationCenter defaultCenter] | 76 [[NSNotificationCenter defaultCenter] |
55 addObserver:self | 77 addObserver:self |
56 selector:@selector(windowMainStatusChanged:) | 78 selector:@selector(windowMainStatusChanged:) |
57 name:NSWindowDidBecomeMainNotification | 79 name:NSWindowDidBecomeMainNotification |
58 object:nil]; | 80 object:nil]; |
59 | 81 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 - (void)addMenuItems:(const extensions::Extension*)app { | 118 - (void)addMenuItems:(const extensions::Extension*)app { |
97 NSString* appId = base::SysUTF8ToNSString(app->id()); | 119 NSString* appId = base::SysUTF8ToNSString(app->id()); |
98 NSString* title = base::SysUTF8ToNSString(app->name()); | 120 NSString* title = base::SysUTF8ToNSString(app->name()); |
99 | 121 |
100 if ([appId_ isEqualToString:appId]) | 122 if ([appId_ isEqualToString:appId]) |
101 return; | 123 return; |
102 | 124 |
103 [self removeMenuItems:appId_]; | 125 [self removeMenuItems:appId_]; |
104 appId_.reset([appId copy]); | 126 appId_.reset([appId copy]); |
105 | 127 |
| 128 // Hide Chrome menu items. |
106 NSMenu* mainMenu = [NSApp mainMenu]; | 129 NSMenu* mainMenu = [NSApp mainMenu]; |
107 for (NSMenuItem* item in [mainMenu itemArray]) | 130 for (NSMenuItem* item in [mainMenu itemArray]) |
108 [item setHidden:YES]; | 131 [item setHidden:YES]; |
109 | 132 |
| 133 NSString* localizedQuitApp = |
| 134 l10n_util::GetNSStringF(IDS_EXIT_MAC, base::UTF8ToUTF16(app->name())); |
| 135 NSMenuItem* appMenuQuitItem = [[[appMenuItem_ submenu] itemArray] lastObject]; |
| 136 [appMenuQuitItem setTitle:localizedQuitApp]; |
| 137 |
| 138 // It seems that two menu items that have the same key equivalent must also |
| 139 // have the same action for the keyboard shortcut to work. (This refers to the |
| 140 // original keyboard shortcut, regardless of any overrides set in OSX). |
| 141 // In order to let the appMenuQuitItem have a different action, we remove the |
| 142 // key equivalent from the chromeMenuQuitItem and restore it later. |
| 143 [chromeMenuQuitItem_ setKeyEquivalent:@""]; |
| 144 |
110 [appMenuItem_ setTitle:appId]; | 145 [appMenuItem_ setTitle:appId]; |
111 [[appMenuItem_ submenu] setTitle:title]; | 146 [[appMenuItem_ submenu] setTitle:title]; |
112 [mainMenu addItem:appMenuItem_]; | 147 [mainMenu addItem:appMenuItem_]; |
113 } | 148 } |
114 | 149 |
115 - (void)removeMenuItems:(NSString*)appId { | 150 - (void)removeMenuItems:(NSString*)appId { |
116 if (![appId_ isEqualToString:appId]) | 151 if (![appId_ isEqualToString:appId]) |
117 return; | 152 return; |
118 | 153 |
119 appId_.reset(); | 154 appId_.reset(); |
120 | 155 |
121 NSMenu* mainMenu = [NSApp mainMenu]; | 156 NSMenu* mainMenu = [NSApp mainMenu]; |
122 [mainMenu removeItem:appMenuItem_]; | 157 [mainMenu removeItem:appMenuItem_]; |
123 | 158 |
124 // Restore the Chrome main menu bar. | 159 // Restore the Chrome main menu bar. |
125 for (NSMenuItem* item in [mainMenu itemArray]) | 160 for (NSMenuItem* item in [mainMenu itemArray]) |
126 [item setHidden:NO]; | 161 [item setHidden:NO]; |
| 162 |
| 163 // Restore the keyboard shortcut to Chrome. This just needs to be set back to |
| 164 // the original keyboard shortcut, regardless of any overrides in OSX. The |
| 165 // overrides still work as they are based on the title of the menu item. |
| 166 [chromeMenuQuitItem_ setKeyEquivalent:@"q"]; |
| 167 } |
| 168 |
| 169 - (void)quitCurrentPlatformApp { |
| 170 apps::ShellWindow* shellWindow = |
| 171 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( |
| 172 [NSApp keyWindow]); |
| 173 if (shellWindow) |
| 174 apps::ExtensionAppShimHandler::QuitAppForWindow(shellWindow); |
127 } | 175 } |
128 | 176 |
129 @end | 177 @end |
OLD | NEW |