Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ash / wm / stacking_controller.cc
blobbbafab48b6d33324266f2b08e3551ba873ffa07e
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/shell.h"
9 #include "ash/shell_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"
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 } // namespace
58 ////////////////////////////////////////////////////////////////////////////////
59 // StackingController, public:
61 StackingController::StackingController() {
64 StackingController::~StackingController() {
67 ////////////////////////////////////////////////////////////////////////////////
68 // StackingController, aura::StackingClient implementation:
70 aura::Window* StackingController::GetDefaultParent(aura::Window* context,
71 aura::Window* window,
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();
77 } else {
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);
96 else
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);
104 default:
105 NOTREACHED() << "Window " << window->id()
106 << " has unhandled type " << window->type();
107 break;
109 return NULL;
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 if (!Shell::GetInstance()->delegate()->IsScreenLocked() &&
123 Shell::GetInstance()->delegate()->IsSessionStarted()) {
124 return GetContainerById(root,
125 internal::kShellWindowId_SystemModalContainer);
128 // Otherwise those that originate from LockScreen container and above are
129 // placed in the screen lock modal container.
130 aura::Window* lock_container =
131 GetContainerById(root, internal::kShellWindowId_LockScreenContainer);
132 int lock_container_id = lock_container->id();
133 int window_container_id = window->transient_parent()->parent()->id();
135 aura::Window* container = NULL;
136 if (window_container_id < lock_container_id) {
137 container = GetContainerById(
138 root, internal::kShellWindowId_SystemModalContainer);
139 } else {
140 container = GetContainerById(
141 root, internal::kShellWindowId_LockSystemModalContainer);
144 return container;
147 // TODO(oshima): Remove this once extended desktop is on by default.
148 internal::AlwaysOnTopController*
149 StackingController::GetAlwaysOnTopController(aura::RootWindow* root_window) {
150 internal::AlwaysOnTopController* controller =
151 root_window->GetProperty(internal::kAlwaysOnTopControllerKey);
152 if (!controller) {
153 controller = new internal::AlwaysOnTopController;
154 controller->SetAlwaysOnTopContainer(
155 root_window->GetChildById(
156 internal::kShellWindowId_AlwaysOnTopContainer));
157 // RootWindow owns the AlwaysOnTopController object.
158 root_window->SetProperty(internal::kAlwaysOnTopControllerKey, controller);
160 return controller;
163 } // namespace ash