Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 "ash/wm/session_state_animator.h" |
| 6 | |
| 7 | #include "ash/shell.h" |
| 8 | #include "ash/shell_window_ids.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 9 | #include "ash/wm/window_animations.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | #include "ui/aura/client/aura_constants.h" |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 11 | #include "ui/aura/window_event_dispatcher.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 12 | #include "ui/compositor/layer_animation_observer.h" |
| 13 | #include "ui/compositor/layer_animation_sequence.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 14 | #include "ui/compositor/scoped_layer_animation_settings.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 15 | #include "ui/views/widget/widget.h" |
| 16 | |
| 17 | namespace ash { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | namespace { |
| 19 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 20 | // Slightly-smaller size that we scale the screen down to for the pre-lock and |
| 21 | // pre-shutdown states. |
| 22 | const float kSlowCloseSizeRatio = 0.95f; |
| 23 | |
| 24 | // Maximum opacity of white layer when animating pre-shutdown state. |
| 25 | const float kPartialFadeRatio = 0.3f; |
| 26 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 27 | // Minimum size. Not zero as it causes numeric issues. |
| 28 | const float kMinimumScale = 1e-4f; |
| 29 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 30 | // Returns the transform that should be applied to containers for the slow-close |
| 31 | // animation. |
| 32 | gfx::Transform GetSlowCloseTransform() { |
| 33 | gfx::Size root_size = Shell::GetPrimaryRootWindow()->bounds().size(); |
| 34 | gfx::Transform transform; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 35 | transform.Translate( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 36 | floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.width() + 0.5), |
| 37 | floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.height() + 0.5)); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 38 | transform.Scale(kSlowCloseSizeRatio, kSlowCloseSizeRatio); |
| 39 | return transform; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // Returns the transform that should be applied to containers for the fast-close |
| 43 | // animation. |
| 44 | gfx::Transform GetFastCloseTransform() { |
| 45 | gfx::Size root_size = Shell::GetPrimaryRootWindow()->bounds().size(); |
| 46 | gfx::Transform transform; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 47 | transform.Translate(floor(0.5 * root_size.width() + 0.5), |
| 48 | floor(0.5 * root_size.height() + 0.5)); |
| 49 | transform.Scale(kMinimumScale, kMinimumScale); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 50 | return transform; |
| 51 | } |
| 52 | |
| 53 | // Slowly shrinks |window| to a slightly-smaller size. |
| 54 | void StartSlowCloseAnimationForWindow(aura::Window* window, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 55 | base::TimeDelta duration, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 56 | ui::LayerAnimationObserver* observer) { |
| 57 | ui::LayerAnimator* animator = window->layer()->GetAnimator(); |
| 58 | animator->set_preemption_strategy( |
| 59 | ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 60 | ui::LayerAnimationSequence* sequence = new ui::LayerAnimationSequence( |
| 61 | ui::LayerAnimationElement::CreateTransformElement( |
| 62 | GetSlowCloseTransform(), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 63 | duration)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 64 | if (observer) |
| 65 | sequence->AddObserver(observer); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 66 | animator->StartAnimation(sequence); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Quickly undoes the effects of the slow-close animation on |window|. |
| 70 | void StartUndoSlowCloseAnimationForWindow( |
| 71 | aura::Window* window, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 72 | base::TimeDelta duration, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 73 | ui::LayerAnimationObserver* observer) { |
| 74 | ui::LayerAnimator* animator = window->layer()->GetAnimator(); |
| 75 | animator->set_preemption_strategy( |
| 76 | ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 77 | ui::LayerAnimationSequence* sequence = new ui::LayerAnimationSequence( |
| 78 | ui::LayerAnimationElement::CreateTransformElement( |
| 79 | gfx::Transform(), |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 80 | duration)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 81 | if (observer) |
| 82 | sequence->AddObserver(observer); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 83 | animator->StartAnimation(sequence); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // Quickly shrinks |window| down to a point in the center of the screen and |
| 87 | // fades it out to 0 opacity. |
| 88 | void StartFastCloseAnimationForWindow(aura::Window* window, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 89 | base::TimeDelta duration, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 90 | ui::LayerAnimationObserver* observer) { |
| 91 | ui::LayerAnimator* animator = window->layer()->GetAnimator(); |
| 92 | animator->set_preemption_strategy( |
| 93 | ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 94 | animator->StartAnimation( |
| 95 | new ui::LayerAnimationSequence( |
| 96 | ui::LayerAnimationElement::CreateTransformElement( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 97 | GetFastCloseTransform(), duration))); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 98 | ui::LayerAnimationSequence* sequence = new ui::LayerAnimationSequence( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 99 | ui::LayerAnimationElement::CreateOpacityElement(0.0, duration)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 100 | if (observer) |
| 101 | sequence->AddObserver(observer); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 102 | animator->StartAnimation(sequence); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 105 | // Fades |window| to |target_opacity| over |duration|. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 106 | void StartPartialFadeAnimation(aura::Window* window, |
| 107 | float target_opacity, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 108 | base::TimeDelta duration, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 109 | ui::LayerAnimationObserver* observer) { |
| 110 | ui::LayerAnimator* animator = window->layer()->GetAnimator(); |
| 111 | animator->set_preemption_strategy( |
| 112 | ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 113 | ui::LayerAnimationSequence* sequence = new ui::LayerAnimationSequence( |
| 114 | ui::LayerAnimationElement::CreateOpacityElement( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 115 | target_opacity, duration)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 116 | if (observer) |
| 117 | sequence->AddObserver(observer); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 118 | animator->StartAnimation(sequence); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 121 | // Fades |window| to |opacity| over |duration|. |
| 122 | void StartOpacityAnimationForWindow(aura::Window* window, |
| 123 | float opacity, |
| 124 | base::TimeDelta duration, |
| 125 | ui::LayerAnimationObserver* observer) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 126 | ui::LayerAnimator* animator = window->layer()->GetAnimator(); |
| 127 | animator->set_preemption_strategy( |
| 128 | ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 129 | ui::LayerAnimationSequence* sequence = new ui::LayerAnimationSequence( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 130 | ui::LayerAnimationElement::CreateOpacityElement(opacity, duration)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 131 | if (observer) |
| 132 | sequence->AddObserver(observer); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 133 | animator->StartAnimation(sequence); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Makes |window| fully transparent instantaneously. |
| 137 | void HideWindowImmediately(aura::Window* window, |
| 138 | ui::LayerAnimationObserver* observer) { |
| 139 | window->layer()->SetOpacity(0.0); |
| 140 | if (observer) |
| 141 | observer->OnLayerAnimationEnded(NULL); |
| 142 | } |
| 143 | |
| 144 | // Restores |window| to its original position and scale and full opacity |
| 145 | // instantaneously. |
| 146 | void RestoreWindow(aura::Window* window, ui::LayerAnimationObserver* observer) { |
| 147 | window->layer()->SetTransform(gfx::Transform()); |
| 148 | window->layer()->SetOpacity(1.0); |
| 149 | if (observer) |
| 150 | observer->OnLayerAnimationEnded(NULL); |
| 151 | } |
| 152 | |
| 153 | void HideWindow(aura::Window* window, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 154 | base::TimeDelta duration, |
| 155 | bool above, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 156 | ui::LayerAnimationObserver* observer) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 157 | ui::Layer* layer = window->layer(); |
| 158 | ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); |
| 159 | |
| 160 | settings.SetPreemptionStrategy( |
| 161 | ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 162 | settings.SetTransitionDuration(duration); |
| 163 | |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 164 | settings.SetTweenType(gfx::Tween::EASE_OUT); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 165 | SetTransformForScaleAnimation(layer, |
| 166 | above ? LAYER_SCALE_ANIMATION_ABOVE : LAYER_SCALE_ANIMATION_BELOW); |
| 167 | |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 168 | settings.SetTweenType(gfx::Tween::EASE_IN_OUT); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 169 | layer->SetOpacity(0.0f); |
| 170 | |
| 171 | // After the animation completes snap the transform back to the identity, |
| 172 | // otherwise any one that asks for screen bounds gets a slightly scaled |
| 173 | // version. |
| 174 | settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION); |
| 175 | settings.SetTransitionDuration(base::TimeDelta()); |
| 176 | layer->SetTransform(gfx::Transform()); |
| 177 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 178 | // A bit of a dirty trick: we need to catch the end of the animation we don't |
| 179 | // control. So we use two facts we know: which animator will be used and the |
| 180 | // target opacity to add "Do nothing" animation sequence. |
| 181 | // Unfortunately, we can not just use empty LayerAnimationSequence, because |
| 182 | // it does not call NotifyEnded(). |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 183 | if (observer) { |
| 184 | ui::LayerAnimationSequence* sequence = new ui::LayerAnimationSequence( |
| 185 | ui::LayerAnimationElement::CreateOpacityElement( |
| 186 | 0.0, base::TimeDelta())); |
| 187 | sequence->AddObserver(observer); |
| 188 | layer->GetAnimator()->ScheduleAnimation(sequence); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // Animates |window| to identity transform and full opacity over |duration|. |
| 193 | void TransformWindowToBaseState(aura::Window* window, |
| 194 | base::TimeDelta duration, |
| 195 | ui::LayerAnimationObserver* observer) { |
| 196 | ui::Layer* layer = window->layer(); |
| 197 | ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); |
| 198 | |
| 199 | // Animate to target values. |
| 200 | settings.SetPreemptionStrategy( |
| 201 | ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 202 | settings.SetTransitionDuration(duration); |
| 203 | |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 204 | settings.SetTweenType(gfx::Tween::EASE_OUT); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 205 | layer->SetTransform(gfx::Transform()); |
| 206 | |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 207 | settings.SetTweenType(gfx::Tween::EASE_IN_OUT); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 208 | layer->SetOpacity(1.0f); |
| 209 | |
| 210 | // A bit of a dirty trick: we need to catch the end of the animation we don't |
| 211 | // control. So we use two facts we know: which animator will be used and the |
| 212 | // target opacity to add "Do nothing" animation sequence. |
| 213 | // Unfortunately, we can not just use empty LayerAnimationSequence, because |
| 214 | // it does not call NotifyEnded(). |
| 215 | if (observer) { |
| 216 | ui::LayerAnimationSequence* sequence = new ui::LayerAnimationSequence( |
| 217 | ui::LayerAnimationElement::CreateOpacityElement( |
| 218 | 1.0, base::TimeDelta())); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 219 | sequence->AddObserver(observer); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 220 | layer->GetAnimator()->ScheduleAnimation(sequence); |
| 221 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void ShowWindow(aura::Window* window, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 225 | base::TimeDelta duration, |
| 226 | bool above, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 227 | ui::LayerAnimationObserver* observer) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 228 | ui::Layer* layer = window->layer(); |
| 229 | ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); |
| 230 | |
| 231 | // Set initial state of animation |
| 232 | settings.SetPreemptionStrategy( |
| 233 | ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 234 | settings.SetTransitionDuration(base::TimeDelta()); |
| 235 | SetTransformForScaleAnimation(layer, |
| 236 | above ? LAYER_SCALE_ANIMATION_ABOVE : LAYER_SCALE_ANIMATION_BELOW); |
| 237 | |
| 238 | TransformWindowToBaseState(window, duration, observer); |
| 239 | } |
| 240 | |
| 241 | // Starts grayscale/brightness animation for |window| over |duration|. Target |
| 242 | // value for both grayscale and brightness are specified by |target|. |
| 243 | void StartGrayscaleBrightnessAnimationForWindow( |
| 244 | aura::Window* window, |
| 245 | float target, |
| 246 | base::TimeDelta duration, |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 247 | gfx::Tween::Type tween_type, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 248 | ui::LayerAnimationObserver* observer) { |
| 249 | ui::LayerAnimator* animator = window->layer()->GetAnimator(); |
| 250 | |
| 251 | scoped_ptr<ui::LayerAnimationSequence> brightness_sequence( |
| 252 | new ui::LayerAnimationSequence()); |
| 253 | scoped_ptr<ui::LayerAnimationSequence> grayscale_sequence( |
| 254 | new ui::LayerAnimationSequence()); |
| 255 | |
| 256 | scoped_ptr<ui::LayerAnimationElement> brightness_element( |
| 257 | ui::LayerAnimationElement::CreateBrightnessElement( |
| 258 | target, duration)); |
| 259 | brightness_element->set_tween_type(tween_type); |
| 260 | brightness_sequence->AddElement(brightness_element.release()); |
| 261 | |
| 262 | scoped_ptr<ui::LayerAnimationElement> grayscale_element( |
| 263 | ui::LayerAnimationElement::CreateGrayscaleElement( |
| 264 | target, duration)); |
| 265 | grayscale_element->set_tween_type(tween_type); |
| 266 | grayscale_sequence->AddElement(grayscale_element.release()); |
| 267 | |
| 268 | std::vector<ui::LayerAnimationSequence*> animations; |
| 269 | animations.push_back(brightness_sequence.release()); |
| 270 | animations.push_back(grayscale_sequence.release()); |
| 271 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 272 | if (observer) |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 273 | animations[0]->AddObserver(observer); |
| 274 | |
| 275 | animator->set_preemption_strategy( |
| 276 | ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 277 | |
| 278 | animator->StartTogether(animations); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // Animation observer that will drop animated foreground once animation is |
| 282 | // finished. It is used in when undoing shutdown animation. |
| 283 | class CallbackAnimationObserver : public ui::LayerAnimationObserver { |
| 284 | public: |
| 285 | explicit CallbackAnimationObserver(base::Callback<void(void)> &callback) |
| 286 | : callback_(callback) { |
| 287 | } |
| 288 | virtual ~CallbackAnimationObserver() { |
| 289 | } |
| 290 | |
| 291 | private: |
| 292 | // Overridden from ui::LayerAnimationObserver: |
| 293 | virtual void OnLayerAnimationEnded(ui::LayerAnimationSequence* seq) |
| 294 | OVERRIDE { |
| 295 | // Drop foreground once animation is over. |
| 296 | callback_.Run(); |
| 297 | delete this; |
| 298 | } |
| 299 | |
| 300 | virtual void OnLayerAnimationAborted(ui::LayerAnimationSequence* seq) |
| 301 | OVERRIDE { |
| 302 | // Drop foreground once animation is over. |
| 303 | callback_.Run(); |
| 304 | delete this; |
| 305 | } |
| 306 | |
| 307 | virtual void OnLayerAnimationScheduled(ui::LayerAnimationSequence* seq) |
| 308 | OVERRIDE {} |
| 309 | |
| 310 | base::Callback<void(void)> callback_; |
| 311 | |
| 312 | DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver); |
| 313 | }; |
| 314 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 315 | |
| 316 | bool IsLayerAnimated(ui::Layer* layer, |
| 317 | SessionStateAnimator::AnimationType type) { |
| 318 | switch (type) { |
| 319 | case SessionStateAnimator::ANIMATION_PARTIAL_CLOSE: |
| 320 | if (layer->GetTargetTransform() != GetSlowCloseTransform()) |
| 321 | return false; |
| 322 | break; |
| 323 | case SessionStateAnimator::ANIMATION_UNDO_PARTIAL_CLOSE: |
| 324 | if (layer->GetTargetTransform() != gfx::Transform()) |
| 325 | return false; |
| 326 | break; |
| 327 | case SessionStateAnimator::ANIMATION_FULL_CLOSE: |
| 328 | if (layer->GetTargetTransform() != GetFastCloseTransform() || |
| 329 | layer->GetTargetOpacity() > 0.0001) |
| 330 | return false; |
| 331 | break; |
| 332 | case SessionStateAnimator::ANIMATION_FADE_IN: |
| 333 | if (layer->GetTargetOpacity() < 0.9999) |
| 334 | return false; |
| 335 | break; |
| 336 | case SessionStateAnimator::ANIMATION_FADE_OUT: |
| 337 | if (layer->GetTargetOpacity() > 0.0001) |
| 338 | return false; |
| 339 | break; |
| 340 | case SessionStateAnimator::ANIMATION_HIDE_IMMEDIATELY: |
| 341 | if (layer->GetTargetOpacity() > 0.0001) |
| 342 | return false; |
| 343 | break; |
| 344 | case SessionStateAnimator::ANIMATION_RESTORE: |
| 345 | if (layer->opacity() < 0.9999 || layer->transform() != gfx::Transform()) |
| 346 | return false; |
| 347 | break; |
| 348 | case SessionStateAnimator::ANIMATION_GRAYSCALE_BRIGHTNESS: |
| 349 | if ((layer->GetTargetBrightness() < 0.9999) || |
| 350 | (layer->GetTargetGrayscale() < 0.9999)) |
| 351 | return false; |
| 352 | break; |
| 353 | case SessionStateAnimator::ANIMATION_UNDO_GRAYSCALE_BRIGHTNESS: |
| 354 | if ((layer->GetTargetBrightness() > 0.0001) || |
| 355 | (layer->GetTargetGrayscale() > 0.0001)) |
| 356 | return false; |
| 357 | break; |
| 358 | case SessionStateAnimator::ANIMATION_DROP: |
| 359 | case SessionStateAnimator::ANIMATION_UNDO_LIFT: |
| 360 | //ToDo(antim) : check other effects |
| 361 | if (layer->GetTargetOpacity() < 0.9999) |
| 362 | return false; |
| 363 | break; |
| 364 | //ToDo(antim) : check other effects |
| 365 | case SessionStateAnimator::ANIMATION_LIFT: |
| 366 | if (layer->GetTargetOpacity() > 0.0001) |
| 367 | return false; |
| 368 | break; |
| 369 | case SessionStateAnimator::ANIMATION_RAISE_TO_SCREEN: |
| 370 | //ToDo(antim) : check other effects |
| 371 | if (layer->GetTargetOpacity() < 0.9999) |
| 372 | return false; |
| 373 | break; |
| 374 | //ToDo(antim) : check other effects |
| 375 | case SessionStateAnimator::ANIMATION_LOWER_BELOW_SCREEN: |
| 376 | if (layer->GetTargetOpacity() > 0.0001) |
| 377 | return false; |
| 378 | break; |
| 379 | default: |
| 380 | NOTREACHED() << "Unhandled animation type " << type; |
| 381 | return false; |
| 382 | } |
| 383 | return true; |
| 384 | } |
| 385 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 386 | } // namespace |
| 387 | |
| 388 | bool SessionStateAnimator::TestApi::ContainersAreAnimated( |
| 389 | int container_mask, AnimationType type) const { |
| 390 | aura::Window::Windows containers; |
| 391 | animator_->GetContainers(container_mask, &containers); |
| 392 | for (aura::Window::Windows::const_iterator it = containers.begin(); |
| 393 | it != containers.end(); ++it) { |
| 394 | aura::Window* window = *it; |
| 395 | ui::Layer* layer = window->layer(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 396 | if (!IsLayerAnimated(layer, type)) |
| 397 | return false; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 398 | } |
| 399 | return true; |
| 400 | } |
| 401 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 402 | bool SessionStateAnimator::TestApi::RootWindowIsAnimated(AnimationType type) |
| 403 | const { |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 404 | aura::Window* root_window = Shell::GetPrimaryRootWindow(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 405 | ui::Layer* layer = root_window->layer(); |
| 406 | return IsLayerAnimated(layer, type); |
| 407 | } |
| 408 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 409 | const int SessionStateAnimator::kAllLockScreenContainersMask = |
| 410 | SessionStateAnimator::LOCK_SCREEN_BACKGROUND | |
| 411 | SessionStateAnimator::LOCK_SCREEN_CONTAINERS | |
| 412 | SessionStateAnimator::LOCK_SCREEN_RELATED_CONTAINERS; |
| 413 | |
| 414 | const int SessionStateAnimator::kAllContainersMask = |
| 415 | SessionStateAnimator::kAllLockScreenContainersMask | |
| 416 | SessionStateAnimator::DESKTOP_BACKGROUND | |
| 417 | SessionStateAnimator::LAUNCHER | |
| 418 | SessionStateAnimator::NON_LOCK_SCREEN_CONTAINERS; |
| 419 | |
| 420 | SessionStateAnimator::SessionStateAnimator() { |
| 421 | } |
| 422 | |
| 423 | SessionStateAnimator::~SessionStateAnimator() { |
| 424 | } |
| 425 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 426 | base::TimeDelta SessionStateAnimator::GetDuration(AnimationSpeed speed) { |
| 427 | switch (speed) { |
| 428 | case ANIMATION_SPEED_IMMEDIATE: |
| 429 | return base::TimeDelta(); |
| 430 | case ANIMATION_SPEED_UNDOABLE: |
| 431 | return base::TimeDelta::FromMilliseconds(400); |
| 432 | case ANIMATION_SPEED_REVERT: |
| 433 | return base::TimeDelta::FromMilliseconds(150); |
| 434 | case ANIMATION_SPEED_FAST: |
| 435 | return base::TimeDelta::FromMilliseconds(150); |
| 436 | case ANIMATION_SPEED_SHOW_LOCK_SCREEN: |
| 437 | return base::TimeDelta::FromMilliseconds(200); |
| 438 | case ANIMATION_SPEED_MOVE_WINDOWS: |
| 439 | return base::TimeDelta::FromMilliseconds(350); |
| 440 | case ANIMATION_SPEED_UNDO_MOVE_WINDOWS: |
| 441 | return base::TimeDelta::FromMilliseconds(350); |
| 442 | case ANIMATION_SPEED_SHUTDOWN: |
| 443 | return base::TimeDelta::FromMilliseconds(1000); |
| 444 | case ANIMATION_SPEED_REVERT_SHUTDOWN: |
| 445 | return base::TimeDelta::FromMilliseconds(500); |
| 446 | } |
| 447 | // Satisfy compilers that do not understand that we will return from switch |
| 448 | // above anyway. |
| 449 | DCHECK(false) << "Unhandled animation speed " << speed; |
| 450 | return base::TimeDelta(); |
| 451 | } |
| 452 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 453 | // Fills |containers| with the containers described by |container_mask|. |
| 454 | void SessionStateAnimator::GetContainers(int container_mask, |
| 455 | aura::Window::Windows* containers) { |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 456 | aura::Window* root_window = Shell::GetPrimaryRootWindow(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 457 | containers->clear(); |
| 458 | |
| 459 | if (container_mask & DESKTOP_BACKGROUND) { |
| 460 | containers->push_back(Shell::GetContainer( |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 461 | root_window, kShellWindowId_DesktopBackgroundContainer)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 462 | } |
| 463 | if (container_mask & LAUNCHER) { |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 464 | containers->push_back( |
| 465 | Shell::GetContainer(root_window, kShellWindowId_ShelfContainer)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 466 | } |
| 467 | if (container_mask & NON_LOCK_SCREEN_CONTAINERS) { |
| 468 | // TODO(antrim): Figure out a way to eliminate a need to exclude launcher |
| 469 | // in such way. |
| 470 | aura::Window* non_lock_screen_containers = Shell::GetContainer( |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 471 | root_window, kShellWindowId_NonLockScreenContainersContainer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 472 | aura::Window::Windows children = non_lock_screen_containers->children(); |
| 473 | |
| 474 | for (aura::Window::Windows::const_iterator it = children.begin(); |
| 475 | it != children.end(); ++it) { |
| 476 | aura::Window* window = *it; |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 477 | if (window->id() == kShellWindowId_ShelfContainer) |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 478 | continue; |
| 479 | containers->push_back(window); |
| 480 | } |
| 481 | } |
| 482 | if (container_mask & LOCK_SCREEN_BACKGROUND) { |
| 483 | containers->push_back(Shell::GetContainer( |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 484 | root_window, kShellWindowId_LockScreenBackgroundContainer)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 485 | } |
| 486 | if (container_mask & LOCK_SCREEN_CONTAINERS) { |
| 487 | containers->push_back(Shell::GetContainer( |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 488 | root_window, kShellWindowId_LockScreenContainersContainer)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 489 | } |
| 490 | if (container_mask & LOCK_SCREEN_RELATED_CONTAINERS) { |
| 491 | containers->push_back(Shell::GetContainer( |
Ben Murdoch | c5cede9 | 2014-04-10 11:22:14 +0100 | [diff] [blame] | 492 | root_window, kShellWindowId_LockScreenRelatedContainersContainer)); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 493 | } |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void SessionStateAnimator::StartAnimation(int container_mask, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 497 | AnimationType type, |
| 498 | AnimationSpeed speed) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 499 | aura::Window::Windows containers; |
| 500 | GetContainers(container_mask, &containers); |
| 501 | for (aura::Window::Windows::const_iterator it = containers.begin(); |
| 502 | it != containers.end(); ++it) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 503 | RunAnimationForWindow(*it, type, speed, NULL); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 507 | void SessionStateAnimator::StartAnimationWithCallback( |
| 508 | int container_mask, |
| 509 | AnimationType type, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 510 | AnimationSpeed speed, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 511 | base::Callback<void(void)>& callback) { |
| 512 | aura::Window::Windows containers; |
| 513 | GetContainers(container_mask, &containers); |
| 514 | for (aura::Window::Windows::const_iterator it = containers.begin(); |
| 515 | it != containers.end(); ++it) { |
| 516 | ui::LayerAnimationObserver* observer = |
| 517 | new CallbackAnimationObserver(callback); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 518 | RunAnimationForWindow(*it, type, speed, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 522 | void SessionStateAnimator::StartAnimationWithObserver( |
| 523 | int container_mask, |
| 524 | AnimationType type, |
| 525 | AnimationSpeed speed, |
| 526 | ui::LayerAnimationObserver* observer) { |
| 527 | aura::Window::Windows containers; |
| 528 | GetContainers(container_mask, &containers); |
| 529 | for (aura::Window::Windows::const_iterator it = containers.begin(); |
| 530 | it != containers.end(); ++it) { |
| 531 | RunAnimationForWindow(*it, type, speed, observer); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | void SessionStateAnimator::StartGlobalAnimation(AnimationType type, |
| 536 | AnimationSpeed speed) { |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 537 | aura::Window* root_window = Shell::GetPrimaryRootWindow(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 538 | RunAnimationForWindow(root_window, type, speed, NULL); |
| 539 | } |
| 540 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 541 | void SessionStateAnimator::RunAnimationForWindow( |
| 542 | aura::Window* window, |
| 543 | AnimationType type, |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 544 | AnimationSpeed speed, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 545 | ui::LayerAnimationObserver* observer) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 546 | base::TimeDelta duration = GetDuration(speed); |
| 547 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 548 | switch (type) { |
| 549 | case ANIMATION_PARTIAL_CLOSE: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 550 | StartSlowCloseAnimationForWindow(window, duration, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 551 | break; |
| 552 | case ANIMATION_UNDO_PARTIAL_CLOSE: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 553 | StartUndoSlowCloseAnimationForWindow(window, duration, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 554 | break; |
| 555 | case ANIMATION_FULL_CLOSE: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 556 | StartFastCloseAnimationForWindow(window, duration, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 557 | break; |
| 558 | case ANIMATION_FADE_IN: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 559 | StartOpacityAnimationForWindow(window, 1.0, duration, observer); |
| 560 | break; |
| 561 | case ANIMATION_FADE_OUT: |
| 562 | StartOpacityAnimationForWindow(window, 0.0, duration, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 563 | break; |
| 564 | case ANIMATION_HIDE_IMMEDIATELY: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 565 | DCHECK_EQ(speed, ANIMATION_SPEED_IMMEDIATE); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 566 | HideWindowImmediately(window, observer); |
| 567 | break; |
| 568 | case ANIMATION_RESTORE: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 569 | DCHECK_EQ(speed, ANIMATION_SPEED_IMMEDIATE); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 570 | RestoreWindow(window, observer); |
| 571 | break; |
| 572 | case ANIMATION_LIFT: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 573 | HideWindow(window, duration, true, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 574 | break; |
| 575 | case ANIMATION_DROP: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 576 | ShowWindow(window, duration, true, observer); |
| 577 | break; |
| 578 | case ANIMATION_UNDO_LIFT: |
| 579 | TransformWindowToBaseState(window, duration, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 580 | break; |
| 581 | case ANIMATION_RAISE_TO_SCREEN: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 582 | ShowWindow(window, duration, false, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 583 | break; |
| 584 | case ANIMATION_LOWER_BELOW_SCREEN: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 585 | HideWindow(window, duration, false, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 586 | break; |
| 587 | case ANIMATION_PARTIAL_FADE_IN: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 588 | StartPartialFadeAnimation( |
| 589 | window, kPartialFadeRatio, duration, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 590 | break; |
| 591 | case ANIMATION_UNDO_PARTIAL_FADE_IN: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 592 | StartPartialFadeAnimation(window, 0.0, duration, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 593 | break; |
| 594 | case ANIMATION_FULL_FADE_IN: |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 595 | StartPartialFadeAnimation(window, 1.0, duration, observer); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 596 | break; |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 597 | case ANIMATION_GRAYSCALE_BRIGHTNESS: |
| 598 | StartGrayscaleBrightnessAnimationForWindow( |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 599 | window, 1.0, duration, gfx::Tween::EASE_IN, observer); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 600 | break; |
| 601 | case ANIMATION_UNDO_GRAYSCALE_BRIGHTNESS: |
| 602 | StartGrayscaleBrightnessAnimationForWindow( |
Torne (Richard Coles) | d0247b1 | 2013-09-19 22:36:51 +0100 | [diff] [blame] | 603 | window, 0.0, duration, gfx::Tween::EASE_IN_OUT, observer); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 604 | break; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 608 | } // namespace ash |