1 | // Copyright (C) 2023 Jan Grulich <jgrulich@redhat.com> |
---|---|
2 | // Copyright (C) 2023 The Qt Company Ltd. |
3 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
4 | |
5 | #include "qwaylandadwaitadecoration_p.h" |
6 | |
7 | // QtCore |
8 | #include <QtCore/QLoggingCategory> |
9 | #include <QScopeGuard> |
10 | |
11 | // QtDBus |
12 | #include <QtDBus/QDBusArgument> |
13 | #include <QtDBus/QDBusConnection> |
14 | #include <QtDBus/QDBusMessage> |
15 | #include <QtDBus/QDBusPendingCall> |
16 | #include <QtDBus/QDBusPendingCallWatcher> |
17 | #include <QtDBus/QDBusPendingReply> |
18 | #include <QtDBus/QDBusVariant> |
19 | #include <QtDBus/QtDBus> |
20 | |
21 | // QtGui |
22 | #include <QtGui/QColor> |
23 | #include <QtGui/QPainter> |
24 | #include <QtGui/QPainterPath> |
25 | |
26 | #include <QtGui/private/qguiapplication_p.h> |
27 | #include <QtGui/qpa/qplatformtheme.h> |
28 | |
29 | // QtSvg |
30 | #include <QtSvg/QSvgRenderer> |
31 | |
32 | // QtWayland |
33 | #include <QtWaylandClient/private/qwaylandshmbackingstore_p.h> |
34 | #include <QtWaylandClient/private/qwaylandwindow_p.h> |
35 | |
36 | |
37 | QT_BEGIN_NAMESPACE |
38 | |
39 | using namespace Qt::StringLiterals; |
40 | |
41 | namespace QtWaylandClient { |
42 | |
43 | static constexpr int ceButtonSpacing = 12; |
44 | static constexpr int ceButtonWidth = 24; |
45 | static constexpr int ceCornerRadius = 12; |
46 | static constexpr int ceShadowsWidth = 10; |
47 | static constexpr int ceTitlebarHeight = 38; |
48 | static constexpr int ceWindowBorderWidth = 1; |
49 | |
50 | static QMap<QWaylandAdwaitaDecoration::ButtonIcon, QString> buttonMap = { |
51 | { QWaylandAdwaitaDecoration::CloseIcon, "window-close-symbolic"_L1}, |
52 | { QWaylandAdwaitaDecoration::MinimizeIcon, "window-minimize-symbolic"_L1}, |
53 | { QWaylandAdwaitaDecoration::MaximizeIcon, "window-maximize-symbolic"_L1}, |
54 | { QWaylandAdwaitaDecoration::RestoreIcon, "window-restore-symbolic"_L1} |
55 | }; |
56 | |
57 | const QDBusArgument &operator>>(const QDBusArgument &argument, QMap<QString, QVariantMap> &map) |
58 | { |
59 | argument.beginMap(); |
60 | map.clear(); |
61 | |
62 | while (!argument.atEnd()) { |
63 | QString key; |
64 | QVariantMap value; |
65 | argument.beginMapEntry(); |
66 | argument >> key >> value; |
67 | argument.endMapEntry(); |
68 | map.insert(key, value); |
69 | } |
70 | |
71 | argument.endMap(); |
72 | return argument; |
73 | } |
74 | |
75 | Q_LOGGING_CATEGORY(lcQWaylandAdwaitaDecorationLog, "qt.qpa.qwaylandadwaitadecoration", QtWarningMsg) |
76 | |
77 | QWaylandAdwaitaDecoration::QWaylandAdwaitaDecoration() |
78 | : QWaylandAbstractDecoration() |
79 | { |
80 | m_lastButtonClick = QDateTime::currentDateTime(); |
81 | |
82 | QTextOption option(Qt::AlignHCenter | Qt::AlignVCenter); |
83 | option.setWrapMode(QTextOption::NoWrap); |
84 | m_windowTitle.setTextOption(option); |
85 | m_windowTitle.setTextFormat(Qt::PlainText); |
86 | |
87 | const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme(); |
88 | if (const QFont *font = theme->font(type: QPlatformTheme::TitleBarFont)) |
89 | m_font = std::make_unique<QFont>(args: *font); |
90 | if (!m_font) // Fallback to GNOME's default font |
91 | m_font = std::make_unique<QFont>(args: "Cantarell"_L1, args: 10); |
92 | |
93 | QTimer::singleShot(interval: 0, receiver: this, slot: &QWaylandAdwaitaDecoration::loadConfiguration); |
94 | } |
95 | |
96 | QMargins QWaylandAdwaitaDecoration::margins(QWaylandAbstractDecoration::MarginsType marginsType) const |
97 | { |
98 | const bool onlyShadows = marginsType == QWaylandAbstractDecoration::ShadowsOnly; |
99 | const bool shadowsExcluded = marginsType == ShadowsExcluded; |
100 | |
101 | if (waylandWindow()->windowStates() & Qt::WindowMaximized) { |
102 | // Maximized windows don't have anything around, no shadows, border, |
103 | // etc. Only report titlebar height in case we are not asking for shadow |
104 | // margins. |
105 | return QMargins(0, onlyShadows ? 0 : ceTitlebarHeight, 0, 0); |
106 | } |
107 | |
108 | const QWaylandWindow::ToplevelWindowTilingStates tilingStates = waylandWindow()->toplevelWindowTilingStates(); |
109 | |
110 | // Since all sides (left, right, bottom) are going to be same |
111 | const int marginsBase = shadowsExcluded ? ceWindowBorderWidth : ceShadowsWidth + ceWindowBorderWidth; |
112 | const int sideMargins = onlyShadows ? ceShadowsWidth : marginsBase; |
113 | const int topMargins = onlyShadows ? ceShadowsWidth : ceTitlebarHeight + marginsBase; |
114 | |
115 | return QMargins(tilingStates & QWaylandWindow::WindowTiledLeft ? 0 : sideMargins, |
116 | tilingStates & QWaylandWindow::WindowTiledTop ? onlyShadows ? 0 : ceTitlebarHeight : topMargins, |
117 | tilingStates & QWaylandWindow::WindowTiledRight ? 0 : sideMargins, |
118 | tilingStates & QWaylandWindow::WindowTiledBottom ? 0 : sideMargins); |
119 | } |
120 | |
121 | void QWaylandAdwaitaDecoration::paint(QPaintDevice *device) |
122 | { |
123 | const QRect surfaceRect = waylandWindow()->windowContentGeometry() + margins(marginsType: ShadowsOnly); |
124 | |
125 | QPainter p(device); |
126 | p.setRenderHint(hint: QPainter::Antialiasing); |
127 | |
128 | /* |
129 | * Titlebar and window border |
130 | */ |
131 | QPainterPath path; |
132 | const QPointF topLeft = { margins(marginsType: ShadowsOnly).left() + 0.5, |
133 | margins(marginsType: ShadowsOnly).top() - 0.5 }; |
134 | const int titleBarWidth = surfaceRect.width() - margins(marginsType: ShadowsOnly).left() |
135 | - margins(marginsType: ShadowsOnly).right() - 0.5; |
136 | const int borderRectHeight = |
137 | surfaceRect.height() - margins().top() - margins().bottom() + 0.5; |
138 | |
139 | // Maximized or tiled won't have rounded corners |
140 | if (waylandWindow()->windowStates() & Qt::WindowMaximized |
141 | || waylandWindow()->toplevelWindowTilingStates() != QWaylandWindow::WindowNoState) { |
142 | path.addRect(rect: QRectF(topLeft, QSizeF(titleBarWidth, margins().top()))); |
143 | } else { |
144 | path.addRoundedRect(rect: QRectF(topLeft, QSizeF(titleBarWidth, margins().top() + ceCornerRadius)), |
145 | xRadius: ceCornerRadius, yRadius: ceCornerRadius); |
146 | } |
147 | |
148 | p.save(); |
149 | p.setPen(color(type: Border)); |
150 | p.fillPath(path: path.simplified(), brush: color(type: Background)); |
151 | p.drawPath(path); |
152 | p.drawRect(rect: QRectF(topLeft.x(), margins().top(), titleBarWidth, borderRectHeight)); |
153 | p.restore(); |
154 | |
155 | /* |
156 | * Window title |
157 | */ |
158 | const QRect top = QRect(margins().left(), margins().bottom(), surfaceRect.width(), |
159 | margins().top() - margins().bottom()); |
160 | const QString windowTitleText = waylandWindow()->windowTitle(); |
161 | if (!windowTitleText.isEmpty()) { |
162 | if (m_windowTitle.text() != windowTitleText) { |
163 | m_windowTitle.setText(windowTitleText); |
164 | m_windowTitle.prepare(); |
165 | } |
166 | |
167 | QRect titleBar = top; |
168 | if (m_placement == Right) { |
169 | titleBar.setLeft(margins().left()); |
170 | titleBar.setRight(static_cast<int>(buttonRect(button: Minimize).left()) - 8); |
171 | } else { |
172 | titleBar.setLeft(static_cast<int>(buttonRect(button: Minimize).right()) + 8); |
173 | titleBar.setRight(surfaceRect.width() - margins().right()); |
174 | } |
175 | |
176 | p.save(); |
177 | p.setClipRect(titleBar); |
178 | p.setPen(color(type: Foreground)); |
179 | QSize size = m_windowTitle.size().toSize(); |
180 | int dx = (top.width() - size.width()) / 2; |
181 | int dy = (top.height() - size.height()) / 2; |
182 | p.setFont(*m_font); |
183 | QPoint windowTitlePoint(top.topLeft().x() + dx, top.topLeft().y() + dy); |
184 | p.drawStaticText(p: windowTitlePoint, staticText: m_windowTitle); |
185 | p.restore(); |
186 | } |
187 | |
188 | |
189 | /* |
190 | * Buttons |
191 | */ |
192 | if (m_buttons.contains(key: Close)) |
193 | drawButton(button: Close, painter: &p); |
194 | |
195 | if (m_buttons.contains(key: Maximize)) |
196 | drawButton(button: Maximize, painter: &p); |
197 | |
198 | if (m_buttons.contains(key: Minimize)) |
199 | drawButton(button: Minimize, painter: &p); |
200 | } |
201 | |
202 | bool QWaylandAdwaitaDecoration::handleMouse(QWaylandInputDevice *inputDevice, const QPointF &local, |
203 | const QPointF &global, Qt::MouseButtons b, |
204 | Qt::KeyboardModifiers mods) |
205 | { |
206 | Q_UNUSED(global) |
207 | |
208 | if (local.y() > margins().top()) |
209 | updateButtonHoverState(hoveredButton: Button::None); |
210 | |
211 | // Figure out what area mouse is in |
212 | QRect surfaceRect = waylandWindow()->windowContentGeometry() + margins(marginsType: ShadowsOnly); |
213 | if (local.y() <= surfaceRect.top() + margins().top()) |
214 | processMouseTop(inputDevice, local, b, mods); |
215 | else if (local.y() > surfaceRect.bottom() - margins().bottom()) |
216 | processMouseBottom(inputDevice, local, b, mods); |
217 | else if (local.x() <= surfaceRect.left() + margins().left()) |
218 | processMouseLeft(inputDevice, local, b, mods); |
219 | else if (local.x() > surfaceRect.right() - margins().right()) |
220 | processMouseRight(inputDevice, local, b, mods); |
221 | else { |
222 | #if QT_CONFIG(cursor) |
223 | waylandWindow()->restoreMouseCursor(device: inputDevice); |
224 | #endif |
225 | } |
226 | |
227 | // Reset clicking state in case a button press is released outside |
228 | // the button area |
229 | if (isLeftReleased(newMouseButtonState: b)) { |
230 | m_clicking = None; |
231 | requestRepaint(); |
232 | } |
233 | |
234 | setMouseButtons(b); |
235 | return false; |
236 | } |
237 | |
238 | bool QWaylandAdwaitaDecoration::handleTouch(QWaylandInputDevice *inputDevice, const QPointF &local, |
239 | const QPointF &global, QEventPoint::State state, |
240 | Qt::KeyboardModifiers mods) |
241 | { |
242 | Q_UNUSED(inputDevice) |
243 | Q_UNUSED(global) |
244 | Q_UNUSED(mods) |
245 | |
246 | bool handled = state == QEventPoint::Pressed; |
247 | |
248 | if (handled) { |
249 | if (buttonRect(button: Close).contains(p: local)) |
250 | QWindowSystemInterface::handleCloseEvent(window: window()); |
251 | else if (m_buttons.contains(key: Maximize) && buttonRect(button: Maximize).contains(p: local)) |
252 | window()->setWindowStates(window()->windowStates() ^ Qt::WindowMaximized); |
253 | else if (m_buttons.contains(key: Minimize) && buttonRect(button: Minimize).contains(p: local)) |
254 | window()->setWindowState(Qt::WindowMinimized); |
255 | else if (local.y() <= margins().top()) |
256 | waylandWindow()->shellSurface()->move(inputDevice); |
257 | else |
258 | handled = false; |
259 | } |
260 | |
261 | return handled; |
262 | } |
263 | |
264 | QString getIconSvg(const QString &iconName) |
265 | { |
266 | const QStringList themeNames = { QIcon::themeName(), QIcon::fallbackThemeName(), "Adwaita"_L1}; |
267 | |
268 | qCDebug(lcQWaylandAdwaitaDecorationLog) << "Searched icon themes: "<< themeNames; |
269 | |
270 | for (const QString &themeName : themeNames) { |
271 | if (themeName.isEmpty()) |
272 | continue; |
273 | |
274 | for (const QString &path : QIcon::themeSearchPaths()) { |
275 | if (path.startsWith(c: QLatin1Char(':'))) |
276 | continue; |
277 | |
278 | const QString fullPath = QString("%1/%2").arg(a: path).arg(a: themeName); |
279 | QDirIterator dirIt(fullPath, {"*.svg"}, QDir::Files, QDirIterator::Subdirectories); |
280 | while (dirIt.hasNext()) { |
281 | const QString fileName = dirIt.next(); |
282 | const QFileInfo fileInfo(fileName); |
283 | |
284 | if (fileInfo.fileName() == iconName) { |
285 | qCDebug(lcQWaylandAdwaitaDecorationLog) << "Using "<< iconName << " from "<< themeName << " theme"; |
286 | QFile readFile(fileInfo.filePath()); |
287 | if (!readFile.open(flags: QFile::ReadOnly)) { |
288 | qCWarning(lcQWaylandAdwaitaDecorationLog, "Failed to open %ls: %ls", |
289 | qUtf16Printable(readFile.fileName()), |
290 | qUtf16Printable(readFile.errorString())); |
291 | } |
292 | return readFile.readAll(); |
293 | } |
294 | } |
295 | } |
296 | } |
297 | |
298 | qCWarning(lcQWaylandAdwaitaDecorationLog) << "Failed to find an svg icon for "<< iconName; |
299 | |
300 | return QString(); |
301 | } |
302 | |
303 | void QWaylandAdwaitaDecoration::loadConfiguration() |
304 | { |
305 | qRegisterMetaType<QDBusVariant>(); |
306 | qDBusRegisterMetaType<QMap<QString, QVariantMap>>(); |
307 | |
308 | QDBusConnection connection = QDBusConnection::sessionBus(); |
309 | |
310 | QDBusMessage message = QDBusMessage::createMethodCall(destination: "org.freedesktop.portal.Desktop"_L1, |
311 | path: "/org/freedesktop/portal/desktop"_L1, |
312 | interface: "org.freedesktop.portal.Settings"_L1, |
313 | method: "ReadAll"_L1); |
314 | message << QStringList{ { "org.gnome.desktop.wm.preferences"_L1}, |
315 | { "org.freedesktop.appearance"_L1} }; |
316 | |
317 | QDBusPendingCall pendingCall = connection.asyncCall(message); |
318 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this); |
319 | QObject::connect(sender: watcher, signal: &QDBusPendingCallWatcher::finished, context: this, slot: [this](QDBusPendingCallWatcher *watcher) { |
320 | QDBusPendingReply<QMap<QString, QVariantMap>> reply = *watcher; |
321 | if (reply.isValid()) { |
322 | QMap<QString, QVariantMap> settings = reply.value(); |
323 | if (!settings.isEmpty()) { |
324 | const uint colorScheme = settings.value(key: "org.freedesktop.appearance"_L1).value(key: "color-scheme"_L1).toUInt(); |
325 | updateColors(isDark: colorScheme == 1); // 1 == Prefer Dark |
326 | const QString buttonLayout = settings.value(key: "org.gnome.desktop.wm.preferences"_L1).value(key: "button-layout"_L1).toString(); |
327 | if (!buttonLayout.isEmpty()) |
328 | updateTitlebarLayout(layout: buttonLayout); |
329 | // Workaround for QGtkStyle not having correct titlebar font |
330 | const QString titlebarFont = |
331 | settings.value(key: "org.gnome.desktop.wm.preferences"_L1).value(key: "titlebar-font"_L1).toString(); |
332 | if (titlebarFont.contains(s: "bold"_L1, cs: Qt::CaseInsensitive)) { |
333 | m_font->setBold(true); |
334 | } |
335 | } |
336 | } |
337 | watcher->deleteLater(); |
338 | }); |
339 | |
340 | QDBusConnection::sessionBus().connect(service: QString(), path: "/org/freedesktop/portal/desktop"_L1, |
341 | interface: "org.freedesktop.portal.Settings"_L1, name: "SettingChanged"_L1, receiver: this, |
342 | SLOT(settingChanged(QString, QString, QDBusVariant))); |
343 | |
344 | // Load SVG icons |
345 | for (auto mapIt = buttonMap.constBegin(); mapIt != buttonMap.constEnd(); mapIt++) { |
346 | const QString fullName = mapIt.value() + QStringLiteral(".svg"); |
347 | m_icons[mapIt.key()] = getIconSvg(iconName: fullName); |
348 | } |
349 | |
350 | updateColors(isDark: false); |
351 | } |
352 | |
353 | void QWaylandAdwaitaDecoration::updateColors(bool isDark) |
354 | { |
355 | qCDebug(lcQWaylandAdwaitaDecorationLog) << "Color scheme changed to: "<< (isDark ? "dark": "light"); |
356 | |
357 | m_colors = { { Background, isDark ? QColor(0x303030) : QColor(0xffffff) }, |
358 | { BackgroundInactive, isDark ? QColor(0x242424) : QColor(0xfafafa) }, |
359 | { Foreground, isDark ? QColor(0xffffff) : QColor(0x2e2e2e) }, |
360 | { ForegroundInactive, isDark ? QColor(0x919191) : QColor(0x949494) }, |
361 | { Border, isDark ? QColor(0x3b3b3b) : QColor(0xdbdbdb) }, |
362 | { BorderInactive, isDark ? QColor(0x303030) : QColor(0xdbdbdb) }, |
363 | { ButtonBackground, isDark ? QColor(0x444444) : QColor(0xebebeb) }, |
364 | { ButtonBackgroundInactive, isDark ? QColor(0x2e2e2e) : QColor(0xf0f0f0) }, |
365 | { HoveredButtonBackground, isDark ? QColor(0x4f4f4f) : QColor(0xe0e0e0) }, |
366 | { PressedButtonBackground, isDark ? QColor(0x6e6e6e) : QColor(0xc2c2c2) } }; |
367 | requestRepaint(); |
368 | } |
369 | |
370 | void QWaylandAdwaitaDecoration::updateTitlebarLayout(const QString &layout) |
371 | { |
372 | const QStringList layouts = layout.split(sep: QLatin1Char(':')); |
373 | if (layouts.count() != 2) |
374 | return; |
375 | |
376 | // Remove previous configuration |
377 | m_buttons.clear(); |
378 | |
379 | const QString &leftLayout = layouts.at(i: 0); |
380 | const QString &rightLayout = layouts.at(i: 1); |
381 | m_placement = leftLayout.contains(s: "close"_L1) ? Left : Right; |
382 | |
383 | int pos = 1; |
384 | const QString &buttonLayout = m_placement == Right ? rightLayout : leftLayout; |
385 | |
386 | QStringList buttonList = buttonLayout.split(sep: QLatin1Char(',')); |
387 | if (m_placement == Right) |
388 | std::reverse(first: buttonList.begin(), last: buttonList.end()); |
389 | |
390 | for (const QString &button : buttonList) { |
391 | if (button == "close"_L1) |
392 | m_buttons.insert(key: Close, value: pos); |
393 | else if (button == "maximize"_L1) |
394 | m_buttons.insert(key: Maximize, value: pos); |
395 | else if (button == "minimize"_L1) |
396 | m_buttons.insert(key: Minimize, value: pos); |
397 | |
398 | pos++; |
399 | } |
400 | |
401 | qCDebug(lcQWaylandAdwaitaDecorationLog) << "Button layout changed to: "<< layout; |
402 | |
403 | requestRepaint(); |
404 | } |
405 | |
406 | void QWaylandAdwaitaDecoration::settingChanged(const QString &group, const QString &key, |
407 | const QDBusVariant &value) |
408 | { |
409 | if (group == "org.gnome.desktop.wm.preferences"_L1&& key == "button-layout"_L1) { |
410 | const QString layout = value.variant().toString(); |
411 | updateTitlebarLayout(layout); |
412 | } else if (group == "org.freedesktop.appearance"_L1&& key == "color-scheme"_L1) { |
413 | const uint colorScheme = value.variant().toUInt(); |
414 | updateColors(isDark: colorScheme == 1); // 1 == Prefer Dark |
415 | } |
416 | } |
417 | |
418 | QRectF QWaylandAdwaitaDecoration::buttonRect(Button button) const |
419 | { |
420 | int xPos; |
421 | int yPos; |
422 | const int btnPos = m_buttons.value(key: button); |
423 | |
424 | const QRect surfaceRect = waylandWindow()->windowContentGeometry() + margins(marginsType: QWaylandAbstractDecoration::ShadowsOnly); |
425 | if (m_placement == Right) { |
426 | xPos = surfaceRect.width(); |
427 | xPos -= ceButtonWidth * btnPos; |
428 | xPos -= ceButtonSpacing * btnPos; |
429 | xPos -= margins(marginsType: ShadowsOnly).right(); |
430 | } else { |
431 | xPos = 0; |
432 | xPos += ceButtonWidth * btnPos; |
433 | xPos += ceButtonSpacing * btnPos; |
434 | xPos += margins(marginsType: ShadowsOnly).left(); |
435 | // We are painting from the left to the right so the real |
436 | // position doesn't need to by moved by the size of the button. |
437 | xPos -= ceButtonWidth; |
438 | } |
439 | |
440 | yPos = margins().top(); |
441 | yPos += margins().bottom(); |
442 | yPos -= ceButtonWidth; |
443 | yPos /= 2; |
444 | |
445 | return QRectF(xPos, yPos, ceButtonWidth, ceButtonWidth); |
446 | } |
447 | |
448 | static void renderFlatRoundedButtonFrame(QPainter *painter, const QRect &rect, const QColor &color) |
449 | { |
450 | painter->save(); |
451 | painter->setRenderHint(hint: QPainter::Antialiasing, on: true); |
452 | painter->setPen(Qt::NoPen); |
453 | painter->setBrush(color); |
454 | painter->drawEllipse(r: rect); |
455 | painter->restore(); |
456 | } |
457 | |
458 | static void renderButtonIcon(const QString &svgIcon, QPainter *painter, const QRect &rect, const QColor &color) |
459 | { |
460 | painter->save(); |
461 | painter->setRenderHints(hints: QPainter::Antialiasing, on: true); |
462 | |
463 | QString icon = svgIcon; |
464 | QRegularExpression regexp("fill=[\"']#[0-9A-F]{6}[\"']", QRegularExpression::CaseInsensitiveOption); |
465 | QRegularExpression regexpAlt("fill:#[0-9A-F]{6}", QRegularExpression::CaseInsensitiveOption); |
466 | QRegularExpression regexpCurrentColor("fill=[\"']currentColor[\"']"); |
467 | icon.replace(re: regexp, after: QString("fill=\"%1\"").arg(a: color.name())); |
468 | icon.replace(re: regexpAlt, after: QString("fill:%1").arg(a: color.name())); |
469 | icon.replace(re: regexpCurrentColor, after: QString("fill=\"%1\"").arg(a: color.name())); |
470 | QSvgRenderer svgRenderer(icon.toLocal8Bit()); |
471 | svgRenderer.render(p: painter, bounds: rect); |
472 | |
473 | painter->restore(); |
474 | } |
475 | |
476 | static void renderButtonIcon(QWaylandAdwaitaDecoration::ButtonIcon buttonIcon, QPainter *painter, const QRect &rect) |
477 | { |
478 | QString iconName = buttonMap[buttonIcon]; |
479 | |
480 | painter->save(); |
481 | painter->setRenderHints(hints: QPainter::Antialiasing, on: true); |
482 | painter->drawPixmap(r: rect, pm: QIcon::fromTheme(name: iconName).pixmap(w: ceButtonWidth, h: ceButtonWidth)); |
483 | painter->restore(); |
484 | } |
485 | |
486 | static QWaylandAdwaitaDecoration::ButtonIcon iconFromButtonAndState(QWaylandAdwaitaDecoration::Button button, bool maximized) |
487 | { |
488 | if (button == QWaylandAdwaitaDecoration::Close) |
489 | return QWaylandAdwaitaDecoration::CloseIcon; |
490 | else if (button == QWaylandAdwaitaDecoration::Minimize) |
491 | return QWaylandAdwaitaDecoration::MinimizeIcon; |
492 | else if (button == QWaylandAdwaitaDecoration::Maximize && maximized) |
493 | return QWaylandAdwaitaDecoration::RestoreIcon; |
494 | else |
495 | return QWaylandAdwaitaDecoration::MaximizeIcon; |
496 | } |
497 | |
498 | void QWaylandAdwaitaDecoration::drawButton(Button button, QPainter *painter) |
499 | { |
500 | const Qt::WindowStates windowStates = waylandWindow()->windowStates(); |
501 | const bool maximized = windowStates & Qt::WindowMaximized; |
502 | |
503 | const QRect btnRect = buttonRect(button).toRect(); |
504 | renderFlatRoundedButtonFrame(painter, rect: btnRect, color: color(type: ButtonBackground, button)); |
505 | |
506 | QRect adjustedBtnRect = btnRect; |
507 | adjustedBtnRect.setSize(QSize(16, 16)); |
508 | adjustedBtnRect.translate(dx: 4, dy: 4); |
509 | const QString svgIcon = m_icons[iconFromButtonAndState(button, maximized)]; |
510 | if (!svgIcon.isEmpty()) |
511 | renderButtonIcon(svgIcon, painter, rect: adjustedBtnRect, color: color(type: Foreground)); |
512 | else // Fallback to use QIcon |
513 | renderButtonIcon(buttonIcon: iconFromButtonAndState(button, maximized), painter, rect: adjustedBtnRect); |
514 | } |
515 | |
516 | QColor QWaylandAdwaitaDecoration::color(ColorType type, Button button) |
517 | { |
518 | const bool active = waylandWindow()->windowStates() & Qt::WindowActive; |
519 | |
520 | switch (type) { |
521 | case Background: |
522 | case BackgroundInactive: |
523 | return active ? m_colors[Background] : m_colors[BackgroundInactive]; |
524 | case Foreground: |
525 | case ForegroundInactive: |
526 | return active ? m_colors[Foreground] : m_colors[ForegroundInactive]; |
527 | case Border: |
528 | case BorderInactive: |
529 | return active ? m_colors[Border] : m_colors[BorderInactive]; |
530 | case ButtonBackground: |
531 | case ButtonBackgroundInactive: |
532 | case HoveredButtonBackground: { |
533 | if (m_clicking == button) { |
534 | return m_colors[PressedButtonBackground]; |
535 | } else if (m_hoveredButtons.testFlag(flag: button)) { |
536 | return m_colors[HoveredButtonBackground]; |
537 | } |
538 | return active ? m_colors[ButtonBackground] : m_colors[ButtonBackgroundInactive]; |
539 | } |
540 | default: |
541 | return m_colors[Background]; |
542 | } |
543 | } |
544 | |
545 | bool QWaylandAdwaitaDecoration::clickButton(Qt::MouseButtons b, Button btn) |
546 | { |
547 | auto repaint = qScopeGuard(f: [this] { requestRepaint(); }); |
548 | |
549 | if (isLeftClicked(newMouseButtonState: b)) { |
550 | m_clicking = btn; |
551 | return false; |
552 | } else if (isLeftReleased(newMouseButtonState: b)) { |
553 | if (m_clicking == btn) { |
554 | m_clicking = None; |
555 | return true; |
556 | } else { |
557 | m_clicking = None; |
558 | } |
559 | } |
560 | return false; |
561 | } |
562 | |
563 | bool QWaylandAdwaitaDecoration::doubleClickButton(Qt::MouseButtons b, const QPointF &local, |
564 | const QDateTime ¤tTime) |
565 | { |
566 | if (isLeftClicked(newMouseButtonState: b)) { |
567 | const qint64 clickInterval = m_lastButtonClick.msecsTo(currentTime); |
568 | m_lastButtonClick = currentTime; |
569 | const int doubleClickDistance = 5; |
570 | const QPointF posDiff = m_lastButtonClickPosition - local; |
571 | if ((clickInterval <= 500) |
572 | && ((posDiff.x() <= doubleClickDistance && posDiff.x() >= -doubleClickDistance) |
573 | && ((posDiff.y() <= doubleClickDistance && posDiff.y() >= -doubleClickDistance)))) { |
574 | return true; |
575 | } |
576 | |
577 | m_lastButtonClickPosition = local; |
578 | } |
579 | |
580 | return false; |
581 | } |
582 | |
583 | void QWaylandAdwaitaDecoration::updateButtonHoverState(Button hoveredButton) |
584 | { |
585 | bool currentCloseButtonState = m_hoveredButtons.testFlag(flag: Close); |
586 | bool currentMaximizeButtonState = m_hoveredButtons.testFlag(flag: Maximize); |
587 | bool currentMinimizeButtonState = m_hoveredButtons.testFlag(flag: Minimize); |
588 | |
589 | m_hoveredButtons.setFlag(flag: Close, on: hoveredButton == Button::Close); |
590 | m_hoveredButtons.setFlag(flag: Maximize, on: hoveredButton == Button::Maximize); |
591 | m_hoveredButtons.setFlag(flag: Minimize, on: hoveredButton == Button::Minimize); |
592 | |
593 | if (m_hoveredButtons.testFlag(flag: Close) != currentCloseButtonState |
594 | || m_hoveredButtons.testFlag(flag: Maximize) != currentMaximizeButtonState |
595 | || m_hoveredButtons.testFlag(flag: Minimize) != currentMinimizeButtonState) { |
596 | requestRepaint(); |
597 | } |
598 | } |
599 | |
600 | void QWaylandAdwaitaDecoration::processMouseTop(QWaylandInputDevice *inputDevice, const QPointF &local, |
601 | Qt::MouseButtons b, Qt::KeyboardModifiers mods) |
602 | { |
603 | Q_UNUSED(mods) |
604 | |
605 | QDateTime currentDateTime = QDateTime::currentDateTime(); |
606 | QRect surfaceRect = waylandWindow()->windowContentGeometry() + margins(marginsType: ShadowsOnly); |
607 | |
608 | if (!buttonRect(button: Close).contains(p: local) && !buttonRect(button: Maximize).contains(p: local) |
609 | && !buttonRect(button: Minimize).contains(p: local)) |
610 | updateButtonHoverState(hoveredButton: Button::None); |
611 | |
612 | if (local.y() <= surfaceRect.top() + margins().bottom()) { |
613 | if (local.x() <= margins().left()) { |
614 | // top left bit |
615 | #if QT_CONFIG(cursor) |
616 | waylandWindow()->setMouseCursor(device: inputDevice, cursor: Qt::SizeFDiagCursor); |
617 | #endif |
618 | startResize(inputDevice, edges: Qt::TopEdge | Qt::LeftEdge, buttons: b); |
619 | } else if (local.x() > surfaceRect.right() - margins().left()) { |
620 | // top right bit |
621 | #if QT_CONFIG(cursor) |
622 | waylandWindow()->setMouseCursor(device: inputDevice, cursor: Qt::SizeBDiagCursor); |
623 | #endif |
624 | startResize(inputDevice, edges: Qt::TopEdge | Qt::RightEdge, buttons: b); |
625 | } else { |
626 | // top resize bit |
627 | #if QT_CONFIG(cursor) |
628 | waylandWindow()->setMouseCursor(device: inputDevice, cursor: Qt::SizeVerCursor); |
629 | #endif |
630 | startResize(inputDevice, edges: Qt::TopEdge, buttons: b); |
631 | } |
632 | } else if (local.x() <= surfaceRect.left() + margins().left()) { |
633 | processMouseLeft(inputDevice, local, b, mods); |
634 | } else if (local.x() > surfaceRect.right() - margins().right()) { |
635 | processMouseRight(inputDevice, local, b, mods); |
636 | } else if (buttonRect(button: Close).contains(p: local)) { |
637 | if (clickButton(b, btn: Close)) { |
638 | QWindowSystemInterface::handleCloseEvent(window: window()); |
639 | m_hoveredButtons.setFlag(flag: Close, on: false); |
640 | } |
641 | updateButtonHoverState(hoveredButton: Close); |
642 | } else if (m_buttons.contains(key: Maximize) && buttonRect(button: Maximize).contains(p: local)) { |
643 | updateButtonHoverState(hoveredButton: Maximize); |
644 | if (clickButton(b, btn: Maximize)) { |
645 | window()->setWindowStates(window()->windowStates() ^ Qt::WindowMaximized); |
646 | m_hoveredButtons.setFlag(flag: Maximize, on: false); |
647 | } |
648 | } else if (m_buttons.contains(key: Minimize) && buttonRect(button: Minimize).contains(p: local)) { |
649 | updateButtonHoverState(hoveredButton: Minimize); |
650 | if (clickButton(b, btn: Minimize)) { |
651 | window()->setWindowState(Qt::WindowMinimized); |
652 | m_hoveredButtons.setFlag(flag: Minimize, on: false); |
653 | } |
654 | } else if (doubleClickButton(b, local, currentTime: currentDateTime)) { |
655 | window()->setWindowStates(window()->windowStates() ^ Qt::WindowMaximized); |
656 | } else { |
657 | // Show window menu |
658 | if (b == Qt::MouseButton::RightButton) |
659 | waylandWindow()->shellSurface()->showWindowMenu(seat: inputDevice); |
660 | #if QT_CONFIG(cursor) |
661 | waylandWindow()->restoreMouseCursor(device: inputDevice); |
662 | #endif |
663 | startMove(inputDevice, buttons: b); |
664 | } |
665 | } |
666 | |
667 | void QWaylandAdwaitaDecoration::processMouseBottom(QWaylandInputDevice *inputDevice, const QPointF &local, |
668 | Qt::MouseButtons b, Qt::KeyboardModifiers mods) |
669 | { |
670 | Q_UNUSED(mods) |
671 | if (local.x() <= margins().left()) { |
672 | // bottom left bit |
673 | #if QT_CONFIG(cursor) |
674 | waylandWindow()->setMouseCursor(device: inputDevice, cursor: Qt::SizeBDiagCursor); |
675 | #endif |
676 | startResize(inputDevice, edges: Qt::BottomEdge | Qt::LeftEdge, buttons: b); |
677 | } else if (local.x() > window()->width() + margins().right()) { |
678 | // bottom right bit |
679 | #if QT_CONFIG(cursor) |
680 | waylandWindow()->setMouseCursor(device: inputDevice, cursor: Qt::SizeFDiagCursor); |
681 | #endif |
682 | startResize(inputDevice, edges: Qt::BottomEdge | Qt::RightEdge, buttons: b); |
683 | } else { |
684 | // bottom bit |
685 | #if QT_CONFIG(cursor) |
686 | waylandWindow()->setMouseCursor(device: inputDevice, cursor: Qt::SizeVerCursor); |
687 | #endif |
688 | startResize(inputDevice, edges: Qt::BottomEdge, buttons: b); |
689 | } |
690 | } |
691 | |
692 | void QWaylandAdwaitaDecoration::processMouseLeft(QWaylandInputDevice *inputDevice, const QPointF &local, |
693 | Qt::MouseButtons b, Qt::KeyboardModifiers mods) |
694 | { |
695 | Q_UNUSED(local) |
696 | Q_UNUSED(mods) |
697 | #if QT_CONFIG(cursor) |
698 | waylandWindow()->setMouseCursor(device: inputDevice, cursor: Qt::SizeHorCursor); |
699 | #endif |
700 | startResize(inputDevice, edges: Qt::LeftEdge, buttons: b); |
701 | } |
702 | |
703 | void QWaylandAdwaitaDecoration::processMouseRight(QWaylandInputDevice *inputDevice, const QPointF &local, |
704 | Qt::MouseButtons b, Qt::KeyboardModifiers mods) |
705 | { |
706 | Q_UNUSED(local) |
707 | Q_UNUSED(mods) |
708 | #if QT_CONFIG(cursor) |
709 | waylandWindow()->setMouseCursor(device: inputDevice, cursor: Qt::SizeHorCursor); |
710 | #endif |
711 | startResize(inputDevice, edges: Qt::RightEdge, buttons: b); |
712 | } |
713 | |
714 | void QWaylandAdwaitaDecoration::requestRepaint() const |
715 | { |
716 | // Set dirty flag |
717 | if (waylandWindow()->decoration()) |
718 | waylandWindow()->decoration()->update(); |
719 | |
720 | // Request re-paint |
721 | waylandWindow()->window()->requestUpdate(); |
722 | } |
723 | |
724 | } // namespace QtWaylandClient |
725 | |
726 | QT_END_NAMESPACE |
727 | |
728 | #include "moc_qwaylandadwaitadecoration_p.cpp" |
729 |
Definitions
- ceButtonSpacing
- ceButtonWidth
- ceCornerRadius
- ceShadowsWidth
- ceTitlebarHeight
- ceWindowBorderWidth
- buttonMap
- operator>>
- lcQWaylandAdwaitaDecorationLog
- QWaylandAdwaitaDecoration
- margins
- paint
- handleMouse
- handleTouch
- getIconSvg
- loadConfiguration
- updateColors
- updateTitlebarLayout
- settingChanged
- buttonRect
- renderFlatRoundedButtonFrame
- renderButtonIcon
- renderButtonIcon
- iconFromButtonAndState
- drawButton
- color
- clickButton
- doubleClickButton
- updateButtonHoverState
- processMouseTop
- processMouseBottom
- processMouseLeft
- processMouseRight
Learn Advanced QML with KDAB
Find out more