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/root_window_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_state.h"
14 #include "ui/aura/client/aura_constants.h"
15 #include "ui/aura/window.h"
16 #include "ui/aura/window_event_dispatcher.h"
17 #include "ui/base/ui_base_types.h"
18 #include "ui/views/corewm/window_util.h"
23 // Find a root window that matches the |bounds|. If the virtual screen
24 // coordinates is enabled and the bounds is specified, the root window
25 // that matches the window's bound will be used. Otherwise, it'll
26 // return the active root window.
27 aura::Window
* FindContainerRoot(const gfx::Rect
& bounds
) {
28 if (bounds
.x() == 0 && bounds
.y() == 0 && bounds
.IsEmpty())
29 return Shell::GetTargetRootWindow();
30 return wm::GetRootWindowMatching(bounds
);
33 aura::Window
* GetContainerById(aura::Window
* root
, int id
) {
34 return Shell::GetContainer(root
, id
);
37 bool IsSystemModal(aura::Window
* window
) {
38 return window
->GetProperty(aura::client::kModalKey
) == ui::MODAL_TYPE_SYSTEM
;
41 bool HasTransientParentWindow(const aura::Window
* window
) {
42 return views::corewm::GetTransientParent(window
) &&
43 views::corewm::GetTransientParent(window
)->type() !=
44 ui::wm::WINDOW_TYPE_UNKNOWN
;
47 internal::AlwaysOnTopController
*
48 GetAlwaysOnTopController(aura::Window
* root_window
) {
49 return internal::GetRootWindowController(root_window
)->
50 always_on_top_controller();
55 ////////////////////////////////////////////////////////////////////////////////
56 // StackingController, public:
58 StackingController::StackingController() {
61 StackingController::~StackingController() {
64 ////////////////////////////////////////////////////////////////////////////////
65 // StackingController, aura::client::WindowTreeClient implementation:
67 aura::Window
* StackingController::GetDefaultParent(aura::Window
* context
,
69 const gfx::Rect
& bounds
) {
70 aura::Window
* target_root
= NULL
;
71 aura::Window
* transient_parent
= views::corewm::GetTransientParent(window
);
72 if (transient_parent
) {
73 // Transient window should use the same root as its transient parent.
74 target_root
= transient_parent
->GetRootWindow();
76 target_root
= FindContainerRoot(bounds
);
79 switch (window
->type()) {
80 case ui::wm::WINDOW_TYPE_NORMAL
:
81 case ui::wm::WINDOW_TYPE_POPUP
:
82 if (IsSystemModal(window
))
83 return GetSystemModalContainer(target_root
, window
);
84 else if (HasTransientParentWindow(window
))
85 return internal::RootWindowController::GetContainerForWindow(
86 views::corewm::GetTransientParent(window
));
87 return GetAlwaysOnTopController(target_root
)->GetContainer(window
);
88 case ui::wm::WINDOW_TYPE_CONTROL
:
89 return GetContainerById(
90 target_root
, internal::kShellWindowId_UnparentedControlContainer
);
91 case ui::wm::WINDOW_TYPE_PANEL
:
92 if (wm::GetWindowState(window
)->panel_attached())
93 return GetContainerById(target_root
,
94 internal::kShellWindowId_PanelContainer
);
96 return GetAlwaysOnTopController(target_root
)->GetContainer(window
);
97 case ui::wm::WINDOW_TYPE_MENU
:
98 return GetContainerById(
99 target_root
, internal::kShellWindowId_MenuContainer
);
100 case ui::wm::WINDOW_TYPE_TOOLTIP
:
101 return GetContainerById(
102 target_root
, internal::kShellWindowId_DragImageAndTooltipContainer
);
104 NOTREACHED() << "Window " << window
->id()
105 << " has unhandled type " << window
->type();
111 ////////////////////////////////////////////////////////////////////////////////
112 // StackingController, private:
114 aura::Window
* StackingController::GetSystemModalContainer(
116 aura::Window
* window
) const {
117 DCHECK(IsSystemModal(window
));
119 // If screen lock is not active and user session is active,
120 // all modal windows are placed into the normal modal container.
121 // In case of missing transient parent (it could happen for alerts from
122 // background pages) assume that the window belongs to user session.
123 SessionStateDelegate
* session_state_delegate
=
124 Shell::GetInstance()->session_state_delegate();
125 if (!session_state_delegate
->IsUserSessionBlocked() ||
126 !views::corewm::GetTransientParent(window
)) {
127 return GetContainerById(root
,
128 internal::kShellWindowId_SystemModalContainer
);
131 // Otherwise those that originate from LockScreen container and above are
132 // placed in the screen lock modal container.
133 int window_container_id
=
134 views::corewm::GetTransientParent(window
)->parent()->id();
135 aura::Window
* container
= NULL
;
136 if (window_container_id
< internal::kShellWindowId_LockScreenContainer
) {
137 container
= GetContainerById(
138 root
, internal::kShellWindowId_SystemModalContainer
);
140 container
= GetContainerById(
141 root
, internal::kShellWindowId_LockSystemModalContainer
);