Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(142)

Side by Side Diff: ash/wm/power_button_controller.cc

Issue 10914016: ash: Extract animator from PowerButtonController (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/wm/power_button_controller.h" 5 #include "ash/wm/power_button_controller.h"
6 6
7 #include "ash/ash_switches.h" 7 #include "ash/ash_switches.h"
8 #include "ash/shell.h" 8 #include "ash/shell.h"
9 #include "ash/shell_delegate.h" 9 #include "ash/shell_delegate.h"
10 #include "ash/shell_window_ids.h" 10 #include "ash/shell_window_ids.h"
11 #include "ash/wm/cursor_manager.h" 11 #include "ash/wm/cursor_manager.h"
12 #include "ash/wm/session_state_animator.h"
12 #include "base/command_line.h" 13 #include "base/command_line.h"
13 #include "base/logging.h" 14 #include "base/logging.h"
14 #include "base/time.h" 15 #include "base/time.h"
15 #include "third_party/skia/include/core/SkColor.h" 16 #include "third_party/skia/include/core/SkColor.h"
16 #include "ui/aura/env.h" 17 #include "ui/aura/env.h"
17 #include "ui/aura/root_window.h" 18 #include "ui/aura/root_window.h"
18 #include "ui/aura/shared/compound_event_filter.h" 19 #include "ui/aura/shared/compound_event_filter.h"
19 #include "ui/aura/window.h" 20 #include "ui/aura/window.h"
20 #include "ui/compositor/layer.h" 21 #include "ui/compositor/layer.h"
21 #include "ui/compositor/layer_animation_element.h" 22 #include "ui/compositor/layer_animation_element.h"
(...skipping 30 matching lines...) Expand all
52 53
53 // Amount of time taken to scale the snapshot of the screen back to its original 54 // Amount of time taken to scale the snapshot of the screen back to its original
54 // size when the button is released. 55 // size when the button is released.
55 const int kUndoSlowCloseAnimMs = 100; 56 const int kUndoSlowCloseAnimMs = 100;
56 57
57 // Amount of time taken to scale the snapshot down to a point in the center of 58 // Amount of time taken to scale the snapshot down to a point in the center of
58 // the screen once the screen has been locked or we've been notified that the 59 // the screen once the screen has been locked or we've been notified that the
59 // system is shutting down. 60 // system is shutting down.
60 const int kFastCloseAnimMs = 150; 61 const int kFastCloseAnimMs = 150;
61 62
62 // Amount of time taken to make the lock window fade in when the screen is
63 // locked.
64 const int kLockFadeInAnimMs = 500;
65
66 // Additional time (beyond kFastCloseAnimMs) to wait after starting the 63 // Additional time (beyond kFastCloseAnimMs) to wait after starting the
67 // fast-close shutdown animation before actually requesting shutdown, to give 64 // fast-close shutdown animation before actually requesting shutdown, to give
68 // the animation time to finish. 65 // the animation time to finish.
69 const int kShutdownRequestDelayMs = 50; 66 const int kShutdownRequestDelayMs = 50;
70 67
71 // Slightly-smaller size that we scale the screen down to for the pre-lock and
72 // pre-shutdown states.
73 const float kSlowCloseSizeRatio = 0.95f;
74
75 // Returns the transform that should be applied to containers for the slow-close
76 // animation.
77 ui::Transform GetSlowCloseTransform() {
78 gfx::Size root_size = Shell::GetPrimaryRootWindow()->bounds().size();
79 ui::Transform transform;
80 transform.SetScale(kSlowCloseSizeRatio, kSlowCloseSizeRatio);
81 transform.ConcatTranslate(
82 floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.width() + 0.5),
83 floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.height() + 0.5));
84 return transform;
85 }
86
87 // Returns the transform that should be applied to containers for the fast-close
88 // animation.
89 ui::Transform GetFastCloseTransform() {
90 gfx::Size root_size = Shell::GetPrimaryRootWindow()->bounds().size();
91 ui::Transform transform;
92 transform.SetScale(0.0, 0.0);
93 transform.ConcatTranslate(floor(0.5 * root_size.width() + 0.5),
94 floor(0.5 * root_size.height() + 0.5));
95 return transform;
96 }
97
98 // Slowly shrinks |window| to a slightly-smaller size.
99 void StartSlowCloseAnimationForWindow(aura::Window* window) {
100 ui::LayerAnimator* animator = window->layer()->GetAnimator();
101 animator->set_preemption_strategy(
102 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
103 animator->StartAnimation(
104 new ui::LayerAnimationSequence(
105 ui::LayerAnimationElement::CreateTransformElement(
106 GetSlowCloseTransform(),
107 base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs))));
108 }
109
110 // Quickly undoes the effects of the slow-close animation on |window|.
111 void StartUndoSlowCloseAnimationForWindow(aura::Window* window) {
112 ui::LayerAnimator* animator = window->layer()->GetAnimator();
113 animator->set_preemption_strategy(
114 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
115 animator->StartAnimation(
116 new ui::LayerAnimationSequence(
117 ui::LayerAnimationElement::CreateTransformElement(
118 ui::Transform(),
119 base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs))));
120 }
121
122 // Quickly shrinks |window| down to a point in the center of the screen and
123 // fades it out to 0 opacity.
124 void StartFastCloseAnimationForWindow(aura::Window* window) {
125 ui::LayerAnimator* animator = window->layer()->GetAnimator();
126 animator->set_preemption_strategy(
127 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
128 animator->StartAnimation(
129 new ui::LayerAnimationSequence(
130 ui::LayerAnimationElement::CreateTransformElement(
131 GetFastCloseTransform(),
132 base::TimeDelta::FromMilliseconds(kFastCloseAnimMs))));
133 animator->StartAnimation(
134 new ui::LayerAnimationSequence(
135 ui::LayerAnimationElement::CreateOpacityElement(
136 0.0, base::TimeDelta::FromMilliseconds(kFastCloseAnimMs))));
137 }
138
139 // Fades |window| in to full opacity.
140 void FadeInWindow(aura::Window* window) {
141 ui::LayerAnimator* animator = window->layer()->GetAnimator();
142 animator->set_preemption_strategy(
143 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
144 animator->StartAnimation(
145 new ui::LayerAnimationSequence(
146 ui::LayerAnimationElement::CreateOpacityElement(
147 1.0, base::TimeDelta::FromMilliseconds(kLockFadeInAnimMs))));
148 }
149
150 // Makes |window| fully transparent instantaneously.
151 void HideWindow(aura::Window* window) {
152 window->layer()->SetOpacity(0.0);
153 }
154
155 // Restores |window| to its original position and scale and full opacity
156 // instantaneously.
157 void RestoreWindow(aura::Window* window) {
158 window->layer()->SetTransform(ui::Transform());
159 window->layer()->SetOpacity(1.0);
160 }
161
162 // Fills |containers| with the containers described by |group|.
163 void GetContainers(PowerButtonController::ContainerGroup group,
164 aura::Window::Windows* containers) {
165 aura::RootWindow* root_window = Shell::GetPrimaryRootWindow();
166
167 aura::Window* non_lock_screen_containers = Shell::GetContainer(
168 root_window,
169 internal::kShellWindowId_NonLockScreenContainersContainer);
170 aura::Window* lock_screen_containers = Shell::GetContainer(
171 root_window,
172 internal::kShellWindowId_LockScreenContainersContainer);
173 aura::Window* lock_screen_related_containers = Shell::GetContainer(
174 root_window,
175 internal::kShellWindowId_LockScreenRelatedContainersContainer);
176
177 containers->clear();
178 switch (group) {
179 case PowerButtonController::ALL_CONTAINERS:
180 containers->push_back(non_lock_screen_containers);
181 containers->push_back(lock_screen_containers);
182 containers->push_back(lock_screen_related_containers);
183 break;
184 case PowerButtonController::SCREEN_LOCKER_CONTAINERS:
185 containers->push_back(lock_screen_containers);
186 break;
187 case PowerButtonController::SCREEN_LOCKER_AND_RELATED_CONTAINERS:
188 containers->push_back(lock_screen_containers);
189 containers->push_back(lock_screen_related_containers);
190 break;
191 case PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS:
192 containers->push_back(non_lock_screen_containers);
193 break;
194 default:
195 NOTREACHED() << "Unhandled container group " << group;
196 }
197 }
198
199 // Apply animation |type| to all containers described by |group|.
200 void StartAnimation(PowerButtonController::ContainerGroup group,
201 PowerButtonController::AnimationType type) {
202 aura::Window::Windows containers;
203 GetContainers(group, &containers);
204
205 for (aura::Window::Windows::const_iterator it = containers.begin();
206 it != containers.end(); ++it) {
207 aura::Window* window = *it;
208 switch (type) {
209 case PowerButtonController::ANIMATION_SLOW_CLOSE:
210 StartSlowCloseAnimationForWindow(window);
211 break;
212 case PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE:
213 StartUndoSlowCloseAnimationForWindow(window);
214 break;
215 case PowerButtonController::ANIMATION_FAST_CLOSE:
216 StartFastCloseAnimationForWindow(window);
217 break;
218 case PowerButtonController::ANIMATION_FADE_IN:
219 FadeInWindow(window);
220 break;
221 case PowerButtonController::ANIMATION_HIDE:
222 HideWindow(window);
223 break;
224 case PowerButtonController::ANIMATION_RESTORE:
225 RestoreWindow(window);
226 break;
227 default:
228 NOTREACHED() << "Unhandled animation type " << type;
229 }
230 }
231 }
232
233 } // namespace 68 } // namespace
234 69
235 bool PowerButtonController::TestApi::ContainerGroupIsAnimated(
236 ContainerGroup group, AnimationType type) const {
237 aura::Window::Windows containers;
238 GetContainers(group, &containers);
239 for (aura::Window::Windows::const_iterator it = containers.begin();
240 it != containers.end(); ++it) {
241 aura::Window* window = *it;
242 ui::Layer* layer = window->layer();
243
244 switch (type) {
245 case PowerButtonController::ANIMATION_SLOW_CLOSE:
246 if (layer->GetTargetTransform() != GetSlowCloseTransform())
247 return false;
248 break;
249 case PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE:
250 if (layer->GetTargetTransform() != ui::Transform())
251 return false;
252 break;
253 case PowerButtonController::ANIMATION_FAST_CLOSE:
254 if (layer->GetTargetTransform() != GetFastCloseTransform() ||
255 layer->GetTargetOpacity() > 0.0001)
256 return false;
257 break;
258 case PowerButtonController::ANIMATION_FADE_IN:
259 if (layer->GetTargetOpacity() < 0.9999)
260 return false;
261 break;
262 case PowerButtonController::ANIMATION_HIDE:
263 if (layer->GetTargetOpacity() > 0.0001)
264 return false;
265 break;
266 case PowerButtonController::ANIMATION_RESTORE:
267 if (layer->opacity() < 0.9999 || layer->transform() != ui::Transform())
268 return false;
269 break;
270 default:
271 NOTREACHED() << "Unhandled animation type " << type;
272 return false;
273 }
274 }
275 return true;
276 }
277
278 bool PowerButtonController::TestApi::BackgroundLayerIsVisible() const {
279 return controller_->background_layer_.get() &&
280 controller_->background_layer_->visible();
281 }
282
283 gfx::Rect PowerButtonController::TestApi::GetBackgroundLayerBounds() const {
284 ui::Layer* layer = controller_->background_layer_.get();
285 return layer ? layer->bounds() : gfx::Rect();
286 }
287
288 PowerButtonController::PowerButtonController() 70 PowerButtonController::PowerButtonController()
289 : login_status_(user::LOGGED_IN_NONE), 71 : login_status_(user::LOGGED_IN_NONE),
290 unlocked_login_status_(user::LOGGED_IN_NONE), 72 unlocked_login_status_(user::LOGGED_IN_NONE),
291 power_button_down_(false), 73 power_button_down_(false),
292 lock_button_down_(false), 74 lock_button_down_(false),
293 screen_is_off_(false), 75 screen_is_off_(false),
294 shutting_down_(false), 76 shutting_down_(false),
295 has_legacy_power_button_( 77 has_legacy_power_button_(
296 CommandLine::ForCurrentProcess()->HasSwitch( 78 CommandLine::ForCurrentProcess()->HasSwitch(
297 switches::kAuraLegacyPowerButton)) { 79 switches::kAuraLegacyPowerButton)),
80 animator_(new SessionStateAnimator()) {
298 Shell::GetPrimaryRootWindow()->AddRootWindowObserver(this); 81 Shell::GetPrimaryRootWindow()->AddRootWindowObserver(this);
299 } 82 }
300 83
301 PowerButtonController::~PowerButtonController() { 84 PowerButtonController::~PowerButtonController() {
302 Shell::GetPrimaryRootWindow()->RemoveRootWindowObserver(this); 85 Shell::GetPrimaryRootWindow()->RemoveRootWindowObserver(this);
86 delete animator_;
303 } 87 }
304 88
305 void PowerButtonController::OnLoginStateChanged(user::LoginStatus status) { 89 void PowerButtonController::OnLoginStateChanged(user::LoginStatus status) {
306 login_status_ = status; 90 login_status_ = status;
307 unlocked_login_status_ = user::LOGGED_IN_NONE; 91 unlocked_login_status_ = user::LOGGED_IN_NONE;
308 } 92 }
309 93
310 void PowerButtonController::OnAppTerminating() { 94 void PowerButtonController::OnAppTerminating() {
311 // If we hear that Chrome is exiting but didn't request it ourselves, all we 95 // If we hear that Chrome is exiting but didn't request it ourselves, all we
312 // can really hope for is that we'll have time to clear the screen. 96 // can really hope for is that we'll have time to clear the screen.
313 if (!shutting_down_) { 97 if (!shutting_down_) {
314 shutting_down_ = true; 98 shutting_down_ = true;
315 Shell* shell = ash::Shell::GetInstance(); 99 Shell* shell = ash::Shell::GetInstance();
316 shell->env_filter()->set_update_cursor_visibility(false); 100 shell->env_filter()->set_update_cursor_visibility(false);
317 shell->cursor_manager()->ShowCursor(false); 101 shell->cursor_manager()->ShowCursor(false);
318 ShowBackgroundLayer(); 102 animator_->ShowBackgroundLayer();
319 StartAnimation(ALL_CONTAINERS, ANIMATION_HIDE); 103 animator_->StartAnimation(ALL_CONTAINERS, ANIMATION_HIDE);
320 } 104 }
321 } 105 }
322 106
323 void PowerButtonController::OnLockStateChanged(bool locked) { 107 void PowerButtonController::OnLockStateChanged(bool locked) {
324 if (shutting_down_ || (login_status_ == user::LOGGED_IN_LOCKED) == locked) 108 if (shutting_down_ || (login_status_ == user::LOGGED_IN_LOCKED) == locked)
325 return; 109 return;
326 110
327 if (!locked && login_status_ == user::LOGGED_IN_LOCKED) { 111 if (!locked && login_status_ == user::LOGGED_IN_LOCKED) {
328 login_status_ = unlocked_login_status_; 112 login_status_ = unlocked_login_status_;
329 unlocked_login_status_ = user::LOGGED_IN_NONE; 113 unlocked_login_status_ = user::LOGGED_IN_NONE;
330 } else { 114 } else {
331 unlocked_login_status_ = login_status_; 115 unlocked_login_status_ = login_status_;
332 login_status_ = user::LOGGED_IN_LOCKED; 116 login_status_ = user::LOGGED_IN_LOCKED;
333 } 117 }
334 118
335 if (locked) { 119 if (locked) {
336 StartAnimation(SCREEN_LOCKER_CONTAINERS, ANIMATION_FADE_IN); 120 animator_->StartAnimation(SCREEN_LOCKER_CONTAINERS, ANIMATION_FADE_IN);
337 lock_timer_.Stop(); 121 lock_timer_.Stop();
338 lock_fail_timer_.Stop(); 122 lock_fail_timer_.Stop();
339 123
340 if (!has_legacy_power_button_ && power_button_down_) { 124 if (!has_legacy_power_button_ && power_button_down_) {
341 lock_to_shutdown_timer_.Stop(); 125 lock_to_shutdown_timer_.Stop();
342 lock_to_shutdown_timer_.Start( 126 lock_to_shutdown_timer_.Start(
343 FROM_HERE, 127 FROM_HERE,
344 base::TimeDelta::FromMilliseconds(kLockToShutdownTimeoutMs), 128 base::TimeDelta::FromMilliseconds(kLockToShutdownTimeoutMs),
345 this, &PowerButtonController::OnLockToShutdownTimeout); 129 this, &PowerButtonController::OnLockToShutdownTimeout);
346 } 130 }
347 } else { 131 } else {
348 StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, 132 animator_->StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
349 ANIMATION_RESTORE); 133 ANIMATION_RESTORE);
350 HideBackgroundLayer(); 134 animator_->HideBackgroundLayer();
351 } 135 }
352 } 136 }
353 137
354 void PowerButtonController::OnScreenBrightnessChanged(double percent) { 138 void PowerButtonController::OnScreenBrightnessChanged(double percent) {
355 screen_is_off_ = percent <= 0.001; 139 screen_is_off_ = percent <= 0.001;
356 } 140 }
357 141
358 void PowerButtonController::OnStartingLock() { 142 void PowerButtonController::OnStartingLock() {
359 if (shutting_down_ || login_status_ == user::LOGGED_IN_LOCKED) 143 if (shutting_down_ || login_status_ == user::LOGGED_IN_LOCKED)
360 return; 144 return;
361 145
362 // Ensure that the background layer is visible -- if the screen was locked via 146 // Ensure that the background layer is visible -- if the screen was locked via
363 // the wrench menu, we won't have already shown the background as part of the 147 // the wrench menu, we won't have already shown the background as part of the
364 // slow-close animation. 148 // slow-close animation.
365 ShowBackgroundLayer(); 149 animator_->ShowBackgroundLayer();
366 150
367 StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, 151 animator_->StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
368 ANIMATION_FAST_CLOSE); 152 ANIMATION_FAST_CLOSE);
369 153
370 // Hide the screen locker containers so we can make them fade in later. 154 // Hide the screen locker containers so we can make them fade in later.
371 StartAnimation(SCREEN_LOCKER_CONTAINERS, ANIMATION_HIDE); 155 animator_->StartAnimation(SCREEN_LOCKER_CONTAINERS, ANIMATION_HIDE);
372 } 156 }
373 157
374 void PowerButtonController::OnPowerButtonEvent( 158 void PowerButtonController::OnPowerButtonEvent(
375 bool down, const base::TimeTicks& timestamp) { 159 bool down, const base::TimeTicks& timestamp) {
376 power_button_down_ = down; 160 power_button_down_ = down;
377 161
378 if (shutting_down_) 162 if (shutting_down_)
379 return; 163 return;
380 164
381 // Avoid starting the lock/shutdown sequence if the power button is pressed 165 // Avoid starting the lock/shutdown sequence if the power button is pressed
382 // while the screen is off (http://crbug.com/128451). 166 // while the screen is off (http://crbug.com/128451).
383 if (screen_is_off_) 167 if (screen_is_off_)
384 return; 168 return;
385 169
386 if (has_legacy_power_button_) { 170 if (has_legacy_power_button_) {
387 // If power button releases won't get reported correctly because we're not 171 // If power button releases won't get reported correctly because we're not
388 // running on official hardware, just lock the screen or shut down 172 // running on official hardware, just lock the screen or shut down
389 // immediately. 173 // immediately.
390 if (down) { 174 if (down) {
391 ShowBackgroundLayer(); 175 animator_->ShowBackgroundLayer();
392 if (LoggedInAsNonGuest() && login_status_ != user::LOGGED_IN_LOCKED) { 176 if (LoggedInAsNonGuest() && login_status_ != user::LOGGED_IN_LOCKED) {
393 StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, 177 animator_->StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
394 ANIMATION_SLOW_CLOSE); 178 ANIMATION_SLOW_CLOSE);
395 OnLockTimeout(); 179 OnLockTimeout();
396 } else { 180 } else {
397 OnShutdownTimeout(); 181 OnShutdownTimeout();
398 } 182 }
399 } 183 }
400 } else { // !has_legacy_power_button_ 184 } else { // !has_legacy_power_button_
401 if (down) { 185 if (down) {
402 // If we already have a pending request to lock the screen, wait. 186 // If we already have a pending request to lock the screen, wait.
403 if (lock_fail_timer_.IsRunning()) 187 if (lock_fail_timer_.IsRunning())
404 return; 188 return;
405 189
406 if (LoggedInAsNonGuest() && login_status_ != user::LOGGED_IN_LOCKED) 190 if (LoggedInAsNonGuest() && login_status_ != user::LOGGED_IN_LOCKED)
407 StartLockTimer(); 191 StartLockTimer();
408 else 192 else
409 StartShutdownTimer(); 193 StartShutdownTimer();
410 } else { // Button is up. 194 } else { // Button is up.
411 if (lock_timer_.IsRunning() || shutdown_timer_.IsRunning()) 195 if (lock_timer_.IsRunning() || shutdown_timer_.IsRunning())
412 StartAnimation( 196 animator_->StartAnimation(
413 (login_status_ == user::LOGGED_IN_LOCKED) ? 197 (login_status_ == user::LOGGED_IN_LOCKED) ?
414 SCREEN_LOCKER_AND_RELATED_CONTAINERS : ALL_CONTAINERS, 198 SCREEN_LOCKER_AND_RELATED_CONTAINERS : ALL_CONTAINERS,
415 ANIMATION_UNDO_SLOW_CLOSE); 199 ANIMATION_UNDO_SLOW_CLOSE);
416 200
417 // Drop the background layer after the undo animation finishes. 201 // Drop the background layer after the undo animation finishes.
418 if (lock_timer_.IsRunning() || 202 if (lock_timer_.IsRunning() ||
419 (shutdown_timer_.IsRunning() && !LoggedInAsNonGuest())) { 203 (shutdown_timer_.IsRunning() && !LoggedInAsNonGuest())) {
420 hide_background_layer_timer_.Stop(); 204 animator_->DropBackgroundLayer();
421 hide_background_layer_timer_.Start(
422 FROM_HERE,
423 base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs),
424 this, &PowerButtonController::HideBackgroundLayer);
425 } 205 }
426 206
427 lock_timer_.Stop(); 207 lock_timer_.Stop();
428 shutdown_timer_.Stop(); 208 shutdown_timer_.Stop();
429 lock_to_shutdown_timer_.Stop(); 209 lock_to_shutdown_timer_.Stop();
430 } 210 }
431 } 211 }
432 } 212 }
433 213
434 void PowerButtonController::OnLockButtonEvent( 214 void PowerButtonController::OnLockButtonEvent(
435 bool down, const base::TimeTicks& timestamp) { 215 bool down, const base::TimeTicks& timestamp) {
436 lock_button_down_ = down; 216 lock_button_down_ = down;
437 217
438 if (shutting_down_ || !LoggedInAsNonGuest()) 218 if (shutting_down_ || !LoggedInAsNonGuest())
439 return; 219 return;
440 220
441 // Bail if we're already locked or are in the process of locking. Also give 221 // Bail if we're already locked or are in the process of locking. Also give
442 // the power button precedence over the lock button (we don't expect both 222 // the power button precedence over the lock button (we don't expect both
443 // buttons to be present, so this is just making sure that we don't do 223 // buttons to be present, so this is just making sure that we don't do
444 // something completely stupid if that assumption changes later). 224 // something completely stupid if that assumption changes later).
445 if (login_status_ == user::LOGGED_IN_LOCKED || 225 if (login_status_ == user::LOGGED_IN_LOCKED ||
446 lock_fail_timer_.IsRunning() || power_button_down_) 226 lock_fail_timer_.IsRunning() || power_button_down_)
447 return; 227 return;
448 228
449 if (down) { 229 if (down) {
450 StartLockTimer(); 230 StartLockTimer();
451 } else { 231 } else {
452 if (lock_timer_.IsRunning()) { 232 if (lock_timer_.IsRunning()) {
453 StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, 233 animator_->StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
454 ANIMATION_UNDO_SLOW_CLOSE); 234 ANIMATION_UNDO_SLOW_CLOSE);
455 hide_background_layer_timer_.Stop(); 235 animator_->DropBackgroundLayer();
456 hide_background_layer_timer_.Start(
457 FROM_HERE,
458 base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs),
459 this, &PowerButtonController::HideBackgroundLayer);
460 lock_timer_.Stop(); 236 lock_timer_.Stop();
461 } 237 }
462 } 238 }
463 } 239 }
464 240
465 void PowerButtonController::RequestShutdown() { 241 void PowerButtonController::RequestShutdown() {
466 if (!shutting_down_) 242 if (!shutting_down_)
467 StartShutdownAnimationAndRequestShutdown(); 243 StartShutdownAnimationAndRequestShutdown();
468 } 244 }
469 245
470 void PowerButtonController::OnRootWindowResized(const aura::RootWindow* root,
471 const gfx::Size& new_size) {
472 if (background_layer_.get())
473 background_layer_->SetBounds(gfx::Rect(root->bounds().size()));
474 }
475
476 void PowerButtonController::OnRootWindowHostCloseRequested( 246 void PowerButtonController::OnRootWindowHostCloseRequested(
477 const aura::RootWindow*) { 247 const aura::RootWindow*) {
478 if(Shell::GetInstance() && Shell::GetInstance()->delegate()) 248 if(Shell::GetInstance() && Shell::GetInstance()->delegate())
479 Shell::GetInstance()->delegate()->Exit(); 249 Shell::GetInstance()->delegate()->Exit();
480 } 250 }
481 251
482 bool PowerButtonController::LoggedInAsNonGuest() const { 252 bool PowerButtonController::LoggedInAsNonGuest() const {
483 if (login_status_ == user::LOGGED_IN_NONE) 253 if (login_status_ == user::LOGGED_IN_NONE)
484 return false; 254 return false;
485 if (login_status_ == user::LOGGED_IN_GUEST) 255 if (login_status_ == user::LOGGED_IN_GUEST)
486 return false; 256 return false;
487 // TODO(mukai): think about kiosk mode. 257 // TODO(mukai): think about kiosk mode.
488 return true; 258 return true;
489 } 259 }
490 260
491 void PowerButtonController::OnLockTimeout() { 261 void PowerButtonController::OnLockTimeout() {
492 delegate_->RequestLockScreen(); 262 delegate_->RequestLockScreen();
493 lock_fail_timer_.Start( 263 lock_fail_timer_.Start(
494 FROM_HERE, 264 FROM_HERE,
495 base::TimeDelta::FromMilliseconds(kLockFailTimeoutMs), 265 base::TimeDelta::FromMilliseconds(kLockFailTimeoutMs),
496 this, &PowerButtonController::OnLockFailTimeout); 266 this, &PowerButtonController::OnLockFailTimeout);
497 } 267 }
498 268
499 void PowerButtonController::OnLockFailTimeout() { 269 void PowerButtonController::OnLockFailTimeout() {
500 DCHECK_NE(login_status_, user::LOGGED_IN_LOCKED); 270 DCHECK_NE(login_status_, user::LOGGED_IN_LOCKED);
501 LOG(ERROR) << "Screen lock request timed out"; 271 LOG(ERROR) << "Screen lock request timed out";
502 StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, 272 animator_->StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
503 ANIMATION_RESTORE); 273 ANIMATION_RESTORE);
504 HideBackgroundLayer(); 274 animator_->HideBackgroundLayer();
505 } 275 }
506 276
507 void PowerButtonController::OnLockToShutdownTimeout() { 277 void PowerButtonController::OnLockToShutdownTimeout() {
508 DCHECK_EQ(login_status_, user::LOGGED_IN_LOCKED); 278 DCHECK_EQ(login_status_, user::LOGGED_IN_LOCKED);
509 StartShutdownTimer(); 279 StartShutdownTimer();
510 } 280 }
511 281
512 void PowerButtonController::OnShutdownTimeout() { 282 void PowerButtonController::OnShutdownTimeout() {
513 if (!shutting_down_) 283 if (!shutting_down_)
514 StartShutdownAnimationAndRequestShutdown(); 284 StartShutdownAnimationAndRequestShutdown();
515 } 285 }
516 286
517 void PowerButtonController::OnRealShutdownTimeout() { 287 void PowerButtonController::OnRealShutdownTimeout() {
518 DCHECK(shutting_down_); 288 DCHECK(shutting_down_);
519 delegate_->RequestShutdown(); 289 delegate_->RequestShutdown();
520 } 290 }
521 291
522 void PowerButtonController::StartLockTimer() { 292 void PowerButtonController::StartLockTimer() {
523 ShowBackgroundLayer(); 293 animator_->ShowBackgroundLayer();
524 StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, 294 animator_->StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
525 ANIMATION_SLOW_CLOSE); 295 ANIMATION_SLOW_CLOSE);
526 lock_timer_.Stop(); 296 lock_timer_.Stop();
527 lock_timer_.Start(FROM_HERE, 297 lock_timer_.Start(FROM_HERE,
528 base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs), 298 base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs),
529 this, &PowerButtonController::OnLockTimeout); 299 this, &PowerButtonController::OnLockTimeout);
530 } 300 }
531 301
532 void PowerButtonController::StartShutdownTimer() { 302 void PowerButtonController::StartShutdownTimer() {
533 ShowBackgroundLayer(); 303 animator_->ShowBackgroundLayer();
534 StartAnimation(ALL_CONTAINERS, ANIMATION_SLOW_CLOSE); 304 animator_->StartAnimation(ALL_CONTAINERS, ANIMATION_SLOW_CLOSE);
535 shutdown_timer_.Stop(); 305 shutdown_timer_.Stop();
536 shutdown_timer_.Start( 306 shutdown_timer_.Start(
537 FROM_HERE, 307 FROM_HERE,
538 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs), 308 base::TimeDelta::FromMilliseconds(kShutdownTimeoutMs),
539 this, &PowerButtonController::OnShutdownTimeout); 309 this, &PowerButtonController::OnShutdownTimeout);
540 } 310 }
541 311
542 void PowerButtonController::StartShutdownAnimationAndRequestShutdown() { 312 void PowerButtonController::StartShutdownAnimationAndRequestShutdown() {
543 DCHECK(!shutting_down_); 313 DCHECK(!shutting_down_);
544 shutting_down_ = true; 314 shutting_down_ = true;
545 315
546 Shell* shell = ash::Shell::GetInstance(); 316 Shell* shell = ash::Shell::GetInstance();
547 shell->env_filter()->set_update_cursor_visibility(false); 317 shell->env_filter()->set_update_cursor_visibility(false);
548 shell->cursor_manager()->ShowCursor(false); 318 shell->cursor_manager()->ShowCursor(false);
549 319
550 ShowBackgroundLayer(); 320 animator_->ShowBackgroundLayer();
551 if (login_status_ != user::LOGGED_IN_NONE) { 321 if (login_status_ != user::LOGGED_IN_NONE) {
552 // Hide the other containers before starting the animation. 322 // Hide the other containers before starting the animation.
553 // ANIMATION_FAST_CLOSE will make the screen locker windows partially 323 // ANIMATION_FAST_CLOSE will make the screen locker windows partially
554 // transparent, and we don't want the other windows to show through. 324 // transparent, and we don't want the other windows to show through.
555 StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, 325 animator_->StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
556 ANIMATION_HIDE); 326 ANIMATION_HIDE);
557 StartAnimation(SCREEN_LOCKER_AND_RELATED_CONTAINERS, ANIMATION_FAST_CLOSE); 327 animator_->StartAnimation(SCREEN_LOCKER_AND_RELATED_CONTAINERS,
328 ANIMATION_FAST_CLOSE);
558 } else { 329 } else {
559 StartAnimation(ALL_CONTAINERS, ANIMATION_FAST_CLOSE); 330 animator_->StartAnimation(ALL_CONTAINERS, ANIMATION_FAST_CLOSE);
560 } 331 }
561 332
562 real_shutdown_timer_.Start( 333 real_shutdown_timer_.Start(
563 FROM_HERE, 334 FROM_HERE,
564 base::TimeDelta::FromMilliseconds( 335 base::TimeDelta::FromMilliseconds(
565 kFastCloseAnimMs + kShutdownRequestDelayMs), 336 kFastCloseAnimMs + kShutdownRequestDelayMs),
566 this, &PowerButtonController::OnRealShutdownTimeout); 337 this, &PowerButtonController::OnRealShutdownTimeout);
567 } 338 }
568 339
569 void PowerButtonController::ShowBackgroundLayer() {
570 if (hide_background_layer_timer_.IsRunning())
571 hide_background_layer_timer_.Stop();
572
573 if (!background_layer_.get()) {
574 background_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR));
575 background_layer_->SetColor(SK_ColorBLACK);
576
577 ui::Layer* root_layer = Shell::GetPrimaryRootWindow()->layer();
578 background_layer_->SetBounds(root_layer->bounds());
579 root_layer->Add(background_layer_.get());
580 root_layer->StackAtBottom(background_layer_.get());
581 }
582 background_layer_->SetVisible(true);
583 }
584
585 void PowerButtonController::HideBackgroundLayer() {
586 background_layer_.reset();
587 }
588
589 } // namespace ash 340 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698