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

Side by Side Diff: ui/views/controls/focus_ring.cc

Issue 2406363003: Update appearance of invalid textfields in Harmony. (Closed)
Patch Set: 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/focus_ring.h" 5 #include "ui/views/controls/focus_ring.h"
6 6
7 #include "ui/gfx/canvas.h" 7 #include "ui/gfx/canvas.h"
8 #include "ui/native_theme/native_theme.h" 8 #include "ui/native_theme/native_theme.h"
9 #include "ui/views/controls/focusable_border.h" 9 #include "ui/views/controls/focusable_border.h"
10 10
11 namespace views { 11 namespace views {
12 12
13 namespace { 13 namespace {
14 14
15 // The stroke width of the focus border in dp. 15 // The stroke width of the focus border in dp.
16 constexpr float kFocusHaloThicknessDp = 2.f; 16 constexpr float kFocusHaloThicknessDp = 2.f;
17 17
18 // The focus indicator should hug the normal border, when present (as in the 18 // The focus indicator should hug the normal border, when present (as in the
19 // case of text buttons). Since it's drawn outside the parent view, we have to 19 // case of text buttons). Since it's drawn outside the parent view, we have to
20 // increase the rounding slightly. 20 // increase the rounding slightly.
21 constexpr float kFocusHaloCornerRadiusDp = 21 constexpr float kFocusHaloCornerRadiusDp =
22 FocusableBorder::kCornerRadiusDp + kFocusHaloThicknessDp / 2.f; 22 FocusableBorder::kCornerRadiusDp + kFocusHaloThicknessDp / 2.f;
23 23
24 FocusRing* GetFocusRing(views::View* parent) {
25 for (int i = 0; i < parent->child_count(); ++i) {
26 if (parent->child_at(i)->GetClassName() == FocusRing::kViewClassName)
27 return static_cast<FocusRing*>(parent->child_at(i));
28 }
29 NOTREACHED();
30 return nullptr;
31 }
32
24 } // namespace 33 } // namespace
25 34
26 const char FocusRing::kViewClassName[] = "FocusRing"; 35 const char FocusRing::kViewClassName[] = "FocusRing";
27 36
28 // static 37 // static
29 void FocusRing::Install(views::View* parent) { 38 void FocusRing::Install(views::View* parent) {
30 DCHECK(parent->HasFocus()); 39 DCHECK(parent->HasFocus());
31 View* ring = new FocusRing(); 40 View* ring = new FocusRing();
32 parent->AddChildView(ring); 41 parent->AddChildView(ring);
33 ring->Layout(); 42 ring->Layout();
34 } 43 }
35 44
36 // static 45 // static
37 void FocusRing::Uninstall(views::View* parent) { 46 void FocusRing::Uninstall(views::View* parent) {
38 for (int i = 0; i < parent->child_count(); ++i) { 47 delete GetFocusRing(parent);
39 if (parent->child_at(i)->GetClassName() == kViewClassName) { 48 }
40 delete parent->child_at(i); 49
41 return; 50 // static
42 } 51 void FocusRing::SetColorId(
43 } 52 views::View* parent,
44 NOTREACHED(); 53 const base::Optional<ui::NativeTheme::ColorId>& override_color_id) {
54 FocusRing* ring = GetFocusRing(parent);
55 ring->override_color_id_ = override_color_id;
56 ring->SchedulePaint();
45 } 57 }
46 58
47 const char* FocusRing::GetClassName() const { 59 const char* FocusRing::GetClassName() const {
48 return kViewClassName; 60 return kViewClassName;
49 } 61 }
50 62
51 bool FocusRing::CanProcessEventsWithinSubtree() const { 63 bool FocusRing::CanProcessEventsWithinSubtree() const {
52 return false; 64 return false;
53 } 65 }
54 66
55 void FocusRing::Layout() { 67 void FocusRing::Layout() {
56 // The focus ring handles its own sizing, which is simply to fill the parent 68 // The focus ring handles its own sizing, which is simply to fill the parent
57 // and extend a little beyond its borders. 69 // and extend a little beyond its borders.
58 gfx::Rect focus_bounds = parent()->GetLocalBounds(); 70 gfx::Rect focus_bounds = parent()->GetLocalBounds();
59 focus_bounds.Inset(gfx::Insets(-kFocusHaloThicknessDp)); 71 focus_bounds.Inset(gfx::Insets(-kFocusHaloThicknessDp));
60 SetBoundsRect(focus_bounds); 72 SetBoundsRect(focus_bounds);
61 } 73 }
62 74
63 void FocusRing::OnPaint(gfx::Canvas* canvas) { 75 void FocusRing::OnPaint(gfx::Canvas* canvas) {
64 SkPaint paint; 76 SkPaint paint;
65 paint.setAntiAlias(true); 77 paint.setAntiAlias(true);
66 paint.setColor(SkColorSetA(GetNativeTheme()->GetSystemColor( 78 paint.setColor(SkColorSetA(
67 ui::NativeTheme::kColorId_FocusedBorderColor), 79 GetNativeTheme()->GetSystemColor(
68 0x66)); 80 override_color_id_ ? *override_color_id_
81 : ui::NativeTheme::kColorId_FocusedBorderColor),
82 0x66));
69 paint.setStyle(SkPaint::kStroke_Style); 83 paint.setStyle(SkPaint::kStroke_Style);
70 paint.setStrokeWidth(kFocusHaloThicknessDp); 84 paint.setStrokeWidth(kFocusHaloThicknessDp);
71 gfx::RectF rect(GetLocalBounds()); 85 gfx::RectF rect(GetLocalBounds());
72 rect.Inset(gfx::InsetsF(kFocusHaloThicknessDp / 2.f)); 86 rect.Inset(gfx::InsetsF(kFocusHaloThicknessDp / 2.f));
73 canvas->DrawRoundRect(rect, kFocusHaloCornerRadiusDp, paint); 87 canvas->DrawRoundRect(rect, kFocusHaloCornerRadiusDp, paint);
74 } 88 }
75 89
76 FocusRing::FocusRing() { 90 FocusRing::FocusRing() {
77 // A layer is necessary to paint beyond the parent's bounds. 91 // A layer is necessary to paint beyond the parent's bounds.
78 SetPaintToLayer(true); 92 SetPaintToLayer(true);
79 layer()->SetFillsBoundsOpaquely(false); 93 layer()->SetFillsBoundsOpaquely(false);
80 } 94 }
81 95
82 FocusRing::~FocusRing() {} 96 FocusRing::~FocusRing() {}
83 97
84 } // namespace views 98 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698