OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "ui/aura_shell/modal_container_layout_manager.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "ui/aura/client/aura_constants.h" |
| 9 #include "ui/aura/desktop.h" |
| 10 #include "ui/aura/event.h" |
| 11 #include "ui/aura/window.h" |
| 12 #include "ui/aura_shell/modality_event_filter.h" |
| 13 #include "ui/aura_shell/shell.h" |
| 14 #include "ui/aura_shell/stacking_controller.h" |
| 15 #include "ui/gfx/canvas.h" |
| 16 #include "ui/gfx/compositor/layer.h" |
| 17 #include "ui/gfx/compositor/layer_animator.h" |
| 18 #include "views/view.h" |
| 19 #include "views/widget/widget.h" |
| 20 |
| 21 namespace aura_shell { |
| 22 namespace internal { |
| 23 |
| 24 namespace { |
| 25 |
| 26 class ScreenView : public views::View { |
| 27 public: |
| 28 ScreenView() {} |
| 29 virtual ~ScreenView() {} |
| 30 |
| 31 // Overridden from views::View: |
| 32 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { |
| 33 canvas->FillRect(SK_ColorBLACK, GetLocalBounds()); |
| 34 } |
| 35 |
| 36 private: |
| 37 DISALLOW_COPY_AND_ASSIGN(ScreenView); |
| 38 }; |
| 39 |
| 40 } // namespace |
| 41 |
| 42 //////////////////////////////////////////////////////////////////////////////// |
| 43 // ModalContainerLayoutManager, public: |
| 44 |
| 45 ModalContainerLayoutManager::ModalContainerLayoutManager( |
| 46 aura::Window* container) |
| 47 : container_(container), |
| 48 modal_screen_(NULL), |
| 49 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 50 modality_filter_(new ModalityEventFilter(container, this))) { |
| 51 } |
| 52 |
| 53 ModalContainerLayoutManager::~ModalContainerLayoutManager() { |
| 54 } |
| 55 |
| 56 //////////////////////////////////////////////////////////////////////////////// |
| 57 // ModalContainerLayoutManager, aura::LayoutManager implementation: |
| 58 |
| 59 void ModalContainerLayoutManager::OnWindowResized() { |
| 60 if (modal_screen_) { |
| 61 modal_screen_->SetBounds(gfx::Rect(0, 0, container_->bounds().width(), |
| 62 container_->bounds().height())); |
| 63 } |
| 64 } |
| 65 |
| 66 void ModalContainerLayoutManager::OnWindowAddedToLayout( |
| 67 aura::Window* child) { |
| 68 child->AddObserver(this); |
| 69 if (child->GetIntProperty(aura::kModalKey)) |
| 70 AddModalWindow(child); |
| 71 } |
| 72 |
| 73 void ModalContainerLayoutManager::OnWillRemoveWindowFromLayout( |
| 74 aura::Window* child) { |
| 75 child->RemoveObserver(this); |
| 76 if (child->GetIntProperty(aura::kModalKey)) |
| 77 RemoveModalWindow(child); |
| 78 } |
| 79 |
| 80 void ModalContainerLayoutManager::OnChildWindowVisibilityChanged( |
| 81 aura::Window* child, |
| 82 bool visible) { |
| 83 } |
| 84 |
| 85 void ModalContainerLayoutManager::SetChildBounds( |
| 86 aura::Window* child, |
| 87 const gfx::Rect& requested_bounds) { |
| 88 SetChildBoundsDirect(child, requested_bounds); |
| 89 } |
| 90 |
| 91 //////////////////////////////////////////////////////////////////////////////// |
| 92 // ModalContainerLayoutManager, aura::WindowObserver implementation: |
| 93 |
| 94 void ModalContainerLayoutManager::OnPropertyChanged(aura::Window* window, |
| 95 const char* key, |
| 96 void* old) { |
| 97 if (key != aura::kModalKey) |
| 98 return; |
| 99 |
| 100 if (window->GetIntProperty(aura::kModalKey)) { |
| 101 AddModalWindow(window); |
| 102 } else if (reinterpret_cast<bool>(old)) { |
| 103 RemoveModalWindow(window); |
| 104 } |
| 105 } |
| 106 |
| 107 //////////////////////////////////////////////////////////////////////////////// |
| 108 // ModalContainerLayoutManager, ui::LayerAnimationObserver implementation: |
| 109 |
| 110 void ModalContainerLayoutManager::OnLayerAnimationEnded( |
| 111 const ui::LayerAnimationSequence* sequence) { |
| 112 if (modal_screen_ && !modal_screen_->GetNativeView()->layer()->ShouldDraw()) |
| 113 DestroyModalScreen(); |
| 114 } |
| 115 |
| 116 void ModalContainerLayoutManager::OnLayerAnimationAborted( |
| 117 const ui::LayerAnimationSequence* sequence) { |
| 118 } |
| 119 |
| 120 void ModalContainerLayoutManager::OnLayerAnimationScheduled( |
| 121 const ui::LayerAnimationSequence* sequence) { |
| 122 } |
| 123 |
| 124 //////////////////////////////////////////////////////////////////////////////// |
| 125 // ModalContainerLayoutManager, ModalityEventFilter::Delegate implementation: |
| 126 |
| 127 bool ModalContainerLayoutManager::CanWindowReceiveEvents( |
| 128 aura::Window* window) { |
| 129 return StackingController::GetActivatableWindow(window) == modal_window(); |
| 130 } |
| 131 |
| 132 //////////////////////////////////////////////////////////////////////////////// |
| 133 // ModalContainerLayoutManager, private: |
| 134 |
| 135 void ModalContainerLayoutManager::AddModalWindow(aura::Window* window) { |
| 136 modal_windows_.push_back(window); |
| 137 CreateModalScreen(); |
| 138 container_->MoveChildToFront(window); |
| 139 window->Activate(); |
| 140 } |
| 141 |
| 142 void ModalContainerLayoutManager::RemoveModalWindow(aura::Window* window) { |
| 143 aura::Window::Windows::iterator it = |
| 144 std::find(modal_windows_.begin(), modal_windows_.end(), window); |
| 145 if (it != modal_windows_.end()) |
| 146 modal_windows_.erase(it); |
| 147 |
| 148 if (modal_windows_.empty()) |
| 149 HideModalScreen(); |
| 150 else |
| 151 modal_window()->Activate(); |
| 152 } |
| 153 |
| 154 void ModalContainerLayoutManager::CreateModalScreen() { |
| 155 if (modal_screen_) |
| 156 return; |
| 157 modal_screen_ = new views::Widget; |
| 158 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); |
| 159 params.parent = container_; |
| 160 params.bounds = gfx::Rect(0, 0, container_->bounds().width(), |
| 161 container_->bounds().height()); |
| 162 modal_screen_->Init(params); |
| 163 modal_screen_->GetNativeView()->set_name( |
| 164 "ModalContainerLayoutManager.ModalScreen"); |
| 165 modal_screen_->SetContentsView(new ScreenView); |
| 166 modal_screen_->GetNativeView()->layer()->SetOpacity(0.0f); |
| 167 modal_screen_->GetNativeView()->layer()->GetAnimator()->AddObserver(this); |
| 168 |
| 169 Shell::GetInstance()->AddDesktopEventFilter(modality_filter_.get()); |
| 170 |
| 171 ui::LayerAnimator::ScopedSettings settings( |
| 172 modal_screen_->GetNativeView()->layer()->GetAnimator()); |
| 173 modal_screen_->Show(); |
| 174 modal_screen_->GetNativeView()->layer()->SetOpacity(0.5f); |
| 175 container_->MoveChildToFront(modal_screen_->GetNativeView()); |
| 176 } |
| 177 |
| 178 void ModalContainerLayoutManager::DestroyModalScreen() { |
| 179 modal_screen_->GetNativeView()->layer()->GetAnimator()->RemoveObserver(this); |
| 180 modal_screen_->Close(); |
| 181 modal_screen_ = NULL; |
| 182 } |
| 183 |
| 184 void ModalContainerLayoutManager::HideModalScreen() { |
| 185 Shell::GetInstance()->RemoveDesktopEventFilter(modality_filter_.get()); |
| 186 ui::LayerAnimator::ScopedSettings settings( |
| 187 modal_screen_->GetNativeView()->layer()->GetAnimator()); |
| 188 modal_screen_->GetNativeView()->layer()->SetOpacity(0.0f); |
| 189 } |
| 190 |
| 191 } // namespace internal |
| 192 } // namespace aura_shell |
OLD | NEW |