Cleanup: move AlwaysOnTopController to RootWindowController
[chromium-blink-merge.git] / ash / wm / stacking_controller.cc
blob24f38d5f4b82d4905f9be5db6458fa468007a7a3
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/property_util.h"
14 #include "ash/wm/window_properties.h"
15 #include "ui/aura/client/aura_constants.h"
16 #include "ui/aura/root_window.h"
17 #include "ui/aura/window.h"
18 #include "ui/base/ui_base_types.h"
20 namespace ash {
21 namespace {
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::RootWindow* FindContainerRoot(const gfx::Rect& bounds) {
28 if (bounds.x() == 0 && bounds.y() == 0 && bounds.IsEmpty())
29 return Shell::GetActiveRootWindow();
30 return wm::GetRootWindowMatching(bounds);
33 aura::Window* GetContainerById(aura::RootWindow* root, int id) {
34 return Shell::GetContainer(root, id);
37 aura::Window* GetContainerForWindow(aura::Window* window) {
38 aura::Window* container = window->parent();
39 while (container && container->type() != aura::client::WINDOW_TYPE_UNKNOWN)
40 container = container->parent();
41 return container;
44 bool IsSystemModal(aura::Window* window) {
45 return window->GetProperty(aura::client::kModalKey) == ui::MODAL_TYPE_SYSTEM;
48 bool HasTransientParentWindow(aura::Window* window) {
49 return window->transient_parent() &&
50 window->transient_parent()->type() != aura::client::WINDOW_TYPE_UNKNOWN;
53 bool IsPanelAttached(aura::Window* window) {
54 return window->GetProperty(internal::kPanelAttachedKey);
57 internal::AlwaysOnTopController*
58 GetAlwaysOnTopController(aura::RootWindow* root_window) {
59 return GetRootWindowController(root_window)->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->IsScreenLocked() &&
133 session_state_delegate->IsActiveUserSessionStarted()) ||
134 !window->transient_parent()) {
135 return GetContainerById(root,
136 internal::kShellWindowId_SystemModalContainer);
139 // Otherwise those that originate from LockScreen container and above are
140 // placed in the screen lock modal container.
141 int window_container_id = window->transient_parent()->parent()->id();
142 aura::Window* container = NULL;
143 if (window_container_id < internal::kShellWindowId_LockScreenContainer) {
144 container = GetContainerById(
145 root, internal::kShellWindowId_SystemModalContainer);
146 } else {
147 container = GetContainerById(
148 root, internal::kShellWindowId_LockSystemModalContainer);
151 return container;
154 } // namespace ash