Debian Codegraph @

wayland/dwayland/dnotitlebarwindowhelper_wl.cpp

raw symbols SCIP
1 // SPDX-FileCopyrightText: 2017 - 2022 Uniontech Software Technology Co.,Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "dnotitlebarwindowhelper_wl.h"
6 #include "vtablehook.h"
7
8 #define protected public
9 #include <QWindow>
10 #undef protected
11 #include <QMouseEvent>
12 #include <QGuiApplication>
13 #include <QStyleHints>
14 #include <QTimer>
15 #include <QMetaProperty>
16 #include <QScreen>
17 #include <qpa/qplatformwindow.h>
18 #include <QtWaylandClientVersion>
19
20 #define private public
21 #include "QtWaylandClient/private/qwaylandintegration_p.h"
22 #include "QtWaylandClient/private/qwaylandshellsurface_p.h"
23 #include "QtWaylandClient/private/qwaylandwindow_p.h"
24 #include "QtWaylandClient/private/qwaylandcursor_p.h"
25 #undef private
26
27 #include <private/qguiapplication_p.h>
28
29 DPP_BEGIN_NAMESPACE
30
31 QHash<const QWindow*, DNoTitlebarWlWindowHelper*> DNoTitlebarWlWindowHelper::mapped;
32
33 DNoTitlebarWlWindowHelper::DNoTitlebarWlWindowHelper(QWindow *window)
34 : QObject(window)
35 , m_window(window)
36 {
37 // 不允许设置窗口为无边框的
38 if (window->flags().testFlag(Qt::FramelessWindowHint)) {
39 window->setFlag(Qt::FramelessWindowHint, false);
40 }
41
42 mapped[window] = this;
43
44 updateEnableSystemMoveFromProperty();
45 }
46
47 DNoTitlebarWlWindowHelper::~DNoTitlebarWlWindowHelper()
48 {
49 if (VtableHook::hasVtable(m_window)) {
50 VtableHook::resetVtable(m_window);
51 }
52
53 mapped.remove(static_cast<QWindow*>(parent()));
54
55 // TODO
56 // if (m_window->handle()) { // 当本地窗口还存在时,移除设置过的窗口属性
57 // //! Utility::clearWindowProperty(m_windowID, Utility::internAtom(_DEEPIN_SCISSOR_WINDOW));
58 // DPlatformIntegration::clearNativeSettings(m_windowID);
59 // }
60 }
61
62 void DNoTitlebarWlWindowHelper::setWindowProperty(QWindow *window, const char *name, const QVariant &value)
63 {
64 const QVariant &old_value = window->property(name);
65
66 if (old_value.isValid() && old_value == value)
67 return;
68
69 if (value.typeName() == QByteArray("QPainterPath")) {
70 const QPainterPath &old_path = qvariant_cast<QPainterPath>(old_value);
71 const QPainterPath &new_path = qvariant_cast<QPainterPath>(value);
72
73 if (old_path == new_path) {
74 return;
75 }
76 }
77
78 if(!window)
79 return;
80
81 window->setProperty(name, value);
82
83 if(window->handle()) {
84 QtWaylandClient::QWaylandWindow *wl_window = static_cast<QtWaylandClient::QWaylandWindow *>(window->handle());
85
86 if (wl_window->shellSurface())
87 wl_window->sendProperty(name, value);
88 }
89
90 if (DNoTitlebarWlWindowHelper *self = mapped.value(window)) {
91
92 QByteArray name_array(name);
93 if (!name_array.startsWith("_d_"))
94 return;
95
96 // to upper
97 name_array[3] = name_array.at(3) & ~0x20;
98
99 const QByteArray slot_name = "update" + name_array.mid(3) + "FromProperty";
100
101 if (self->metaObject()->indexOfSlot(slot_name + QByteArray("()")) < 0)
102 return;
103
104 if (!QMetaObject::invokeMethod(self, slot_name.constData(), Qt::DirectConnection)) {
105 qWarning() << "Failed to update property:" << slot_name;
106 }
107 }
108 }
109
110 void DNoTitlebarWlWindowHelper::requestByWindowProperty(QWindow *window, const char *name)
111 {
112 if (window && window->handle()) {
113 QtWaylandClient::QWaylandWindow *wl_window = static_cast<QtWaylandClient::QWaylandWindow *>(window->handle());
114
115 if (wl_window && wl_window->shellSurface())
116 wl_window->sendProperty(name, QVariant());
117 }
118 }
119
120 void DNoTitlebarWlWindowHelper::popupSystemWindowMenu(quintptr wid)
121 {
122 QWindow *window = fromQtWinId(wid);
123 if(!window || !window->handle())
124 return;
125
126 QtWaylandClient::QWaylandWindow *wl_window = static_cast<QtWaylandClient::QWaylandWindow *>(window->handle());
127 if (!wl_window->shellSurface())
128 return;
129
130 if (QtWaylandClient::QWaylandShellSurface *s = wl_window->shellSurface()) {
131 auto wl_integration = static_cast<QtWaylandClient::QWaylandIntegration *>(QGuiApplicationPrivate::platformIntegration());
132 s->showWindowMenu(wl_integration->display()->defaultInputDevice());
133 }
134 }
135
136
137 void DNoTitlebarWlWindowHelper::updateEnableSystemMoveFromProperty()
138 {
139 const QVariant &v = m_window->property(enableSystemMove);
140
141 m_enableSystemMove = !v.isValid() || v.toBool();
142
143 if (m_enableSystemMove) {
144 HookOverride(m_window, &QWindow::event, &DNoTitlebarWlWindowHelper::windowEvent);
145 } else if (VtableHook::hasVtable(m_window)) {
146 HookReset(m_window, &QWindow::event);
147 }
148 }
149
150 bool DNoTitlebarWlWindowHelper::windowEvent(QWindow *w, QEvent *event)
151 {
152 DNoTitlebarWlWindowHelper *self = mapped.value(w);
153
154 if (!self)
155 return VtableHook::callOriginalFun(w, &QWindow::event, event);
156 // m_window 的 event 被 override 以后,在 windowEvent 里面获取到的 this 就成 m_window 了,
157 // 而不是 DNoTitlebarWlWindowHelper,所以此处 windowEvent 改为 static 并传 self 进来
158 {
159 // get touch begin position
160 static bool isTouchDown = false;
161 static QPointF touchBeginPosition;
162 if (event->type() == QEvent::TouchBegin) {
163 isTouchDown = true;
164 }
165 if (event->type() == QEvent::TouchEnd || event->type() == QEvent::MouseButtonRelease) {
166 isTouchDown = false;
167 }
168 if (isTouchDown && event->type() == QEvent::MouseButtonPress) {
169 touchBeginPosition = static_cast<QMouseEvent*>(event)->globalPos();
170 }
171 // add some redundancy to distinguish trigger between system menu and system move
172 if (event->type() == QEvent::MouseMove) {
173 QPointF currentPos = static_cast<QMouseEvent*>(event)->globalPos();
174 QPointF delta = touchBeginPosition - currentPos;
175 if (delta.manhattanLength() < QGuiApplication::styleHints()->startDragDistance()) {
176 return VtableHook::callOriginalFun(w, &QWindow::event, event);
177 }
178 }
179 }
180
181 bool is_mouse_move = event->type() == QEvent::MouseMove && static_cast<QMouseEvent*>(event)->buttons() == Qt::LeftButton;
182
183 if (event->type() == QEvent::MouseButtonRelease) {
184 self->m_windowMoving = false;
185 }
186
187 if (!HookCall(w, &QWindow::event, event))
188 return false;
189
190 // workaround for kwin: Qt receives no release event when kwin finishes MOVE operation,
191 // which makes app hang in windowMoving state. when a press happens, there's no sense of
192 // keeping the moving state, we can just reset ti back to normal.
193 if (event->type() == QEvent::MouseButtonPress) {
194 self->m_windowMoving = false;
195 }
196
197 if (is_mouse_move && !event->isAccepted()
198 && w->geometry().contains(static_cast<QMouseEvent*>(event)->globalPos())) {
199 if (!self->m_windowMoving && self->isEnableSystemMove()) {
200 self->m_windowMoving = true;
201
202 event->accept();
203 startMoveWindow(w);
204 }
205 }
206
207 return true;
208 }
209
210 bool DNoTitlebarWlWindowHelper::isEnableSystemMove(/*quint32 winId*/)
211 {
212 return m_enableSystemMove;
213 }
214
215 void DNoTitlebarWlWindowHelper::startMoveWindow(QWindow *window)
216 {
217 // QWaylandWindow::startSystemMove
218 if (window && window->handle()) {
219 #if QTWAYLANDCLIENT_VERSION < QT_VERSION_CHECK(5, 15, 0)
220 static_cast<QtWaylandClient::QWaylandWindow *>(window->handle())->startSystemMove(QCursor::pos());
221 #else
222 static_cast<QtWaylandClient::QWaylandWindow *>(window->handle())->startSystemMove();
223 #endif
224 }
225 }
226
227 DPP_END_NAMESPACE