Chromium Code Reviews| 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 views::Widget* dimmer = new views::Widget(); | |
| 42 dimmer->Init(params); | |
| 43 dimmer->GetNativeWindow()->SetName("ShelfDimmer"); | |
|
James Cook
2016/08/03 03:09:10
nit: use params.name = "ShelfDimmer";
I'm trying
msw
2016/08/03 16:48:23
Done.
| |
| 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->GetNativeView()->SetName("ShelfDimmerView"); | |
| 50 dimmer->Show(); | |
| 51 return view; | |
| 52 } | |
| 53 | |
| 54 DimmerView::DimmerView(WmShelf* shelf, bool disable_animations_for_test) | |
| 55 : shelf_(shelf), | |
| 56 alpha_(kDimAlpha), | |
| 57 is_hovered_(false), | |
| 58 force_hovered_(false), | |
| 59 disable_animations_for_test_(disable_animations_for_test), | |
| 60 background_animator_(this, 0, kDimAlpha) { | |
| 61 event_filter_.reset(new DimmerEventFilter(this)); | |
| 62 // Make sure it is undimmed at the beginning and then fire off the dimming | |
| 63 // animation. | |
| 64 background_animator_.SetPaintsBackground(false, BACKGROUND_CHANGE_IMMEDIATE); | |
| 65 SetHovered(false); | |
| 66 shelf_->GetWindow()->AddObserver(this); | |
| 67 } | |
| 68 | |
| 69 DimmerView::~DimmerView() { | |
| 70 // Some unit tests will come here with a destroyed window. | |
| 71 if (shelf_->GetWindow()) | |
| 72 shelf_->GetWindow()->RemoveObserver(this); | |
| 73 } | |
| 74 | |
| 75 void DimmerView::SetHovered(bool hovered) { | |
| 76 // Remember the hovered state so that we can correct the state once a | |
| 77 // possible force state has disappeared. | |
| 78 is_hovered_ = hovered; | |
| 79 // Undimm also if we were forced to by e.g. an open menu. | |
|
James Cook
2016/08/03 03:09:10
nit: Undimm -> Undim (not your fault, I know)
msw
2016/08/03 16:48:24
Done.
| |
| 80 hovered |= force_hovered_; | |
| 81 background_animator_.SetDuration(hovered ? kTimeToUnDimMs : kTimeToDimMs); | |
| 82 background_animator_.SetPaintsBackground( | |
| 83 !hovered, disable_animations_for_test_ ? BACKGROUND_CHANGE_IMMEDIATE | |
| 84 : BACKGROUND_CHANGE_ANIMATE); | |
| 85 } | |
| 86 | |
| 87 void DimmerView::ForceUndimming(bool force) { | |
| 88 bool previous = force_hovered_; | |
| 89 force_hovered_ = force; | |
| 90 // If the forced change does change the result we apply the change. | |
| 91 if (is_hovered_ || force_hovered_ != is_hovered_ || previous) | |
| 92 SetHovered(is_hovered_); | |
| 93 } | |
| 94 | |
| 95 views::Widget* DimmerView::GetWidget() { | |
| 96 return View::GetWidget(); | |
| 97 } | |
| 98 | |
| 99 const views::Widget* DimmerView::GetWidget() const { | |
| 100 return View::GetWidget(); | |
| 101 } | |
| 102 | |
| 103 void DimmerView::UpdateBackground(BackgroundAnimator* animator, int alpha) { | |
| 104 alpha_ = alpha; | |
| 105 SchedulePaint(); | |
| 106 } | |
| 107 | |
| 108 void DimmerView::BackgroundAnimationEnded(BackgroundAnimator* animator) {} | |
| 109 | |
| 110 void DimmerView::OnPaintBackground(gfx::Canvas* canvas) { | |
| 111 SkPaint paint; | |
| 112 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | |
| 113 gfx::ImageSkia shelf_background = | |
| 114 *rb->GetImageNamed(IDR_ASH_SHELF_DIMMING).ToImageSkia(); | |
| 115 | |
| 116 if (!IsHorizontalAlignment(shelf_->GetAlignment())) { | |
| 117 shelf_background = gfx::ImageSkiaOperations::CreateRotatedImage( | |
| 118 shelf_background, shelf_->GetAlignment() == SHELF_ALIGNMENT_LEFT | |
| 119 ? SkBitmapOperations::ROTATION_90_CW | |
| 120 : SkBitmapOperations::ROTATION_270_CW); | |
| 121 } | |
| 122 paint.setAlpha(alpha_); | |
| 123 canvas->DrawImageInt(shelf_background, 0, 0, shelf_background.width(), | |
| 124 shelf_background.height(), 0, 0, width(), height(), | |
| 125 false, paint); | |
| 126 } | |
| 127 | |
| 128 void DimmerView::OnWindowBoundsChanged(WmWindow* window, | |
| 129 const gfx::Rect& old_bounds, | |
| 130 const gfx::Rect& new_bounds) { | |
| 131 // Coming here the shelf got repositioned and since the dimmer is placed | |
| 132 // in screen coordinates and not relative to the parent it needs to be | |
| 133 // repositioned accordingly. | |
| 134 GetWidget()->SetBounds(shelf_->GetWindow()->GetBoundsInScreen()); | |
| 135 } | |
| 136 | |
| 137 DimmerView::DimmerEventFilter::DimmerEventFilter(DimmerView* owner) | |
| 138 : owner_(owner), mouse_inside_(false), touch_inside_(false) { | |
| 139 Shell::GetInstance()->AddPreTargetHandler(this); | |
| 140 } | |
| 141 | |
| 142 DimmerView::DimmerEventFilter::~DimmerEventFilter() { | |
| 143 Shell::GetInstance()->RemovePreTargetHandler(this); | |
| 144 } | |
| 145 | |
| 146 void DimmerView::DimmerEventFilter::OnMouseEvent(ui::MouseEvent* event) { | |
| 147 if (event->type() != ui::ET_MOUSE_MOVED && | |
| 148 event->type() != ui::ET_MOUSE_DRAGGED) | |
| 149 return; | |
| 150 | |
| 151 gfx::Point screen_point(event->location()); | |
| 152 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()), | |
| 153 &screen_point); | |
| 154 bool inside = owner_->GetBoundsInScreen().Contains(screen_point); | |
| 155 if (mouse_inside_ || touch_inside_ != inside || touch_inside_) | |
| 156 owner_->SetHovered(inside || touch_inside_); | |
| 157 mouse_inside_ = inside; | |
| 158 } | |
| 159 | |
| 160 void DimmerView::DimmerEventFilter::OnTouchEvent(ui::TouchEvent* event) { | |
| 161 bool touch_inside = false; | |
| 162 if (event->type() != ui::ET_TOUCH_RELEASED && | |
| 163 event->type() != ui::ET_TOUCH_CANCELLED) { | |
| 164 gfx::Point screen_point(event->location()); | |
| 165 ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()), | |
| 166 &screen_point); | |
| 167 touch_inside = owner_->GetBoundsInScreen().Contains(screen_point); | |
| 168 } | |
| 169 | |
| 170 if (mouse_inside_ || touch_inside_ != mouse_inside_ || touch_inside) | |
| 171 owner_->SetHovered(mouse_inside_ || touch_inside); | |
| 172 touch_inside_ = touch_inside; | |
| 173 } | |
| 174 | |
| 175 } // namespace ash | |
| OLD | NEW |