OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/ui/cocoa/apps/app_menu_controller_mac.h" |
| 6 |
| 7 #include "apps/app_shim/extension_app_shim_handler_mac.h" |
| 8 #include "apps/shell_window.h" |
| 9 #include "apps/shell_window_registry.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "chrome/browser/ui/cocoa/apps/native_app_window_cocoa.h" |
| 12 #include "chrome/common/extensions/extension.h" |
| 13 #include "ui/base/l10n/l10n_util.h" |
| 14 #include "ui/base/l10n/l10n_util_mac.h" |
| 15 |
| 16 @interface AppMenuController (Private) |
| 17 - (void)windowMainStatusChanged:(NSNotification*)notification; |
| 18 - (void)addMenuItems:(const extensions::Extension*)app; |
| 19 - (void)removeMenuItems:(NSString*)appId; |
| 20 @end |
| 21 |
| 22 @implementation AppMenuController |
| 23 |
| 24 - (id)init { |
| 25 self = [super init]; |
| 26 if (self == nil) |
| 27 return nil; |
| 28 |
| 29 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" |
| 30 action:nil |
| 31 keyEquivalent:@""]); |
| 32 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); |
| 33 [appMenuItem_ setSubmenu:appMenu]; |
| 34 |
| 35 [[NSNotificationCenter defaultCenter] |
| 36 addObserver:self |
| 37 selector:@selector(windowMainStatusChanged:) |
| 38 name:NSWindowDidBecomeMainNotification |
| 39 object:nil]; |
| 40 |
| 41 [[NSNotificationCenter defaultCenter] |
| 42 addObserver:self |
| 43 selector:@selector(windowMainStatusChanged:) |
| 44 name:NSWindowDidResignMainNotification |
| 45 object:nil]; |
| 46 |
| 47 [[NSNotificationCenter defaultCenter] |
| 48 addObserver:self |
| 49 selector:@selector(windowMainStatusChanged:) |
| 50 name:NSWindowWillCloseNotification |
| 51 object:nil]; |
| 52 |
| 53 return self; |
| 54 } |
| 55 |
| 56 // If the window is an app window, add or remove menu items. |
| 57 - (void)windowMainStatusChanged:(NSNotification*)notification { |
| 58 id window = [notification object]; |
| 59 id windowController = [window windowController]; |
| 60 if (![windowController isKindOfClass:[NativeAppWindowController class]]) |
| 61 return; |
| 62 |
| 63 apps::ShellWindow* shellWindow = |
| 64 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( |
| 65 window); |
| 66 if (!shellWindow) |
| 67 return; |
| 68 |
| 69 if ([notification name] == NSWindowDidBecomeMainNotification) { |
| 70 [self addMenuItems:shellWindow->extension()]; |
| 71 } else if ([notification name] == NSWindowDidResignMainNotification || |
| 72 [notification name] == NSWindowWillCloseNotification) { |
| 73 [self removeMenuItems:base::SysUTF8ToNSString(shellWindow->extension_id())]; |
| 74 } |
| 75 } |
| 76 |
| 77 // Add menu items for an app and hide Chrome menu items. |
| 78 - (void)addMenuItems:(const extensions::Extension*)app { |
| 79 NSString* appId = base::SysUTF8ToNSString(app->id()); |
| 80 NSString* title = base::SysUTF8ToNSString(app->name()); |
| 81 |
| 82 NSMenu* mainMenu = [NSApp mainMenu]; |
| 83 |
| 84 if (appId_) { |
| 85 if ([appId_ isEqualToString:appId]) |
| 86 return; |
| 87 else |
| 88 [self removeMenuItems:appId_]; |
| 89 } |
| 90 [appId retain]; |
| 91 appId_.reset(appId); |
| 92 |
| 93 for (NSMenuItem* item in [mainMenu itemArray]) |
| 94 [item setHidden:YES]; |
| 95 |
| 96 [appMenuItem_ setTitle:appId]; |
| 97 [[appMenuItem_ submenu] setTitle:title]; |
| 98 [mainMenu addItem:appMenuItem_]; |
| 99 } |
| 100 |
| 101 // If the window belongs to the currently focused app, remove the menu items and |
| 102 // unhide Chrome menu items. |
| 103 - (void)removeMenuItems:(NSString*)appId { |
| 104 if (![appId_ isEqualToString:appId]) |
| 105 return; |
| 106 |
| 107 NSMenu* mainMenu = [NSApp mainMenu]; |
| 108 |
| 109 appId_.reset(); |
| 110 [mainMenu removeItem:appMenuItem_]; |
| 111 |
| 112 // Restore the Chrome main menu bar. |
| 113 for (NSMenuItem* item in [mainMenu itemArray]) |
| 114 [item setHidden:NO]; |
| 115 } |
| 116 |
| 117 @end // @implementation AppMenuController |
OLD | NEW |