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

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

Issue 9348089: Make power button controller restore to original transformation on the layer. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: update Created 8 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « ash/wm/power_button_controller.h ('k') | ash/wm/power_button_controller_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_window_ids.h" 9 #include "ash/shell_window_ids.h"
10 #include "ash/wm/root_window_event_filter.h" 10 #include "ash/wm/root_window_event_filter.h"
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 89
90 // Is |window| a container that holds other windows that should be shown while 90 // Is |window| a container that holds other windows that should be shown while
91 // the screen is locked? 91 // the screen is locked?
92 bool IsRelatedContainer(aura::Window* window) { 92 bool IsRelatedContainer(aura::Window* window) {
93 for (size_t i = 0; i < arraysize(kRelatedContainerIds); ++i) 93 for (size_t i = 0; i < arraysize(kRelatedContainerIds); ++i)
94 if (window->id() == kRelatedContainerIds[i]) 94 if (window->id() == kRelatedContainerIds[i])
95 return true; 95 return true;
96 return false; 96 return false;
97 } 97 }
98 98
99 // Returns the transform that should be applied to containers for the slow-close 99 // Returns the transform, based on |base_transform|, that should be applied
100 // animation. 100 // to containers for slow-close animation.
101 ui::Transform GetSlowCloseTransform() { 101 ui::Transform GetSlowCloseTransform(const ui::Transform& base_transform) {
102 gfx::Size root_size = aura::RootWindow::GetInstance()->bounds().size(); 102 gfx::Size root_size = aura::RootWindow::GetInstance()->bounds().size();
103 ui::Transform transform; 103 ui::Transform transform(base_transform);
104 transform.SetScale(kSlowCloseSizeRatio, kSlowCloseSizeRatio); 104 transform.ConcatScale(kSlowCloseSizeRatio, kSlowCloseSizeRatio);
105 transform.ConcatTranslate( 105 transform.ConcatTranslate(
106 floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.width() + 0.5), 106 floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.width() + 0.5),
107 floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.height() + 0.5)); 107 floor(0.5 * (1.0 - kSlowCloseSizeRatio) * root_size.height() + 0.5));
108 return transform; 108 return transform;
109 } 109 }
110 110
111 // Returns the transform that should be applied to containers for the fast-close 111 // Returns the transform, based on |base_transform|, that should be applied
112 // animation. 112 // to containers for fast-close animation.
113 ui::Transform GetFastCloseTransform() { 113 ui::Transform GetFastCloseTransform(const ui::Transform& base_transform) {
114 gfx::Size root_size = aura::RootWindow::GetInstance()->bounds().size(); 114 gfx::Size root_size = aura::RootWindow::GetInstance()->bounds().size();
115 ui::Transform transform; 115 ui::Transform transform(base_transform);
116 transform.SetScale(0.0, 0.0); 116 transform.ConcatScale(0.0, 0.0);
117 transform.ConcatTranslate(floor(0.5 * root_size.width() + 0.5), 117 transform.ConcatTranslate(floor(0.5 * root_size.width() + 0.5),
118 floor(0.5 * root_size.height() + 0.5)); 118 floor(0.5 * root_size.height() + 0.5));
119 return transform; 119 return transform;
120 } 120 }
121 121
122 // Slowly shrinks |window| to a slightly-smaller size. 122 // Slowly shrinks |window| to a slightly-smaller size.
123 void StartSlowCloseAnimationForWindow(aura::Window* window) { 123 void StartSlowCloseAnimationForWindow(aura::Window* window) {
124 ui::LayerAnimator* animator = window->layer()->GetAnimator(); 124 ui::LayerAnimator* animator = window->layer()->GetAnimator();
125 animator->set_preemption_strategy( 125 animator->set_preemption_strategy(
126 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 126 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
127 animator->StartAnimation( 127 animator->StartAnimation(
128 new ui::LayerAnimationSequence( 128 new ui::LayerAnimationSequence(
129 ui::LayerAnimationElement::CreateTransformElement( 129 ui::LayerAnimationElement::CreateTransformElement(
130 GetSlowCloseTransform(), 130 GetSlowCloseTransform(window->layer()->GetTargetTransform()),
131 base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs)))); 131 base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs))));
132 } 132 }
133 133
134 // Quickly undoes the effects of the slow-close animation on |window|. 134 // Quickly undoes the effects of the slow-close animation on |window|.
135 void StartUndoSlowCloseAnimationForWindow(aura::Window* window) { 135 void StartUndoSlowCloseAnimationForWindow(aura::Window* window,
136 const ui::Transform& orig_transform) {
136 ui::LayerAnimator* animator = window->layer()->GetAnimator(); 137 ui::LayerAnimator* animator = window->layer()->GetAnimator();
137 animator->set_preemption_strategy( 138 animator->set_preemption_strategy(
138 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 139 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
139 animator->StartAnimation( 140 animator->StartAnimation(
140 new ui::LayerAnimationSequence( 141 new ui::LayerAnimationSequence(
141 ui::LayerAnimationElement::CreateTransformElement( 142 ui::LayerAnimationElement::CreateTransformElement(
142 ui::Transform(), 143 orig_transform,
143 base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs)))); 144 base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs))));
144 } 145 }
145 146
146 // Quickly shrinks |window| down to a point in the center of the screen and 147 // Quickly shrinks |window| down to a point in the center of the screen and
147 // fades it out to 0 opacity. 148 // fades it out to 0 opacity.
148 void StartFastCloseAnimationForWindow(aura::Window* window) { 149 void StartFastCloseAnimationForWindow(aura::Window* window) {
149 ui::LayerAnimator* animator = window->layer()->GetAnimator(); 150 ui::LayerAnimator* animator = window->layer()->GetAnimator();
150 animator->set_preemption_strategy( 151 animator->set_preemption_strategy(
151 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 152 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
152 animator->StartAnimation( 153 animator->StartAnimation(
153 new ui::LayerAnimationSequence( 154 new ui::LayerAnimationSequence(
154 ui::LayerAnimationElement::CreateTransformElement( 155 ui::LayerAnimationElement::CreateTransformElement(
155 GetFastCloseTransform(), 156 GetFastCloseTransform(window->layer()->GetTargetTransform()),
156 base::TimeDelta::FromMilliseconds(kFastCloseAnimMs)))); 157 base::TimeDelta::FromMilliseconds(kFastCloseAnimMs))));
157 animator->StartAnimation( 158 animator->StartAnimation(
158 new ui::LayerAnimationSequence( 159 new ui::LayerAnimationSequence(
159 ui::LayerAnimationElement::CreateOpacityElement( 160 ui::LayerAnimationElement::CreateOpacityElement(
160 0.0, base::TimeDelta::FromMilliseconds(kFastCloseAnimMs)))); 161 0.0, base::TimeDelta::FromMilliseconds(kFastCloseAnimMs))));
161 } 162 }
162 163
163 // Fades |window| in to full opacity. 164 // Fades |window| in to full opacity.
164 void FadeInWindow(aura::Window* window) { 165 void FadeInWindow(aura::Window* window) {
165 ui::LayerAnimator* animator = window->layer()->GetAnimator(); 166 ui::LayerAnimator* animator = window->layer()->GetAnimator();
166 animator->set_preemption_strategy( 167 animator->set_preemption_strategy(
167 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 168 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
168 animator->StartAnimation( 169 animator->StartAnimation(
169 new ui::LayerAnimationSequence( 170 new ui::LayerAnimationSequence(
170 ui::LayerAnimationElement::CreateOpacityElement( 171 ui::LayerAnimationElement::CreateOpacityElement(
171 1.0, base::TimeDelta::FromMilliseconds(kLockFadeInAnimMs)))); 172 1.0, base::TimeDelta::FromMilliseconds(kLockFadeInAnimMs))));
172 } 173 }
173 174
174 // Makes |window| fully transparent instantaneously. 175 // Makes |window| fully transparent instantaneously.
175 void HideWindow(aura::Window* window) { 176 void HideWindow(aura::Window* window) {
176 window->layer()->SetOpacity(0.0); 177 window->layer()->SetOpacity(0.0);
177 } 178 }
178 179
179 // Restores |window| to its original position and scale and full opacity 180 // Restores |window| to its original position and scale and full opacity
180 // instantaneously. 181 // instantaneously.
181 void RestoreWindow(aura::Window* window) { 182 void RestoreWindow(aura::Window* window,
182 window->layer()->SetTransform(ui::Transform()); 183 const ui::Transform& orig_transform) {
184 window->layer()->SetTransform(orig_transform);
183 window->layer()->SetOpacity(1.0); 185 window->layer()->SetOpacity(1.0);
184 } 186 }
185 187
186 // Fills |containers| with the containers described by |group|.
187 void GetContainers(PowerButtonController::ContainerGroup group,
188 aura::Window::Windows* containers) {
189 containers->clear();
190
191 aura::Window* root = aura::RootWindow::GetInstance();
192 for (aura::Window::Windows::const_iterator it = root->children().begin();
193 it != root->children().end(); ++it) {
194 aura::Window* window = *it;
195
196 bool matched = true;
197 if (group != PowerButtonController::ALL_CONTAINERS) {
198 bool is_screen_locker = IsScreenLockerContainer(window);
199 bool is_related = IsRelatedContainer(window);
200
201 switch (group) {
202 case PowerButtonController::SCREEN_LOCKER_CONTAINERS:
203 matched = is_screen_locker;
204 break;
205 case PowerButtonController::SCREEN_LOCKER_AND_RELATED_CONTAINERS:
206 matched = is_screen_locker || is_related;
207 break;
208 case PowerButtonController::
209 ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS:
210 matched = !is_screen_locker && !is_related;
211 break;
212 default:
213 NOTREACHED() << "Unhandled container group " << group;
214 }
215 }
216
217 if (matched)
218 containers->push_back(window);
219 }
220 }
221
222 // Apply animation |type| to all containers described by |group|.
223 void StartAnimation(PowerButtonController::ContainerGroup group,
224 PowerButtonController::AnimationType type) {
225 aura::Window::Windows containers;
226 GetContainers(group, &containers);
227
228 for (aura::Window::Windows::const_iterator it = containers.begin();
229 it != containers.end(); ++it) {
230 aura::Window* window = *it;
231 switch (type) {
232 case PowerButtonController::ANIMATION_SLOW_CLOSE:
233 StartSlowCloseAnimationForWindow(window);
234 break;
235 case PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE:
236 StartUndoSlowCloseAnimationForWindow(window);
237 break;
238 case PowerButtonController::ANIMATION_FAST_CLOSE:
239 StartFastCloseAnimationForWindow(window);
240 break;
241 case PowerButtonController::ANIMATION_FADE_IN:
242 FadeInWindow(window);
243 break;
244 case PowerButtonController::ANIMATION_HIDE:
245 HideWindow(window);
246 break;
247 case PowerButtonController::ANIMATION_RESTORE:
248 RestoreWindow(window);
249 break;
250 default:
251 NOTREACHED() << "Unhandled animation type " << type;
252 }
253 }
254 }
255
256 } // namespace 188 } // namespace
257 189
258 bool PowerButtonController::TestApi::ContainerGroupIsAnimated( 190 bool PowerButtonController::TestApi::ContainerGroupIsAnimated(
259 ContainerGroup group, AnimationType type) const { 191 ContainerGroup group, AnimationType type) const {
260 aura::Window::Windows containers; 192 aura::Window::Windows containers;
261 GetContainers(group, &containers); 193 controller_->GetContainers(group, &containers);
262 for (aura::Window::Windows::const_iterator it = containers.begin(); 194 for (aura::Window::Windows::const_iterator it = containers.begin();
263 it != containers.end(); ++it) { 195 it != containers.end(); ++it) {
264 aura::Window* window = *it; 196 aura::Window* window = *it;
265 ui::Layer* layer = window->layer(); 197 ui::Layer* layer = window->layer();
266 198
267 switch (type) { 199 switch (type) {
268 case PowerButtonController::ANIMATION_SLOW_CLOSE: 200 case PowerButtonController::ANIMATION_SLOW_CLOSE:
269 if (layer->GetTargetTransform() != GetSlowCloseTransform()) 201 if (layer->GetTargetTransform() !=
202 GetSlowCloseTransform(
203 controller_->RetrieveOriginalTransform(window)))
270 return false; 204 return false;
271 break; 205 break;
272 case PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE: 206 case PowerButtonController::ANIMATION_UNDO_SLOW_CLOSE:
273 if (layer->GetTargetTransform() != ui::Transform()) 207 if (layer->GetTargetTransform() !=
274 return false; 208 controller_->RetrieveOriginalTransform(window))
209 return false;
275 break; 210 break;
276 case PowerButtonController::ANIMATION_FAST_CLOSE: 211 case PowerButtonController::ANIMATION_FAST_CLOSE:
277 if (layer->GetTargetTransform() != GetFastCloseTransform() || 212 if (layer->GetTargetTransform() !=
213 GetFastCloseTransform(layer->GetTargetTransform()) ||
278 layer->GetTargetOpacity() > 0.0001) 214 layer->GetTargetOpacity() > 0.0001)
279 return false; 215 return false;
280 break; 216 break;
281 case PowerButtonController::ANIMATION_FADE_IN: 217 case PowerButtonController::ANIMATION_FADE_IN:
282 if (layer->GetTargetOpacity() < 0.9999) 218 if (layer->GetTargetOpacity() < 0.9999)
283 return false; 219 return false;
284 break; 220 break;
285 case PowerButtonController::ANIMATION_HIDE: 221 case PowerButtonController::ANIMATION_HIDE:
286 if (layer->GetTargetOpacity() > 0.0001) 222 if (layer->GetTargetOpacity() > 0.0001)
287 return false; 223 return false;
288 break; 224 break;
289 case PowerButtonController::ANIMATION_RESTORE: 225 case PowerButtonController::ANIMATION_RESTORE:
290 if (layer->opacity() < 0.9999 || layer->transform() != ui::Transform()) 226 if (layer->opacity() < 0.9999 ||
227 layer->transform() !=
228 controller_->RetrieveOriginalTransform(window))
291 return false; 229 return false;
292 break; 230 break;
293 default: 231 default:
294 NOTREACHED() << "Unhandled animation type " << type; 232 NOTREACHED() << "Unhandled animation type " << type;
295 return false; 233 return false;
296 } 234 }
297 } 235 }
298 return true; 236 return true;
299 } 237 }
300 238
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 lock_timer_.Stop(); 381 lock_timer_.Stop();
444 } 382 }
445 } 383 }
446 } 384 }
447 385
448 void PowerButtonController::OnRootWindowResized(const gfx::Size& new_size) { 386 void PowerButtonController::OnRootWindowResized(const gfx::Size& new_size) {
449 if (background_layer_.get()) 387 if (background_layer_.get())
450 background_layer_->SetBounds(gfx::Rect(new_size)); 388 background_layer_->SetBounds(gfx::Rect(new_size));
451 } 389 }
452 390
391 // Fills |containers| with the containers described by |group|.
392 void PowerButtonController::GetContainers(ContainerGroup group,
393 aura::Window::Windows* containers) {
394 containers->clear();
395
396 aura::Window* root = aura::RootWindow::GetInstance();
397 for (aura::Window::Windows::const_iterator it = root->children().begin();
398 it != root->children().end(); ++it) {
399 aura::Window* window = *it;
400
401 bool matched = true;
402 if (group != ALL_CONTAINERS) {
403 bool is_screen_locker = IsScreenLockerContainer(window);
404 bool is_related = IsRelatedContainer(window);
405
406 switch (group) {
407 case SCREEN_LOCKER_CONTAINERS:
408 matched = is_screen_locker;
409 break;
410 case SCREEN_LOCKER_AND_RELATED_CONTAINERS:
411 matched = is_screen_locker || is_related;
412 break;
413 case ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS:
414 matched = !is_screen_locker && !is_related;
415 break;
416 default:
417 NOTREACHED() << "Unhandled container group " << group;
418 }
419 }
420
421 if (matched)
422 containers->push_back(window);
423 }
424 }
425
453 void PowerButtonController::OnLockTimeout() { 426 void PowerButtonController::OnLockTimeout() {
454 delegate_->RequestLockScreen(); 427 delegate_->RequestLockScreen();
455 lock_fail_timer_.Start( 428 lock_fail_timer_.Start(
456 FROM_HERE, 429 FROM_HERE,
457 base::TimeDelta::FromMilliseconds(kLockFailTimeoutMs), 430 base::TimeDelta::FromMilliseconds(kLockFailTimeoutMs),
458 this, &PowerButtonController::OnLockFailTimeout); 431 this, &PowerButtonController::OnLockFailTimeout);
459 } 432 }
460 433
461 void PowerButtonController::OnLockFailTimeout() { 434 void PowerButtonController::OnLockFailTimeout() {
462 DCHECK(!locked_); 435 DCHECK(!locked_);
463 LOG(ERROR) << "Screen lock request timed out";
Daniel Erat 2012/02/15 20:57:53 nit: mind keeping this in there? we sadly seem to
alicet1 2012/02/16 01:21:30 oops, reverted. I thought it was one of my debuggi
464 StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS, 436 StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
465 ANIMATION_RESTORE); 437 ANIMATION_RESTORE);
466 HideBackgroundLayer(); 438 HideBackgroundLayer();
467 } 439 }
468 440
469 void PowerButtonController::OnLockToShutdownTimeout() { 441 void PowerButtonController::OnLockToShutdownTimeout() {
470 DCHECK(locked_); 442 DCHECK(locked_);
471 StartShutdownTimer(); 443 StartShutdownTimer();
472 } 444 }
473 445
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 root_layer->Add(background_layer_.get()); 493 root_layer->Add(background_layer_.get());
522 root_layer->StackAtBottom(background_layer_.get()); 494 root_layer->StackAtBottom(background_layer_.get());
523 } 495 }
524 background_layer_->SetVisible(true); 496 background_layer_->SetVisible(true);
525 } 497 }
526 498
527 void PowerButtonController::HideBackgroundLayer() { 499 void PowerButtonController::HideBackgroundLayer() {
528 background_layer_.reset(); 500 background_layer_.reset();
529 } 501 }
530 502
503 // Apply animation |type| to all containers described by |group|.
504 void PowerButtonController::StartAnimation(ContainerGroup group,
505 AnimationType type) {
506 aura::Window::Windows containers;
507 GetContainers(group, &containers);
508 for (aura::Window::Windows::const_iterator it = containers.begin();
509 it != containers.end(); ++it) {
510 aura::Window* window = *it;
511
512 // Store this away so we can restore.
513 if (type != ANIMATION_RESTORE && type != ANIMATION_UNDO_SLOW_CLOSE) {
Daniel Erat 2012/02/15 20:57:53 nit: delete space before '&&' don't need curly br
alicet1 2012/02/16 01:21:30 Done.
514 container_transforms_[window] = window->layer()->GetTargetTransform();
515 }
516
517 switch (type) {
518 case ANIMATION_SLOW_CLOSE:
519 StartSlowCloseAnimationForWindow(window);
520 break;
521 case ANIMATION_UNDO_SLOW_CLOSE:
522 StartUndoSlowCloseAnimationForWindow(
523 window,
524 RetrieveOriginalTransform(window));
525 break;
526 case ANIMATION_FAST_CLOSE:
527 StartFastCloseAnimationForWindow(window);
528 break;
529 case ANIMATION_FADE_IN:
530 FadeInWindow(window);
531 break;
532 case ANIMATION_HIDE:
533 HideWindow(window);
534 break;
535 case ANIMATION_RESTORE:
536 RestoreWindow(window, RetrieveOriginalTransform(window));
537 break;
538 default:
539 NOTREACHED() << "Unhandled animation type " << type;
540 }
541 }
542 }
543
544 ui::Transform PowerButtonController::RetrieveOriginalTransform(
545 aura::Window* window) {
546 WindowTransformsMap::const_iterator it = container_transforms_.find(window);
547 if (it != container_transforms_.end())
548 return it->second;
549 return ui::Transform();
550 }
551
531 } // namespace ash 552 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/power_button_controller.h ('k') | ash/wm/power_button_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698