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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
|
// 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/power_button_controller.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "base/logging.h"
#include "base/time.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/compositor/layer.h"
#include "ui/gfx/compositor/layer_animation_element.h"
#include "ui/gfx/compositor/layer_animation_sequence.h"
#include "ui/gfx/compositor/layer_animator.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
#include "ui/gfx/transform.h"
namespace ash {
namespace {
// Amount of time that the power button needs to be held before we lock the
// screen.
const int kLockTimeoutMs = 400;
// Amount of time that the power button needs to be held before we shut down.
const int kShutdownTimeoutMs = 400;
// Amount of time to wait for our lock requests to be honored before giving up.
const int kLockFailTimeoutMs = 4000;
// When the button has been held continuously from the unlocked state, amount of
// time that we wait after the screen locker window is shown before starting the
// pre-shutdown animation.
const int kLockToShutdownTimeoutMs = 150;
// 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 = 500;
// Slightly-smaller size that we scale the screen down to for the pre-lock and
// pre-shutdown states.
const float kSlowCloseSizeRatio = 0.95f;
// Containers holding screen locker windows.
const int kScreenLockerContainerIds[] = {
internal::kShellWindowId_LockScreenContainer,
internal::kShellWindowId_LockModalContainer,
};
// Containers holding additional windows that should be shown while the screen
// is locked.
const int kRelatedContainerIds[] = {
internal::kShellWindowId_StatusContainer,
internal::kShellWindowId_MenusAndTooltipsContainer,
};
// Is |window| a container that holds screen locker windows?
bool IsScreenLockerContainer(aura::Window* window) {
for (size_t i = 0; i < arraysize(kScreenLockerContainerIds); ++i)
if (window->id() == kScreenLockerContainerIds[i])
return true;
return false;
}
// Is |window| a container that holds other windows that should be shown while
// the screen is locked?
bool IsRelatedContainer(aura::Window* window) {
for (size_t i = 0; i < arraysize(kRelatedContainerIds); ++i)
if (window->id() == kRelatedContainerIds[i])
return true;
return false;
}
// Returns the transform that should be applied to containers for the slow-close
// animation.
ui::Transform GetSlowCloseTransform() {
gfx::Size root_size = aura::RootWindow::GetInstance()->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 = aura::RootWindow::GetInstance()->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);
}
// Fills |containers| with the containers described by |group|.
void GetContainers(PowerButtonController::ContainerGroup group,
aura::Window::Windows* containers) {
containers->clear();
aura::Window* root = aura::RootWindow::GetInstance();
for (aura::Window::Windows::const_iterator it = root->children().begin();
it != root->children().end(); ++it) {
aura::Window* window = *it;
bool matched = true;
if (group != PowerButtonController::ALL_CONTAINERS) {
bool is_screen_locker = IsScreenLockerContainer(window);
bool is_related = IsRelatedContainer(window);
switch (group) {
case PowerButtonController::SCREEN_LOCKER_CONTAINERS:
matched = is_screen_locker;
break;
case PowerButtonController::SCREEN_LOCKER_AND_RELATED_CONTAINERS:
matched = is_screen_locker || is_related;
break;
case PowerButtonController::
ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS:
matched = !is_screen_locker && !is_related;
break;
default:
NOTREACHED() << "Unhandled container group " << group;
}
}
if (matched)
containers->push_back(window);
}
}
// Apply animation |type| to all containers described by |group|.
void StartAnimation(PowerButtonController::ContainerGroup group,
PowerButtonController::AnimationType type) {
aura::Window::Windows containers;
GetContainers(group, &containers);
for (aura::Window::Windows::const_iterator it = containers.begin();
it != containers.end(); ++it) {
aura::Window* window = *it;
switch (type) {
case PowerButtonController::ANIMATION_SLOW_CLOSE:
StartSlowCloseAnimationForWindow(window);
break;
case PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE:
StartUndoSlowCloseAnimationForWindow(window);
break;
case PowerButtonController::ANIMATION_FAST_CLOSE:
StartFastCloseAnimationForWindow(window);
break;
case PowerButtonController::ANIMATION_FADE_IN:
FadeInWindow(window);
break;
case PowerButtonController::ANIMATION_HIDE:
HideWindow(window);
break;
case PowerButtonController::ANIMATION_RESTORE:
RestoreWindow(window);
break;
default:
NOTREACHED() << "Unhandled animation type " << type;
}
}
}
} // namespace
bool PowerButtonController::TestApi::ContainerGroupIsAnimated(
ContainerGroup group, AnimationType type) const {
aura::Window::Windows containers;
GetContainers(group, &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 PowerButtonController::ANIMATION_SLOW_CLOSE:
if (layer->GetTargetTransform() != GetSlowCloseTransform())
return false;
break;
case PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE:
if (layer->GetTargetTransform() != ui::Transform())
return false;
break;
case PowerButtonController::ANIMATION_FAST_CLOSE:
if (layer->GetTargetTransform() != GetFastCloseTransform() ||
layer->GetTargetOpacity() > 0.0001)
return false;
break;
case PowerButtonController::ANIMATION_FADE_IN:
if (layer->GetTargetOpacity() < 0.9999)
return false;
break;
case PowerButtonController::ANIMATION_HIDE:
if (layer->GetTargetOpacity() > 0.0001)
return false;
break;
case PowerButtonController::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 PowerButtonController::TestApi::BackgroundLayerIsVisible() const {
return controller_->background_layer_.get() &&
controller_->background_layer_->visible();
}
// Simple class that fills |background_layer_| with black.
class PowerButtonController::BackgroundLayerDelegate
: public ui::LayerDelegate {
public:
BackgroundLayerDelegate() {}
virtual ~BackgroundLayerDelegate() {}
// ui::LayerDelegate implementation:
virtual void OnPaintLayer(gfx::Canvas* canvas) OVERRIDE {
canvas->FillRect(SK_ColorBLACK, aura::RootWindow::GetInstance()->bounds());
}
private:
DISALLOW_COPY_AND_ASSIGN(BackgroundLayerDelegate);
};
PowerButtonController::PowerButtonController()
: logged_in_as_non_guest_(false),
locked_(false),
power_button_down_(false),
lock_button_down_(false),
shutting_down_(false) {
}
PowerButtonController::~PowerButtonController() {
}
void PowerButtonController::OnLoginStateChange(bool logged_in, bool is_guest) {
logged_in_as_non_guest_ = logged_in && !is_guest;
}
void PowerButtonController::OnLockStateChange(bool locked) {
if (shutting_down_ || locked_ == locked)
return;
locked_ = locked;
if (locked) {
StartAnimation(SCREEN_LOCKER_CONTAINERS, ANIMATION_FADE_IN);
lock_timer_.Stop();
lock_fail_timer_.Stop();
if (power_button_down_) {
lock_to_shutdown_timer_.Stop();
lock_to_shutdown_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kLockToShutdownTimeoutMs),
this, &PowerButtonController::OnLockToShutdownTimeout);
}
} else {
StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
ANIMATION_RESTORE);
HideBackgroundLayer();
}
}
void PowerButtonController::OnStartingLock() {
if (shutting_down_ || locked_)
return;
StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
ANIMATION_FAST_CLOSE);
// Hide the screen locker containers so we can make them fade in later.
StartAnimation(SCREEN_LOCKER_CONTAINERS, ANIMATION_HIDE);
}
void PowerButtonController::OnPowerButtonEvent(
bool down, const base::TimeTicks& timestamp) {
power_button_down_ = down;
if (shutting_down_)
return;
if (down) {
// If we already have a pending request to lock the screen, wait.
if (lock_fail_timer_.IsRunning())
return;
if (logged_in_as_non_guest_ && !locked_) {
ShowBackgroundLayer();
StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
ANIMATION_SLOW_CLOSE);
lock_timer_.Stop();
lock_timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs),
this, &PowerButtonController::OnLockTimeout);
} else {
StartShutdownTimer();
}
} else { // Button is up.
if (lock_timer_.IsRunning() || shutdown_timer_.IsRunning())
StartAnimation(
locked_ ? SCREEN_LOCKER_AND_RELATED_CONTAINERS : ALL_CONTAINERS,
ANIMATION_UNDO_SLOW_CLOSE);
// Drop the background layer after the undo animation finishes.
if (lock_timer_.IsRunning() ||
(shutdown_timer_.IsRunning() && !logged_in_as_non_guest_)) {
hide_background_layer_timer_.Stop();
hide_background_layer_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs),
this, &PowerButtonController::HideBackgroundLayer);
}
lock_timer_.Stop();
shutdown_timer_.Stop();
lock_to_shutdown_timer_.Stop();
}
}
void PowerButtonController::OnLockButtonEvent(
bool down, const base::TimeTicks& timestamp) {
lock_button_down_ = down;
if (shutting_down_)
return;
NOTIMPLEMENTED();
}
void PowerButtonController::OnRootWindowResized(const gfx::Size& new_size) {
if (background_layer_.get())
background_layer_->SetBounds(gfx::Rect(new_size));
}
void PowerButtonController::OnLockTimeout() {
delegate_->RequestLockScreen();
lock_fail_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kLockFailTimeoutMs),
this, &PowerButtonController::OnLockFailTimeout);
}
void PowerButtonController::OnLockFailTimeout() {
DCHECK(!locked_);
LOG(ERROR) << "Screen lock request timed out";
StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
ANIMATION_RESTORE);
HideBackgroundLayer();
}
void PowerButtonController::OnLockToShutdownTimeout() {
DCHECK(locked_);
StartShutdownTimer();
}
void PowerButtonController::OnShutdownTimeout() {
DCHECK(!shutting_down_);
shutting_down_ = true;
aura::RootWindow::GetInstance()->ShowCursor(false);
StartAnimation(ALL_CONTAINERS, ANIMATION_FAST_CLOSE);
real_shutdown_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kFastCloseAnimMs),
this, &PowerButtonController::OnRealShutdownTimeout);
}
void PowerButtonController::OnRealShutdownTimeout() {
DCHECK(shutting_down_);
delegate_->RequestShutdown();
}
void PowerButtonController::StartShutdownTimer() {
ShowBackgroundLayer();
StartAnimation(ALL_CONTAINERS, ANIMATION_SLOW_CLOSE);
shutdown_timer_.Stop();
shutdown_timer_.Start(
FROM_HERE,
base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs),
this, &PowerButtonController::OnShutdownTimeout);
}
void PowerButtonController::ShowBackgroundLayer() {
if (hide_background_layer_timer_.IsRunning())
hide_background_layer_timer_.Stop();
if (!background_layer_.get()) {
background_layer_delegate_.reset(new BackgroundLayerDelegate);
background_layer_.reset(new ui::Layer(ui::Layer::LAYER_HAS_TEXTURE));
background_layer_->set_delegate(background_layer_delegate_.get());
ui::Layer* root_layer = aura::RootWindow::GetInstance()->layer();
background_layer_->SetBounds(root_layer->bounds());
root_layer->Add(background_layer_.get());
root_layer->StackAtBottom(background_layer_.get());
}
background_layer_->SetVisible(true);
}
void PowerButtonController::HideBackgroundLayer() {
background_layer_.reset();
}
} // namespace ash
|