OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/views/controls/focus_ring.h" |
| 6 |
| 7 #include "ui/gfx/canvas.h" |
| 8 #include "ui/native_theme/native_theme.h" |
| 9 #include "ui/views/controls/focusable_border.h" |
| 10 |
| 11 namespace views { |
| 12 |
| 13 namespace { |
| 14 |
| 15 // The stroke width of the focus border in dp. |
| 16 constexpr float kFocusHaloThicknessDp = 2.f; |
| 17 |
| 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 |
| 20 // increase the rounding slightly. |
| 21 constexpr float kFocusHaloCornerRadiusDp = |
| 22 FocusableBorder::kCornerRadiusDp + kFocusHaloThicknessDp / 2.f; |
| 23 |
| 24 } // namespace |
| 25 |
| 26 const char FocusRing::kViewClassName[] = "FocusRing"; |
| 27 |
| 28 // static |
| 29 void FocusRing::Install(views::View* parent) { |
| 30 DCHECK(parent->HasFocus()); |
| 31 View* ring = new FocusRing(); |
| 32 parent->AddChildView(ring); |
| 33 ring->Layout(); |
| 34 } |
| 35 |
| 36 // static |
| 37 void FocusRing::Uninstall(views::View* parent) { |
| 38 for (int i = 0; i < parent->child_count(); ++i) { |
| 39 if (parent->child_at(i)->GetClassName() == kViewClassName) { |
| 40 delete parent->child_at(i); |
| 41 return; |
| 42 } |
| 43 } |
| 44 NOTREACHED(); |
| 45 } |
| 46 |
| 47 const char* FocusRing::GetClassName() const { |
| 48 return kViewClassName; |
| 49 } |
| 50 |
| 51 bool FocusRing::CanProcessEventsWithinSubtree() const { |
| 52 return false; |
| 53 } |
| 54 |
| 55 void FocusRing::Layout() { |
| 56 // The focus ring handles its own sizing, which is simply to fill the parent |
| 57 // and extend a little beyond its borders. |
| 58 gfx::Rect focus_bounds = parent()->GetLocalBounds(); |
| 59 focus_bounds.Inset(gfx::Insets(-kFocusHaloThicknessDp)); |
| 60 SetBoundsRect(focus_bounds); |
| 61 } |
| 62 |
| 63 void FocusRing::OnPaint(gfx::Canvas* canvas) { |
| 64 SkPaint paint; |
| 65 paint.setAntiAlias(true); |
| 66 paint.setColor(SkColorSetA(GetNativeTheme()->GetSystemColor( |
| 67 ui::NativeTheme::kColorId_FocusedBorderColor), |
| 68 0x66)); |
| 69 paint.setStyle(SkPaint::kStroke_Style); |
| 70 paint.setStrokeWidth(kFocusHaloThicknessDp); |
| 71 gfx::RectF rect(GetLocalBounds()); |
| 72 rect.Inset(gfx::InsetsF(kFocusHaloThicknessDp / 2.f)); |
| 73 canvas->DrawRoundRect(rect, kFocusHaloCornerRadiusDp, paint); |
| 74 } |
| 75 |
| 76 FocusRing::FocusRing() { |
| 77 // A layer is necessary to paint beyond the parent's bounds. |
| 78 SetPaintToLayer(true); |
| 79 layer()->SetFillsBoundsOpaquely(false); |
| 80 } |
| 81 |
| 82 FocusRing::~FocusRing() {} |
| 83 |
| 84 } // namespace views |
OLD | NEW |