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 (visible != [menu_item isHidden]) | |
| 92 return; | |
| 93 | |
| 94 if (!has_alternate) { | |
| 95 [menu_item setHidden:!visible]; | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 NSInteger next_index = [submenu indexOfItem:menu_item] + 1; | |
| 100 DCHECK_LT(next_index, [submenu numberOfItems]); | |
| 101 | |
| 102 NSMenuItem* alternate_item = [submenu itemAtIndex:next_index]; | |
| 103 if (visible) { | |
| 104 // Only restore alternate if we've previously hidden the item. | |
| 105 DCHECK([alternate_item isHidden]); | |
| 106 [alternate_item setAlternate:YES]; | |
| 107 } else if (!visible) { | |
| 108 DCHECK(![alternate_item isHidden]); | |
| 109 [alternate_item setAlternate:NO]; | |
|
tapted
2015/02/02 00:05:52
we still want DCHECK([alternate_item isAlternate])
mitchellj
2015/02/02 00:35:51
Right. I think adding the check
if (visible != [m
| |
| 110 } | |
| 111 [alternate_item setHidden:!visible]; | |
| 112 [menu_item setHidden:!visible]; | |
| 113 } | |
| 114 | |
| 78 } // namespace | 115 } // namespace |
| 79 | 116 |
| 80 // Used by AppShimMenuController to manage menu items that are a copy of a | 117 // 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 | 118 // 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 | 119 // 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_| | 120 // 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. | 121 // 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 | 122 // 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 | 123 // 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. | 124 // 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); | 300 AddDuplicateItem(fileMenuItem_, IDC_FILE_MENU, IDC_CLOSE_WINDOW); |
| 264 // Set the expected key equivalent explicitly here because | 301 // Set the expected key equivalent explicitly here because |
| 265 // -[AppControllerMac adjustCloseWindowMenuItemKeyEquivalent:] sets it to | 302 // -[AppControllerMac adjustCloseWindowMenuItemKeyEquivalent:] sets it to |
| 266 // "W" (Cmd+Shift+w) when a tabbed window has focus; it will change it back | 303 // "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. | 304 // to Cmd+w when a non-tabbed window has focus. |
| 268 NSMenuItem* closeWindowMenuItem = | 305 NSMenuItem* closeWindowMenuItem = |
| 269 [[fileMenuItem_ submenu] itemWithTag:IDC_CLOSE_WINDOW]; | 306 [[fileMenuItem_ submenu] itemWithTag:IDC_CLOSE_WINDOW]; |
| 270 [closeWindowMenuItem setKeyEquivalent:@"w"]; | 307 [closeWindowMenuItem setKeyEquivalent:@"w"]; |
| 271 [closeWindowMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask]; | 308 [closeWindowMenuItem setKeyEquivalentModifierMask:NSCommandKeyMask]; |
| 272 | 309 |
| 273 // Edit menu. This copies the menu entirely and removes | 310 // 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, | 311 // 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 | 312 // explicitly. |
| 276 // copy them explicitly. | |
| 277 editMenuItem_.reset([[[NSApp mainMenu] itemWithTag:IDC_EDIT_MENU] copy]); | 313 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 | 314 |
| 282 // View menu. Remove "Always Show Bookmark Bar" and separator. | 315 // View menu. Remove "Always Show Bookmark Bar" and separator. |
| 283 viewMenuItem_.reset([[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] copy]); | 316 viewMenuItem_.reset([[[NSApp mainMenu] itemWithTag:IDC_VIEW_MENU] copy]); |
| 284 RemoveMenuItemWithTag(viewMenuItem_, IDC_SHOW_BOOKMARK_BAR, YES); | 317 RemoveMenuItemWithTag(viewMenuItem_, IDC_SHOW_BOOKMARK_BAR, YES); |
| 285 | 318 |
| 286 // History menu. | 319 // History menu. |
| 287 historyMenuItem_.reset([NewTopLevelItemFrom(IDC_HISTORY_MENU) retain]); | 320 historyMenuItem_.reset([NewTopLevelItemFrom(IDC_HISTORY_MENU) retain]); |
| 288 AddDuplicateItem(historyMenuItem_, IDC_HISTORY_MENU, IDC_BACK); | 321 AddDuplicateItem(historyMenuItem_, IDC_HISTORY_MENU, IDC_BACK); |
| 289 AddDuplicateItem(historyMenuItem_, IDC_HISTORY_MENU, IDC_FORWARD); | 322 AddDuplicateItem(historyMenuItem_, IDC_HISTORY_MENU, IDC_FORWARD); |
| 290 | 323 |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 372 [hideDoppelganger_ enableForApp:app]; | 405 [hideDoppelganger_ enableForApp:app]; |
| 373 [quitDoppelganger_ enableForApp:app]; | 406 [quitDoppelganger_ enableForApp:app]; |
| 374 [newDoppelganger_ enableForApp:app]; | 407 [newDoppelganger_ enableForApp:app]; |
| 375 [openDoppelganger_ enableForApp:app]; | 408 [openDoppelganger_ enableForApp:app]; |
| 376 | 409 |
| 377 [appMenuItem_ setTitle:appId]; | 410 [appMenuItem_ setTitle:appId]; |
| 378 [[appMenuItem_ submenu] setTitle:title]; | 411 [[appMenuItem_ submenu] setTitle:title]; |
| 379 | 412 |
| 380 [mainMenu addItem:appMenuItem_]; | 413 [mainMenu addItem:appMenuItem_]; |
| 381 [mainMenu addItem:fileMenuItem_]; | 414 [mainMenu addItem:fileMenuItem_]; |
| 415 | |
| 416 SetItemWithTagVisible(editMenuItem_, | |
| 417 IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE, | |
| 418 app->is_hosted_app(), true); | |
| 419 SetItemWithTagVisible(editMenuItem_, IDC_FIND_MENU, app->is_hosted_app(), | |
| 420 false); | |
| 382 [mainMenu addItem:editMenuItem_]; | 421 [mainMenu addItem:editMenuItem_]; |
| 422 | |
| 383 if (app->is_hosted_app()) { | 423 if (app->is_hosted_app()) { |
| 384 [mainMenu addItem:viewMenuItem_]; | 424 [mainMenu addItem:viewMenuItem_]; |
| 385 [mainMenu addItem:historyMenuItem_]; | 425 [mainMenu addItem:historyMenuItem_]; |
| 386 } | 426 } |
| 387 [mainMenu addItem:windowMenuItem_]; | 427 [mainMenu addItem:windowMenuItem_]; |
| 388 } | 428 } |
| 389 | 429 |
| 390 - (void)removeMenuItems { | 430 - (void)removeMenuItems { |
| 391 if (!appId_) | 431 if (!appId_) |
| 392 return; | 432 return; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 448 | 488 |
| 449 - (void)focusCurrentPlatformApp { | 489 - (void)focusCurrentPlatformApp { |
| 450 extensions::AppWindow* appWindow = | 490 extensions::AppWindow* appWindow = |
| 451 AppWindowRegistryUtil::GetAppWindowForNativeWindowAnyProfile( | 491 AppWindowRegistryUtil::GetAppWindowForNativeWindowAnyProfile( |
| 452 [NSApp keyWindow]); | 492 [NSApp keyWindow]); |
| 453 if (appWindow) | 493 if (appWindow) |
| 454 apps::ExtensionAppShimHandler::FocusAppForWindow(appWindow); | 494 apps::ExtensionAppShimHandler::FocusAppForWindow(appWindow); |
| 455 } | 495 } |
| 456 | 496 |
| 457 @end | 497 @end |
| OLD | NEW |