Debian Codegraph @

src/plugins/platform/treeland/dtreelandplatformwindowinterface.cpp

raw
1 // SPDX-FileCopyrightText: 2024 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #define protected public
6 #include <QWindow>
7 #undef protected
8
9 #include "dtreelandplatformwindowinterface.h"
10
11 #include <QWaylandClientExtension>
12 #include <QStyleHints>
13 #include <private/qwaylandintegration_p.h>
14 #include <private/qguiapplication_p.h>
15 #include <private/qwaylandwindow_p.h>
16 #include <private/qwaylandsurface_p.h>
17
18 #include "dvtablehook.h"
19
20 #include "personalizationwaylandclientextension.h"
21
22 DCORE_USE_NAMESPACE
23
24 DGUI_BEGIN_NAMESPACE
25 class Q_DECL_HIDDEN MoveWindowHelper : public QObject
26 {
27 public:
28 explicit MoveWindowHelper(QWindow *w);
29 ~MoveWindowHelper();
30
31 static QHash<const QWindow*, MoveWindowHelper*> mapped;
32
33 private:
34 static bool windowEvent(QWindow *w, QEvent *event);
35 void updateEnableSystemMoveFromProperty();
36
37 QWindow *m_window;
38 bool m_windowMoving;
39 bool m_enableSystemMove;
40 };
41
42 QHash<const QWindow*, MoveWindowHelper*> MoveWindowHelper::mapped;
43
44 MoveWindowHelper::MoveWindowHelper(QWindow *window)
45 : QObject(window)
46 , m_window(window)
47 {
48 mapped[window] = this;
49 updateEnableSystemMoveFromProperty();
50 }
51
52 MoveWindowHelper::~MoveWindowHelper()
53 {
54 mapped.remove(static_cast<QWindow*>(parent()));
55 }
56
57 void MoveWindowHelper::updateEnableSystemMoveFromProperty()
58 {
59 if (!m_window) {
60 return;
61 }
62 const QVariant &v = m_window->property("_d_enableSystemMove");
63
64 m_enableSystemMove = !v.isValid() || v.toBool();
65
66 if (m_enableSystemMove) {
67 DVtableHook::overrideVfptrFun(m_window, &QWindow::event, &MoveWindowHelper::windowEvent);
68 } else if (DVtableHook::hasVtable(m_window)) {
69 DVtableHook::resetVfptrFun(m_window, &QWindow::event);
70 }
71 }
72
73 bool MoveWindowHelper::windowEvent(QWindow *w, QEvent *event)
74 {
75 MoveWindowHelper *self = mapped.value(w);
76
77 if (!self)
78 return DVtableHook::callOriginalFun(w, &QWindow::event, event);
79
80 // TODO Crashed when delete by Vtable.
81 if (event->type() == QEvent::DeferredDelete) {
82 DVtableHook::resetVtable(w);
83 return w->event(event);
84 }
85
86 // m_window 的 event 被 override 以后,在 windowEvent 里面获取到的 this 就成 m_window 了,
87 // 而不是 DNoTitlebarWlWindowHelper,所以此处 windowEvent 改为 static 并传 self 进来
88 {
89 static bool isTouchDown = false;
90 static QPointF touchBeginPosition;
91 if (event->type() == QEvent::TouchBegin) {
92 isTouchDown = true;
93 }
94 if (event->type() == QEvent::TouchEnd || event->type() == QEvent::MouseButtonRelease) {
95 isTouchDown = false;
96 }
97 if (isTouchDown && event->type() == QEvent::MouseButtonPress) {
98 touchBeginPosition = static_cast<QMouseEvent*>(event)->globalPos();
99 }
100 // add some redundancy to distinguish trigger between system menu and system move
101 if (event->type() == QEvent::MouseMove) {
102 QPointF currentPos = static_cast<QMouseEvent*>(event)->globalPos();
103 QPointF delta = touchBeginPosition - currentPos;
104 if (delta.manhattanLength() < QGuiApplication::styleHints()->startDragDistance()) {
105 return DVtableHook::callOriginalFun(w, &QWindow::event, event);
106 }
107 }
108 }
109
110 bool is_mouse_move = event->type() == QEvent::MouseMove && static_cast<QMouseEvent*>(event)->buttons() == Qt::LeftButton;
111
112 if (event->type() == QEvent::MouseButtonRelease) {
113 self->m_windowMoving = false;
114 }
115
116 if (!DVtableHook::callOriginalFun(w, &QWindow::event, event))
117 return false;
118
119 // workaround for kwin: Qt receives no release event when kwin finishes MOVE operation,
120 // which makes app hang in windowMoving state. when a press happens, there's no sense of
121 // keeping the moving state, we can just reset ti back to normal.
122 if (event->type() == QEvent::MouseButtonPress) {
123 self->m_windowMoving = false;
124 }
125
126 if (is_mouse_move && !event->isAccepted()
127 && w->geometry().contains(static_cast<QMouseEvent*>(event)->globalPos())) {
128 if (!self->m_windowMoving && self->m_enableSystemMove) {
129 self->m_windowMoving = true;
130
131 event->accept();
132 if (w && w->handle()) {
133 #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
134 static_cast<QPlatformWindow *>(w->handle())->startSystemMove(QCursor::pos());
135 #else
136 static_cast<QPlatformWindow *>(w->handle())->startSystemMove();
137 #endif
138 }
139 }
140 }
141
142 return true;
143 }
144
145 QMap<QWindow *, DTreeLandPlatformWindowHelper*> DTreeLandPlatformWindowHelper::windowMap;
146 DTreeLandPlatformWindowHelper *DTreeLandPlatformWindowHelper::get(QWindow *window)
147 {
148 if (!PersonalizationManager::instance()->isSupported()) {
149 return nullptr;
150 }
151
152 if (!window) {
153 return nullptr;
154 }
155
156 if (auto helper = windowMap.value(window)) {
157 return helper;
158 }
159 auto helper = new DTreeLandPlatformWindowHelper(window);
160 windowMap[window] = helper;
161 return helper;
162 }
163
164 DTreeLandPlatformWindowHelper::DTreeLandPlatformWindowHelper(QWindow *window)
165 : QObject(window)
166 {
167 window->installEventFilter(this);
168
169 if (!PersonalizationManager::instance()->isActive()) {
170 qWarning() << "Personalization is not active" << window;
171 connect(PersonalizationManager::instance(), &PersonalizationManager::activeChanged, this, &DTreeLandPlatformWindowHelper::onActiveChanged, Qt::QueuedConnection);
172 }
173
174 if (window->handle()) {
175 initWaylandWindow();
176 }
177 }
178
179 DTreeLandPlatformWindowHelper::~DTreeLandPlatformWindowHelper()
180 {
181 // see tst_qwindow.cpp tst_QWindow::qobject_castOnDestruction()
182 windowMap.remove(static_cast<QWindow*>(parent()));
183 }
184
185 bool DTreeLandPlatformWindowHelper::eventFilter(QObject *watched, QEvent *event) {
186 if (event->type() == QEvent::PlatformSurface) {
187 QPlatformSurfaceEvent *se = static_cast<QPlatformSurfaceEvent*>(event);
188 if (se->surfaceEventType() == QPlatformSurfaceEvent::SurfaceCreated) {
189 if (PersonalizationManager::instance()->isActive()) {
190 initWaylandWindow();
191 onSurfaceCreated();
192 }
193 }
194 }
195 return QObject::eventFilter(watched, event);
196 }
197
198 void DTreeLandPlatformWindowHelper::initWaylandWindow()
199 {
200 auto waylandWindow = dynamic_cast<QtWaylandClient::QWaylandWindow *>(window()->handle());
201 if (!waylandWindow) {
202 qWarning() << "waylandWindow is nullptr!!!";
203 return;
204 }
205
206 connect(waylandWindow, &QtWaylandClient::QWaylandWindow::wlSurfaceCreated, this, &DTreeLandPlatformWindowHelper::onSurfaceCreated, Qt::UniqueConnection);
207 connect(waylandWindow, &QtWaylandClient::QWaylandWindow::wlSurfaceDestroyed, this, &DTreeLandPlatformWindowHelper::onSurfaceDestroyed, Qt::UniqueConnection);
208 }
209
210 void DTreeLandPlatformWindowHelper::onActiveChanged()
211 {
212 if (PersonalizationManager::instance()->isActive()) {
213 qDebug() << "Personalization is actived, window" << window();
214 if (window()->handle()) {
215 onSurfaceCreated();
216 }
217 }
218 }
219
220 void DTreeLandPlatformWindowHelper::onSurfaceCreated()
221 {
222 if (m_isNoTitlebar) {
223 doSetEnabledNoTitlebar();
224 }
225 if (m_radius > 0) {
226 doSetWindowRadius();
227 }
228 if (m_isWindowBlur) {
229 doSetEnabledBlurWindow();
230 }
231 }
232
233 void DTreeLandPlatformWindowHelper::onSurfaceDestroyed()
234 {
235 if (m_windowContext) {
236 m_windowContext->deleteLater();
237 m_windowContext = nullptr;
238 }
239 }
240
241 PersonalizationWindowContext *DTreeLandPlatformWindowHelper::windowContext() const
242 {
243 if (m_windowContext) {
244 return m_windowContext;
245 }
246
247 auto waylandWindow = dynamic_cast<QtWaylandClient::QWaylandWindow *>(window()->handle());
248 if (!waylandWindow)
249 return nullptr;
250
251 if (!waylandWindow->waylandSurface()) {
252 qWarning() << "waylandSurface is nullptr!!!";
253 return nullptr;
254 }
255
256 auto surface = waylandWindow->waylandSurface()->object();
257 if (!surface) {
258 qWarning() << "wl_surface is nullptr!!!";
259 return nullptr;
260 }
261
262 if (!m_windowContext) {
263 const_cast<DTreeLandPlatformWindowHelper *>(this)->m_windowContext = new PersonalizationWindowContext(PersonalizationManager::instance()->get_window_context(surface));
264 }
265
266 return m_windowContext;
267 }
268
269 void DTreeLandPlatformWindowHelper::setEnabledNoTitlebar(bool enable)
270 {
271 m_isNoTitlebar = enable;
272 doSetEnabledNoTitlebar();
273 }
274 void DTreeLandPlatformWindowHelper::setWindowRadius(int windowRadius)
275 {
276 m_radius = windowRadius;
277 doSetWindowRadius();
278 }
279 void DTreeLandPlatformWindowHelper::setEnableBlurWindow(bool enableBlurWindow)
280 {
281 m_isWindowBlur = enableBlurWindow;
282 doSetEnabledBlurWindow();
283 }
284
285 void DTreeLandPlatformWindowHelper::doSetEnabledNoTitlebar()
286 {
287 if (auto context = windowContext()) {
288 context->set_titlebar(m_isNoTitlebar ? PersonalizationWindowContext::enable_mode_disable : PersonalizationWindowContext::enable_mode_enable);
289 }
290 }
291
292 void DTreeLandPlatformWindowHelper::doSetWindowRadius()
293 {
294 if (auto context = windowContext()) {
295 context->set_round_corner_radius(m_radius);
296 }
297 }
298
299 void DTreeLandPlatformWindowHelper::doSetEnabledBlurWindow()
300 {
301 if (auto context = windowContext()) {
302 context->set_blend_mode(m_isWindowBlur ? PersonalizationWindowContext::blend_mode_blur : PersonalizationWindowContext::blend_mode_transparent);
303 }
304 }
305
306 DTreeLandPlatformWindowInterface::DTreeLandPlatformWindowInterface(QWindow *window, DPlatformHandle *platformHandle, QObject *parent)
307 : QObject(parent)
308 , DPlatformWindowInterface(window, platformHandle)
309 {
310 if (!MoveWindowHelper::mapped.value(window)) {
311 Q_UNUSED(new MoveWindowHelper(window))
312 }
313 }
314
315 DTreeLandPlatformWindowInterface::~DTreeLandPlatformWindowInterface()
316 {
317 }
318
319 void DTreeLandPlatformWindowInterface::setEnabled(bool enabled)
320 {
321 if (setEnabledNoTitlebar(enabled)) {
322 return;
323 }
324 }
325
326 bool DTreeLandPlatformWindowInterface::isEnabled() const
327 {
328 return isEnabledNoTitlebar();
329 }
330
331 bool DTreeLandPlatformWindowInterface::isEnabledNoTitlebar() const
332 {
333 return m_isNoTitlebar;
334 }
335
336 bool DTreeLandPlatformWindowInterface::setEnabledNoTitlebar(bool enable)
337 {
338 if (m_isNoTitlebar == enable) {
339 return true;
340 }
341 m_isNoTitlebar = enable;
342 if (auto helper = DTreeLandPlatformWindowHelper::get(m_window)) {
343 helper->setEnabledNoTitlebar(enable);
344 }
345 return true;
346 }
347
348 int DTreeLandPlatformWindowInterface::windowRadius() const
349 {
350 return m_radius;
351 }
352
353 void DTreeLandPlatformWindowInterface::setWindowRadius(int windowRadius)
354 {
355 if (m_radius == windowRadius) {
356 return;
357 }
358 m_radius = windowRadius;
359 if (auto helper = DTreeLandPlatformWindowHelper::get(m_window)) {
360 helper->setWindowRadius(m_radius);
361 }
362 if (m_platformHandle) {
363 Q_EMIT m_platformHandle->windowRadiusChanged();
364 }
365 }
366
367 bool DTreeLandPlatformWindowInterface::enableBlurWindow() const
368 {
369 return m_isWindowBlur;
370 }
371
372 void DTreeLandPlatformWindowInterface::setEnableBlurWindow(bool enable)
373 {
374 if (m_isWindowBlur == enable) {
375 return;
376 }
377 m_isWindowBlur = enable;
378 if (auto helper = DTreeLandPlatformWindowHelper::get(m_window)) {
379 helper->setEnableBlurWindow(enable);
380 }
381 if (m_platformHandle) {
382 Q_EMIT m_platformHandle->enableBlurWindowChanged();
383 }
384 }
385 DGUI_END_NAMESPACE