| 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" |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 - (void)registerEventHandlers; | 60 - (void)registerEventHandlers; |
| 61 // 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. |
| 62 - (void)windowMainStatusChanged:(NSNotification*)notification; | 62 - (void)windowMainStatusChanged:(NSNotification*)notification; |
| 63 // Add menu items for an app and hide Chrome menu items. | 63 // Add menu items for an app and hide Chrome menu items. |
| 64 - (void)addMenuItems:(const extensions::Extension*)app; | 64 - (void)addMenuItems:(const extensions::Extension*)app; |
| 65 // 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 |
| 66 // unhide Chrome menu items. | 66 // unhide Chrome menu items. |
| 67 - (void)removeMenuItems:(NSString*)appId; | 67 - (void)removeMenuItems:(NSString*)appId; |
| 68 // If the currently focused window belongs to a platform app, quit the app. | 68 // If the currently focused window belongs to a platform app, quit the app. |
| 69 - (void)quitCurrentPlatformApp; | 69 - (void)quitCurrentPlatformApp; |
| 70 // If the currently focused window belongs to a platform app, hide the app. |
| 71 - (void)hideCurrentPlatformApp; |
| 70 @end | 72 @end |
| 71 | 73 |
| 72 @implementation AppShimMenuController | 74 @implementation AppShimMenuController |
| 73 | 75 |
| 74 - (id)init { | 76 - (id)init { |
| 75 if ((self = [super init])) { | 77 if ((self = [super init])) { |
| 76 [self buildAppMenuItems]; | 78 [self buildAppMenuItems]; |
| 77 [self registerEventHandlers]; | 79 [self registerEventHandlers]; |
| 78 } | 80 } |
| 79 return self; | 81 return self; |
| 80 } | 82 } |
| 81 | 83 |
| 82 - (void)dealloc { | 84 - (void)dealloc { |
| 83 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 85 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 84 [super dealloc]; | 86 [super dealloc]; |
| 85 } | 87 } |
| 86 | 88 |
| 87 - (void)buildAppMenuItems { | 89 - (void)buildAppMenuItems { |
| 88 // Find the "Quit Chrome" menu item. | 90 chromeMenuHideItem_.reset( |
| 91 [GetItemByTag(IDC_CHROME_MENU, IDC_HIDE_APP) retain]); |
| 92 DCHECK(chromeMenuHideItem_); |
| 89 chromeMenuQuitItem_.reset([GetItemByTag(IDC_CHROME_MENU, IDC_EXIT) retain]); | 93 chromeMenuQuitItem_.reset([GetItemByTag(IDC_CHROME_MENU, IDC_EXIT) retain]); |
| 90 DCHECK(chromeMenuQuitItem_); | 94 DCHECK(chromeMenuQuitItem_); |
| 91 | 95 |
| 92 // The app's menu. | 96 // The app's menu. |
| 93 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" | 97 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" |
| 94 action:nil | 98 action:nil |
| 95 keyEquivalent:@""]); | 99 keyEquivalent:@""]); |
| 96 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); | 100 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); |
| 101 [appMenuItem_ setSubmenu:appMenu]; |
| 97 [appMenu setAutoenablesItems:NO]; | 102 [appMenu setAutoenablesItems:NO]; |
| 103 |
| 104 NSMenuItem* appMenuHideItem = |
| 105 [appMenu addItemWithTitle:@"" |
| 106 action:@selector(hideCurrentPlatformApp) |
| 107 keyEquivalent:@"h"]; |
| 108 [appMenuHideItem setTarget:self]; |
| 109 [appMenuHideItem setTag:IDC_HIDE_APP]; |
| 110 |
| 111 [appMenu addItem:[NSMenuItem separatorItem]]; |
| 112 |
| 98 NSMenuItem* appMenuQuitItem = | 113 NSMenuItem* appMenuQuitItem = |
| 99 [appMenu addItemWithTitle:@"" | 114 [appMenu addItemWithTitle:@"" |
| 100 action:@selector(quitCurrentPlatformApp) | 115 action:@selector(quitCurrentPlatformApp) |
| 101 keyEquivalent:@"q"]; | 116 keyEquivalent:@"q"]; |
| 102 [appMenuQuitItem setKeyEquivalentModifierMask: | |
| 103 [chromeMenuQuitItem_ keyEquivalentModifierMask]]; | |
| 104 [appMenuQuitItem setTarget:self]; | 117 [appMenuQuitItem setTarget:self]; |
| 105 [appMenuItem_ setSubmenu:appMenu]; | 118 [appMenuQuitItem setTag:IDC_EXIT]; |
| 106 | 119 |
| 107 // File menu. | 120 // File menu. |
| 108 fileMenuItem_.reset([NewTopLevelItemFrom(IDC_FILE_MENU) retain]); | 121 fileMenuItem_.reset([NewTopLevelItemFrom(IDC_FILE_MENU) retain]); |
| 109 AddDuplicateItem(fileMenuItem_, IDC_FILE_MENU, IDC_CLOSE_WINDOW); | 122 AddDuplicateItem(fileMenuItem_, IDC_FILE_MENU, IDC_CLOSE_WINDOW); |
| 110 | 123 |
| 111 // Edit menu. | 124 // Edit menu. |
| 112 editMenuItem_.reset([NewTopLevelItemFrom(IDC_EDIT_MENU) retain]); | 125 editMenuItem_.reset([NewTopLevelItemFrom(IDC_EDIT_MENU) retain]); |
| 113 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_UNDO); | 126 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_UNDO); |
| 114 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_REDO); | 127 AddDuplicateItem(editMenuItem_, IDC_EDIT_MENU, IDC_CONTENT_CONTEXT_REDO); |
| 115 [[editMenuItem_ submenu] addItem:[NSMenuItem separatorItem]]; | 128 [[editMenuItem_ submenu] addItem:[NSMenuItem separatorItem]]; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 return; | 191 return; |
| 179 | 192 |
| 180 [self removeMenuItems:appId_]; | 193 [self removeMenuItems:appId_]; |
| 181 appId_.reset([appId copy]); | 194 appId_.reset([appId copy]); |
| 182 | 195 |
| 183 // Hide Chrome menu items. | 196 // Hide Chrome menu items. |
| 184 NSMenu* mainMenu = [NSApp mainMenu]; | 197 NSMenu* mainMenu = [NSApp mainMenu]; |
| 185 for (NSMenuItem* item in [mainMenu itemArray]) | 198 for (NSMenuItem* item in [mainMenu itemArray]) |
| 186 [item setHidden:YES]; | 199 [item setHidden:YES]; |
| 187 | 200 |
| 201 NSString* localizedHideApp = |
| 202 l10n_util::GetNSStringF(IDS_HIDE_APP_MAC, base::UTF8ToUTF16(app->name())); |
| 203 [[[appMenuItem_ submenu] itemWithTag:IDC_HIDE_APP] setTitle:localizedHideApp]; |
| 204 |
| 188 NSString* localizedQuitApp = | 205 NSString* localizedQuitApp = |
| 189 l10n_util::GetNSStringF(IDS_EXIT_MAC, base::UTF8ToUTF16(app->name())); | 206 l10n_util::GetNSStringF(IDS_EXIT_MAC, base::UTF8ToUTF16(app->name())); |
| 190 NSMenuItem* appMenuQuitItem = [[[appMenuItem_ submenu] itemArray] lastObject]; | 207 [[[appMenuItem_ submenu] itemWithTag:IDC_EXIT] setTitle:localizedQuitApp]; |
| 191 [appMenuQuitItem setTitle:localizedQuitApp]; | |
| 192 | 208 |
| 193 // It seems that two menu items that have the same key equivalent must also | 209 // It seems that two menu items that have the same key equivalent must also |
| 194 // have the same action for the keyboard shortcut to work. (This refers to the | 210 // have the same action for the keyboard shortcut to work. (This refers to the |
| 195 // original keyboard shortcut, regardless of any overrides set in OSX). | 211 // original keyboard shortcut, regardless of any overrides set in OSX). |
| 196 // In order to let the appMenuQuitItem have a different action, we remove the | 212 // In order to let the app menu items have a different action, we remove the |
| 197 // key equivalent from the chromeMenuQuitItem and restore it later. | 213 // key equivalent from the chromeMenuQuitItem and restore it later. |
| 214 [chromeMenuHideItem_ setKeyEquivalent:@""]; |
| 198 [chromeMenuQuitItem_ setKeyEquivalent:@""]; | 215 [chromeMenuQuitItem_ setKeyEquivalent:@""]; |
| 199 | 216 |
| 200 [appMenuItem_ setTitle:appId]; | 217 [appMenuItem_ setTitle:appId]; |
| 201 [[appMenuItem_ submenu] setTitle:title]; | 218 [[appMenuItem_ submenu] setTitle:title]; |
| 202 | 219 |
| 203 [mainMenu addItem:appMenuItem_]; | 220 [mainMenu addItem:appMenuItem_]; |
| 204 [mainMenu addItem:fileMenuItem_]; | 221 [mainMenu addItem:fileMenuItem_]; |
| 205 [mainMenu addItem:editMenuItem_]; | 222 [mainMenu addItem:editMenuItem_]; |
| 206 [mainMenu addItem:windowMenuItem_]; | 223 [mainMenu addItem:windowMenuItem_]; |
| 207 } | 224 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 218 [mainMenu removeItem:editMenuItem_]; | 235 [mainMenu removeItem:editMenuItem_]; |
| 219 [mainMenu removeItem:windowMenuItem_]; | 236 [mainMenu removeItem:windowMenuItem_]; |
| 220 | 237 |
| 221 // Restore the Chrome main menu bar. | 238 // Restore the Chrome main menu bar. |
| 222 for (NSMenuItem* item in [mainMenu itemArray]) | 239 for (NSMenuItem* item in [mainMenu itemArray]) |
| 223 [item setHidden:NO]; | 240 [item setHidden:NO]; |
| 224 | 241 |
| 225 // Restore the keyboard shortcut to Chrome. This just needs to be set back to | 242 // Restore the keyboard shortcut to Chrome. This just needs to be set back to |
| 226 // the original keyboard shortcut, regardless of any overrides in OSX. The | 243 // the original keyboard shortcut, regardless of any overrides in OSX. The |
| 227 // overrides still work as they are based on the title of the menu item. | 244 // overrides still work as they are based on the title of the menu item. |
| 245 [chromeMenuHideItem_ setKeyEquivalent:@"h"]; |
| 228 [chromeMenuQuitItem_ setKeyEquivalent:@"q"]; | 246 [chromeMenuQuitItem_ setKeyEquivalent:@"q"]; |
| 229 } | 247 } |
| 230 | 248 |
| 231 - (void)quitCurrentPlatformApp { | 249 - (void)quitCurrentPlatformApp { |
| 232 apps::ShellWindow* shellWindow = | 250 apps::ShellWindow* shellWindow = |
| 233 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( | 251 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( |
| 234 [NSApp keyWindow]); | 252 [NSApp keyWindow]); |
| 235 if (shellWindow) | 253 if (shellWindow) |
| 236 apps::ExtensionAppShimHandler::QuitAppForWindow(shellWindow); | 254 apps::ExtensionAppShimHandler::QuitAppForWindow(shellWindow); |
| 237 } | 255 } |
| 238 | 256 |
| 257 - (void)hideCurrentPlatformApp { |
| 258 apps::ShellWindow* shellWindow = |
| 259 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( |
| 260 [NSApp keyWindow]); |
| 261 if (shellWindow) |
| 262 apps::ExtensionAppShimHandler::HideAppForWindow(shellWindow); |
| 263 } |
| 264 |
| 239 @end | 265 @end |
| OLD | NEW |