OLD | NEW |
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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 } | 88 } |
94 } | 89 } |
95 | 90 |
96 private: | 91 private: |
97 ui::ThrobAnimation animation_; | 92 ui::ThrobAnimation animation_; |
98 | 93 |
99 DISALLOW_COPY_AND_ASSIGN(BarView); | 94 DISALLOW_COPY_AND_ASSIGN(BarView); |
100 }; | 95 }; |
101 | 96 |
102 //////////////////////////////////////////////////////////////////////////////// | 97 //////////////////////////////////////////////////////////////////////////////// |
103 // LauncherButton::IconPulseAnimation | |
104 | |
105 // IconPulseAnimation plays a pulse animation in a loop for given |icon_view|. | |
106 // It iterates through all animations, wait for one duration then starts again. | |
107 class LauncherButton::IconPulseAnimation { | |
108 public: | |
109 explicit IconPulseAnimation(IconView* icon_view) | |
110 : icon_view_(icon_view) { | |
111 SchedulePulseAnimations(); | |
112 } | |
113 virtual ~IconPulseAnimation() { | |
114 // Restore icon_view_ on destruction. | |
115 ScheduleRestoreAnimation(); | |
116 } | |
117 | |
118 private: | |
119 // Animation duration in millisecond. | |
120 static const int kAnimationDurationInMs; | |
121 | |
122 // Number of animations to run and animation parameters. | |
123 static const float kAnimationOpacity[]; | |
124 static const float kAnimationScale[]; | |
125 | |
126 // Schedules pulse animations. | |
127 void SchedulePulseAnimations(); | |
128 | |
129 // Schedule an animation to restore the view to normal state. | |
130 void ScheduleRestoreAnimation(); | |
131 | |
132 IconView* icon_view_; // Owned by views hierarchy of LauncherButton. | |
133 | |
134 DISALLOW_COPY_AND_ASSIGN(IconPulseAnimation); | |
135 }; | |
136 | |
137 // static | |
138 const int LauncherButton::IconPulseAnimation::kAnimationDurationInMs = 600; | |
139 const float LauncherButton::IconPulseAnimation::kAnimationOpacity[] = | |
140 { 0.4f, 0.8f }; | |
141 const float LauncherButton::IconPulseAnimation::kAnimationScale[] = | |
142 { 0.8f, 1.0f }; | |
143 | |
144 void LauncherButton::IconPulseAnimation::SchedulePulseAnimations() { | |
145 // The two animation set should have the same size. | |
146 DCHECK(arraysize(kAnimationOpacity) == arraysize(kAnimationScale)); | |
147 | |
148 scoped_ptr<ui::LayerAnimationSequence> opacity_sequence( | |
149 new ui::LayerAnimationSequence()); | |
150 scoped_ptr<ui::LayerAnimationSequence> transform_sequence( | |
151 new ui::LayerAnimationSequence()); | |
152 | |
153 // The animations loop infinitely. | |
154 opacity_sequence->set_is_cyclic(true); | |
155 transform_sequence->set_is_cyclic(true); | |
156 | |
157 for (size_t i = 0; i < arraysize(kAnimationOpacity); ++i) { | |
158 opacity_sequence->AddElement( | |
159 ui::LayerAnimationElement::CreateOpacityElement( | |
160 kAnimationOpacity[i], | |
161 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs))); | |
162 transform_sequence->AddElement( | |
163 ui::LayerAnimationElement::CreateTransformElement( | |
164 ui::GetScaleTransform(icon_view_->GetLocalBounds().CenterPoint(), | |
165 kAnimationScale[i]), | |
166 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs))); | |
167 } | |
168 | |
169 ui::LayerAnimationElement::AnimatableProperties opacity_properties; | |
170 opacity_properties.insert(ui::LayerAnimationElement::OPACITY); | |
171 opacity_sequence->AddElement( | |
172 ui::LayerAnimationElement::CreatePauseElement( | |
173 opacity_properties, | |
174 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs))); | |
175 | |
176 ui::LayerAnimationElement::AnimatableProperties transform_properties; | |
177 transform_properties.insert(ui::LayerAnimationElement::TRANSFORM); | |
178 transform_sequence->AddElement( | |
179 ui::LayerAnimationElement::CreatePauseElement( | |
180 transform_properties, | |
181 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs))); | |
182 | |
183 std::vector<ui::LayerAnimationSequence*> animations; | |
184 // LayerAnimator::ScheduleTogether takes ownership of the sequences. | |
185 animations.push_back(opacity_sequence.release()); | |
186 animations.push_back(transform_sequence.release()); | |
187 icon_view_->layer()->GetAnimator()->ScheduleTogether(animations); | |
188 } | |
189 | |
190 // Schedule an animation to restore the view to normal state. | |
191 void LauncherButton::IconPulseAnimation::ScheduleRestoreAnimation() { | |
192 ui::Layer* layer = icon_view_->layer(); | |
193 ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); | |
194 layer->SetOpacity(1.0f); | |
195 layer->SetTransform(ui::Transform()); | |
196 } | |
197 | |
198 //////////////////////////////////////////////////////////////////////////////// | |
199 // LauncherButton::IconView | 98 // LauncherButton::IconView |
200 | 99 |
201 LauncherButton::IconView::IconView() : icon_size_(kIconSize) { | 100 LauncherButton::IconView::IconView() : icon_size_(kIconSize) { |
202 } | 101 } |
203 | 102 |
204 LauncherButton::IconView::~IconView() { | 103 LauncherButton::IconView::~IconView() { |
205 } | 104 } |
206 | 105 |
207 bool LauncherButton::IconView::HitTestRect(const gfx::Rect& rect) const { | 106 bool LauncherButton::IconView::HitTestRect(const gfx::Rect& rect) const { |
208 // Return false so that LauncherButton gets all the mouse events. | 107 // Return false so that LauncherButton gets all the mouse events. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 scoped_setter.SetTransitionDuration( | 185 scoped_setter.SetTransitionDuration( |
287 base::TimeDelta::FromMilliseconds(kHopUpMS)); | 186 base::TimeDelta::FromMilliseconds(kHopUpMS)); |
288 state_ |= state; | 187 state_ |= state; |
289 UpdateState(); | 188 UpdateState(); |
290 } else { | 189 } else { |
291 state_ |= state; | 190 state_ |= state; |
292 UpdateState(); | 191 UpdateState(); |
293 } | 192 } |
294 if (state & STATE_ATTENTION) | 193 if (state & STATE_ATTENTION) |
295 bar_->ShowAttention(true); | 194 bar_->ShowAttention(true); |
296 if (state & STATE_PENDING) | |
297 icon_pulse_animation_.reset(new IconPulseAnimation(icon_view_)); | |
298 } | 195 } |
299 } | 196 } |
300 | 197 |
301 void LauncherButton::ClearState(State state) { | 198 void LauncherButton::ClearState(State state) { |
302 if (state_ & state) { | 199 if (state_ & state) { |
303 if (!ShouldHop(state) || ShouldHop(state_)) { | 200 if (!ShouldHop(state) || ShouldHop(state_)) { |
304 ui::ScopedLayerAnimationSettings scoped_setter( | 201 ui::ScopedLayerAnimationSettings scoped_setter( |
305 icon_view_->layer()->GetAnimator()); | 202 icon_view_->layer()->GetAnimator()); |
306 scoped_setter.SetTweenType(ui::Tween::LINEAR); | 203 scoped_setter.SetTweenType(ui::Tween::LINEAR); |
307 scoped_setter.SetTransitionDuration( | 204 scoped_setter.SetTransitionDuration( |
308 base::TimeDelta::FromMilliseconds(kHopDownMS)); | 205 base::TimeDelta::FromMilliseconds(kHopDownMS)); |
309 state_ &= ~state; | 206 state_ &= ~state; |
310 UpdateState(); | 207 UpdateState(); |
311 } else { | 208 } else { |
312 state_ &= ~state; | 209 state_ &= ~state; |
313 UpdateState(); | 210 UpdateState(); |
314 } | 211 } |
315 if (state & STATE_ATTENTION) | 212 if (state & STATE_ATTENTION) |
316 bar_->ShowAttention(false); | 213 bar_->ShowAttention(false); |
317 if (state & STATE_PENDING) | |
318 icon_pulse_animation_.reset(); | |
319 } | 214 } |
320 } | 215 } |
321 | 216 |
322 gfx::Rect LauncherButton::GetIconBounds() const { | 217 gfx::Rect LauncherButton::GetIconBounds() const { |
323 return icon_view_->bounds(); | 218 return icon_view_->bounds(); |
324 } | 219 } |
325 | 220 |
326 bool LauncherButton::OnMousePressed(const ui::MouseEvent& event) { | 221 bool LauncherButton::OnMousePressed(const ui::MouseEvent& event) { |
327 CustomButton::OnMousePressed(event); | 222 CustomButton::OnMousePressed(event); |
328 host_->PointerPressedOnButton(this, LauncherButtonHost::MOUSE, event); | 223 host_->PointerPressedOnButton(this, LauncherButtonHost::MOUSE, event); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 | 349 |
455 LauncherButton::IconView* LauncherButton::CreateIconView() { | 350 LauncherButton::IconView* LauncherButton::CreateIconView() { |
456 return new IconView; | 351 return new IconView; |
457 } | 352 } |
458 | 353 |
459 bool LauncherButton::IsShelfHorizontal() const { | 354 bool LauncherButton::IsShelfHorizontal() const { |
460 return host_->GetShelfAlignment() == SHELF_ALIGNMENT_BOTTOM; | 355 return host_->GetShelfAlignment() == SHELF_ALIGNMENT_BOTTOM; |
461 } | 356 } |
462 | 357 |
463 void LauncherButton::UpdateState() { | 358 void LauncherButton::UpdateState() { |
464 if (state_ == STATE_NORMAL || state_ & STATE_PENDING) { | 359 if (state_ == STATE_NORMAL) { |
465 bar_->SetVisible(false); | 360 bar_->SetVisible(false); |
466 } else { | 361 } else { |
467 int bar_id; | 362 int bar_id; |
468 if (IsShelfHorizontal()) { | 363 if (IsShelfHorizontal()) { |
469 if (state_ & (STATE_HOVERED | STATE_FOCUSED | STATE_ATTENTION)) | 364 if (state_ & (STATE_HOVERED | STATE_FOCUSED | STATE_ATTENTION)) |
470 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_HOVER; | 365 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_HOVER; |
471 else if (state_ & STATE_ACTIVE) | 366 else if (state_ & STATE_ACTIVE) |
472 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_ACTIVE; | 367 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_ACTIVE; |
473 else | 368 else |
474 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_RUNNING; | 369 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_RUNNING; |
(...skipping 29 matching lines...) Expand all Loading... |
504 bar_->SetVerticalAlignment(views::ImageView::CENTER); | 399 bar_->SetVerticalAlignment(views::ImageView::CENTER); |
505 break; | 400 break; |
506 } | 401 } |
507 | 402 |
508 Layout(); | 403 Layout(); |
509 SchedulePaint(); | 404 SchedulePaint(); |
510 } | 405 } |
511 | 406 |
512 } // namespace internal | 407 } // namespace internal |
513 } // namespace ash | 408 } // namespace ash |
OLD | NEW |