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 #include "base/strings/utf_string_conversions.h" |
| 12 #include "chrome/app/chrome_command_ids.h" |
12 #import "chrome/browser/ui/cocoa/apps/native_app_window_cocoa.h" | 13 #import "chrome/browser/ui/cocoa/apps/native_app_window_cocoa.h" |
13 #include "chrome/common/extensions/extension.h" | 14 #include "chrome/common/extensions/extension.h" |
14 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
15 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
16 #include "ui/base/l10n/l10n_util_mac.h" | 17 #include "ui/base/l10n/l10n_util_mac.h" |
17 | 18 |
| 19 namespace { |
| 20 |
| 21 // Gets an item from the main menu given the tag of the top level item |
| 22 // |menu_tag| and the tag of the item |item_tag|. |
| 23 NSMenuItem* GetItemByTag(NSInteger menu_tag, NSInteger item_tag) { |
| 24 return [[[[NSApp mainMenu] itemWithTag:menu_tag] submenu] |
| 25 itemWithTag:item_tag]; |
| 26 } |
| 27 |
| 28 // Finds a top level menu item using |menu_tag| and creates a new NSMenuItem |
| 29 // with the same title. |
| 30 NSMenuItem* NewTopLevelItemFrom(NSInteger menu_tag) { |
| 31 NSMenuItem* original = [[NSApp mainMenu] itemWithTag:menu_tag]; |
| 32 base::scoped_nsobject<NSMenuItem> item([[NSMenuItem alloc] |
| 33 initWithTitle:[original title] |
| 34 action:nil |
| 35 keyEquivalent:@""]); |
| 36 DCHECK([original hasSubmenu]); |
| 37 base::scoped_nsobject<NSMenu> sub_menu([[NSMenu alloc] |
| 38 initWithTitle:[[original submenu] title]]); |
| 39 [item setSubmenu:sub_menu]; |
| 40 return item.autorelease(); |
| 41 } |
| 42 |
| 43 // Finds an item using |menu_tag| and |item_tag| and adds a duplicate of it to |
| 44 // the submenu of |top_level_item|. |
| 45 void AddDuplicateItem(NSMenuItem* top_level_item, |
| 46 NSInteger menu_tag, |
| 47 NSInteger item_tag) { |
| 48 base::scoped_nsobject<NSMenuItem> item( |
| 49 [GetItemByTag(menu_tag, item_tag) copy]); |
| 50 DCHECK(item); |
| 51 [[top_level_item submenu] addItem:item]; |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
18 @interface AppShimMenuController () | 56 @interface AppShimMenuController () |
19 // Construct the NSMenuItems for apps. | 57 // Construct the NSMenuItems for apps. |
20 - (void)buildAppMenuItems; | 58 - (void)buildAppMenuItems; |
21 // Register for NSWindow notifications. | 59 // Register for NSWindow notifications. |
22 - (void)registerEventHandlers; | 60 - (void)registerEventHandlers; |
23 // If the window is an app window, add or remove menu items. | 61 // If the window is an app window, add or remove menu items. |
24 - (void)windowMainStatusChanged:(NSNotification*)notification; | 62 - (void)windowMainStatusChanged:(NSNotification*)notification; |
25 // Add menu items for an app and hide Chrome menu items. | 63 // Add menu items for an app and hide Chrome menu items. |
26 - (void)addMenuItems:(const extensions::Extension*)app; | 64 - (void)addMenuItems:(const extensions::Extension*)app; |
27 // If the window belongs to the currently focused app, remove the menu items and | 65 // If the window belongs to the currently focused app, remove the menu items and |
(...skipping 13 matching lines...) Expand all Loading... |
41 return self; | 79 return self; |
42 } | 80 } |
43 | 81 |
44 - (void)dealloc { | 82 - (void)dealloc { |
45 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 83 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
46 [super dealloc]; | 84 [super dealloc]; |
47 } | 85 } |
48 | 86 |
49 - (void)buildAppMenuItems { | 87 - (void)buildAppMenuItems { |
50 // Find the "Quit Chrome" menu item. | 88 // Find the "Quit Chrome" menu item. |
51 NSMenu* chromeMenu = [[[NSApp mainMenu] itemAtIndex:0] submenu]; | 89 chromeMenuQuitItem_.reset([GetItemByTag(IDC_CHROME_MENU, IDC_EXIT) retain]); |
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_); | 90 DCHECK(chromeMenuQuitItem_); |
59 | 91 |
| 92 // The app's menu. |
60 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" | 93 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" |
61 action:nil | 94 action:nil |
62 keyEquivalent:@""]); | 95 keyEquivalent:@""]); |
63 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); | 96 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); |
64 [appMenu setAutoenablesItems:NO]; | 97 [appMenu setAutoenablesItems:NO]; |
65 NSMenuItem* appMenuQuitItem = | 98 NSMenuItem* appMenuQuitItem = |
66 [appMenu addItemWithTitle:@"" | 99 [appMenu addItemWithTitle:@"" |
67 action:@selector(quitCurrentPlatformApp) | 100 action:@selector(quitCurrentPlatformApp) |
68 keyEquivalent:@"q"]; | 101 keyEquivalent:@"q"]; |
69 [appMenuQuitItem setKeyEquivalentModifierMask: | 102 [appMenuQuitItem setKeyEquivalentModifierMask: |
70 [chromeMenuQuitItem_ keyEquivalentModifierMask]]; | 103 [chromeMenuQuitItem_ keyEquivalentModifierMask]]; |
71 [appMenuQuitItem setTarget:self]; | 104 [appMenuQuitItem setTarget:self]; |
72 [appMenuItem_ setSubmenu:appMenu]; | 105 [appMenuItem_ setSubmenu:appMenu]; |
| 106 |
| 107 // File menu. |
| 108 fileMenuItem_.reset([NewTopLevelItemFrom(IDC_FILE_MENU) retain]); |
| 109 AddDuplicateItem(fileMenuItem_, IDC_FILE_MENU, IDC_CLOSE_WINDOW); |
| 110 |
| 111 // Edit menu. |
| 112 editMenuItem_.reset([NewTopLevelItemFrom(IDC_EDIT_MENU) retain]); |
| 113 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_UNDO); |
| 114 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_REDO); |
| 115 [[editMenuItem_ submenu] addItem:[NSMenuItem separatorItem]]; |
| 116 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_CUT); |
| 117 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_COPY); |
| 118 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_PASTE); |
| 119 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_DELETE); |
| 120 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_SELECTALL); |
| 121 |
| 122 // Window menu. |
| 123 windowMenuItem_.reset([NewTopLevelItemFrom(IDC_WINDOW_MENU) retain]); |
| 124 AddDuplicateItem(windowMenuItem_, IDC_WINDOW_MENU, IDC_MINIMIZE_WINDOW); |
| 125 AddDuplicateItem(windowMenuItem_, IDC_WINDOW_MENU, IDC_MAXIMIZE_WINDOW); |
| 126 [[windowMenuItem_ submenu] addItem:[NSMenuItem separatorItem]]; |
| 127 AddDuplicateItem(windowMenuItem_, IDC_WINDOW_MENU, IDC_ALL_WINDOWS_FRONT); |
73 } | 128 } |
74 | 129 |
75 - (void)registerEventHandlers { | 130 - (void)registerEventHandlers { |
76 [[NSNotificationCenter defaultCenter] | 131 [[NSNotificationCenter defaultCenter] |
77 addObserver:self | 132 addObserver:self |
78 selector:@selector(windowMainStatusChanged:) | 133 selector:@selector(windowMainStatusChanged:) |
79 name:NSWindowDidBecomeMainNotification | 134 name:NSWindowDidBecomeMainNotification |
80 object:nil]; | 135 object:nil]; |
81 | 136 |
82 [[NSNotificationCenter defaultCenter] | 137 [[NSNotificationCenter defaultCenter] |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 192 |
138 // It seems that two menu items that have the same key equivalent must also | 193 // 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 | 194 // 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). | 195 // original keyboard shortcut, regardless of any overrides set in OSX). |
141 // In order to let the appMenuQuitItem have a different action, we remove the | 196 // In order to let the appMenuQuitItem have a different action, we remove the |
142 // key equivalent from the chromeMenuQuitItem and restore it later. | 197 // key equivalent from the chromeMenuQuitItem and restore it later. |
143 [chromeMenuQuitItem_ setKeyEquivalent:@""]; | 198 [chromeMenuQuitItem_ setKeyEquivalent:@""]; |
144 | 199 |
145 [appMenuItem_ setTitle:appId]; | 200 [appMenuItem_ setTitle:appId]; |
146 [[appMenuItem_ submenu] setTitle:title]; | 201 [[appMenuItem_ submenu] setTitle:title]; |
| 202 |
147 [mainMenu addItem:appMenuItem_]; | 203 [mainMenu addItem:appMenuItem_]; |
| 204 [mainMenu addItem:fileMenuItem_]; |
| 205 [mainMenu addItem:editMenuItem_]; |
| 206 [mainMenu addItem:windowMenuItem_]; |
148 } | 207 } |
149 | 208 |
150 - (void)removeMenuItems:(NSString*)appId { | 209 - (void)removeMenuItems:(NSString*)appId { |
151 if (![appId_ isEqualToString:appId]) | 210 if (![appId_ isEqualToString:appId]) |
152 return; | 211 return; |
153 | 212 |
154 appId_.reset(); | 213 appId_.reset(); |
155 | 214 |
156 NSMenu* mainMenu = [NSApp mainMenu]; | 215 NSMenu* mainMenu = [NSApp mainMenu]; |
157 [mainMenu removeItem:appMenuItem_]; | 216 [mainMenu removeItem:appMenuItem_]; |
| 217 [mainMenu removeItem:fileMenuItem_]; |
| 218 [mainMenu removeItem:editMenuItem_]; |
| 219 [mainMenu removeItem:windowMenuItem_]; |
158 | 220 |
159 // Restore the Chrome main menu bar. | 221 // Restore the Chrome main menu bar. |
160 for (NSMenuItem* item in [mainMenu itemArray]) | 222 for (NSMenuItem* item in [mainMenu itemArray]) |
161 [item setHidden:NO]; | 223 [item setHidden:NO]; |
162 | 224 |
163 // Restore the keyboard shortcut to Chrome. This just needs to be set back to | 225 // 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 | 226 // 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. | 227 // overrides still work as they are based on the title of the menu item. |
166 [chromeMenuQuitItem_ setKeyEquivalent:@"q"]; | 228 [chromeMenuQuitItem_ setKeyEquivalent:@"q"]; |
167 } | 229 } |
168 | 230 |
169 - (void)quitCurrentPlatformApp { | 231 - (void)quitCurrentPlatformApp { |
170 apps::ShellWindow* shellWindow = | 232 apps::ShellWindow* shellWindow = |
171 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( | 233 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( |
172 [NSApp keyWindow]); | 234 [NSApp keyWindow]); |
173 if (shellWindow) | 235 if (shellWindow) |
174 apps::ExtensionAppShimHandler::QuitAppForWindow(shellWindow); | 236 apps::ExtensionAppShimHandler::QuitAppForWindow(shellWindow); |
175 } | 237 } |
176 | 238 |
177 @end | 239 @end |
OLD | NEW |