| 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..431ef6d7922085a9d8788078a1aba9db8621187f
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/cocoa/apps/app_menu_controller_mac.mm
|
| @@ -0,0 +1,117 @@
|
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved.
|
| +// 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"
|
| +#include "chrome/common/extensions/extension.h"
|
| +#include "ui/base/l10n/l10n_util.h"
|
| +#include "ui/base/l10n/l10n_util_mac.h"
|
| +
|
| +@interface AppMenuController (Private)
|
| +- (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)
|
| + return nil;
|
| +
|
| + appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@""
|
| + action:nil
|
| + keyEquivalent:@""]);
|
| + base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]);
|
| + [appMenuItem_ setSubmenu:appMenu];
|
| +
|
| + [[NSNotificationCenter defaultCenter]
|
| + addObserver:self
|
| + selector:@selector(windowMainStatusChanged:)
|
| + name:NSWindowDidBecomeMainNotification
|
| + object:nil];
|
| +
|
| + [[NSNotificationCenter defaultCenter]
|
| + addObserver:self
|
| + selector:@selector(windowMainStatusChanged:)
|
| + name:NSWindowDidResignMainNotification
|
| + object:nil];
|
| +
|
| + [[NSNotificationCenter defaultCenter]
|
| + addObserver:self
|
| + selector:@selector(windowMainStatusChanged:)
|
| + name:NSWindowWillCloseNotification
|
| + object:nil];
|
| +
|
| + return self;
|
| +}
|
| +
|
| +// If the window is an app window, add or remove menu items.
|
| +- (void)windowMainStatusChanged:(NSNotification*)notification {
|
| + id window = [notification object];
|
| + id windowController = [window windowController];
|
| + if (![windowController isKindOfClass:[NativeAppWindowController class]])
|
| + return;
|
| +
|
| + apps::ShellWindow* shellWindow =
|
| + apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile(
|
| + window);
|
| + if (!shellWindow)
|
| + return;
|
| +
|
| + if ([notification name] == NSWindowDidBecomeMainNotification) {
|
| + [self addMenuItems:shellWindow->extension()];
|
| + } else if ([notification name] == NSWindowDidResignMainNotification ||
|
| + [notification name] == NSWindowWillCloseNotification) {
|
| + [self removeMenuItems:base::SysUTF8ToNSString(shellWindow->extension_id())];
|
| + }
|
| +}
|
| +
|
| +// 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());
|
| +
|
| + NSMenu* mainMenu = [NSApp mainMenu];
|
| +
|
| + if (appId_) {
|
| + if ([appId_ isEqualToString:appId])
|
| + return;
|
| + else
|
| + [self removeMenuItems:appId_];
|
| + }
|
| + [appId retain];
|
| + appId_.reset(appId);
|
| +
|
| + for (NSMenuItem* item in [mainMenu itemArray])
|
| + [item setHidden:YES];
|
| +
|
| + [appMenuItem_ setTitle:appId];
|
| + [[appMenuItem_ submenu] setTitle:title];
|
| + [mainMenu addItem:appMenuItem_];
|
| +}
|
| +
|
| +// 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();
|
| + [mainMenu removeItem:appMenuItem_];
|
| +
|
| + // Restore the Chrome main menu bar.
|
| + for (NSMenuItem* item in [mainMenu itemArray])
|
| + [item setHidden:NO];
|
| +}
|
| +
|
| +@end // @implementation AppMenuController
|
|
|