Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: ash/launcher/launcher_button.cc

Issue 10829268: chromeos: Sync animation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: wrap up + rebase Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/launcher/launcher_button.h" 5 #include "ash/launcher/launcher_button.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector>
9 8
10 #include "ash/launcher/launcher_button_host.h" 9 #include "ash/launcher/launcher_button_host.h"
11 #include "grit/ui_resources.h" 10 #include "grit/ui_resources.h"
12 #include "skia/ext/image_operations.h" 11 #include "skia/ext/image_operations.h"
13 #include "ui/base/accessibility/accessible_view_state.h" 12 #include "ui/base/accessibility/accessible_view_state.h"
14 #include "ui/base/animation/animation_delegate.h" 13 #include "ui/base/animation/animation_delegate.h"
15 #include "ui/base/animation/throb_animation.h" 14 #include "ui/base/animation/throb_animation.h"
16 #include "ui/base/events.h" 15 #include "ui/base/events.h"
17 #include "ui/base/resource/resource_bundle.h" 16 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/compositor/layer.h" 17 #include "ui/compositor/layer.h"
19 #include "ui/compositor/layer_animation_element.h"
20 #include "ui/compositor/layer_animation_observer.h"
21 #include "ui/compositor/layer_animation_sequence.h"
22 #include "ui/compositor/scoped_layer_animation_settings.h" 18 #include "ui/compositor/scoped_layer_animation_settings.h"
23 #include "ui/gfx/canvas.h" 19 #include "ui/gfx/canvas.h"
24 #include "ui/gfx/image/image.h" 20 #include "ui/gfx/image/image.h"
25 #include "ui/gfx/image/image_skia_operations.h" 21 #include "ui/gfx/image/image_skia_operations.h"
26 #include "ui/gfx/transform_util.h"
27 #include "ui/views/controls/image_view.h" 22 #include "ui/views/controls/image_view.h"
28 23
29 namespace { 24 namespace {
30 25
31 // Size of the bar. This is along the opposite axis of the shelf. For example, 26 // Size of the bar. This is along the opposite axis of the shelf. For example,
32 // if the shelf is aligned horizontally then this is the height of the bar. 27 // if the shelf is aligned horizontally then this is the height of the bar.
33 const int kBarSize = 3; 28 const int kBarSize = 3;
34 const int kBarSpacing = 5; 29 const int kBarSpacing = 5;
35 const int kIconSize = 32; 30 const int kIconSize = 32;
36 const int kHopSpacing = 2; 31 const int kHopSpacing = 2;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 } 90 }
96 } 91 }
97 92
98 private: 93 private:
99 ui::ThrobAnimation animation_; 94 ui::ThrobAnimation animation_;
100 95
101 DISALLOW_COPY_AND_ASSIGN(BarView); 96 DISALLOW_COPY_AND_ASSIGN(BarView);
102 }; 97 };
103 98
104 //////////////////////////////////////////////////////////////////////////////// 99 ////////////////////////////////////////////////////////////////////////////////
105 // LauncherButton::IconPulseAnimation
106
107 // IconPulseAnimation plays a pulse animation in a loop for given |icon_view|.
108 // It iterates through all animations, wait for one duration then starts again.
109 class LauncherButton::IconPulseAnimation {
110 public:
111 explicit IconPulseAnimation(IconView* icon_view)
112 : icon_view_(icon_view) {
113 SchedulePulseAnimations();
114 }
115 virtual ~IconPulseAnimation() {
116 // Restore icon_view_ on destruction.
117 ScheduleRestoreAnimation();
118 }
119
120 private:
121 // Animation duration in millisecond.
122 static const int kAnimationDurationInMs;
123
124 // Number of animations to run and animation parameters.
125 static const float kAnimationOpacity[];
126 static const float kAnimationScale[];
127
128 // Schedules pulse animations.
129 void SchedulePulseAnimations();
130
131 // Schedule an animation to restore the view to normal state.
132 void ScheduleRestoreAnimation();
133
134 IconView* icon_view_; // Owned by views hierarchy of LauncherButton.
135
136 DISALLOW_COPY_AND_ASSIGN(IconPulseAnimation);
137 };
138
139 // static
140 const int LauncherButton::IconPulseAnimation::kAnimationDurationInMs = 600;
141 const float LauncherButton::IconPulseAnimation::kAnimationOpacity[] =
142 { 0.4f, 0.8f };
143 const float LauncherButton::IconPulseAnimation::kAnimationScale[] =
144 { 0.8f, 1.0f };
145
146 void LauncherButton::IconPulseAnimation::SchedulePulseAnimations() {
147 // The two animation set should have the same size.
148 DCHECK(arraysize(kAnimationOpacity) == arraysize(kAnimationScale));
149
150 scoped_ptr<ui::LayerAnimationSequence> opacity_sequence(
151 new ui::LayerAnimationSequence());
152 scoped_ptr<ui::LayerAnimationSequence> transform_sequence(
153 new ui::LayerAnimationSequence());
154
155 // The animations loop infinitely.
156 opacity_sequence->set_is_cyclic(true);
157 transform_sequence->set_is_cyclic(true);
158
159 for (size_t i = 0; i < arraysize(kAnimationOpacity); ++i) {
160 opacity_sequence->AddElement(
161 ui::LayerAnimationElement::CreateOpacityElement(
162 kAnimationOpacity[i],
163 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
164 transform_sequence->AddElement(
165 ui::LayerAnimationElement::CreateTransformElement(
166 ui::GetScaleTransform(icon_view_->GetLocalBounds().CenterPoint(),
167 kAnimationScale[i]),
168 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
169 }
170
171 ui::LayerAnimationElement::AnimatableProperties opacity_properties;
172 opacity_properties.insert(ui::LayerAnimationElement::OPACITY);
173 opacity_sequence->AddElement(
174 ui::LayerAnimationElement::CreatePauseElement(
175 opacity_properties,
176 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
177
178 ui::LayerAnimationElement::AnimatableProperties transform_properties;
179 transform_properties.insert(ui::LayerAnimationElement::TRANSFORM);
180 transform_sequence->AddElement(
181 ui::LayerAnimationElement::CreatePauseElement(
182 transform_properties,
183 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
184
185 std::vector<ui::LayerAnimationSequence*> animations;
186 // LayerAnimator::ScheduleTogether takes ownership of the sequences.
187 animations.push_back(opacity_sequence.release());
188 animations.push_back(transform_sequence.release());
189 icon_view_->layer()->GetAnimator()->ScheduleTogether(animations);
190 }
191
192 // Schedule an animation to restore the view to normal state.
193 void LauncherButton::IconPulseAnimation::ScheduleRestoreAnimation() {
194 ui::Layer* layer = icon_view_->layer();
195 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
196 layer->SetOpacity(1.0f);
197 layer->SetTransform(ui::Transform());
198 }
199
200 ////////////////////////////////////////////////////////////////////////////////
201 // LauncherButton::IconView 100 // LauncherButton::IconView
202 101
203 LauncherButton::IconView::IconView() : icon_size_(kIconSize) { 102 LauncherButton::IconView::IconView() : icon_size_(kIconSize) {
204 } 103 }
205 104
206 LauncherButton::IconView::~IconView() { 105 LauncherButton::IconView::~IconView() {
207 } 106 }
208 107
209 bool LauncherButton::IconView::HitTestRect(const gfx::Rect& rect) const { 108 bool LauncherButton::IconView::HitTestRect(const gfx::Rect& rect) const {
210 // Return false so that LauncherButton gets all the mouse events. 109 // Return false so that LauncherButton gets all the mouse events.
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 scoped_setter.SetTransitionDuration( 187 scoped_setter.SetTransitionDuration(
289 base::TimeDelta::FromMilliseconds(kHopUpMS)); 188 base::TimeDelta::FromMilliseconds(kHopUpMS));
290 state_ |= state; 189 state_ |= state;
291 UpdateState(); 190 UpdateState();
292 } else { 191 } else {
293 state_ |= state; 192 state_ |= state;
294 UpdateState(); 193 UpdateState();
295 } 194 }
296 if (state & STATE_ATTENTION) 195 if (state & STATE_ATTENTION)
297 bar_->ShowAttention(true); 196 bar_->ShowAttention(true);
298 if (state & STATE_PENDING)
299 icon_pulse_animation_.reset(new IconPulseAnimation(icon_view_));
300 } 197 }
301 } 198 }
302 199
303 void LauncherButton::ClearState(State state) { 200 void LauncherButton::ClearState(State state) {
304 if (state_ & state) { 201 if (state_ & state) {
305 if (!ShouldHop(state) || ShouldHop(state_)) { 202 if (!ShouldHop(state) || ShouldHop(state_)) {
306 ui::ScopedLayerAnimationSettings scoped_setter( 203 ui::ScopedLayerAnimationSettings scoped_setter(
307 icon_view_->layer()->GetAnimator()); 204 icon_view_->layer()->GetAnimator());
308 scoped_setter.SetTweenType(ui::Tween::LINEAR); 205 scoped_setter.SetTweenType(ui::Tween::LINEAR);
309 scoped_setter.SetTransitionDuration( 206 scoped_setter.SetTransitionDuration(
310 base::TimeDelta::FromMilliseconds(kHopDownMS)); 207 base::TimeDelta::FromMilliseconds(kHopDownMS));
311 state_ &= ~state; 208 state_ &= ~state;
312 UpdateState(); 209 UpdateState();
313 } else { 210 } else {
314 state_ &= ~state; 211 state_ &= ~state;
315 UpdateState(); 212 UpdateState();
316 } 213 }
317 if (state & STATE_ATTENTION) 214 if (state & STATE_ATTENTION)
318 bar_->ShowAttention(false); 215 bar_->ShowAttention(false);
319 if (state & STATE_PENDING)
320 icon_pulse_animation_.reset();
321 } 216 }
322 } 217 }
323 218
324 gfx::Rect LauncherButton::GetIconBounds() const { 219 gfx::Rect LauncherButton::GetIconBounds() const {
325 return icon_view_->bounds(); 220 return icon_view_->bounds();
326 } 221 }
327 222
328 bool LauncherButton::OnMousePressed(const ui::MouseEvent& event) { 223 bool LauncherButton::OnMousePressed(const ui::MouseEvent& event) {
329 CustomButton::OnMousePressed(event); 224 CustomButton::OnMousePressed(event);
330 host_->PointerPressedOnButton(this, LauncherButtonHost::MOUSE, event); 225 host_->PointerPressedOnButton(this, LauncherButtonHost::MOUSE, event);
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 351
457 LauncherButton::IconView* LauncherButton::CreateIconView() { 352 LauncherButton::IconView* LauncherButton::CreateIconView() {
458 return new IconView; 353 return new IconView;
459 } 354 }
460 355
461 bool LauncherButton::IsShelfHorizontal() const { 356 bool LauncherButton::IsShelfHorizontal() const {
462 return host_->GetShelfAlignment() == SHELF_ALIGNMENT_BOTTOM; 357 return host_->GetShelfAlignment() == SHELF_ALIGNMENT_BOTTOM;
463 } 358 }
464 359
465 void LauncherButton::UpdateState() { 360 void LauncherButton::UpdateState() {
466 if (state_ == STATE_NORMAL || state_ & STATE_PENDING) { 361 if (state_ == STATE_NORMAL) {
467 bar_->SetVisible(false); 362 bar_->SetVisible(false);
468 } else { 363 } else {
469 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 364 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
470 int bar_id; 365 int bar_id;
471 bar_->SetVisible(true); 366 bar_->SetVisible(true);
472 367
473 if (state_ & STATE_ACTIVE || state_ & STATE_ATTENTION) { 368 if (state_ & STATE_ACTIVE || state_ & STATE_ATTENTION) {
474 bar_id = IsShelfHorizontal() ? IDR_AURA_LAUNCHER_UNDERLINE_ACTIVE : 369 bar_id = IsShelfHorizontal() ? IDR_AURA_LAUNCHER_UNDERLINE_ACTIVE :
475 IDR_AURA_LAUNCHER_UNDERLINE_VERTICAL_ACTIVE; 370 IDR_AURA_LAUNCHER_UNDERLINE_VERTICAL_ACTIVE;
476 } else if (state_ & STATE_HOVERED || state_ & STATE_FOCUSED) { 371 } else if (state_ & STATE_HOVERED || state_ & STATE_FOCUSED) {
(...skipping 25 matching lines...) Expand all
502 bar_->SetVerticalAlignment(views::ImageView::CENTER); 397 bar_->SetVerticalAlignment(views::ImageView::CENTER);
503 break; 398 break;
504 } 399 }
505 400
506 Layout(); 401 Layout();
507 SchedulePaint(); 402 SchedulePaint();
508 } 403 }
509 404
510 } // namespace internal 405 } // namespace internal
511 } // namespace ash 406 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698