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> |
8 | 9 |
9 #include "ash/launcher/launcher_button_host.h" | 10 #include "ash/launcher/launcher_button_host.h" |
10 #include "grit/ui_resources.h" | 11 #include "grit/ui_resources.h" |
11 #include "skia/ext/image_operations.h" | 12 #include "skia/ext/image_operations.h" |
12 #include "ui/base/accessibility/accessible_view_state.h" | 13 #include "ui/base/accessibility/accessible_view_state.h" |
13 #include "ui/base/animation/animation_delegate.h" | 14 #include "ui/base/animation/animation_delegate.h" |
14 #include "ui/base/animation/throb_animation.h" | 15 #include "ui/base/animation/throb_animation.h" |
15 #include "ui/base/events.h" | 16 #include "ui/base/events.h" |
16 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
17 #include "ui/compositor/layer.h" | 18 #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" |
18 #include "ui/compositor/scoped_layer_animation_settings.h" | 22 #include "ui/compositor/scoped_layer_animation_settings.h" |
19 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
20 #include "ui/gfx/image/image.h" | 24 #include "ui/gfx/image/image.h" |
21 #include "ui/gfx/image/image_skia_operations.h" | 25 #include "ui/gfx/image/image_skia_operations.h" |
| 26 #include "ui/gfx/transform_util.h" |
22 #include "ui/views/controls/image_view.h" | 27 #include "ui/views/controls/image_view.h" |
23 | 28 |
24 namespace { | 29 namespace { |
25 | 30 |
26 // Size of the bar. This is along the opposite axis of the shelf. For example, | 31 // Size of the bar. This is along the opposite axis of the shelf. For example, |
27 // if the shelf is aligned horizontally then this is the height of the bar. | 32 // if the shelf is aligned horizontally then this is the height of the bar. |
28 const int kBarSize = 3; | 33 const int kBarSize = 3; |
29 const int kBarSpacing = 5; | 34 const int kBarSpacing = 5; |
30 const int kIconSize = 32; | 35 const int kIconSize = 32; |
31 const int kHopSpacing = 2; | 36 const int kHopSpacing = 2; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 } | 93 } |
89 } | 94 } |
90 | 95 |
91 private: | 96 private: |
92 ui::ThrobAnimation animation_; | 97 ui::ThrobAnimation animation_; |
93 | 98 |
94 DISALLOW_COPY_AND_ASSIGN(BarView); | 99 DISALLOW_COPY_AND_ASSIGN(BarView); |
95 }; | 100 }; |
96 | 101 |
97 //////////////////////////////////////////////////////////////////////////////// | 102 //////////////////////////////////////////////////////////////////////////////// |
| 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 //////////////////////////////////////////////////////////////////////////////// |
98 // LauncherButton::IconView | 199 // LauncherButton::IconView |
99 | 200 |
100 LauncherButton::IconView::IconView() : icon_size_(kIconSize) { | 201 LauncherButton::IconView::IconView() : icon_size_(kIconSize) { |
101 } | 202 } |
102 | 203 |
103 LauncherButton::IconView::~IconView() { | 204 LauncherButton::IconView::~IconView() { |
104 } | 205 } |
105 | 206 |
106 bool LauncherButton::IconView::HitTestRect(const gfx::Rect& rect) const { | 207 bool LauncherButton::IconView::HitTestRect(const gfx::Rect& rect) const { |
107 // Return false so that LauncherButton gets all the mouse events. | 208 // Return false so that LauncherButton gets all the mouse events. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 scoped_setter.SetTransitionDuration( | 286 scoped_setter.SetTransitionDuration( |
186 base::TimeDelta::FromMilliseconds(kHopUpMS)); | 287 base::TimeDelta::FromMilliseconds(kHopUpMS)); |
187 state_ |= state; | 288 state_ |= state; |
188 UpdateState(); | 289 UpdateState(); |
189 } else { | 290 } else { |
190 state_ |= state; | 291 state_ |= state; |
191 UpdateState(); | 292 UpdateState(); |
192 } | 293 } |
193 if (state & STATE_ATTENTION) | 294 if (state & STATE_ATTENTION) |
194 bar_->ShowAttention(true); | 295 bar_->ShowAttention(true); |
| 296 if (state & STATE_PENDING) |
| 297 icon_pulse_animation_.reset(new IconPulseAnimation(icon_view_)); |
195 } | 298 } |
196 } | 299 } |
197 | 300 |
198 void LauncherButton::ClearState(State state) { | 301 void LauncherButton::ClearState(State state) { |
199 if (state_ & state) { | 302 if (state_ & state) { |
200 if (!ShouldHop(state) || ShouldHop(state_)) { | 303 if (!ShouldHop(state) || ShouldHop(state_)) { |
201 ui::ScopedLayerAnimationSettings scoped_setter( | 304 ui::ScopedLayerAnimationSettings scoped_setter( |
202 icon_view_->layer()->GetAnimator()); | 305 icon_view_->layer()->GetAnimator()); |
203 scoped_setter.SetTweenType(ui::Tween::LINEAR); | 306 scoped_setter.SetTweenType(ui::Tween::LINEAR); |
204 scoped_setter.SetTransitionDuration( | 307 scoped_setter.SetTransitionDuration( |
205 base::TimeDelta::FromMilliseconds(kHopDownMS)); | 308 base::TimeDelta::FromMilliseconds(kHopDownMS)); |
206 state_ &= ~state; | 309 state_ &= ~state; |
207 UpdateState(); | 310 UpdateState(); |
208 } else { | 311 } else { |
209 state_ &= ~state; | 312 state_ &= ~state; |
210 UpdateState(); | 313 UpdateState(); |
211 } | 314 } |
212 if (state & STATE_ATTENTION) | 315 if (state & STATE_ATTENTION) |
213 bar_->ShowAttention(false); | 316 bar_->ShowAttention(false); |
| 317 if (state & STATE_PENDING) |
| 318 icon_pulse_animation_.reset(); |
214 } | 319 } |
215 } | 320 } |
216 | 321 |
217 gfx::Rect LauncherButton::GetIconBounds() const { | 322 gfx::Rect LauncherButton::GetIconBounds() const { |
218 return icon_view_->bounds(); | 323 return icon_view_->bounds(); |
219 } | 324 } |
220 | 325 |
221 bool LauncherButton::OnMousePressed(const ui::MouseEvent& event) { | 326 bool LauncherButton::OnMousePressed(const ui::MouseEvent& event) { |
222 CustomButton::OnMousePressed(event); | 327 CustomButton::OnMousePressed(event); |
223 host_->PointerPressedOnButton(this, LauncherButtonHost::MOUSE, event); | 328 host_->PointerPressedOnButton(this, LauncherButtonHost::MOUSE, event); |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 | 454 |
350 LauncherButton::IconView* LauncherButton::CreateIconView() { | 455 LauncherButton::IconView* LauncherButton::CreateIconView() { |
351 return new IconView; | 456 return new IconView; |
352 } | 457 } |
353 | 458 |
354 bool LauncherButton::IsShelfHorizontal() const { | 459 bool LauncherButton::IsShelfHorizontal() const { |
355 return host_->GetShelfAlignment() == SHELF_ALIGNMENT_BOTTOM; | 460 return host_->GetShelfAlignment() == SHELF_ALIGNMENT_BOTTOM; |
356 } | 461 } |
357 | 462 |
358 void LauncherButton::UpdateState() { | 463 void LauncherButton::UpdateState() { |
359 if (state_ == STATE_NORMAL) { | 464 if (state_ == STATE_NORMAL || state_ & STATE_PENDING) { |
360 bar_->SetVisible(false); | 465 bar_->SetVisible(false); |
361 } else { | 466 } else { |
362 int bar_id; | 467 int bar_id; |
363 if (IsShelfHorizontal()) { | 468 if (IsShelfHorizontal()) { |
364 if (state_ & (STATE_HOVERED | STATE_FOCUSED | STATE_ATTENTION)) | 469 if (state_ & (STATE_HOVERED | STATE_FOCUSED | STATE_ATTENTION)) |
365 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_HOVER; | 470 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_HOVER; |
366 else if (state_ & STATE_ACTIVE) | 471 else if (state_ & STATE_ACTIVE) |
367 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_ACTIVE; | 472 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_ACTIVE; |
368 else | 473 else |
369 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_RUNNING; | 474 bar_id = IDR_AURA_LAUNCHER_UNDERLINE_RUNNING; |
(...skipping 29 matching lines...) Expand all Loading... |
399 bar_->SetVerticalAlignment(views::ImageView::CENTER); | 504 bar_->SetVerticalAlignment(views::ImageView::CENTER); |
400 break; | 505 break; |
401 } | 506 } |
402 | 507 |
403 Layout(); | 508 Layout(); |
404 SchedulePaint(); | 509 SchedulePaint(); |
405 } | 510 } |
406 | 511 |
407 } // namespace internal | 512 } // namespace internal |
408 } // namespace ash | 513 } // namespace ash |
OLD | NEW |