Revert 222512 "Group WM related properties to ash::wm::WindowSet..."
[chromium-blink-merge.git] / ash / wm / stacking_controller.cc
blob4476a86a7564ece6b4a4935913263f2318eedb03
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"
9 #include "ash/shell.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"
19 namespace ash {
20 namespace {
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();
40 return container;
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);
56 internal::AlwaysOnTopController*
57 GetAlwaysOnTopController(aura::RootWindow* root_window) {
58 return internal::GetRootWindowController(root_window)->
59 always_on_top_controller();
62 } // namespace
64 ////////////////////////////////////////////////////////////////////////////////
65 // StackingController, public:
67 StackingController::StackingController() {
70 StackingController::~StackingController() {
73 ////////////////////////////////////////////////////////////////////////////////
74 // StackingController, aura::StackingClient implementation:
76 aura::Window* StackingController::GetDefaultParent(aura::Window* context,
77 aura::Window* window,
78 const gfx::Rect& bounds) {
79 aura::RootWindow* target_root = NULL;
80 if (window->transient_parent()) {
81 // Transient window should use the same root as its transient parent.
82 target_root = window->transient_parent()->GetRootWindow();
83 } else {
84 target_root = FindContainerRoot(bounds);
87 switch (window->type()) {
88 case aura::client::WINDOW_TYPE_NORMAL:
89 case aura::client::WINDOW_TYPE_POPUP:
90 if (IsSystemModal(window))
91 return GetSystemModalContainer(target_root, window);
92 else if (HasTransientParentWindow(window))
93 return GetContainerForWindow(window->transient_parent());
94 return GetAlwaysOnTopController(target_root)->GetContainer(window);
95 case aura::client::WINDOW_TYPE_CONTROL:
96 return GetContainerById(
97 target_root, internal::kShellWindowId_UnparentedControlContainer);
98 case aura::client::WINDOW_TYPE_PANEL:
99 if (IsPanelAttached(window))
100 return GetContainerById(target_root,
101 internal::kShellWindowId_PanelContainer);
102 else
103 return GetAlwaysOnTopController(target_root)->GetContainer(window);
104 case aura::client::WINDOW_TYPE_MENU:
105 return GetContainerById(
106 target_root, internal::kShellWindowId_MenuContainer);
107 case aura::client::WINDOW_TYPE_TOOLTIP:
108 return GetContainerById(
109 target_root, internal::kShellWindowId_DragImageAndTooltipContainer);
110 default:
111 NOTREACHED() << "Window " << window->id()
112 << " has unhandled type " << window->type();
113 break;
115 return NULL;
118 ////////////////////////////////////////////////////////////////////////////////
119 // StackingController, private:
121 aura::Window* StackingController::GetSystemModalContainer(
122 aura::RootWindow* root,
123 aura::Window* window) const {
124 DCHECK(IsSystemModal(window));
126 // If screen lock is not active and user session is active,
127 // all modal windows are placed into the normal modal container.
128 // In case of missing transient parent (it could happen for alerts from
129 // background pages) assume that the window belongs to user session.
130 SessionStateDelegate* session_state_delegate =
131 Shell::GetInstance()->session_state_delegate();
132 if (!session_state_delegate->IsUserSessionBlocked() ||
133 !window->transient_parent()) {
134 return GetContainerById(root,
135 internal::kShellWindowId_SystemModalContainer);
138 // Otherwise those that originate from LockScreen container and above are
139 // placed in the screen lock modal container.
140 int window_container_id = window->transient_parent()->parent()->id();
141 aura::Window* container = NULL;
142 if (window_container_id < internal::kShellWindowId_LockScreenContainer) {
143 container = GetContainerById(
144 root, internal::kShellWindowId_SystemModalContainer);
145 } else {
146 container = GetContainerById(
147 root, internal::kShellWindowId_LockSystemModalContainer);
150 return container;
153 } // namespace ash