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" | 11 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
13 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
14 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 13 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
15 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
16 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
17 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
18 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
19 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
20 #include "ui/gfx/color_utils.h" | 19 #include "ui/gfx/color_utils.h" |
21 #include "ui/gfx/font_list.h" | 20 #include "ui/gfx/font_list.h" |
22 #include "ui/gfx/text_constants.h" | |
23 #include "ui/gfx/text_elider.h" | 21 #include "ui/gfx/text_elider.h" |
24 #include "ui/views/border.h" | 22 #include "ui/views/border.h" |
25 #include "ui/views/controls/button/label_button_border.h" | |
26 #include "ui/views/painter.h" | 23 #include "ui/views/painter.h" |
27 | 24 |
28 namespace { | 25 namespace { |
29 | 26 |
30 // Text padding within the button border. | 27 // Text padding within the button border. |
31 const int kLeftRightInset = 7; | 28 const int kInset = 10; |
32 const int kTopBottomInset = 2; | |
33 | 29 |
34 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], | 30 scoped_ptr<views::Border> CreateBorder(const int normal_image_set[], |
35 const int hot_image_set[], | 31 const int hot_image_set[], |
36 const int pushed_image_set[]) { | 32 const int pushed_image_set[]) { |
37 scoped_ptr<views::LabelButtonBorder> border( | 33 scoped_ptr<views::TextButtonDefaultBorder> border( |
38 new views::LabelButtonBorder(views::Button::STYLE_TEXTBUTTON)); | 34 new views::TextButtonDefaultBorder()); |
39 | 35 |
40 border->SetPainter(false, views::Button::STATE_NORMAL, | 36 border->SetInsets(gfx::Insets(kInset, kInset, kInset, kInset)); |
| 37 border->set_normal_painter( |
41 views::Painter::CreateImageGridPainter(normal_image_set)); | 38 views::Painter::CreateImageGridPainter(normal_image_set)); |
42 border->SetPainter(false, views::Button::STATE_HOVERED, | 39 border->set_hot_painter( |
43 views::Painter::CreateImageGridPainter(hot_image_set)); | 40 views::Painter::CreateImageGridPainter(hot_image_set)); |
44 border->SetPainter(false, views::Button::STATE_PRESSED, | 41 border->set_pushed_painter( |
45 views::Painter::CreateImageGridPainter(pushed_image_set)); | 42 views::Painter::CreateImageGridPainter(pushed_image_set)); |
46 | 43 |
47 border->set_insets(gfx::Insets(kTopBottomInset, kLeftRightInset, | |
48 kTopBottomInset, kLeftRightInset)); | |
49 | |
50 return border.PassAs<views::Border>(); | 44 return border.PassAs<views::Border>(); |
51 } | 45 } |
52 | 46 |
53 base::string16 GetElidedText(const base::string16& original_text) { | 47 base::string16 GetElidedText(const base::string16& original_text) { |
54 // Maximum characters the button can be before the text will get elided. | 48 // Maximum characters the button can be before the text will get elided. |
55 const int kMaxCharactersToDisplay = 15; | 49 const int kMaxCharactersToDisplay = 15; |
56 const gfx::FontList font_list; | 50 const gfx::FontList font_list; |
57 return gfx::ElideText(original_text, font_list, | 51 return gfx::ElideText(original_text, font_list, |
58 font_list.GetExpectedTextWidth(kMaxCharactersToDisplay), | 52 font_list.GetExpectedTextWidth(kMaxCharactersToDisplay), |
59 gfx::ELIDE_TAIL); | 53 gfx::ELIDE_TAIL); |
(...skipping 10 matching lines...) Expand all Loading... |
70 } // namespace | 64 } // namespace |
71 | 65 |
72 NewAvatarButton::NewAvatarButton( | 66 NewAvatarButton::NewAvatarButton( |
73 views::ButtonListener* listener, | 67 views::ButtonListener* listener, |
74 const base::string16& profile_name, | 68 const base::string16& profile_name, |
75 AvatarButtonStyle button_style, | 69 AvatarButtonStyle button_style, |
76 Browser* browser) | 70 Browser* browser) |
77 : MenuButton(listener, GetButtonText(browser->profile()), NULL, true), | 71 : MenuButton(listener, GetButtonText(browser->profile()), NULL, true), |
78 browser_(browser) { | 72 browser_(browser) { |
79 set_animate_on_state_change(false); | 73 set_animate_on_state_change(false); |
80 SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE); | 74 set_icon_placement(ICON_ON_RIGHT); |
81 SetTextColor(views::Button::STATE_HOVERED, SK_ColorWHITE); | |
82 SetTextColor(views::Button::STATE_PRESSED, SK_ColorWHITE); | |
83 SetHaloColor(SK_ColorDKGRAY); | |
84 SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
85 | 75 |
86 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 76 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
87 | 77 |
88 bool is_win8 = false; | 78 bool is_win8 = false; |
89 #if defined(OS_WIN) | 79 #if defined(OS_WIN) |
90 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8; | 80 is_win8 = base::win::GetVersion() >= base::win::VERSION_WIN8; |
91 #endif | 81 #endif |
92 | 82 |
93 if (button_style == THEMED_BUTTON) { | 83 if (button_style == THEMED_BUTTON) { |
94 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); | 84 const int kNormalImageSet[] = IMAGE_GRID(IDR_AVATAR_THEMED_BUTTON_NORMAL); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 | 123 |
134 NewAvatarButton::~NewAvatarButton() { | 124 NewAvatarButton::~NewAvatarButton() { |
135 g_browser_process->profile_manager()-> | 125 g_browser_process->profile_manager()-> |
136 GetProfileInfoCache().RemoveObserver(this); | 126 GetProfileInfoCache().RemoveObserver(this); |
137 SigninErrorController* error = | 127 SigninErrorController* error = |
138 profiles::GetSigninErrorController(browser_->profile()); | 128 profiles::GetSigninErrorController(browser_->profile()); |
139 if (error) | 129 if (error) |
140 error->RemoveObserver(this); | 130 error->RemoveObserver(this); |
141 } | 131 } |
142 | 132 |
| 133 void NewAvatarButton::OnPaintText(gfx::Canvas* canvas, PaintButtonMode mode) { |
| 134 // Get text bounds, and then adjust for the top and RTL languages. |
| 135 gfx::Rect rect = GetTextBounds(); |
| 136 rect.Offset(0, -rect.y()); |
| 137 if (rect.width() > 0) |
| 138 rect.set_x(GetMirroredXForRect(rect)); |
| 139 |
| 140 canvas->DrawStringRectWithHalo( |
| 141 text(), |
| 142 gfx::FontList(), |
| 143 SK_ColorWHITE, |
| 144 SK_ColorDKGRAY, |
| 145 rect, |
| 146 gfx::Canvas::NO_SUBPIXEL_RENDERING); |
| 147 } |
| 148 |
143 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { | 149 void NewAvatarButton::OnProfileAdded(const base::FilePath& profile_path) { |
144 UpdateAvatarButtonAndRelayoutParent(); | 150 UpdateAvatarButtonAndRelayoutParent(); |
145 } | 151 } |
146 | 152 |
147 void NewAvatarButton::OnProfileWasRemoved( | 153 void NewAvatarButton::OnProfileWasRemoved( |
148 const base::FilePath& profile_path, | 154 const base::FilePath& profile_path, |
149 const base::string16& profile_name) { | 155 const base::string16& profile_name) { |
150 UpdateAvatarButtonAndRelayoutParent(); | 156 UpdateAvatarButtonAndRelayoutParent(); |
151 } | 157 } |
152 | 158 |
153 void NewAvatarButton::OnProfileNameChanged( | 159 void NewAvatarButton::OnProfileNameChanged( |
154 const base::FilePath& profile_path, | 160 const base::FilePath& profile_path, |
155 const base::string16& old_profile_name) { | 161 const base::string16& old_profile_name) { |
156 UpdateAvatarButtonAndRelayoutParent(); | 162 UpdateAvatarButtonAndRelayoutParent(); |
157 } | 163 } |
158 | 164 |
159 void NewAvatarButton::OnErrorChanged() { | 165 void NewAvatarButton::OnErrorChanged() { |
160 gfx::ImageSkia icon; | 166 gfx::ImageSkia icon; |
161 | 167 |
162 // If there is an error, show an warning icon. | 168 // If there is an error, show an warning icon. |
163 const SigninErrorController* error = | 169 const SigninErrorController* error = |
164 profiles::GetSigninErrorController(browser_->profile()); | 170 profiles::GetSigninErrorController(browser_->profile()); |
165 if (error && error->HasError()) { | 171 if (error && error->HasError()) { |
166 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 172 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
167 icon = *rb->GetImageNamed(IDR_WARNING).ToImageSkia(); | 173 icon = *rb->GetImageNamed(IDR_WARNING).ToImageSkia(); |
168 } | 174 } |
169 | 175 |
170 SetImage(views::Button::STATE_NORMAL, icon); | 176 SetIcon(icon); |
171 UpdateAvatarButtonAndRelayoutParent(); | 177 UpdateAvatarButtonAndRelayoutParent(); |
172 } | 178 } |
173 | 179 |
174 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { | 180 void NewAvatarButton::UpdateAvatarButtonAndRelayoutParent() { |
175 // We want the button to resize if the new text is shorter. | 181 // We want the button to resize if the new text is shorter. |
176 SetText(GetButtonText(browser_->profile())); | 182 SetText(GetButtonText(browser_->profile())); |
177 set_min_size(gfx::Size()); | 183 ClearMaxTextSize(); |
178 InvalidateLayout(); | |
179 | 184 |
180 // Because the width of the button might have changed, the parent browser | 185 // Because the width of the button might have changed, the parent browser |
181 // frame needs to recalculate the button bounds and redraw it. | 186 // frame needs to recalculate the button bounds and redraw it. |
182 if (parent()) | 187 if (parent()) |
183 parent()->Layout(); | 188 parent()->Layout(); |
184 } | 189 } |
OLD | NEW |