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
|
// Copyright 2018 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 "components/exo/shell_surface_util.h"
#include <memory>
#include "ash/public/cpp/app_types.h"
#include "base/strings/string_number_conversions.h"
#include "base/trace_event/trace_event.h"
#include "components/exo/permission.h"
#include "components/exo/shell_surface_base.h"
#include "components/exo/surface.h"
#include "components/exo/wm_helper.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_targeter.h"
#include "ui/events/event.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/window_util.h"
DEFINE_UI_CLASS_PROPERTY_TYPE(exo::Permission*)
namespace exo {
namespace {
DEFINE_UI_CLASS_PROPERTY_KEY(Surface*, kMainSurfaceKey, nullptr)
// Application Id set by the client.
DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(std::string, kApplicationIdKey, nullptr)
// Startup Id set by the client.
DEFINE_OWNED_UI_CLASS_PROPERTY_KEY(std::string, kStartupIdKey, nullptr)
// Accessibility Id set by the client.
DEFINE_UI_CLASS_PROPERTY_KEY(int32_t, kClientAccessibilityIdKey, -1)
// Permission object allowing this window to activate itself.
DEFINE_UI_CLASS_PROPERTY_KEY(exo::Permission*, kPermissionKey, nullptr)
// Returns true if the component for a located event should be taken care of
// by the window system.
bool ShouldHTComponentBlocked(int component) {
switch (component) {
case HTCAPTION:
case HTCLOSE:
case HTMAXBUTTON:
case HTMINBUTTON:
case HTMENU:
case HTSYSMENU:
return true;
default:
return false;
}
}
// Find the lowest targeter in the parent chain.
aura::WindowTargeter* FindTargeter(ui::EventTarget* target) {
do {
ui::EventTargeter* targeter = target->GetEventTargeter();
if (targeter)
return static_cast<aura::WindowTargeter*>(targeter);
target = target->GetParentTarget();
} while (target);
return nullptr;
}
} // namespace
void SetShellApplicationId(aura::Window* window,
const base::Optional<std::string>& id) {
TRACE_EVENT1("exo", "SetApplicationId", "application_id", id ? *id : "null");
if (id)
window->SetProperty(kApplicationIdKey, *id);
else
window->ClearProperty(kApplicationIdKey);
}
const std::string* GetShellApplicationId(const aura::Window* window) {
return window->GetProperty(kApplicationIdKey);
}
void SetArcAppType(aura::Window* window) {
window->SetProperty(aura::client::kAppType,
static_cast<int>(ash::AppType::ARC_APP));
}
void SetShellStartupId(aura::Window* window,
const base::Optional<std::string>& id) {
TRACE_EVENT1("exo", "SetStartupId", "startup_id", id ? *id : "null");
if (id)
window->SetProperty(kStartupIdKey, *id);
else
window->ClearProperty(kStartupIdKey);
}
const std::string* GetShellStartupId(aura::Window* window) {
return window->GetProperty(kStartupIdKey);
}
void SetShellClientAccessibilityId(aura::Window* window,
const base::Optional<int32_t>& id) {
TRACE_EVENT1("exo", "SetClientAccessibilityId", "id",
id ? base::NumberToString(*id) : "null");
if (id)
window->SetProperty(kClientAccessibilityIdKey, *id);
else
window->ClearProperty(kClientAccessibilityIdKey);
}
const base::Optional<int32_t> GetShellClientAccessibilityId(
aura::Window* window) {
auto id = window->GetProperty(kClientAccessibilityIdKey);
if (id < 0)
return base::nullopt;
else
return id;
}
void SetShellMainSurface(aura::Window* window, Surface* surface) {
window->SetProperty(kMainSurfaceKey, surface);
}
Surface* GetShellMainSurface(const aura::Window* window) {
return window->GetProperty(kMainSurfaceKey);
}
ShellSurfaceBase* GetShellSurfaceBaseForWindow(aura::Window* window) {
// Only windows with a surface can have a shell surface.
if (!GetShellMainSurface(window))
return nullptr;
views::Widget* widget = views::Widget::GetWidgetForNativeWindow(window);
if (!widget)
return nullptr;
return static_cast<ShellSurfaceBase*>(widget->widget_delegate());
}
Surface* GetTargetSurfaceForLocatedEvent(
const ui::LocatedEvent* original_event) {
aura::Window* window =
WMHelper::GetInstance()->GetCaptureClient()->GetCaptureWindow();
if (!window) {
return Surface::AsSurface(
static_cast<aura::Window*>(original_event->target()));
}
Surface* main_surface = GetShellMainSurface(window);
// Skip if the event is captured by non exo windows.
if (!main_surface) {
auto* widget = views::Widget::GetTopLevelWidgetForNativeView(window);
if (!widget)
return nullptr;
main_surface = GetShellMainSurface(widget->GetNativeWindow());
if (!main_surface)
return nullptr;
}
DCHECK_EQ(window, static_cast<aura::Window*>(original_event->target()));
// Create a clone of the event as targeter may update it during the
// search.
auto cloned = ui::Event::Clone(*original_event);
ui::LocatedEvent* event = cloned->AsLocatedEvent();
while (true) {
gfx::PointF location_in_target_f = event->location_f();
gfx::Point location_in_target = event->location();
ui::EventTarget* event_target = window;
aura::WindowTargeter* targeter = FindTargeter(event_target);
DCHECK(targeter);
aura::Window* focused =
static_cast<aura::Window*>(targeter->FindTargetForEvent(window, event));
if (focused) {
Surface* surface = Surface::AsSurface(focused);
if (focused != window)
return surface;
else if (surface && surface->HitTest(location_in_target)) {
// If the targeting fallback to the root (first) window, test the
// hit region again.
return surface;
}
}
// If the event falls into the place where the window system should care
// about (i.e. window caption), do not check the transient parent but just
// return nullptr. See b/149517682.
if (window->delegate() &&
ShouldHTComponentBlocked(
window->delegate()->GetNonClientComponent(location_in_target))) {
return nullptr;
}
aura::Window* parent_window = wm::GetTransientParent(window);
if (!parent_window)
return main_surface;
event->set_location_f(location_in_target_f);
event_target->ConvertEventToTarget(parent_window, event);
window = parent_window;
}
}
namespace {
// An activation-permission object whose lifetime is tied to a window property.
class ScopedWindowActivationPermission : public Permission {
public:
ScopedWindowActivationPermission(aura::Window* window,
base::TimeDelta timeout)
: Permission(Permission::Capability::kActivate, timeout),
window_(window) {
Permission* other = window_->GetProperty(kPermissionKey);
if (other) {
other->Revoke();
}
window_->SetProperty(kPermissionKey, reinterpret_cast<Permission*>(this));
}
~ScopedWindowActivationPermission() override {
if (window_->GetProperty(kPermissionKey) == this)
window_->ClearProperty(kPermissionKey);
}
private:
aura::Window* window_;
};
} // namespace
std::unique_ptr<Permission> GrantPermissionToActivate(aura::Window* window,
base::TimeDelta timeout) {
return std::make_unique<ScopedWindowActivationPermission>(window, timeout);
}
bool HasPermissionToActivate(aura::Window* window) {
Permission* permission = window->GetProperty(kPermissionKey);
return permission && permission->Check(Permission::Capability::kActivate);
}
} // namespace exo
|