Index: chrome/browser/ui/cocoa/apps/app_menu_controller_mac.mm |
diff --git a/chrome/browser/ui/cocoa/apps/app_menu_controller_mac.mm b/chrome/browser/ui/cocoa/apps/app_menu_controller_mac.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..99a6bae9d0b82f3e634cf8ec9e54510b49c61afe |
--- /dev/null |
+++ b/chrome/browser/ui/cocoa/apps/app_menu_controller_mac.mm |
@@ -0,0 +1,111 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
tapted
2013/08/15 04:39:23
nit: no (c)
jackhou1
2013/08/16 02:50:41
Done.
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "chrome/browser/ui/cocoa/apps/app_menu_controller_mac.h" |
+ |
+#include "apps/app_shim/extension_app_shim_handler_mac.h" |
+#include "apps/shell_window.h" |
+#include "apps/shell_window_registry.h" |
+#include "base/strings/sys_string_conversions.h" |
+#include "chrome/browser/ui/cocoa/apps/native_app_window_cocoa.h" |
tapted
2013/08/15 04:39:23
nit: import
jackhou1
2013/08/16 02:50:41
Done.
|
+#include "chrome/common/extensions/extension.h" |
+#include "ui/base/l10n/l10n_util.h" |
+#include "ui/base/l10n/l10n_util_mac.h" |
+ |
+@interface AppMenuController (Private) |
tapted
2013/08/15 04:39:23
I think the new trend is just to use an unnamed ca
jackhou1
2013/08/16 02:50:41
Done.
|
+- (void)windowMainStatusChanged:(NSNotification*)notification; |
+- (void)addMenuItems:(const extensions::Extension*)app; |
+- (void)removeMenuItems:(NSString*)appId; |
+@end |
+ |
+@implementation AppMenuController |
+ |
+- (id)init { |
+ self = [super init]; |
+ if (self == nil) |
tapted
2013/08/15 04:39:23
Obj-C initializers all follow the pattern
- (id)i
jackhou1
2013/08/16 02:50:41
Done.
|
+ return nil; |
+ |
+ appMenuItems_.reset([[NSMutableArray alloc] initWithCapacity:0]); |
+ |
+ [[NSNotificationCenter defaultCenter] |
+ addObserver:self |
+ selector:@selector(windowMainStatusChanged:) |
+ name:NSWindowDidBecomeMainNotification |
+ object:nil]; |
+ |
+ [[NSNotificationCenter defaultCenter] |
+ addObserver:self |
+ selector:@selector(windowMainStatusChanged:) |
+ name:NSWindowDidResignMainNotification |
+ object:nil]; |
+ |
+ return self; |
+} |
+ |
+// If the window is an app window, add or remove menu items. |
tapted
2013/08/15 04:39:23
nit: I think these function comments should be at
jackhou1
2013/08/16 02:50:41
Done.
|
+- (void)windowMainStatusChanged:(NSNotification*)notification { |
+ id window = [notification object]; |
+ id windowController = [window windowController]; |
+ if (![windowController isKindOfClass:[NativeAppWindowController class]]) |
+ return; |
+ |
+ apps::ShellWindow* shellWindow = |
+ apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( |
tapted
2013/08/15 04:39:23
is there any danger this can return NULL for a win
jackhou1
2013/08/16 02:50:41
I don't think so. ShellWindow::Close closes the na
jackhou1
2013/08/19 03:47:57
Changed to use isEqualToString
|
+ window); |
+ if (!shellWindow) |
+ return; |
+ |
+ if ([notification name] == NSWindowDidBecomeMainNotification) { |
tapted
2013/08/15 04:39:23
need to use isEqualToString: here - same below.
jackhou1
2013/08/16 02:50:41
I copied this from app_controller_mac.mm, e.g. lin
|
+ [self addMenuItems:shellWindow->extension()]; |
+ } else if ([notification name] == NSWindowDidResignMainNotification) { |
+ [self removeMenuItems:base::SysUTF8ToNSString(shellWindow->extension_id())]; |
+ } |
tapted
2013/08/15 04:39:23
else { NOTREACHED() } ?
And... don't need curlies
jackhou1
2013/08/16 02:50:41
Done.
|
+} |
+ |
+// Add menu items for an app and hide Chrome menu items. |
+- (void)addMenuItems:(const extensions::Extension*)app { |
+ NSString* appId = base::SysUTF8ToNSString(app->id()); |
+ NSString* title = base::SysUTF8ToNSString(app->name()); |
tapted
2013/08/15 04:39:23
I don't think you need this temporary - you can mo
jackhou1
2013/08/16 02:50:41
There will be other menu items added later that us
|
+ |
+ NSMenu* mainMenu = [NSApp mainMenu]; |
tapted
2013/08/15 04:39:23
nit: move closer to first use
jackhou1
2013/08/16 02:50:41
Done.
|
+ |
+ if (appId_) { |
tapted
2013/08/15 04:39:23
You can lean on Obj-C's "yes you really can call a
jackhou1
2013/08/16 02:50:41
Done.
|
+ if ([appId_ isEqualToString:appId]) |
+ return; |
+ else |
+ [self removeMenuItems:appId_]; |
+ } |
+ [appId retain]; |
+ appId_.reset(appId); |
+ |
+ for (NSMenuItem* item in [mainMenu itemArray]) |
+ [item setHidden:YES]; |
+ |
+ NSMenuItem* item = [mainMenu addItemWithTitle:appId |
+ action:nil |
+ keyEquivalent:@""]; |
+ base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:title]); |
+ [item setSubmenu:appMenu]; |
+ [appMenuItems_ addObject:item]; |
+} |
+ |
+// If the window belongs to the currently focused app, remove the menu items and |
+// unhide Chrome menu items. |
+- (void)removeMenuItems:(NSString*)appId { |
+ if (![appId_ isEqualToString:appId]) |
+ return; |
+ |
+ NSMenu* mainMenu = [NSApp mainMenu]; |
+ |
+ appId_.reset(); |
tapted
2013/08/15 04:39:23
nit: move this up? (i.e. swap with line 99). It's
jackhou1
2013/08/16 02:50:41
Done.
|
+ for (NSMenuItem* item in appMenuItems_.get()) |
+ [mainMenu removeItem:item]; |
+ [appMenuItems_ removeAllObjects]; |
+ |
+ // Restore the Chrome main menu bar. |
+ for (NSMenuItem* item in [mainMenu itemArray]) |
+ [item setHidden:NO]; |
+} |
+ |
+@end // @implementation AppMenuController |
tapted
2013/08/15 04:39:23
nit: I think current trend is to not have comments
jackhou1
2013/08/16 02:50:41
Done.
|