OLD | NEW |
(Empty) | |
| 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 |
| 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 #import "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 () |
| 17 // Construct the NSMenuItems for apps. |
| 18 - (void)buildAppMenuItems; |
| 19 // Register for NSWindow notifications. |
| 20 - (void)registerEventHandlers; |
| 21 // If the window is an app window, add or remove menu items. |
| 22 - (void)windowMainStatusChanged:(NSNotification*)notification; |
| 23 // Add menu items for an app and hide Chrome menu items. |
| 24 - (void)addMenuItems:(const extensions::Extension*)app; |
| 25 // If the window belongs to the currently focused app, remove the menu items and |
| 26 // unhide Chrome menu items. |
| 27 - (void)removeMenuItems:(NSString*)appId; |
| 28 @end |
| 29 |
| 30 @implementation AppMenuController |
| 31 |
| 32 - (id)init { |
| 33 if ((self = [super init])) { |
| 34 [self buildAppMenuItems]; |
| 35 [self registerEventHandlers]; |
| 36 } |
| 37 return self; |
| 38 } |
| 39 |
| 40 - (void)buildAppMenuItems { |
| 41 appMenuItem_.reset([[NSMenuItem alloc] initWithTitle:@"" |
| 42 action:nil |
| 43 keyEquivalent:@""]); |
| 44 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:@""]); |
| 45 [appMenuItem_ setSubmenu:appMenu]; |
| 46 } |
| 47 |
| 48 - (void)registerEventHandlers { |
| 49 [[NSNotificationCenter defaultCenter] |
| 50 addObserver:self |
| 51 selector:@selector(windowMainStatusChanged:) |
| 52 name:NSWindowDidBecomeMainNotification |
| 53 object:nil]; |
| 54 |
| 55 [[NSNotificationCenter defaultCenter] |
| 56 addObserver:self |
| 57 selector:@selector(windowMainStatusChanged:) |
| 58 name:NSWindowDidResignMainNotification |
| 59 object:nil]; |
| 60 |
| 61 [[NSNotificationCenter defaultCenter] |
| 62 addObserver:self |
| 63 selector:@selector(windowMainStatusChanged:) |
| 64 name:NSWindowWillCloseNotification |
| 65 object:nil]; |
| 66 } |
| 67 |
| 68 - (void)unregisterEventHandlers { |
| 69 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 70 } |
| 71 |
| 72 - (void)windowMainStatusChanged:(NSNotification*)notification { |
| 73 id window = [notification object]; |
| 74 id windowController = [window windowController]; |
| 75 if (![windowController isKindOfClass:[NativeAppWindowController class]]) |
| 76 return; |
| 77 |
| 78 apps::ShellWindow* shellWindow = |
| 79 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( |
| 80 window); |
| 81 if (!shellWindow) |
| 82 return; |
| 83 |
| 84 if ([notification name] == NSWindowDidBecomeMainNotification) { |
| 85 [self addMenuItems:shellWindow->extension()]; |
| 86 } else if ([notification name] == NSWindowDidResignMainNotification || |
| 87 [notification name] == NSWindowWillCloseNotification) { |
| 88 [self removeMenuItems:base::SysUTF8ToNSString(shellWindow->extension_id())]; |
| 89 } else { |
| 90 NOTREACHED(); |
| 91 } |
| 92 } |
| 93 |
| 94 - (void)addMenuItems:(const extensions::Extension*)app { |
| 95 NSString* appId = base::SysUTF8ToNSString(app->id()); |
| 96 NSString* title = base::SysUTF8ToNSString(app->name()); |
| 97 |
| 98 if ([appId_ isEqualToString:appId]) |
| 99 return; |
| 100 |
| 101 [self removeMenuItems:appId_]; |
| 102 appId_.reset([appId retain]); |
| 103 |
| 104 NSMenu* mainMenu = [NSApp mainMenu]; |
| 105 for (NSMenuItem* item in [mainMenu itemArray]) |
| 106 [item setHidden:YES]; |
| 107 |
| 108 [appMenuItem_ setTitle:appId]; |
| 109 [[appMenuItem_ submenu] setTitle:title]; |
| 110 [mainMenu addItem:appMenuItem_]; |
| 111 } |
| 112 |
| 113 - (void)removeMenuItems:(NSString*)appId { |
| 114 if (![appId_ isEqualToString:appId]) |
| 115 return; |
| 116 |
| 117 appId_.reset(); |
| 118 |
| 119 NSMenu* mainMenu = [NSApp mainMenu]; |
| 120 [mainMenu removeItem:appMenuItem_]; |
| 121 |
| 122 // Restore the Chrome main menu bar. |
| 123 for (NSMenuItem* item in [mainMenu itemArray]) |
| 124 [item setHidden:NO]; |
| 125 } |
| 126 |
| 127 @end |
OLD | NEW |