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

Unified 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: Naming fixes Created 8 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: ash/wm/power_button_controller.cc
diff --git a/ash/wm/power_button_controller.cc b/ash/wm/power_button_controller.cc
index 55d59931ee8f75ef047a408b53b320f0f9914044..5d44276433cad42dd09eca63d9c2792b609a2eae 100644
--- a/ash/wm/power_button_controller.cc
+++ b/ash/wm/power_button_controller.cc
@@ -9,6 +9,7 @@
#include "ash/shell_delegate.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/cursor_manager.h"
+#include "ash/wm/session_state_animator.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/time.h"
@@ -59,232 +60,13 @@ const int kUndoSlowCloseAnimMs = 100;
// 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;
-
// Additional time (beyond kFastCloseAnimMs) to wait after starting the
// fast-close shutdown animation before actually requesting shutdown, to give
// the animation time to finish.
const int kShutdownRequestDelayMs = 50;
-// 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);
-}
-
-// Fills |containers| with the containers described by |group|.
-void GetContainers(PowerButtonController::ContainerGroup group,
- aura::Window::Windows* containers) {
- aura::RootWindow* root_window = Shell::GetPrimaryRootWindow();
-
- aura::Window* non_lock_screen_containers = Shell::GetContainer(
- root_window,
- internal::kShellWindowId_NonLockScreenContainersContainer);
- aura::Window* lock_screen_containers = Shell::GetContainer(
- root_window,
- internal::kShellWindowId_LockScreenContainersContainer);
- aura::Window* lock_screen_related_containers = Shell::GetContainer(
- root_window,
- internal::kShellWindowId_LockScreenRelatedContainersContainer);
-
- containers->clear();
- switch (group) {
- case PowerButtonController::ALL_CONTAINERS:
- containers->push_back(non_lock_screen_containers);
- containers->push_back(lock_screen_containers);
- containers->push_back(lock_screen_related_containers);
- break;
- case PowerButtonController::SCREEN_LOCKER_CONTAINERS:
- containers->push_back(lock_screen_containers);
- break;
- case PowerButtonController::SCREEN_LOCKER_AND_RELATED_CONTAINERS:
- containers->push_back(lock_screen_containers);
- containers->push_back(lock_screen_related_containers);
- break;
- case PowerButtonController::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS:
- containers->push_back(non_lock_screen_containers);
- break;
- default:
- NOTREACHED() << "Unhandled container group " << group;
- }
-}
-
-// 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();
-}
-
-gfx::Rect PowerButtonController::TestApi::GetBackgroundLayerBounds() const {
- ui::Layer* layer = controller_->background_layer_.get();
- return layer ? layer->bounds() : gfx::Rect();
-}
-
PowerButtonController::PowerButtonController()
: login_status_(user::LOGGED_IN_NONE),
unlocked_login_status_(user::LOGGED_IN_NONE),
@@ -294,12 +76,14 @@ PowerButtonController::PowerButtonController()
shutting_down_(false),
has_legacy_power_button_(
CommandLine::ForCurrentProcess()->HasSwitch(
- switches::kAuraLegacyPowerButton)) {
+ switches::kAuraLegacyPowerButton)),
+ animator_(new SessionStateAnimator()) {
Shell::GetPrimaryRootWindow()->AddRootWindowObserver(this);
}
PowerButtonController::~PowerButtonController() {
Shell::GetPrimaryRootWindow()->RemoveRootWindowObserver(this);
+ delete animator_;
}
void PowerButtonController::OnLoginStateChanged(user::LoginStatus status) {
@@ -315,8 +99,9 @@ void PowerButtonController::OnAppTerminating() {
Shell* shell = ash::Shell::GetInstance();
shell->env_filter()->set_update_cursor_visibility(false);
shell->cursor_manager()->ShowCursor(false);
- ShowBackgroundLayer();
- StartAnimation(ALL_CONTAINERS, ANIMATION_HIDE);
+ animator_->ShowBackgroundLayer();
+ animator_->StartAnimation(SessionStateAnimator::ALL_CONTAINERS,
+ SessionStateAnimator::HIDE);
}
}
@@ -333,7 +118,8 @@ void PowerButtonController::OnLockStateChanged(bool locked) {
}
if (locked) {
- StartAnimation(SCREEN_LOCKER_CONTAINERS, ANIMATION_FADE_IN);
+ animator_->StartAnimation(SessionStateAnimator::SCREEN_LOCKER_CONTAINERS,
+ SessionStateAnimator::FADE_IN);
lock_timer_.Stop();
lock_fail_timer_.Stop();
@@ -345,9 +131,10 @@ void PowerButtonController::OnLockStateChanged(bool locked) {
this, &PowerButtonController::OnLockToShutdownTimeout);
}
} else {
- StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
- ANIMATION_RESTORE);
- HideBackgroundLayer();
+ animator_->StartAnimation(
+ SessionStateAnimator::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
+ SessionStateAnimator::RESTORE);
+ animator_->HideBackgroundLayer();
}
}
@@ -362,13 +149,16 @@ void PowerButtonController::OnStartingLock() {
// Ensure that the background layer is visible -- if the screen was locked via
// the wrench menu, we won't have already shown the background as part of the
// slow-close animation.
- ShowBackgroundLayer();
+ animator_->ShowBackgroundLayer();
- StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
- ANIMATION_FAST_CLOSE);
+ animator_->StartAnimation(
+ SessionStateAnimator::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
+ SessionStateAnimator::FAST_CLOSE);
// Hide the screen locker containers so we can make them fade in later.
- StartAnimation(SCREEN_LOCKER_CONTAINERS, ANIMATION_HIDE);
+ animator_->StartAnimation(
+ SessionStateAnimator::SCREEN_LOCKER_CONTAINERS,
+ SessionStateAnimator::HIDE);
}
void PowerButtonController::OnPowerButtonEvent(
@@ -388,10 +178,11 @@ void PowerButtonController::OnPowerButtonEvent(
// running on official hardware, just lock the screen or shut down
// immediately.
if (down) {
- ShowBackgroundLayer();
+ animator_->ShowBackgroundLayer();
if (LoggedInAsNonGuest() && login_status_ != user::LOGGED_IN_LOCKED) {
- StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
- ANIMATION_SLOW_CLOSE);
+ animator_->StartAnimation(
+ SessionStateAnimator::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
+ SessionStateAnimator::SLOW_CLOSE);
OnLockTimeout();
} else {
OnShutdownTimeout();
@@ -409,19 +200,16 @@ void PowerButtonController::OnPowerButtonEvent(
StartShutdownTimer();
} else { // Button is up.
if (lock_timer_.IsRunning() || shutdown_timer_.IsRunning())
- StartAnimation(
+ animator_->StartAnimation(
(login_status_ == user::LOGGED_IN_LOCKED) ?
- SCREEN_LOCKER_AND_RELATED_CONTAINERS : ALL_CONTAINERS,
- ANIMATION_UNDO_SLOW_CLOSE);
+ SessionStateAnimator::SCREEN_LOCKER_AND_RELATED_CONTAINERS :
+ SessionStateAnimator::ALL_CONTAINERS,
+ SessionStateAnimator::UNDO_SLOW_CLOSE);
// Drop the background layer after the undo animation finishes.
if (lock_timer_.IsRunning() ||
(shutdown_timer_.IsRunning() && !LoggedInAsNonGuest())) {
- hide_background_layer_timer_.Stop();
- hide_background_layer_timer_.Start(
- FROM_HERE,
- base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs),
- this, &PowerButtonController::HideBackgroundLayer);
+ animator_->DropBackgroundLayer();
}
lock_timer_.Stop();
@@ -450,13 +238,10 @@ void PowerButtonController::OnLockButtonEvent(
StartLockTimer();
} else {
if (lock_timer_.IsRunning()) {
- StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
- ANIMATION_UNDO_SLOW_CLOSE);
- hide_background_layer_timer_.Stop();
- hide_background_layer_timer_.Start(
- FROM_HERE,
- base::TimeDelta::FromMilliseconds(kUndoSlowCloseAnimMs),
- this, &PowerButtonController::HideBackgroundLayer);
+ animator_->StartAnimation(
+ SessionStateAnimator::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
+ SessionStateAnimator::UNDO_SLOW_CLOSE);
+ animator_->DropBackgroundLayer();
lock_timer_.Stop();
}
}
@@ -467,12 +252,6 @@ void PowerButtonController::RequestShutdown() {
StartShutdownAnimationAndRequestShutdown();
}
-void PowerButtonController::OnRootWindowResized(const aura::RootWindow* root,
- const gfx::Size& new_size) {
- if (background_layer_.get())
- background_layer_->SetBounds(gfx::Rect(root->bounds().size()));
-}
-
void PowerButtonController::OnRootWindowHostCloseRequested(
const aura::RootWindow*) {
if(Shell::GetInstance() && Shell::GetInstance()->delegate())
@@ -499,9 +278,10 @@ void PowerButtonController::OnLockTimeout() {
void PowerButtonController::OnLockFailTimeout() {
DCHECK_NE(login_status_, user::LOGGED_IN_LOCKED);
LOG(ERROR) << "Screen lock request timed out";
- StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
- ANIMATION_RESTORE);
- HideBackgroundLayer();
+ animator_->StartAnimation(
+ SessionStateAnimator::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
+ SessionStateAnimator::RESTORE);
+ animator_->HideBackgroundLayer();
}
void PowerButtonController::OnLockToShutdownTimeout() {
@@ -520,9 +300,10 @@ void PowerButtonController::OnRealShutdownTimeout() {
}
void PowerButtonController::StartLockTimer() {
- ShowBackgroundLayer();
- StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
- ANIMATION_SLOW_CLOSE);
+ animator_->ShowBackgroundLayer();
+ animator_->StartAnimation(
+ SessionStateAnimator::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
+ SessionStateAnimator::SLOW_CLOSE);
lock_timer_.Stop();
lock_timer_.Start(FROM_HERE,
base::TimeDelta::FromMilliseconds(kSlowCloseAnimMs),
@@ -530,8 +311,10 @@ void PowerButtonController::StartLockTimer() {
}
void PowerButtonController::StartShutdownTimer() {
- ShowBackgroundLayer();
- StartAnimation(ALL_CONTAINERS, ANIMATION_SLOW_CLOSE);
+ animator_->ShowBackgroundLayer();
+ animator_->StartAnimation(
+ SessionStateAnimator::ALL_CONTAINERS,
+ SessionStateAnimator::SLOW_CLOSE);
shutdown_timer_.Stop();
shutdown_timer_.Start(
FROM_HERE,
@@ -547,16 +330,20 @@ void PowerButtonController::StartShutdownAnimationAndRequestShutdown() {
shell->env_filter()->set_update_cursor_visibility(false);
shell->cursor_manager()->ShowCursor(false);
- ShowBackgroundLayer();
+ animator_->ShowBackgroundLayer();
if (login_status_ != user::LOGGED_IN_NONE) {
// Hide the other containers before starting the animation.
- // ANIMATION_FAST_CLOSE will make the screen locker windows partially
+ // FAST_CLOSE will make the screen locker windows partially
// transparent, and we don't want the other windows to show through.
- StartAnimation(ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
- ANIMATION_HIDE);
- StartAnimation(SCREEN_LOCKER_AND_RELATED_CONTAINERS, ANIMATION_FAST_CLOSE);
+ animator_->StartAnimation(
+ SessionStateAnimator::ALL_BUT_SCREEN_LOCKER_AND_RELATED_CONTAINERS,
+ SessionStateAnimator::HIDE);
+ animator_->StartAnimation(
+ SessionStateAnimator::SCREEN_LOCKER_AND_RELATED_CONTAINERS,
+ SessionStateAnimator::FAST_CLOSE);
} else {
- StartAnimation(ALL_CONTAINERS, ANIMATION_FAST_CLOSE);
+ animator_->StartAnimation(SessionStateAnimator::ALL_CONTAINERS,
+ SessionStateAnimator::FAST_CLOSE);
}
real_shutdown_timer_.Start(
@@ -566,24 +353,4 @@ void PowerButtonController::StartShutdownAnimationAndRequestShutdown() {
this, &PowerButtonController::OnRealShutdownTimeout);
}
-void PowerButtonController::ShowBackgroundLayer() {
- if (hide_background_layer_timer_.IsRunning())
- hide_background_layer_timer_.Stop();
-
- if (!background_layer_.get()) {
- background_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR));
- background_layer_->SetColor(SK_ColorBLACK);
-
- ui::Layer* root_layer = Shell::GetPrimaryRootWindow()->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

Powered by Google App Engine
This is Rietveld 408576698