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