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