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.
5 #include "ash/wm/system_modal_container_layout_manager.h"
9 #include "ash/session/session_state_delegate.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/wm/system_modal_container_event_filter.h"
13 #include "ash/wm/window_animations.h"
14 #include "ash/wm/window_util.h"
15 #include "base/bind.h"
16 #include "ui/aura/client/aura_constants.h"
17 #include "ui/aura/client/capture_client.h"
18 #include "ui/aura/window.h"
19 #include "ui/aura/window_event_dispatcher.h"
20 #include "ui/aura/window_property.h"
21 #include "ui/base/ui_base_switches_util.h"
22 #include "ui/compositor/layer.h"
23 #include "ui/compositor/layer_animator.h"
24 #include "ui/compositor/scoped_layer_animation_settings.h"
25 #include "ui/events/event.h"
26 #include "ui/gfx/screen.h"
27 #include "ui/keyboard/keyboard_controller.h"
28 #include "ui/views/background.h"
29 #include "ui/views/view.h"
30 #include "ui/views/widget/widget.h"
31 #include "ui/wm/core/compound_event_filter.h"
35 // If this is set to true, the window will get centered.
36 DEFINE_WINDOW_PROPERTY_KEY(bool, kCenteredKey
, false);
38 // The center point of the window can diverge this much from the center point
39 // of the container to be kept centered upon resizing operations.
40 const int kCenterPixelDelta
= 32;
42 ////////////////////////////////////////////////////////////////////////////////
43 // SystemModalContainerLayoutManager, public:
45 SystemModalContainerLayoutManager::SystemModalContainerLayoutManager(
46 aura::Window
* container
)
47 : SnapToPixelLayoutManager(container
),
48 container_(container
),
49 modal_background_(NULL
) {
52 SystemModalContainerLayoutManager::~SystemModalContainerLayoutManager() {
55 ////////////////////////////////////////////////////////////////////////////////
56 // SystemModalContainerLayoutManager, aura::LayoutManager implementation:
58 void SystemModalContainerLayoutManager::OnWindowResized() {
59 if (modal_background_
) {
60 // Note: we have to set the entire bounds with the screen offset.
61 modal_background_
->SetBounds(
62 Shell::GetScreen()->GetDisplayNearestWindow(container_
).bounds());
64 PositionDialogsAfterWorkAreaResize();
67 void SystemModalContainerLayoutManager::OnWindowAddedToLayout(
68 aura::Window
* child
) {
69 DCHECK((modal_background_
&& child
== modal_background_
->GetNativeView()) ||
70 child
->type() == ui::wm::WINDOW_TYPE_NORMAL
||
71 child
->type() == ui::wm::WINDOW_TYPE_POPUP
);
73 container_
->id() != kShellWindowId_LockSystemModalContainer
||
74 Shell::GetInstance()->session_state_delegate()->IsUserSessionBlocked());
76 child
->AddObserver(this);
77 if (child
->GetProperty(aura::client::kModalKey
) != ui::MODAL_TYPE_NONE
)
78 AddModalWindow(child
);
81 void SystemModalContainerLayoutManager::OnWillRemoveWindowFromLayout(
82 aura::Window
* child
) {
83 child
->RemoveObserver(this);
84 if (child
->GetProperty(aura::client::kModalKey
) != ui::MODAL_TYPE_NONE
)
85 RemoveModalWindow(child
);
88 void SystemModalContainerLayoutManager::SetChildBounds(
90 const gfx::Rect
& requested_bounds
) {
91 SnapToPixelLayoutManager::SetChildBounds(child
, requested_bounds
);
92 child
->SetProperty(kCenteredKey
, DialogIsCentered(requested_bounds
));
95 ////////////////////////////////////////////////////////////////////////////////
96 // SystemModalContainerLayoutManager, aura::WindowObserver implementation:
98 void SystemModalContainerLayoutManager::OnWindowPropertyChanged(
102 if (key
!= aura::client::kModalKey
)
105 if (window
->GetProperty(aura::client::kModalKey
) != ui::MODAL_TYPE_NONE
) {
106 AddModalWindow(window
);
107 } else if (static_cast<ui::ModalType
>(old
) != ui::MODAL_TYPE_NONE
) {
108 RemoveModalWindow(window
);
109 Shell::GetInstance()->OnModalWindowRemoved(window
);
113 void SystemModalContainerLayoutManager::OnWindowDestroying(
114 aura::Window
* window
) {
115 if (modal_background_
&& modal_background_
->GetNativeView() == window
)
116 modal_background_
= NULL
;
119 ////////////////////////////////////////////////////////////////////////////////
120 // SystemModalContainerLayoutManager, Keyboard::KeybaordControllerObserver
123 void SystemModalContainerLayoutManager::OnKeyboardBoundsChanging(
124 const gfx::Rect
& new_bounds
) {
125 PositionDialogsAfterWorkAreaResize();
128 bool SystemModalContainerLayoutManager::CanWindowReceiveEvents(
129 aura::Window
* window
) {
130 // We could get when we're at lock screen and there is modal window at
131 // system modal window layer which added event filter.
132 // Now this lock modal windows layer layout manager should not block events
133 // for windows at lock layer.
134 // See SystemModalContainerLayoutManagerTest.EventFocusContainers and
135 // http://crbug.com/157469
136 if (modal_windows_
.empty())
138 // This container can not handle events if the screen is locked and it is not
139 // above the lock screen layer (crbug.com/110920).
140 if (Shell::GetInstance()->session_state_delegate()->IsUserSessionBlocked() &&
141 container_
->id() < ash::kShellWindowId_LockScreenContainer
)
143 return wm::GetActivatableWindow(window
) == modal_window();
146 bool SystemModalContainerLayoutManager::ActivateNextModalWindow() {
147 if (modal_windows_
.empty())
149 wm::ActivateWindow(modal_window());
153 void SystemModalContainerLayoutManager::CreateModalBackground() {
154 if (!modal_background_
) {
155 modal_background_
= new views::Widget
;
156 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_CONTROL
);
157 params
.parent
= container_
;
158 params
.bounds
= Shell::GetScreen()->GetDisplayNearestWindow(
159 container_
).bounds();
160 modal_background_
->Init(params
);
161 modal_background_
->GetNativeView()->SetName(
162 "SystemModalContainerLayoutManager.ModalBackground");
163 views::View
* contents_view
= new views::View();
164 // TODO(jamescook): This could be SK_ColorWHITE for the new dialog style.
165 contents_view
->set_background(
166 views::Background::CreateSolidBackground(SK_ColorBLACK
));
167 modal_background_
->SetContentsView(contents_view
);
168 modal_background_
->GetNativeView()->layer()->SetOpacity(0.0f
);
169 // There isn't always a keyboard controller.
170 if (keyboard::KeyboardController::GetInstance())
171 keyboard::KeyboardController::GetInstance()->AddObserver(this);
174 ui::ScopedLayerAnimationSettings
settings(
175 modal_background_
->GetNativeView()->layer()->GetAnimator());
176 // Show should not be called with a target opacity of 0. We therefore start
177 // the fade to show animation before Show() is called.
178 modal_background_
->GetNativeView()->layer()->SetOpacity(0.5f
);
179 modal_background_
->Show();
180 container_
->StackChildAtTop(modal_background_
->GetNativeView());
183 void SystemModalContainerLayoutManager::DestroyModalBackground() {
184 // modal_background_ can be NULL when a root window is shutting down
185 // and OnWindowDestroying is called first.
186 if (modal_background_
) {
187 if (keyboard::KeyboardController::GetInstance())
188 keyboard::KeyboardController::GetInstance()->RemoveObserver(this);
189 ::wm::ScopedHidingAnimationSettings
settings(
190 modal_background_
->GetNativeView());
191 modal_background_
->Close();
192 modal_background_
->GetNativeView()->layer()->SetOpacity(0.0f
);
193 modal_background_
= NULL
;
198 bool SystemModalContainerLayoutManager::IsModalBackground(
199 aura::Window
* window
) {
200 int id
= window
->parent()->id();
201 if (id
!= kShellWindowId_SystemModalContainer
&&
202 id
!= kShellWindowId_LockSystemModalContainer
)
204 SystemModalContainerLayoutManager
* layout_manager
=
205 static_cast<SystemModalContainerLayoutManager
*>(
206 window
->parent()->layout_manager());
207 return layout_manager
->modal_background_
&&
208 layout_manager
->modal_background_
->GetNativeWindow() == window
;
211 ////////////////////////////////////////////////////////////////////////////////
212 // SystemModalContainerLayoutManager, private:
214 void SystemModalContainerLayoutManager::AddModalWindow(aura::Window
* window
) {
215 if (modal_windows_
.empty()) {
216 aura::Window
* capture_window
= aura::client::GetCaptureWindow(container_
);
218 capture_window
->ReleaseCapture();
220 modal_windows_
.push_back(window
);
221 Shell::GetInstance()->CreateModalBackground(window
);
222 window
->parent()->StackChildAtTop(window
);
224 gfx::Rect target_bounds
= window
->bounds();
225 target_bounds
.AdjustToFit(GetUsableDialogArea());
226 window
->SetBounds(target_bounds
);
229 void SystemModalContainerLayoutManager::RemoveModalWindow(
230 aura::Window
* window
) {
231 aura::Window::Windows::iterator it
=
232 std::find(modal_windows_
.begin(), modal_windows_
.end(), window
);
233 if (it
!= modal_windows_
.end())
234 modal_windows_
.erase(it
);
237 void SystemModalContainerLayoutManager::PositionDialogsAfterWorkAreaResize() {
238 if (!modal_windows_
.empty()) {
239 for (aura::Window::Windows::iterator it
= modal_windows_
.begin();
240 it
!= modal_windows_
.end(); ++it
) {
241 (*it
)->SetBounds(GetCenteredAndOrFittedBounds(*it
));
246 gfx::Rect
SystemModalContainerLayoutManager::GetUsableDialogArea() {
247 // Instead of resizing the system modal container, we move only the modal
248 // windows. This way we avoid flashing lines upon resize animation and if the
249 // keyboard will not fill left to right, the background is still covered.
250 gfx::Rect valid_bounds
= container_
->bounds();
251 keyboard::KeyboardController
* keyboard_controller
=
252 keyboard::KeyboardController::GetInstance();
253 if (keyboard_controller
) {
254 gfx::Rect bounds
= keyboard_controller
->current_keyboard_bounds();
255 if (!bounds
.IsEmpty()) {
256 valid_bounds
.set_height(std::max(
257 0, valid_bounds
.height() - bounds
.height()));
263 gfx::Rect
SystemModalContainerLayoutManager::GetCenteredAndOrFittedBounds(
264 const aura::Window
* window
) {
265 gfx::Rect target_bounds
;
266 gfx::Rect usable_area
= GetUsableDialogArea();
267 if (window
->GetProperty(kCenteredKey
)) {
268 // Keep the dialog centered if it was centered before.
269 target_bounds
= usable_area
;
270 target_bounds
.ClampToCenteredSize(window
->bounds().size());
272 // Keep the dialog within the usable area.
273 target_bounds
= window
->bounds();
274 target_bounds
.AdjustToFit(usable_area
);
276 if (usable_area
!= container_
->bounds()) {
277 // Don't clamp the dialog for the keyboard. Keep the size as it is but make
278 // sure that the top remains visible.
279 // TODO(skuhne): M37 should add over scroll functionality to address this.
280 target_bounds
.set_size(window
->bounds().size());
282 return target_bounds
;
285 bool SystemModalContainerLayoutManager::DialogIsCentered(
286 const gfx::Rect
& window_bounds
) {
287 gfx::Point window_center
= window_bounds
.CenterPoint();
288 gfx::Point container_center
= GetUsableDialogArea().CenterPoint();
290 std::abs(window_center
.x() - container_center
.x()) < kCenterPixelDelta
&&
291 std::abs(window_center
.y() - container_center
.y()) < kCenterPixelDelta
;