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

Side by Side Diff: chrome/browser/ui/cocoa/profiles/avatar_button_controller.mm

Issue 2636473003: [Mac] Update the Text Color for the Avatar Button (Closed)
Patch Set: Added comment Created 3 years, 10 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "chrome/browser/ui/cocoa/profiles/avatar_button_controller.h" 5 #import "chrome/browser/ui/cocoa/profiles/avatar_button_controller.h"
6 6
7 #include "base/mac/foundation_util.h" 7 #include "base/mac/foundation_util.h"
8 #include "base/strings/sys_string_conversions.h" 8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/browser_process.h" 9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile_attributes_entry.h" 10 #include "chrome/browser/profiles/profile_attributes_entry.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 // NSButtons have a default padding of 5px. Buttons should have a padding of 42 // NSButtons have a default padding of 5px. Buttons should have a padding of
43 // 6px. 43 // 6px.
44 const CGFloat kButtonExtraPadding = 6 - 5; 44 const CGFloat kButtonExtraPadding = 6 - 5;
45 45
46 // Extra padding for the signed out avatar button. 46 // Extra padding for the signed out avatar button.
47 const CGFloat kSignedOutWidthPadding = 2; 47 const CGFloat kSignedOutWidthPadding = 2;
48 48
49 // Kern value for the avatar button title. 49 // Kern value for the avatar button title.
50 const CGFloat kTitleKern = 0.25; 50 const CGFloat kTitleKern = 0.25;
51 51
52 // Upper and lower bounds for determining if the frame's theme color is a
53 // "dark" theme. This value is determined by trial and error.
54 const CGFloat kFrameColorDarkUpperBound = 0.33;
55
52 } // namespace 56 } // namespace
53 57
54 // Button cell with a custom border given by a set of nine-patch image grids. 58 // Button cell with a custom border given by a set of nine-patch image grids.
55 @interface CustomThemeButtonCell : NSButtonCell { 59 @interface CustomThemeButtonCell : NSButtonCell {
56 @private 60 @private
57 BOOL isThemedWindow_; 61 BOOL isThemedWindow_;
58 BOOL hasError_; 62 BOOL hasError_;
59 } 63 }
60 - (void)setIsThemedWindow:(BOOL)isThemedWindow; 64 - (void)setIsThemedWindow:(BOOL)isThemedWindow;
61 - (void)setHasError:(BOOL)hasError withTitle:(NSString*)title; 65 - (void)setHasError:(BOOL)hasError withTitle:(NSString*)title;
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 forAttribute:NSAccessibilityTitleAttribute]; 148 forAttribute:NSAccessibilityTitleAttribute];
145 } 149 }
146 150
147 @end 151 @end
148 152
149 @interface AvatarButtonController (Private) 153 @interface AvatarButtonController (Private)
150 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent; 154 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent;
151 - (void)setErrorStatus:(BOOL)hasError; 155 - (void)setErrorStatus:(BOOL)hasError;
152 - (void)dealloc; 156 - (void)dealloc;
153 - (void)themeDidChangeNotification:(NSNotification*)aNotification; 157 - (void)themeDidChangeNotification:(NSNotification*)aNotification;
158
159 // Called right after |window_| became/resigned the main window.
160 - (void)mainWindowDidChangeNotification:(NSNotification*)aNotification;
154 @end 161 @end
155 162
156 @implementation AvatarButtonController 163 @implementation AvatarButtonController
157 164
158 - (id)initWithBrowser:(Browser*)browser { 165 - (id)initWithBrowser:(Browser*)browser window:(NSWindow*)window {
159 if ((self = [super initWithBrowser:browser])) { 166 if ((self = [super initWithBrowser:browser])) {
160 ThemeService* themeService = 167 ThemeService* themeService =
161 ThemeServiceFactory::GetForProfile(browser->profile()); 168 ThemeServiceFactory::GetForProfile(browser->profile());
162 isThemedWindow_ = !themeService->UsingSystemTheme(); 169 isThemedWindow_ = !themeService->UsingSystemTheme();
163 170
164 AvatarButton* avatarButton = 171 AvatarButton* avatarButton =
165 [[AvatarButton alloc] initWithFrame:NSZeroRect]; 172 [[AvatarButton alloc] initWithFrame:NSZeroRect];
166 button_.reset(avatarButton); 173 button_.reset(avatarButton);
167 174
168 base::scoped_nsobject<NSButtonCell> cell( 175 base::scoped_nsobject<NSButtonCell> cell(
(...skipping 16 matching lines...) Expand all
185 [avatarButton setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin]; 192 [avatarButton setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
186 [avatarButton setTarget:self]; 193 [avatarButton setTarget:self];
187 [avatarButton setAction:@selector(buttonClicked:)]; 194 [avatarButton setAction:@selector(buttonClicked:)];
188 [avatarButton setRightAction:@selector(buttonClicked:)]; 195 [avatarButton setRightAction:@selector(buttonClicked:)];
189 196
190 // Check if the account already has an authentication or sync error and 197 // Check if the account already has an authentication or sync error and
191 // initialize the avatar button UI. 198 // initialize the avatar button UI.
192 hasError_ = profileObserver_->HasAvatarError(); 199 hasError_ = profileObserver_->HasAvatarError();
193 [self updateAvatarButtonAndLayoutParent:NO]; 200 [self updateAvatarButtonAndLayoutParent:NO];
194 201
202 window_ = window;
203
195 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; 204 NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
196 [center addObserver:self 205 [center addObserver:self
197 selector:@selector(themeDidChangeNotification:) 206 selector:@selector(themeDidChangeNotification:)
198 name:kBrowserThemeDidChangeNotification 207 name:kBrowserThemeDidChangeNotification
199 object:nil]; 208 object:nil];
209
210 [center addObserver:self
211 selector:@selector(mainWindowDidChangeNotification:)
212 name:NSWindowDidBecomeMainNotification
213 object:window];
214 [center addObserver:self
215 selector:@selector(mainWindowDidChangeNotification:)
216 name:NSWindowDidResignMainNotification
217 object:window];
200 } 218 }
201 return self; 219 return self;
202 } 220 }
203 221
204 - (void)dealloc { 222 - (void)dealloc {
205 [[NSNotificationCenter defaultCenter] removeObserver:self]; 223 [[NSNotificationCenter defaultCenter] removeObserver:self];
206 [super dealloc]; 224 [super dealloc];
207 } 225 }
208 226
209 - (void)themeDidChangeNotification:(NSNotification*)aNotification { 227 - (void)themeDidChangeNotification:(NSNotification*)aNotification {
210 ThemeService* themeService = 228 ThemeService* themeService =
211 ThemeServiceFactory::GetForProfile(browser_->profile()); 229 ThemeServiceFactory::GetForProfile(browser_->profile());
212 BOOL updatedIsThemedWindow = !themeService->UsingSystemTheme(); 230 BOOL updatedIsThemedWindow = !themeService->UsingSystemTheme();
213 isThemedWindow_ = updatedIsThemedWindow; 231 isThemedWindow_ = updatedIsThemedWindow;
214 [[button_ cell] setIsThemedWindow:isThemedWindow_]; 232 [[button_ cell] setIsThemedWindow:isThemedWindow_];
215 [self updateAvatarButtonAndLayoutParent:YES]; 233 [self updateAvatarButtonAndLayoutParent:YES];
216 } 234 }
217 235
236 - (void)mainWindowDidChangeNotification:(NSNotification*)aNotification {
237 [self updateAvatarButtonAndLayoutParent:NO];
238 }
239
218 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent { 240 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent {
219 // The button text has a black foreground and a white drop shadow for regular 241 // The button text has a black foreground and a white drop shadow for regular
220 // windows, and a light text with a dark drop shadow for guest windows 242 // windows, and a light text with a dark drop shadow for guest windows
221 // which are themed with a dark background. 243 // which are themed with a dark background.
222 NSColor* foregroundColor; 244 NSColor* foregroundColor =
223 const ui::ThemeProvider* theme = 245 [self isFrameColorDark] ? [NSColor whiteColor] : [NSColor blackColor];
224 &ThemeService::GetThemeProviderForProfile(browser_->profile());
225 foregroundColor = theme ? theme->GetNSColor(ThemeProperties::COLOR_TAB_TEXT)
226 : [NSColor blackColor];
227 246
228 ProfileAttributesStorage& storage = 247 ProfileAttributesStorage& storage =
229 g_browser_process->profile_manager()->GetProfileAttributesStorage(); 248 g_browser_process->profile_manager()->GetProfileAttributesStorage();
230 // If there is a single local profile, then use the generic avatar button 249 // If there is a single local profile, then use the generic avatar button
231 // instead of the profile name. Never use the generic button if the active 250 // instead of the profile name. Never use the generic button if the active
232 // profile is Guest. 251 // profile is Guest.
233 bool useGenericButton = 252 bool useGenericButton =
234 !browser_->profile()->IsGuestSession() && 253 !browser_->profile()->IsGuestSession() &&
235 storage.GetNumberOfProfiles() == 1 && 254 storage.GetNumberOfProfiles() == 1 &&
236 !storage.GetAllProfilesAttributes().front()->IsAuthenticated(); 255 !storage.GetAllProfilesAttributes().front()->IsAuthenticated();
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()] 306 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()]
288 layoutSubviews]; 307 layoutSubviews];
289 } 308 }
290 } 309 }
291 310
292 - (void)setErrorStatus:(BOOL)hasError { 311 - (void)setErrorStatus:(BOOL)hasError {
293 hasError_ = hasError; 312 hasError_ = hasError;
294 [self updateAvatarButtonAndLayoutParent:YES]; 313 [self updateAvatarButtonAndLayoutParent:YES];
295 } 314 }
296 315
316 - (BOOL)isFrameColorDark {
317 const ui::ThemeProvider* themeProvider =
318 &ThemeService::GetThemeProviderForProfile(browser_->profile());
319 const int propertyId = [window_ isMainWindow]
320 ? ThemeProperties::COLOR_FRAME
321 : ThemeProperties::COLOR_FRAME_INACTIVE;
322 if (themeProvider && themeProvider->HasCustomColor(propertyId)) {
323 NSColor* frameColor = themeProvider->GetNSColor(propertyId);
324 frameColor =
325 [frameColor colorUsingColorSpaceName:NSCalibratedWhiteColorSpace];
326 return frameColor &&
327 [frameColor whiteComponent] < kFrameColorDarkUpperBound;
328 }
329
330 return false;
331 }
332
297 - (void)showAvatarBubbleAnchoredAt:(NSView*)anchor 333 - (void)showAvatarBubbleAnchoredAt:(NSView*)anchor
298 withMode:(BrowserWindow::AvatarBubbleMode)mode 334 withMode:(BrowserWindow::AvatarBubbleMode)mode
299 withServiceType:(signin::GAIAServiceType)serviceType 335 withServiceType:(signin::GAIAServiceType)serviceType
300 fromAccessPoint:(signin_metrics::AccessPoint)accessPoint { 336 fromAccessPoint:(signin_metrics::AccessPoint)accessPoint {
301 [super showAvatarBubbleAnchoredAt:anchor 337 [super showAvatarBubbleAnchoredAt:anchor
302 withMode:mode 338 withMode:mode
303 withServiceType:serviceType 339 withServiceType:serviceType
304 fromAccessPoint:accessPoint]; 340 fromAccessPoint:accessPoint];
305 341
306 AvatarButton* button = base::mac::ObjCCastStrict<AvatarButton>(button_); 342 AvatarButton* button = base::mac::ObjCCastStrict<AvatarButton>(button_);
307 [button setIsActive:[[menuController_ window] isVisible]]; 343 [button setIsActive:[[menuController_ window] isVisible]];
308 } 344 }
309 345
310 - (void)bubbleWillClose:(NSNotification*)notif { 346 - (void)bubbleWillClose:(NSNotification*)notif {
311 AvatarButton* button = base::mac::ObjCCastStrict<AvatarButton>(button_); 347 AvatarButton* button = base::mac::ObjCCastStrict<AvatarButton>(button_);
312 [button setIsActive:NO]; 348 [button setIsActive:NO];
349 [self updateAvatarButtonAndLayoutParent:NO];
313 [super bubbleWillClose:notif]; 350 [super bubbleWillClose:notif];
314 } 351 }
315 352
316 @end 353 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698