15267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)// found in the LICENSE file.
45267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
55267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ash/display/shared_display_edge_indicator.h"
65267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
75267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ash/shell.h"
85267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ash/shell_window_ids.h"
95267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ash/wm/coordinate_conversion.h"
105267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "third_party/skia/include/core/SkColor.h"
115267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ui/aura/client/screen_position_client.h"
125267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ui/aura/window_event_dispatcher.h"
135267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ui/gfx/animation/throb_animation.h"
145267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ui/gfx/canvas.h"
155267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ui/gfx/display.h"
165267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ui/gfx/screen.h"
175267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ui/views/view.h"
185267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)#include "ui/views/widget/widget.h"
195267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
205267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)namespace ash {
215267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)namespace {
225267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
235267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)const int kIndicatorAnimationDurationMs = 1000;
245267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
255267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)class IndicatorView : public views::View {
265267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) public:
275267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  IndicatorView() {
285267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  }
295267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  virtual ~IndicatorView() {
305267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  }
315267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
325267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  void SetColor(SkColor color) {
335267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    color_ = color;
345267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    SchedulePaint();
355267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  }
365267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
375267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  // views::Views overrides:
385267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
395267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    canvas->FillRect(gfx::Rect(bounds().size()), color_);
405267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  }
415267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
425267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles) private:
435267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  SkColor color_;
445267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(IndicatorView);
455267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)};
465267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
475267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)views::Widget* CreateWidget(const gfx::Rect& bounds,
485267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)                            views::View* contents_view) {
495267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  views::Widget* widget = new views::Widget;
505267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
5106f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)  params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
5206f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)  params.keep_on_top = true;
5306f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)  // We set the context to the primary root window; this is OK because the ash
545267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  // stacking controller will still place us in the correct RootWindow.
555267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  params.context = Shell::GetPrimaryRootWindow();
5606f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)  widget->set_focus_on_creation(false);
575267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  widget->Init(params);
585267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  widget->SetVisibilityChangedAnimationsEnabled(false);
5906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)  widget->GetNativeWindow()->SetName("SharedEdgeIndicator");
605267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  widget->SetContentsView(contents_view);
615267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  gfx::Display display = Shell::GetScreen()->GetDisplayMatching(bounds);
625267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  aura::Window* window = widget->GetNativeWindow();
6306f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)  aura::client::ScreenPositionClient* screen_position_client =
645267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)      aura::client::GetScreenPositionClient(window->GetRootWindow());
655267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  screen_position_client->SetBounds(window, bounds, display);
6606f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)  widget->Show();
675267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  return widget;
685267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}
6906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
705267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}  // namespace
715267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
7206f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)SharedDisplayEdgeIndicator::SharedDisplayEdgeIndicator()
735267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)    : src_indicator_(NULL),
7406f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)      dst_indicator_(NULL) {
755267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}
765267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
775267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)SharedDisplayEdgeIndicator::~SharedDisplayEdgeIndicator() {
785267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  Hide();
795267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)}
805267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)
815267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)void SharedDisplayEdgeIndicator::Show(const gfx::Rect& src_bounds,
825267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)                                      const gfx::Rect& dst_bounds) {
835267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  DCHECK(!src_indicator_);
845267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  DCHECK(!dst_indicator_);
855267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  src_indicator_ = new IndicatorView;
865267f701546148b83dfbe1d151cb184385bb5c22Torne (Richard Coles)  dst_indicator_ = new IndicatorView;
87  CreateWidget(src_bounds, src_indicator_);
88  CreateWidget(dst_bounds, dst_indicator_);
89  animation_.reset(new gfx::ThrobAnimation(this));
90  animation_->SetThrobDuration(kIndicatorAnimationDurationMs);
91  animation_->StartThrobbing(-1 /* infinite */);
92}
93
94void SharedDisplayEdgeIndicator::Hide() {
95  if (src_indicator_)
96    src_indicator_->GetWidget()->Close();
97  src_indicator_ = NULL;
98  if (dst_indicator_)
99    dst_indicator_->GetWidget()->Close();
100  dst_indicator_ = NULL;
101}
102
103void SharedDisplayEdgeIndicator::AnimationProgressed(
104    const gfx::Animation* animation) {
105  int value = animation->CurrentValueBetween(0, 255);
106  SkColor color = SkColorSetARGB(0xFF, value, value, value);
107  if (src_indicator_)
108    static_cast<IndicatorView*>(src_indicator_)->SetColor(color);
109  if (dst_indicator_)
110    static_cast<IndicatorView*>(dst_indicator_)->SetColor(color);
111
112}
113
114}  // namespace ash
115