1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/display/display_configurator_animation.h"
8 #include "ash/shell_window_ids.h"
10 #include "base/stl_util.h"
11 #include "base/time/time.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_event_dispatcher.h"
14 #include "ui/compositor/layer.h"
15 #include "ui/compositor/layer_animation_observer.h"
16 #include "ui/compositor/layer_animation_sequence.h"
17 #include "ui/compositor/layer_animator.h"
18 #include "ui/compositor/scoped_layer_animation_settings.h"
23 const int kFadingAnimationDurationInMS
= 200;
24 const int kFadingTimeoutDurationInSeconds
= 10;
26 // CallbackRunningObserver accepts multiple layer animations and
27 // runs the specified |callback| when all of the animations have finished.
28 class CallbackRunningObserver
{
30 CallbackRunningObserver(base::Closure callback
)
31 : completed_counter_(0),
32 animation_aborted_(false),
33 callback_(callback
) {}
35 void AddNewAnimator(ui::LayerAnimator
* animator
) {
36 Observer
* observer
= new Observer(animator
, this);
37 animator
->AddObserver(observer
);
38 observer_list_
.push_back(observer
);
42 void OnSingleTaskCompleted() {
44 if (completed_counter_
>= observer_list_
.size()) {
45 base::MessageLoopForUI::current()->DeleteSoon(FROM_HERE
, this);
46 if (!animation_aborted_
)
47 base::MessageLoopForUI::current()->PostTask(FROM_HERE
, callback_
);
51 void OnSingleTaskAborted() {
52 animation_aborted_
= true;
53 OnSingleTaskCompleted();
56 // The actual observer to listen each animation completion.
57 class Observer
: public ui::LayerAnimationObserver
{
59 Observer(ui::LayerAnimator
* animator
,
60 CallbackRunningObserver
* observer
)
61 : animator_(animator
),
62 observer_(observer
) {}
65 // ui::LayerAnimationObserver overrides:
66 void OnLayerAnimationEnded(ui::LayerAnimationSequence
* sequence
) override
{
67 animator_
->RemoveObserver(this);
68 observer_
->OnSingleTaskCompleted();
70 void OnLayerAnimationAborted(
71 ui::LayerAnimationSequence
* sequence
) override
{
72 animator_
->RemoveObserver(this);
73 observer_
->OnSingleTaskAborted();
75 void OnLayerAnimationScheduled(
76 ui::LayerAnimationSequence
* sequence
) override
{}
77 bool RequiresNotificationWhenAnimatorDestroyed() const override
{
82 ui::LayerAnimator
* animator_
;
83 CallbackRunningObserver
* observer_
;
85 DISALLOW_COPY_AND_ASSIGN(Observer
);
88 size_t completed_counter_
;
89 bool animation_aborted_
;
90 ScopedVector
<Observer
> observer_list_
;
91 base::Closure callback_
;
93 DISALLOW_COPY_AND_ASSIGN(CallbackRunningObserver
);
98 DisplayConfiguratorAnimation::DisplayConfiguratorAnimation()
99 : weak_ptr_factory_(this) {
102 DisplayConfiguratorAnimation::~DisplayConfiguratorAnimation() {
106 void DisplayConfiguratorAnimation::StartFadeOutAnimation(
107 base::Closure callback
) {
108 CallbackRunningObserver
* observer
= new CallbackRunningObserver(callback
);
111 // Make the fade-out animation for all root windows. Instead of actually
112 // hiding the root windows, we put a black layer over a root window for
113 // safety. These layers remain to hide root windows and will be deleted
114 // after the animation of OnDisplayModeChanged().
115 aura::Window::Windows root_windows
=
116 Shell::GetInstance()->GetAllRootWindows();
117 for (aura::Window::Windows::const_iterator it
= root_windows
.begin();
118 it
!= root_windows
.end(); ++it
) {
119 aura::Window
* root_window
= *it
;
120 ui::Layer
* hiding_layer
= new ui::Layer(ui::LAYER_SOLID_COLOR
);
121 hiding_layer
->SetColor(SK_ColorBLACK
);
122 hiding_layer
->SetBounds(root_window
->bounds());
124 ash::Shell::GetContainer(root_window
,
125 ash::kShellWindowId_OverlayContainer
)->layer();
126 parent
->Add(hiding_layer
);
128 hiding_layer
->SetOpacity(0.0);
130 ui::ScopedLayerAnimationSettings
settings(hiding_layer
->GetAnimator());
131 settings
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
132 kFadingAnimationDurationInMS
));
133 observer
->AddNewAnimator(hiding_layer
->GetAnimator());
134 hiding_layer
->SetOpacity(1.0f
);
135 hiding_layer
->SetVisible(true);
136 hiding_layers_
[root_window
] = hiding_layer
;
139 // In case that OnDisplayModeChanged() isn't called or its animator is
140 // canceled due to some unknown errors, we set a timer to clear these
142 timer_
.reset(new base::OneShotTimer
<DisplayConfiguratorAnimation
>());
143 timer_
->Start(FROM_HERE
,
144 base::TimeDelta::FromSeconds(kFadingTimeoutDurationInSeconds
),
146 &DisplayConfiguratorAnimation::ClearHidingLayers
);
149 void DisplayConfiguratorAnimation::StartFadeInAnimation() {
150 // We want to make sure clearing all of hiding layers after the animation
151 // finished. Note that this callback can be canceled, but the cancel only
152 // happens when the next animation is scheduled. Thus the hiding layers
153 // should be deleted eventually.
154 CallbackRunningObserver
* observer
= new CallbackRunningObserver(
155 base::Bind(&DisplayConfiguratorAnimation::ClearHidingLayers
,
156 weak_ptr_factory_
.GetWeakPtr()));
158 // Ensure that layers are not animating.
159 for (std::map
<aura::Window
*, ui::Layer
*>::iterator it
=
160 hiding_layers_
.begin(); it
!= hiding_layers_
.end(); ++it
) {
161 ui::LayerAnimator
* animator
= it
->second
->GetAnimator();
162 if (animator
->is_animating())
163 animator
->StopAnimating();
166 // Schedules the fade-in effect for all root windows. Because we put the
167 // black layers for fade-out, here we actually turn those black layers
169 aura::Window::Windows root_windows
=
170 Shell::GetInstance()->GetAllRootWindows();
171 for (aura::Window::Windows::const_iterator it
= root_windows
.begin();
172 it
!= root_windows
.end(); ++it
) {
173 aura::Window
* root_window
= *it
;
174 ui::Layer
* hiding_layer
= NULL
;
175 if (hiding_layers_
.find(root_window
) == hiding_layers_
.end()) {
176 // In case of the transition from mirroring->non-mirroring, new root
177 // windows appear and we do not have the black layers for them. Thus
178 // we need to create the layer and make it visible.
179 hiding_layer
= new ui::Layer(ui::LAYER_SOLID_COLOR
);
180 hiding_layer
->SetColor(SK_ColorBLACK
);
181 hiding_layer
->SetBounds(root_window
->bounds());
183 ash::Shell::GetContainer(
184 root_window
, ash::kShellWindowId_OverlayContainer
)->layer();
185 parent
->Add(hiding_layer
);
186 hiding_layer
->SetOpacity(1.0f
);
187 hiding_layer
->SetVisible(true);
188 hiding_layers_
[root_window
] = hiding_layer
;
190 hiding_layer
= hiding_layers_
[root_window
];
191 if (hiding_layer
->bounds() != root_window
->bounds())
192 hiding_layer
->SetBounds(root_window
->bounds());
195 ui::ScopedLayerAnimationSettings
settings(hiding_layer
->GetAnimator());
196 settings
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
197 kFadingAnimationDurationInMS
));
198 observer
->AddNewAnimator(hiding_layer
->GetAnimator());
199 hiding_layer
->SetOpacity(0.0f
);
200 hiding_layer
->SetVisible(false);
204 void DisplayConfiguratorAnimation::OnDisplayModeChanged(
205 const ui::DisplayConfigurator::DisplayStateList
& displays
) {
206 if (!hiding_layers_
.empty())
207 StartFadeInAnimation();
210 void DisplayConfiguratorAnimation::OnDisplayModeChangeFailed(
211 const ui::DisplayConfigurator::DisplayStateList
& displays
,
212 ui::MultipleDisplayState failed_new_state
) {
213 if (!hiding_layers_
.empty())
214 StartFadeInAnimation();
217 void DisplayConfiguratorAnimation::ClearHidingLayers() {
222 STLDeleteContainerPairSecondPointers(
223 hiding_layers_
.begin(), hiding_layers_
.end());
224 hiding_layers_
.clear();