Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(347)

Unified Diff: ash/shelf/dimmer_view.cc

Issue 2204843003: mash: Move the shelf DimmerView out of shelf_widget.cc. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ash/shelf/dimmer_view.h ('k') | ash/shelf/shelf_widget.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ash/shelf/dimmer_view.cc
diff --git a/ash/shelf/dimmer_view.cc b/ash/shelf/dimmer_view.cc
new file mode 100644
index 0000000000000000000000000000000000000000..de49e040971486b89671a4b2f1362aaeeee6eac4
--- /dev/null
+++ b/ash/shelf/dimmer_view.cc
@@ -0,0 +1,174 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ash/shelf/dimmer_view.h"
+
+#include "ash/aura/wm_window_aura.h"
+#include "ash/common/shelf/wm_shelf.h"
+#include "ash/common/shelf/wm_shelf_util.h"
+#include "ash/shell.h"
+#include "grit/ash_resources.h"
+#include "ui/aura/window.h"
+#include "ui/base/resource/resource_bundle.h"
+#include "ui/gfx/canvas.h"
+#include "ui/gfx/image/image_skia_operations.h"
+#include "ui/views/widget/widget.h"
+#include "ui/wm/core/coordinate_conversion.h"
+
+namespace ash {
+
+namespace {
+
+// Alpha to paint dimming image with.
+const int kDimAlpha = 128;
+
+// The time to dim and un-dim.
+const int kTimeToDimMs = 3000; // Slow in dimming.
+const int kTimeToUnDimMs = 200; // Fast in activating.
+
+} // namespace
+
+// static
+DimmerView* DimmerView::Create(WmShelf* shelf,
+ bool disable_animations_for_test) {
+ views::Widget::InitParams params(
+ views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
+ params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
+ params.activatable = views::Widget::InitParams::ACTIVATABLE_NO;
+ params.accept_events = false;
+ params.parent = WmWindowAura::GetAuraWindow(shelf->GetWindow());
+ params.name = "DimmerView";
+ views::Widget* dimmer = new views::Widget();
+ dimmer->Init(params);
+ dimmer->SetBounds(shelf->GetWindow()->GetBoundsInScreen());
+ // The shelf should not take focus when it is initially shown.
+ dimmer->set_focus_on_creation(false);
+ DimmerView* view = new DimmerView(shelf, disable_animations_for_test);
+ dimmer->SetContentsView(view);
+ dimmer->Show();
+ return view;
+}
+
+void DimmerView::SetHovered(bool hovered) {
+ // Remember the hovered state so that we can correct the state once a
+ // possible force state has disappeared.
+ is_hovered_ = hovered;
+ // Undim also if we were forced to by e.g. an open menu.
+ hovered |= force_hovered_;
+ background_animator_.SetDuration(hovered ? kTimeToUnDimMs : kTimeToDimMs);
+ background_animator_.SetPaintsBackground(
+ !hovered, disable_animations_for_test_ ? BACKGROUND_CHANGE_IMMEDIATE
+ : BACKGROUND_CHANGE_ANIMATE);
+}
+
+void DimmerView::ForceUndimming(bool force) {
+ bool previous = force_hovered_;
+ force_hovered_ = force;
+ // If the forced change does change the result we apply the change.
+ if (is_hovered_ || force_hovered_ != is_hovered_ || previous)
+ SetHovered(is_hovered_);
+}
+
+void DimmerView::OnPaintBackground(gfx::Canvas* canvas) {
+ SkPaint paint;
+ ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
+ gfx::ImageSkia shelf_background =
+ *rb->GetImageNamed(IDR_ASH_SHELF_DIMMING).ToImageSkia();
+
+ if (!IsHorizontalAlignment(shelf_->GetAlignment())) {
+ shelf_background = gfx::ImageSkiaOperations::CreateRotatedImage(
+ shelf_background, shelf_->GetAlignment() == SHELF_ALIGNMENT_LEFT
+ ? SkBitmapOperations::ROTATION_90_CW
+ : SkBitmapOperations::ROTATION_270_CW);
+ }
+ paint.setAlpha(alpha_);
+ canvas->DrawImageInt(shelf_background, 0, 0, shelf_background.width(),
+ shelf_background.height(), 0, 0, width(), height(),
+ false, paint);
+}
+
+views::Widget* DimmerView::GetWidget() {
+ return View::GetWidget();
+}
+
+const views::Widget* DimmerView::GetWidget() const {
+ return View::GetWidget();
+}
+
+void DimmerView::UpdateBackground(BackgroundAnimator* animator, int alpha) {
+ alpha_ = alpha;
+ SchedulePaint();
+}
+
+void DimmerView::BackgroundAnimationEnded(BackgroundAnimator* animator) {}
+
+void DimmerView::OnWindowBoundsChanged(WmWindow* window,
+ const gfx::Rect& old_bounds,
+ const gfx::Rect& new_bounds) {
+ // Coming here the shelf got repositioned and since the dimmer is placed
+ // in screen coordinates and not relative to the parent it needs to be
+ // repositioned accordingly.
+ GetWidget()->SetBounds(shelf_->GetWindow()->GetBoundsInScreen());
+}
+
+DimmerView::DimmerView(WmShelf* shelf, bool disable_animations_for_test)
+ : shelf_(shelf),
+ alpha_(kDimAlpha),
+ is_hovered_(false),
+ force_hovered_(false),
+ disable_animations_for_test_(disable_animations_for_test),
+ background_animator_(this, 0, kDimAlpha) {
+ event_filter_.reset(new DimmerEventFilter(this));
+ // Make sure it is undimmed at the beginning and then fire off the dimming
+ // animation.
+ background_animator_.SetPaintsBackground(false, BACKGROUND_CHANGE_IMMEDIATE);
+ SetHovered(false);
+ shelf_->GetWindow()->AddObserver(this);
+}
+
+DimmerView::~DimmerView() {
+ // Some unit tests will come here with a destroyed window.
+ if (shelf_->GetWindow())
+ shelf_->GetWindow()->RemoveObserver(this);
+}
+
+DimmerView::DimmerEventFilter::DimmerEventFilter(DimmerView* owner)
+ : owner_(owner), mouse_inside_(false), touch_inside_(false) {
+ Shell::GetInstance()->AddPreTargetHandler(this);
+}
+
+DimmerView::DimmerEventFilter::~DimmerEventFilter() {
+ Shell::GetInstance()->RemovePreTargetHandler(this);
+}
+
+void DimmerView::DimmerEventFilter::OnMouseEvent(ui::MouseEvent* event) {
+ if (event->type() != ui::ET_MOUSE_MOVED &&
+ event->type() != ui::ET_MOUSE_DRAGGED)
+ return;
+
+ gfx::Point screen_point(event->location());
+ ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()),
+ &screen_point);
+ bool inside = owner_->GetBoundsInScreen().Contains(screen_point);
+ if (mouse_inside_ || touch_inside_ != inside || touch_inside_)
+ owner_->SetHovered(inside || touch_inside_);
+ mouse_inside_ = inside;
+}
+
+void DimmerView::DimmerEventFilter::OnTouchEvent(ui::TouchEvent* event) {
+ bool touch_inside = false;
+ if (event->type() != ui::ET_TOUCH_RELEASED &&
+ event->type() != ui::ET_TOUCH_CANCELLED) {
+ gfx::Point screen_point(event->location());
+ ::wm::ConvertPointToScreen(static_cast<aura::Window*>(event->target()),
+ &screen_point);
+ touch_inside = owner_->GetBoundsInScreen().Contains(screen_point);
+ }
+
+ if (mouse_inside_ || touch_inside_ != mouse_inside_ || touch_inside)
+ owner_->SetHovered(mouse_inside_ || touch_inside);
+ touch_inside_ = touch_inside;
+}
+
+} // namespace ash
« no previous file with comments | « ash/shelf/dimmer_view.h ('k') | ash/shelf/shelf_widget.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698