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