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/stacking_controller.h"
7 #include "ash/display/display_controller.h"
8 #include "ash/session_state_delegate.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/wm/always_on_top_controller.h"
12 #include "ash/wm/coordinate_conversion.h"
13 #include "ash/wm/window_properties.h"
14 #include "ui/aura/client/aura_constants.h"
15 #include "ui/aura/root_window.h"
16 #include "ui/aura/window.h"
17 #include "ui/base/ui_base_types.h"
22 // Find a root window that matches the |bounds|. If the virtual screen
23 // coordinates is enabled and the bounds is specified, the root window
24 // that matches the window's bound will be used. Otherwise, it'll
25 // return the active root window.
26 aura::RootWindow
* FindContainerRoot(const gfx::Rect
& bounds
) {
27 if (bounds
.x() == 0 && bounds
.y() == 0 && bounds
.IsEmpty())
28 return Shell::GetActiveRootWindow();
29 return wm::GetRootWindowMatching(bounds
);
32 aura::Window
* GetContainerById(aura::RootWindow
* root
, int id
) {
33 return Shell::GetContainer(root
, id
);
36 aura::Window
* GetContainerForWindow(aura::Window
* window
) {
37 aura::Window
* container
= window
->parent();
38 while (container
&& container
->type() != aura::client::WINDOW_TYPE_UNKNOWN
)
39 container
= container
->parent();
43 bool IsSystemModal(aura::Window
* window
) {
44 return window
->GetProperty(aura::client::kModalKey
) == ui::MODAL_TYPE_SYSTEM
;
47 bool HasTransientParentWindow(aura::Window
* window
) {
48 return window
->transient_parent() &&
49 window
->transient_parent()->type() != aura::client::WINDOW_TYPE_UNKNOWN
;
52 bool IsPanelAttached(aura::Window
* window
) {
53 return window
->GetProperty(internal::kPanelAttachedKey
);
58 ////////////////////////////////////////////////////////////////////////////////
59 // StackingController, public:
61 StackingController::StackingController() {
64 StackingController::~StackingController() {
67 ////////////////////////////////////////////////////////////////////////////////
68 // StackingController, aura::StackingClient implementation:
70 aura::Window
* StackingController::GetDefaultParent(aura::Window
* context
,
72 const gfx::Rect
& bounds
) {
73 aura::RootWindow
* target_root
= NULL
;
74 if (window
->transient_parent()) {
75 // Transient window should use the same root as its transient parent.
76 target_root
= window
->transient_parent()->GetRootWindow();
78 target_root
= FindContainerRoot(bounds
);
81 switch (window
->type()) {
82 case aura::client::WINDOW_TYPE_NORMAL
:
83 case aura::client::WINDOW_TYPE_POPUP
:
84 if (IsSystemModal(window
))
85 return GetSystemModalContainer(target_root
, window
);
86 else if (HasTransientParentWindow(window
))
87 return GetContainerForWindow(window
->transient_parent());
88 return GetAlwaysOnTopController(target_root
)->GetContainer(window
);
89 case aura::client::WINDOW_TYPE_CONTROL
:
90 return GetContainerById(
91 target_root
, internal::kShellWindowId_UnparentedControlContainer
);
92 case aura::client::WINDOW_TYPE_PANEL
:
93 if (IsPanelAttached(window
))
94 return GetContainerById(target_root
,
95 internal::kShellWindowId_PanelContainer
);
97 return GetAlwaysOnTopController(target_root
)->GetContainer(window
);
98 case aura::client::WINDOW_TYPE_MENU
:
99 return GetContainerById(
100 target_root
, internal::kShellWindowId_MenuContainer
);
101 case aura::client::WINDOW_TYPE_TOOLTIP
:
102 return GetContainerById(
103 target_root
, internal::kShellWindowId_DragImageAndTooltipContainer
);
105 NOTREACHED() << "Window " << window
->id()
106 << " has unhandled type " << window
->type();
112 ////////////////////////////////////////////////////////////////////////////////
113 // StackingController, private:
115 aura::Window
* StackingController::GetSystemModalContainer(
116 aura::RootWindow
* root
,
117 aura::Window
* window
) const {
118 DCHECK(IsSystemModal(window
));
120 // If screen lock is not active and user session is active,
121 // all modal windows are placed into the normal modal container.
122 // In case of missing transient parent (it could happen for alerts from
123 // background pages) assume that the window belongs to user session.
124 SessionStateDelegate
* session_state_delegate
=
125 Shell::GetInstance()->session_state_delegate();
126 if ((!session_state_delegate
->IsScreenLocked() &&
127 session_state_delegate
->IsActiveUserSessionStarted()) ||
128 !window
->transient_parent()) {
129 return GetContainerById(root
,
130 internal::kShellWindowId_SystemModalContainer
);
133 // Otherwise those that originate from LockScreen container and above are
134 // placed in the screen lock modal container.
135 int window_container_id
= window
->transient_parent()->parent()->id();
136 aura::Window
* container
= NULL
;
137 if (window_container_id
< internal::kShellWindowId_LockScreenContainer
) {
138 container
= GetContainerById(
139 root
, internal::kShellWindowId_SystemModalContainer
);
141 container
= GetContainerById(
142 root
, internal::kShellWindowId_LockSystemModalContainer
);
148 // TODO(oshima): Remove this once extended desktop is on by default.
149 internal::AlwaysOnTopController
*
150 StackingController::GetAlwaysOnTopController(aura::RootWindow
* root_window
) {
151 internal::AlwaysOnTopController
* controller
=
152 root_window
->GetProperty(internal::kAlwaysOnTopControllerKey
);
154 controller
= new internal::AlwaysOnTopController
;
155 controller
->SetAlwaysOnTopContainer(
156 root_window
->GetChildById(
157 internal::kShellWindowId_AlwaysOnTopContainer
));
158 // RootWindow owns the AlwaysOnTopController object.
159 root_window
->SetProperty(internal::kAlwaysOnTopControllerKey
, controller
);