shared_display_edge_indicator.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
1// Copyright (c) 2012 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/display/shared_display_edge_indicator.h"
6
7#include "ash/shell.h"
8#include "ash/shell_window_ids.h"
9#include "ash/wm/coordinate_conversion.h"
10#include "third_party/skia/include/core/SkColor.h"
11#include "ui/aura/client/screen_position_client.h"
12#include "ui/aura/root_window.h"
13#include "ui/base/animation/throb_animation.h"
14#include "ui/gfx/canvas.h"
15#include "ui/gfx/display.h"
16#include "ui/gfx/screen.h"
17#include "ui/views/view.h"
18#include "ui/views/widget/widget.h"
19
20namespace ash {
21namespace internal {
22namespace {
23
24const int kIndicatorAnimationDurationMs = 1000;
25
26class IndicatorView : public views::View {
27 public:
28  IndicatorView() {
29  }
30  virtual ~IndicatorView() {
31  }
32
33  void SetColor(SkColor color) {
34    color_ = color;
35    SchedulePaint();
36  }
37
38  // views::Views overrides:
39  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
40    canvas->FillRect(gfx::Rect(bounds().size()), color_);
41  }
42
43 private:
44  SkColor color_;
45  DISALLOW_COPY_AND_ASSIGN(IndicatorView);
46};
47
48views::Widget* CreateWidget(const gfx::Rect& bounds,
49                            views::View* contents_view) {
50  views::Widget* widget = new views::Widget;
51  views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
52  params.transparent = true;
53  params.can_activate = false;
54  params.keep_on_top = true;
55  // We set the context to the primary root window; this is OK because the ash
56  // stacking controller will still place us in the correct RootWindow.
57  params.context = Shell::GetPrimaryRootWindow();
58  widget->set_focus_on_creation(false);
59  widget->Init(params);
60  widget->SetVisibilityChangedAnimationsEnabled(false);
61  widget->GetNativeWindow()->SetName("SharedEdgeIndicator");
62  widget->SetContentsView(contents_view);
63  gfx::Display display = Shell::GetScreen()->GetDisplayMatching(bounds);
64  aura::Window* window = widget->GetNativeWindow();
65  aura::client::ScreenPositionClient* screen_position_client =
66      aura::client::GetScreenPositionClient(window->GetRootWindow());
67  screen_position_client->SetBounds(window, bounds, display);
68  widget->Show();
69  return widget;
70}
71
72}  // namespace
73
74SharedDisplayEdgeIndicator::SharedDisplayEdgeIndicator()
75    : src_indicator_(NULL),
76      dst_indicator_(NULL) {
77}
78
79SharedDisplayEdgeIndicator::~SharedDisplayEdgeIndicator() {
80  Hide();
81}
82
83void SharedDisplayEdgeIndicator::Show(const gfx::Rect& src_bounds,
84                                      const gfx::Rect& dst_bounds) {
85  DCHECK(!src_indicator_);
86  DCHECK(!dst_indicator_);
87  src_indicator_ = new IndicatorView;
88  dst_indicator_ = new IndicatorView;
89  CreateWidget(src_bounds, src_indicator_);
90  CreateWidget(dst_bounds, dst_indicator_);
91  animation_.reset(new ui::ThrobAnimation(this));
92  animation_->SetThrobDuration(kIndicatorAnimationDurationMs);
93  animation_->StartThrobbing(-1 /* infinite */);
94}
95
96void SharedDisplayEdgeIndicator::Hide() {
97  if (src_indicator_)
98    src_indicator_->GetWidget()->Close();
99  src_indicator_ = NULL;
100  if (dst_indicator_)
101    dst_indicator_->GetWidget()->Close();
102  dst_indicator_ = NULL;
103}
104
105void SharedDisplayEdgeIndicator::AnimationProgressed(
106    const ui::Animation* animation) {
107  int value = animation->CurrentValueBetween(0, 255);
108  SkColor color = SkColorSetARGB(0xFF, value, value, value);
109  if (src_indicator_)
110    static_cast<IndicatorView*>(src_indicator_)->SetColor(color);
111  if (dst_indicator_)
112    static_cast<IndicatorView*>(dst_indicator_)->SetColor(color);
113
114}
115
116}  // namespace internal
117}  // namespace ash
118