- add sources.
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / pulsing_block_view.cc
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 "ui/app_list/views/pulsing_block_view.h"
6
7 #include <vector>
8
9 #include "base/rand_util.h"
10 #include "third_party/skia/include/core/SkColor.h"
11 #include "ui/compositor/layer.h"
12 #include "ui/compositor/layer_animation_element.h"
13 #include "ui/compositor/layer_animation_sequence.h"
14 #include "ui/compositor/layer_animator.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/transform_util.h"
17
18 namespace {
19
20 const SkColor kBlockColor = SkColorSetRGB(225, 225, 225);
21 const int kBlockSize = 64;
22
23 const int kAnimationDurationInMs = 600;
24 const float kAnimationOpacity[] = { 0.4f, 0.8f, 0.4f };
25 const float kAnimationScale[] = { 0.8f, 1.0f, 0.8f };
26
27 void SchedulePulsingAnimation(ui::Layer* layer) {
28   DCHECK(layer);
29   DCHECK_EQ(arraysize(kAnimationOpacity), arraysize(kAnimationScale));
30
31   scoped_ptr<ui::LayerAnimationSequence> opacity_sequence(
32       new ui::LayerAnimationSequence());
33   scoped_ptr<ui::LayerAnimationSequence> transform_sequence(
34       new ui::LayerAnimationSequence());
35
36   // The animations loop infinitely.
37   opacity_sequence->set_is_cyclic(true);
38   transform_sequence->set_is_cyclic(true);
39
40   const gfx::Rect local_bounds(layer->bounds().size());
41   for (size_t i = 0; i < arraysize(kAnimationOpacity); ++i) {
42     opacity_sequence->AddElement(
43         ui::LayerAnimationElement::CreateOpacityElement(
44             kAnimationOpacity[i],
45             base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
46     transform_sequence->AddElement(
47         ui::LayerAnimationElement::CreateTransformElement(
48             gfx::GetScaleTransform(local_bounds.CenterPoint(),
49                                    kAnimationScale[i]),
50             base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
51   }
52
53   ui::LayerAnimationElement::AnimatableProperties opacity_properties;
54   opacity_properties.insert(ui::LayerAnimationElement::OPACITY);
55   opacity_sequence->AddElement(
56       ui::LayerAnimationElement::CreatePauseElement(
57           opacity_properties,
58            base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
59
60   ui::LayerAnimationElement::AnimatableProperties transform_properties;
61   transform_properties.insert(ui::LayerAnimationElement::TRANSFORM);
62   transform_sequence->AddElement(
63       ui::LayerAnimationElement::CreatePauseElement(
64           transform_properties,
65           base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
66
67   std::vector<ui::LayerAnimationSequence*> animations;
68   animations.push_back(opacity_sequence.release());
69   animations.push_back(transform_sequence.release());
70   layer->GetAnimator()->ScheduleTogether(animations);
71 }
72
73 }  // namespace
74
75 namespace app_list {
76
77 PulsingBlockView::PulsingBlockView(const gfx::Size& size, bool start_delay) {
78 #if defined(USE_AURA)
79   SetPaintToLayer(true);
80   SetFillsBoundsOpaquely(false);
81
82   const int max_delay = kAnimationDurationInMs * arraysize(kAnimationOpacity);
83   const int delay = start_delay ? base::RandInt(0, max_delay) : 0;
84   start_delay_timer_.Start(
85       FROM_HERE,
86       base::TimeDelta::FromMilliseconds(delay),
87       this, &PulsingBlockView::OnStartDelayTimer);
88 #else
89   NOTREACHED() << "Pulsing animation is not supported on Windows";
90 #endif
91 }
92
93 PulsingBlockView::~PulsingBlockView() {
94 }
95
96 void PulsingBlockView::OnStartDelayTimer() {
97   SchedulePulsingAnimation(layer());
98 }
99
100 void PulsingBlockView::OnPaint(gfx::Canvas* canvas) {
101   gfx::Rect rect(GetContentsBounds());
102   rect.ClampToCenteredSize(gfx::Size(kBlockSize, kBlockSize));
103   canvas->FillRect(rect, kBlockColor);
104 }
105
106 }  // namespace app_list