File: window_util.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (256 lines) | stat: -rw-r--r-- 9,482 bytes parent folder | download | duplicates (6)
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
// 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 "ui/wm/core/window_util.h"

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/window.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_tree_owner.h"
#include "ui/display/screen.h"
#include "ui/display/types/display_constants.h"
#include "ui/wm/core/transient_window_manager.h"
#include "ui/wm/core/window_properties.h"
#include "ui/wm/public/activation_client.h"

namespace {

// Invokes |map_func| on all the children of |to_clone|, adding the newly
// cloned children to |parent|. If |map_func| returns nullptr on
// the layer owner, all its layer's children will not be cloned.
//
// WARNING: It is assumed that |parent| is ultimately owned by a LayerTreeOwner.
void CloneChildren(ui::Layer* to_clone,
                   ui::Layer* parent,
                   const wm::MapLayerFunc& map_func) {
  typedef std::vector<raw_ptr<ui::Layer, VectorExperimental>> Layers;
  // Make a copy of the children since RecreateLayer() mutates it.
  Layers children(to_clone->children());
  for (Layers::const_iterator i = children.begin(); i != children.end(); ++i) {
    ui::LayerOwner* owner = (*i)->owner();
    ui::Layer* old_layer = owner ? map_func.Run(owner).release() : nullptr;
    if (old_layer) {
      parent->Add(old_layer);
      // RecreateLayer() moves the existing children to the new layer. Create a
      // copy of those.
      CloneChildren(owner->layer(), old_layer, map_func);
    }
  }
}

// Invokes Mirror() on all the children of |to_mirror|, adding the newly cloned
// children to |parent|.
//
// WARNING: It is assumed that |parent| is ultimately owned by a LayerTreeOwner.
void MirrorChildren(ui::Layer* to_mirror,
                    ui::Layer* parent,
                    bool sync_bounds) {
  for (ui::Layer* child : to_mirror->children()) {
    ui::Layer* mirror = child->Mirror().release();
    mirror->set_sync_bounds_with_source(sync_bounds);
    parent->Add(mirror);
    MirrorChildren(child, mirror, sync_bounds);
  }
}

}  // namespace

namespace wm {

void ActivateWindow(aura::Window* window) {
  DCHECK(window);
  DCHECK(window->GetRootWindow());
  GetActivationClient(window->GetRootWindow())->ActivateWindow(window);
}

void DeactivateWindow(aura::Window* window) {
  DCHECK(window);
  DCHECK(window->GetRootWindow());
  GetActivationClient(window->GetRootWindow())->DeactivateWindow(window);
}

bool IsActiveWindow(const aura::Window* window) {
  DCHECK(window);
  if (!window->GetRootWindow())
    return false;
  const ActivationClient* client = GetActivationClient(window->GetRootWindow());
  return client && client->GetActiveWindow() == window;
}

bool CanActivateWindow(const aura::Window* window) {
  DCHECK(window);
  if (!window->GetRootWindow())
    return false;
  const ActivationClient* client = GetActivationClient(window->GetRootWindow());
  return client && client->CanActivateWindow(window);
}

void SetWindowFullscreen(aura::Window* window,
                         bool fullscreen,
                         int64_t target_display_id) {
  DCHECK(window);
  // Should only specify display id when entering fullscreen.
  DCHECK(target_display_id == display::kInvalidDisplayId || fullscreen);

  ui::mojom::WindowShowState current_show_state =
      window->GetProperty(aura::client::kShowStateKey);
  const bool is_fullscreen =
      current_show_state == ui::mojom::WindowShowState::kFullscreen;
  if (fullscreen == is_fullscreen &&
      target_display_id == display::kInvalidDisplayId) {
    return;
  }
  if (fullscreen) {
    // We only want the current display id if the window is rooted in a display.
    // Need to check for root window, otherwise GetDisplayNearestWindow() would
    // return the primary display by default.
    int64_t current_display_id =
        window->GetRootWindow()
            ? display::Screen::GetScreen()->GetDisplayNearestWindow(window).id()
            : display::kInvalidDisplayId;
    if (is_fullscreen && target_display_id == current_display_id) {
      // Already fullscreened on the target display.
      return;
    }

    // Save the current show state as its restore show state so that we can
    // correctly restore it after exiting the fullscreen mode.
    // Note `aura::client::kRestoreShowStateKey` can be overwritten later by the
    // window state restore history stack on Chrome OS, see the function
    // WindowState::UpdateWindowStateRestoreHistoryStack(). But We still set the
    // `aura::client::kRestoreShowStateKey` here since this function is also
    // used on other non-ChromeOS platforms.
    if (current_show_state != ui::mojom::WindowShowState::kMinimized) {
      window->SetProperty(aura::client::kRestoreShowStateKey,
                          current_show_state);
    }
    // Set fullscreen display id first, so it's available when the show state
    // property change is processed.
    window->SetProperty(aura::client::kFullscreenTargetDisplayIdKey,
                        target_display_id);
    window->SetProperty(aura::client::kShowStateKey,
                        ui::mojom::WindowShowState::kFullscreen);
    window->ClearProperty(aura::client::kFullscreenTargetDisplayIdKey);
  } else {
    Restore(window);
  }
}

bool WindowStateIs(const aura::Window* window,
                   ui::mojom::WindowShowState state) {
  return window->GetProperty(aura::client::kShowStateKey) == state;
}

ui::mojom::WindowShowState GetWindowState(const aura::Window* window) {
  return window->GetProperty(aura::client::kShowStateKey);
}

void SetWindowState(aura::Window* window, ui::mojom::WindowShowState state) {
  window->SetProperty(aura::client::kShowStateKey, state);
}

void Restore(aura::Window* window) {
  window->SetProperty(aura::client::kIsRestoringKey, true);
  window->SetProperty(aura::client::kShowStateKey,
                      window->GetProperty(aura::client::kRestoreShowStateKey));
  window->ClearProperty(aura::client::kIsRestoringKey);
}

void Unminimize(aura::Window* window) {
  DCHECK_EQ(window->GetProperty(aura::client::kShowStateKey),
            ui::mojom::WindowShowState::kMinimized);
  Restore(window);
}

aura::Window* GetActivatableWindow(aura::Window* window) {
  ActivationClient* client = GetActivationClient(window->GetRootWindow());
  return client ? client->GetActivatableWindow(window) : nullptr;
}

aura::Window* GetToplevelWindow(aura::Window* window) {
  return const_cast<aura::Window*>(
      GetToplevelWindow(const_cast<const aura::Window*>(window)));
}

const aura::Window* GetToplevelWindow(const aura::Window* window) {
  const ActivationClient* client = GetActivationClient(window->GetRootWindow());
  return client ? client->GetToplevelWindow(window) : nullptr;
}

std::unique_ptr<ui::LayerTreeOwner> RecreateLayers(ui::LayerOwner* root) {
  DCHECK(root->OwnsLayer());
  return RecreateLayersWithClosure(
      root, base::BindRepeating(
                [](ui::LayerOwner* owner) { return owner->RecreateLayer(); }));
}

std::unique_ptr<ui::LayerTreeOwner> RecreateLayersWithClosure(
    ui::LayerOwner* root,
    const MapLayerFunc& map_func) {
  DCHECK(root->OwnsLayer());
  auto layer = map_func.Run(root);
  if (!layer)
    return nullptr;
  auto old_layer = std::make_unique<ui::LayerTreeOwner>(std::move(layer));
  CloneChildren(root->layer(), old_layer->root(), map_func);
  return old_layer;
}

std::unique_ptr<ui::LayerTreeOwner> MirrorLayers(
    ui::LayerOwner* root, bool sync_bounds) {
  auto mirror = std::make_unique<ui::LayerTreeOwner>(root->layer()->Mirror());
  MirrorChildren(root->layer(), mirror->root(), sync_bounds);
  return mirror;
}

aura::Window* GetTransientParent(aura::Window* window) {
  return const_cast<aura::Window*>(GetTransientParent(
                                 const_cast<const aura::Window*>(window)));
}

const aura::Window* GetTransientParent(const aura::Window* window) {
  const TransientWindowManager* manager =
      TransientWindowManager::GetIfExists(window);
  return manager ? manager->transient_parent() : nullptr;
}

const std::vector<raw_ptr<aura::Window, VectorExperimental>>&
GetTransientChildren(const aura::Window* window) {
  const TransientWindowManager* manager =
      TransientWindowManager::GetIfExists(window);
  if (manager)
    return manager->transient_children();

  static std::vector<raw_ptr<aura::Window, VectorExperimental>>* shared =
      new std::vector<raw_ptr<aura::Window, VectorExperimental>>;
  return *shared;
}

aura::Window* GetTransientRoot(aura::Window* window) {
  while (window && GetTransientParent(window))
    window = GetTransientParent(window);
  return window;
}

void AddTransientChild(aura::Window* parent, aura::Window* child) {
  TransientWindowManager::GetOrCreate(parent)->AddTransientChild(child);
}

void RemoveTransientChild(aura::Window* parent, aura::Window* child) {
  TransientWindowManager::GetOrCreate(parent)->RemoveTransientChild(child);
}

bool HasTransientAncestor(const aura::Window* window,
                          const aura::Window* ancestor) {
  const aura::Window* transient_parent = GetTransientParent(window);
  if (transient_parent == ancestor)
    return true;
  return transient_parent ?
      HasTransientAncestor(transient_parent, ancestor) : false;
}

}  // namespace wm