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 "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
16 #include "grit/theme_resources.h" | 16 #include "grit/theme_resources.h" |
17 #import "ui/base/cocoa/appkit_utils.h" | 17 #import "ui/base/cocoa/appkit_utils.h" |
18 #import "ui/base/cocoa/hover_image_button.h" | 18 #import "ui/base/cocoa/hover_image_button.h" |
19 #include "ui/base/l10n/l10n_util_mac.h" | 19 #include "ui/base/l10n/l10n_util_mac.h" |
20 #include "ui/base/nine_image_painter_factory.h" | 20 #include "ui/base/nine_image_painter_factory.h" |
21 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
22 #include "ui/gfx/text_elider.h" | 22 #include "ui/gfx/text_elider.h" |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 const CGFloat kButtonPadding = 12; | 26 const CGFloat kButtonPadding = 12; |
27 const CGFloat kButtonDefaultPadding = 5; | 27 const CGFloat kButtonDefaultPadding = 5; |
28 const CGFloat kButtonHeight = 27; | 28 const CGFloat kButtonHeight = 27; |
29 const CGFloat kButtonTitleImageSpacing = 10; | 29 const CGFloat kButtonTitleImageSpacing = 10; |
30 const CGFloat kMaxButtonWidth = 120; | 30 const CGFloat kMaxButtonContentWidth = 100; |
31 | 31 |
32 const ui::NinePartImageIds kNormalBorderImageIds = | 32 const ui::NinePartImageIds kNormalBorderImageIds = |
33 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_NORMAL); | 33 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_NORMAL); |
34 const ui::NinePartImageIds kHoverBorderImageIds = | 34 const ui::NinePartImageIds kHoverBorderImageIds = |
35 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_HOVER); | 35 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_HOVER); |
36 const ui::NinePartImageIds kPressedBorderImageIds = | 36 const ui::NinePartImageIds kPressedBorderImageIds = |
37 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_PRESSED); | 37 IMAGE_GRID(IDR_AVATAR_MAC_BUTTON_PRESSED); |
38 const ui::NinePartImageIds kThemedBorderImageIds = | 38 const ui::NinePartImageIds kThemedBorderImageIds = |
39 IMAGE_GRID(IDR_AVATAR_THEMED_MAC_BUTTON_NORMAL); | 39 IMAGE_GRID(IDR_AVATAR_THEMED_MAC_BUTTON_NORMAL); |
40 | 40 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
102 imageIds = kHoverBorderImageIds; | 102 imageIds = kHoverBorderImageIds; |
103 ui::DrawNinePartImage(frame, imageIds, NSCompositeSourceOver, 1.0, true); | 103 ui::DrawNinePartImage(frame, imageIds, NSCompositeSourceOver, 1.0, true); |
104 } | 104 } |
105 | 105 |
106 - (void)setIsThemedWindow:(BOOL)isThemedWindow { | 106 - (void)setIsThemedWindow:(BOOL)isThemedWindow { |
107 isThemedWindow_ = isThemedWindow; | 107 isThemedWindow_ = isThemedWindow; |
108 } | 108 } |
109 @end | 109 @end |
110 | 110 |
111 @interface AvatarButtonController (Private) | 111 @interface AvatarButtonController (Private) |
112 - (base::string16)getElidedAvatarName; | |
112 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent; | 113 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent; |
113 - (void)dealloc; | 114 - (void)dealloc; |
114 - (void)themeDidChangeNotification:(NSNotification*)aNotification; | 115 - (void)themeDidChangeNotification:(NSNotification*)aNotification; |
115 @end | 116 @end |
116 | 117 |
117 @implementation AvatarButtonController | 118 @implementation AvatarButtonController |
118 | 119 |
119 - (id)initWithBrowser:(Browser*)browser { | 120 - (id)initWithBrowser:(Browser*)browser { |
120 if ((self = [super initWithBrowser:browser])) { | 121 if ((self = [super initWithBrowser:browser])) { |
121 ThemeService* themeService = | 122 ThemeService* themeService = |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
170 ThemeService* themeService = | 171 ThemeService* themeService = |
171 ThemeServiceFactory::GetForProfile(browser_->profile()); | 172 ThemeServiceFactory::GetForProfile(browser_->profile()); |
172 BOOL updatedIsThemedWindow = !themeService->UsingSystemTheme(); | 173 BOOL updatedIsThemedWindow = !themeService->UsingSystemTheme(); |
173 if (isThemedWindow_ != updatedIsThemedWindow) { | 174 if (isThemedWindow_ != updatedIsThemedWindow) { |
174 isThemedWindow_ = updatedIsThemedWindow; | 175 isThemedWindow_ = updatedIsThemedWindow; |
175 [[button_ cell] setIsThemedWindow:isThemedWindow_]; | 176 [[button_ cell] setIsThemedWindow:isThemedWindow_]; |
176 [self updateAvatarButtonAndLayoutParent:YES]; | 177 [self updateAvatarButtonAndLayoutParent:YES]; |
177 } | 178 } |
178 } | 179 } |
179 | 180 |
181 - (base::string16)getElidedAvatarName { | |
182 base::string16 avatarName = | |
183 profiles::GetAvatarNameForProfile(browser_->profile()); | |
184 int maxTextWidth = kMaxButtonContentWidth - [[button_ image] size].width; | |
msw
2014/05/27 17:16:41
nit: it seems like you're decreasing the width twi
Marc Treib
2014/05/28 11:04:05
Yes: The old 120 pixels were the total width of th
| |
185 return gfx::ElideText(avatarName, | |
msw
2014/05/27 17:16:41
nit: will this fade/truncate and [not] add ellipsi
Marc Treib
2014/05/28 11:04:05
This adds an ellipsis. The button looks identical
| |
186 gfx::FontList(gfx::Font([button_ font])), | |
187 maxTextWidth, | |
188 gfx::ELIDE_AT_END); | |
189 } | |
190 | |
180 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent { | 191 - (void)updateAvatarButtonAndLayoutParent:(BOOL)layoutParent { |
181 // The button text has a black foreground and a white drop shadow for regular | 192 // The button text has a black foreground and a white drop shadow for regular |
182 // windows, and a light text with a dark drop shadow for guest windows | 193 // windows, and a light text with a dark drop shadow for guest windows |
183 // which are themed with a dark background. | 194 // which are themed with a dark background. |
184 // TODO(noms): Figure out something similar for themed windows, if possible. | 195 // TODO(noms): Figure out something similar for themed windows, if possible. |
185 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); | 196 base::scoped_nsobject<NSShadow> shadow([[NSShadow alloc] init]); |
186 [shadow setShadowOffset:NSMakeSize(0, -1)]; | 197 [shadow setShadowOffset:NSMakeSize(0, -1)]; |
187 [shadow setShadowBlurRadius:0]; | 198 [shadow setShadowBlurRadius:0]; |
188 | 199 |
189 NSColor* foregroundColor; | 200 NSColor* foregroundColor; |
190 if (browser_->profile()->IsGuestSession()) { | 201 if (browser_->profile()->IsGuestSession()) { |
191 foregroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.9]; | 202 foregroundColor = [NSColor colorWithCalibratedWhite:1.0 alpha:0.9]; |
192 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]]; | 203 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]]; |
193 } else if (!isThemedWindow_) { | 204 } else if (!isThemedWindow_) { |
194 foregroundColor = [NSColor blackColor]; | 205 foregroundColor = [NSColor blackColor]; |
195 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.7]]; | 206 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.7]]; |
196 } else { | 207 } else { |
197 foregroundColor = [NSColor blackColor]; | 208 foregroundColor = [NSColor blackColor]; |
198 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.4]]; | 209 [shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.4]]; |
199 } | 210 } |
200 | 211 |
212 base::string16 profileName = [self getElidedAvatarName]; | |
213 NSString* buttonTitle = nil; | |
214 if (browser_->profile()->IsManaged()) { | |
215 // Add the "supervised" label after eliding the profile name, so the label | |
216 // will not get elided, but will instead enlarge the button. | |
217 buttonTitle = l10n_util::GetNSStringF(IDS_MANAGED_USER_NEW_AVATAR_LABEL, | |
218 profileName); | |
219 } else { | |
220 buttonTitle = base::SysUTF16ToNSString(profileName); | |
221 } | |
222 | |
201 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( | 223 base::scoped_nsobject<NSMutableParagraphStyle> paragraphStyle( |
202 [[NSMutableParagraphStyle alloc] init]); | 224 [[NSMutableParagraphStyle alloc] init]); |
203 [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail]; | |
204 [paragraphStyle setAlignment:NSLeftTextAlignment]; | 225 [paragraphStyle setAlignment:NSLeftTextAlignment]; |
205 | 226 |
206 NSString* buttonTitle = base::SysUTF16ToNSString( | |
207 profiles::GetAvatarNameForProfile(browser_->profile())); | |
208 | |
209 base::scoped_nsobject<NSAttributedString> attributedTitle( | 227 base::scoped_nsobject<NSAttributedString> attributedTitle( |
210 [[NSAttributedString alloc] | 228 [[NSAttributedString alloc] |
211 initWithString:buttonTitle | 229 initWithString:buttonTitle |
212 attributes:@{ NSShadowAttributeName : shadow.get(), | 230 attributes:@{ NSShadowAttributeName : shadow.get(), |
213 NSForegroundColorAttributeName : foregroundColor, | 231 NSForegroundColorAttributeName : foregroundColor, |
214 NSParagraphStyleAttributeName : paragraphStyle }]); | 232 NSParagraphStyleAttributeName : paragraphStyle }]); |
215 [button_ setAttributedTitle:attributedTitle]; | 233 [button_ setAttributedTitle:attributedTitle]; |
216 [button_ sizeToFit]; | 234 [button_ sizeToFit]; |
217 | 235 |
218 // Truncate the title if needed. | |
219 if (NSWidth([button_ bounds]) > kMaxButtonWidth) | |
220 [button_ setFrameSize:NSMakeSize(kMaxButtonWidth, kButtonHeight)]; | |
221 | |
222 if (layoutParent) { | 236 if (layoutParent) { |
223 // Because the width of the button might have changed, the parent browser | 237 // Because the width of the button might have changed, the parent browser |
224 // frame needs to recalculate the button bounds and redraw it. | 238 // frame needs to recalculate the button bounds and redraw it. |
225 [[BrowserWindowController | 239 [[BrowserWindowController |
226 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()] | 240 browserWindowControllerForWindow:browser_->window()->GetNativeWindow()] |
227 layoutSubviews]; | 241 layoutSubviews]; |
228 } | 242 } |
229 } | 243 } |
230 | 244 |
231 @end | 245 @end |
OLD | NEW |