Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/profiles/profiles_state.h" | 9 #include "chrome/browser/profiles/profiles_state.h" |
| 10 #include "chrome/browser/themes/theme_service.h" | 10 #include "chrome/browser/themes/theme_service.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 #include "ui/gfx/image/image_skia_operations.h" | 23 #include "ui/gfx/image/image_skia_operations.h" |
| 24 #include "ui/gfx/image/image_skia_util_mac.h" | 24 #include "ui/gfx/image/image_skia_util_mac.h" |
| 25 #include "ui/gfx/text_elider.h" | 25 #include "ui/gfx/text_elider.h" |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 const CGFloat kButtonPadding = 12; | 29 const CGFloat kButtonPadding = 12; |
| 30 const CGFloat kButtonDefaultPadding = 5; | 30 const CGFloat kButtonDefaultPadding = 5; |
| 31 const CGFloat kButtonHeight = 27; | 31 const CGFloat kButtonHeight = 27; |
| 32 const CGFloat kButtonTitleImageSpacing = 10; | 32 const CGFloat kButtonTitleImageSpacing = 10; |
| 33 const CGFloat kMaxButtonContentWidth = 100; | |
| 34 | 33 |
| 35 const ui::NinePartImageIds kNormalBorderImageIds = | 34 const ui::NinePartImageIds kNormalBorderImageIds = |
| 36 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_NORMAL); | 35 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_NORMAL); |
| 37 const ui::NinePartImageIds kHoverBorderImageIds = | 36 const ui::NinePartImageIds kHoverBorderImageIds = |
| 38 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_HOVER); | 37 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_HOVER); |
| 39 const ui::NinePartImageIds kPressedBorderImageIds = | 38 const ui::NinePartImageIds kPressedBorderImageIds = |
| 40 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_PRESSED); | 39 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_PRESSED); |
| 41 const ui::NinePartImageIds kThemedBorderImageIds = | 40 const ui::NinePartImageIds kThemedBorderImageIds = |
| 42 IMAGE_GRID(IDR_AVATAR_THEMED_MAC_BUTTON_NORMAL); | 41 IMAGE_GRID(IDR_AVATAR_THEMED_MAC_BUTTON_NORMAL); |
| 43 | 42 |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 214 ThemeService* themeService = | 213 ThemeService* themeService = |
| 215 ThemeServiceFactory::GetForProfile(browser_->profile()); | 214 ThemeServiceFactory::GetForProfile(browser_->profile()); |
| 216 BOOL updatedIsThemedWindow = !themeService->UsingSystemTheme(); | 215 BOOL updatedIsThemedWindow = !themeService->UsingSystemTheme(); |
| 217 if (isThemedWindow_ != updatedIsThemedWindow) { | 216 if (isThemedWindow_ != updatedIsThemedWindow) { |
| 218 isThemedWindow_ = updatedIsThemedWindow; | 217 isThemedWindow_ = updatedIsThemedWindow; |
| 219 [[button_ cell] setIsThemedWindow:isThemedWindow_]; | 218 [[button_ cell] setIsThemedWindow:isThemedWindow_]; |
| 220 [self updateAvatarButtonAndLayoutParent:YES]; | 219 [self updateAvatarButtonAndLayoutParent:YES]; |
| 221 } | 220 } |
| 222 } | 221 } |
| 223 | 222 |
| 224 - (base::string16)getElidedAvatarName { | 223 - (NSString*)getButtonText { |
| 225 base::string16 name = profiles::GetAvatarNameForProfile( | 224 return base::SysUTF16ToNSString( |
|
msw
2014/07/17 17:18:35
nit: maybe inline this below.
Marc Treib
2014/07/18 10:05:09
Done.
| |
| 226 browser_->profile()->GetPath()); | 225 profiles::GetAvatarButtonTextForProfile(browser_->profile())); |
| 227 int maxTextWidth = kMaxButtonContentWidth - [[button_ image] size].width; | |
| 228 return gfx::ElideText(name, gfx::FontList(gfx::Font([button_ font])), | |
| 229 maxTextWidth, gfx::ELIDE_TAIL); | |
| 230 } | 226 } |
| 231 | 227 |
| 232 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent { | 228 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent { |
| 233 // The button text has a black foreground and a white drop shadow for regular | 229 // The button text has a black foreground and a white drop shadow for regular |
| 234 // windows, and a light text with a dark drop shadow for guest windows | 230 // windows, and a light text with a dark drop shadow for guest windows |
| 235 // which are themed with a dark background. | 231 // which are themed with a dark background. |
| 236 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); | 232 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); |
| 237 [shadow setShadowOffset:NSMakeSize(0, -1)]; | 233 [shadow setShadowOffset:NSMakeSize(0, -1)]; |
| 238 [shadow setShadowBlurRadius:0]; | 234 [shadow setShadowBlurRadius:0]; |
| 239 | 235 |
| 240 NSColor* foregroundColor; | 236 NSColor* foregroundColor; |
| 241 if (browser_->profile()->IsGuestSession()) { | 237 if (browser_->profile()->IsGuestSession()) { |
| 242 foregroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.9]; | 238 foregroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.9]; |
| 243 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]]; | 239 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]]; |
| 244 } else if (!isThemedWindow_) { | 240 } else if (!isThemedWindow_) { |
| 245 foregroundColor = [NSColor blackColor]; | 241 foregroundColor = [NSColor blackColor]; |
| 246 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.7]]; | 242 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.7]]; |
| 247 } else { | 243 } else { |
| 248 foregroundColor = [NSColor blackColor]; | 244 foregroundColor = [NSColor blackColor]; |
| 249 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.4]]; | 245 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.4]]; |
| 250 } | 246 } |
| 251 | 247 |
| 252 base::string16 profileName = [self getElidedAvatarName]; | 248 NSString* buttonTitle = [self getButtonText]; |
| 253 NSString* buttonTitle = nil; | |
| 254 if (browser_->profile()->IsSupervised()) { | |
| 255 // Add the "supervised" label after eliding the profile name, so the label | |
| 256 // will not get elided, but will instead enlarge the button. | |
| 257 buttonTitle = l10n_util::GetNSStringF(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL, | |
| 258 profileName); | |
| 259 } else { | |
| 260 buttonTitle = base::SysUTF16ToNSString(profileName); | |
| 261 } | |
| 262 | 249 |
| 263 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( | 250 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
| 264 [[NSMutableParagraphStyle alloc] init]); | 251 [[NSMutableParagraphStyle alloc] init]); |
| 265 [paragraphStyle setAlignment:NSLeftTextAlignment]; | 252 [paragraphStyle setAlignment:NSLeftTextAlignment]; |
| 266 | 253 |
| 267 base::scoped_nsobject<NSAttributedString> attributedTitle( | 254 base::scoped_nsobject<NSAttributedString> attributedTitle( |
| 268 [[NSAttributedString alloc] | 255 [[NSAttributedString alloc] |
| 269 initWithString:buttonTitle | 256 initWithString:buttonTitle |
| 270 attributes:@{ NSShadowAttributeName : shadow.get(), | 257 attributes:@{ NSShadowAttributeName : shadow.get(), |
| 271 NSForegroundColorAttributeName : foregroundColor, | 258 NSForegroundColorAttributeName : foregroundColor, |
| 272 NSParagraphStyleAttributeName : paragraphStyle }]); | 259 NSParagraphStyleAttributeName : paragraphStyle }]); |
| 273 [button_ setAttributedTitle:attributedTitle]; | 260 [button_ setAttributedTitle:attributedTitle]; |
| 274 [button_ sizeToFit]; | 261 [button_ sizeToFit]; |
| 275 | 262 |
| 276 if (layoutParent) { | 263 if (layoutParent) { |
| 277 // Because the width of the button might have changed, the parent browser | 264 // Because the width of the button might have changed, the parent browser |
| 278 // frame needs to recalculate the button bounds and redraw it. | 265 // frame needs to recalculate the button bounds and redraw it. |
| 279 [[BrowserWindowController | 266 [[BrowserWindowController |
| 280 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()] | 267 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()] |
| 281 layoutSubviews]; | 268 layoutSubviews]; |
| 282 } | 269 } |
| 283 } | 270 } |
| 284 | 271 |
| 285 - (void)updateErrorStatus:(BOOL)hasError { | 272 - (void)updateErrorStatus:(BOOL)hasError { |
| 286 [[button_ cell] setHasError:hasError]; | 273 [[button_ cell] setHasError:hasError]; |
| 287 [self updateAvatarButtonAndLayoutParent:YES]; | 274 [self updateAvatarButtonAndLayoutParent:YES]; |
| 288 } | 275 } |
| 289 | 276 |
| 290 @end | 277 @end |
| OLD | NEW |