Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8558)

Unified Diff: chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.mm

Issue 23514021: Add Hide to app menu. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.mm
diff --git a/chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.mm b/chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.mm
index bcc272e0029b835baffa84ae0a3c02a9dd97575e..d56914513d9e1a0065e55987464ea08c3a4ff17b 100644
--- a/chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.mm
+++ b/chrome/browser/ui/cocoa/apps/app_shim_menu_controller_mac.mm
@@ -53,6 +53,71 @@ void AddDuplicateItem(NSMenuItem* top_level_item,
} // namespace
+@interface DoppelgangerMenuItem ()
+// Get the source item using the tags and create the menu item.
+- (id)initWithController:(AppShimMenuController*)controller
+ menuTag:(NSInteger)menuTag
+ itemTag:(NSInteger)itemTag
+ resourceId:(int)resourceId
+ action:(SEL)action
+ keyEquivalent:(NSString*)keyEquivalent;
+// Returns the menu item.
tapted 2013/09/03 08:17:25 nit: Maybe declare this as a @property(readonly, n
jackhou1 2013/09/04 01:31:31 Done.
+- (NSMenuItem*)menuItem;
+// Set the title using |resourceId_| and unset the source item's key equivalent.
+- (void)enableForApp:(const extensions::Extension*) app;
tapted 2013/09/03 08:17:25 nit: no space before the last `app`
jackhou1 2013/09/04 01:31:31 Done.
+// Restore the source item's key equivalent.
+- (void)disable;
+@end
+
+@implementation DoppelgangerMenuItem
+
+- (NSMenuItem*)menuItem {
+ return menuItem_;
+}
+
+- (id)initWithController:(AppShimMenuController*)controller
+ menuTag:(NSInteger)menuTag
+ itemTag:(NSInteger)itemTag
+ resourceId:(int)resourceId
+ action:(SEL)action
+ keyEquivalent:(NSString*)keyEquivalent {
+ if ((self = [super init])) {
+ sourceItem_.reset([GetItemByTag(menuTag, itemTag) retain]);
+ DCHECK(sourceItem_);
+ menuItem_.reset([[NSMenuItem alloc]
+ initWithTitle:@""
+ action:action
+ keyEquivalent:keyEquivalent]);
+ [menuItem_ setTarget:controller];
+ [menuItem_ setTag:itemTag];
+ resourceId_ = resourceId;
+ }
+ return self;
+}
+
+- (void)enableForApp:(const extensions::Extension*)app {
+ // It seems that two menu items that have the same key equivalent must also
+ // have the same action for the keyboard shortcut to work. (This refers to the
+ // original keyboard shortcut, regardless of any overrides set in OSX).
+ // In order to let the app menu items have a different action, we remove the
+ // key equivalent of the original items and restore them later.
+ [sourceItem_ setKeyEquivalent:@""];
+ if (!resourceId_)
+ return;
+
+ [menuItem_ setTitle:l10n_util::GetNSStringF(resourceId_,
+ base::UTF8ToUTF16(app->name()))];
+}
+
+- (void)disable {
+ // Restore the keyboard shortcut to Chrome. This just needs to be set back to
+ // the original keyboard shortcut, regardless of any overrides in OSX. The
+ // overrides still work as they are based on the title of the menu item.
+ [sourceItem_ setKeyEquivalent:[menuItem_ keyEquivalent]];
+}
+
+@end
+
@interface AppShimMenuController ()
// Construct the NSMenuItems for apps.
- (void)buildAppMenuItems;
@@ -67,6 +132,8 @@ void AddDuplicateItem(NSMenuItem* top_level_item,
- (void)removeMenuItems:(NSString*)appId;
// If the currently focused window belongs to a platform app, quit the app.
- (void)quitCurrentPlatformApp;
+// If the currently focused window belongs to a platform app, hide the app.
+- (void)hideCurrentPlatformApp;
@end
@implementation AppShimMenuController
@@ -85,24 +152,32 @@ void AddDuplicateItem(NSMenuItem* top_level_item,
}
- (void)buildAppMenuItems {
- // Find the "Quit Chrome" menu item.
- chromeMenuQuitItem_.reset([GetItemByTag(IDC_CHROME_MENU, IDC_EXIT) retain]);
- DCHECK(chromeMenuQuitItem_);
+ hideDoppelganger_.reset([[DoppelgangerMenuItem alloc]
+ initWithController:self
+ menuTag:IDC_CHROME_MENU
+ itemTag:IDC_HIDE_APP
+ resourceId:IDS_HIDE_APP_MAC
+ action:@selector(hideCurrentPlatformApp)
+ keyEquivalent:@"h"]);
+ quitDoppelganger_.reset([[DoppelgangerMenuItem alloc]
+ initWithController:self
+ menuTag:IDC_CHROME_MENU
+ itemTag:IDC_EXIT
+ resourceId:IDS_EXIT_MAC
+ action:@selector(quitCurrentPlatformApp)
+ keyEquivalent:@"q"]);
// The app's menu.
appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@""
action:nil
keyEquivalent:@""]);
base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]);
- [appMenu setAutoenablesItems:NO];
- NSMenuItem* appMenuQuitItem =
- [appMenu addItemWithTitle:@""
- action:@selector(quitCurrentPlatformApp)
- keyEquivalent:@"q"];
- [appMenuQuitItem setKeyEquivalentModifierMask:
- [chromeMenuQuitItem_ keyEquivalentModifierMask]];
- [appMenuQuitItem setTarget:self];
[appMenuItem_ setSubmenu:appMenu];
+ [appMenu setAutoenablesItems:NO];
+
+ [appMenu addItem:[hideDoppelganger_ menuItem]];
+ [appMenu addItem:[NSMenuItem separatorItem]];
+ [appMenu addItem:[quitDoppelganger_ menuItem]];
// File menu.
fileMenuItem_.reset([NewTopLevelItemFrom(IDC_FILE_MENU) retain]);
@@ -185,17 +260,8 @@ void AddDuplicateItem(NSMenuItem* top_level_item,
for (NSMenuItem* item in [mainMenu itemArray])
[item setHidden:YES];
- NSString* localizedQuitApp =
- l10n_util::GetNSStringF(IDS_EXIT_MAC, base::UTF8ToUTF16(app->name()));
- NSMenuItem* appMenuQuitItem = [[[appMenuItem_ submenu] itemArray] lastObject];
- [appMenuQuitItem setTitle:localizedQuitApp];
-
- // It seems that two menu items that have the same key equivalent must also
- // have the same action for the keyboard shortcut to work. (This refers to the
- // original keyboard shortcut, regardless of any overrides set in OSX).
- // In order to let the appMenuQuitItem have a different action, we remove the
- // key equivalent from the chromeMenuQuitItem and restore it later.
- [chromeMenuQuitItem_ setKeyEquivalent:@""];
+ [hideDoppelganger_ enableForApp:app];
+ [quitDoppelganger_ enableForApp:app];
[appMenuItem_ setTitle:appId];
[[appMenuItem_ submenu] setTitle:title];
@@ -222,10 +288,8 @@ void AddDuplicateItem(NSMenuItem* top_level_item,
for (NSMenuItem* item in [mainMenu itemArray])
[item setHidden:NO];
- // Restore the keyboard shortcut to Chrome. This just needs to be set back to
- // the original keyboard shortcut, regardless of any overrides in OSX. The
- // overrides still work as they are based on the title of the menu item.
- [chromeMenuQuitItem_ setKeyEquivalent:@"q"];
+ [hideDoppelganger_ disable];
+ [quitDoppelganger_ disable];
}
- (void)quitCurrentPlatformApp {
@@ -236,4 +300,12 @@ void AddDuplicateItem(NSMenuItem* top_level_item,
apps::ExtensionAppShimHandler::QuitAppForWindow(shellWindow);
}
+- (void)hideCurrentPlatformApp {
+ apps::ShellWindow* shellWindow =
+ apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile(
+ [NSApp keyWindow]);
+ if (shellWindow)
+ apps::ExtensionAppShimHandler::HideAppForWindow(shellWindow);
+}
+
@end

Powered by Google App Engine
This is Rietveld 408576698