OLD | NEW |
(Empty) | |
| 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/workspace/workspace_animations.h" |
| 6 |
| 7 #include "ash/ash_switches.h" |
| 8 #include "base/command_line.h" |
| 9 #include "ui/aura/window.h" |
| 10 #include "ui/compositor/layer.h" |
| 11 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 12 |
| 13 namespace ash { |
| 14 namespace internal { |
| 15 |
| 16 const int kWorkspaceSwitchTimeMS = 200; |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Tween type used when showing/hiding workspaces. |
| 21 const ui::Tween::Type kWorkspaceTweenType = ui::Tween::EASE_OUT; |
| 22 |
| 23 // Scales for workspaces above/below current workspace. |
| 24 const float kWorkspaceScaleAbove = 1.1f; |
| 25 const float kWorkspaceScaleBelow = .9f; |
| 26 |
| 27 enum WorkspaceScaleType { |
| 28 WORKSPACE_SCALE_ABOVE, |
| 29 WORKSPACE_SCALE_BELOW, |
| 30 }; |
| 31 |
| 32 // Applies the specified WorkspaceScaleType. |
| 33 void ApplyWorkspaceScale(ui::Layer* layer, WorkspaceScaleType type) { |
| 34 const float scale = type == WORKSPACE_SCALE_ABOVE ? kWorkspaceScaleAbove : |
| 35 kWorkspaceScaleBelow; |
| 36 ui::Transform transform; |
| 37 transform.ConcatScale(scale, scale); |
| 38 transform.ConcatTranslate( |
| 39 -layer->bounds().width() * (scale - 1.0f) / 2, |
| 40 -layer->bounds().height() * (scale - 1.0f) / 2); |
| 41 layer->SetTransform(transform); |
| 42 } |
| 43 |
| 44 // If |details.duration| is not-empty it is returned, otherwise |
| 45 // |kWorkspaceSwitchTimeMS| is returned. |
| 46 base::TimeDelta DurationForWorkspaceShowOrHide( |
| 47 const WorkspaceAnimationDetails& details) { |
| 48 return details.duration == base::TimeDelta() ? |
| 49 base::TimeDelta::FromMilliseconds(kWorkspaceSwitchTimeMS) : |
| 50 details.duration; |
| 51 } |
| 52 |
| 53 } // namespace |
| 54 |
| 55 WorkspaceAnimationDetails::WorkspaceAnimationDetails() |
| 56 : direction(WORKSPACE_ANIMATE_UP), |
| 57 animate(false), |
| 58 animate_opacity(false), |
| 59 animate_scale(false), |
| 60 pause_time_ms(0) { |
| 61 } |
| 62 |
| 63 WorkspaceAnimationDetails::~WorkspaceAnimationDetails() { |
| 64 } |
| 65 |
| 66 void ShowWorkspace(aura::Window* window, |
| 67 const WorkspaceAnimationDetails& details) { |
| 68 window->Show(); |
| 69 |
| 70 if (!details.animate || CommandLine::ForCurrentProcess()->HasSwitch( |
| 71 ash::switches::kAshWindowAnimationsDisabled)) { |
| 72 window->layer()->SetOpacity(1.0f); |
| 73 window->layer()->SetTransform(ui::Transform()); |
| 74 return; |
| 75 } |
| 76 |
| 77 window->layer()->SetOpacity(details.animate_opacity ? 0.0f : 1.0f); |
| 78 if (details.animate_scale) { |
| 79 ApplyWorkspaceScale(window->layer(), |
| 80 details.direction == WORKSPACE_ANIMATE_UP ? |
| 81 WORKSPACE_SCALE_BELOW : WORKSPACE_SCALE_ABOVE); |
| 82 } else { |
| 83 window->layer()->SetTransform(ui::Transform()); |
| 84 } |
| 85 |
| 86 // In order for pause to work we need to stop animations. |
| 87 window->layer()->GetAnimator()->StopAnimating(); |
| 88 |
| 89 { |
| 90 ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator()); |
| 91 |
| 92 if (details.pause_time_ms > 0) { |
| 93 settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION); |
| 94 window->layer()->GetAnimator()->SchedulePauseForProperties( |
| 95 base::TimeDelta::FromMilliseconds(details.pause_time_ms), |
| 96 ui::LayerAnimationElement::TRANSFORM, |
| 97 ui::LayerAnimationElement::OPACITY, |
| 98 ui::LayerAnimationElement::BRIGHTNESS, |
| 99 ui::LayerAnimationElement::VISIBILITY, |
| 100 -1); |
| 101 } |
| 102 |
| 103 settings.SetTweenType(kWorkspaceTweenType); |
| 104 settings.SetTransitionDuration(DurationForWorkspaceShowOrHide(details)); |
| 105 window->layer()->SetTransform(ui::Transform()); |
| 106 window->layer()->SetOpacity(1.0f); |
| 107 } |
| 108 } |
| 109 |
| 110 void HideWorkspace(aura::Window* window, |
| 111 const WorkspaceAnimationDetails& details) { |
| 112 window->layer()->SetTransform(ui::Transform()); |
| 113 window->layer()->SetOpacity(1.0f); |
| 114 window->layer()->GetAnimator()->StopAnimating(); |
| 115 |
| 116 if (!details.animate || CommandLine::ForCurrentProcess()->HasSwitch( |
| 117 ash::switches::kAshWindowAnimationsDisabled)) { |
| 118 window->Hide(); |
| 119 return; |
| 120 } |
| 121 |
| 122 ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator()); |
| 123 if (details.pause_time_ms > 0) { |
| 124 settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION); |
| 125 window->layer()->GetAnimator()->SchedulePauseForProperties( |
| 126 base::TimeDelta::FromMilliseconds(details.pause_time_ms), |
| 127 ui::LayerAnimationElement::TRANSFORM, |
| 128 ui::LayerAnimationElement::OPACITY, |
| 129 ui::LayerAnimationElement::BRIGHTNESS, |
| 130 ui::LayerAnimationElement::VISIBILITY, |
| 131 -1); |
| 132 } |
| 133 |
| 134 settings.SetTransitionDuration(DurationForWorkspaceShowOrHide(details)); |
| 135 settings.SetTweenType(kWorkspaceTweenType); |
| 136 if (details.animate_scale) { |
| 137 ApplyWorkspaceScale(window->layer(), |
| 138 details.direction == WORKSPACE_ANIMATE_UP ? |
| 139 WORKSPACE_SCALE_ABOVE : WORKSPACE_SCALE_BELOW); |
| 140 } else { |
| 141 window->layer()->SetTransform(ui::Transform()); |
| 142 } |
| 143 |
| 144 // NOTE: Hide() must be before SetOpacity(), else |
| 145 // VisibilityController::UpdateLayerVisibility doesn't pass the false to the |
| 146 // layer so that the layer and window end up out of sync and confused. |
| 147 window->Hide(); |
| 148 |
| 149 if (details.animate_opacity) |
| 150 window->layer()->SetOpacity(0.0f); |
| 151 |
| 152 // After the animation completes snap the transform back to the identity, |
| 153 // otherwise any one that asks for screen bounds gets a slightly scaled |
| 154 // version. |
| 155 settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION); |
| 156 settings.SetTransitionDuration(base::TimeDelta()); |
| 157 window->layer()->SetTransform(ui::Transform()); |
| 158 } |
| 159 |
| 160 } // namespace internal |
| 161 } // namespace ash |
OLD | NEW |