Chromium Code Reviews| 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 "base/mac/scoped_nsautorelease_pool.h" | 7 #include "base/mac/scoped_nsautorelease_pool.h" |
| 8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 [submenu removeItemAtIndex:index]; | 68 [submenu removeItemAtIndex:index]; |
| 69 | 69 |
| 70 if (!remove_following_separator || index == [submenu numberOfItems]) | 70 if (!remove_following_separator || index == [submenu numberOfItems]) |
| 71 return; | 71 return; |
| 72 | 72 |
| 73 NSMenuItem* nextItem = [submenu itemAtIndex:index]; | 73 NSMenuItem* nextItem = [submenu itemAtIndex:index]; |
| 74 if ([nextItem isSeparatorItem]) | 74 if ([nextItem isSeparatorItem]) |
| 75 [submenu removeItem:nextItem]; | 75 [submenu removeItem:nextItem]; |
| 76 } | 76 } |
| 77 | 77 |
| 78 // Sets the menu item with |item_tag| in |top_level_item| visible. | |
| 79 // If |has_alternate| is true, the item immediately following |item_tag| is | |
| 80 // assumed to be its (only) alternate. Since AppKit is unable to hide items | |
| 81 // with alternates, |has_alternate| will cause -[NSMenuItem alternate] to be | |
| 82 // removed when hiding and restored when showing. | |
| 83 void SetItemWithTagVisible(NSMenuItem* top_level_item, | |
| 84 NSInteger item_tag, | |
| 85 bool visible, | |
| 86 bool has_alternate) { | |
| 87 NSMenu* submenu = [top_level_item submenu]; | |
| 88 NSMenuItem* menu_item = [submenu itemWithTag:item_tag]; | |
| 89 DCHECK(menu_item); | |
| 90 | |
| 91 if (!has_alternate) { | |
| 92 [menu_item setHidden:!visible]; | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 NSInteger next_index = [submenu indexOfItem:menu_item] + 1; | |
| 97 DCHECK_LT(next_index, [submenu numberOfItems]); | |
| 98 | |
| 99 NSMenuItem* alternate_item = [submenu itemAtIndex:next_index]; | |
| 100 [alternate_item setAlternate:visible]; | |
|
tapted
2015/01/30 05:02:31
hm. since there's something to fix anyway.. I thou
mitchellj
2015/02/01 23:37:53
Done.
| |
| 101 [alternate_item setHidden:!visible]; | |
| 102 [menu_item setHidden:!visible]; | |
| 103 } | |
| 104 | |
| 78 } // namespace | 105 } // namespace |
| 79 | 106 |
| 80 // Used by AppShimMenuController to manage menu items that are a copy of a | 107 // Used by AppShimMenuController to manage menu items that are a copy of a |
| 81 // Chrome menu item but with a different action. This manages unsetting and | 108 // Chrome menu item but with a different action. This manages unsetting and |
| 82 // restoring the original item's key equivalent, so that we can use the same | 109 // restoring the original item's key equivalent, so that we can use the same |
| 83 // key equivalent in the copied item with a different action. If |resourceId_| | 110 // key equivalent in the copied item with a different action. If |resourceId_| |
| 84 // is non-zero, this will also update the title to include the app name. | 111 // is non-zero, this will also update the title to include the app name. |
| 85 // If the copy (menuItem) has no key equivalent, and the title does not have the | 112 // If the copy (menuItem) has no key equivalent, and the title does not have the |
| 86 // app name, then enableForApp and disable do not need to be called. I.e. the | 113 // app name, then enableForApp and disable do not need to be called. I.e. the |
| 87 // doppelganger just copies the item and sets a new action. | 114 // doppelganger just copies the item and sets a new action. |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 263 AddDuplicateItem(fileMenuItem_, IDC_FILE_MENU, IDC_CLOSE_WINDOW); | 290 AddDuplicateItem(fileMenuItem_, IDC_FILE_MENU, IDC_CLOSE_WINDOW); |
| 264 // Set the expected key equivalent explicitly here because | 291 // Set the expected key equivalent explicitly here because |
| 265 // -[AppControllerMac adjustCloseWindowMenuItemKeyEquivalent:] sets it to | 292 // -[AppControllerMac adjustCloseWindowMenuItemKeyEquivalent:] sets it to |
| 266 // "W" (Cmd+Shift+w) when a tabbed window has focus; it will change it back | 293 // "W" (Cmd+Shift+w) when a tabbed window has focus; it will change it back |
| 267 // to Cmd+w when a non-tabbed window has focus. | 294 // to Cmd+w when a non-tabbed window has focus. |
| 268 NSMenuItem* closeWindowMenuItem = | 295 NSMenuItem* closeWindowMenuItem = |
| 269 [[fileMenuItem_ submenu] itemWithTag:IDC_CLOSE_WINDOW]; | 296 [[fileMenuItem_ submenu] itemWithTag:IDC_CLOSE_WINDOW]; |
| 270 [closeWindowMenuItem setKeyEquivalent:@"w"]; | 297 [closeWindowMenuItem setKeyEquivalent:@"w"]; |
| 271 [closeWindowMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask]; | 298 [closeWindowMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask]; |
| 272 | 299 |
| 273 // Edit menu. This copies the menu entirely and removes | 300 // Edit menu. We copy the menu because the last two items, "Start Dictation" |
| 274 // "Paste and Match Style" and "Find". This is because the last two items, | 301 // and "Special Characters" are added by OSX, so we can't copy them |
| 275 // "Start Dictation" and "Special Characters" are added by OSX, so we can't | 302 // explicitly. |
| 276 // copy them explicitly. | |
| 277 editMenuItem_.reset([[[NSApp mainMenu] itemWithTag:IDC_EDIT_MENU] copy]); | 303 editMenuItem_.reset([[[NSApp mainMenu] itemWithTag:IDC_EDIT_MENU] copy]); |
| 278 RemoveMenuItemWithTag(editMenuItem_, | |
| 279 IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE, NO); | |
| 280 RemoveMenuItemWithTag(editMenuItem_, IDC_FIND_MENU, NO); | |
| 281 | 304 |
| 282 // View menu. Remove "Always Show Bookmark Bar" and separator. | 305 // View menu. Remove "Always Show Bookmark Bar" and separator. |
| 283 viewMenuItem_.reset([[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] copy]); | 306 viewMenuItem_.reset([[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] copy]); |
| 284 RemoveMenuItemWithTag(viewMenuItem_, IDC_SHOW_BOOKMARK_BAR, YES); | 307 RemoveMenuItemWithTag(viewMenuItem_, IDC_SHOW_BOOKMARK_BAR, YES); |
| 285 | 308 |
| 286 // History menu. | 309 // History menu. |
| 287 historyMenuItem_.reset([NewTopLevelItemFrom(IDC_HISTORY_MENU) retain]); | 310 historyMenuItem_.reset([NewTopLevelItemFrom(IDC_HISTORY_MENU) retain]); |
| 288 AddDuplicateItem(historyMenuItem_, IDC_HISTORY_MENU, IDC_BACK); | 311 AddDuplicateItem(historyMenuItem_, IDC_HISTORY_MENU, IDC_BACK); |
| 289 AddDuplicateItem(historyMenuItem_, IDC_HISTORY_MENU, IDC_FORWARD); | 312 AddDuplicateItem(historyMenuItem_, IDC_HISTORY_MENU, IDC_FORWARD); |
| 290 | 313 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 [hideDoppelganger_ enableForApp:app]; | 395 [hideDoppelganger_ enableForApp:app]; |
| 373 [quitDoppelganger_ enableForApp:app]; | 396 [quitDoppelganger_ enableForApp:app]; |
| 374 [newDoppelganger_ enableForApp:app]; | 397 [newDoppelganger_ enableForApp:app]; |
| 375 [openDoppelganger_ enableForApp:app]; | 398 [openDoppelganger_ enableForApp:app]; |
| 376 | 399 |
| 377 [appMenuItem_ setTitle:appId]; | 400 [appMenuItem_ setTitle:appId]; |
| 378 [[appMenuItem_ submenu] setTitle:title]; | 401 [[appMenuItem_ submenu] setTitle:title]; |
| 379 | 402 |
| 380 [mainMenu addItem:appMenuItem_]; | 403 [mainMenu addItem:appMenuItem_]; |
| 381 [mainMenu addItem:fileMenuItem_]; | 404 [mainMenu addItem:fileMenuItem_]; |
| 405 | |
| 406 SetItemWithTagVisible(editMenuItem_, | |
| 407 IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE, | |
| 408 app->is_hosted_app(), true); | |
| 409 SetItemWithTagVisible(editMenuItem_, IDC_FIND_MENU, app->is_hosted_app(), | |
| 410 false); | |
| 382 [mainMenu addItem:editMenuItem_]; | 411 [mainMenu addItem:editMenuItem_]; |
| 412 | |
| 383 if (app->is_hosted_app()) { | 413 if (app->is_hosted_app()) { |
| 384 [mainMenu addItem:viewMenuItem_]; | 414 [mainMenu addItem:viewMenuItem_]; |
| 385 [mainMenu addItem:historyMenuItem_]; | 415 [mainMenu addItem:historyMenuItem_]; |
| 386 } | 416 } |
| 387 [mainMenu addItem:windowMenuItem_]; | 417 [mainMenu addItem:windowMenuItem_]; |
| 388 } | 418 } |
| 389 | 419 |
| 390 - (void)removeMenuItems { | 420 - (void)removeMenuItems { |
| 391 if (!appId_) | 421 if (!appId_) |
| 392 return; | 422 return; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 448 | 478 |
| 449 - (void)focusCurrentPlatformApp { | 479 - (void)focusCurrentPlatformApp { |
| 450 extensions::AppWindow* appWindow = | 480 extensions::AppWindow* appWindow = |
| 451 AppWindowRegistryUtil::GetAppWindowForNativeWindowAnyProfile( | 481 AppWindowRegistryUtil::GetAppWindowForNativeWindowAnyProfile( |
| 452 [NSApp keyWindow]); | 482 [NSApp keyWindow]); |
| 453 if (appWindow) | 483 if (appWindow) |
| 454 apps::ExtensionAppShimHandler::FocusAppForWindow(appWindow); | 484 apps::ExtensionAppShimHandler::FocusAppForWindow(appWindow); |
| 455 } | 485 } |
| 456 | 486 |
| 457 @end | 487 @end |
| OLD | NEW |