| 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 #include "chrome/browser/ui/views/profiles/new_avatar_button.h" | 5 #include "chrome/browser/ui/views/profiles/new_avatar_button.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | |
| 8 #include "base/win/windows_version.h" | 7 #include "base/win/windows_version.h" |
| 9 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/profiles/profile_manager.h" | 9 #include "chrome/browser/profiles/profile_manager.h" |
| 11 #include "chrome/browser/profiles/profiles_state.h" | 10 #include "chrome/browser/profiles/profiles_state.h" |
| 12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | |
| 13 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
| 14 #include "components/signin/core/browser/profile_oauth2_token_service.h" | |
| 15 #include "grit/generated_resources.h" | |
| 16 #include "grit/theme_resources.h" | 12 #include "grit/theme_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | |
| 18 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 20 #include "ui/gfx/color_utils.h" | |
| 21 #include "ui/gfx/font_list.h" | |
| 22 #include "ui/gfx/text_constants.h" | |
| 23 #include "ui/gfx/text_elider.h" | |
| 24 #include "ui/views/border.h" | 15 #include "ui/views/border.h" |
| 25 #include "ui/views/controls/button/label_button_border.h" | 16 #include "ui/views/controls/button/label_button_border.h" |
| 26 #include "ui/views/painter.h" | 17 #include "ui/views/painter.h" |
| 27 | 18 |
| 28 namespace { | 19 namespace { |
| 29 | 20 |
| 30 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], | 21 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], |
| 31 const int hot_image_set[], | 22 const int hot_image_set[], |
| 32 const int pushed_image_set[]) { | 23 const int pushed_image_set[]) { |
| 33 scoped_ptr<views::LabelButtonBorder> border( | 24 scoped_ptr<views::LabelButtonBorder> border( |
| 34 new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON)); | 25 new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON)); |
| 35 border->SetPainter(false, views::Button::STATE_NORMAL, | 26 border->SetPainter(false, views::Button::STATE_NORMAL, |
| 36 views::Painter::CreateImageGridPainter(normal_image_set)); | 27 views::Painter::CreateImageGridPainter(normal_image_set)); |
| 37 border->SetPainter(false, views::Button::STATE_HOVERED, | 28 border->SetPainter(false, views::Button::STATE_HOVERED, |
| 38 views::Painter::CreateImageGridPainter(hot_image_set)); | 29 views::Painter::CreateImageGridPainter(hot_image_set)); |
| 39 border->SetPainter(false, views::Button::STATE_PRESSED, | 30 border->SetPainter(false, views::Button::STATE_PRESSED, |
| 40 views::Painter::CreateImageGridPainter(pushed_image_set)); | 31 views::Painter::CreateImageGridPainter(pushed_image_set)); |
| 41 | 32 |
| 42 const int kLeftRightInset = 10; | 33 const int kLeftRightInset = 10; |
| 43 const int kTopInset = 0; | 34 const int kTopInset = 0; |
| 44 const int kBottomInset = 4; | 35 const int kBottomInset = 4; |
| 45 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset, | 36 border->set_insets(gfx::Insets(kTopInset, kLeftRightInset, |
| 46 kBottomInset, kLeftRightInset)); | 37 kBottomInset, kLeftRightInset)); |
| 47 | 38 |
| 48 return border.PassAs<views::Border>(); | 39 return border.PassAs<views::Border>(); |
| 49 } | 40 } |
| 50 | 41 |
| 51 base::string16 GetElidedText(const base::string16& original_text) { | |
| 52 // Maximum characters the button can be before the text will get elided. | |
| 53 const int kMaxCharactersToDisplay = 15; | |
| 54 const gfx::FontList font_list; | |
| 55 return gfx::ElideText(original_text, font_list, | |
| 56 font_list.GetExpectedTextWidth(kMaxCharactersToDisplay), | |
| 57 gfx::ELIDE_TAIL); | |
| 58 } | |
| 59 | |
| 60 base::string16 GetButtonText(Profile* profile) { | |
| 61 base::string16 name = GetElidedText(profiles::GetAvatarNameForProfile( | |
| 62 profile->GetPath())); | |
| 63 if (profile->IsSupervised()) | |
| 64 name = l10n_util::GetStringFUTF16(IDS_SUPERVISED_USER_NEW_AVATAR_LABEL, | |
| 65 name); | |
| 66 return name; | |
| 67 } | |
| 68 | |
| 69 } // namespace | 42 } // namespace |
| 70 | 43 |
| 71 NewAvatarButton::NewAvatarButton( | 44 NewAvatarButton::NewAvatarButton( |
| 72 views::ButtonListener* listener, | 45 views::ButtonListener* listener, |
| 73 const base::string16& profile_name, | 46 const base::string16& profile_name, |
| 74 AvatarButtonStyle button_style, | 47 AvatarButtonStyle button_style, |
| 75 Browser* browser) | 48 Browser* browser) |
| 76 : MenuButton(listener, GetButtonText(browser->profile()), NULL, true), | 49 : MenuButton(listener, |
| 50 profiles::GetAvatarButtonTextForProfile(browser->profile()), |
| 51 NULL, |
| 52 true), |
| 77 browser_(browser) { | 53 browser_(browser) { |
| 78 set_animate_on_state_change(false); | 54 set_animate_on_state_change(false); |
| 79 SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE); | 55 SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE); |
| 80 SetTextColor(views::Button::STATE_HOVERED, SK_ColorWHITE); | 56 SetTextColor(views::Button::STATE_HOVERED, SK_ColorWHITE); |
| 81 SetTextColor(views::Button::STATE_PRESSED, SK_ColorWHITE); | 57 SetTextColor(views::Button::STATE_PRESSED, SK_ColorWHITE); |
| 82 SetTextShadows(gfx::ShadowValues(10, | 58 SetTextShadows(gfx::ShadowValues(10, |
| 83 gfx::ShadowValue(gfx::Point(), 1.0f, SK_ColorDKGRAY))); | 59 gfx::ShadowValue(gfx::Point(), 1.0f, SK_ColorDKGRAY))); |
| 84 SetTextSubpixelRenderingEnabled(false); | 60 SetTextSubpixelRenderingEnabled(false); |
| 85 | 61 |
| 86 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 62 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 icon = *ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 142 icon = *ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 167 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToImageSkia(); | 143 IDR_ICON_PROFILES_AVATAR_BUTTON_ERROR).ToImageSkia(); |
| 168 } | 144 } |
| 169 | 145 |
| 170 SetImage(views::Button::STATE_NORMAL, icon); | 146 SetImage(views::Button::STATE_NORMAL, icon); |
| 171 UpdateAvatarButtonAndRelayoutParent(); | 147 UpdateAvatarButtonAndRelayoutParent(); |
| 172 } | 148 } |
| 173 | 149 |
| 174 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { | 150 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { |
| 175 // We want the button to resize if the new text is shorter. | 151 // We want the button to resize if the new text is shorter. |
| 176 SetText(GetButtonText(browser_->profile())); | 152 SetText(profiles::GetAvatarButtonTextForProfile(browser_->profile())); |
| 177 set_min_size(gfx::Size()); | 153 set_min_size(gfx::Size()); |
| 178 InvalidateLayout(); | 154 InvalidateLayout(); |
| 179 | 155 |
| 180 // Because the width of the button might have changed, the parent browser | 156 // Because the width of the button might have changed, the parent browser |
| 181 // frame needs to recalculate the button bounds and redraw it. | 157 // frame needs to recalculate the button bounds and redraw it. |
| 182 if (parent()) | 158 if (parent()) |
| 183 parent()->Layout(); | 159 parent()->Layout(); |
| 184 } | 160 } |
| OLD | NEW |