1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/focus_ring.h"
#include "ui/gfx/canvas.h"
namespace views {
namespace {
// The default stroke width of the focus border in dp.
constexpr float kFocusHaloThicknessDp = 2.f;
FocusRing* GetFocusRing(View* parent) {
for (int i = 0; i < parent->child_count(); ++i) {
if (parent->child_at(i)->GetClassName() == FocusRing::kViewClassName)
return static_cast<FocusRing*>(parent->child_at(i));
}
return nullptr;
}
} // namespace
const char FocusRing::kViewClassName[] = "FocusRing";
// static
FocusRing* FocusRing::Install(View* parent,
SkColor color,
float corner_radius) {
FocusRing* ring = GetFocusRing(parent);
if (!ring) {
ring = new FocusRing(color, corner_radius);
parent->AddChildView(ring);
} else {
ring->color_ = color;
ring->corner_radius_ = corner_radius;
}
ring->Layout();
ring->SchedulePaint();
return ring;
}
// static
FocusRing* FocusRing::Install(View* parent,
ui::NativeTheme::ColorId override_color_id) {
SkColor ring_color = parent->GetNativeTheme()->GetSystemColor(
override_color_id == ui::NativeTheme::kColorId_NumColors
? ui::NativeTheme::kColorId_FocusedBorderColor
: override_color_id);
FocusRing* ring = Install(parent, ring_color);
DCHECK(ring);
ring->override_color_id_ = override_color_id;
if (!ring->view_observer_.IsObserving(parent))
ring->view_observer_.Add(parent);
return ring;
}
// static
void FocusRing::Uninstall(View* parent) {
delete GetFocusRing(parent);
}
// static
void FocusRing::InitFocusRing(View* view) {
// A layer is necessary to paint beyond the parent's bounds.
view->SetPaintToLayer();
view->layer()->SetFillsBoundsOpaquely(false);
// Don't allow the view to process events.
view->set_can_process_events_within_subtree(false);
}
const char* FocusRing::GetClassName() const {
return kViewClassName;
}
void FocusRing::Layout() {
// The focus ring handles its own sizing, which is simply to fill the parent
// and extend a little beyond its borders.
gfx::Rect focus_bounds = parent()->GetLocalBounds();
focus_bounds.Inset(gfx::Insets(-kFocusHaloThicknessDp));
SetBoundsRect(focus_bounds);
}
void FocusRing::OnPaint(gfx::Canvas* canvas) {
cc::PaintFlags flags;
flags.setAntiAlias(true);
flags.setColor(SkColorSetA(color_, 0x66));
flags.setStyle(cc::PaintFlags::kStroke_Style);
flags.setStrokeWidth(kFocusHaloThicknessDp);
gfx::RectF rect(GetLocalBounds());
rect.Inset(gfx::InsetsF(kFocusHaloThicknessDp / 2.f));
// The focus indicator should hug the normal border, when present (as in the
// case of text buttons). Since it's drawn outside the parent view, increase
// the rounding slightly by adding half the ring thickness.
canvas->DrawRoundRect(rect, corner_radius_ + kFocusHaloThicknessDp / 2.f,
flags);
}
void FocusRing::OnViewNativeThemeChanged(View* observed_view) {
if (override_color_id_) {
color_ = observed_view->GetNativeTheme()->GetSystemColor(
override_color_id_.value());
}
}
FocusRing::FocusRing(SkColor color, float corner_radius)
: color_(color), corner_radius_(corner_radius), view_observer_(this) {
InitFocusRing(this);
}
FocusRing::~FocusRing() {}
} // namespace views
|