File: window_modality_controller.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 (248 lines) | stat: -rw-r--r-- 8,566 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
// 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_modality_controller.h"

#include <stddef.h>

#include <algorithm>
#include <queue>
#include <string_view>

#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/class_property.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/base/ui_base_types.h"
#include "ui/events/event.h"
#include "ui/events/event_target.h"
#include "ui/events/gestures/gesture_recognizer.h"
#include "ui/wm/core/window_animations.h"
#include "ui/wm/core/window_util.h"

namespace wm {
namespace {

bool HasAncestor(const aura::Window* window, const aura::Window* ancestor) {
  return ancestor && ancestor->Contains(window);
}

bool TransientChildIsWindowModal(const aura::Window* window) {
  return window->GetProperty(aura::client::kModalKey) ==
         ui::mojom::ModalType::kWindow;
}

bool TransientChildIsSystemModal(const aura::Window* window) {
  return window->GetProperty(aura::client::kModalKey) ==
         ui::mojom::ModalType::kSystem;
}

bool TransientChildIsChildModal(const aura::Window* window) {
  return window->GetProperty(aura::client::kModalKey) ==
         ui::mojom::ModalType::kChild;
}

aura::Window* GetModalParent(const aura::Window* window) {
  return window->GetProperty(aura::client::kChildModalParentKey);
}

bool IsModalTransientChild(const aura::Window* transient,
                           const aura::Window* original) {
  return transient->IsVisible() &&
         (TransientChildIsWindowModal(transient) ||
          TransientChildIsSystemModal(transient) ||
          (TransientChildIsChildModal(transient) &&
           HasAncestor(original, GetModalParent(transient))));
}

const aura::Window* GetModalTransientChild(const aura::Window* activatable,
                                           const aura::Window* original) {
  for (const aura::Window* transient : GetTransientChildren(activatable)) {
    if (IsModalTransientChild(transient, original)) {
      if (GetTransientChildren(transient).empty())
        return transient;

      const aura::Window* modal_child =
          GetModalTransientChild(transient, original);
      return modal_child ? modal_child : transient;
    }
  }
  return nullptr;
}

}  // namespace

void SetModalParent(aura::Window* child, aura::Window* parent) {
  child->SetProperty(aura::client::kChildModalParentKey, parent);
}

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

const aura::Window* GetModalTransient(const aura::Window* window) {
  if (!window)
    return nullptr;

  // We always want to check for the transient child of the toplevel window.
  const aura::Window* toplevel = GetToplevelWindow(window);
  if (!toplevel)
    return nullptr;

  return GetModalTransientChild(toplevel, window);
}

////////////////////////////////////////////////////////////////////////////////
// WindowModalityController, public:

WindowModalityController::WindowModalityController(
    ui::EventTarget* event_target,
    aura::Env* env)
    : env_(env ? env : aura::Env::GetInstance()), event_target_(event_target) {
  env_->AddObserver(this);
  DCHECK(event_target->IsPreTargetListEmpty());
  event_target_->AddPreTargetHandler(this);
}

WindowModalityController::~WindowModalityController() {
  event_target_->RemovePreTargetHandler(this);
  env_->RemoveObserver(this);
  for (size_t i = 0; i < windows_.size(); ++i)
    windows_[i]->RemoveObserver(this);
}

////////////////////////////////////////////////////////////////////////////////
// WindowModalityController, aura::EventFilter implementation:

void WindowModalityController::OnKeyEvent(ui::KeyEvent* event) {
  aura::Window* target = static_cast<aura::Window*>(event->target());
  if (GetModalTransient(target))
    event->SetHandled();
}

void WindowModalityController::OnMouseEvent(ui::MouseEvent* event) {
  aura::Window* target = static_cast<aura::Window*>(event->target());
  if (ProcessLocatedEvent(target, event))
    event->SetHandled();
}

void WindowModalityController::OnTouchEvent(ui::TouchEvent* event) {
  aura::Window* target = static_cast<aura::Window*>(event->target());
  if (ProcessLocatedEvent(target, event))
    event->SetHandled();
}

std::string_view WindowModalityController::GetLogContext() const {
  return "WindowModalityController";
}

////////////////////////////////////////////////////////////////////////////////
// WindowModalityController, aura::EnvObserver implementation:

void WindowModalityController::OnWindowInitialized(aura::Window* window) {
  windows_.push_back(window);
  window->AddObserver(this);
}

////////////////////////////////////////////////////////////////////////////////
// WindowModalityController, aura::WindowObserver implementation:

void WindowModalityController::OnWindowPropertyChanged(aura::Window* window,
                                                       const void* key,
                                                       intptr_t old) {
  // In tests, we sometimes create the modality relationship after a window is
  // visible.
  if (key == aura::client::kModalKey &&
      window->GetProperty(aura::client::kModalKey) !=
          ui::mojom::ModalType::kNone &&
      window->IsVisible()) {
    ActivateWindow(window);
    CancelTouchesOnTransientWindowTree(window);
  }
}

void WindowModalityController::OnWindowVisibilityChanged(aura::Window* window,
                                                         bool visible) {
  if (visible && window->GetProperty(aura::client::kModalKey) !=
                     ui::mojom::ModalType::kNone) {
    CancelTouchesOnTransientWindowTree(window);

    // Make sure no other window has capture, otherwise |window| won't get mouse
    // events.
    aura::Window* capture_window = aura::client::GetCaptureWindow(window);
    if (capture_window) {
      bool should_release_capture = true;
      if (window->GetProperty(aura::client::kModalKey) ==
              ui::mojom::ModalType::kChild &&
          !HasAncestor(capture_window, GetModalParent(window))) {
        // For child modal windows we only need ensure capture is not on a
        // descendant of the modal parent. This way we block events to the
        // parents subtree appropriately.
        should_release_capture = false;
      }

      if (should_release_capture)
        capture_window->ReleaseCapture();
    }
  }
}

void WindowModalityController::OnWindowDestroyed(aura::Window* window) {
  windows_.erase(std::ranges::find(windows_, window));
  window->RemoveObserver(this);
}

bool WindowModalityController::ProcessLocatedEvent(aura::Window* target,
                                                   ui::LocatedEvent* event) {
  if (event->handled())
    return false;
  aura::Window* modal_transient_child = GetModalTransient(target);
  if (modal_transient_child &&
      (event->type() == ui::EventType::kMousePressed ||
       event->type() == ui::EventType::kTouchPressed)) {
    // Activate top window if transient child window is window modal.
    if (TransientChildIsWindowModal(modal_transient_child)) {
      aura::Window* toplevel = GetToplevelWindow(target);
      DCHECK(toplevel);
      ActivateWindow(toplevel);
    }

    AnimateWindow(modal_transient_child, WINDOW_ANIMATION_TYPE_BOUNCE);
  }
  if (event->type() == ui::EventType::kTouchCancelled) {
    return false;
  }
  return !!modal_transient_child;
}

void WindowModalityController::CancelTouchesOnTransientWindowTree(
    aura::Window* window) {
  // Find the top level transient window.
  aura::Window* top_level_window = window;
  while (wm::GetTransientParent(top_level_window))
    top_level_window = wm::GetTransientParent(top_level_window);

  // BFS to get all transient windows in the tree rooted to the top level
  // transient window.
  std::vector<ui::GestureConsumer*> blocked_consumers;
  std::queue<aura::Window*> que;
  que.emplace(top_level_window);
  while (!que.empty()) {
    aura::Window* parent = que.front();
    que.pop();
    blocked_consumers.emplace_back(parent);
    for (aura::Window* w : wm::GetTransientChildren(parent)) {
      que.emplace(w);
    }
  }

  // Cancel touches on all the transient windows.
  env_->gesture_recognizer()->CancelActiveTouchesOn(blocked_consumers);
}

}  // namespace wm