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

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

Issue 1800973002: MD text buttons - implement focus ring. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 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') | no next file » | 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/color_utils.h" 10 #include "ui/gfx/color_utils.h"
10 #include "ui/native_theme/native_theme.h" 11 #include "ui/native_theme/native_theme.h"
11 #include "ui/views/border.h" 12 #include "ui/views/border.h"
12 #include "ui/views/painter.h" 13 #include "ui/views/painter.h"
13 14
14 namespace views { 15 namespace views {
15 16
16 namespace { 17 namespace {
17 18
18 // Inset between clickable region border and button contents (text). 19 // Inset between clickable region border and button contents (text).
19 const int kHorizontalPadding = 12; 20 const int kHorizontalPadding = 12;
20 const int kVerticalPadding = 6; 21 const int kVerticalPadding = 6;
21 22
22 // Minimum size to reserve for the button contents. 23 // Minimum size to reserve for the button contents.
23 const int kMinWidth = 48; 24 const int kMinWidth = 48;
24 25
26 // The amount to enlarge the focus border in all directions relative to the
27 // button.
28 const int kFocusBorderOutset = -4;
29
30 // The corner radius of the focus border roundrect.
31 const int kFocusBorderCornerRadius = 3;
32
33 class MdFocusRing : public views::View {
sky 2016/03/15 16:49:26 Seems rather heavy to create a view for the focus
Evan Stade 2016/03/15 19:09:43 The focus ring sits outside the button. We can't a
34 public:
35 MdFocusRing() {
36 SetPaintToLayer(true);
37 layer()->SetFillsBoundsOpaquely(false);
38 }
39 ~MdFocusRing() override {}
40
41 void OnPaint(gfx::Canvas* canvas) override {
42 SkPaint paint;
43 paint.setAntiAlias(true);
44 paint.setColor(GetNativeTheme()->GetSystemColor(
45 ui::NativeTheme::kColorId_CallToActionColor));
46 paint.setStyle(SkPaint::kStroke_Style);
47 paint.setStrokeWidth(1);
48 gfx::RectF rect(GetLocalBounds());
49 rect.Inset(gfx::InsetsF(0.5));
50 canvas->DrawRoundRect(rect, kFocusBorderCornerRadius, paint);
51 }
52
53 private:
54 DISALLOW_COPY_AND_ASSIGN(MdFocusRing);
55 };
56
25 } // namespace 57 } // namespace
26 58
27 // static 59 // static
28 LabelButton* MdTextButton::CreateStandardButton(ButtonListener* listener, 60 LabelButton* MdTextButton::CreateStandardButton(ButtonListener* listener,
29 const base::string16& text) { 61 const base::string16& text) {
30 if (ui::MaterialDesignController::IsModeMaterial()) { 62 if (ui::MaterialDesignController::IsModeMaterial()) {
31 MdTextButton* button = new MdTextButton(listener); 63 MdTextButton* button = new MdTextButton(listener);
32 button->SetText(text); 64 button->SetText(text);
33 // TODO(estade): can we get rid of the platform style border hoopla if 65 // TODO(estade): can we get rid of the platform style border hoopla if
34 // we apply the MD treatment to all buttons, even GTK buttons? 66 // we apply the MD treatment to all buttons, even GTK buttons?
35 button->SetBorder( 67 button->SetBorder(
36 Border::CreateEmptyBorder(kVerticalPadding, kHorizontalPadding, 68 Border::CreateEmptyBorder(kVerticalPadding, kHorizontalPadding,
37 kVerticalPadding, kHorizontalPadding)); 69 kVerticalPadding, kHorizontalPadding));
38 return button; 70 return button;
39 } 71 }
40 72
41 LabelButton* button = new LabelButton(listener, text); 73 LabelButton* button = new LabelButton(listener, text);
42 button->SetStyle(STYLE_BUTTON); 74 button->SetStyle(STYLE_BUTTON);
43 return button; 75 return button;
44 } 76 }
45 77
78 void MdTextButton::Layout() {
79 LabelButton::Layout();
80
81 gfx::Rect focus_bounds = GetLocalBounds();
82 focus_bounds.Inset(gfx::Insets(kFocusBorderOutset));
83 focus_ring_->SetBoundsRect(focus_bounds);
84 }
85
86 void MdTextButton::OnFocus() {
87 View::OnFocus();
88 focus_ring_->SetVisible(true);
89 }
90
91 void MdTextButton::OnBlur() {
92 View::OnBlur();
93 focus_ring_->SetVisible(false);
94 }
95
46 SkColor MdTextButton::GetInkDropBaseColor() const { 96 SkColor MdTextButton::GetInkDropBaseColor() const {
47 return color_utils::DeriveDefaultIconColor(label()->enabled_color()); 97 return color_utils::DeriveDefaultIconColor(label()->enabled_color());
48 } 98 }
49 99
50 void MdTextButton::SetText(const base::string16& text) { 100 void MdTextButton::SetText(const base::string16& text) {
51 LabelButton::SetText(base::i18n::ToUpper(text)); 101 LabelButton::SetText(base::i18n::ToUpper(text));
52 } 102 }
53 103
54 MdTextButton::MdTextButton(ButtonListener* listener) 104 MdTextButton::MdTextButton(ButtonListener* listener)
55 : LabelButton(listener, base::string16()), 105 : LabelButton(listener, base::string16()),
56 ink_drop_delegate_(this, this) { 106 ink_drop_delegate_(this, this),
107 focus_ring_(new MdFocusRing()) {
57 set_ink_drop_delegate(&ink_drop_delegate_); 108 set_ink_drop_delegate(&ink_drop_delegate_);
58 set_has_ink_drop_action_on_click(true); 109 set_has_ink_drop_action_on_click(true);
59 SetHorizontalAlignment(gfx::ALIGN_CENTER); 110 SetHorizontalAlignment(gfx::ALIGN_CENTER);
60 SetFocusable(true); 111 SetFocusable(true);
61 // TODO(estade): create a focus painter. 112 SetMinSize(gfx::Size(kMinWidth, 0));
113
114 AddChildView(focus_ring_);
115 focus_ring_->SetVisible(false);
62 SetFocusPainter(nullptr); 116 SetFocusPainter(nullptr);
63 SetMinSize(gfx::Size(kMinWidth, 0)); 117 set_request_focus_on_press(false);
64 } 118 }
65 119
66 MdTextButton::~MdTextButton() {} 120 MdTextButton::~MdTextButton() {}
67 121
68 } // namespace views 122 } // namespace views
OLDNEW
« no previous file with comments | « ui/views/controls/button/md_text_button.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698