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

Side by Side Diff: chrome/browser/ui/cocoa/browser/avatar_base_controller.mm

Issue 117533002: [Mac] Redesign of the avatar menu button (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: now with less incorrect usage of initializers Created 6 years, 11 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 2014 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/browser/avatar_base_controller.h"
6
7 #include "chrome/app/chrome_command_ids.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/profiles/profile_info_cache_observer.h"
10 #include "chrome/browser/profiles/profile_info_util.h"
11 #include "chrome/browser/profiles/profile_manager.h"
12 #include "chrome/browser/profiles/profile_metrics.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/browser_commands.h"
15 #include "chrome/browser/ui/browser_window.h"
16 #import "chrome/browser/ui/cocoa/base_bubble_controller.h"
17 #import "chrome/browser/ui/cocoa/browser/avatar_menu_bubble_controller.h"
18 #import "chrome/browser/ui/cocoa/browser/profile_chooser_controller.h"
19 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
20 #include "chrome/common/profile_management_switches.h"
21 #include "ui/base/resource/resource_bundle.h"
22
23 // Space between the avatar icon and the avatar menu bubble.
24 const CGFloat kMenuYOffsetAdjust = 1.0;
25
26 @interface AvatarBaseController (Private)
27 // Shows the avatar bubble.
28 - (IBAction)buttonClicked:(id)sender;
29 - (void)bubbleWillClose:(NSNotification*)notif;
30 // Updates the profile name displayed by the avatar button. If |layoutParent| is
31 // yes, then the BrowserWindowController is notified to relayout the subviews,
32 // as the button needs to be repositioned.
33 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent;
34 @end
35
36 class ProfileInfoUpdateObserver : public ProfileInfoCacheObserver {
37 public:
38 ProfileInfoUpdateObserver(AvatarBaseController* avatarButton)
39 : avatarButton_(avatarButton) {
40 g_browser_process->profile_manager()->
41 GetProfileInfoCache().AddObserver(this);
42 }
43
44 virtual ~ProfileInfoUpdateObserver() {
45 g_browser_process->profile_manager()->
46 GetProfileInfoCache().RemoveObserver(this);
47 }
48
49 // ProfileInfoCacheObserver:
50 virtual void OnProfileAdded(const base::FilePath& profile_path) OVERRIDE {
51 [avatarButton_ updateAvatarButtonAndLayoutParent:YES];
52 }
53
54 virtual void OnProfileWasRemoved(
55 const base::FilePath& profile_path,
56 const base::string16& profile_name) OVERRIDE {
57 [avatarButton_ updateAvatarButtonAndLayoutParent:YES];
58 }
59
60 virtual void OnProfileNameChanged(
61 const base::FilePath& profile_path,
62 const base::string16& old_profile_name) OVERRIDE {
63 [avatarButton_ updateAvatarButtonAndLayoutParent:YES];
64 }
65
66 virtual void OnProfileAvatarChanged(
67 const base::FilePath& profile_path) OVERRIDE {
68 [avatarButton_ updateAvatarButtonAndLayoutParent:YES];
69 }
70
71 private:
72 AvatarBaseController* avatarButton_; // Weak; owns this.
73
74 DISALLOW_COPY_AND_ASSIGN(ProfileInfoUpdateObserver);
75 };
76
77 @implementation AvatarBaseController
78
79 - (id)initWithBrowser:(Browser*)browser {
80 if ((self = [super init])) {
81 browser_ = browser;
82 profileInfoObserver_.reset(new ProfileInfoUpdateObserver(self));
83 }
84 return self;
85 }
86
87 - (void)dealloc {
88 [[NSNotificationCenter defaultCenter]
89 removeObserver:self
90 name:NSWindowWillCloseNotification
91 object:[menuController_ window]];
92 [super dealloc];
93 }
94
95 - (NSButton*)buttonView {
Nico 2014/01/23 00:51:33 Maybe CHECK(button_get()); // Subclasses must s
noms (inactive) 2014/01/23 18:24:27 Done.
96 return button_.get();
97 }
98
99 - (void)showAvatarBubble:(NSView*)anchor {
100 if (menuController_)
101 return;
102
103 DCHECK(chrome::IsCommandEnabled(browser_, IDC_SHOW_AVATAR_MENU));
104
105 NSWindowController* wc =
106 [browser_->window()->GetNativeWindow() windowController];
107 if ([wc isKindOfClass:[BrowserWindowController class]]) {
108 [static_cast<BrowserWindowController*>(wc)
109 lockBarVisibilityForOwner:self withAnimation:NO delay:NO];
110 }
111
112 NSPoint point = NSMakePoint(NSMidX([anchor bounds]),
113 NSMaxY([anchor bounds]) - kMenuYOffsetAdjust);
114 point = [anchor convertPoint:point toView:nil];
115 point = [[anchor window] convertBaseToScreen:point];
116
117 // |menuController_| will automatically release itself on close.
118 if (switches::IsNewProfileManagement()) {
119 menuController_ =
120 [[ProfileChooserController alloc] initWithBrowser:browser_
121 anchoredAt:point];
122 } else {
123 menuController_ =
124 [[AvatarMenuBubbleController alloc] initWithBrowser:browser_
125 anchoredAt:point];
126 }
127
128 [[NSNotificationCenter defaultCenter]
129 addObserver:self
130 selector:@selector(bubbleWillClose:)
131 name:NSWindowWillCloseNotification
132 object:[menuController_ window]];
133 [menuController_ showWindow:self];
134
135 ProfileMetrics::LogProfileOpenMethod(ProfileMetrics::ICON_AVATAR_BUBBLE);
136 }
137
138 - (IBAction)buttonClicked:(id)sender {
139 DCHECK(sender == button_.get());
Nico 2014/01/23 00:51:33 DCHECK_EQ if it compiles
noms (inactive) 2014/01/23 18:24:27 Done.
140 [self showAvatarBubble:button_];
141 }
142
143 - (void)bubbleWillClose:(NSNotification*)notif {
144 NSWindowController* wc =
145 [browser_->window()->GetNativeWindow() windowController];
146 if ([wc isKindOfClass:[BrowserWindowController class]]) {
147 [static_cast<BrowserWindowController*>(wc)
148 releaseBarVisibilityForOwner:self withAnimation:YES delay:NO];
149 }
150 menuController_ = nil;
151 }
152
153 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent {
154 NOTREACHED();
155 }
156
157 - (BaseBubbleController*)menuController {
158 return menuController_;
159 }
160
161 @end
162
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698