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_menu_controller_mac.h" | 5 #import "chrome/browser/ui/cocoa/apps/app_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 #import "chrome/browser/ui/cocoa/apps/native_app_window_cocoa.h" | 12 #import "chrome/browser/ui/cocoa/apps/native_app_window_cocoa.h" |
| 12 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
| 14 #include "grit/generated_resources.h" | |
| 13 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 14 #include "ui/base/l10n/l10n_util_mac.h" | 16 #include "ui/base/l10n/l10n_util_mac.h" |
| 15 | 17 |
| 16 @interface AppMenuController () | 18 @interface AppMenuController () |
| 17 // Construct the NSMenuItems for apps. | 19 // Construct the NSMenuItems for apps. |
| 18 - (void)buildAppMenuItems; | 20 - (void)buildAppMenuItems; |
| 19 // Register for NSWindow notifications. | 21 // Register for NSWindow notifications. |
| 20 - (void)registerEventHandlers; | 22 - (void)registerEventHandlers; |
| 21 // If the window is an app window, add or remove menu items. | 23 // If the window is an app window, add or remove menu items. |
| 22 - (void)windowMainStatusChanged:(NSNotification*)notification; | 24 - (void)windowMainStatusChanged:(NSNotification*)notification; |
| 23 // Add menu items for an app and hide Chrome menu items. | 25 // Add menu items for an app and hide Chrome menu items. |
| 24 - (void)addMenuItems:(const extensions::Extension*)app; | 26 - (void)addMenuItems:(const extensions::Extension*)app; |
| 25 // If the window belongs to the currently focused app, remove the menu items and | 27 // If the window belongs to the currently focused app, remove the menu items and |
| 26 // unhide Chrome menu items. | 28 // unhide Chrome menu items. |
| 27 - (void)removeMenuItems:(NSString*)appId; | 29 - (void)removeMenuItems:(NSString*)appId; |
| 30 // If the currently focused window belongs to a platform app, quit the app. | |
| 31 - (void)quitCurrentPlatformApp; | |
| 28 @end | 32 @end |
| 29 | 33 |
| 30 @implementation AppMenuController | 34 @implementation AppMenuController |
| 31 | 35 |
| 32 - (id)init { | 36 - (id)init { |
| 33 if ((self = [super init])) { | 37 if ((self = [super init])) { |
| 34 [self buildAppMenuItems]; | 38 [self buildAppMenuItems]; |
| 35 [self registerEventHandlers]; | 39 [self registerEventHandlers]; |
| 36 } | 40 } |
| 37 return self; | 41 return self; |
| 38 } | 42 } |
| 39 | 43 |
| 40 - (void)dealloc { | 44 - (void)dealloc { |
| 41 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 45 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 42 [super dealloc]; | 46 [super dealloc]; |
| 43 } | 47 } |
| 44 | 48 |
| 45 - (void)buildAppMenuItems { | 49 - (void)buildAppMenuItems { |
| 50 // Find the "Quit Chrome" menu item. | |
| 51 NSMenu* chromeMenu = [[[NSApp mainMenu] itemAtIndex:0] submenu]; | |
| 52 for (NSMenuItem* item in [chromeMenu itemArray]) { | |
| 53 if ([item action] == @selector(terminate:)) { | |
| 54 chromeMenuQuitItem_.reset([item retain]); | |
| 55 break; | |
| 56 } | |
| 57 } | |
| 58 | |
|
tapted
2013/08/22 07:20:15
Hm.. what to do if it's not found. Perhaps DCHECK
jackhou1
2013/08/23 05:31:56
Done.
| |
| 46 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" | 59 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" |
| 47 action:nil | 60 action:nil |
| 48 keyEquivalent:@""]); | 61 keyEquivalent:@""]); |
| 49 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); | 62 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); |
| 63 [appMenu setAutoenablesItems:NO]; | |
| 64 NSMenuItem* appMenuQuitItem = | |
| 65 [appMenu addItemWithTitle:@"" | |
| 66 action:@selector(quitCurrentPlatformApp) | |
| 67 keyEquivalent:@""]; | |
| 68 [appMenuQuitItem setKeyEquivalentModifierMask: | |
| 69 [chromeMenuQuitItem_ keyEquivalentModifierMask]]; | |
| 70 [appMenuQuitItem setTarget:self]; | |
| 50 [appMenuItem_ setSubmenu:appMenu]; | 71 [appMenuItem_ setSubmenu:appMenu]; |
| 51 } | 72 } |
| 52 | 73 |
| 53 - (void)registerEventHandlers { | 74 - (void)registerEventHandlers { |
| 54 [[NSNotificationCenter defaultCenter] | 75 [[NSNotificationCenter defaultCenter] |
| 55 addObserver:self | 76 addObserver:self |
| 56 selector:@selector(windowMainStatusChanged:) | 77 selector:@selector(windowMainStatusChanged:) |
| 57 name:NSWindowDidBecomeMainNotification | 78 name:NSWindowDidBecomeMainNotification |
| 58 object:nil]; | 79 object:nil]; |
| 59 | 80 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 - (void)addMenuItems:(const extensions::Extension*)app { | 117 - (void)addMenuItems:(const extensions::Extension*)app { |
| 97 NSString* appId = base::SysUTF8ToNSString(app->id()); | 118 NSString* appId = base::SysUTF8ToNSString(app->id()); |
| 98 NSString* title = base::SysUTF8ToNSString(app->name()); | 119 NSString* title = base::SysUTF8ToNSString(app->name()); |
| 99 | 120 |
| 100 if ([appId_ isEqualToString:appId]) | 121 if ([appId_ isEqualToString:appId]) |
| 101 return; | 122 return; |
| 102 | 123 |
| 103 [self removeMenuItems:appId_]; | 124 [self removeMenuItems:appId_]; |
| 104 appId_.reset([appId copy]); | 125 appId_.reset([appId copy]); |
| 105 | 126 |
| 127 // Hide Chrome menu items. | |
| 106 NSMenu* mainMenu = [NSApp mainMenu]; | 128 NSMenu* mainMenu = [NSApp mainMenu]; |
| 107 for (NSMenuItem* item in [mainMenu itemArray]) | 129 for (NSMenuItem* item in [mainMenu itemArray]) |
| 108 [item setHidden:YES]; | 130 [item setHidden:YES]; |
| 109 | 131 |
| 132 NSString* localizedQuitApp = | |
| 133 l10n_util::GetNSStringF(IDS_EXIT_MAC, base::UTF8ToUTF16(app->name())); | |
| 134 NSMenuItem* appMenuQuitItem = [[[appMenuItem_ submenu] itemArray] lastObject]; | |
| 135 [appMenuQuitItem setTitle:localizedQuitApp]; | |
| 136 | |
| 137 // It seems that two menu items that have the same key equivalent must also | |
| 138 // have the same action for the keyboard shortcut to work. | |
| 139 // In order to let the appMenuQuitItem have a different action, we 'steal' | |
| 140 // the key equivalent from the chromeMenuQuitItem and restore it later. | |
| 141 [appMenuQuitItem setKeyEquivalent:[chromeMenuQuitItem_ keyEquivalent]]; | |
| 142 [chromeMenuQuitItem_ setKeyEquivalent:@""]; | |
| 143 | |
| 110 [appMenuItem_ setTitle:appId]; | 144 [appMenuItem_ setTitle:appId]; |
| 111 [[appMenuItem_ submenu] setTitle:title]; | 145 [[appMenuItem_ submenu] setTitle:title]; |
| 112 [mainMenu addItem:appMenuItem_]; | 146 [mainMenu addItem:appMenuItem_]; |
| 113 } | 147 } |
| 114 | 148 |
| 115 - (void)removeMenuItems:(NSString*)appId { | 149 - (void)removeMenuItems:(NSString*)appId { |
| 116 if (![appId_ isEqualToString:appId]) | 150 if (![appId_ isEqualToString:appId]) |
| 117 return; | 151 return; |
| 118 | 152 |
| 119 appId_.reset(); | 153 appId_.reset(); |
| 120 | 154 |
| 121 NSMenu* mainMenu = [NSApp mainMenu]; | 155 NSMenu* mainMenu = [NSApp mainMenu]; |
| 122 [mainMenu removeItem:appMenuItem_]; | 156 [mainMenu removeItem:appMenuItem_]; |
| 123 | 157 |
| 124 // Restore the Chrome main menu bar. | 158 // Restore the Chrome main menu bar. |
| 125 for (NSMenuItem* item in [mainMenu itemArray]) | 159 for (NSMenuItem* item in [mainMenu itemArray]) |
| 126 [item setHidden:NO]; | 160 [item setHidden:NO]; |
| 161 | |
| 162 // Restore the keyboard shortcut to Chrome. | |
| 163 NSMenuItem* appMenuQuitItem = [[[appMenuItem_ submenu] itemArray] lastObject]; | |
| 164 [chromeMenuQuitItem_ setKeyEquivalent:[appMenuQuitItem keyEquivalent]]; | |
| 165 [appMenuQuitItem setKeyEquivalent:@""]; | |
| 166 } | |
| 167 | |
| 168 - (void)quitCurrentPlatformApp { | |
| 169 apps::ShellWindow* shellWindow = | |
| 170 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( | |
| 171 [NSApp keyWindow]); | |
| 172 if (shellWindow) | |
| 173 apps::ExtensionAppShimHandler::QuitAppForWindow(shellWindow); | |
| 127 } | 174 } |
| 128 | 175 |
| 129 @end | 176 @end |
| OLD | NEW |