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.
5 #include "ui/app_list/views/pulsing_block_view.h"
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"
20 const SkColor kBlockColor = SkColorSetRGB(225, 225, 225);
21 const int kBlockSize = 64;
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 };
27 void SchedulePulsingAnimation(ui::Layer* layer) {
29 DCHECK_EQ(arraysize(kAnimationOpacity), arraysize(kAnimationScale));
31 scoped_ptr<ui::LayerAnimationSequence> opacity_sequence(
32 new ui::LayerAnimationSequence());
33 scoped_ptr<ui::LayerAnimationSequence> transform_sequence(
34 new ui::LayerAnimationSequence());
36 // The animations loop infinitely.
37 opacity_sequence->set_is_cyclic(true);
38 transform_sequence->set_is_cyclic(true);
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(
45 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
46 transform_sequence->AddElement(
47 ui::LayerAnimationElement::CreateTransformElement(
48 gfx::GetScaleTransform(local_bounds.CenterPoint(),
50 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
53 ui::LayerAnimationElement::AnimatableProperties opacity_properties;
54 opacity_properties.insert(ui::LayerAnimationElement::OPACITY);
55 opacity_sequence->AddElement(
56 ui::LayerAnimationElement::CreatePauseElement(
58 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
60 ui::LayerAnimationElement::AnimatableProperties transform_properties;
61 transform_properties.insert(ui::LayerAnimationElement::TRANSFORM);
62 transform_sequence->AddElement(
63 ui::LayerAnimationElement::CreatePauseElement(
65 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
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);
77 PulsingBlockView::PulsingBlockView(const gfx::Size& size, bool start_delay) {
79 SetPaintToLayer(true);
80 SetFillsBoundsOpaquely(false);
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(
86 base::TimeDelta::FromMilliseconds(delay),
87 this, &PulsingBlockView::OnStartDelayTimer);
89 NOTREACHED() << "Pulsing animation is not supported on Windows";
93 PulsingBlockView::~PulsingBlockView() {
96 void PulsingBlockView::OnStartDelayTimer() {
97 SchedulePulsingAnimation(layer());
100 void PulsingBlockView::OnPaint(gfx::Canvas* canvas) {
101 gfx::Rect rect(GetContentsBounds());
102 rect.ClampToCenteredSize(gfx::Size(kBlockSize, kBlockSize));
103 canvas->FillRect(rect, kBlockColor);
106 } // namespace app_list