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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
ash / wm / multi_display / persistent_window_controller.cc [blame]
// Copyright 2018 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/multi_display/persistent_window_controller.h"
#include "base/memory/raw_ptr.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/wm/mru_window_tracker.h"
#include "ash/wm/multi_display/persistent_window_info.h"
#include "ash/wm/tablet_mode/scoped_skip_user_session_blocked_check.h"
#include "ash/wm/window_state.h"
#include "base/command_line.h"
#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "chromeos/ui/base/display_util.h"
#include "ui/display/manager/display_manager.h"
#include "ui/wm/core/coordinate_conversion.h"
namespace ash {
namespace {
// This controls the UMA histogram `kNumOfWindowsRestoredOnDisplayAdded` and
// `kNumOfWindowsRestoredOnScreenRotation`. It should not be changed without
// deprecating these two metrics.
constexpr int kMaxRestoredWindowCount = 50;
display::DisplayManager* GetDisplayManager() {
return Shell::Get()->display_manager();
}
MruWindowTracker::WindowList GetWindowList() {
// MRU tracker normally skips windows if called during a non active session.
// |scoped_skip_user_session_blocked_check| allows us to get the list of MRU
// windows even when a display is added during for example lock session.
ScopedSkipUserSessionBlockedCheck scoped_skip_user_session_blocked_check;
return Shell::Get()->mru_window_tracker()->BuildWindowForCycleList(kAllDesks);
}
// Returns true when window cycle list can be processed to perform save/restore
// operations on observing display changes.
bool ShouldProcessWindowList() {
if (!Shell::Get()->desks_controller()) {
return false;
}
return !GetDisplayManager()->IsInMirrorMode();
}
} // namespace
// -----------------------------------------------------------------------------
// PersistentWindowController::WindowTracker:
PersistentWindowController::WindowTracker::WindowTracker() = default;
PersistentWindowController::WindowTracker::~WindowTracker() {
RemoveAll();
}
void PersistentWindowController::WindowTracker::Add(
aura::Window* window,
const gfx::Rect& restore_bounds_in_parent) {
if (window_restore_bounds_map_.emplace(window, restore_bounds_in_parent)
.second) {
window->AddObserver(this);
}
}
void PersistentWindowController::WindowTracker::RemoveAll() {
for (auto& item : window_restore_bounds_map_) {
item.first->RemoveObserver(this);
}
window_restore_bounds_map_.clear();
}
void PersistentWindowController::WindowTracker::Remove(aura::Window* window) {
auto iter = window_restore_bounds_map_.find(window);
if (iter != window_restore_bounds_map_.end()) {
iter->first->RemoveObserver(this);
window_restore_bounds_map_.erase(iter);
}
}
void PersistentWindowController::WindowTracker::OnWindowDestroying(
aura::Window* window) {
Remove(window);
}
// -----------------------------------------------------------------------------
// PersistentWindowController:
constexpr char
PersistentWindowController::kNumOfWindowsRestoredOnDisplayAdded[];
constexpr char
PersistentWindowController::kNumOfWindowsRestoredOnScreenRotation[];
PersistentWindowController::PersistentWindowController() {
display_manager_observation_.Observe(Shell::Get()->display_manager());
Shell::Get()->session_controller()->AddObserver(this);
}
PersistentWindowController::~PersistentWindowController() {
Shell::Get()->session_controller()->RemoveObserver(this);
}
void PersistentWindowController::OnDisplayAdded(
const display::Display& new_display) {
display_added_restore_callback_ =
base::BindOnce(&PersistentWindowController::
MaybeRestorePersistentWindowBoundsOnDisplayAdded,
base::Unretained(this));
}
void PersistentWindowController::OnWillRemoveDisplays(
const display::Displays& removed_displays) {
for (const auto& [window, restore_bounds_in_parent] :
need_persistent_info_windows_.window_restore_bounds_map()) {
WindowState* window_state = WindowState::Get(window);
window_state->CreatePersistentWindowInfo(
/*is_landscape_before_rotation=*/false, restore_bounds_in_parent,
/*for_display_removal=*/true);
}
need_persistent_info_windows_.RemoveAll();
for (const auto& removed : removed_displays) {
is_landscape_orientation_map_.erase(removed.id());
}
}
void PersistentWindowController::OnDisplayMetricsChanged(
const display::Display& display,
uint32_t changed_metrics) {
if (!(changed_metrics & DISPLAY_METRIC_ROTATION)) {
return;
}
const bool was_landscape_before_rotation =
base::Contains(is_landscape_orientation_map_, display.id())
? is_landscape_orientation_map_[display.id()]
: false;
for (aura::Window* window : GetWindowList()) {
if (window->GetRootWindow() !=
Shell::GetRootWindowForDisplayId(display.id())) {
continue;
}
auto* window_state = WindowState::Get(window);
if (window_state->persistent_window_info_of_screen_rotation()) {
continue;
}
// Do not restore the bounds on screen rotation of windows that are snapped,
// maximized or fullscreened. A snapped window is expected to have different
// snap positions in different orientations, which means different bounds.
// E.g, a left snapped window in landscape primary is expected to be right
// snapped in landscape secondary. Restoring is not needed for maximized or
// fullscreened windows either, since they will be kept maximized or
// fullscreened after rotation.
if (window_state->IsSnapped() || window_state->IsMaximized() ||
window_state->IsFullscreen()) {
continue;
}
window_state->CreatePersistentWindowInfo(
was_landscape_before_rotation,
/*restore_bounds_in_parent=*/gfx::Rect(),
/*for_display_removal=*/false);
}
screen_rotation_restore_callback_ =
base::BindOnce(&PersistentWindowController::
MaybeRestorePersistentWindowBoundsOnScreenRotation,
base::Unretained(this));
}
void PersistentWindowController::OnWillProcessDisplayChanges() {
if (!ShouldProcessWindowList()) {
return;
}
for (aura::Window* window : GetWindowList()) {
WindowState* window_state = WindowState::Get(window);
// This implies that we keep the first persistent info until they're valid
// to restore, or until they're cleared by user-invoked bounds change.
if (PersistentWindowInfo* info =
window_state->persistent_window_info_of_display_removal();
info) {
info->set_display_id_after_removal(
display::Screen::GetScreen()->GetDisplayNearestWindow(window).id());
continue;
}
// Place the window that needs persistent window info into the temporary
// set. The persistent window info will be created and set if a display is
// removed. Store the window's restore bounds in parent here instead of
// `OnDisplayRemoved`. As the window's restore bounds in parent are
// converted from its restore bounds in screen, which relies on the
// displays' layout. And displays' layout will have been updated inside
// `OnDisplayRemoved`.
need_persistent_info_windows_.Add(
window, window_state->HasRestoreBounds()
? window_state->GetRestoreBoundsInParent()
: gfx::Rect());
}
}
void PersistentWindowController::OnDidProcessDisplayChanges(
const DisplayConfigurationChange& configuration_change) {
if (display_added_restore_callback_) {
std::move(display_added_restore_callback_).Run();
}
need_persistent_info_windows_.RemoveAll();
if (screen_rotation_restore_callback_) {
std::move(screen_rotation_restore_callback_).Run();
}
if (display::Screen::GetScreen()) {
for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
is_landscape_orientation_map_[display.id()] = display.is_landscape();
}
}
}
void PersistentWindowController::OnFirstSessionStarted() {
if (!display::Screen::GetScreen()) {
return;
}
for (const auto& display : display::Screen::GetScreen()->GetAllDisplays()) {
is_landscape_orientation_map_[display.id()] = display.is_landscape();
}
}
void PersistentWindowController::
MaybeRestorePersistentWindowBoundsOnDisplayAdded() {
if (!ShouldProcessWindowList()) {
return;
}
int window_restored_count = 0;
// Maybe add the windows to a new display via SetBoundsInScreen() depending on
// their persistent window info. Go backwards so that if they do get added to
// another root window's container, the stacking order will match the MRU
// order (windows added first are stacked at the bottom).
std::vector<raw_ptr<aura::Window, VectorExperimental>> mru_window_list =
GetWindowList();
for (aura::Window* window : base::Reversed(mru_window_list)) {
WindowState* window_state = WindowState::Get(window);
if (!window_state->persistent_window_info_of_display_removal()) {
continue;
}
PersistentWindowInfo* info =
window_state->persistent_window_info_of_display_removal();
const int64_t persistent_display_id = info->display_id();
auto* display_manager = GetDisplayManager();
if (!display_manager->IsDisplayIdValid(persistent_display_id)) {
continue;
}
const auto& display =
display_manager->GetDisplayForId(persistent_display_id);
// It is possible to have display size in pixel changes, such as changing
// cable, bad cable signal etc., but it should be rare.
if (display.GetSizeInPixel() != info->display_size_in_pixel()) {
continue;
}
const int64_t display_id_after_removal = info->display_id_after_removal();
const bool is_display_changed =
persistent_display_id != display_id_after_removal;
if (!is_display_changed) {
// Update based on the the window's current bounds if its physical display
// never changed. This will help keep the window's bounds to match its
// current window state.
gfx::Rect bounds = window->bounds();
wm::ConvertRectToScreen(
Shell::GetRootWindowForDisplayId(persistent_display_id), &bounds);
window->SetBoundsInScreen(bounds, display);
} else {
// Update |persistent_window_bounds| based on
// |persistent_display_bounds|'s position change. This ensures that
// |persistent_window_bounds| is associated with the right target display.
gfx::Rect persistent_window_bounds = info->window_bounds_in_screen();
const gfx::Vector2d offset = display.bounds().OffsetFromOrigin() -
info->display_offset_from_origin_in_screen();
persistent_window_bounds.Offset(offset);
window->SetBoundsInScreen(persistent_window_bounds, display);
}
// Set the restore bounds back if window's physical display changed, and now
// we are moving it back to its previous display. Or its physical display
// didn't change, and currently the window still has restore bounds set.
if ((is_display_changed || window_state->HasRestoreBounds()) &&
!info->restore_bounds_in_parent().IsEmpty()) {
const gfx::Rect restore_bounds = info->restore_bounds_in_parent();
// Use the stored window's restore bounds in parent to set the window's
// restore bounds in screen. The conversion from the bounds in parent to
// the bounds in screen will be based on the current displays' layout.
window_state->SetRestoreBoundsInParent(restore_bounds);
}
// Reset persistent window info every time the window bounds have restored.
window_state->reset_persistent_window_info_of_display_removal();
++window_restored_count;
}
if (window_restored_count != 0) {
base::UmaHistogramExactLinear(kNumOfWindowsRestoredOnDisplayAdded,
window_restored_count,
kMaxRestoredWindowCount);
}
}
void PersistentWindowController::
MaybeRestorePersistentWindowBoundsOnScreenRotation() {
if (!ShouldProcessWindowList()) {
return;
}
int window_restored_count = 0;
for (aura::Window* window : GetWindowList()) {
WindowState* window_state = WindowState::Get(window);
if (!window_state->persistent_window_info_of_screen_rotation()) {
continue;
}
PersistentWindowInfo* info =
window_state->persistent_window_info_of_screen_rotation();
const int64_t display_id = info->display_id();
auto* display_manager = GetDisplayManager();
if (!display_manager->IsDisplayIdValid(display_id)) {
continue;
}
// Restore window's bounds if we are rotating back to the screen orientation
// that window's bounds was stored. Note, `kLandscapePrimary` and
// `kLandscapeSecondary` will be treated the same in this case since
// window's bounds should be the same in each landscape orientation. Same
// for portrait screen orientation.
if (display_manager->GetDisplayForId(display_id).is_landscape() ==
info->is_landscape()) {
window->SetBounds(info->window_bounds_in_screen());
++window_restored_count;
}
}
if (window_restored_count != 0) {
base::UmaHistogramExactLinear(kNumOfWindowsRestoredOnScreenRotation,
window_restored_count,
kMaxRestoredWindowCount);
}
}
} // namespace ash