| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/chromeos/ui/focus_ring_layer.h" | 5 #include "chrome/browser/chromeos/ui/focus_ring_layer.h" |
| 6 | 6 |
| 7 #include "ash/system/tray/actionable_view.h" | |
| 8 #include "ash/system/tray/tray_background_view.h" | |
| 9 #include "ash/system/tray/tray_popup_header_button.h" | |
| 10 #include "base/bind.h" | 7 #include "base/bind.h" |
| 11 #include "ui/aura/window.h" | 8 #include "ui/aura/window.h" |
| 12 #include "ui/compositor/layer.h" | 9 #include "ui/compositor/layer.h" |
| 13 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 14 #include "ui/views/controls/button/label_button.h" | |
| 15 #include "ui/views/view.h" | |
| 16 #include "ui/views/widget/widget.h" | |
| 17 | 11 |
| 18 namespace chromeos { | 12 namespace chromeos { |
| 19 | 13 |
| 20 namespace { | 14 namespace { |
| 21 | 15 |
| 22 const int kShadowRadius = 10; | 16 const int kShadowRadius = 10; |
| 23 const int kShadowAlpha = 90; | 17 const int kShadowAlpha = 90; |
| 24 const SkColor kShadowColor = SkColorSetRGB(77, 144, 254); | 18 const SkColor kShadowColor = SkColorSetRGB(77, 144, 254); |
| 25 | 19 |
| 26 } // namespace | 20 } // namespace |
| 27 | 21 |
| 28 FocusRingLayer::FocusRingLayer() | 22 FocusRingLayerDelegate::~FocusRingLayerDelegate() {} |
| 29 : window_(NULL), | 23 |
| 24 FocusRingLayer::FocusRingLayer(FocusRingLayerDelegate* delegate) |
| 25 : delegate_(delegate), |
| 30 root_window_(NULL) { | 26 root_window_(NULL) { |
| 31 } | 27 } |
| 32 | 28 |
| 33 FocusRingLayer::~FocusRingLayer() {} | 29 FocusRingLayer::~FocusRingLayer() {} |
| 34 | 30 |
| 35 void FocusRingLayer::Update() { | 31 void FocusRingLayer::Set(aura::Window* root_window, const gfx::Rect& bounds) { |
| 36 if (!window_) | 32 focus_ring_ = bounds; |
| 37 return; | |
| 38 | |
| 39 aura::Window* root_window = window_->GetRootWindow(); | |
| 40 if (!layer_ || root_window != root_window_) { | 33 if (!layer_ || root_window != root_window_) { |
| 41 root_window_ = root_window; | 34 root_window_ = root_window; |
| 42 ui::Layer* root_layer = root_window->layer(); | 35 ui::Layer* root_layer = root_window->layer(); |
| 43 layer_.reset(new ui::Layer(ui::LAYER_TEXTURED)); | 36 layer_.reset(new ui::Layer(ui::LAYER_TEXTURED)); |
| 44 layer_->set_name("FocusRing"); | 37 layer_->set_name("FocusRing"); |
| 45 layer_->set_delegate(this); | 38 layer_->set_delegate(this); |
| 46 layer_->SetFillsBoundsOpaquely(false); | 39 layer_->SetFillsBoundsOpaquely(false); |
| 47 root_layer->Add(layer_.get()); | 40 root_layer->Add(layer_.get()); |
| 48 } | 41 } |
| 49 | 42 |
| 50 // Keep moving it to the top in case new layers have been added | 43 // Keep moving it to the top in case new layers have been added |
| 51 // since we created this layer. | 44 // since we created this layer. |
| 52 layer_->parent()->StackAtTop(layer_.get()); | 45 layer_->parent()->StackAtTop(layer_.get()); |
| 53 | 46 |
| 54 // Translate native window coordinates to root window coordinates. | 47 // Update the layer bounds. |
| 55 gfx::Point origin = focus_ring_.origin(); | 48 gfx::Rect layer_bounds = bounds; |
| 56 aura::Window::ConvertPointToTarget(window_, root_window_, &origin); | |
| 57 gfx::Rect layer_bounds = focus_ring_; | |
| 58 layer_bounds.set_origin(origin); | |
| 59 int inset = -(kShadowRadius + 2); | 49 int inset = -(kShadowRadius + 2); |
| 60 layer_bounds.Inset(inset, inset, inset, inset); | 50 layer_bounds.Inset(inset, inset, inset, inset); |
| 61 layer_->SetBounds(layer_bounds); | 51 layer_->SetBounds(layer_bounds); |
| 62 } | 52 } |
| 63 | 53 |
| 64 void FocusRingLayer::SetForView(views::View* view) { | |
| 65 if (!view) { | |
| 66 if (layer_ && !focus_ring_.IsEmpty()) | |
| 67 layer_->SchedulePaint(focus_ring_); | |
| 68 focus_ring_ = gfx::Rect(); | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 DCHECK(view->GetWidget()); | |
| 73 window_ = view->GetWidget()->GetNativeWindow(); | |
| 74 | |
| 75 gfx::Rect view_bounds = view->GetContentsBounds(); | |
| 76 | |
| 77 // Workarounds that attempts to pick a better bounds. | |
| 78 if (view->GetClassName() == views::LabelButton::kViewClassName) { | |
| 79 view_bounds = view->GetLocalBounds(); | |
| 80 view_bounds.Inset(2, 2, 2, 2); | |
| 81 } | |
| 82 | |
| 83 // Workarounds for system tray items that have customized focus borders. The | |
| 84 // insets here must be consistent with the ones used by those classes. | |
| 85 if (view->GetClassName() == ash::ActionableView::kViewClassName) { | |
| 86 view_bounds = view->GetLocalBounds(); | |
| 87 view_bounds.Inset(1, 1, 3, 3); | |
| 88 } else if (view->GetClassName() == ash::TrayBackgroundView::kViewClassName) { | |
| 89 view_bounds.Inset(1, 1, 3, 3); | |
| 90 } else if (view->GetClassName() == | |
| 91 ash::TrayPopupHeaderButton::kViewClassName) { | |
| 92 view_bounds = view->GetLocalBounds(); | |
| 93 view_bounds.Inset(2, 1, 2, 2); | |
| 94 } | |
| 95 | |
| 96 focus_ring_ = view->ConvertRectToWidget(view_bounds); | |
| 97 Update(); | |
| 98 } | |
| 99 | |
| 100 void FocusRingLayer::OnPaintLayer(gfx::Canvas* canvas) { | 54 void FocusRingLayer::OnPaintLayer(gfx::Canvas* canvas) { |
| 101 if (focus_ring_.IsEmpty()) | 55 if (!root_window_ || focus_ring_.IsEmpty()) |
| 102 return; | 56 return; |
| 103 | 57 |
| 104 // Convert the focus ring from native-window-relative coordinates to | 58 gfx::Rect bounds = focus_ring_ - layer_->bounds().OffsetFromOrigin(); |
| 105 // layer-relative coordinates. | |
| 106 gfx::Point origin = focus_ring_.origin(); | |
| 107 aura::Window::ConvertPointToTarget(window_, root_window_, &origin); | |
| 108 origin -= layer_->bounds().OffsetFromOrigin(); | |
| 109 gfx::Rect bounds = focus_ring_; | |
| 110 bounds.set_origin(origin); | |
| 111 | |
| 112 SkPaint paint; | 59 SkPaint paint; |
| 113 paint.setColor(kShadowColor); | 60 paint.setColor(kShadowColor); |
| 114 paint.setFlags(SkPaint::kAntiAlias_Flag); | 61 paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 115 paint.setStyle(SkPaint::kStroke_Style); | 62 paint.setStyle(SkPaint::kStroke_Style); |
| 116 paint.setStrokeWidth(2); | 63 paint.setStrokeWidth(2); |
| 117 int r = kShadowRadius; | 64 int r = kShadowRadius; |
| 118 for (int i = 0; i < r; i++) { | 65 for (int i = 0; i < r; i++) { |
| 119 // Fade out alpha quadratically. | 66 // Fade out alpha quadratically. |
| 120 paint.setAlpha((kShadowAlpha * (r - i) * (r - i)) / (r * r)); | 67 paint.setAlpha((kShadowAlpha * (r - i) * (r - i)) / (r * r)); |
| 121 gfx::Rect outsetRect = bounds; | 68 gfx::Rect outsetRect = bounds; |
| 122 outsetRect.Inset(-i, -i, -i, -i); | 69 outsetRect.Inset(-i, -i, -i, -i); |
| 123 canvas->DrawRect(outsetRect, paint); | 70 canvas->DrawRect(outsetRect, paint); |
| 124 } | 71 } |
| 125 } | 72 } |
| 126 | 73 |
| 127 void FocusRingLayer::OnDelegatedFrameDamage( | 74 void FocusRingLayer::OnDelegatedFrameDamage( |
| 128 const gfx::Rect& damage_rect_in_dip) { | 75 const gfx::Rect& damage_rect_in_dip) { |
| 129 } | 76 } |
| 130 | 77 |
| 131 void FocusRingLayer::OnDeviceScaleFactorChanged(float device_scale_factor) { | 78 void FocusRingLayer::OnDeviceScaleFactorChanged(float device_scale_factor) { |
| 132 Update(); | 79 if (delegate_) |
| 80 delegate_->OnDeviceScaleFactorChanged(); |
| 133 } | 81 } |
| 134 | 82 |
| 135 base::Closure FocusRingLayer::PrepareForLayerBoundsChange() { | 83 base::Closure FocusRingLayer::PrepareForLayerBoundsChange() { |
| 136 return base::Bind(&base::DoNothing); | 84 return base::Bind(&base::DoNothing); |
| 137 } | 85 } |
| 138 | 86 |
| 139 } // namespace chromeos | 87 } // namespace chromeos |
| OLD | NEW |