| OLD | NEW |
| 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 return nullptr; |
| 30 } |
| 31 |
| 24 } // namespace | 32 } // namespace |
| 25 | 33 |
| 26 const char FocusRing::kViewClassName[] = "FocusRing"; | 34 const char FocusRing::kViewClassName[] = "FocusRing"; |
| 27 | 35 |
| 28 // static | 36 // static |
| 29 void FocusRing::Install(views::View* parent) { | 37 void FocusRing::Install(views::View* parent, |
| 38 ui::NativeTheme::ColorId override_color_id) { |
| 30 DCHECK(parent->HasFocus()); | 39 DCHECK(parent->HasFocus()); |
| 31 View* ring = new FocusRing(); | 40 FocusRing* ring = GetFocusRing(parent); |
| 32 parent->AddChildView(ring); | 41 if (!ring) { |
| 42 ring = new FocusRing(); |
| 43 parent->AddChildView(ring); |
| 44 } |
| 45 ring->override_color_id_ = override_color_id; |
| 33 ring->Layout(); | 46 ring->Layout(); |
| 47 ring->SchedulePaint(); |
| 34 } | 48 } |
| 35 | 49 |
| 36 // static | 50 // static |
| 37 void FocusRing::Uninstall(views::View* parent) { | 51 void FocusRing::Uninstall(views::View* parent) { |
| 38 for (int i = 0; i < parent->child_count(); ++i) { | 52 delete GetFocusRing(parent); |
| 39 if (parent->child_at(i)->GetClassName() == kViewClassName) { | |
| 40 delete parent->child_at(i); | |
| 41 return; | |
| 42 } | |
| 43 } | |
| 44 NOTREACHED(); | |
| 45 } | 53 } |
| 46 | 54 |
| 47 const char* FocusRing::GetClassName() const { | 55 const char* FocusRing::GetClassName() const { |
| 48 return kViewClassName; | 56 return kViewClassName; |
| 49 } | 57 } |
| 50 | 58 |
| 51 bool FocusRing::CanProcessEventsWithinSubtree() const { | 59 bool FocusRing::CanProcessEventsWithinSubtree() const { |
| 52 return false; | 60 return false; |
| 53 } | 61 } |
| 54 | 62 |
| 55 void FocusRing::Layout() { | 63 void FocusRing::Layout() { |
| 56 // The focus ring handles its own sizing, which is simply to fill the parent | 64 // The focus ring handles its own sizing, which is simply to fill the parent |
| 57 // and extend a little beyond its borders. | 65 // and extend a little beyond its borders. |
| 58 gfx::Rect focus_bounds = parent()->GetLocalBounds(); | 66 gfx::Rect focus_bounds = parent()->GetLocalBounds(); |
| 59 focus_bounds.Inset(gfx::Insets(-kFocusHaloThicknessDp)); | 67 focus_bounds.Inset(gfx::Insets(-kFocusHaloThicknessDp)); |
| 60 SetBoundsRect(focus_bounds); | 68 SetBoundsRect(focus_bounds); |
| 61 } | 69 } |
| 62 | 70 |
| 63 void FocusRing::OnPaint(gfx::Canvas* canvas) { | 71 void FocusRing::OnPaint(gfx::Canvas* canvas) { |
| 64 SkPaint paint; | 72 SkPaint paint; |
| 65 paint.setAntiAlias(true); | 73 paint.setAntiAlias(true); |
| 66 paint.setColor(SkColorSetA(GetNativeTheme()->GetSystemColor( | 74 paint.setColor( |
| 67 ui::NativeTheme::kColorId_FocusedBorderColor), | 75 SkColorSetA(GetNativeTheme()->GetSystemColor( |
| 68 0x66)); | 76 override_color_id_ != ui::NativeTheme::kColorId_NumColors |
| 77 ? override_color_id_ |
| 78 : ui::NativeTheme::kColorId_FocusedBorderColor), |
| 79 0x66)); |
| 69 paint.setStyle(SkPaint::kStroke_Style); | 80 paint.setStyle(SkPaint::kStroke_Style); |
| 70 paint.setStrokeWidth(kFocusHaloThicknessDp); | 81 paint.setStrokeWidth(kFocusHaloThicknessDp); |
| 71 gfx::RectF rect(GetLocalBounds()); | 82 gfx::RectF rect(GetLocalBounds()); |
| 72 rect.Inset(gfx::InsetsF(kFocusHaloThicknessDp / 2.f)); | 83 rect.Inset(gfx::InsetsF(kFocusHaloThicknessDp / 2.f)); |
| 73 canvas->DrawRoundRect(rect, kFocusHaloCornerRadiusDp, paint); | 84 canvas->DrawRoundRect(rect, kFocusHaloCornerRadiusDp, paint); |
| 74 } | 85 } |
| 75 | 86 |
| 76 FocusRing::FocusRing() { | 87 FocusRing::FocusRing() |
| 88 : override_color_id_(ui::NativeTheme::kColorId_NumColors) { |
| 77 // A layer is necessary to paint beyond the parent's bounds. | 89 // A layer is necessary to paint beyond the parent's bounds. |
| 78 SetPaintToLayer(true); | 90 SetPaintToLayer(true); |
| 79 layer()->SetFillsBoundsOpaquely(false); | 91 layer()->SetFillsBoundsOpaquely(false); |
| 80 } | 92 } |
| 81 | 93 |
| 82 FocusRing::~FocusRing() {} | 94 FocusRing::~FocusRing() {} |
| 83 | 95 |
| 84 } // namespace views | 96 } // namespace views |
| OLD | NEW |