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
ash / wm / always_on_top_controller.cc [blame]
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/wm/always_on_top_controller.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/wm/desks/desk.h"
#include "ash/wm/desks/desks_controller.h"
#include "ash/wm/desks/desks_util.h"
#include "ash/wm/window_state.h"
#include "ash/wm/workspace/workspace_layout_manager.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
namespace ash {
DEFINE_UI_CLASS_PROPERTY_KEY(bool, kDisallowReparentKey, false)
AlwaysOnTopController::AlwaysOnTopController(
aura::Window* always_on_top_container,
aura::Window* pip_container)
: always_on_top_container_(always_on_top_container),
pip_container_(pip_container) {
DCHECK(!desks_util::IsDeskContainer(always_on_top_container_));
DCHECK(!desks_util::IsDeskContainer(pip_container_));
always_on_top_container_->SetLayoutManager(
std::make_unique<WorkspaceLayoutManager>(always_on_top_container_));
pip_container_->SetLayoutManager(
std::make_unique<WorkspaceLayoutManager>(pip_container_));
// Container should be empty.
DCHECK(always_on_top_container_->children().empty());
DCHECK(pip_container_->children().empty());
always_on_top_container_->AddObserver(this);
pip_container->AddObserver(this);
}
AlwaysOnTopController::~AlwaysOnTopController() {
// At this point, all windows should be removed and AlwaysOnTopController
// will have removed itself as an observer in OnWindowDestroying.
DCHECK(!always_on_top_container_);
DCHECK(!pip_container_);
}
// static
void AlwaysOnTopController::SetDisallowReparent(aura::Window* window) {
window->SetProperty(kDisallowReparentKey, true);
}
aura::Window* AlwaysOnTopController::GetContainer(aura::Window* window) const {
DCHECK(always_on_top_container_);
DCHECK(pip_container_);
// On other platforms, there are different window levels. For now, treat any
// window with non-normal level as "always on top". Perhaps the nuance of
// multiple levels will be needed later.
if (window->GetProperty(aura::client::kZOrderingKey) ==
ui::ZOrderLevel::kNormal) {
aura::Window* root = always_on_top_container_->GetRootWindow();
DesksController* desks_controller = DesksController::Get();
const std::string* desk_uuid_string =
window->GetProperty(aura::client::kDeskUuidKey);
if (desk_uuid_string) {
const base::Uuid desk_guid =
base::Uuid::ParseLowercase(*desk_uuid_string);
if (desk_guid.is_valid()) {
if (Desk* target_desk = desks_controller->GetDeskByUuid(desk_guid)) {
if (auto* container = target_desk->GetDeskContainerForRoot(root)) {
return container;
}
}
}
}
const int window_workspace =
window->GetProperty(aura::client::kWindowWorkspaceKey);
if (window_workspace != aura::client::kWindowWorkspaceUnassignedWorkspace) {
auto* desk_container =
desks_controller->GetDeskContainer(root, window_workspace);
if (desk_container)
return desk_container;
}
return desks_util::GetActiveDeskContainerForRoot(root);
}
if (window->parent() && WindowState::Get(window)->IsPip())
return pip_container_;
return always_on_top_container_;
}
void AlwaysOnTopController::ClearLayoutManagers() {
always_on_top_container_->SetLayoutManager(nullptr);
pip_container_->SetLayoutManager(nullptr);
}
void AlwaysOnTopController::SetLayoutManagerForTest(
std::unique_ptr<WorkspaceLayoutManager> layout_manager) {
always_on_top_container_->SetLayoutManager(std::move(layout_manager));
}
void AlwaysOnTopController::AddWindow(aura::Window* window) {
window->AddObserver(this);
WindowState::Get(window)->AddObserver(this);
}
void AlwaysOnTopController::RemoveWindow(aura::Window* window) {
window->RemoveObserver(this);
WindowState::Get(window)->RemoveObserver(this);
}
void AlwaysOnTopController::ReparentWindow(aura::Window* window) {
DCHECK(window->GetType() == aura::client::WINDOW_TYPE_NORMAL ||
window->GetType() == aura::client::WINDOW_TYPE_POPUP);
aura::Window* container = GetContainer(window);
if (window->parent() != container &&
!window->GetProperty(kDisallowReparentKey))
container->AddChild(window);
}
void AlwaysOnTopController::OnWindowHierarchyChanged(
const HierarchyChangeParams& params) {
if (params.old_parent == always_on_top_container_.get() ||
params.old_parent == pip_container_.get()) {
RemoveWindow(params.target);
}
if (params.new_parent == always_on_top_container_.get() ||
params.new_parent == pip_container_.get()) {
AddWindow(params.target);
}
}
void AlwaysOnTopController::OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) {
if (window != always_on_top_container_ && window != pip_container_ &&
key == aura::client::kZOrderingKey) {
ReparentWindow(window);
}
}
void AlwaysOnTopController::OnWindowDestroying(aura::Window* window) {
if (window == always_on_top_container_) {
always_on_top_container_->RemoveObserver(this);
always_on_top_container_ = nullptr;
} else if (window == pip_container_) {
pip_container_->RemoveObserver(this);
pip_container_ = nullptr;
} else {
RemoveWindow(window);
}
}
void AlwaysOnTopController::OnPreWindowStateTypeChange(
WindowState* window_state,
chromeos::WindowStateType old_type) {
ReparentWindow(window_state->window());
}
} // namespace ash