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" |
| 11 #include "chrome/browser/themes/theme_service_factory.h" | 11 #include "chrome/browser/themes/theme_service_factory.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_window.h" | 13 #include "chrome/browser/ui/browser_window.h" |
| 14 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 14 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 15 #include "components/signin/core/browser/signin_error_controller.h" | 15 #include "components/signin/core/browser/signin_error_controller.h" |
| 16 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
|
msw
2014/07/21 15:26:06
Remove this.
Marc Treib
2014/07/21 16:43:51
Done.
| |
| 17 #include "grit/theme_resources.h" | 17 #include "grit/theme_resources.h" |
| 18 #import "ui/base/cocoa/appkit_utils.h" | 18 #import "ui/base/cocoa/appkit_utils.h" |
| 19 #import "ui/base/cocoa/hover_image_button.h" | 19 #import "ui/base/cocoa/hover_image_button.h" |
| 20 #include "ui/base/l10n/l10n_util_mac.h" | 20 #include "ui/base/l10n/l10n_util_mac.h" |
|
msw
2014/07/21 15:26:07
Remove this.
Marc Treib
2014/07/21 16:43:51
Done.
| |
| 21 #include "ui/base/nine_image_painter_factory.h" | 21 #include "ui/base/nine_image_painter_factory.h" |
| 22 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
| 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" |
|
msw
2014/07/21 15:26:06
Remove this.
Marc Treib
2014/07/21 16:43:51
Done.
| |
| 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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 [ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 140 [ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 142 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToNSImage() retain]); | 141 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToNSImage() retain]); |
| 143 } else { | 142 } else { |
| 144 authenticationErrorImage_.reset(); | 143 authenticationErrorImage_.reset(); |
| 145 } | 144 } |
| 146 } | 145 } |
| 147 | 146 |
| 148 @end | 147 @end |
| 149 | 148 |
| 150 @interface AvatarButtonController (Private) | 149 @interface AvatarButtonController (Private) |
| 151 - (base::string16)getElidedAvatarName; | 150 - (base::string16)getElidedAvatarName; |
|
msw
2014/07/21 15:26:07
Remove this.
Marc Treib
2014/07/21 16:43:51
Done.
| |
| 152 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent; | 151 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent; |
| 153 - (void)updateErrorStatus:(BOOL)hasError; | 152 - (void)updateErrorStatus:(BOOL)hasError; |
| 154 - (void)dealloc; | 153 - (void)dealloc; |
| 155 - (void)themeDidChangeNotification:(NSNotification*)aNotification; | 154 - (void)themeDidChangeNotification:(NSNotification*)aNotification; |
| 156 @end | 155 @end |
| 157 | 156 |
| 158 @implementation AvatarButtonController | 157 @implementation AvatarButtonController |
| 159 | 158 |
| 160 - (id)initWithBrowser:(Browser*)browser { | 159 - (id)initWithBrowser:(Browser*)browser { |
| 161 if ((self = [super initWithBrowser:browser])) { | 160 if ((self = [super initWithBrowser:browser])) { |
| (...skipping 52 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 { | |
| 225 base::string16 name = profiles::GetAvatarNameForProfile( | |
| 226 browser_->profile()->GetPath()); | |
| 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 } | |
| 231 | |
| 232 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent { | 223 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent { |
| 233 // The button text has a black foreground and a white drop shadow for regular | 224 // 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 | 225 // windows, and a light text with a dark drop shadow for guest windows |
| 235 // which are themed with a dark background. | 226 // which are themed with a dark background. |
| 236 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); | 227 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); |
| 237 [shadow setShadowOffset:NSMakeSize(0, -1)]; | 228 [shadow setShadowOffset:NSMakeSize(0, -1)]; |
| 238 [shadow setShadowBlurRadius:0]; | 229 [shadow setShadowBlurRadius:0]; |
| 239 | 230 |
| 240 NSColor* foregroundColor; | 231 NSColor* foregroundColor; |
| 241 if (browser_->profile()->IsGuestSession()) { | 232 if (browser_->profile()->IsGuestSession()) { |
| 242 foregroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.9]; | 233 foregroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.9]; |
| 243 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]]; | 234 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]]; |
| 244 } else if (!isThemedWindow_) { | 235 } else if (!isThemedWindow_) { |
| 245 foregroundColor = [NSColor blackColor]; | 236 foregroundColor = [NSColor blackColor]; |
| 246 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.7]]; | 237 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.7]]; |
| 247 } else { | 238 } else { |
| 248 foregroundColor = [NSColor blackColor]; | 239 foregroundColor = [NSColor blackColor]; |
| 249 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.4]]; | 240 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.4]]; |
| 250 } | 241 } |
| 251 | 242 |
| 252 base::string16 profileName = [self getElidedAvatarName]; | 243 NSString* buttonTitle = base::SysUTF16ToNSString( |
| 253 NSString* buttonTitle = nil; | 244 profiles::GetAvatarButtonTextForProfile(browser_->profile())); |
| 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 | 245 |
| 263 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( | 246 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
| 264 [[NSMutableParagraphStyle alloc] init]); | 247 [[NSMutableParagraphStyle alloc] init]); |
| 265 [paragraphStyle setAlignment:NSLeftTextAlignment]; | 248 [paragraphStyle setAlignment:NSLeftTextAlignment]; |
| 266 | 249 |
| 267 base::scoped_nsobject<NSAttributedString> attributedTitle( | 250 base::scoped_nsobject<NSAttributedString> attributedTitle( |
| 268 [[NSAttributedString alloc] | 251 [[NSAttributedString alloc] |
| 269 initWithString:buttonTitle | 252 initWithString:buttonTitle |
| 270 attributes:@{ NSShadowAttributeName : shadow.get(), | 253 attributes:@{ NSShadowAttributeName : shadow.get(), |
| 271 NSForegroundColorAttributeName : foregroundColor, | 254 NSForegroundColorAttributeName : foregroundColor, |
| 272 NSParagraphStyleAttributeName : paragraphStyle }]); | 255 NSParagraphStyleAttributeName : paragraphStyle }]); |
| 273 [button_ setAttributedTitle:attributedTitle]; | 256 [button_ setAttributedTitle:attributedTitle]; |
| 274 [button_ sizeToFit]; | 257 [button_ sizeToFit]; |
| 275 | 258 |
| 276 if (layoutParent) { | 259 if (layoutParent) { |
| 277 // Because the width of the button might have changed, the parent browser | 260 // Because the width of the button might have changed, the parent browser |
| 278 // frame needs to recalculate the button bounds and redraw it. | 261 // frame needs to recalculate the button bounds and redraw it. |
| 279 [[BrowserWindowController | 262 [[BrowserWindowController |
| 280 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()] | 263 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()] |
| 281 layoutSubviews]; | 264 layoutSubviews]; |
| 282 } | 265 } |
| 283 } | 266 } |
| 284 | 267 |
| 285 - (void)updateErrorStatus:(BOOL)hasError { | 268 - (void)updateErrorStatus:(BOOL)hasError { |
| 286 [[button_ cell] setHasError:hasError]; | 269 [[button_ cell] setHasError:hasError]; |
| 287 [self updateAvatarButtonAndLayoutParent:YES]; | 270 [self updateAvatarButtonAndLayoutParent:YES]; |
| 288 } | 271 } |
| 289 | 272 |
| 290 @end | 273 @end |
| OLD | NEW |