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

Side by Side Diff: ash/display/output_configurator_animation.cc

Issue 10817028: Add Fade-out/Fade-in animation during output-configuration change. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ash/display/output_configurator_animation.h"
6
7 #include "ash/shell.h"
8 #include "ash/shell_window_ids.h"
9 #include "base/bind.h"
10 #include "base/stl_util.h"
11 #include "base/time.h"
12 #include "ui/aura/root_window.h"
13 #include "ui/aura/window.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"
19
20 namespace ash {
21 namespace internal {
22 namespace {
23
24 const int kFadingAnimationDurationInMS = 200;
25 const int kFadingTimeoutDurationInSeconds = 10;
26
27 // CallbackRunningObserver accepts multiple layer animations and
28 // runs the specified |callback| when all of the animations have finished.
29 class CallbackRunningObserver {
30 public:
31 CallbackRunningObserver(base::Closure callback)
32 : completed_counter_(0),
33 animation_aborted_(false),
34 callback_(callback) {}
35
36 void AddNewAnimator(ui::LayerAnimator* animator) {
37 Observer* observer = new Observer(this);
38 animator->AddObserver(observer);
39 observer_list_.push_back(observer);
40 }
41
42 private:
43 void OnSingleTaskCompleted() {
44 completed_counter_++;
45 if (completed_counter_ >= observer_list_.size()) {
46 if (!animation_aborted_)
47 callback_.Run();
48 MessageLoopForUI::current()->DeleteSoon(FROM_HERE, this);
49 }
50 }
51
52 void OnSingleTaskAborted() {
53 animation_aborted_ = true;
54 OnSingleTaskCompleted();
55 }
56
57 // The actual observer to listen each animation completion.
58 class Observer : public ui::LayerAnimationObserver {
59 public:
60 Observer(CallbackRunningObserver* observer)
61 : observer_(observer) {}
62
63 protected:
64 // ui::LayerAnimationObserver overrides:
65 virtual void OnLayerAnimationEnded(
66 ui::LayerAnimationSequence* sequence) OVERRIDE {
67 StopObserving();
68 observer_->OnSingleTaskCompleted();
69 }
70 virtual void OnLayerAnimationAborted(
71 ui::LayerAnimationSequence* sequence) OVERRIDE {
72 StopObserving();
73 observer_->OnSingleTaskAborted();
74 }
75 virtual void OnLayerAnimationScheduled(
76 ui::LayerAnimationSequence* sequence) OVERRIDE {
77 }
78 virtual bool RequiresNotificationWhenAnimatorDestroyed() const OVERRIDE {
79 return true;
80 }
81
82 private:
83 CallbackRunningObserver* observer_;
84
85 DISALLOW_COPY_AND_ASSIGN(Observer);
86 };
87
88 size_t completed_counter_;
89 bool animation_aborted_;
90 ScopedVector<Observer> observer_list_;
91 base::Closure callback_;
92
93 DISALLOW_COPY_AND_ASSIGN(CallbackRunningObserver);
94 };
95
96 } // namespace
97
98 OutputConfiguratorAnimation::OutputConfiguratorAnimation() {
99 }
100
101 OutputConfiguratorAnimation::~OutputConfiguratorAnimation() {
102 ClearHidingLayers();
103 }
104
105 void OutputConfiguratorAnimation::WillDisplayModeChange(
106 base::Closure callback) {
107 CallbackRunningObserver* observer = new CallbackRunningObserver(callback);
108 ClearHidingLayers();
109
110 // Make the fade-out animation for all root windows. Instead of actually
111 // hiding the root windows, we put a black layer over a root window for
112 // safety. These layers remain to hide root windows and will be deleted
113 // after the animation of OnDisplayModeChanged().
114 Shell::RootWindowList root_windows =
115 Shell::GetInstance()->GetAllRootWindows();
116 for (Shell::RootWindowList::const_iterator it = root_windows.begin();
117 it != root_windows.end(); ++it) {
118 aura::RootWindow* root_window = *it;
119 ui::Layer* hiding_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
120 hiding_layer->SetColor(SK_ColorBLACK);
121 hiding_layer->SetBounds(root_window->bounds());
122 ui::Layer* parent = ash::Shell::GetContainer(
123 root_window,
124 ash::internal::kShellWindowId_OverlayContainer)->layer();
125 parent->Add(hiding_layer);
126
127 hiding_layer->SetOpacity(0.0);
128
129 ui::ScopedLayerAnimationSettings settings(hiding_layer->GetAnimator());
130 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
131 kFadingAnimationDurationInMS));
132 observer->AddNewAnimator(hiding_layer->GetAnimator());
133 hiding_layer->SetOpacity(1.0f);
134 hiding_layer->SetVisible(true);
135 hiding_layers_[root_window] = hiding_layer;
136 }
137
138 // In case that OnDisplayModeChanged() isn't called or its animator is
139 // canceled due to some unknown errors, we set a timer to clear these
140 // hiding layers.
141 timer_.reset(new base::OneShotTimer<OutputConfiguratorAnimation>());
142 timer_->Start(FROM_HERE,
143 base::TimeDelta::FromSeconds(kFadingTimeoutDurationInSeconds),
144 this,
145 &OutputConfiguratorAnimation::ClearHidingLayers);
146 }
147
148 void OutputConfiguratorAnimation::OnDisplayModeChanged() {
149 // We want to make sure clearing all of hiding layers after the animation
150 // finished. Note that this callback can be canceled, but the cancel only
151 // happens when the next animation is scheduled. Thus the hiding layers
152 // should be deleted eventually.
153 CallbackRunningObserver* observer = new CallbackRunningObserver(
154 base::Bind(&OutputConfiguratorAnimation::ClearHidingLayers,
155 base::Unretained(this)));
156
157 // Ensure that layers are not animating.
158 for (std::map<aura::RootWindow*, ui::Layer*>::iterator it =
159 hiding_layers_.begin(); it != hiding_layers_.end(); ++it) {
160 ui::LayerAnimator* animator = it->second->GetAnimator();
161 if (animator->is_animating())
162 animator->StopAnimating();
163 }
164
165 // Schedules the fade-in effect for all root windows. Because we put the
166 // black layers for fade-out, here we actually turn those black layers
167 // invisible.
168 Shell::RootWindowList root_windows =
169 Shell::GetInstance()->GetAllRootWindows();
170 for (Shell::RootWindowList::const_iterator it = root_windows.begin();
171 it != root_windows.end(); ++it) {
172 aura::RootWindow* root_window = *it;
173 ui::Layer* hiding_layer = NULL;
174 if (hiding_layers_.find(root_window) == hiding_layers_.end()) {
175 // In case of the transition from mirroring->non-mirroring, new root
176 // windows appear and we do not have the black layers for them. Thus
177 // we need to create the layer and make it visible.
178 hiding_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
179 hiding_layer->SetColor(SK_ColorBLACK);
180 hiding_layer->SetBounds(root_window->bounds());
181 ui::Layer* parent = ash::Shell::GetContainer(
182 root_window,
183 ash::internal::kShellWindowId_OverlayContainer)->layer();
184 parent->Add(hiding_layer);
185 hiding_layer->SetOpacity(1.0f);
186 hiding_layer->SetVisible(true);
187 hiding_layers_[root_window] = hiding_layer;
188 } else {
189 hiding_layer = hiding_layers_[root_window];
190 }
191
192 ui::ScopedLayerAnimationSettings settings(hiding_layer->GetAnimator());
193 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
194 kFadingAnimationDurationInMS));
195 observer->AddNewAnimator(hiding_layer->GetAnimator());
196 hiding_layer->SetOpacity(0.0f);
197 hiding_layer->SetVisible(false);
198 }
199 }
200
201 void OutputConfiguratorAnimation::ClearHidingLayers() {
202 if (timer_.get()) {
203 timer_->Stop();
204 timer_.reset();
205 }
206 STLDeleteContainerPairSecondPointers(
207 hiding_layers_.begin(), hiding_layers_.end());
208 hiding_layers_.clear();
209 }
210
211 } // namespace internal
212 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698