Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 several layer animations and | |
|
oshima
2012/07/27 06:22:57
s/several/multiple/
Jun Mukai
2012/07/27 06:35:05
Done.
| |
| 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 completed_successfully_(true), | |
| 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 (completed_successfully_) | |
| 47 callback_.Run(); | |
| 48 MessageLoopForUI::current()->DeleteSoon(FROM_HERE, this); | |
| 49 } | |
| 50 } | |
| 51 | |
| 52 void OnSingleTaskAborted() { | |
| 53 completed_successfully_ = false; | |
| 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_; | |
|
oshima
2012/07/27 06:22:57
DISALLOW_COPY_AND_ASSIGN
Jun Mukai
2012/07/27 06:35:06
Done.
| |
| 84 }; | |
| 85 | |
| 86 size_t completed_counter_; | |
| 87 bool completed_successfully_; | |
|
oshima
2012/07/27 06:22:57
animation_aborted_ maybe more intuitive, but i'll
Jun Mukai
2012/07/27 06:35:06
Agree with you and changed to animation_aborted_ (
| |
| 88 ScopedVector<Observer> observer_list_; | |
| 89 base::Closure callback_; | |
| 90 }; | |
|
oshima
2012/07/27 06:22:57
DISALLOW_COPY_AND_ASSIGN
Jun Mukai
2012/07/27 06:35:06
Done.
| |
| 91 | |
| 92 } // namespace | |
| 93 | |
| 94 OutputConfiguratorAnimation::OutputConfiguratorAnimation() { | |
| 95 } | |
| 96 | |
| 97 OutputConfiguratorAnimation::~OutputConfiguratorAnimation() { | |
| 98 ClearHidingLayers(); | |
| 99 } | |
| 100 | |
| 101 void OutputConfiguratorAnimation::WillDisplayModeChange( | |
| 102 base::Closure callback) { | |
| 103 CallbackRunningObserver* observer = new CallbackRunningObserver(callback); | |
| 104 ClearHidingLayers(); | |
| 105 | |
| 106 // Make the fade-out animation for all root windows. Instead of actually | |
| 107 // hiding the root windows, we put a black layer over a root window for | |
| 108 // safety. | |
| 109 Shell::RootWindowList root_windows = | |
| 110 Shell::GetInstance()->GetAllRootWindows(); | |
| 111 for (Shell::RootWindowList::const_iterator it = root_windows.begin(); | |
| 112 it != root_windows.end(); ++it) { | |
| 113 aura::RootWindow* root_window = *it; | |
| 114 ui::Layer* hiding_layer = new ui::Layer(ui::LAYER_SOLID_COLOR); | |
| 115 hiding_layer->SetColor(SK_ColorBLACK); | |
| 116 hiding_layer->SetBounds(root_window->bounds()); | |
| 117 ui::Layer* parent = ash::Shell::GetContainer( | |
| 118 root_window, | |
| 119 ash::internal::kShellWindowId_OverlayContainer)->layer(); | |
| 120 parent->Add(hiding_layer); | |
| 121 | |
| 122 hiding_layer->SetOpacity(0.0); | |
| 123 | |
| 124 ui::ScopedLayerAnimationSettings settings(hiding_layer->GetAnimator()); | |
| 125 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds( | |
| 126 kFadingAnimationDurationInMS)); | |
| 127 observer->AddNewAnimator(hiding_layer->GetAnimator()); | |
| 128 hiding_layer->SetOpacity(1.0f); | |
| 129 hiding_layer->SetVisible(true); | |
| 130 hiding_layers_[root_window] = hiding_layer; | |
| 131 } | |
| 132 | |
| 133 // In case that OnDisplayModeChanged() isn't called or its animator is | |
| 134 // canceled due to some unknown errors, we set a timer to clear these | |
| 135 // hiding layers. | |
| 136 timer_.reset(new base::OneShotTimer<OutputConfiguratorAnimation>()); | |
| 137 timer_->Start(FROM_HERE, | |
| 138 base::TimeDelta::FromSeconds(kFadingTimeoutDurationInSeconds), | |
| 139 this, | |
| 140 &OutputConfiguratorAnimation::ClearHidingLayers); | |
| 141 } | |
| 142 | |
| 143 void OutputConfiguratorAnimation::OnDisplayModeChanged() { | |
| 144 // We want to make sure clearing all of hiding layers after the animation | |
| 145 // finished. | |
| 146 CallbackRunningObserver* observer = new CallbackRunningObserver( | |
| 147 base::Bind(&OutputConfiguratorAnimation::ClearHidingLayers, | |
| 148 base::Unretained(this))); | |
|
oshima
2012/07/27 06:22:57
this can be canceled, but I assume this only happe
Jun Mukai
2012/07/27 06:35:06
Done.
| |
| 149 | |
| 150 // Schedules the fade-in effect for all root windows. Because we put the | |
| 151 // black layers for fade-out, here we actually turn those black layers | |
| 152 // invisible. | |
| 153 Shell::RootWindowList root_windows = | |
| 154 Shell::GetInstance()->GetAllRootWindows(); | |
| 155 for (Shell::RootWindowList::const_iterator it = root_windows.begin(); | |
| 156 it != root_windows.end(); ++it) { | |
| 157 aura::RootWindow* root_window = *it; | |
| 158 ui::Layer* hiding_layer = NULL; | |
| 159 if (hiding_layers_.find(root_window) == hiding_layers_.end()) { | |
| 160 // In case of the transition from mirroring->non-mirroring, new root | |
| 161 // windows appear and we do not have the black layers for them. Thus | |
| 162 // we need to create the layer and make it visible. | |
| 163 hiding_layer = new ui::Layer(ui::LAYER_SOLID_COLOR); | |
| 164 hiding_layer->SetColor(SK_ColorBLACK); | |
| 165 hiding_layer->SetBounds(root_window->bounds()); | |
| 166 ui::Layer* parent = ash::Shell::GetContainer( | |
| 167 root_window, | |
| 168 ash::internal::kShellWindowId_OverlayContainer)->layer(); | |
| 169 parent->Add(hiding_layer); | |
| 170 hiding_layer->SetOpacity(1.0f); | |
| 171 hiding_layer->SetVisible(true); | |
| 172 hiding_layers_[root_window] = hiding_layer; | |
| 173 } else { | |
| 174 hiding_layer = hiding_layers_[root_window]; | |
| 175 } | |
| 176 | |
| 177 ui::ScopedLayerAnimationSettings settings(hiding_layer->GetAnimator()); | |
| 178 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds( | |
| 179 kFadingAnimationDurationInMS)); | |
| 180 observer->AddNewAnimator(hiding_layer->GetAnimator()); | |
| 181 hiding_layer->SetOpacity(0.0f); | |
| 182 hiding_layer->SetVisible(false); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 void OutputConfiguratorAnimation::ClearHidingLayers() { | |
| 187 if (timer_.get()) { | |
| 188 timer_->Stop(); | |
| 189 timer_.reset(); | |
| 190 } | |
| 191 STLDeleteContainerPairSecondPointers( | |
| 192 hiding_layers_.begin(), hiding_layers_.end()); | |
| 193 hiding_layers_.clear(); | |
| 194 } | |
| 195 | |
| 196 } // namespace internal | |
| 197 } // namespace ash | |
| OLD | NEW |