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

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

Issue 381953002: New avatar button: Consolidate text elision between Mac and Win/Linux (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 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
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
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
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 const gfx::FontList font_list = gfx::FontList(gfx::Font([button_ font]));
226 browser_->profile()->GetPath()); 225 base::string16 name =
227 int maxTextWidth = kMaxButtonContentWidth - [[button_ image] size].width; 226 profiles::GetAvatarButtonTextForProfile(browser_->profile(), font_list);
228 return gfx::ElideText(name, gfx::FontList(gfx::Font([button_ font])), 227 return base::SysUTF16ToNSString(name);
229 maxTextWidth, gfx::ELIDE_TAIL);
230 } 228 }
231 229
232 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent { 230 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent {
233 // The button text has a black foreground and a white drop shadow for regular 231 // 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 232 // windows, and a light text with a dark drop shadow for guest windows
235 // which are themed with a dark background. 233 // which are themed with a dark background.
236 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); 234 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]);
237 [shadow setShadowOffset:NSMakeSize(0, -1)]; 235 [shadow setShadowOffset:NSMakeSize(0, -1)];
238 [shadow setShadowBlurRadius:0]; 236 [shadow setShadowBlurRadius:0];
239 237
240 NSColor* foregroundColor; 238 NSColor* foregroundColor;
241 if (browser_->profile()->IsGuestSession()) { 239 if (browser_->profile()->IsGuestSession()) {
242 foregroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.9]; 240 foregroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.9];
243 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]]; 241 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]];
244 } else if (!isThemedWindow_) { 242 } else if (!isThemedWindow_) {
245 foregroundColor = [NSColor blackColor]; 243 foregroundColor = [NSColor blackColor];
246 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.7]]; 244 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.7]];
247 } else { 245 } else {
248 foregroundColor = [NSColor blackColor]; 246 foregroundColor = [NSColor blackColor];
249 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.4]]; 247 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.4]];
250 } 248 }
251 249
252 base::string16 profileName = [self getElidedAvatarName]; 250 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 251
263 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( 252 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle(
264 [[NSMutableParagraphStyle alloc] init]); 253 [[NSMutableParagraphStyle alloc] init]);
265 [paragraphStyle setAlignment:NSLeftTextAlignment]; 254 [paragraphStyle setAlignment:NSLeftTextAlignment];
266 255
267 base::scoped_nsobject<NSAttributedString> attributedTitle( 256 base::scoped_nsobject<NSAttributedString> attributedTitle(
268 [[NSAttributedString alloc] 257 [[NSAttributedString alloc]
269 initWithString:buttonTitle 258 initWithString:buttonTitle
270 attributes:@{ NSShadowAttributeName : shadow.get(), 259 attributes:@{ NSShadowAttributeName : shadow.get(),
271 NSForegroundColorAttributeName : foregroundColor, 260 NSForegroundColorAttributeName : foregroundColor,
272 NSParagraphStyleAttributeName : paragraphStyle }]); 261 NSParagraphStyleAttributeName : paragraphStyle }]);
273 [button_ setAttributedTitle:attributedTitle]; 262 [button_ setAttributedTitle:attributedTitle];
274 [button_ sizeToFit]; 263 [button_ sizeToFit];
275 264
276 if (layoutParent) { 265 if (layoutParent) {
277 // Because the width of the button might have changed, the parent browser 266 // Because the width of the button might have changed, the parent browser
278 // frame needs to recalculate the button bounds and redraw it. 267 // frame needs to recalculate the button bounds and redraw it.
279 [[BrowserWindowController 268 [[BrowserWindowController
280 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()] 269 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()]
281 layoutSubviews]; 270 layoutSubviews];
282 } 271 }
283 } 272 }
284 273
285 - (void)updateErrorStatus:(BOOL)hasError { 274 - (void)updateErrorStatus:(BOOL)hasError {
286 [[button_ cell] setHasError:hasError]; 275 [[button_ cell] setHasError:hasError];
287 [self updateAvatarButtonAndLayoutParent:YES]; 276 [self updateAvatarButtonAndLayoutParent:YES];
288 } 277 }
289 278
290 @end 279 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698