OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ui/views/controls/button/md_text_button.h" | 5 #include "ui/views/controls/button/md_text_button.h" |
6 | 6 |
7 #include "base/i18n/case_conversion.h" | 7 #include "base/i18n/case_conversion.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "ui/base/material_design/material_design_controller.h" | 9 #include "ui/base/material_design/material_design_controller.h" |
10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
11 #include "ui/gfx/color_palette.h" | 11 #include "ui/gfx/color_palette.h" |
12 #include "ui/gfx/color_utils.h" | 12 #include "ui/gfx/color_utils.h" |
13 #include "ui/native_theme/native_theme.h" | 13 #include "ui/native_theme/native_theme.h" |
14 #include "ui/views/animation/flood_fill_ink_drop_ripple.h" | 14 #include "ui/views/animation/flood_fill_ink_drop_ripple.h" |
15 #include "ui/views/animation/ink_drop_highlight.h" | 15 #include "ui/views/animation/ink_drop_highlight.h" |
16 #include "ui/views/animation/ink_drop_impl.h" | 16 #include "ui/views/animation/ink_drop_impl.h" |
17 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" | 17 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" |
18 #include "ui/views/background.h" | 18 #include "ui/views/background.h" |
19 #include "ui/views/border.h" | 19 #include "ui/views/border.h" |
20 #include "ui/views/controls/button/blue_button.h" | 20 #include "ui/views/controls/button/blue_button.h" |
21 #include "ui/views/controls/focus_ring.h" | 21 #include "ui/views/controls/focus_ring.h" |
22 #include "ui/views/layout/layout_provider.h" | 22 #include "ui/views/layout/layout_provider.h" |
23 #include "ui/views/painter.h" | 23 #include "ui/views/painter.h" |
24 #include "ui/views/style/platform_style.h" | 24 #include "ui/views/style/platform_style.h" |
| 25 #include "ui/views/style/typography.h" |
25 | 26 |
26 namespace views { | 27 namespace views { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 LabelButton* CreateButton(ButtonListener* listener, | 31 LabelButton* CreateButton(ButtonListener* listener, |
31 const base::string16& text, | 32 const base::string16& text, |
32 bool md) { | 33 bool md) { |
33 if (md) | 34 if (md) |
34 return MdTextButton::Create(listener, text); | 35 return MdTextButton::Create(listener, text, style::CONTEXT_BUTTON_MD); |
35 | 36 |
36 LabelButton* button = new LabelButton(listener, text); | 37 LabelButton* button = new LabelButton(listener, text, style::CONTEXT_BUTTON); |
37 button->SetStyleDeprecated(CustomButton::STYLE_BUTTON); | 38 button->SetStyleDeprecated(CustomButton::STYLE_BUTTON); |
38 return button; | 39 return button; |
39 } | 40 } |
40 | 41 |
41 const gfx::FontList& GetMdFontList() { | |
42 static base::LazyInstance<gfx::FontList>::Leaky font_list = | |
43 LAZY_INSTANCE_INITIALIZER; | |
44 const gfx::Font::Weight min_weight = gfx::Font::Weight::MEDIUM; | |
45 if (font_list.Get().GetFontWeight() < min_weight) | |
46 font_list.Get() = font_list.Get().DeriveWithWeight(min_weight); | |
47 return font_list.Get(); | |
48 } | |
49 | |
50 } // namespace | 42 } // namespace |
51 | 43 |
52 // static | 44 // static |
53 LabelButton* MdTextButton::CreateSecondaryUiButton(ButtonListener* listener, | 45 LabelButton* MdTextButton::CreateSecondaryUiButton(ButtonListener* listener, |
54 const base::string16& text) { | 46 const base::string16& text) { |
55 return CreateButton(listener, text, | 47 return CreateButton(listener, text, |
56 ui::MaterialDesignController::IsSecondaryUiMaterial()); | 48 ui::MaterialDesignController::IsSecondaryUiMaterial()); |
57 } | 49 } |
58 | 50 |
59 // static | 51 // static |
60 LabelButton* MdTextButton::CreateSecondaryUiBlueButton( | 52 LabelButton* MdTextButton::CreateSecondaryUiBlueButton( |
61 ButtonListener* listener, | 53 ButtonListener* listener, |
62 const base::string16& text) { | 54 const base::string16& text) { |
63 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { | 55 if (ui::MaterialDesignController::IsSecondaryUiMaterial()) { |
64 MdTextButton* md_button = MdTextButton::Create(listener, text); | 56 MdTextButton* md_button = |
| 57 MdTextButton::Create(listener, text, style::CONTEXT_BUTTON_MD); |
65 md_button->SetProminent(true); | 58 md_button->SetProminent(true); |
66 return md_button; | 59 return md_button; |
67 } | 60 } |
68 | 61 |
69 return new BlueButton(listener, text); | 62 return new BlueButton(listener, text); |
70 } | 63 } |
71 | 64 |
72 // static | 65 // static |
73 MdTextButton* MdTextButton::Create(ButtonListener* listener, | 66 MdTextButton* MdTextButton::Create(ButtonListener* listener, |
74 const base::string16& text) { | 67 const base::string16& text, |
75 MdTextButton* button = new MdTextButton(listener); | 68 int button_context) { |
| 69 MdTextButton* button = new MdTextButton(listener, button_context); |
76 button->SetText(text); | 70 button->SetText(text); |
77 button->SetFocusForPlatform(); | 71 button->SetFocusForPlatform(); |
78 return button; | 72 return button; |
79 } | 73 } |
80 | 74 |
81 MdTextButton::~MdTextButton() {} | 75 MdTextButton::~MdTextButton() {} |
82 | 76 |
83 void MdTextButton::SetProminent(bool is_prominent) { | 77 void MdTextButton::SetProminent(bool is_prominent) { |
84 if (is_prominent_ == is_prominent) | 78 if (is_prominent_ == is_prominent) |
85 return; | 79 return; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 void MdTextButton::SetEnabledTextColors(SkColor color) { | 157 void MdTextButton::SetEnabledTextColors(SkColor color) { |
164 LabelButton::SetEnabledTextColors(color); | 158 LabelButton::SetEnabledTextColors(color); |
165 UpdateColors(); | 159 UpdateColors(); |
166 } | 160 } |
167 | 161 |
168 void MdTextButton::SetText(const base::string16& text) { | 162 void MdTextButton::SetText(const base::string16& text) { |
169 LabelButton::SetText(text); | 163 LabelButton::SetText(text); |
170 UpdatePadding(); | 164 UpdatePadding(); |
171 } | 165 } |
172 | 166 |
173 void MdTextButton::AdjustFontSize(int size_delta) { | |
174 LabelButton::AdjustFontSize(size_delta); | |
175 UpdatePadding(); | |
176 } | |
177 | |
178 void MdTextButton::UpdateStyleToIndicateDefaultStatus() { | 167 void MdTextButton::UpdateStyleToIndicateDefaultStatus() { |
179 is_prominent_ = is_prominent_ || is_default(); | 168 is_prominent_ = is_prominent_ || is_default(); |
180 UpdateColors(); | 169 UpdateColors(); |
181 } | 170 } |
182 | 171 |
183 void MdTextButton::SetFontList(const gfx::FontList& font_list) { | 172 MdTextButton::MdTextButton(ButtonListener* listener, int button_context) |
184 NOTREACHED() | 173 : LabelButton(listener, base::string16(), button_context), |
185 << "Don't call MdTextButton::SetFontList (it will soon be protected)"; | |
186 } | |
187 | |
188 MdTextButton::MdTextButton(ButtonListener* listener) | |
189 : LabelButton(listener, base::string16()), | |
190 is_prominent_(false) { | 174 is_prominent_(false) { |
191 SetInkDropMode(InkDropMode::ON); | 175 SetInkDropMode(InkDropMode::ON); |
192 set_has_ink_drop_action_on_click(true); | 176 set_has_ink_drop_action_on_click(true); |
193 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 177 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
194 SetFocusForPlatform(); | 178 SetFocusForPlatform(); |
195 const int minimum_width = LayoutProvider::Get()->GetDistanceMetric( | 179 const int minimum_width = LayoutProvider::Get()->GetDistanceMetric( |
196 DISTANCE_DIALOG_BUTTON_MINIMUM_WIDTH); | 180 DISTANCE_DIALOG_BUTTON_MINIMUM_WIDTH); |
197 SetMinSize(gfx::Size(minimum_width, 0)); | 181 SetMinSize(gfx::Size(minimum_width, 0)); |
198 SetFocusPainter(nullptr); | 182 SetFocusPainter(nullptr); |
199 label()->SetAutoColorReadabilityEnabled(false); | 183 label()->SetAutoColorReadabilityEnabled(false); |
200 set_request_focus_on_press(false); | 184 set_request_focus_on_press(false); |
201 LabelButton::SetFontList(GetMdFontList()); | |
202 | 185 |
203 set_animate_on_state_change(true); | 186 set_animate_on_state_change(true); |
204 | 187 |
205 // Paint to a layer so that the canvas is snapped to pixel boundaries (useful | 188 // Paint to a layer so that the canvas is snapped to pixel boundaries (useful |
206 // for fractional DSF). | 189 // for fractional DSF). |
207 SetPaintToLayer(); | 190 SetPaintToLayer(); |
208 layer()->SetFillsBoundsOpaquely(false); | 191 layer()->SetFillsBoundsOpaquely(false); |
209 } | 192 } |
210 | 193 |
211 void MdTextButton::UpdatePadding() { | 194 void MdTextButton::UpdatePadding() { |
212 // Don't use font-based padding when there's no text visible. | 195 // Don't use font-based padding when there's no text visible. |
213 if (GetText().empty()) { | 196 if (GetText().empty()) { |
214 SetBorder(NullBorder()); | 197 SetBorder(NullBorder()); |
215 return; | 198 return; |
216 } | 199 } |
217 | 200 |
218 // Text buttons default to 28dp in height on all platforms when the base font | 201 // Text buttons default to 28dp in height on all platforms when the base font |
219 // is in use, but should grow or shrink if the font size is adjusted up or | 202 // is in use, but should grow or shrink if the font size is adjusted up or |
220 // down. When the system font size has been adjusted, the base font will be | 203 // down. When the system font size has been adjusted, the base font will be |
221 // larger than normal such that 28dp might not be enough, so also enforce a | 204 // larger than normal such that 28dp might not be enough, so also enforce a |
222 // minimum height of twice the font size. | 205 // minimum height of twice the font size. |
223 // Example 1: | 206 // Example 1: |
224 // * Normal button on ChromeOS, 12pt Roboto. Button height of 28dp. | 207 // * Normal button on ChromeOS, 12pt Roboto. Button height of 28dp. |
225 // * Button on ChromeOS that has been adjusted to 14pt Roboto. Button height | 208 // * Button on ChromeOS that has been adjusted to 14pt Roboto. Button height |
226 // of 28 + 2 * 2 = 32dp. | 209 // of 28 + 2 * 2 = 32dp. |
227 // * Linux user sets base system font size to 17dp. For a normal button, the | 210 // * Linux user sets base system font size to 17dp. For a normal button, the |
228 // |size_delta| will be zero, so to adjust upwards we double 17 to get 34. | 211 // |size_delta| will be zero, so to adjust upwards we double 17 to get 34. |
229 int size_delta = | 212 int size_delta = |
230 label()->font_list().GetFontSize() - GetMdFontList().GetFontSize(); | 213 label()->font_list().GetFontSize() - |
| 214 style::GetFont(style::CONTEXT_BUTTON_MD, style::STYLE_PRIMARY) |
| 215 .GetFontSize(); |
231 const int kBaseHeight = 28; | 216 const int kBaseHeight = 28; |
232 int target_height = std::max(kBaseHeight + size_delta * 2, | 217 int target_height = std::max(kBaseHeight + size_delta * 2, |
233 label()->font_list().GetFontSize() * 2); | 218 label()->font_list().GetFontSize() * 2); |
234 | 219 |
235 int label_height = label()->GetPreferredSize().height(); | 220 int label_height = label()->GetPreferredSize().height(); |
236 int top_padding = (target_height - label_height) / 2; | 221 int top_padding = (target_height - label_height) / 2; |
237 int bottom_padding = (target_height - label_height + 1) / 2; | 222 int bottom_padding = (target_height - label_height + 1) / 2; |
238 DCHECK_EQ(target_height, label_height + top_padding + bottom_padding); | 223 DCHECK_EQ(target_height, label_height + top_padding + bottom_padding); |
239 | 224 |
240 // TODO(estade): can we get rid of the platform style border hoopla if | 225 // TODO(estade): can we get rid of the platform style border hoopla if |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 stroke_color, gfx::kDisabledControlAlpha); | 282 stroke_color, gfx::kDisabledControlAlpha); |
298 } | 283 } |
299 | 284 |
300 DCHECK_EQ(SK_AlphaOPAQUE, static_cast<int>(SkColorGetA(bg_color))); | 285 DCHECK_EQ(SK_AlphaOPAQUE, static_cast<int>(SkColorGetA(bg_color))); |
301 set_background(Background::CreateBackgroundPainter( | 286 set_background(Background::CreateBackgroundPainter( |
302 Painter::CreateRoundRectWith1PxBorderPainter(bg_color, stroke_color, | 287 Painter::CreateRoundRectWith1PxBorderPainter(bg_color, stroke_color, |
303 kInkDropSmallCornerRadius))); | 288 kInkDropSmallCornerRadius))); |
304 } | 289 } |
305 | 290 |
306 } // namespace views | 291 } // namespace views |
OLD | NEW |