| 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 "ash/shelf/dimmer_view.h" |
| 6 |
| 7 #include "ash/aura/wm_window_aura.h" |
| 8 #include "ash/common/shelf/wm_shelf.h" |
| 9 #include "ash/common/shelf/wm_shelf_util.h" |
| 10 #include "ash/shell.h" |
| 11 #include "grit/ash_resources.h" |
| 12 #include "ui/aura/window.h" |
| 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/canvas.h" |
| 15 #include "ui/gfx/image/image_skia_operations.h" |
| 16 #include "ui/views/widget/widget.h" |
| 17 #include "ui/wm/core/coordinate_conversion.h" |
| 18 |
| 19 namespace ash { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Alpha to paint dimming image with. |
| 24 const int kDimAlpha = 128; |
| 25 |
| 26 // The time to dim and un-dim. |
| 27 const int kTimeToDimMs = 3000; // Slow in dimming. |
| 28 const int kTimeToUnDimMs = 200; // Fast in activating. |
| 29 |
| 30 } // namespace |
| 31 |
| 32 // static |
| 33 DimmerView* DimmerView::Create(WmShelf* shelf, |
| 34 bool disable_animations_for_test) { |
| 35 views::Widget::InitParams params( |
| 36 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
| 37 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; |
| 38 params.activatable = views::Widget::InitParams::ACTIVATABLE_NO; |
| 39 params.accept_events = false; |
| 40 params.parent = WmWindowAura::GetAuraWindow(shelf->GetWindow()); |
| 41 params.name = "DimmerView"; |
| 42 views::Widget* dimmer = new views::Widget(); |
| 43 dimmer->Init(params); |
| 44 dimmer->SetBounds(shelf->GetWindow()->GetBoundsInScreen()); |
| 45 // The shelf should not take focus when it is initially shown. |
| 46 dimmer->set_focus_on_creation(false); |
| 47 DimmerView* view = new DimmerView(shelf, disable_animations_for_test); |
| 48 dimmer->SetContentsView(view); |
| 49 dimmer->Show(); |
| 50 return view; |
| 51 } |
| 52 |
| 53 void DimmerView::SetHovered(bool hovered) { |
| 54 // Remember the hovered state so that we can correct the state once a |
| 55 // possible force state has disappeared. |
| 56 is_hovered_ = hovered; |
| 57 // Undim also if we were forced to by e.g. an open menu. |
| 58 hovered |= force_hovered_; |
| 59 background_animator_.SetDuration(hovered ? kTimeToUnDimMs : kTimeToDimMs); |
| 60 background_animator_.SetPaintsBackground( |
| 61 !hovered, disable_animations_for_test_ ? BACKGROUND_CHANGE_IMMEDIATE |
| 62 : BACKGROUND_CHANGE_ANIMATE); |
| 63 } |
| 64 |
| 65 void DimmerView::ForceUndimming(bool force) { |
| 66 bool previous = force_hovered_; |
| 67 force_hovered_ = force; |
| 68 // If the forced change does change the result we apply the change. |
| 69 if (is_hovered_ || force_hovered_ != is_hovered_ || previous) |
| 70 SetHovered(is_hovered_); |
| 71 } |
| 72 |
| 73 void DimmerView::OnPaintBackground(gfx::Canvas* canvas) { |
| 74 SkPaint paint; |
| 75 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 76 gfx::ImageSkia shelf_background = |
| 77 *rb->GetImageNamed(IDR_ASH_SHELF_DIMMING).ToImageSkia(); |
| 78 |
| 79 if (!IsHorizontalAlignment(shelf_->GetAlignment())) { |
| 80 shelf_background = gfx::ImageSkiaOperations::CreateRotatedImage( |
| 81 shelf_background, shelf_->GetAlignment() == SHELF_ALIGNMENT_LEFT |
| 82 ? SkBitmapOperations::ROTATION_90_CW |
| 83 : SkBitmapOperations::ROTATION_270_CW); |
| 84 } |
| 85 paint.setAlpha(alpha_); |
| 86 canvas->DrawImageInt(shelf_background, 0, 0, shelf_background.width(), |
| 87 shelf_background.height(), 0, 0, width(), height(), |
| 88 false, paint); |
| 89 } |
| 90 |
| 91 views::Widget* DimmerView::GetWidget() { |
| 92 return View::GetWidget(); |
| 93 } |
| 94 |
| 95 const views::Widget* DimmerView::GetWidget() const { |
| 96 return View::GetWidget(); |
| 97 } |
| 98 |
| 99 void DimmerView::UpdateBackground(BackgroundAnimator* animator, int alpha) { |
| 100 alpha_ = alpha; |
| 101 SchedulePaint(); |
| 102 } |
| 103 |
| 104 void DimmerView::BackgroundAnimationEnded(BackgroundAnimator* animator) {} |
| 105 |
| 106 void DimmerView::OnWindowBoundsChanged(WmWindow* window, |
| 107 const gfx::Rect& old_bounds, |
| 108 const gfx::Rect& new_bounds) { |
| 109 // Coming here the shelf got repositioned and since the dimmer is placed |
| 110 // in screen coordinates and not relative to the parent it needs to be |
| 111 // repositioned accordingly. |
| 112 GetWidget()->SetBounds(shelf_->GetWindow()->GetBoundsInScreen()); |
| 113 } |
| 114 |
| 115 DimmerView::DimmerView(WmShelf* shelf, bool disable_animations_for_test) |
| 116 : shelf_(shelf), |
| 117 alpha_(kDimAlpha), |
| 118 is_hovered_(false), |
| 119 force_hovered_(false), |
| 120 disable_animations_for_test_(disable_animations_for_test), |
| 121 background_animator_(this, 0, kDimAlpha) { |
| 122 event_filter_.reset(new DimmerEventFilter(this)); |
| 123 // Make sure it is undimmed at the beginning and then fire off the dimming |
| 124 // animation. |
| 125 background_animator_.SetPaintsBackground(false, BACKGROUND_CHANGE_IMMEDIATE); |
| 126 SetHovered(false); |
| 127 shelf_->GetWindow()->AddObserver(this); |
| 128 } |
| 129 |
| 130 DimmerView::~DimmerView() { |
| 131 // Some unit tests will come here with a destroyed window. |
| 132 if (shelf_->GetWindow()) |
| 133 shelf_->GetWindow()->RemoveObserver(this); |
| 134 } |
| 135 |
| 136 DimmerView::DimmerEventFilter::DimmerEventFilter(DimmerView* owner) |
| 137 : owner_(owner), mouse_inside_(false), touch_inside_(false) { |
| 138 Shell::GetInstance()->AddPreTargetHandler(this); |
| 139 } |
| 140 |
| 141 DimmerView::DimmerEventFilter::~DimmerEventFilter() { |
| 142 Shell::GetInstance()->RemovePreTargetHandler(this); |
| 143 } |
| 144 |
| 145 void DimmerView::DimmerEventFilter::OnMouseEvent(ui::MouseEvent* event) { |
| 146 if (event->type() != ui::ET_MOUSE_MOVED && |
| 147 event->type() != ui::ET_MOUSE_DRAGGED) |
| 148 return; |
| 149 |
| 150 gfx::Point screen_point(event->location()); |
| 151 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()), |
| 152 &screen_point); |
| 153 bool inside = owner_->GetBoundsInScreen().Contains(screen_point); |
| 154 if (mouse_inside_ || touch_inside_ != inside || touch_inside_) |
| 155 owner_->SetHovered(inside || touch_inside_); |
| 156 mouse_inside_ = inside; |
| 157 } |
| 158 |
| 159 void DimmerView::DimmerEventFilter::OnTouchEvent(ui::TouchEvent* event) { |
| 160 bool touch_inside = false; |
| 161 if (event->type() != ui::ET_TOUCH_RELEASED && |
| 162 event->type() != ui::ET_TOUCH_CANCELLED) { |
| 163 gfx::Point screen_point(event->location()); |
| 164 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()), |
| 165 &screen_point); |
| 166 touch_inside = owner_->GetBoundsInScreen().Contains(screen_point); |
| 167 } |
| 168 |
| 169 if (mouse_inside_ || touch_inside_ != mouse_inside_ || touch_inside) |
| 170 owner_->SetHovered(mouse_inside_ || touch_inside); |
| 171 touch_inside_ = touch_inside; |
| 172 } |
| 173 |
| 174 } // namespace ash |
| OLD | NEW |