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 |
| 26 // CallbackRunningObserver accepts several layer animations and |
| 27 // runs the specified |callback| when all of the animations have finished. |
| 28 class CallbackRunningObserver { |
| 29 public: |
| 30 CallbackRunningObserver(base::Closure callback) |
| 31 : completed_counter_(0), |
| 32 callback_(callback) {} |
| 33 |
| 34 void AddNewAnimator(ui::ScopedLayerAnimationSettings *settings) { |
| 35 Observer* observer = new Observer(this); |
| 36 settings->AddObserver(observer); |
| 37 observer_list_.push_back(observer); |
| 38 } |
| 39 |
| 40 private: |
| 41 void OnSingleTaskCompleted() { |
| 42 completed_counter_++; |
| 43 if (completed_counter_ >= observer_list_.size()) { |
| 44 callback_.Run(); |
| 45 MessageLoopForUI::current()->DeleteSoon(FROM_HERE, this); |
| 46 } |
| 47 } |
| 48 |
| 49 // The actual observer to listen each animation completion. |
| 50 class Observer : public ui::ImplicitAnimationObserver { |
| 51 public: |
| 52 Observer(CallbackRunningObserver* observer) |
| 53 : observer_(observer) {} |
| 54 |
| 55 virtual void OnImplicitAnimationsCompleted() OVERRIDE { |
| 56 observer_->OnSingleTaskCompleted(); |
| 57 } |
| 58 |
| 59 private: |
| 60 CallbackRunningObserver* observer_; |
| 61 }; |
| 62 |
| 63 size_t completed_counter_; |
| 64 ScopedVector<Observer> observer_list_; |
| 65 base::Closure callback_; |
| 66 }; |
| 67 |
| 68 } // namespace |
| 69 |
| 70 OutputConfiguratorAnimation::OutputConfiguratorAnimation() { |
| 71 } |
| 72 |
| 73 OutputConfiguratorAnimation::~OutputConfiguratorAnimation() { |
| 74 ClearHidingLayers(); |
| 75 } |
| 76 |
| 77 void OutputConfiguratorAnimation::WillDisplayModeChange( |
| 78 base::Closure callback) { |
| 79 CallbackRunningObserver *observer = new CallbackRunningObserver(callback); |
| 80 ClearHidingLayers(); |
| 81 |
| 82 // Make the fade-out animation for all root windows. Instead of actually |
| 83 // hiding the root windows, we put a black layer over a root window for |
| 84 // safety. |
| 85 Shell::RootWindowList root_windows = |
| 86 Shell::GetInstance()->GetAllRootWindows(); |
| 87 for (Shell::RootWindowList::const_iterator it = root_windows.begin(); |
| 88 it != root_windows.end(); ++it) { |
| 89 aura::RootWindow* root_window = *it; |
| 90 ui::Layer* hiding_layer = new ui::Layer(ui::LAYER_SOLID_COLOR); |
| 91 hiding_layer->SetColor(SK_ColorBLACK); |
| 92 hiding_layer->SetBounds(root_window->bounds()); |
| 93 ui::Layer* parent = ash::Shell::GetContainer( |
| 94 root_window, |
| 95 ash::internal::kShellWindowId_OverlayContainer)->layer(); |
| 96 parent->Add(hiding_layer); |
| 97 |
| 98 hiding_layer->SetOpacity(0.0); |
| 99 |
| 100 ui::ScopedLayerAnimationSettings settings(hiding_layer->GetAnimator()); |
| 101 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds( |
| 102 kFadingAnimationDurationInMS)); |
| 103 observer->AddNewAnimator(&settings); |
| 104 hiding_layer->SetOpacity(1.0f); |
| 105 hiding_layer->SetVisible(true); |
| 106 hiding_layers_[root_window] = hiding_layer; |
| 107 } |
| 108 } |
| 109 |
| 110 void OutputConfiguratorAnimation::OnDisplayModeChanged() { |
| 111 // We want to make sure clearing all of hiding layers after the animation |
| 112 // finished. |
| 113 CallbackRunningObserver *observer = new CallbackRunningObserver( |
| 114 base::Bind(&OutputConfiguratorAnimation::ClearHidingLayers, |
| 115 base::Unretained(this))); |
| 116 |
| 117 // Schedules the fade-in effect for all root windows. Because we put the |
| 118 // black layers for fade-out, here we actually turn those black layers |
| 119 // invisible. |
| 120 Shell::RootWindowList root_windows = |
| 121 Shell::GetInstance()->GetAllRootWindows(); |
| 122 for (Shell::RootWindowList::const_iterator it = root_windows.begin(); |
| 123 it != root_windows.end(); ++it) { |
| 124 aura::RootWindow* root_window = *it; |
| 125 ui::Layer* hiding_layer = NULL; |
| 126 if (hiding_layers_.find(root_window) == hiding_layers_.end()) { |
| 127 // In case of the transition from mirroring->non-mirroring, new root |
| 128 // windows appear and we do not have the black layers for them. Thus |
| 129 // we need to create the layer and make it visible. |
| 130 hiding_layer = new ui::Layer(ui::LAYER_SOLID_COLOR); |
| 131 hiding_layer->SetColor(SK_ColorBLACK); |
| 132 hiding_layer->SetBounds(root_window->bounds()); |
| 133 ui::Layer* parent = ash::Shell::GetContainer( |
| 134 root_window, |
| 135 ash::internal::kShellWindowId_OverlayContainer)->layer(); |
| 136 parent->Add(hiding_layer); |
| 137 hiding_layer->SetOpacity(1.0f); |
| 138 hiding_layer->SetVisible(true); |
| 139 hiding_layers_[root_window] = hiding_layer; |
| 140 } else { |
| 141 hiding_layer = hiding_layers_[root_window]; |
| 142 } |
| 143 |
| 144 ui::ScopedLayerAnimationSettings settings(hiding_layer->GetAnimator()); |
| 145 settings.SetTransitionDuration(base::TimeDelta::FromMilliseconds( |
| 146 kFadingAnimationDurationInMS)); |
| 147 observer->AddNewAnimator(&settings); |
| 148 hiding_layer->SetOpacity(0.0f); |
| 149 hiding_layer->SetVisible(false); |
| 150 } |
| 151 } |
| 152 |
| 153 void OutputConfiguratorAnimation::ClearHidingLayers() { |
| 154 STLDeleteContainerPairSecondPointers( |
| 155 hiding_layers_.begin(), hiding_layers_.end()); |
| 156 hiding_layers_.clear(); |
| 157 } |
| 158 |
| 159 } // namespace internal |
| 160 } // namespace ash |
OLD | NEW |