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" | |
17 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
18 #import "ui/base/cocoa/appkit_utils.h" | 17 #import "ui/base/cocoa/appkit_utils.h" |
19 #import "ui/base/cocoa/hover_image_button.h" | 18 #import "ui/base/cocoa/hover_image_button.h" |
20 #include "ui/base/l10n/l10n_util_mac.h" | |
21 #include "ui/base/nine_image_painter_factory.h" | 19 #include "ui/base/nine_image_painter_factory.h" |
22 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
23 #include "ui/gfx/image/image_skia_operations.h" | 21 #include "ui/gfx/image/image_skia_operations.h" |
24 #include "ui/gfx/image/image_skia_util_mac.h" | 22 #include "ui/gfx/image/image_skia_util_mac.h" |
25 #include "ui/gfx/text_elider.h" | |
26 | 23 |
27 namespace { | 24 namespace { |
28 | 25 |
29 const CGFloat kButtonPadding = 12; | 26 const CGFloat kButtonPadding = 12; |
30 const CGFloat kButtonDefaultPadding = 5; | 27 const CGFloat kButtonDefaultPadding = 5; |
31 const CGFloat kButtonHeight = 27; | 28 const CGFloat kButtonHeight = 27; |
32 const CGFloat kButtonTitleImageSpacing = 10; | 29 const CGFloat kButtonTitleImageSpacing = 10; |
33 const CGFloat kMaxButtonContentWidth = 100; | |
34 | 30 |
35 const ui::NinePartImageIds kNormalBorderImageIds = | 31 const ui::NinePartImageIds kNormalBorderImageIds = |
36 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_NORMAL); | 32 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_NORMAL); |
37 const ui::NinePartImageIds kHoverBorderImageIds = | 33 const ui::NinePartImageIds kHoverBorderImageIds = |
38 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_HOVER); | 34 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_HOVER); |
39 const ui::NinePartImageIds kPressedBorderImageIds = | 35 const ui::NinePartImageIds kPressedBorderImageIds = |
40 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_PRESSED); | 36 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_PRESSED); |
41 const ui::NinePartImageIds kThemedBorderImageIds = | 37 const ui::NinePartImageIds kThemedBorderImageIds = |
42 IMAGE_GRID(IDR_AVATAR_THEMED_MAC_BUTTON_NORMAL); | 38 IMAGE_GRID(IDR_AVATAR_THEMED_MAC_BUTTON_NORMAL); |
43 | 39 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 [ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 137 [ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
142 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToNSImage() retain]); | 138 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToNSImage() retain]); |
143 } else { | 139 } else { |
144 authenticationErrorImage_.reset(); | 140 authenticationErrorImage_.reset(); |
145 } | 141 } |
146 } | 142 } |
147 | 143 |
148 @end | 144 @end |
149 | 145 |
150 @interface AvatarButtonController (Private) | 146 @interface AvatarButtonController (Private) |
151 - (base::string16)getElidedAvatarName; | |
152 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent; | 147 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent; |
153 - (void)updateErrorStatus:(BOOL)hasError; | 148 - (void)updateErrorStatus:(BOOL)hasError; |
154 - (void)dealloc; | 149 - (void)dealloc; |
155 - (void)themeDidChangeNotification:(NSNotification*)aNotification; | 150 - (void)themeDidChangeNotification:(NSNotification*)aNotification; |
156 @end | 151 @end |
157 | 152 |
158 @implementation AvatarButtonController | 153 @implementation AvatarButtonController |
159 | 154 |
160 - (id)initWithBrowser:(Browser*)browser { | 155 - (id)initWithBrowser:(Browser*)browser { |
161 if ((self = [super initWithBrowser:browser])) { | 156 if ((self = [super initWithBrowser:browser])) { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 ThemeService* themeService = | 209 ThemeService* themeService = |
215 ThemeServiceFactory::GetForProfile(browser_->profile()); | 210 ThemeServiceFactory::GetForProfile(browser_->profile()); |
216 BOOL updatedIsThemedWindow = !themeService->UsingSystemTheme(); | 211 BOOL updatedIsThemedWindow = !themeService->UsingSystemTheme(); |
217 if (isThemedWindow_ != updatedIsThemedWindow) { | 212 if (isThemedWindow_ != updatedIsThemedWindow) { |
218 isThemedWindow_ = updatedIsThemedWindow; | 213 isThemedWindow_ = updatedIsThemedWindow; |
219 [[button_ cell] setIsThemedWindow:isThemedWindow_]; | 214 [[button_ cell] setIsThemedWindow:isThemedWindow_]; |
220 [self updateAvatarButtonAndLayoutParent:YES]; | 215 [self updateAvatarButtonAndLayoutParent:YES]; |
221 } | 216 } |
222 } | 217 } |
223 | 218 |
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 { | 219 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent { |
233 // The button text has a black foreground and a white drop shadow for regular | 220 // 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 | 221 // windows, and a light text with a dark drop shadow for guest windows |
235 // which are themed with a dark background. | 222 // which are themed with a dark background. |
236 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); | 223 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); |
237 [shadow setShadowOffset:NSMakeSize(0, -1)]; | 224 [shadow setShadowOffset:NSMakeSize(0, -1)]; |
238 [shadow setShadowBlurRadius:0]; | 225 [shadow setShadowBlurRadius:0]; |
239 | 226 |
240 NSColor* foregroundColor; | 227 NSColor* foregroundColor; |
241 if (browser_->profile()->IsGuestSession()) { | 228 if (browser_->profile()->IsGuestSession()) { |
242 foregroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.9]; | 229 foregroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.9]; |
243 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]]; | 230 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]]; |
244 } else if (!isThemedWindow_) { | 231 } else if (!isThemedWindow_) { |
245 foregroundColor = [NSColor blackColor]; | 232 foregroundColor = [NSColor blackColor]; |
246 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.7]]; | 233 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.7]]; |
247 } else { | 234 } else { |
248 foregroundColor = [NSColor blackColor]; | 235 foregroundColor = [NSColor blackColor]; |
249 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.4]]; | 236 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.4]]; |
250 } | 237 } |
251 | 238 |
252 base::string16 profileName = [self getElidedAvatarName]; | 239 NSString* buttonTitle = base::SysUTF16ToNSString( |
253 NSString* buttonTitle = nil; | 240 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 | 241 |
263 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( | 242 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
264 [[NSMutableParagraphStyle alloc] init]); | 243 [[NSMutableParagraphStyle alloc] init]); |
265 [paragraphStyle setAlignment:NSLeftTextAlignment]; | 244 [paragraphStyle setAlignment:NSLeftTextAlignment]; |
266 | 245 |
267 base::scoped_nsobject<NSAttributedString> attributedTitle( | 246 base::scoped_nsobject<NSAttributedString> attributedTitle( |
268 [[NSAttributedString alloc] | 247 [[NSAttributedString alloc] |
269 initWithString:buttonTitle | 248 initWithString:buttonTitle |
270 attributes:@{ NSShadowAttributeName : shadow.get(), | 249 attributes:@{ NSShadowAttributeName : shadow.get(), |
271 NSForegroundColorAttributeName : foregroundColor, | 250 NSForegroundColorAttributeName : foregroundColor, |
272 NSParagraphStyleAttributeName : paragraphStyle }]); | 251 NSParagraphStyleAttributeName : paragraphStyle }]); |
273 [button_ setAttributedTitle:attributedTitle]; | 252 [button_ setAttributedTitle:attributedTitle]; |
274 [button_ sizeToFit]; | 253 [button_ sizeToFit]; |
275 | 254 |
276 if (layoutParent) { | 255 if (layoutParent) { |
277 // Because the width of the button might have changed, the parent browser | 256 // Because the width of the button might have changed, the parent browser |
278 // frame needs to recalculate the button bounds and redraw it. | 257 // frame needs to recalculate the button bounds and redraw it. |
279 [[BrowserWindowController | 258 [[BrowserWindowController |
280 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()] | 259 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()] |
281 layoutSubviews]; | 260 layoutSubviews]; |
282 } | 261 } |
283 } | 262 } |
284 | 263 |
285 - (void)updateErrorStatus:(BOOL)hasError { | 264 - (void)updateErrorStatus:(BOOL)hasError { |
286 [[button_ cell] setHasError:hasError]; | 265 [[button_ cell] setHasError:hasError]; |
287 [self updateAvatarButtonAndLayoutParent:YES]; | 266 [self updateAvatarButtonAndLayoutParent:YES]; |
288 } | 267 } |
289 | 268 |
290 @end | 269 @end |
OLD | NEW |