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