OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
tapted
2013/08/15 04:39:23
nit: no (c)
jackhou1
2013/08/16 02:50:41
Done.
| |
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" | |
tapted
2013/08/15 04:39:23
nit: import
jackhou1
2013/08/16 02:50:41
Done.
| |
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) | |
tapted
2013/08/15 04:39:23
I think the new trend is just to use an unnamed ca
jackhou1
2013/08/16 02:50:41
Done.
| |
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) | |
tapted
2013/08/15 04:39:23
Obj-C initializers all follow the pattern
- (id)i
jackhou1
2013/08/16 02:50:41
Done.
| |
27 return nil; | |
28 | |
29 appMenuItems_.reset([[NSMutableArray alloc] initWithCapacity:0]); | |
30 | |
31 [[NSNotificationCenter defaultCenter] | |
32 addObserver:self | |
33 selector:@selector(windowMainStatusChanged:) | |
34 name:NSWindowDidBecomeMainNotification | |
35 object:nil]; | |
36 | |
37 [[NSNotificationCenter defaultCenter] | |
38 addObserver:self | |
39 selector:@selector(windowMainStatusChanged:) | |
40 name:NSWindowDidResignMainNotification | |
41 object:nil]; | |
42 | |
43 return self; | |
44 } | |
45 | |
46 // If the window is an app window, add or remove menu items. | |
tapted
2013/08/15 04:39:23
nit: I think these function comments should be at
jackhou1
2013/08/16 02:50:41
Done.
| |
47 - (void)windowMainStatusChanged:(NSNotification*)notification { | |
48 id window = [notification object]; | |
49 id windowController = [window windowController]; | |
50 if (![windowController isKindOfClass:[NativeAppWindowController class]]) | |
51 return; | |
52 | |
53 apps::ShellWindow* shellWindow = | |
54 apps::ShellWindowRegistry::GetShellWindowForNativeWindowAnyProfile( | |
tapted
2013/08/15 04:39:23
is there any danger this can return NULL for a win
jackhou1
2013/08/16 02:50:41
I don't think so. ShellWindow::Close closes the na
jackhou1
2013/08/19 03:47:57
Changed to use isEqualToString
| |
55 window); | |
56 if (!shellWindow) | |
57 return; | |
58 | |
59 if ([notification name] == NSWindowDidBecomeMainNotification) { | |
tapted
2013/08/15 04:39:23
need to use isEqualToString: here - same below.
jackhou1
2013/08/16 02:50:41
I copied this from app_controller_mac.mm, e.g. lin
| |
60 [self addMenuItems:shellWindow->extension()]; | |
61 } else if ([notification name] == NSWindowDidResignMainNotification) { | |
62 [self removeMenuItems:base::SysUTF8ToNSString(shellWindow->extension_id())]; | |
63 } | |
tapted
2013/08/15 04:39:23
else { NOTREACHED() } ?
And... don't need curlies
jackhou1
2013/08/16 02:50:41
Done.
| |
64 } | |
65 | |
66 // Add menu items for an app and hide Chrome menu items. | |
67 - (void)addMenuItems:(const extensions::Extension*)app { | |
68 NSString* appId = base::SysUTF8ToNSString(app->id()); | |
69 NSString* title = base::SysUTF8ToNSString(app->name()); | |
tapted
2013/08/15 04:39:23
I don't think you need this temporary - you can mo
jackhou1
2013/08/16 02:50:41
There will be other menu items added later that us
| |
70 | |
71 NSMenu* mainMenu = [NSApp mainMenu]; | |
tapted
2013/08/15 04:39:23
nit: move closer to first use
jackhou1
2013/08/16 02:50:41
Done.
| |
72 | |
73 if (appId_) { | |
tapted
2013/08/15 04:39:23
You can lean on Obj-C's "yes you really can call a
jackhou1
2013/08/16 02:50:41
Done.
| |
74 if ([appId_ isEqualToString:appId]) | |
75 return; | |
76 else | |
77 [self removeMenuItems:appId_]; | |
78 } | |
79 [appId retain]; | |
80 appId_.reset(appId); | |
81 | |
82 for (NSMenuItem* item in [mainMenu itemArray]) | |
83 [item setHidden:YES]; | |
84 | |
85 NSMenuItem* item = [mainMenu addItemWithTitle:appId | |
86 action:nil | |
87 keyEquivalent:@""]; | |
88 base::scoped_nsobject<NSMenu> appMenu([[NSMenu alloc] initWithTitle:title]); | |
89 [item setSubmenu:appMenu]; | |
90 [appMenuItems_ addObject:item]; | |
91 } | |
92 | |
93 // If the window belongs to the currently focused app, remove the menu items and | |
94 // unhide Chrome menu items. | |
95 - (void)removeMenuItems:(NSString*)appId { | |
96 if (![appId_ isEqualToString:appId]) | |
97 return; | |
98 | |
99 NSMenu* mainMenu = [NSApp mainMenu]; | |
100 | |
101 appId_.reset(); | |
tapted
2013/08/15 04:39:23
nit: move this up? (i.e. swap with line 99). It's
jackhou1
2013/08/16 02:50:41
Done.
| |
102 for (NSMenuItem* item in appMenuItems_.get()) | |
103 [mainMenu removeItem:item]; | |
104 [appMenuItems_ removeAllObjects]; | |
105 | |
106 // Restore the Chrome main menu bar. | |
107 for (NSMenuItem* item in [mainMenu itemArray]) | |
108 [item setHidden:NO]; | |
109 } | |
110 | |
111 @end // @implementation AppMenuController | |
tapted
2013/08/15 04:39:23
nit: I think current trend is to not have comments
jackhou1
2013/08/16 02:50:41
Done.
| |
OLD | NEW |