1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/wm/activation_controller.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/window_modality_controller.h"
#include "ash/wm/window_util.h"
#include "base/auto_reset.h"
#include "ui/aura/client/activation_delegate.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/base/ui_base_types.h"
namespace ash {
namespace internal {
namespace {
// These are the list of container ids of containers which may contain windows
// that need to be activated in the order that they should be activated.
const int kWindowContainerIds[] = {
kShellWindowId_LockSystemModalContainer,
kShellWindowId_LockScreenContainer,
kShellWindowId_SystemModalContainer,
kShellWindowId_AlwaysOnTopContainer,
kShellWindowId_SettingBubbleContainer,
kShellWindowId_DefaultContainer,
// Panel, launcher and status are intentionally checked after other
// containers even though these layers are higher. The user expects their
// windows to be focused before these elements.
kShellWindowId_PanelContainer,
kShellWindowId_LauncherContainer,
kShellWindowId_StatusContainer,
};
aura::Window* GetContainer(int id) {
return Shell::GetInstance()->GetContainer(id);
}
// Returns true if children of |window| can be activated.
// These are the only containers in which windows can receive focus.
bool SupportsChildActivation(aura::Window* window) {
for (size_t i = 0; i < arraysize(kWindowContainerIds); i++) {
if (window->id() == kWindowContainerIds[i])
return true;
}
return false;
}
// Returns true if |window| can be activated or deactivated.
// A window manager typically defines some notion of "top level window" that
// supports activation/deactivation.
bool CanActivateWindow(aura::Window* window) {
return window &&
window->IsVisible() &&
(!aura::client::GetActivationDelegate(window) ||
aura::client::GetActivationDelegate(window)->ShouldActivate(NULL)) &&
SupportsChildActivation(window->parent());
}
// When a modal window is activated, we bring its entire transient parent chain
// to the front. This function must be called before the modal transient is
// stacked at the top to ensure correct stacking order.
void StackTransientParentsBelowModalWindow(aura::Window* window) {
if (window->GetProperty(aura::client::kModalKey) != ui::MODAL_TYPE_WINDOW)
return;
aura::Window* transient_parent = window->transient_parent();
while (transient_parent) {
transient_parent->parent()->StackChildAtTop(transient_parent);
transient_parent = transient_parent->transient_parent();
}
}
} // namespace
////////////////////////////////////////////////////////////////////////////////
// ActivationController, public:
ActivationController::ActivationController()
: updating_activation_(false) {
aura::client::SetActivationClient(Shell::GetRootWindow(), this);
aura::Env::GetInstance()->AddObserver(this);
Shell::GetRootWindow()->AddRootWindowObserver(this);
}
ActivationController::~ActivationController() {
Shell::GetRootWindow()->RemoveRootWindowObserver(this);
aura::Env::GetInstance()->RemoveObserver(this);
}
// static
aura::Window* ActivationController::GetActivatableWindow(aura::Window* window) {
aura::Window* parent = window->parent();
aura::Window* child = window;
while (parent) {
if (SupportsChildActivation(parent))
return child;
// If |child| isn't activatable, but has transient parent, trace
// that path instead.
if (child->transient_parent())
return GetActivatableWindow(child->transient_parent());
parent = parent->parent();
child = child->parent();
}
return NULL;
}
////////////////////////////////////////////////////////////////////////////////
// ActivationController, aura::client::ActivationClient implementation:
void ActivationController::ActivateWindow(aura::Window* window) {
aura::Window* window_modal_transient =
WindowModalityController::GetWindowModalTransient(window);
if (window_modal_transient) {
ActivateWindow(window_modal_transient);
return;
}
// Prevent recursion when called from focus.
if (updating_activation_)
return;
AutoReset<bool> in_activate_window(&updating_activation_, true);
// Nothing may actually have changed.
aura::Window* old_active = GetActiveWindow();
if (old_active == window)
return;
// The stacking client may impose rules on what window configurations can be
// activated or deactivated.
if (window && !CanActivateWindow(window))
return;
// If the screen is locked, just bring the window to top so that
// it will be activated when the lock window is destroyed.
if (window && !window->CanReceiveEvents()) {
StackTransientParentsBelowModalWindow(window);
window->parent()->StackChildAtTop(window);
return;
}
if (window &&
!window->Contains(window->GetFocusManager()->GetFocusedWindow())) {
window->GetFocusManager()->SetFocusedWindow(window);
}
Shell::GetRootWindow()->SetProperty(aura::client::kRootWindowActiveWindowKey,
window);
// Invoke OnLostActive after we've changed the active window. That way if the
// delegate queries for active state it doesn't think the window is still
// active.
if (old_active && aura::client::GetActivationDelegate(old_active))
aura::client::GetActivationDelegate(old_active)->OnLostActive();
if (window) {
StackTransientParentsBelowModalWindow(window);
window->parent()->StackChildAtTop(window);
if (aura::client::GetActivationDelegate(window))
aura::client::GetActivationDelegate(window)->OnActivated();
}
}
void ActivationController::DeactivateWindow(aura::Window* window) {
if (window)
ActivateNextWindow(window);
}
aura::Window* ActivationController::GetActiveWindow() {
return Shell::GetRootWindow()->GetProperty(
aura::client::kRootWindowActiveWindowKey);
}
bool ActivationController::CanFocusWindow(aura::Window* window) const {
return CanActivateWindow(GetActivatableWindow(window));
}
////////////////////////////////////////////////////////////////////////////////
// ActivationController, aura::WindowObserver implementation:
void ActivationController::OnWindowVisibilityChanged(aura::Window* window,
bool visible) {
if (!visible)
ActivateNextWindow(window);
}
void ActivationController::OnWindowDestroying(aura::Window* window) {
if (wm::IsActiveWindow(window)) {
// Clear the property before activating something else, since
// ActivateWindow() will attempt to notify the window stored in this value
// otherwise.
Shell::GetRootWindow()->ClearProperty(
aura::client::kRootWindowActiveWindowKey);
ActivateWindow(GetTopmostWindowToActivate(window));
}
window->RemoveObserver(this);
}
////////////////////////////////////////////////////////////////////////////////
// ActivationController, aura::EnvObserver implementation:
void ActivationController::OnWindowInitialized(aura::Window* window) {
window->AddObserver(this);
}
////////////////////////////////////////////////////////////////////////////////
// ActivationController, aura::RootWindowObserver implementation:
void ActivationController::OnWindowFocused(aura::Window* window) {
ActivateWindow(GetActivatableWindow(window));
}
////////////////////////////////////////////////////////////////////////////////
// ActivationController, private:
void ActivationController::ActivateNextWindow(aura::Window* window) {
if (wm::IsActiveWindow(window))
ActivateWindow(GetTopmostWindowToActivate(window));
}
aura::Window* ActivationController::GetTopmostWindowToActivate(
aura::Window* ignore) const {
size_t current_container_index = 0;
// If the container of the window losing focus is in the list, start from that
// container.
for (size_t i = 0; ignore && i < arraysize(kWindowContainerIds); i++) {
aura::Window* container = GetContainer(kWindowContainerIds[i]);
if (container && container->Contains(ignore)) {
current_container_index = i;
break;
}
}
// Look for windows to focus in that container and below.
aura::Window* window = NULL;
for (; !window && current_container_index < arraysize(kWindowContainerIds);
current_container_index++) {
aura::Window* container =
GetContainer(kWindowContainerIds[current_container_index]);
if (container)
window = GetTopmostWindowToActivateInContainer(container, ignore);
}
return window;
}
aura::Window* ActivationController::GetTopmostWindowToActivateInContainer(
aura::Window* container,
aura::Window* ignore) const {
for (aura::Window::Windows::const_reverse_iterator i =
container->children().rbegin();
i != container->children().rend();
++i) {
if (*i != ignore && CanActivateWindow(*i))
return *i;
}
return NULL;
}
} // namespace internal
} // namespace ash
|