1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/wm/session_state_animator.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ui/aura/root_window.h"
#include "ui/compositor/layer_animation_sequence.h"
namespace ash {
namespace internal {
namespace {
// Amount of time taken to scale the snapshot of the screen down to a
// slightly-smaller size once the user starts holding the power button. Used
// for both the pre-lock and pre-shutdown animations.
const int kSlowCloseAnimMs = 400;
// Amount of time taken to scale the snapshot of the screen back to its original
// size when the button is released.
const int kUndoSlowCloseAnimMs = 100;
// Amount of time taken to scale the snapshot down to a point in the center of
// the screen once the screen has been locked or we've been notified that the
// system is shutting down.
const int kFastCloseAnimMs = 150;
// Amount of time taken to make the lock window fade in when the screen is
// locked.
const int kLockFadeInAnimMs = 200;
// Slightly-smaller size that we scale the screen down to for the pre-lock and
// pre-shutdown states.
const float kSlowCloseSizeRatio = 0.95f;
// Returns the transform that should be applied to containers for the slow-close
// animation.
ui::Transform GetSlowCloseTransform() {
gfx::Size root_size = Shell::GetPrimaryRootWindow()->bounds().size();
ui::Transform transform;
transform.SetScale(kSlowCloseSizeRatio, kSlowCloseSizeRatio);
transform.ConcatTranslate(
floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.width() + 0.5),
floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.height() + 0.5));
return transform;
}
// Returns the transform that should be applied to containers for the fast-close
// animation.
ui::Transform GetFastCloseTransform() {
gfx::Size root_size = Shell::GetPrimaryRootWindow()->bounds().size();
ui::Transform transform;
transform.SetScale(0.0, 0.0);
transform.ConcatTranslate(floor(0.5 * root_size.width() + 0.5),
floor(0.5 * root_size.height() + 0.5));
return transform;
}
// Slowly shrinks |window| to a slightly-smaller size.
void StartSlowCloseAnimationForWindow(aura::Window* window) {
ui::LayerAnimator* animator = window->layer()->GetAnimator();
animator->set_preemption_strategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
animator->StartAnimation(
new ui::LayerAnimationSequence(
ui::LayerAnimationElement::CreateTransformElement(
GetSlowCloseTransform(),
base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs))));
}
// Quickly undoes the effects of the slow-close animation on |window|.
void StartUndoSlowCloseAnimationForWindow(aura::Window* window) {
ui::LayerAnimator* animator = window->layer()->GetAnimator();
animator->set_preemption_strategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
animator->StartAnimation(
new ui::LayerAnimationSequence(
ui::LayerAnimationElement::CreateTransformElement(
ui::Transform(),
base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs))));
}
// Quickly shrinks |window| down to a point in the center of the screen and
// fades it out to 0 opacity.
void StartFastCloseAnimationForWindow(aura::Window* window) {
ui::LayerAnimator* animator = window->layer()->GetAnimator();
animator->set_preemption_strategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
animator->StartAnimation(
new ui::LayerAnimationSequence(
ui::LayerAnimationElement::CreateTransformElement(
GetFastCloseTransform(),
base::TimeDelta::FromMilliseconds(kFastCloseAnimMs))));
animator->StartAnimation(
new ui::LayerAnimationSequence(
ui::LayerAnimationElement::CreateOpacityElement(
0.0, base::TimeDelta::FromMilliseconds(kFastCloseAnimMs))));
}
// Fades |window| in to full opacity.
void FadeInWindow(aura::Window* window) {
ui::LayerAnimator* animator = window->layer()->GetAnimator();
animator->set_preemption_strategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
animator->StartAnimation(
new ui::LayerAnimationSequence(
ui::LayerAnimationElement::CreateOpacityElement(
1.0, base::TimeDelta::FromMilliseconds(kLockFadeInAnimMs))));
}
// Makes |window| fully transparent instantaneously.
void HideWindow(aura::Window* window) {
window->layer()->SetOpacity(0.0);
}
// Restores |window| to its original position and scale and full opacity
// instantaneously.
void RestoreWindow(aura::Window* window) {
window->layer()->SetTransform(ui::Transform());
window->layer()->SetOpacity(1.0);
}
} // namespace
void SessionStateAnimator::TestApi::TriggerHideBlackLayerTimeout() {
animator_->DropBlackLayer();
animator_->hide_black_layer_timer_.Stop();
}
bool SessionStateAnimator::TestApi::ContainersAreAnimated(
int container_mask, AnimationType type) const {
aura::Window::Windows containers;
animator_->GetContainers(container_mask, &containers);
for (aura::Window::Windows::const_iterator it = containers.begin();
it != containers.end(); ++it) {
aura::Window* window = *it;
ui::Layer* layer = window->layer();
switch (type) {
case ANIMATION_SLOW_CLOSE:
if (layer->GetTargetTransform() != GetSlowCloseTransform())
return false;
break;
case ANIMATION_UNDO_SLOW_CLOSE:
if (layer->GetTargetTransform() != ui::Transform())
return false;
break;
case ANIMATION_FAST_CLOSE:
if (layer->GetTargetTransform() != GetFastCloseTransform() ||
layer->GetTargetOpacity() > 0.0001)
return false;
break;
case ANIMATION_FADE_IN:
if (layer->GetTargetOpacity() < 0.9999)
return false;
break;
case ANIMATION_HIDE:
if (layer->GetTargetOpacity() > 0.0001)
return false;
break;
case ANIMATION_RESTORE:
if (layer->opacity() < 0.9999 || layer->transform() != ui::Transform())
return false;
break;
default:
NOTREACHED() << "Unhandled animation type " << type;
return false;
}
}
return true;
}
bool SessionStateAnimator::TestApi::BlackLayerIsVisible() const {
return animator_->black_layer_.get() &&
animator_->black_layer_->visible();
}
gfx::Rect SessionStateAnimator::TestApi::GetBlackLayerBounds() const {
ui::Layer* layer = animator_->black_layer_.get();
return layer ? layer->bounds() : gfx::Rect();
}
SessionStateAnimator::SessionStateAnimator() {
Shell::GetPrimaryRootWindow()->AddRootWindowObserver(this);
}
SessionStateAnimator::~SessionStateAnimator() {
Shell::GetPrimaryRootWindow()->RemoveRootWindowObserver(this);
}
// Fills |containers| with the containers described by |container_mask|.
void SessionStateAnimator::GetContainers(int container_mask,
aura::Window::Windows* containers) {
aura::RootWindow* root_window = Shell::GetPrimaryRootWindow();
containers->clear();
if (container_mask & DESKTOP_BACKGROUND) {
containers->push_back(Shell::GetContainer(
root_window,
internal::kShellWindowId_DesktopBackgroundContainer));
}
if (container_mask & LAUNCHER) {
containers->push_back(Shell::GetContainer(
root_window,
internal::kShellWindowId_LauncherContainer));
}
if (container_mask & NON_LOCK_SCREEN_CONTAINERS) {
// TODO(antrim): Figure out a way to eliminate a need to exclude launcher
// in such way.
aura::Window* non_lock_screen_containers = Shell::GetContainer(
root_window,
internal::kShellWindowId_NonLockScreenContainersContainer);
aura::Window::Windows children = non_lock_screen_containers->children();
for (aura::Window::Windows::const_iterator it = children.begin();
it != children.end(); ++it) {
aura::Window* window = *it;
if (window->id() == internal::kShellWindowId_LauncherContainer)
continue;
containers->push_back(window);
}
}
if (container_mask & LOCK_SCREEN_BACKGROUND) {
containers->push_back(Shell::GetContainer(
root_window,
internal::kShellWindowId_LockScreenBackgroundContainer));
}
if (container_mask & LOCK_SCREEN_CONTAINERS) {
containers->push_back(Shell::GetContainer(
root_window,
internal::kShellWindowId_LockScreenContainersContainer));
}
if (container_mask & LOCK_SCREEN_RELATED_CONTAINERS) {
containers->push_back(Shell::GetContainer(
root_window,
internal::kShellWindowId_LockScreenRelatedContainersContainer));
}
}
// Apply animation |type| to all containers described by |container_mask|.
void SessionStateAnimator::StartAnimation(int container_mask,
AnimationType type) {
aura::Window::Windows containers;
GetContainers(container_mask, &containers);
for (aura::Window::Windows::const_iterator it = containers.begin();
it != containers.end(); ++it) {
aura::Window* window = *it;
switch (type) {
case ANIMATION_SLOW_CLOSE:
StartSlowCloseAnimationForWindow(window);
break;
case ANIMATION_UNDO_SLOW_CLOSE:
StartUndoSlowCloseAnimationForWindow(window);
break;
case ANIMATION_FAST_CLOSE:
StartFastCloseAnimationForWindow(window);
break;
case ANIMATION_FADE_IN:
FadeInWindow(window);
break;
case ANIMATION_HIDE:
HideWindow(window);
break;
case ANIMATION_RESTORE:
RestoreWindow(window);
break;
default:
NOTREACHED() << "Unhandled animation type " << type;
}
}
}
void SessionStateAnimator::OnRootWindowResized(const aura::RootWindow* root,
const gfx::Size& new_size) {
if (black_layer_.get())
black_layer_->SetBounds(gfx::Rect(root->bounds().size()));
}
void SessionStateAnimator::ShowBlackLayer() {
if (hide_black_layer_timer_.IsRunning())
hide_black_layer_timer_.Stop();
if (!black_layer_.get()) {
black_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR));
black_layer_->SetColor(SK_ColorBLACK);
ui::Layer* root_layer = Shell::GetPrimaryRootWindow()->layer();
black_layer_->SetBounds(root_layer->bounds());
root_layer->Add(black_layer_.get());
root_layer->StackAtBottom(black_layer_.get());
}
black_layer_->SetVisible(true);
}
void SessionStateAnimator::DropBlackLayer() {
black_layer_.reset();
}
void SessionStateAnimator::ScheduleDropBlackLayer() {
hide_black_layer_timer_.Stop();
hide_black_layer_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs),
this, &SessionStateAnimator::DropBlackLayer);
}
} // namespace internal
} // namespace ash
|