Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: ui/views/controls/button/md_text_button.cc

Issue 2383243002: Refactor MdFocusRing to make it easier to reuse on other controls. (Closed)
Patch Set: install Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/views/controls/button/md_text_button.h ('k') | ui/views/controls/combobox/combobox.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 FocusRing::Install(this);
137 } 92 }
138 93
139 void MdTextButton::OnBlur() { 94 void MdTextButton::OnBlur() {
140 LabelButton::OnBlur(); 95 LabelButton::OnBlur();
141 focus_ring_->SetVisible(false); 96 FocusRing::Uninstall(this);
142 } 97 }
143 98
144 void MdTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) { 99 void MdTextButton::OnNativeThemeChanged(const ui::NativeTheme* theme) {
145 LabelButton::OnNativeThemeChanged(theme); 100 LabelButton::OnNativeThemeChanged(theme);
146 UpdateColors(); 101 UpdateColors();
147 } 102 }
148 103
149 SkColor MdTextButton::GetInkDropBaseColor() const { 104 SkColor MdTextButton::GetInkDropBaseColor() const {
150 return color_utils::DeriveDefaultIconColor(label()->enabled_color()); 105 return color_utils::DeriveDefaultIconColor(label()->enabled_color());
151 } 106 }
(...skipping 29 matching lines...) Expand all
181 shadows.push_back(gfx::ShadowValue(gfx::Vector2d(0, kYOffset), 136 shadows.push_back(gfx::ShadowValue(gfx::Vector2d(0, kYOffset),
182 2 * kSkiaBlurRadius, 137 2 * kSkiaBlurRadius,
183 SkColorSetA(SK_ColorBLACK, 0x3D))); 138 SkColorSetA(SK_ColorBLACK, 0x3D)));
184 return base::MakeUnique<InkDropHighlight>( 139 return base::MakeUnique<InkDropHighlight>(
185 gfx::RectF(GetLocalBounds()).CenterPoint(), 140 gfx::RectF(GetLocalBounds()).CenterPoint(),
186 base::WrapUnique(new BorderShadowLayerDelegate( 141 base::WrapUnique(new BorderShadowLayerDelegate(
187 shadows, GetLocalBounds(), kInkDropSmallCornerRadius))); 142 shadows, GetLocalBounds(), kInkDropSmallCornerRadius)));
188 } 143 }
189 144
190 bool MdTextButton::ShouldShowInkDropForFocus() const { 145 bool MdTextButton::ShouldShowInkDropForFocus() const {
191 // These types of button use |focus_ring_|. 146 // These types of button use FocusRing.
192 return false; 147 return false;
193 } 148 }
194 149
195 void MdTextButton::SetEnabledTextColors(SkColor color) { 150 void MdTextButton::SetEnabledTextColors(SkColor color) {
196 LabelButton::SetEnabledTextColors(color); 151 LabelButton::SetEnabledTextColors(color);
197 UpdateColors(); 152 UpdateColors();
198 } 153 }
199 154
200 void MdTextButton::SetText(const base::string16& text) { 155 void MdTextButton::SetText(const base::string16& text) {
201 LabelButton::SetText(text); 156 LabelButton::SetText(text);
(...skipping 10 matching lines...) Expand all
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()),
223 is_prominent_(false) { 177 is_prominent_(false) {
224 SetInkDropMode(PlatformStyle::kUseRipples ? InkDropMode::ON 178 SetInkDropMode(PlatformStyle::kUseRipples ? InkDropMode::ON
225 : InkDropMode::OFF); 179 : InkDropMode::OFF);
226 set_has_ink_drop_action_on_click(true); 180 set_has_ink_drop_action_on_click(true);
227 SetHorizontalAlignment(gfx::ALIGN_CENTER); 181 SetHorizontalAlignment(gfx::ALIGN_CENTER);
228 SetFocusForPlatform(); 182 SetFocusForPlatform();
229 SetMinSize(gfx::Size(kMinWidth, 0)); 183 SetMinSize(gfx::Size(kMinWidth, 0));
230 SetFocusPainter(nullptr); 184 SetFocusPainter(nullptr);
231 label()->SetAutoColorReadabilityEnabled(false); 185 label()->SetAutoColorReadabilityEnabled(false);
232 AddChildView(focus_ring_);
233 focus_ring_->SetVisible(false);
234 set_request_focus_on_press(false); 186 set_request_focus_on_press(false);
235 LabelButton::SetFontList(GetMdFontList()); 187 LabelButton::SetFontList(GetMdFontList());
236 188
237 // Paint to a layer so that the canvas is snapped to pixel boundaries (useful 189 // Paint to a layer so that the canvas is snapped to pixel boundaries (useful
238 // for fractional DSF). 190 // for fractional DSF).
239 SetPaintToLayer(true); 191 SetPaintToLayer(true);
240 layer()->SetFillsBoundsOpaquely(false); 192 layer()->SetFillsBoundsOpaquely(false);
241 } 193 }
242 194
243 MdTextButton::~MdTextButton() {} 195 MdTextButton::~MdTextButton() {}
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity) 269 ? SkColorSetA(SK_ColorBLACK, kStrokeOpacity)
318 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity); 270 : SkColorSetA(SK_ColorWHITE, 2 * kStrokeOpacity);
319 271
320 DCHECK_EQ(SK_AlphaOPAQUE, static_cast<int>(SkColorGetA(bg_color))); 272 DCHECK_EQ(SK_AlphaOPAQUE, static_cast<int>(SkColorGetA(bg_color)));
321 set_background(Background::CreateBackgroundPainter( 273 set_background(Background::CreateBackgroundPainter(
322 true, Painter::CreateRoundRectWith1PxBorderPainter( 274 true, Painter::CreateRoundRectWith1PxBorderPainter(
323 bg_color, stroke_color, kInkDropSmallCornerRadius))); 275 bg_color, stroke_color, kInkDropSmallCornerRadius)));
324 } 276 }
325 277
326 } // namespace views 278 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/button/md_text_button.h ('k') | ui/views/controls/combobox/combobox.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698