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 "ui/base/material_design/material_design_controller.h" | 8 #include "ui/base/material_design/material_design_controller.h" |
9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
10 #include "ui/gfx/color_palette.h" | 10 #include "ui/gfx/color_palette.h" |
11 #include "ui/gfx/color_utils.h" | 11 #include "ui/gfx/color_utils.h" |
12 #include "ui/native_theme/native_theme.h" | 12 #include "ui/native_theme/native_theme.h" |
13 #include "ui/views/animation/flood_fill_ink_drop_ripple.h" | 13 #include "ui/views/animation/flood_fill_ink_drop_ripple.h" |
14 #include "ui/views/animation/ink_drop_highlight.h" | 14 #include "ui/views/animation/ink_drop_highlight.h" |
15 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" | 15 #include "ui/views/animation/ink_drop_painted_layer_delegates.h" |
16 #include "ui/views/background.h" | 16 #include "ui/views/background.h" |
17 #include "ui/views/border.h" | 17 #include "ui/views/border.h" |
18 #include "ui/views/controls/button/blue_button.h" | 18 #include "ui/views/controls/button/blue_button.h" |
| 19 #include "ui/views/controls/focus_ring.h" |
19 #include "ui/views/painter.h" | 20 #include "ui/views/painter.h" |
20 #include "ui/views/style/platform_style.h" | 21 #include "ui/views/style/platform_style.h" |
21 | 22 |
22 namespace views { | 23 namespace views { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 // Minimum size to reserve for the button contents. | 27 // Minimum size to reserve for the button contents. |
27 const int kMinWidth = 48; | 28 const int kMinWidth = 48; |
28 | 29 |
29 // The stroke width of the focus border in dp. | |
30 const int kFocusBorderThickness = 2; | |
31 | |
32 // The corner radius of the focus border roundrect. | |
33 const int kFocusBorderCornerRadius = 3; | |
34 | |
35 LabelButton* CreateButton(ButtonListener* listener, | 30 LabelButton* CreateButton(ButtonListener* listener, |
36 const base::string16& text, | 31 const base::string16& text, |
37 bool md) { | 32 bool md) { |
38 if (md) | 33 if (md) |
39 return MdTextButton::Create(listener, text); | 34 return MdTextButton::Create(listener, text); |
40 | 35 |
41 LabelButton* button = new LabelButton(listener, text); | 36 LabelButton* button = new LabelButton(listener, text); |
42 button->SetStyle(CustomButton::STYLE_BUTTON); | 37 button->SetStyle(CustomButton::STYLE_BUTTON); |
43 return button; | 38 return button; |
44 } | 39 } |
45 | 40 |
46 const gfx::FontList& GetMdFontList() { | 41 const gfx::FontList& GetMdFontList() { |
47 static base::LazyInstance<gfx::FontList>::Leaky font_list = | 42 static base::LazyInstance<gfx::FontList>::Leaky font_list = |
48 LAZY_INSTANCE_INITIALIZER; | 43 LAZY_INSTANCE_INITIALIZER; |
49 const gfx::Font::Weight min_weight = gfx::Font::Weight::MEDIUM; | 44 const gfx::Font::Weight min_weight = gfx::Font::Weight::MEDIUM; |
50 if (font_list.Get().GetFontWeight() < min_weight) | 45 if (font_list.Get().GetFontWeight() < min_weight) |
51 font_list.Get() = font_list.Get().DeriveWithWeight(min_weight); | 46 font_list.Get() = font_list.Get().DeriveWithWeight(min_weight); |
52 return font_list.Get(); | 47 return font_list.Get(); |
53 } | 48 } |
54 | 49 |
55 } // namespace | 50 } // namespace |
56 | 51 |
57 namespace internal { | |
58 | |
59 class MdFocusRing : public View { | |
60 public: | |
61 MdFocusRing() { | |
62 SetPaintToLayer(true); | |
63 layer()->SetFillsBoundsOpaquely(false); | |
64 } | |
65 ~MdFocusRing() override {} | |
66 | |
67 // View: | |
68 bool CanProcessEventsWithinSubtree() const override { return false; } | |
69 | |
70 void OnPaint(gfx::Canvas* canvas) override { | |
71 SkPaint paint; | |
72 paint.setAntiAlias(true); | |
73 paint.setColor( | |
74 SkColorSetA(GetNativeTheme()->GetSystemColor( | |
75 ui::NativeTheme::kColorId_FocusedBorderColor), | |
76 0x66)); | |
77 paint.setStyle(SkPaint::kStroke_Style); | |
78 paint.setStrokeWidth(kFocusBorderThickness); | |
79 gfx::RectF rect(GetLocalBounds()); | |
80 rect.Inset(gfx::InsetsF(kFocusBorderThickness / 2.f)); | |
81 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint); | |
82 } | |
83 | |
84 private: | |
85 DISALLOW_COPY_AND_ASSIGN(MdFocusRing); | |
86 }; | |
87 | |
88 } // namespace internal | |
89 | |
90 // static | 52 // static |
91 LabelButton* MdTextButton::CreateSecondaryUiButton(ButtonListener* listener, | 53 LabelButton* MdTextButton::CreateSecondaryUiButton(ButtonListener* listener, |
92 const base::string16& text) { | 54 const base::string16& text) { |
93 return CreateButton(listener, text, | 55 return CreateButton(listener, text, |
94 ui::MaterialDesignController::IsSecondaryUiMaterial()); | 56 ui::MaterialDesignController::IsSecondaryUiMaterial()); |
95 } | 57 } |
96 | 58 |
97 // static | 59 // static |
98 LabelButton* MdTextButton::CreateSecondaryUiBlueButton( | 60 LabelButton* MdTextButton::CreateSecondaryUiBlueButton( |
99 ButtonListener* listener, | 61 ButtonListener* listener, |
(...skipping 17 matching lines...) Expand all Loading... |
117 } | 79 } |
118 | 80 |
119 void MdTextButton::SetProminent(bool is_prominent) { | 81 void MdTextButton::SetProminent(bool is_prominent) { |
120 if (is_prominent_ == is_prominent) | 82 if (is_prominent_ == is_prominent) |
121 return; | 83 return; |
122 | 84 |
123 is_prominent_ = is_prominent; | 85 is_prominent_ = is_prominent; |
124 UpdateColors(); | 86 UpdateColors(); |
125 } | 87 } |
126 | 88 |
127 void MdTextButton::Layout() { | |
128 LabelButton::Layout(); | |
129 gfx::Rect focus_bounds = GetLocalBounds(); | |
130 focus_bounds.Inset(gfx::Insets(-kFocusBorderThickness)); | |
131 focus_ring_->SetBoundsRect(focus_bounds); | |
132 } | |
133 | |
134 void MdTextButton::OnFocus() { | 89 void MdTextButton::OnFocus() { |
135 LabelButton::OnFocus(); | 90 LabelButton::OnFocus(); |
136 focus_ring_->SetVisible(true); | 91 focus_ring_->SetVisible(true); |
137 } | 92 } |
138 | 93 |
139 void MdTextButton::OnBlur() { | 94 void MdTextButton::OnBlur() { |
140 LabelButton::OnBlur(); | 95 LabelButton::OnBlur(); |
141 focus_ring_->SetVisible(false); | 96 focus_ring_->SetVisible(false); |
142 } | 97 } |
143 | 98 |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 UpdateColors(); | 167 UpdateColors(); |
213 } | 168 } |
214 | 169 |
215 void MdTextButton::SetFontList(const gfx::FontList& font_list) { | 170 void MdTextButton::SetFontList(const gfx::FontList& font_list) { |
216 NOTREACHED() | 171 NOTREACHED() |
217 << "Don't call MdTextButton::SetFontList (it will soon be protected)"; | 172 << "Don't call MdTextButton::SetFontList (it will soon be protected)"; |
218 } | 173 } |
219 | 174 |
220 MdTextButton::MdTextButton(ButtonListener* listener) | 175 MdTextButton::MdTextButton(ButtonListener* listener) |
221 : LabelButton(listener, base::string16()), | 176 : LabelButton(listener, base::string16()), |
222 focus_ring_(new internal::MdFocusRing()), | 177 focus_ring_(new FocusRing()), |
223 is_prominent_(false) { | 178 is_prominent_(false) { |
224 SetInkDropMode(PlatformStyle::kUseRipples ? InkDropMode::ON | 179 SetInkDropMode(PlatformStyle::kUseRipples ? InkDropMode::ON |
225 : InkDropMode::OFF); | 180 : InkDropMode::OFF); |
226 set_has_ink_drop_action_on_click(true); | 181 set_has_ink_drop_action_on_click(true); |
227 SetHorizontalAlignment(gfx::ALIGN_CENTER); | 182 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
228 SetFocusForPlatform(); | 183 SetFocusForPlatform(); |
229 SetMinSize(gfx::Size(kMinWidth, 0)); | 184 SetMinSize(gfx::Size(kMinWidth, 0)); |
230 SetFocusPainter(nullptr); | 185 SetFocusPainter(nullptr); |
231 label()->SetAutoColorReadabilityEnabled(false); | 186 label()->SetAutoColorReadabilityEnabled(false); |
232 AddChildView(focus_ring_); | 187 AddChildView(focus_ring_); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) | 272 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) |
318 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); | 273 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); |
319 | 274 |
320 DCHECK_EQ(SK_AlphaOPAQUE, static_cast<int>(SkColorGetA(bg_color))); | 275 DCHECK_EQ(SK_AlphaOPAQUE, static_cast<int>(SkColorGetA(bg_color))); |
321 set_background(Background::CreateBackgroundPainter( | 276 set_background(Background::CreateBackgroundPainter( |
322 true, Painter::CreateRoundRectWith1PxBorderPainter( | 277 true, Painter::CreateRoundRectWith1PxBorderPainter( |
323 bg_color, stroke_color, kInkDropSmallCornerRadius))); | 278 bg_color, stroke_color, kInkDropSmallCornerRadius))); |
324 } | 279 } |
325 | 280 |
326 } // namespace views | 281 } // namespace views |
OLD | NEW |