Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(542)

Side by Side Diff: chrome/browser/ui/cocoa/apps/app_menu_controller_mac.mm

Issue 22867009: Swap main menu with app-specific menu when app window focused. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Missed something in rebase. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 {
Robert Sesek 2013/08/21 14:21:49 Method order should be: - Any class methods (none
jackhou1 2013/08/21 23:54:48 Done.
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] isEqualToString:NSWindowDidBecomeMainNotification]) {
85 [self addMenuItems:shellWindow->extension()];
86 } else if (
87 [[notification name] isEqualToString:NSWindowDidResignMainNotification] ||
Robert Sesek 2013/08/21 14:21:49 If you pull [notification name] out into a local,
jackhou1 2013/08/21 23:54:48 Done.
88 [[notification name] isEqualToString:NSWindowWillCloseNotification]) {
89 [self removeMenuItems:base::SysUTF8ToNSString(shellWindow->extension_id())];
90 } else {
91 NOTREACHED();
92 }
93 }
94
95 - (void)addMenuItems:(const extensions::Extension*)app {
96 NSString* appId = base::SysUTF8ToNSString(app->id());
97 NSString* title = base::SysUTF8ToNSString(app->name());
98
99 if ([appId_ isEqualToString:appId])
100 return;
101
102 [self removeMenuItems:appId_];
103 appId_.reset([appId retain]);
Robert Sesek 2013/08/21 14:21:49 Strings should be -copy'ed
jackhou1 2013/08/21 23:54:48 Done.
104
105 NSMenu* mainMenu = [NSApp mainMenu];
106 for (NSMenuItem* item in [mainMenu itemArray])
107 [item setHidden:YES];
108
109 [appMenuItem_ setTitle:appId];
110 [[appMenuItem_ submenu] setTitle:title];
111 [mainMenu addItem:appMenuItem_];
112 }
113
114 - (void)removeMenuItems:(NSString*)appId {
115 if (![appId_ isEqualToString:appId])
116 return;
117
118 appId_.reset();
119
120 NSMenu* mainMenu = [NSApp mainMenu];
121 [mainMenu removeItem:appMenuItem_];
122
123 // Restore the Chrome main menu bar.
124 for (NSMenuItem* item in [mainMenu itemArray])
125 [item setHidden:NO];
126 }
127
128 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698