diff options
Diffstat (limited to 'src/gui/platform')
35 files changed, 2007 insertions, 418 deletions
diff --git a/src/gui/platform/android/qandroidnativeinterface.cpp b/src/gui/platform/android/qandroidnativeinterface.cpp index 3062b5255e..c1c4b7149f 100644 --- a/src/gui/platform/android/qandroidnativeinterface.cpp +++ b/src/gui/platform/android/qandroidnativeinterface.cpp @@ -7,6 +7,8 @@ #include <QtGui/qoffscreensurface_platform.h> #include <QtGui/private/qguiapplication_p.h> +#include <QtGui/qpa/qplatformscreen_p.h> + QT_BEGIN_NAMESPACE using namespace QNativeInterface::Private; @@ -33,4 +35,20 @@ QOffscreenSurface *QNativeInterface::QAndroidOffscreenSurface::fromNative(ANati &QAndroidOffScreenIntegration::createOffscreenSurface>(nativeSurface); } +/*! + \class QNativeInterface::QAndroidScreen + \since 6.7 + \brief Native interface to a screen. + + Accessed through QScreen::nativeInterface(). + \inmodule QtGui + \ingroup native-interfaces + \ingroup native-interfaces-qscreen +*/ +/*! + \fn int QNativeInterface::QAndroidScreen::displayId() const; + \return the id of the underlying Android display. +*/ +QT_DEFINE_NATIVE_INTERFACE(QAndroidScreen); + QT_END_NAMESPACE diff --git a/src/gui/platform/darwin/qappleiconengine.mm b/src/gui/platform/darwin/qappleiconengine.mm new file mode 100644 index 0000000000..7e0ed184dc --- /dev/null +++ b/src/gui/platform/darwin/qappleiconengine.mm @@ -0,0 +1,464 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qappleiconengine_p.h" + +#if defined(Q_OS_MACOS) +# include <AppKit/AppKit.h> +#elif defined(QT_PLATFORM_UIKIT) +# include <UIKit/UIKit.h> +#endif + +#include <QtGui/qguiapplication.h> +#include <QtGui/qpainter.h> +#include <QtGui/qpalette.h> +#include <QtGui/qstylehints.h> + +#include <QtGui/private/qcoregraphics_p.h> + +QT_BEGIN_NAMESPACE + +using namespace Qt::StringLiterals; + +namespace { +auto *loadImage(const QString &iconName) +{ + static constexpr std::pair<QLatin1StringView, NSString *> iconMap[] = { + {"address-book-new"_L1, @"book.closed"}, + {"application-exit"_L1, @"xmark.circle"}, + {"appointment-new"_L1, @"calendar.badge.plus"}, + {"call-start"_L1, @"phone.arrow.up.right"}, + {"call-stop"_L1, @"phone.down"}, + {"contact-new"_L1, @"person.crop.circle.badge.plus"}, + {"document-new"_L1, @"doc.badge.plus"}, + {"document-open"_L1, @"folder"}, + {"document-open-recent"_L1, @"doc.badge.clock"}, + {"document-page-setup"_L1, @"doc.badge.gearshape"}, + {"document-print"_L1, @"printer"}, + //{"document-print-preview"_L1, @""}, + {"document-properties"_L1, @"doc.badge.ellipsis"}, + //{"document-revert"_L1, @""}, + {"document-save"_L1, @"square.and.arrow.down"}, + //{"document-save-as"_L1, @""}, + {"document-send"_L1, @"paperplane"}, + {"edit-clear"_L1, @"xmark.circle"}, + {"edit-copy"_L1, @"doc.on.doc"}, + {"edit-cut"_L1, @"scissors"}, + {"edit-delete"_L1, @"delete.left"}, + {"edit-find"_L1, @"magnifyingglass"}, + //{"edit-find-replace"_L1, @"arrow.up.left.and.down.right.magnifyingglass"}, + {"edit-paste"_L1, @"clipboard"}, + {"edit-redo"_L1, @"arrowshape.turn.up.right"}, + //{"edit-select-all"_L1, @""}, + {"edit-undo"_L1, @"arrowshape.turn.up.left"}, + {"folder-new"_L1, @"folder.badge.plus"}, + {"format-indent-less"_L1, @"decrease.indent"}, + {"format-indent-more"_L1, @"increase.indent"}, + {"format-justify-center"_L1, @"text.aligncenter"}, + {"format-justify-fill"_L1, @"text.justify"}, + {"format-justify-left"_L1, @"text.justify.left"}, + {"format-justify-right"_L1, @"text.justify.right"}, + {"format-text-direction-ltr"_L1, @"text.justify.leading"}, + {"format-text-direction-rtl"_L1, @"text.justify.trailing"}, + {"format-text-bold"_L1, @"bold"}, + {"format-text-italic"_L1, @"italic"}, + {"format-text-underline"_L1, @"underline"}, + {"format-text-strikethrough"_L1, @"strikethrough"}, + //{"go-bottom"_L1, @""}, + {"go-down"_L1, @"arrowshape.down"}, + {"go-first"_L1, @"increase.indent"}, + {"go-home"_L1, @"house"}, + //{"go-jump"_L1, @""}, + //{"go-last"_L1, @""}, + {"go-next"_L1, @"arrowshape.right"}, + {"go-previous"_L1, @"arrowshape.left"}, + //{"go-top"_L1, @""}, + {"go-up"_L1, @"arrowshape.up"}, + {"help-about"_L1, @"info.circle"}, + //{"help-contents"_L1, @""}, + {"help-faq"_L1, @"questionmark.app"}, + {"insert-image"_L1, @"photo.badge.plus"}, + {"insert-link"_L1, @"link.badge.plus"}, + //{"insert-object"_L1, @""}, + {"insert-text"_L1, @"textformat"}, + {"list-add"_L1, @"plus.circle"}, + {"list-remove"_L1, @"minus.circle"}, + {"mail-forward"_L1, @"arrowshape.turn.up.right"}, + {"mail-mark-important"_L1, @"star"}, + {"mail-mark-junk"_L1, @"xmark.bin"}, + {"mail-mark-notjunk"_L1, @"trash.slash"}, + {"mail-mark-read"_L1, @"envelope.open"}, + {"mail-mark-unread"_L1, @"envelope.fill"}, + {"mail-message-new"_L1, @"square.and.pencil"}, + {"mail-reply-all"_L1, @"arrowshape.turn.up.left.2"}, + {"mail-reply-sender"_L1, @"arrowshape.turn.up.left"}, + {"mail-send"_L1, @"paperplane"}, + {"mail-send-receive"_L1, @"envelope.arrow.triangle.branch"}, + {"media-eject"_L1, @"eject"}, + {"media-playback-pause"_L1, @"pause"}, + {"media-playback-start"_L1, @"play"}, + {"media-playback-stop"_L1, @"stop"}, + {"media-record"_L1, @"record.circle"}, + {"media-seek-backward"_L1, @"backward"}, + {"media-seek-forward"_L1, @"forward"}, + {"media-skip-backward"_L1, @"backward.end.alt"}, + {"media-skip-forward"_L1, @"forward.end.alt"}, + {"object-flip-horizontal"_L1, @"rectangle.landscape.rotate"}, + {"object-flip-vertical"_L1, @"rectangle.portrait.rotate"}, + {"object-rotate-left"_L1, @"rotate.left"}, + {"object-rotate-right"_L1, @"rotate.right"}, + {"process-stop"_L1, @"stop.circle"}, + {"system-lock-screen"_L1, @"lock.display"}, + {"system-log-out"_L1, @"door.left.hand.open"}, + //{"system-run"_L1, @""}, + {"system-search"_L1, @"magnifyingglass"}, + //{"system-reboot"_L1, @""}, + {"system-shutdown"_L1, @"power"}, + //{"tools-check-spelling"_L1, @""}, + {"view-fullscreen"_L1, @"arrow.up.left.and.arrow.down.right"}, + {"view-refresh"_L1, @"arrow.clockwise"}, + {"view-restore"_L1, @"arrow.down.right.and.arrow.up.left"}, + //{"view-sort-ascending"_L1, @""}, + //{"view-sort-descending"_L1, @""}, + {"window-close"_L1, @"xmark.circle"}, + {"window-new"_L1, @"macwindow.badge.plus"}, + {"zoom-fit-best"_L1, @"square.arrowtriangle.4.outward"}, + {"zoom-in"_L1, @"plus.magnifyingglass"}, + //{"zoom-original"_L1, @""}, + {"zoom-out"_L1, @"minus.magnifyingglass"}, + {"process-working"_L1, @"circle.dotted"}, + //{"accessories-calculator"_L1, @""}, + //{"accessories-character-map"_L1, @""}, + {"accessories-dictionary"_L1, @"character.book.closed"}, + {"accessories-text-editor"_L1, @"textformat"}, + {"help-browser"_L1, @"folder.badge.questionmark"}, + {"multimedia-volume-control"_L1, @"speaker.wave.3"}, + {"preferences-desktop-accessibility"_L1, @"accessibility"}, + //{"preferences-desktop-font"_L1, @""}, + {"preferences-desktop-keyboard"_L1, @"keyboard.badge.ellipsis"}, + //{"preferences-desktop-locale"_L1, @""}, + //{"preferences-desktop-multimedia"_L1, @""}, + //{"preferences-desktop-screensaver"_L1, @""}, + //{"preferences-desktop-theme"_L1, @""}, + //{"preferences-desktop-wallpaper"_L1, @""}, + {"system-file-manager"_L1, @"folder.badge.gearshape"}, + //{"system-software-install"_L1, @""}, + //{"system-software-update"_L1, @""}, d + //{"utilities-system-monitor"_L1, @""}, + {"utilities-terminal"_L1, @"apple.terminal"}, + //{"applications-accessories"_L1, @""}, + //{"applications-development"_L1, @""}, + //{"applications-engineering"_L1, @""}, + {"applications-games"_L1, @"gamecontroller"}, + //{"applications-graphics"_L1, @""}, + {"applications-internet"_L1, @"network"}, + {"applications-multimedia"_L1, @"tv.and.mediabox"}, + //{"applications-office"_L1, @""}, + //{"applications-other"_L1, @""}, + {"applications-science"_L1, @"atom"}, + //{"applications-system"_L1, @""}, + //{"applications-utilities"_L1, @""}, + {"preferences-desktop"_L1, @"menubar.dock.rectangle"}, + //{"preferences-desktop-peripherals"_L1, @""}, + //{"preferences-desktop-personal"_L1, @""}, + //{"preferences-other"_L1, @""}, + //{"preferences-system"_L1, @""}, + {"preferences-system-network"_L1, @"network"}, + {"system-help"_L1, @"questionmark.diamond"}, + {"audio-card"_L1, @"waveform.circle"}, + {"audio-input-microphone"_L1, @"mic"}, + {"battery"_L1, @"battery.100percent"}, + {"camera-photo"_L1, @"camera"}, + {"camera-video"_L1, @"video"}, + {"camera-web"_L1, @"web.camera"}, + {"computer"_L1, @"desktopcomputer"}, + {"drive-harddisk"_L1, @"internaldrive"}, + {"drive-optical"_L1, @"opticaldiscdrive"}, + {"drive-removable-media"_L1, @"externaldrive"}, + {"input-gaming"_L1, @"gamecontroller"}, // "games" also using this one + {"input-keyboard"_L1, @"keyboard"}, + {"input-mouse"_L1, @"computermouse"}, + {"input-tablet"_L1, @"ipad"}, + {"media-flash"_L1, @"mediastick"}, + //{"media-floppy"_L1, @""}, + //{"media-optical"_L1, @""}, + {"media-tape"_L1, @"recordingtape"}, + //{"modem"_L1, @""}, + {"multimedia-player"_L1, @"play.rectangle"}, + {"network-wired"_L1, @"app.connected.to.app.below.fill"}, + {"network-wireless"_L1, @"wifi"}, + //{"pda"_L1, @""}, + {"phone"_L1, @"iphone"}, + {"printer"_L1, @"printer"}, + {"scanner"_L1, @"scanner"}, + {"video-display"_L1, @"play.display"}, + //{"emblem-default"_L1, @""}, + {"emblem-documents"_L1, @"doc.circle"}, + {"emblem-downloads"_L1, @"arrow.down.circle"}, + {"emblem-favorite"_L1, @"star"}, + {"emblem-important"_L1, @"exclamationmark.bubble.circle"}, + {"emblem-mail"_L1, @"envelope"}, + {"emblem-photos"_L1, @"photo.stack"}, + //{"emblem-readonly"_L1, @""}, + {"emblem-shared"_L1, @"folder.badge.person.crop"}, + {"emblem-symbolic-link"_L1, @"link.circle"}, + {"emblem-synchronized"_L1, @"arrow.triangle.2.circlepath.circle"}, + {"emblem-system"_L1, @"gear"}, + //{"emblem-unreadable"_L1, @""}, + {"folder"_L1, @"folder"}, + //{"folder-remote"_L1, @""}, + {"network-server"_L1, @"server.rack"}, + //{"network-workgroup"_L1, @""}, + //{"start-here"_L1, @""}, + {"user-bookmarks"_L1, @"bookmark.circle"}, + {"user-desktop"_L1, @"desktopcomputer"}, //"computer" also using this one + {"user-home"_L1, @"house"}, //"go-home" also using this one + {"user-trash"_L1, @"trash"}, + {"appointment-missed"_L1, @"calendar.badge.exclamationmark"}, + {"appointment-soon"_L1, @"calendar.badge.clock"}, + {"audio-volume-high"_L1, @"speaker.wave.3"}, + {"audio-volume-low"_L1, @"speaker.wave.1"}, + {"audio-volume-medium"_L1, @"speaker.wave.2"}, + {"audio-volume-muted"_L1, @"speaker.slash"}, + {"battery-caution"_L1, @"minus.plus.batteryblock.exclamationmark"}, + {"battery-low"_L1, @"battery.25percent"}, // there are different levels that can be low battery + {"dialog-error"_L1, @"exclamationmark.bubble"}, + {"dialog-information"_L1, @"info.circle"}, + {"dialog-password"_L1, @"lock"}, + {"dialog-question"_L1, @"questionmark.circle"}, + {"dialog-warning"_L1, @"exclamationmark.octagon"}, + {"folder-drag-accept"_L1, @"plus.rectangle.on.folder"}, + //{"folder-open"_L1, @""}, + {"folder-visiting"_L1, @"folder.circle"}, + {"image-loading"_L1, @"photo.circle"}, + {"image-missing"_L1, @"photo"}, + {"mail-attachment"_L1, @"paperclip"}, + {"mail-unread"_L1, @"envelope.badge"}, + {"mail-read"_L1, @"envelope.open"}, + {"mail-replied"_L1, @"arrowshape.turn.up.left"}, + //{"mail-signed"_L1, @""}, + //{"mail-signed-verified"_L1, @""}, + {"media-playlist-repeat"_L1, @"repet"}, + {"media-playlist-shuffle"_L1, @"shuffle"}, + //{"network-error"_L1, @""}, + //{"network-idle"_L1, @""}, + {"network-offline"_L1, @"network.slash"}, + //{"network-receive"_L1, @""}, + //{"network-transmit"_L1, @""}, + //{"network-transmit-receive"_L1, @""}, + //{"printer-error"_L1, @""}, + {"printer-printing"_L1, @"printer.dotmatrix.filled.and.paper"}, // not sure + {"security-high"_L1, @"lock.shield"}, + //{"security-medium"_L1, @""}, + {"security-low"_L1, @"lock.trianglebadge.exclamationmark"}, + {"software-update-available"_L1, @"arrowshape.up.circle"}, + {"software-update-urgent"_L1, @"exclamationmark.transmission"}, + {"sync-error"_L1, @"exclamationmark.arrow.triangle.2.circlepath"}, + {"sync-synchronizing"_L1, @"arrow.triangle.2.circlepath"}, + {"task-due"_L1, @"clock.badge.exclamationmark"}, + {"task-past-due"_L1, @"clock.badge.xmark"}, + {"user-available"_L1, @"person.crop.circle.badge.checkmark"}, + {"user-away"_L1, @"person.crop.circle.badge.clock"}, + //{"user-idle"_L1, @""}, + {"user-offline"_L1, @"person.crop.circle.badge.xmark"}, + //{"user-trash-full"_L1, @""}, + {"weather-clear"_L1, @"sun.max"}, + {"weather-clear-night"_L1, @"moon"}, + {"weather-few-clouds"_L1, @"cloud.sun"}, + {"weather-few-clouds-night"_L1, @"cloud.moon"}, + {"weather-fog"_L1, @"cloud.fog"}, + {"weather-overcast"_L1, @"cloud"}, + //{"weather-severe-alert"_L1, @""}, + {"weather-showers"_L1, @"cloud.rain"}, + //{"weather-showers-scattered"_L1, @""}, + {"weather-snow"_L1, @"cloud.snow"}, + {"weather-storm"_L1, @"tropicalstorm"}, + }; + const auto it = std::find_if(std::begin(iconMap), std::end(iconMap), [iconName](const auto &c){ + return c.first == iconName; + }); + NSString *systemIconName = it != std::end(iconMap) ? it->second : iconName.toNSString(); +#if defined(Q_OS_MACOS) + return [NSImage imageWithSystemSymbolName:systemIconName accessibilityDescription:nil]; +#elif defined(QT_PLATFORM_UIKIT) + return [UIImage systemImageNamed:systemIconName]; +#endif +} +} + +QAppleIconEngine::QAppleIconEngine(const QString &iconName) + : m_iconName(iconName), m_image(loadImage(iconName)) +{ + if (m_image) + [m_image retain]; +} + +QAppleIconEngine::~QAppleIconEngine() +{ + if (m_image) + [m_image release]; +} + +QIconEngine *QAppleIconEngine::clone() const +{ + return new QAppleIconEngine(m_iconName); +} + +QString QAppleIconEngine::key() const +{ + return u"QAppleIconEngine"_s; +} + +QString QAppleIconEngine::iconName() +{ + return m_iconName; +} + +bool QAppleIconEngine::isNull() +{ + return m_image == nullptr; +} + +QList<QSize> QAppleIconEngine::availableIconSizes(double aspectRatio) +{ + const qreal devicePixelRatio = qGuiApp->devicePixelRatio(); + const QList<QSize> sizes = { + {qRound(16 * devicePixelRatio), qRound(16. * devicePixelRatio / aspectRatio)}, + {qRound(32 * devicePixelRatio), qRound(32. * devicePixelRatio / aspectRatio)}, + {qRound(64 * devicePixelRatio), qRound(64. * devicePixelRatio / aspectRatio)}, + {qRound(128 * devicePixelRatio), qRound(128. * devicePixelRatio / aspectRatio)}, + {qRound(256 * devicePixelRatio), qRound(256. * devicePixelRatio / aspectRatio)}, + }; + return sizes; +} + +QList<QSize> QAppleIconEngine::availableSizes(QIcon::Mode, QIcon::State) +{ + const double aspectRatio = isNull() ? 1.0 : m_image.size.width / m_image.size.height; + return availableIconSizes(aspectRatio); +} + +QSize QAppleIconEngine::actualSize(const QSize &size, QIcon::Mode /*mode*/, QIcon::State /*state*/) +{ + const double inputAspectRatio = isNull() ? 1.0 : m_image.size.width / m_image.size.height; + const double outputAspectRatio = size.width() / size.height(); + QSize result = size; + if (outputAspectRatio > inputAspectRatio) + result.rwidth() = result.height() * inputAspectRatio; + else + result.rheight() = result.width() / inputAspectRatio; + return result; +} + +QPixmap QAppleIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) +{ + return scaledPixmap(size, mode, state, 1.0); +} + +namespace { +#if defined(Q_OS_MACOS) +auto *configuredImage(const NSImage *image, const QColor &color) +{ + auto *config = [NSImageSymbolConfiguration configurationWithPointSize:48 + weight:NSFontWeightRegular + scale:NSImageSymbolScaleLarge]; + if (@available(macOS 12, *)) { + auto *primaryColor = [NSColor colorWithSRGBRed:color.redF() + green:color.greenF() + blue:color.blueF() + alpha:color.alphaF()]; + + auto *colorConfig = [NSImageSymbolConfiguration configurationWithHierarchicalColor:primaryColor]; + config = [config configurationByApplyingConfiguration:colorConfig]; + } + + return [image imageWithSymbolConfiguration:config]; +} +#elif defined(QT_PLATFORM_UIKIT) +auto *configuredImage(const UIImage *image, const QColor &color) +{ + auto *config = [UIImageSymbolConfiguration configurationWithPointSize:48 + weight:UIImageSymbolWeightRegular + scale:UIImageSymbolScaleLarge]; + + if (@available(iOS 15, *)) { + auto *primaryColor = [UIColor colorWithRed:color.redF() + green:color.greenF() + blue:color.blueF() + alpha:color.alphaF()]; + + auto *colorConfig = [UIImageSymbolConfiguration configurationWithHierarchicalColor:primaryColor]; + config = [config configurationByApplyingConfiguration:colorConfig]; + } + return [image imageByApplyingSymbolConfiguration:config]; +} +#endif +} + +QPixmap QAppleIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) +{ + const quint64 cacheKey = calculateCacheKey(mode, state); + if (cacheKey != m_cacheKey || m_pixmap.size() != size || m_pixmap.devicePixelRatio() != scale) { + const QSize paintSize = actualSize(size, mode, state); + const QSize paintOffset = paintSize != size + ? (QSizeF(size - paintSize) * 0.5).toSize() + : QSize(); + + m_pixmap = QPixmap(size * scale); + m_pixmap.setDevicePixelRatio(scale); + m_pixmap.fill(Qt::transparent); + + QPainter painter(&m_pixmap); + paint(&painter, QRect(paintOffset.width(), paintOffset.height(), + paintSize.width(), paintSize.height()), mode, state); + + m_cacheKey = cacheKey; + } + return m_pixmap; +} + +void QAppleIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) +{ + Q_UNUSED(state); + + QColor color; + const QPalette palette; + switch (mode) { + case QIcon::Normal: + color = palette.color(QPalette::Inactive, QPalette::Text); + break; + case QIcon::Disabled: + color = palette.color(QPalette::Disabled, QPalette::Text); + break; + case QIcon::Active: + color = palette.color(QPalette::Active, QPalette::Text); + break; + case QIcon::Selected: + color = palette.color(QPalette::Active, QPalette::HighlightedText); + break; + } + const auto *image = configuredImage(m_image, color); + + QMacCGContext ctx(painter); + +#if defined(Q_OS_MACOS) + NSGraphicsContext *gc = [NSGraphicsContext graphicsContextWithCGContext:ctx flipped:YES]; + [NSGraphicsContext saveGraphicsState]; + [NSGraphicsContext setCurrentContext:gc]; + + const NSSize pixmapSize = NSMakeSize(rect.width(), rect.height()); + [image setSize:pixmapSize]; + const NSRect sourceRect = NSMakeRect(0, 0, pixmapSize.width, pixmapSize.height); + const NSRect iconRect = NSMakeRect(rect.x(), rect.y(), pixmapSize.width, pixmapSize.height); + + [image drawInRect:iconRect fromRect:sourceRect operation:NSCompositingOperationSourceOver fraction:1.0 respectFlipped:YES hints:nil]; + [NSGraphicsContext restoreGraphicsState]; +#elif defined(QT_PLATFORM_UIKIT) + UIGraphicsPushContext(ctx); + const CGRect cgrect = CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()); + [image drawInRect:cgrect]; + UIGraphicsPopContext(); +#endif +} + +QT_END_NAMESPACE diff --git a/src/gui/platform/darwin/qappleiconengine_p.h b/src/gui/platform/darwin/qappleiconengine_p.h new file mode 100644 index 0000000000..2a4ff7fc64 --- /dev/null +++ b/src/gui/platform/darwin/qappleiconengine_p.h @@ -0,0 +1,64 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QAPPLEICONENGINE_P_H +#define QAPPLEICONENGINE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtGui/qiconengine.h> + +#include <QtCore/private/qcore_mac_p.h> + +Q_FORWARD_DECLARE_OBJC_CLASS(UIImage); +Q_FORWARD_DECLARE_OBJC_CLASS(NSImage); + +QT_BEGIN_NAMESPACE + +class Q_GUI_EXPORT QAppleIconEngine : public QIconEngine +{ +public: + QAppleIconEngine(const QString &iconName); + ~QAppleIconEngine(); + QIconEngine *clone() const override; + QString key() const override; + QString iconName() override; + bool isNull() override; + + QList<QSize> availableSizes(QIcon::Mode, QIcon::State) override; + QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) override; + QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override; + QPixmap scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) override; + void paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) override; + + static QList<QSize> availableIconSizes(double aspectRatio = 1.0); + +private: + static constexpr quint64 calculateCacheKey(QIcon::Mode mode, QIcon::State state) + { + return (quint64(mode) << 32) | state; + } + + const QString m_iconName; +#if defined(Q_OS_MACOS) + const NSImage *m_image; +#elif defined(QT_PLATFORM_UIKIT) + const UIImage *m_image; +#endif + mutable QPixmap m_pixmap; + mutable quint64 m_cacheKey = {}; +}; + + +QT_END_NAMESPACE + +#endif // QAPPLEICONENGINE_P_H diff --git a/src/gui/platform/darwin/qapplekeymapper.mm b/src/gui/platform/darwin/qapplekeymapper.mm index a0a2eb208d..b8ff5c9d6d 100644 --- a/src/gui/platform/darwin/qapplekeymapper.mm +++ b/src/gui/platform/darwin/qapplekeymapper.mm @@ -18,7 +18,6 @@ QT_BEGIN_NAMESPACE -Q_LOGGING_CATEGORY(lcQpaKeyMapper, "qt.qpa.keymapper"); Q_LOGGING_CATEGORY(lcQpaKeyMapperKeys, "qt.qpa.keymapper.keys"); static Qt::KeyboardModifiers swapModifiersIfNeeded(const Qt::KeyboardModifiers modifiers) @@ -37,36 +36,6 @@ static Qt::KeyboardModifiers swapModifiersIfNeeded(const Qt::KeyboardModifiers m return swappedModifiers; } -Qt::Key QAppleKeyMapper::fromNSString(Qt::KeyboardModifiers qtModifiers, NSString *characters, - NSString *charactersIgnoringModifiers, QString &text) -{ - if ([characters isEqualToString:@"\t"]) { - if (qtModifiers & Qt::ShiftModifier) - return Qt::Key_Backtab; - return Qt::Key_Tab; - } else if ([characters isEqualToString:@"\r"]) { - if (qtModifiers & Qt::KeypadModifier) - return Qt::Key_Enter; - return Qt::Key_Return; - } - if ([characters length] != 0 || [charactersIgnoringModifiers length] != 0) { - QChar ch; - if (((qtModifiers & Qt::MetaModifier) || (qtModifiers & Qt::AltModifier)) && - ([charactersIgnoringModifiers length] != 0)) { - ch = QChar([charactersIgnoringModifiers characterAtIndex:0]); - } else if ([characters length] != 0) { - ch = QChar([characters characterAtIndex:0]); - } - if (!(qtModifiers & (Qt::ControlModifier | Qt::MetaModifier)) && - (ch.unicode() < 0xf700 || ch.unicode() > 0xf8ff)) { - text = QString::fromNSString(characters); - } - if (!ch.isNull()) - return Qt::Key(ch.toUpper().unicode()); - } - return Qt::Key_unknown; -} - #ifdef Q_OS_MACOS static constexpr std::tuple<NSEventModifierFlags, Qt::KeyboardModifier> cocoaModifierMap[] = { { NSEventModifierFlagShift, Qt::ShiftModifier }, @@ -360,7 +329,7 @@ QChar QAppleKeyMapper::toCocoaKey(Qt::Key key) { // Prioritize overloaded keys if (key == Qt::Key_Return) - return QChar(NSNewlineCharacter); + return QChar(NSCarriageReturnCharacter); if (key == Qt::Key_Backspace) return QChar(NSBackspaceCharacter); @@ -384,7 +353,7 @@ Qt::Key QAppleKeyMapper::fromCocoaKey(QChar keyCode) // ------------------------------------------------ -Qt::KeyboardModifiers QAppleKeyMapper::queryKeyboardModifiers() +Qt::KeyboardModifiers QAppleKeyMapper::queryKeyboardModifiers() const { return fromCocoaModifiers(NSEvent.modifierFlags); } @@ -538,11 +507,9 @@ const QAppleKeyMapper::KeyMap &QAppleKeyMapper::keyMapForKey(VirtualKeyCode virt where each modifier-key combination has been mapped to the key it will produce. */ -QList<int> QAppleKeyMapper::possibleKeys(const QKeyEvent *event) const +QList<QKeyCombination> QAppleKeyMapper::possibleKeyCombinations(const QKeyEvent *event) const { - QList<int> ret; - - qCDebug(lcQpaKeyMapper) << "Computing possible keys for" << event; + QList<QKeyCombination> ret; const auto nativeVirtualKey = event->nativeVirtualKey(); if (!nativeVirtualKey) @@ -555,16 +522,49 @@ QList<int> QAppleKeyMapper::possibleKeys(const QKeyEvent *event) const auto eventModifiers = event->modifiers(); - // The complete set of event modifiers, along with the - // unmodified key, is always a valid key combination, - // and the first priority. - ret << int(eventModifiers) + int(unmodifiedKey); + int startingModifierLayer = 0; + if (toCocoaModifiers(eventModifiers) & NSEventModifierFlagCommand) { + // When the Command key is pressed AppKit seems to do key equivalent + // matching using a Latin/Roman interpretation of the current keyboard + // layout. For example, for a Greek layout, pressing Option+Command+C + // produces a key event with chars="ç" and unmodchars="ψ", but AppKit + // still treats this as a match for a key equivalent of Option+Command+C. + // We can't do the same by just applying the modifiers to our key map, + // as that too contains "ψ" for the Option+Command combination. What we + // can do instead is take advantage of the fact that the Command + // modifier layer in all/most keyboard layouts contains a Latin + // layer. We then combine that with the modifiers of the event + // to produce the resulting "Latin" key combination. + static constexpr int kCommandLayer = 2; + ret << QKeyCombination::fromCombined( + int(eventModifiers) + int(keyMap[kCommandLayer])); + + // If the unmodified key is outside of Latin1, we also treat + // that as a valid key combination, even if AppKit natively + // does not. For example, for a Greek layout, we still want + // to support Option+Command+ψ as a key combination, as it's + // unlikely to clash with the Latin key combination we added + // above. + + // However, if the unmodified key is within Latin1, we skip + // it, to avoid these types of conflicts. For example, in + // the same Greek layout, pressing the key next to Tab will + // produce a Latin ';' symbol, but we've already treated that + // as 'q' above, thanks to the Command modifier, so we skip + // the potential Command+; key combination. This is also in + // line with what AppKit natively does. + + // Skipping Latin1 unmodified keys also handles the case of + // a Latin layout, where the unmodified and modified keys + // are the same. + + if (unmodifiedKey <= 0xff) + startingModifierLayer = 1; + } // FIXME: We only compute the first 8 combinations. Why? - for (int i = 1; i < 8; ++i) { + for (int i = startingModifierLayer; i < 15; ++i) { auto keyAfterApplyingModifiers = keyMap[i]; - if (keyAfterApplyingModifiers == unmodifiedKey) - continue; if (!keyAfterApplyingModifiers) continue; @@ -575,18 +575,39 @@ QList<int> QAppleKeyMapper::possibleKeys(const QKeyEvent *event) const // If the event includes more modifiers than the candidate they // will need to be included in the resulting key combination. auto additionalModifiers = eventModifiers & ~candidateModifiers; - ret << int(additionalModifiers) + int(keyAfterApplyingModifiers); - } - } - if (lcQpaKeyMapper().isDebugEnabled()) { - qCDebug(lcQpaKeyMapper) << "Possible keys:"; - for (int keyAndModifiers : ret) { - auto keyCombination = QKeyCombination::fromCombined(keyAndModifiers); - auto keySequence = QKeySequence(keyCombination); - qCDebug(lcQpaKeyMapper).verbosity(0) << "\t-" - << keyCombination << "/" << keySequence << "/" - << qUtf8Printable(keySequence.toString(QKeySequence::NativeText)); + auto keyCombination = QKeyCombination::fromCombined( + int(additionalModifiers) + int(keyAfterApplyingModifiers)); + + // If there's an existing key combination with the same key, + // but a different set of modifiers, we want to choose only + // one of them, by priority (see below). + const auto existingCombination = std::find_if( + ret.begin(), ret.end(), [&](auto existingCombination) { + return existingCombination.key() == keyAfterApplyingModifiers; + }); + + if (existingCombination != ret.end()) { + // We prioritize the combination with the more specific + // modifiers. In the case where the number of modifiers + // are the same, we want to prioritize Command over Option + // over Control over Shift. Unfortunately the order (and + // hence value) of the modifiers in Qt::KeyboardModifier + // does not match our preferred order when Control and + // Meta is switched, but we can work around that by + // explicitly swapping the modifiers and using that + // for the comparison. This also works when the + // Qt::AA_MacDontSwapCtrlAndMeta application attribute + // is set, as the incoming modifiers are then left + // as is, and we can still trust the order. + auto existingModifiers = swapModifiersIfNeeded(existingCombination->keyboardModifiers()); + auto replacementModifiers = swapModifiersIfNeeded(additionalModifiers); + if (replacementModifiers > existingModifiers) + *existingCombination = keyCombination; + } else { + // All is good, no existing combination has this key + ret << keyCombination; + } } } @@ -597,6 +618,36 @@ QList<int> QAppleKeyMapper::possibleKeys(const QKeyEvent *event) const #else // iOS +Qt::Key QAppleKeyMapper::fromNSString(Qt::KeyboardModifiers qtModifiers, NSString *characters, + NSString *charactersIgnoringModifiers, QString &text) +{ + if ([characters isEqualToString:@"\t"]) { + if (qtModifiers & Qt::ShiftModifier) + return Qt::Key_Backtab; + return Qt::Key_Tab; + } else if ([characters isEqualToString:@"\r"]) { + if (qtModifiers & Qt::KeypadModifier) + return Qt::Key_Enter; + return Qt::Key_Return; + } + if ([characters length] != 0 || [charactersIgnoringModifiers length] != 0) { + QChar ch; + if (((qtModifiers & Qt::MetaModifier) || (qtModifiers & Qt::AltModifier)) && + ([charactersIgnoringModifiers length] != 0)) { + ch = QChar([charactersIgnoringModifiers characterAtIndex:0]); + } else if ([characters length] != 0) { + ch = QChar([characters characterAtIndex:0]); + } + if (!(qtModifiers & (Qt::ControlModifier | Qt::MetaModifier)) && + (ch.unicode() < 0xf700 || ch.unicode() > 0xf8ff)) { + text = QString::fromNSString(characters); + } + if (!ch.isNull()) + return Qt::Key(ch.toUpper().unicode()); + } + return Qt::Key_unknown; +} + // Keyboard keys (non-modifiers) API_AVAILABLE(ios(13.4)) Qt::Key QAppleKeyMapper::fromUIKitKey(NSString *keyCode) { diff --git a/src/gui/platform/darwin/qapplekeymapper_p.h b/src/gui/platform/darwin/qapplekeymapper_p.h index 34557c8ede..1f3494d16f 100644 --- a/src/gui/platform/darwin/qapplekeymapper_p.h +++ b/src/gui/platform/darwin/qapplekeymapper_p.h @@ -19,6 +19,8 @@ #include <Carbon/Carbon.h> #endif +#include <qpa/qplatformkeymapper.h> + #include <QtCore/QList> #include <QtCore/QHash> #include <QtGui/QKeyEvent> @@ -27,13 +29,12 @@ QT_BEGIN_NAMESPACE -class Q_GUI_EXPORT QAppleKeyMapper +class Q_GUI_EXPORT QAppleKeyMapper : public QPlatformKeyMapper { public: - static Qt::KeyboardModifiers queryKeyboardModifiers(); - QList<int> possibleKeys(const QKeyEvent *event) const; - static Qt::Key fromNSString(Qt::KeyboardModifiers qtMods, NSString *characters, - NSString *charactersIgnoringModifiers, QString &text); + Qt::KeyboardModifiers queryKeyboardModifiers() const override; + QList<QKeyCombination> possibleKeyCombinations(const QKeyEvent *event) const override; + #ifdef Q_OS_MACOS static Qt::KeyboardModifiers fromCocoaModifiers(NSEventModifierFlags cocoaModifiers); static NSEventModifierFlags toCocoaModifiers(Qt::KeyboardModifiers); @@ -41,6 +42,9 @@ public: static QChar toCocoaKey(Qt::Key key); static Qt::Key fromCocoaKey(QChar keyCode); #else + static Qt::Key fromNSString(Qt::KeyboardModifiers qtMods, NSString *characters, + NSString *charactersIgnoringModifiers, QString &text); + static Qt::Key fromUIKitKey(NSString *keyCode); static Qt::KeyboardModifiers fromUIKitModifiers(ulong uikitModifiers); static ulong toUIKitModifiers(Qt::KeyboardModifiers); diff --git a/src/gui/platform/darwin/qmacmimeregistry.mm b/src/gui/platform/darwin/qmacmimeregistry.mm index abf013248f..6710a0656f 100644 --- a/src/gui/platform/darwin/qmacmimeregistry.mm +++ b/src/gui/platform/darwin/qmacmimeregistry.mm @@ -21,20 +21,6 @@ Q_GLOBAL_STATIC(QStringList, globalDraggedTypesList) // implemented in qutimimeconverter.mm void registerBuiltInTypes(); -/*! - \fn void qRegisterDraggedTypes(const QStringList &types) - \relates QUtiMimeConverter - - Registers the given \a types as custom pasteboard types. - - This function should be called to enable the Drag and Drop events - for custom pasteboard types on Cocoa implementations. This is required - in addition to a QUtiMimeConverter subclass implementation. By default - drag and drop is enabled for all standard pasteboard types. - - \sa QUtiMimeConverter -*/ - void registerDraggedTypes(const QStringList &types) { (*globalDraggedTypesList()) += types; @@ -82,15 +68,15 @@ void destroyMimeTypes() */ QString flavorToMime(QUtiMimeConverter::HandlerScope scope, const QString &uti) { - MimeList *mimes = globalMimeList(); - for (MimeList::const_iterator it = mimes->constBegin(); it != mimes->constEnd(); ++it) { - const bool relevantScope = uchar((*it)->scope()) & uchar(scope); + const MimeList &mimes = *globalMimeList(); + for (const auto &mime : mimes) { + const bool relevantScope = mime->scope() & scope; #ifdef DEBUG_MIME_MAPS qDebug("QMacMimeRegistry::flavorToMime: attempting (%d) for uti %s [%s]", relevantScope, qPrintable(uti), qPrintable((*it)->mimeForUti(uti))); #endif if (relevantScope) { - QString mimeType = (*it)->mimeForUti(uti); + const QString mimeType = mime->mimeForUti(uti); if (!mimeType.isNull()) return mimeType; } @@ -119,11 +105,10 @@ void unregisterMimeConverter(QUtiMimeConverter *macMime) QList<QUtiMimeConverter *> all(QUtiMimeConverter::HandlerScope scope) { MimeList ret; - MimeList *mimes = globalMimeList(); - for (MimeList::const_iterator it = mimes->constBegin(); it != mimes->constEnd(); ++it) { - const bool relevantScope = uchar((*it)->scope()) & uchar(scope); - if (relevantScope) - ret.append((*it)); + const MimeList &mimes = *globalMimeList(); + for (const auto &mime : mimes) { + if (mime->scope() & scope) + ret.append(mime); } return ret; } diff --git a/src/gui/platform/darwin/qmetallayer.mm b/src/gui/platform/darwin/qmetallayer.mm new file mode 100644 index 0000000000..e8a27a7b06 --- /dev/null +++ b/src/gui/platform/darwin/qmetallayer.mm @@ -0,0 +1,73 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qmetallayer_p.h" + +#include <QtCore/qreadwritelock.h> +#include <QtCore/qrect.h> + +using namespace std::chrono_literals; + +QT_BEGIN_NAMESPACE +Q_LOGGING_CATEGORY(lcMetalLayer, "qt.gui.metal") +QT_END_NAMESPACE + +QT_USE_NAMESPACE + +@implementation QMetalLayer +{ + std::unique_ptr<QReadWriteLock> m_displayLock; +} + +- (instancetype)init +{ + if ((self = [super init])) { + m_displayLock.reset(new QReadWriteLock(QReadWriteLock::Recursive)); + self.mainThreadPresentation = nil; + } + + return self; +} + +- (QReadWriteLock &)displayLock +{ + return *m_displayLock.get(); +} + +- (void)setNeedsDisplay +{ + [self setNeedsDisplayInRect:CGRectInfinite]; +} + +- (void)setNeedsDisplayInRect:(CGRect)rect +{ + if (!self.needsDisplay) { + // We lock for writing here, blocking in case a secondary thread is in + // the middle of presenting to the layer, as we want the main thread's + // display to happen after the secondary thread finishes presenting. + qCDebug(lcMetalLayer) << "Locking" << self << "for writing" + << "due to needing display in rect" << QRectF::fromCGRect(rect); + + // For added safety, we use a 5 second timeout, and try to fail + // gracefully by not marking the layer as needing display, as + // doing so would lead us to unlock and unheld lock in displayLayer. + if (!self.displayLock.tryLockForWrite(5s)) { + qCWarning(lcMetalLayer) << "Timed out waiting for display lock"; + return; + } + } + + [super setNeedsDisplayInRect:rect]; +} + +- (id<CAMetalDrawable>)nextDrawable +{ + // Drop the presentation block early, so that if the main thread for + // some reason doesn't handle the presentation, the block won't hold on + // to a drawable unnecessarily. + self.mainThreadPresentation = nil; + return [super nextDrawable]; +} + + +@end diff --git a/src/gui/platform/darwin/qmetallayer_p.h b/src/gui/platform/darwin/qmetallayer_p.h new file mode 100644 index 0000000000..81f8760ec2 --- /dev/null +++ b/src/gui/platform/darwin/qmetallayer_p.h @@ -0,0 +1,41 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QMETALLAYER_P_H +#define QMETALLAYER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtGui/qtguiglobal.h> + +#include <QtCore/qreadwritelock.h> +#include <QtCore/qloggingcategory.h> +#include <QtCore/private/qcore_mac_p.h> + +#include <QuartzCore/CAMetalLayer.h> + +QT_BEGIN_NAMESPACE +class QReadWriteLock; + +QT_DECLARE_EXPORTED_QT_LOGGING_CATEGORY(lcMetalLayer, Q_GUI_EXPORT) + +QT_END_NAMESPACE + +#if defined(__OBJC__) +Q_GUI_EXPORT +#endif +QT_DECLARE_NAMESPACED_OBJC_INTERFACE(QMetalLayer, CAMetalLayer +@property (nonatomic, readonly) QT_PREPEND_NAMESPACE(QReadWriteLock) &displayLock; +@property (atomic, copy) void (^mainThreadPresentation)(); +) + +#endif // QMETALLAYER_P_H diff --git a/src/gui/platform/darwin/qutimimeconverter.h b/src/gui/platform/darwin/qutimimeconverter.h index f3de4c0663..e9297b5fa0 100644 --- a/src/gui/platform/darwin/qutimimeconverter.h +++ b/src/gui/platform/darwin/qutimimeconverter.h @@ -17,8 +17,9 @@ class QMimeData; class Q_GUI_EXPORT QUtiMimeConverter { + Q_DISABLE_COPY(QUtiMimeConverter) public: - enum class HandlerScope : uchar + enum class HandlerScopeFlag : uint8_t { DnD = 0x01, Clipboard = 0x02, @@ -27,9 +28,9 @@ public: All = DnD|Clipboard, AllCompatible = All|Qt_compatible }; + Q_DECLARE_FLAGS(HandlerScope, HandlerScopeFlag) QUtiMimeConverter(); - explicit QUtiMimeConverter(HandlerScope scope); // internal virtual ~QUtiMimeConverter(); HandlerScope scope() const { return m_scope; } @@ -45,8 +46,15 @@ public: virtual int count(const QMimeData *mimeData) const; private: + friend class QMacMimeTypeName; + friend class QMacMimeAny; + + explicit QUtiMimeConverter(HandlerScope scope); + const HandlerScope m_scope; }; +Q_DECLARE_OPERATORS_FOR_FLAGS(QUtiMimeConverter::HandlerScope) + QT_END_NAMESPACE diff --git a/src/gui/platform/darwin/qutimimeconverter.mm b/src/gui/platform/darwin/qutimimeconverter.mm index 8f8e348c0d..ee643fd0c6 100644 --- a/src/gui/platform/darwin/qutimimeconverter.mm +++ b/src/gui/platform/darwin/qutimimeconverter.mm @@ -3,6 +3,7 @@ #include <ImageIO/ImageIO.h> #include <CoreFoundation/CoreFoundation.h> +#include <UniformTypeIdentifiers/UTCoreTypes.h> #include <QtCore/qsystemdetection.h> #include <QtCore/qurl.h> @@ -36,11 +37,10 @@ using namespace Qt::StringLiterals; /*! \class QUtiMimeConverter - \internal \brief The QUtiMimeConverter class converts between a MIME type and a \l{https://developer.apple.com/documentation/uniformtypeidentifiers} {Uniform Type Identifier (UTI)} format. - \since 4.2 + \since 6.5 \ingroup draganddrop \inmodule QtGui @@ -54,7 +54,21 @@ using namespace Qt::StringLiterals; By subclasses this class, one can extend Qt's drag and drop and clipboard handling to convert to and from unsupported, or proprietary, UTI formats. - A subclass of QUtiMimeConverter will automatically be registered, and active, upon instantiation. + Construct an instance of your converter implementation after instantiating + QGuiApplication: + + \code + int main(int argc, char **argv) + { + QGuiApplication app(argc, argv); + JsonMimeConverter jsonConverter; + } + \endcode + + Destroying the instance will unregister the converter and remove support + for the conversion. It is also valid to heap-allocate the converter + instance; Qt takes ownership and will delete the converter object during + QGuiApplication shut-down. Qt has predefined support for the following UTIs: \list @@ -82,6 +96,7 @@ using namespace Qt::StringLiterals; */ /*! + \internal Constructs a new conversion object of type \a scope, adding it to the globally accessed list of available converters. */ @@ -94,9 +109,11 @@ QUtiMimeConverter::QUtiMimeConverter(HandlerScope scope) /*! Constructs a new conversion object and adds it to the globally accessed list of available converters. + + Call this constructor after QGuiApplication has been created. */ QUtiMimeConverter::QUtiMimeConverter() - : QUtiMimeConverter(HandlerScope::All) + : QUtiMimeConverter(HandlerScopeFlag::All) { } @@ -126,7 +143,7 @@ int QUtiMimeConverter::count(const QMimeData *mimeData) const */ /*! - \fn QString QUtiMimeConverter::mimeForUti(QString uti) + \fn QString QUtiMimeConverter::mimeForUti(const QString &uti) const Returns the MIME type used for Mac UTI \a uti, or an empty string if this converter does not support converting from \a uti. @@ -135,7 +152,7 @@ int QUtiMimeConverter::count(const QMimeData *mimeData) const */ /*! - \fn QString QUtiMimeConverter::utiForMime(const QString &mime) + \fn QString QUtiMimeConverter::utiForMime(const QString &mime) const Returns the Mac UTI used for MIME type \a mime, or an empty string if this converter does not support converting from \a mime. @@ -145,7 +162,7 @@ int QUtiMimeConverter::count(const QMimeData *mimeData) const /*! \fn QVariant QUtiMimeConverter::convertToMime(const QString &mime, - const QList<QByteArray> &data, const QString &uti) + const QList<QByteArray> &data, const QString &uti) const Returns \a data converted from Mac UTI \a uti to MIME type \a mime. @@ -157,7 +174,7 @@ int QUtiMimeConverter::count(const QMimeData *mimeData) const /*! \fn QList<QByteArray> QUtiMimeConverter::convertFromMime(const QString &mime, - const QVariant &data, const QString & uti) + const QVariant &data, const QString & uti) const Returns \a data converted from MIME type \a mime to Mac UTI \a uti. @@ -170,7 +187,7 @@ int QUtiMimeConverter::count(const QMimeData *mimeData) const class QMacMimeAny : public QUtiMimeConverter { public: - QMacMimeAny() : QUtiMimeConverter(HandlerScope::AllCompatible) {} + QMacMimeAny() : QUtiMimeConverter(HandlerScopeFlag::AllCompatible) {} QString utiForMime(const QString &mime) const override; QString mimeForUti(const QString &uti) const override; @@ -225,7 +242,7 @@ class QMacMimeTypeName : public QUtiMimeConverter { private: public: - QMacMimeTypeName(): QUtiMimeConverter(HandlerScope::AllCompatible) {} + QMacMimeTypeName(): QUtiMimeConverter(HandlerScopeFlag::AllCompatible) {} QString utiForMime(const QString &mime) const override; QString mimeForUti(const QString &uti) const override; @@ -763,7 +780,7 @@ QList<QByteArray> QMacMimeTiff::convertFromMime(const QString &mime, QCFType<CFMutableDataRef> data = CFDataCreateMutable(0, 0); QCFType<CGImageDestinationRef> imageDestination = CGImageDestinationCreateWithData(data, - kUTTypeTIFF, 1, 0); + (CFStringRef)UTTypeTIFF.identifier, 1, 0); if (!imageDestination) return QList<QByteArray>(); diff --git a/src/gui/platform/ios/PrivacyInfo.xcprivacy b/src/gui/platform/ios/PrivacyInfo.xcprivacy new file mode 100644 index 0000000000..bde2b167c7 --- /dev/null +++ b/src/gui/platform/ios/PrivacyInfo.xcprivacy @@ -0,0 +1,23 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> +<plist version="1.0"> +<dict> + <key>NSPrivacyTracking</key> + <false/> + <key>NSPrivacyCollectedDataTypes</key> + <array/> + <key>NSPrivacyTrackingDomains</key> + <array/> + <key>NSPrivacyAccessedAPITypes</key> + <array> + <dict> + <key>NSPrivacyAccessedAPIType</key> + <string>NSPrivacyAccessedAPICategorySystemBootTime</string> + <key>NSPrivacyAccessedAPITypeReasons</key> + <array> + <string>35F9.1</string> <!-- QUIView event handling --> + </array> + </dict> + </array> +</dict> +</plist> diff --git a/src/gui/platform/ios/qiosnativeinterface.cpp b/src/gui/platform/ios/qiosnativeinterface.cpp new file mode 100644 index 0000000000..c942709e33 --- /dev/null +++ b/src/gui/platform/ios/qiosnativeinterface.cpp @@ -0,0 +1,26 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include <QtGui/private/qguiapplication_p.h> + +QT_BEGIN_NAMESPACE + +using namespace QNativeInterface::Private; + +#if defined(Q_OS_VISIONOS) + +/*! + \class QNativeInterface::QVisionOSApplication + \since 6.8 + \internal + \preliminary + \brief Native interface to QGuiApplication, to be retrieved from QPlatformIntegration. + \inmodule QtGui + \ingroup native-interfaces +*/ + +QT_DEFINE_NATIVE_INTERFACE(QVisionOSApplication); + +#endif // Q_OS_VISIONOS + +QT_END_NAMESPACE diff --git a/src/gui/platform/macos/qcocoanativeinterface.mm b/src/gui/platform/macos/qcocoanativeinterface.mm index a41f9b16da..cb6acb4496 100644 --- a/src/gui/platform/macos/qcocoanativeinterface.mm +++ b/src/gui/platform/macos/qcocoanativeinterface.mm @@ -1,7 +1,11 @@ // Copyright (C) 2020 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only -#include <QtGui/private/qopenglcontext_p.h> +#include <QtGui/qtgui-config.h> +#ifndef QT_NO_OPENGL +# include <QtGui/private/qopenglcontext_p.h> +#endif + #include <QtGui/private/qguiapplication_p.h> #include <qpa/qplatformopenglcontext.h> #include <qpa/qplatformintegration.h> diff --git a/src/gui/platform/unix/dbusmenu/qdbusmenuconnection.cpp b/src/gui/platform/unix/dbusmenu/qdbusmenuconnection.cpp index 9391b77f6a..1023b16662 100644 --- a/src/gui/platform/unix/dbusmenu/qdbusmenuconnection.cpp +++ b/src/gui/platform/unix/dbusmenu/qdbusmenuconnection.cpp @@ -40,14 +40,14 @@ QDBusMenuConnection::QDBusMenuConnection(QObject *parent, const QString &service , m_connection(serviceName.isNull() ? QDBusConnection::sessionBus() : QDBusConnection::connectToBus(QDBusConnection::SessionBus, serviceName)) , m_dbusWatcher(new QDBusServiceWatcher(StatusNotifierWatcherService, m_connection, QDBusServiceWatcher::WatchForRegistration, this)) - , m_statusNotifierHostRegistered(false) + , m_watcherRegistered(false) { #ifndef QT_NO_SYSTEMTRAYICON - QDBusInterface systrayHost(StatusNotifierWatcherService, StatusNotifierWatcherPath, StatusNotifierWatcherService, m_connection); - if (systrayHost.isValid() && systrayHost.property("IsStatusNotifierHostRegistered").toBool()) - m_statusNotifierHostRegistered = true; + // Start monitoring if any known tray-related services are registered. + if (m_connection.interface()->isServiceRegistered(StatusNotifierWatcherService)) + m_watcherRegistered = true; else - qCDebug(qLcMenu) << "StatusNotifierHost is not registered"; + qCDebug(qLcMenu) << "failed to find service" << StatusNotifierWatcherService; #endif } diff --git a/src/gui/platform/unix/dbusmenu/qdbusmenuconnection_p.h b/src/gui/platform/unix/dbusmenu/qdbusmenuconnection_p.h index 69713b12bd..37033e2fa3 100644 --- a/src/gui/platform/unix/dbusmenu/qdbusmenuconnection_p.h +++ b/src/gui/platform/unix/dbusmenu/qdbusmenuconnection_p.h @@ -39,7 +39,7 @@ public: ~QDBusMenuConnection(); QDBusConnection connection() const { return m_connection; } QDBusServiceWatcher *dbusWatcher() const { return m_dbusWatcher; } - bool isStatusNotifierHostRegistered() const { return m_statusNotifierHostRegistered; } + bool isWatcherRegistered() const { return m_watcherRegistered; } #ifndef QT_NO_SYSTEMTRAYICON bool registerTrayIconMenu(QDBusTrayIcon *item); void unregisterTrayIconMenu(QDBusTrayIcon *item); @@ -60,7 +60,7 @@ private: QString m_serviceName; QDBusConnection m_connection; QDBusServiceWatcher *m_dbusWatcher; - bool m_statusNotifierHostRegistered; + bool m_watcherRegistered; }; QT_END_NAMESPACE diff --git a/src/gui/platform/unix/dbusmenu/qdbusmenutypes.cpp b/src/gui/platform/unix/dbusmenu/qdbusmenutypes.cpp index d2469a0d26..b7fd035883 100644 --- a/src/gui/platform/unix/dbusmenu/qdbusmenutypes.cpp +++ b/src/gui/platform/unix/dbusmenu/qdbusmenutypes.cpp @@ -217,19 +217,19 @@ QDBusMenuShortcut QDBusMenuItem::convertKeySequence(const QKeySequence &sequence QDBusMenuShortcut shortcut; for (int i = 0; i < sequence.count(); ++i) { QStringList tokens; - int key = sequence[i].toCombined(); - if (key & Qt::MetaModifier) + auto modifiers = sequence[i].keyboardModifiers(); + if (modifiers & Qt::MetaModifier) tokens << QStringLiteral("Super"); - if (key & Qt::ControlModifier) + if (modifiers & Qt::ControlModifier) tokens << QStringLiteral("Control"); - if (key & Qt::AltModifier) + if (modifiers & Qt::AltModifier) tokens << QStringLiteral("Alt"); - if (key & Qt::ShiftModifier) + if (modifiers & Qt::ShiftModifier) tokens << QStringLiteral("Shift"); - if (key & Qt::KeypadModifier) + if (modifiers & Qt::KeypadModifier) tokens << QStringLiteral("Num"); - QString keyName = QKeySequencePrivate::keyName(key, QKeySequence::PortableText); + QString keyName = QKeySequencePrivate::keyName(sequence[i].key(), QKeySequence::PortableText); if (keyName == "+"_L1) tokens << QStringLiteral("plus"); else if (keyName == "-"_L1) diff --git a/src/gui/platform/unix/dbustray/qdbustrayicon.cpp b/src/gui/platform/unix/dbustray/qdbustrayicon.cpp index 18334e5715..0dff9b598e 100644 --- a/src/gui/platform/unix/dbustray/qdbustrayicon.cpp +++ b/src/gui/platform/unix/dbustray/qdbustrayicon.cpp @@ -198,7 +198,10 @@ QTemporaryFile *QDBusTrayIcon::tempIcon(const QIcon &icon) if (!necessary) return nullptr; QTemporaryFile *ret = new QTemporaryFile(tempFileTemplate(), this); - ret->open(); + if (!ret->open()) { + delete ret; + return nullptr; + } icon.pixmap(QSize(22, 22)).save(ret); ret->close(); return ret; @@ -331,8 +334,11 @@ void QDBusTrayIcon::notificationClosed(uint id, uint reason) bool QDBusTrayIcon::isSystemTrayAvailable() const { QDBusMenuConnection * conn = const_cast<QDBusTrayIcon *>(this)->dBusConnection(); - qCDebug(qLcTray) << conn->isStatusNotifierHostRegistered(); - return conn->isStatusNotifierHostRegistered(); + + // If the KDE watcher service is registered, we must be on a desktop + // where a StatusNotifier-conforming system tray exists. + qCDebug(qLcTray) << conn->isWatcherRegistered(); + return conn->isWatcherRegistered(); } QT_END_NAMESPACE diff --git a/src/gui/platform/unix/qgenericunixservices.cpp b/src/gui/platform/unix/qgenericunixservices.cpp index 892854fae3..bfd2556b1e 100644 --- a/src/gui/platform/unix/qgenericunixservices.cpp +++ b/src/gui/platform/unix/qgenericunixservices.cpp @@ -5,6 +5,9 @@ #include <QtGui/private/qtguiglobal_p.h> #include "qguiapplication.h" #include "qwindow.h" +#include <QtGui/qpa/qplatformwindow_p.h> +#include <QtGui/qpa/qplatformwindow.h> +#include <QtGui/qpa/qplatformnativeinterface.h> #include <QtCore/QDebug> #include <QtCore/QFile> @@ -128,8 +131,13 @@ static inline bool detectWebBrowser(const QByteArray &desktop, return false; } -static inline bool launch(const QString &launcher, const QUrl &url) +static inline bool launch(const QString &launcher, const QUrl &url, + const QString &xdgActivationToken) { + if (!xdgActivationToken.isEmpty()) { + qputenv("XDG_ACTIVATION_TOKEN", xdgActivationToken.toUtf8()); + } + const QString command = launcher + u' ' + QLatin1StringView(url.toEncoded()); if (debug) qDebug("Launching %s", qPrintable(command)); @@ -145,16 +153,20 @@ static inline bool launch(const QString &launcher, const QUrl &url) #endif if (!ok) qWarning("Launch failed (%s)", qPrintable(command)); + + qunsetenv("XDG_ACTIVATION_TOKEN"); + return ok; } #if QT_CONFIG(dbus) static inline bool checkNeedPortalSupport() { - return !QStandardPaths::locate(QStandardPaths::RuntimeLocation, "flatpak-info"_L1).isEmpty() || qEnvironmentVariableIsSet("SNAP"); + return QFileInfo::exists("/.flatpak-info"_L1) || qEnvironmentVariableIsSet("SNAP"); } -static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url, const QString &parentWindow) +static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url, const QString &parentWindow, + const QString &xdgActivationToken) { // DBus signature: // OpenFile (IN s parent_window, @@ -165,8 +177,7 @@ static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url, const QStri // handle_token (s) - A string that will be used as the last element of the @handle. // writable (b) - Whether to allow the chosen application to write to the file. -#ifdef O_PATH - const int fd = qt_safe_open(QFile::encodeName(url.toLocalFile()), O_PATH); + const int fd = qt_safe_open(QFile::encodeName(url.toLocalFile()), O_RDONLY); if (fd != -1) { QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop"_L1, "/org/freedesktop/portal/desktop"_L1, @@ -176,21 +187,22 @@ static inline QDBusMessage xdgDesktopPortalOpenFile(const QUrl &url, const QStri QDBusUnixFileDescriptor descriptor; descriptor.giveFileDescriptor(fd); - const QVariantMap options = {{"writable"_L1, true}}; + QVariantMap options = {}; + + if (!xdgActivationToken.isEmpty()) { + options.insert("activation_token"_L1, xdgActivationToken); + } message << parentWindow << QVariant::fromValue(descriptor) << options; return QDBusConnection::sessionBus().call(message); } -#else - Q_UNUSED(url); - Q_UNUSED(parentWindow) -#endif return QDBusMessage::createError(QDBusError::InternalError, qt_error_string()); } -static inline QDBusMessage xdgDesktopPortalOpenUrl(const QUrl &url, const QString &parentWindow) +static inline QDBusMessage xdgDesktopPortalOpenUrl(const QUrl &url, const QString &parentWindow, + const QString &xdgActivationToken) { // DBus signature: // OpenURI (IN s parent_window, @@ -208,12 +220,19 @@ static inline QDBusMessage xdgDesktopPortalOpenUrl(const QUrl &url, const QStrin "org.freedesktop.portal.OpenURI"_L1, "OpenURI"_L1); // FIXME parent_window_id and handle writable option - message << parentWindow << url.toString() << QVariantMap(); + QVariantMap options; + + if (!xdgActivationToken.isEmpty()) { + options.insert("activation_token"_L1, xdgActivationToken); + } + + message << parentWindow << url.toString() << options; return QDBusConnection::sessionBus().call(message); } -static inline QDBusMessage xdgDesktopPortalSendEmail(const QUrl &url, const QString &parentWindow) +static inline QDBusMessage xdgDesktopPortalSendEmail(const QUrl &url, const QString &parentWindow, + const QString &xdgActivationToken) { // DBus signature: // ComposeEmail (IN s parent_window, @@ -248,6 +267,10 @@ static inline QDBusMessage xdgDesktopPortalSendEmail(const QUrl &url, const QStr options.insert("attachment_fds"_L1, QVariant::fromValue(attachments)); #endif + if (!xdgActivationToken.isEmpty()) { + options.insert("activation_token"_L1, xdgActivationToken); + } + QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.portal.Desktop"_L1, "/org/freedesktop/portal/desktop"_L1, "org.freedesktop.portal.Email"_L1, @@ -331,9 +354,13 @@ private Q_SLOTS: { if (result != 0) return; - XDGDesktopColor color{}; - map.value(u"color"_s).value<QDBusArgument>() >> color; - Q_EMIT colorPicked(color.toQColor()); + if (map.contains(u"color"_s)) { + XDGDesktopColor color{}; + map.value(u"color"_s).value<QDBusArgument>() >> color; + Q_EMIT colorPicked(color.toQColor()); + } else { + Q_EMIT colorPicked({}); + } deleteLater(); } @@ -392,60 +419,117 @@ QByteArray QGenericUnixServices::desktopEnvironment() const return result; } +template<typename F> +void runWithXdgActivationToken(F &&functionToCall) +{ +#if QT_CONFIG(wayland) + QWindow *window = qGuiApp->focusWindow(); + + if (!window) { + functionToCall({}); + return; + } + + auto waylandApp = dynamic_cast<QNativeInterface::QWaylandApplication *>( + qGuiApp->platformNativeInterface()); + auto waylandWindow = + dynamic_cast<QNativeInterface::Private::QWaylandWindow *>(window->handle()); + + if (!waylandWindow || !waylandApp) { + functionToCall({}); + return; + } + + QObject::connect(waylandWindow, + &QNativeInterface::Private::QWaylandWindow::xdgActivationTokenCreated, + waylandWindow, functionToCall, Qt::SingleShotConnection); + waylandWindow->requestXdgActivationToken(waylandApp->lastInputSerial()); +#else + functionToCall({}); +#endif +} + bool QGenericUnixServices::openUrl(const QUrl &url) { - if (url.scheme() == "mailto"_L1) { -#if QT_CONFIG(dbus) + auto openUrlInternal = [this](const QUrl &url, const QString &xdgActivationToken) { + if (url.scheme() == "mailto"_L1) { +# if QT_CONFIG(dbus) + if (checkNeedPortalSupport()) { + const QString parentWindow = QGuiApplication::focusWindow() + ? portalWindowIdentifier(QGuiApplication::focusWindow()) + : QString(); + QDBusError error = xdgDesktopPortalSendEmail(url, parentWindow, xdgActivationToken); + if (!error.isValid()) + return true; + + // service not running, fall back + } +# endif + return openDocument(url); + } + +# if QT_CONFIG(dbus) if (checkNeedPortalSupport()) { const QString parentWindow = QGuiApplication::focusWindow() ? portalWindowIdentifier(QGuiApplication::focusWindow()) : QString(); - QDBusError error = xdgDesktopPortalSendEmail(url, parentWindow); + QDBusError error = xdgDesktopPortalOpenUrl(url, parentWindow, xdgActivationToken); if (!error.isValid()) return true; + } +# endif - // service not running, fall back + if (m_webBrowser.isEmpty() + && !detectWebBrowser(desktopEnvironment(), true, &m_webBrowser)) { + qWarning("Unable to detect a web browser to launch '%s'", qPrintable(url.toString())); + return false; } -#endif - return openDocument(url); - } + return launch(m_webBrowser, url, xdgActivationToken); + }; -#if QT_CONFIG(dbus) - if (checkNeedPortalSupport()) { - const QString parentWindow = QGuiApplication::focusWindow() - ? portalWindowIdentifier(QGuiApplication::focusWindow()) - : QString(); - QDBusError error = xdgDesktopPortalOpenUrl(url, parentWindow); - if (!error.isValid()) - return true; - } -#endif + if (QGuiApplication::platformName().startsWith("wayland"_L1)) { + runWithXdgActivationToken( + [openUrlInternal, url](const QString &token) { openUrlInternal(url, token); }); + + return true; - if (m_webBrowser.isEmpty() && !detectWebBrowser(desktopEnvironment(), true, &m_webBrowser)) { - qWarning("Unable to detect a web browser to launch '%s'", qPrintable(url.toString())); - return false; + } else { + return openUrlInternal(url, QString()); } - return launch(m_webBrowser, url); } bool QGenericUnixServices::openDocument(const QUrl &url) { -#if QT_CONFIG(dbus) - if (checkNeedPortalSupport()) { - const QString parentWindow = QGuiApplication::focusWindow() - ? portalWindowIdentifier(QGuiApplication::focusWindow()) - : QString(); - QDBusError error = xdgDesktopPortalOpenFile(url, parentWindow); - if (!error.isValid()) - return true; - } -#endif + auto openDocumentInternal = [this](const QUrl &url, const QString &xdgActivationToken) { + +# if QT_CONFIG(dbus) + if (checkNeedPortalSupport()) { + const QString parentWindow = QGuiApplication::focusWindow() + ? portalWindowIdentifier(QGuiApplication::focusWindow()) + : QString(); + QDBusError error = xdgDesktopPortalOpenFile(url, parentWindow, xdgActivationToken); + if (!error.isValid()) + return true; + } +# endif + + if (m_documentLauncher.isEmpty() + && !detectWebBrowser(desktopEnvironment(), false, &m_documentLauncher)) { + qWarning("Unable to detect a launcher for '%s'", qPrintable(url.toString())); + return false; + } + return launch(m_documentLauncher, url, xdgActivationToken); + }; - if (m_documentLauncher.isEmpty() && !detectWebBrowser(desktopEnvironment(), false, &m_documentLauncher)) { - qWarning("Unable to detect a launcher for '%s'", qPrintable(url.toString())); - return false; + if (QGuiApplication::platformName().startsWith("wayland"_L1)) { + runWithXdgActivationToken([openDocumentInternal, url](const QString &token) { + openDocumentInternal(url, token); + }); + + return true; + } else { + return openDocumentInternal(url, QString()); } - return launch(m_documentLauncher, url); } #else @@ -480,9 +564,7 @@ QPlatformServiceColorPicker *QGenericUnixServices::colorPicker(QWindow *parent) QString QGenericUnixServices::portalWindowIdentifier(QWindow *window) { - if (QGuiApplication::platformName() == QLatin1String("xcb")) - return "x11:"_L1 + QString::number(window->winId(), 16); - + Q_UNUSED(window); return QString(); } @@ -495,6 +577,37 @@ bool QGenericUnixServices::hasCapability(Capability capability) const return false; } +void QGenericUnixServices::setApplicationBadge(qint64 number) +{ +#if QT_CONFIG(dbus) + if (qGuiApp->desktopFileName().isEmpty()) { + qWarning("QGuiApplication::desktopFileName() is empty"); + return; + } + + + const QString launcherUrl = QStringLiteral("application://") + qGuiApp->desktopFileName() + QStringLiteral(".desktop"); + const qint64 count = qBound(0, number, 9999); + QVariantMap dbusUnityProperties; + + if (count > 0) { + dbusUnityProperties[QStringLiteral("count")] = count; + dbusUnityProperties[QStringLiteral("count-visible")] = true; + } else { + dbusUnityProperties[QStringLiteral("count-visible")] = false; + } + + auto signal = QDBusMessage::createSignal(QStringLiteral("/com/canonical/unity/launcherentry/") + + qGuiApp->applicationName(), QStringLiteral("com.canonical.Unity.LauncherEntry"), QStringLiteral("Update")); + + signal.setArguments({launcherUrl, dbusUnityProperties}); + + QDBusConnection::sessionBus().send(signal); +#else + Q_UNUSED(number) +#endif +} + QT_END_NAMESPACE #include "qgenericunixservices.moc" diff --git a/src/gui/platform/unix/qgenericunixservices_p.h b/src/gui/platform/unix/qgenericunixservices_p.h index 701bcfc78f..56e15103f7 100644 --- a/src/gui/platform/unix/qgenericunixservices_p.h +++ b/src/gui/platform/unix/qgenericunixservices_p.h @@ -35,6 +35,7 @@ public: bool openDocument(const QUrl &url) override; QPlatformServiceColorPicker *colorPicker(QWindow *parent = nullptr) override; + void setApplicationBadge(qint64 number); virtual QString portalWindowIdentifier(QWindow *window); private: diff --git a/src/gui/platform/unix/qgenericunixthemes.cpp b/src/gui/platform/unix/qgenericunixthemes.cpp index ad229af624..8a7f7cd6f7 100644 --- a/src/gui/platform/unix/qgenericunixthemes.cpp +++ b/src/gui/platform/unix/qgenericunixthemes.cpp @@ -33,6 +33,12 @@ #include <QDBusConnectionInterface> #include <private/qdbusplatformmenu_p.h> #include <private/qdbusmenubar_p.h> +#include <private/qflatmap_p.h> +#include <QJsonDocument> +#include <QJsonArray> +#include <QJsonObject> +#include <QJsonValue> +#include <QJsonParseError> #endif #if !defined(QT_NO_DBUS) && !defined(QT_NO_SYSTEMTRAYICON) #include <private/qdbustrayicon_p.h> @@ -72,17 +78,14 @@ static const char defaultFixedFontNameC[] = "monospace"; enum { defaultSystemFontSize = 9 }; #if !defined(QT_NO_DBUS) && !defined(QT_NO_SYSTEMTRAYICON) -static bool isDBusTrayAvailable() { - static bool dbusTrayAvailable = false; - static bool dbusTrayAvailableKnown = false; - if (!dbusTrayAvailableKnown) { - QDBusMenuConnection conn; - if (conn.isStatusNotifierHostRegistered()) - dbusTrayAvailable = true; - dbusTrayAvailableKnown = true; - qCDebug(qLcTray) << "D-Bus tray available:" << dbusTrayAvailable; - } - return dbusTrayAvailable; +static bool shouldUseDBusTray() { + // There's no other tray implementation to fallback to on non-X11 + // and QDBusTrayIcon can register the icon on the fly after creation + if (QGuiApplication::platformName() != "xcb"_L1) + return true; + const bool result = QDBusMenuConnection().isWatcherRegistered(); + qCDebug(qLcTray) << "D-Bus tray available:" << result; + return result; } #endif @@ -119,7 +122,7 @@ static bool isDBusGlobalMenuAvailable() /*! * \internal * The QGenericUnixThemeDBusListener class listens to the SettingChanged DBus signal - * and translates it into the QDbusSettingType enum. + * and translates it into combinations of the enums \c Provider and \c Setting. * Upon construction, it logs success/failure of the DBus connection. * * The signal settingChanged delivers the normalized setting type and the new value as a string. @@ -131,35 +134,98 @@ class QGenericUnixThemeDBusListener : public QObject Q_OBJECT public: - QGenericUnixThemeDBusListener(const QString &service, const QString &path, const QString &interface, const QString &signal); - enum class SettingType { - KdeGlobalTheme, - KdeApplicationStyle, - GtkTheme, - Unknown + enum class Provider { + Kde, + Gtk, + Gnome, }; - Q_ENUM(SettingType) + Q_ENUM(Provider) - static SettingType toSettingType(const QString &location, const QString &key); + enum class Setting { + Theme, + ApplicationStyle, + ColorScheme, + }; + Q_ENUM(Setting) + + QGenericUnixThemeDBusListener(); + QGenericUnixThemeDBusListener(const QString &service, const QString &path, + const QString &interface, const QString &signal); private Q_SLOTS: void onSettingChanged(const QString &location, const QString &key, const QDBusVariant &value); Q_SIGNALS: - void settingChanged(QGenericUnixThemeDBusListener::SettingType type, const QString &value); + void settingChanged(QGenericUnixThemeDBusListener::Provider provider, + QGenericUnixThemeDBusListener::Setting setting, + const QString &value); + +private: + struct DBusKey + { + QString location; + QString key; + DBusKey(const QString &loc, const QString &k) : location(loc), key(k) {}; + bool operator<(const DBusKey &other) const + { + return location + key < other.location + other.key; + } + }; + + struct ChangeSignal + { + Provider provider; + Setting setting; + ChangeSignal(Provider p, Setting s) : provider(p), setting(s) {} + ChangeSignal() {} + }; + + // Json keys + static constexpr QLatin1StringView s_dbusLocation = QLatin1StringView("DBusLocation"); + static constexpr QLatin1StringView s_dbusKey = QLatin1StringView("DBusKey"); + static constexpr QLatin1StringView s_provider = QLatin1StringView("Provider"); + static constexpr QLatin1StringView s_setting = QLatin1StringView("Setting"); + static constexpr QLatin1StringView s_signals = QLatin1StringView("DbusSignals"); + static constexpr QLatin1StringView s_root = QLatin1StringView("Qt.qpa.DBusSignals"); + QFlatMap <DBusKey, ChangeSignal> m_signalMap; + + void init(const QString &service, const QString &path, + const QString &interface, const QString &signal); + + std::optional<ChangeSignal> findSignal(const QString &location, const QString &key) const; + void populateSignalMap(); + void loadJson(const QString &fileName); + void saveJson(const QString &fileName) const; }; QGenericUnixThemeDBusListener::QGenericUnixThemeDBusListener(const QString &service, const QString &path, const QString &interface, const QString &signal) { + init (service, path, interface, signal); +} + +QGenericUnixThemeDBusListener::QGenericUnixThemeDBusListener() +{ + static constexpr QLatin1StringView service(""); + static constexpr QLatin1StringView path("/org/freedesktop/portal/desktop"); + static constexpr QLatin1StringView interface("org.freedesktop.portal.Settings"); + static constexpr QLatin1StringView signal("SettingChanged"); + + init (service, path, interface, signal); +} + +void QGenericUnixThemeDBusListener::init(const QString &service, const QString &path, + const QString &interface, const QString &signal) +{ QDBusConnection dbus = QDBusConnection::sessionBus(); const bool dBusRunning = dbus.isConnected(); bool dBusSignalConnected = false; #define LOG service << path << interface << signal; if (dBusRunning) { + populateSignalMap(); qRegisterMetaType<QDBusVariant>(); dBusSignalConnected = dbus.connect(service, path, interface, signal, this, SLOT(onSettingChanged(QString,QString,QDBusVariant))); @@ -182,26 +248,155 @@ QGenericUnixThemeDBusListener::QGenericUnixThemeDBusListener(const QString &serv #undef LOG } -QGenericUnixThemeDBusListener::SettingType QGenericUnixThemeDBusListener::toSettingType( - const QString &location, const QString &key) -{ - if (location == QLatin1StringView("org.kde.kdeglobals.KDE") - && key == QLatin1StringView("widgetStyle")) - return SettingType::KdeApplicationStyle; - if (location == QLatin1StringView("org.kde.kdeglobals.General") - && key == QLatin1StringView("ColorScheme")) - return SettingType::KdeGlobalTheme; - if (location == QLatin1StringView("org.gnome.desktop.interface") - && key == QLatin1StringView("gtk-theme")) - return SettingType::GtkTheme; - return SettingType::Unknown; +void QGenericUnixThemeDBusListener::loadJson(const QString &fileName) +{ + Q_ASSERT(!fileName.isEmpty()); +#define CHECK(cond, warning)\ + if (!cond) {\ + qCWarning(lcQpaThemeDBus) << fileName << warning << "Falling back to default.";\ + return;\ + } + +#define PARSE(var, enumeration, string)\ + enumeration var;\ + {\ + bool success;\ + const int val = QMetaEnum::fromType<enumeration>().keyToValue(string.toLatin1(), &success);\ + CHECK(success, "Parse Error: Invalid value" << string << "for" << #var);\ + var = static_cast<enumeration>(val);\ + } + + QFile file(fileName); + CHECK(file.exists(), fileName << "doesn't exist."); + CHECK(file.open(QIODevice::ReadOnly), "could not be opened for reading."); + + QJsonParseError error; + QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &error); + CHECK((error.error == QJsonParseError::NoError), error.errorString()); + CHECK(doc.isObject(), "Parse Error: Expected root object" << s_root); + + const QJsonObject &root = doc.object(); + CHECK(root.contains(s_root), "Parse Error: Expected root object" << s_root); + CHECK(root[s_root][s_signals].isArray(), "Parse Error: Expected array" << s_signals); + + const QJsonArray &sigs = root[s_root][s_signals].toArray(); + CHECK((sigs.count() > 0), "Parse Error: Found empty array" << s_signals); + + for (auto sig = sigs.constBegin(); sig != sigs.constEnd(); ++sig) { + CHECK(sig->isObject(), "Parse Error: Expected object array" << s_signals); + const QJsonObject &obj = sig->toObject(); + CHECK(obj.contains(s_dbusLocation), "Parse Error: Expected key" << s_dbusLocation); + CHECK(obj.contains(s_dbusKey), "Parse Error: Expected key" << s_dbusKey); + CHECK(obj.contains(s_provider), "Parse Error: Expected key" << s_provider); + CHECK(obj.contains(s_setting), "Parse Error: Expected key" << s_setting); + const QString &location = obj[s_dbusLocation].toString(); + const QString &key = obj[s_dbusKey].toString(); + const QString &providerString = obj[s_provider].toString(); + const QString &settingString = obj[s_setting].toString(); + PARSE(provider, Provider, providerString); + PARSE(setting, Setting, settingString); + const DBusKey dkey(location, key); + CHECK (!m_signalMap.contains(dkey), "Duplicate key" << location << key); + m_signalMap.insert(dkey, ChangeSignal(provider, setting)); + } +#undef PARSE +#undef CHECK + + if (m_signalMap.count() > 0) + qCInfo(lcQpaThemeDBus) << "Successfully imported" << fileName; + else + qCWarning(lcQpaThemeDBus) << "No data imported from" << fileName << "falling back to default."; + +#ifdef QT_DEBUG + const int count = m_signalMap.count(); + if (count == 0) + return; + + qCDebug(lcQpaThemeDBus) << "Listening to" << count << "signals:"; + for (auto it = m_signalMap.constBegin(); it != m_signalMap.constEnd(); ++it) { + qDebug() << it.key().key << it.key().location << "mapped to" + << it.value().provider << it.value().setting; + } + +#endif +} + +void QGenericUnixThemeDBusListener::saveJson(const QString &fileName) const +{ + Q_ASSERT(!m_signalMap.isEmpty()); + Q_ASSERT(!fileName.isEmpty()); + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly)) { + qCWarning(lcQpaThemeDBus) << fileName << "could not be opened for writing."; + return; + } + + QJsonArray sigs; + for (auto sig = m_signalMap.constBegin(); sig != m_signalMap.constEnd(); ++sig) { + const DBusKey &dkey = sig.key(); + const ChangeSignal &csig = sig.value(); + QJsonObject obj; + obj[s_dbusLocation] = dkey.location; + obj[s_dbusKey] = dkey.key; + obj[s_provider] = QLatin1StringView(QMetaEnum::fromType<Provider>() + .valueToKey(static_cast<int>(csig.provider))); + obj[s_setting] = QLatin1StringView(QMetaEnum::fromType<Setting>() + .valueToKey(static_cast<int>(csig.setting))); + sigs.append(obj); + } + QJsonObject obj; + obj[s_signals] = sigs; + QJsonObject root; + root[s_root] = obj; + QJsonDocument doc(root); + file.write(doc.toJson()); + file.close(); +} + +void QGenericUnixThemeDBusListener::populateSignalMap() +{ + m_signalMap.clear(); + const QString &loadJsonFile = qEnvironmentVariable("QT_QPA_DBUS_SIGNALS"); + if (!loadJsonFile.isEmpty()) + loadJson(loadJsonFile); + if (!m_signalMap.isEmpty()) + return; + + m_signalMap.insert(DBusKey("org.kde.kdeglobals.KDE"_L1, "widgetStyle"_L1), + ChangeSignal(Provider::Kde, Setting::ApplicationStyle)); + + m_signalMap.insert(DBusKey("org.kde.kdeglobals.General"_L1, "ColorScheme"_L1), + ChangeSignal(Provider::Kde, Setting::Theme)); + + m_signalMap.insert(DBusKey("org.gnome.desktop.interface"_L1, "gtk-theme"_L1), + ChangeSignal(Provider::Gtk, Setting::Theme)); + + m_signalMap.insert(DBusKey("org.freedesktop.appearance"_L1, "color-scheme"_L1), + ChangeSignal(Provider::Gnome, Setting::ColorScheme)); + + const QString &saveJsonFile = qEnvironmentVariable("QT_QPA_DBUS_SIGNALS_SAVE"); + if (!saveJsonFile.isEmpty()) + saveJson(saveJsonFile); +} + +std::optional<QGenericUnixThemeDBusListener::ChangeSignal> + QGenericUnixThemeDBusListener::findSignal(const QString &location, const QString &key) const +{ + const DBusKey dkey(location, key); + std::optional<QGenericUnixThemeDBusListener::ChangeSignal> ret; + if (m_signalMap.contains(dkey)) + ret.emplace(m_signalMap.value(dkey)); + + return ret; } void QGenericUnixThemeDBusListener::onSettingChanged(const QString &location, const QString &key, const QDBusVariant &value) { - const SettingType type = toSettingType(location, key); - if (type != SettingType::Unknown) - emit settingChanged(type, value.variant().toString()); + auto sig = findSignal(location, key); + if (!sig.has_value()) + return; + + emit settingChanged(sig.value().provider, sig.value().setting, value.variant().toString()); } #endif //QT_NO_DBUS @@ -278,7 +473,7 @@ QPlatformMenuBar *QGenericUnixTheme::createPlatformMenuBar() const #if !defined(QT_NO_DBUS) && !defined(QT_NO_SYSTEMTRAYICON) QPlatformSystemTrayIcon *QGenericUnixTheme::createPlatformSystemTrayIcon() const { - if (isDBusTrayAvailable()) + if (shouldUseDBusTray()) return new QDBusTrayIcon(); return nullptr; } @@ -343,6 +538,48 @@ class QKdeThemePrivate : public QPlatformThemePrivate { public: + enum class KdeSettingType { + Root, + KDE, + Icons, + ToolBarIcons, + ToolBarStyle, + Fonts, + Colors, + }; + + enum class KdeSetting { + WidgetStyle, + ColorScheme, + SingleClick, + ShowIconsOnPushButtons, + IconTheme, + ToolBarIconSize, + ToolButtonStyle, + WheelScrollLines, + DoubleClickInterval, + StartDragDistance, + StartDragTime, + CursorBlinkRate, + Font, + Fixed, + MenuFont, + ToolBarFont, + ButtonBackground, + WindowBackground, + ViewForeground, + WindowForeground, + ViewBackground, + SelectionBackground, + SelectionForeground, + ViewBackgroundAlternate, + ButtonForeground, + ViewForegroundLink, + ViewForegroundVisited, + TooltipBackground, + TooltipForeground, + }; + QKdeThemePrivate(const QStringList &kdeDirs, int kdeVersion); static QString kdeGlobals(const QString &kdeDir, int kdeVersion) @@ -353,7 +590,9 @@ public: } void refresh(); - static QVariant readKdeSetting(const QString &key, const QStringList &kdeDirs, int kdeVersion, QHash<QString, QSettings*> &kdeSettings); + static QVariant readKdeSetting(KdeSetting s, const QStringList &kdeDirs, int kdeVersion, QHash<QString, QSettings*> &settings); + QVariant readKdeSetting(KdeSetting s) const; + void clearKdeSettings() const; static void readKdeSystemPalette(const QStringList &kdeDirs, int kdeVersion, QHash<QString, QSettings*> &kdeSettings, QPalette *pal); static QFont *kdeFont(const QVariant &fontValue); static QStringList kdeIconThemeSearchPaths(const QStringList &kdeDirs); @@ -374,31 +613,38 @@ public: int startDragDist = 10; int startDragTime = 500; int cursorBlinkRate = 1000; - Qt::Appearance m_appearance = Qt::Appearance::Unknown; - void updateAppearance(const QString &themeName); + Qt::ColorScheme m_colorScheme = Qt::ColorScheme::Unknown; + void updateColorScheme(const QString &themeName); -#ifndef QT_NO_DBUS private: + mutable QHash<QString, QSettings *> kdeSettings; +#ifndef QT_NO_DBUS std::unique_ptr<QGenericUnixThemeDBusListener> dbus; bool initDbus(); - void settingChangedHandler(QGenericUnixThemeDBusListener::SettingType type, const QString &value); + void settingChangedHandler(QGenericUnixThemeDBusListener::Provider provider, + QGenericUnixThemeDBusListener::Setting setting, + const QString &value); #endif // QT_NO_DBUS }; #ifndef QT_NO_DBUS -void QKdeThemePrivate::settingChangedHandler(QGenericUnixThemeDBusListener::SettingType type, const QString &value) +void QKdeThemePrivate::settingChangedHandler(QGenericUnixThemeDBusListener::Provider provider, + QGenericUnixThemeDBusListener::Setting setting, + const QString &value) { - switch (type) { - case QGenericUnixThemeDBusListener::SettingType::KdeGlobalTheme: + if (provider != QGenericUnixThemeDBusListener::Provider::Kde) + return; + + switch (setting) { + case QGenericUnixThemeDBusListener::Setting::ColorScheme: + qCDebug(lcQpaThemeDBus) << "KDE color theme changed to:" << value; + break; + case QGenericUnixThemeDBusListener::Setting::Theme: qCDebug(lcQpaThemeDBus) << "KDE global theme changed to:" << value; break; - case QGenericUnixThemeDBusListener::SettingType::KdeApplicationStyle: + case QGenericUnixThemeDBusListener::Setting::ApplicationStyle: qCDebug(lcQpaThemeDBus) << "KDE application style changed to:" << value; break; - case QGenericUnixThemeDBusListener::SettingType::GtkTheme: - return; // KDE can change GTK2 / GTK3 themes. Ignored here, handled in GnomeTheme - case QGenericUnixThemeDBusListener::SettingType::Unknown: - Q_UNREACHABLE(); } refresh(); @@ -406,20 +652,17 @@ void QKdeThemePrivate::settingChangedHandler(QGenericUnixThemeDBusListener::Sett bool QKdeThemePrivate::initDbus() { - static constexpr QLatin1StringView service(""); - static constexpr QLatin1StringView path("/org/freedesktop/portal/desktop"); - static constexpr QLatin1StringView interface("org.freedesktop.portal.Settings"); - static constexpr QLatin1StringView signal("SettingChanged"); - - dbus.reset(new QGenericUnixThemeDBusListener(service, path, interface, signal)); + dbus.reset(new QGenericUnixThemeDBusListener()); Q_ASSERT(dbus); // Wrap slot in a lambda to avoid inheriting QKdeThemePrivate from QObject - auto wrapper = [this](QGenericUnixThemeDBusListener::SettingType type, const QString &value) { - settingChangedHandler(type, value); + auto wrapper = [this](QGenericUnixThemeDBusListener::Provider provider, + QGenericUnixThemeDBusListener::Setting setting, + const QString &value) { + settingChangedHandler(provider, setting, value); }; - return QObject::connect(dbus.get(), &QGenericUnixThemeDBusListener::settingChanged, wrapper); + return QObject::connect(dbus.get(), &QGenericUnixThemeDBusListener::settingChanged, dbus.get(), wrapper); } #endif // QT_NO_DBUS @@ -431,9 +674,136 @@ QKdeThemePrivate::QKdeThemePrivate(const QStringList &kdeDirs, int kdeVersion) #endif // QT_NO_DBUS } +static constexpr QLatin1StringView settingsPrefix(QKdeThemePrivate::KdeSettingType type) +{ + switch (type) { + case QKdeThemePrivate::KdeSettingType::Root: + return QLatin1StringView(); + case QKdeThemePrivate::KdeSettingType::KDE: + return QLatin1StringView("KDE/"); + case QKdeThemePrivate::KdeSettingType::Fonts: + return QLatin1StringView(); + case QKdeThemePrivate::KdeSettingType::Colors: + return QLatin1StringView("Colors:"); + case QKdeThemePrivate::KdeSettingType::Icons: + return QLatin1StringView("Icons/"); + case QKdeThemePrivate::KdeSettingType::ToolBarIcons: + return QLatin1StringView("ToolbarIcons/"); + case QKdeThemePrivate::KdeSettingType::ToolBarStyle: + return QLatin1StringView("Toolbar style/"); + } + Q_UNREACHABLE_RETURN(QLatin1StringView()); +} + +static constexpr QKdeThemePrivate::KdeSettingType settingsType(QKdeThemePrivate::KdeSetting setting) +{ +#define CASE(s, type) case QKdeThemePrivate::KdeSetting::s:\ + return QKdeThemePrivate::KdeSettingType::type + + switch (setting) { + CASE(WidgetStyle, Root); + CASE(ColorScheme, Root); + CASE(SingleClick, KDE); + CASE(ShowIconsOnPushButtons, KDE); + CASE(IconTheme, Icons); + CASE(ToolBarIconSize, ToolBarIcons); + CASE(ToolButtonStyle, ToolBarStyle); + CASE(WheelScrollLines, KDE); + CASE(DoubleClickInterval, KDE); + CASE(StartDragDistance, KDE); + CASE(StartDragTime, KDE); + CASE(CursorBlinkRate, KDE); + CASE(Font, Root); + CASE(Fixed, Root); + CASE(MenuFont, Root); + CASE(ToolBarFont, Root); + CASE(ButtonBackground, Colors); + CASE(WindowBackground, Colors); + CASE(ViewForeground, Colors); + CASE(WindowForeground, Colors); + CASE(ViewBackground, Colors); + CASE(SelectionBackground, Colors); + CASE(SelectionForeground, Colors); + CASE(ViewBackgroundAlternate, Colors); + CASE(ButtonForeground, Colors); + CASE(ViewForegroundLink, Colors); + CASE(ViewForegroundVisited, Colors); + CASE(TooltipBackground, Colors); + CASE(TooltipForeground, Colors); + }; + Q_UNREACHABLE_RETURN(QKdeThemePrivate::KdeSettingType::Root); +} +#undef CASE + +static constexpr QLatin1StringView settingsKey(QKdeThemePrivate::KdeSetting setting) +{ + switch (setting) { + case QKdeThemePrivate::KdeSetting::WidgetStyle: + return QLatin1StringView("widgetStyle"); + case QKdeThemePrivate::KdeSetting::ColorScheme: + return QLatin1StringView("ColorScheme"); + case QKdeThemePrivate::KdeSetting::SingleClick: + return QLatin1StringView("SingleClick"); + case QKdeThemePrivate::KdeSetting::ShowIconsOnPushButtons: + return QLatin1StringView("ShowIconsOnPushButtons"); + case QKdeThemePrivate::KdeSetting::IconTheme: + return QLatin1StringView("Theme"); + case QKdeThemePrivate::KdeSetting::ToolBarIconSize: + return QLatin1StringView("Size"); + case QKdeThemePrivate::KdeSetting::ToolButtonStyle: + return QLatin1StringView("ToolButtonStyle"); + case QKdeThemePrivate::KdeSetting::WheelScrollLines: + return QLatin1StringView("WheelScrollLines"); + case QKdeThemePrivate::KdeSetting::DoubleClickInterval: + return QLatin1StringView("DoubleClickInterval"); + case QKdeThemePrivate::KdeSetting::StartDragDistance: + return QLatin1StringView("StartDragDist"); + case QKdeThemePrivate::KdeSetting::StartDragTime: + return QLatin1StringView("StartDragTime"); + case QKdeThemePrivate::KdeSetting::CursorBlinkRate: + return QLatin1StringView("CursorBlinkRate"); + case QKdeThemePrivate::KdeSetting::Font: + return QLatin1StringView("font"); + case QKdeThemePrivate::KdeSetting::Fixed: + return QLatin1StringView("fixed"); + case QKdeThemePrivate::KdeSetting::MenuFont: + return QLatin1StringView("menuFont"); + case QKdeThemePrivate::KdeSetting::ToolBarFont: + return QLatin1StringView("toolBarFont"); + case QKdeThemePrivate::KdeSetting::ButtonBackground: + return QLatin1StringView("Button/BackgroundNormal"); + case QKdeThemePrivate::KdeSetting::WindowBackground: + return QLatin1StringView("Window/BackgroundNormal"); + case QKdeThemePrivate::KdeSetting::ViewForeground: + return QLatin1StringView("View/ForegroundNormal"); + case QKdeThemePrivate::KdeSetting::WindowForeground: + return QLatin1StringView("Window/ForegroundNormal"); + case QKdeThemePrivate::KdeSetting::ViewBackground: + return QLatin1StringView("View/BackgroundNormal"); + case QKdeThemePrivate::KdeSetting::SelectionBackground: + return QLatin1StringView("Selection/BackgroundNormal"); + case QKdeThemePrivate::KdeSetting::SelectionForeground: + return QLatin1StringView("Selection/ForegroundNormal"); + case QKdeThemePrivate::KdeSetting::ViewBackgroundAlternate: + return QLatin1StringView("View/BackgroundAlternate"); + case QKdeThemePrivate::KdeSetting::ButtonForeground: + return QLatin1StringView("Button/ForegroundNormal"); + case QKdeThemePrivate::KdeSetting::ViewForegroundLink: + return QLatin1StringView("View/ForegroundLink"); + case QKdeThemePrivate::KdeSetting::ViewForegroundVisited: + return QLatin1StringView("View/ForegroundVisited"); + case QKdeThemePrivate::KdeSetting::TooltipBackground: + return QLatin1StringView("Tooltip/BackgroundNormal"); + case QKdeThemePrivate::KdeSetting::TooltipForeground: + return QLatin1StringView("Tooltip/ForegroundNormal"); + }; + Q_UNREACHABLE_RETURN(QLatin1StringView()); +} + void QKdeThemePrivate::refresh() { resources.clear(); + clearKdeSettings(); toolButtonStyle = Qt::ToolButtonTextBesideIcon; toolBarIconSize = 0; @@ -446,45 +816,39 @@ void QKdeThemePrivate::refresh() else iconFallbackThemeName = iconThemeName = QStringLiteral("oxygen"); - QHash<QString, QSettings*> kdeSettings; - QPalette systemPalette = QPalette(); readKdeSystemPalette(kdeDirs, kdeVersion, kdeSettings, &systemPalette); resources.palettes[QPlatformTheme::SystemPalette] = new QPalette(systemPalette); //## TODO tooltip color - const QVariant styleValue = readKdeSetting(QStringLiteral("widgetStyle"), kdeDirs, kdeVersion, kdeSettings); + const QVariant styleValue = readKdeSetting(KdeSetting::WidgetStyle); if (styleValue.isValid()) { const QString style = styleValue.toString(); if (style != styleNames.front()) styleNames.push_front(style); } - const QVariant colorScheme = readKdeSetting(QStringLiteral("ColorScheme"), kdeDirs, - kdeVersion, kdeSettings); + const QVariant colorScheme = readKdeSetting(KdeSetting::ColorScheme); - if (colorScheme.isValid()) - updateAppearance(colorScheme.toString()); - else - m_appearance = Qt::Appearance::Unknown; + updateColorScheme(colorScheme.toString()); - const QVariant singleClickValue = readKdeSetting(QStringLiteral("KDE/SingleClick"), kdeDirs, kdeVersion, kdeSettings); + const QVariant singleClickValue = readKdeSetting(KdeSetting::SingleClick); if (singleClickValue.isValid()) singleClick = singleClickValue.toBool(); - const QVariant showIconsOnPushButtonsValue = readKdeSetting(QStringLiteral("KDE/ShowIconsOnPushButtons"), kdeDirs, kdeVersion, kdeSettings); + const QVariant showIconsOnPushButtonsValue = readKdeSetting(KdeSetting::ShowIconsOnPushButtons); if (showIconsOnPushButtonsValue.isValid()) showIconsOnPushButtons = showIconsOnPushButtonsValue.toBool(); - const QVariant themeValue = readKdeSetting(QStringLiteral("Icons/Theme"), kdeDirs, kdeVersion, kdeSettings); + const QVariant themeValue = readKdeSetting(KdeSetting::IconTheme); if (themeValue.isValid()) iconThemeName = themeValue.toString(); - const QVariant toolBarIconSizeValue = readKdeSetting(QStringLiteral("ToolbarIcons/Size"), kdeDirs, kdeVersion, kdeSettings); + const QVariant toolBarIconSizeValue = readKdeSetting(KdeSetting::ToolBarIconSize); if (toolBarIconSizeValue.isValid()) toolBarIconSize = toolBarIconSizeValue.toInt(); - const QVariant toolbarStyleValue = readKdeSetting(QStringLiteral("Toolbar style/ToolButtonStyle"), kdeDirs, kdeVersion, kdeSettings); + const QVariant toolbarStyleValue = readKdeSetting(KdeSetting::ToolButtonStyle); if (toolbarStyleValue.isValid()) { const QString toolBarStyle = toolbarStyleValue.toString(); if (toolBarStyle == "TextBesideIcon"_L1) @@ -495,35 +859,35 @@ void QKdeThemePrivate::refresh() toolButtonStyle = Qt::ToolButtonTextUnderIcon; } - const QVariant wheelScrollLinesValue = readKdeSetting(QStringLiteral("KDE/WheelScrollLines"), kdeDirs, kdeVersion, kdeSettings); + const QVariant wheelScrollLinesValue = readKdeSetting(KdeSetting::WheelScrollLines); if (wheelScrollLinesValue.isValid()) wheelScrollLines = wheelScrollLinesValue.toInt(); - const QVariant doubleClickIntervalValue = readKdeSetting(QStringLiteral("KDE/DoubleClickInterval"), kdeDirs, kdeVersion, kdeSettings); + const QVariant doubleClickIntervalValue = readKdeSetting(KdeSetting::DoubleClickInterval); if (doubleClickIntervalValue.isValid()) doubleClickInterval = doubleClickIntervalValue.toInt(); - const QVariant startDragDistValue = readKdeSetting(QStringLiteral("KDE/StartDragDist"), kdeDirs, kdeVersion, kdeSettings); + const QVariant startDragDistValue = readKdeSetting(KdeSetting::StartDragDistance); if (startDragDistValue.isValid()) startDragDist = startDragDistValue.toInt(); - const QVariant startDragTimeValue = readKdeSetting(QStringLiteral("KDE/StartDragTime"), kdeDirs, kdeVersion, kdeSettings); + const QVariant startDragTimeValue = readKdeSetting(KdeSetting::StartDragTime); if (startDragTimeValue.isValid()) startDragTime = startDragTimeValue.toInt(); - const QVariant cursorBlinkRateValue = readKdeSetting(QStringLiteral("KDE/CursorBlinkRate"), kdeDirs, kdeVersion, kdeSettings); + const QVariant cursorBlinkRateValue = readKdeSetting(KdeSetting::CursorBlinkRate); if (cursorBlinkRateValue.isValid()) { cursorBlinkRate = cursorBlinkRateValue.toInt(); cursorBlinkRate = cursorBlinkRate > 0 ? qBound(200, cursorBlinkRate, 2000) : 0; } // Read system font, ignore 'smallestReadableFont' - if (QFont *systemFont = kdeFont(readKdeSetting(QStringLiteral("font"), kdeDirs, kdeVersion, kdeSettings))) + if (QFont *systemFont = kdeFont(readKdeSetting(KdeSetting::Font))) resources.fonts[QPlatformTheme::SystemFont] = systemFont; else resources.fonts[QPlatformTheme::SystemFont] = new QFont(QLatin1StringView(defaultSystemFontNameC), defaultSystemFontSize); - if (QFont *fixedFont = kdeFont(readKdeSetting(QStringLiteral("fixed"), kdeDirs, kdeVersion, kdeSettings))) { + if (QFont *fixedFont = kdeFont(readKdeSetting(KdeSetting::Fixed))) { resources.fonts[QPlatformTheme::FixedFont] = fixedFont; } else { fixedFont = new QFont(QLatin1StringView(defaultFixedFontNameC), defaultSystemFontSize); @@ -531,12 +895,12 @@ void QKdeThemePrivate::refresh() resources.fonts[QPlatformTheme::FixedFont] = fixedFont; } - if (QFont *menuFont = kdeFont(readKdeSetting(QStringLiteral("menuFont"), kdeDirs, kdeVersion, kdeSettings))) { + if (QFont *menuFont = kdeFont(readKdeSetting(KdeSetting::MenuFont))) { resources.fonts[QPlatformTheme::MenuFont] = menuFont; resources.fonts[QPlatformTheme::MenuBarFont] = new QFont(*menuFont); } - if (QFont *toolBarFont = kdeFont(readKdeSetting(QStringLiteral("toolBarFont"), kdeDirs, kdeVersion, kdeSettings))) + if (QFont *toolBarFont = kdeFont(readKdeSetting(KdeSetting::ToolBarFont))) resources.fonts[QPlatformTheme::ToolButtonFont] = toolBarFont; QWindowSystemInterface::handleThemeChange(); @@ -546,7 +910,7 @@ void QKdeThemePrivate::refresh() qDeleteAll(kdeSettings); } -QVariant QKdeThemePrivate::readKdeSetting(const QString &key, const QStringList &kdeDirs, int kdeVersion, QHash<QString, QSettings*> &kdeSettings) +QVariant QKdeThemePrivate::readKdeSetting(KdeSetting s, const QStringList &kdeDirs, int kdeVersion, QHash<QString, QSettings*> &kdeSettings) { for (const QString &kdeDir : kdeDirs) { QSettings *settings = kdeSettings.value(kdeDir); @@ -558,6 +922,7 @@ QVariant QKdeThemePrivate::readKdeSetting(const QString &key, const QStringList } } if (settings) { + const QString key = settingsPrefix(settingsType(s)) + settingsKey(s); const QVariant value = settings->value(key); if (value.isValid()) return value; @@ -566,6 +931,16 @@ QVariant QKdeThemePrivate::readKdeSetting(const QString &key, const QStringList return QVariant(); } +QVariant QKdeThemePrivate::readKdeSetting(KdeSetting s) const +{ + return readKdeSetting(s, kdeDirs, kdeVersion, kdeSettings); +} + +void QKdeThemePrivate::clearKdeSettings() const +{ + kdeSettings.clear(); +} + // Reads the color from the KDE configuration, and store it in the // palette with the given color role if found. static inline bool kdeColor(QPalette *pal, QPalette::ColorRole role, const QVariant &value) @@ -581,7 +956,7 @@ static inline bool kdeColor(QPalette *pal, QPalette::ColorRole role, const QVari void QKdeThemePrivate::readKdeSystemPalette(const QStringList &kdeDirs, int kdeVersion, QHash<QString, QSettings*> &kdeSettings, QPalette *pal) { - if (!kdeColor(pal, QPalette::Button, readKdeSetting(QStringLiteral("Colors:Button/BackgroundNormal"), kdeDirs, kdeVersion, kdeSettings))) { + if (!kdeColor(pal, QPalette::Button, readKdeSetting(KdeSetting::ButtonBackground, kdeDirs, kdeVersion, kdeSettings))) { // kcolorscheme.cpp: SetDefaultColors const QColor defaultWindowBackground(214, 210, 208); const QColor defaultButtonBackground(223, 220, 217); @@ -589,18 +964,18 @@ void QKdeThemePrivate::readKdeSystemPalette(const QStringList &kdeDirs, int kdeV return; } - kdeColor(pal, QPalette::Window, readKdeSetting(QStringLiteral("Colors:Window/BackgroundNormal"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::Text, readKdeSetting(QStringLiteral("Colors:View/ForegroundNormal"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::WindowText, readKdeSetting(QStringLiteral("Colors:Window/ForegroundNormal"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::Base, readKdeSetting(QStringLiteral("Colors:View/BackgroundNormal"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::Highlight, readKdeSetting(QStringLiteral("Colors:Selection/BackgroundNormal"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::HighlightedText, readKdeSetting(QStringLiteral("Colors:Selection/ForegroundNormal"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::AlternateBase, readKdeSetting(QStringLiteral("Colors:View/BackgroundAlternate"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::ButtonText, readKdeSetting(QStringLiteral("Colors:Button/ForegroundNormal"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::Link, readKdeSetting(QStringLiteral("Colors:View/ForegroundLink"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::LinkVisited, readKdeSetting(QStringLiteral("Colors:View/ForegroundVisited"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::ToolTipBase, readKdeSetting(QStringLiteral("Colors:Tooltip/BackgroundNormal"), kdeDirs, kdeVersion, kdeSettings)); - kdeColor(pal, QPalette::ToolTipText, readKdeSetting(QStringLiteral("Colors:Tooltip/ForegroundNormal"), kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::Window, readKdeSetting(KdeSetting::WindowBackground, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::Text, readKdeSetting(KdeSetting::ViewForeground, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::WindowText, readKdeSetting(KdeSetting::WindowForeground, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::Base, readKdeSetting(KdeSetting::ViewBackground, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::Highlight, readKdeSetting(KdeSetting::SelectionBackground, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::HighlightedText, readKdeSetting(KdeSetting::SelectionForeground, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::AlternateBase, readKdeSetting(KdeSetting::ViewBackgroundAlternate, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::ButtonText, readKdeSetting(KdeSetting::ButtonForeground, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::Link, readKdeSetting(KdeSetting::ViewForegroundLink, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::LinkVisited, readKdeSetting(KdeSetting::ViewForegroundVisited, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::ToolTipBase, readKdeSetting(KdeSetting::TooltipBackground, kdeDirs, kdeVersion, kdeSettings)); + kdeColor(pal, QPalette::ToolTipText, readKdeSetting(KdeSetting::TooltipForeground, kdeDirs, kdeVersion, kdeSettings)); // The above code sets _all_ color roles to "normal" colors. In KDE, the disabled // color roles are calculated by applying various effects described in kdeglobals. @@ -749,48 +1124,47 @@ QIcon QKdeTheme::fileIcon(const QFileInfo &fileInfo, QPlatformTheme::IconOptions #endif } -Qt::Appearance QKdeTheme::appearance() const +Qt::ColorScheme QKdeTheme::colorScheme() const { - return d_func()->m_appearance; + return d_func()->m_colorScheme; } /*! \internal - \brief QKdeTheme::setAppearance - guess and set appearance for unix themes. - KDE themes do not have an appearance property. - The key words "dark" or "light" should be part of the theme name. + \brief QKdeTheme::updateColorScheme - guess and set a color scheme for unix themes. + KDE themes do not have a color scheme property. + The key words "dark" or "light" are usually part of the theme name. This is, however, not a mandatory convention. - If \param themeName contains a key word, the respective appearance is set. - If it doesn't, the appearance is heuristically determined by comparing text and base color + If \param themeName contains a valid key word, the respective color scheme is set. + If it doesn't, the color scheme is heuristically determined by comparing text and base color of the system palette. */ -void QKdeThemePrivate::updateAppearance(const QString &themeName) +void QKdeThemePrivate::updateColorScheme(const QString &themeName) { if (themeName.contains(QLatin1StringView("light"), Qt::CaseInsensitive)) { - m_appearance = Qt::Appearance::Light; + m_colorScheme = Qt::ColorScheme::Light; return; } if (themeName.contains(QLatin1StringView("dark"), Qt::CaseInsensitive)) { - m_appearance = Qt::Appearance::Dark; + m_colorScheme = Qt::ColorScheme::Dark; return; } if (systemPalette) { if (systemPalette->text().color().lightness() < systemPalette->base().color().lightness()) { - m_appearance = Qt::Appearance::Light; + m_colorScheme = Qt::ColorScheme::Light; return; } if (systemPalette->text().color().lightness() > systemPalette->base().color().lightness()) { - m_appearance = Qt::Appearance::Dark; + m_colorScheme = Qt::ColorScheme::Dark; return; } } - m_appearance = Qt::Appearance::Unknown; + m_colorScheme = Qt::ColorScheme::Unknown; } - const QPalette *QKdeTheme::palette(Palette type) const { Q_D(const QKdeTheme); @@ -870,7 +1244,7 @@ QPlatformMenuBar *QKdeTheme::createPlatformMenuBar() const #if !defined(QT_NO_DBUS) && !defined(QT_NO_SYSTEMTRAYICON) QPlatformSystemTrayIcon *QKdeTheme::createPlatformSystemTrayIcon() const { - if (isDBusTrayAvailable()) + if (shouldUseDBusTray()) return new QDBusTrayIcon(); return nullptr; } @@ -911,11 +1285,11 @@ public: mutable QFont *fixedFont = nullptr; #ifndef QT_NO_DBUS - Qt::Appearance m_appearance = Qt::Appearance::Unknown; + Qt::ColorScheme m_colorScheme = Qt::ColorScheme::Unknown; private: std::unique_ptr<QGenericUnixThemeDBusListener> dbus; bool initDbus(); - void updateAppearance(const QString &themeName); + void updateColorScheme(const QString &themeName); #endif // QT_NO_DBUS }; @@ -936,35 +1310,37 @@ QGnomeThemePrivate::~QGnomeThemePrivate() #ifndef QT_NO_DBUS bool QGnomeThemePrivate::initDbus() { - static constexpr QLatin1StringView service(""); - static constexpr QLatin1StringView path("/org/freedesktop/portal/desktop"); - static constexpr QLatin1StringView interface("org.freedesktop.portal.Settings"); - static constexpr QLatin1StringView signal("SettingChanged"); - dbus.reset(new QGenericUnixThemeDBusListener(service, path, interface, signal)); + dbus.reset(new QGenericUnixThemeDBusListener()); Q_ASSERT(dbus); // Wrap slot in a lambda to avoid inheriting QGnomeThemePrivate from QObject - auto wrapper = [this](QGenericUnixThemeDBusListener::SettingType type, const QString &value) { - if (type == QGenericUnixThemeDBusListener::SettingType::GtkTheme) - updateAppearance(value); - }; + auto wrapper = [this](QGenericUnixThemeDBusListener::Provider provider, + QGenericUnixThemeDBusListener::Setting setting, + const QString &value) { + if (provider != QGenericUnixThemeDBusListener::Provider::Gnome + && provider != QGenericUnixThemeDBusListener::Provider::Gtk) { + return; + } - return QObject::connect(dbus.get(), &QGenericUnixThemeDBusListener::settingChanged, wrapper); + if (setting == QGenericUnixThemeDBusListener::Setting::Theme) + updateColorScheme(value); + }; + return QObject::connect(dbus.get(), &QGenericUnixThemeDBusListener::settingChanged, dbus.get(), wrapper); } -void QGnomeThemePrivate::updateAppearance(const QString &themeName) +void QGnomeThemePrivate::updateColorScheme(const QString &themeName) { - const auto oldAppearance = m_appearance; + const auto oldColorScheme = m_colorScheme; if (themeName.contains(QLatin1StringView("light"), Qt::CaseInsensitive)) { - m_appearance = Qt::Appearance::Light; + m_colorScheme = Qt::ColorScheme::Light; } else if (themeName.contains(QLatin1StringView("dark"), Qt::CaseInsensitive)) { - m_appearance = Qt::Appearance::Dark; + m_colorScheme = Qt::ColorScheme::Dark; } else { - m_appearance = Qt::Appearance::Unknown; + m_colorScheme = Qt::ColorScheme::Unknown; } - if (oldAppearance != m_appearance) + if (oldColorScheme != m_colorScheme) QWindowSystemInterface::handleThemeChange(); } #endif // QT_NO_DBUS @@ -1053,9 +1429,9 @@ QPlatformMenuBar *QGnomeTheme::createPlatformMenuBar() const return nullptr; } -Qt::Appearance QGnomeTheme::appearance() const +Qt::ColorScheme QGnomeTheme::colorScheme() const { - return d_func()->m_appearance; + return d_func()->m_colorScheme; } #endif @@ -1063,7 +1439,7 @@ Qt::Appearance QGnomeTheme::appearance() const #if !defined(QT_NO_DBUS) && !defined(QT_NO_SYSTEMTRAYICON) QPlatformSystemTrayIcon *QGnomeTheme::createPlatformSystemTrayIcon() const { - if (isDBusTrayAvailable()) + if (shouldUseDBusTray()) return new QDBusTrayIcon(); return nullptr; } diff --git a/src/gui/platform/unix/qgenericunixthemes_p.h b/src/gui/platform/unix/qgenericunixthemes_p.h index ed60f9484c..63b20651e6 100644 --- a/src/gui/platform/unix/qgenericunixthemes_p.h +++ b/src/gui/platform/unix/qgenericunixthemes_p.h @@ -77,7 +77,7 @@ public: QPlatformTheme::IconOptions iconOptions = { }) const override; const QPalette *palette(Palette type = SystemPalette) const override; - Qt::Appearance appearance() const override; + Qt::ColorScheme colorScheme() const override; const QFont *font(Font type) const override; #ifndef QT_NO_DBUS @@ -107,7 +107,7 @@ public: virtual QString gtkFontName() const; #ifndef QT_NO_DBUS QPlatformMenuBar *createPlatformMenuBar() const override; - Qt::Appearance appearance() const override; + Qt::ColorScheme colorScheme() const override; #endif #if !defined(QT_NO_DBUS) && !defined(QT_NO_SYSTEMTRAYICON) QPlatformSystemTrayIcon *createPlatformSystemTrayIcon() const override; diff --git a/src/gui/platform/unix/qunixnativeinterface.cpp b/src/gui/platform/unix/qunixnativeinterface.cpp index 1cca97b34c..09561d9ada 100644 --- a/src/gui/platform/unix/qunixnativeinterface.cpp +++ b/src/gui/platform/unix/qunixnativeinterface.cpp @@ -123,6 +123,22 @@ QOpenGLContext *QNativeInterface::QGLXContext::fromNative(GLXContext visualBased \return the EGLDisplay associated with the underlying EGLContext. */ + +/*! + \fn void QNativeInterface::QEGLContext::invalidateContext() + \since 6.5 + \brief Marks the context as invalid + + If this context is used by the Qt Quick scenegraph, this will trigger the + SceneGraph to destroy this context and create a new one. + + Similarly to QPlatformWindow::invalidateSurface(), + this function can only be expected to have an effect on certain platforms, + such as eglfs. + + \sa QOpenGLContext::isValid(), QPlatformWindow::invalidateSurface() +*/ + QT_DEFINE_NATIVE_INTERFACE(QEGLContext); QT_DEFINE_PRIVATE_NATIVE_INTERFACE(QEGLIntegration); @@ -215,10 +231,11 @@ QT_DEFINE_PRIVATE_NATIVE_INTERFACE(QEvdevKeyMapper); #endif // QT_CONFIG(evdev) -#if defined(Q_OS_UNIX) +#if QT_CONFIG(wayland) /*! \class QNativeInterface::QWaylandApplication + \inheaderfile QGuiApplication \since 6.5 \brief Native interface to a Wayland application. @@ -255,19 +272,28 @@ QT_DEFINE_PRIVATE_NATIVE_INTERFACE(QEvdevKeyMapper); \fn wl_seat *QNativeInterface::QWaylandApplication::lastInputSeat() const \return the seat on which the last input event happened. */ +/*! + \fn wl_seat *QNativeInterface::QWaylandApplication::seat() const + \return the seat associated with the default input device. +*/ QT_DEFINE_NATIVE_INTERFACE(QWaylandApplication); /*! - \class QNativeInterface::Private::QWaylandScreen - \since 6.5 - \internal - \brief Native interface to QPlatformScreen. + \class QNativeInterface::QWaylandScreen + \since 6.7 + \brief Native interface to a screen on Wayland. + + Accessed through QScreen::nativeInterface(). \inmodule QtGui \ingroup native-interfaces + \ingroup native-interfaces-qscreen */ - -QT_DEFINE_PRIVATE_NATIVE_INTERFACE(QWaylandScreen); +/*! + \fn wl_output *QNativeInterface::QWaylandScreen::output() const + \return the underlying wl_output of this QScreen. +*/ +QT_DEFINE_NATIVE_INTERFACE(QWaylandScreen); /*! \class QNativeInterface::QWaylandWindow @@ -280,6 +306,6 @@ QT_DEFINE_PRIVATE_NATIVE_INTERFACE(QWaylandScreen); QT_DEFINE_PRIVATE_NATIVE_INTERFACE(QWaylandWindow); -#endif // Q_OS_UNIX +#endif // QT_CONFIG(wayland) QT_END_NAMESPACE diff --git a/src/gui/platform/unix/qxkbcommon.cpp b/src/gui/platform/unix/qxkbcommon.cpp index fd368f8282..ed29db3005 100644 --- a/src/gui/platform/unix/qxkbcommon.cpp +++ b/src/gui/platform/unix/qxkbcommon.cpp @@ -17,8 +17,6 @@ QT_BEGIN_NAMESPACE -Q_LOGGING_CATEGORY(lcXkbcommon, "qt.xkbcommon") - static int keysymToQtKey_internal(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers, xkb_state *state, xkb_keycode_t code, bool superAsMeta, bool hyperAsMeta); @@ -239,10 +237,14 @@ static constexpr const auto KeyTbl = qMakeArray( Xkb2Qt<XKB_KEY_dead_small_schwa, Qt::Key_Dead_Small_Schwa>, Xkb2Qt<XKB_KEY_dead_capital_schwa, Qt::Key_Dead_Capital_Schwa>, Xkb2Qt<XKB_KEY_dead_greek, Qt::Key_Dead_Greek>, +/* The following four XKB_KEY_dead keys got removed in libxkbcommon 1.6.0 + The define check is kind of version check here. */ +#ifdef XKB_KEY_dead_lowline Xkb2Qt<XKB_KEY_dead_lowline, Qt::Key_Dead_Lowline>, Xkb2Qt<XKB_KEY_dead_aboveverticalline, Qt::Key_Dead_Aboveverticalline>, Xkb2Qt<XKB_KEY_dead_belowverticalline, Qt::Key_Dead_Belowverticalline>, Xkb2Qt<XKB_KEY_dead_longsolidusoverlay, Qt::Key_Dead_Longsolidusoverlay>, +#endif // Special keys from X.org - This include multimedia keys, // wireless/bluetooth/uwb keys, special launcher keys, etc. @@ -298,6 +300,7 @@ static constexpr const auto KeyTbl = qMakeArray( Xkb2Qt<XKB_KEY_XF86Book, Qt::Key_Book>, Xkb2Qt<XKB_KEY_XF86CD, Qt::Key_CD>, Xkb2Qt<XKB_KEY_XF86Calculater, Qt::Key_Calculator>, + Xkb2Qt<XKB_KEY_XF86Calculator, Qt::Key_Calculator>, Xkb2Qt<XKB_KEY_XF86Clear, Qt::Key_Clear>, Xkb2Qt<XKB_KEY_XF86ClearGrab, Qt::Key_ClearGrab>, Xkb2Qt<XKB_KEY_XF86Close, Qt::Key_Close>, @@ -512,13 +515,13 @@ static int keysymToQtKey_internal(xkb_keysym_t keysym, Qt::KeyboardModifiers mod // numeric keypad keys qtKey = Qt::Key_0 + (keysym - XKB_KEY_KP_0); } else if (QXkbCommon::isLatin1(keysym)) { - // Upper-case first, since Qt::Keys are defined in terms of their - // upper-case versions. + // Most Qt::Key values are determined by their upper-case version, + // where this is in the Latin-1 repertoire. So start with that: qtKey = QXkbCommon::qxkbcommon_xkb_keysym_to_upper(keysym); - // Upper-casing a Latin1 character might move it out of Latin1 range, - // for example U+00B5 MICRO SIGN, which upper-case equivalent is - // U+039C GREEK CAPITAL LETTER MU. If that's the case, then map the - // original lower-case character. + // However, Key_mu and Key_ydiaeresis are U+00B5 MICRO SIGN and + // U+00FF LATIN SMALL LETTER Y WITH DIAERESIS, both lower-case, + // with upper-case forms outside Latin-1, so use them as they are + // since they're the Qt::Key values. if (!QXkbCommon::isLatin1(qtKey)) qtKey = keysym; } else { @@ -564,7 +567,7 @@ static int keysymToQtKey_internal(xkb_keysym_t keysym, Qt::KeyboardModifiers mod return qtKey; } -Qt::KeyboardModifiers QXkbCommon::modifiers(struct xkb_state *state) +Qt::KeyboardModifiers QXkbCommon::modifiers(struct xkb_state *state, xkb_keysym_t keysym) { Qt::KeyboardModifiers modifiers = Qt::NoModifier; @@ -577,6 +580,9 @@ Qt::KeyboardModifiers QXkbCommon::modifiers(struct xkb_state *state) if (xkb_state_mod_name_is_active(state, XKB_MOD_NAME_LOGO, XKB_STATE_MODS_EFFECTIVE) > 0) modifiers |= Qt::MetaModifier; + if (isKeypad(keysym)) + modifiers |= Qt::KeypadModifier; + return modifiers; } @@ -593,10 +599,24 @@ static const Qt::KeyboardModifiers ModsTbl[] = { Qt::NoModifier // Fall-back to raw Key_*, for non-latin1 kb layouts }; +/* + Compatibility until all sub modules have transitioned to new API below +*/ QList<int> QXkbCommon::possibleKeys(xkb_state *state, const QKeyEvent *event, bool superAsMeta, bool hyperAsMeta) { QList<int> result; + auto keyCombinations = possibleKeyCombinations(state, event, superAsMeta, hyperAsMeta); + for (auto keyCombination : keyCombinations) + result << keyCombination.toCombined(); + + return result; +} + +QList<QKeyCombination> QXkbCommon::possibleKeyCombinations(xkb_state *state, const QKeyEvent *event, + bool superAsMeta, bool hyperAsMeta) +{ + QList<QKeyCombination> result; quint32 keycode = event->nativeScanCode(); if (!keycode) return result; @@ -610,7 +630,7 @@ QList<int> QXkbCommon::possibleKeys(xkb_state *state, const QKeyEvent *event, ScopedXKBState scopedXkbQueryState(xkb_state_new(keymap)); xkb_state *queryState = scopedXkbQueryState.get(); if (!queryState) { - qCWarning(lcXkbcommon) << Q_FUNC_INFO << "failed to compile xkb keymap"; + qCWarning(lcQpaKeyMapper) << Q_FUNC_INFO << "failed to compile xkb keymap"; return result; } // get kb state from the master state and update the temporary state @@ -636,7 +656,7 @@ QList<int> QXkbCommon::possibleKeys(xkb_state *state, const QKeyEvent *event, int baseQtKey = keysymToQtKey_internal(sym, modifiers, queryState, keycode, superAsMeta, hyperAsMeta); if (baseQtKey) - result += (baseQtKey + int(modifiers)); + result += QKeyCombination::fromCombined(baseQtKey + int(modifiers)); xkb_mod_index_t shiftMod = xkb_keymap_mod_get_index(keymap, "Shift"); xkb_mod_index_t altMod = xkb_keymap_mod_get_index(keymap, "Alt"); @@ -682,8 +702,9 @@ QList<int> QXkbCommon::possibleKeys(xkb_state *state, const QKeyEvent *event, // catch only more specific shortcuts, i.e. Ctrl+Shift+= also generates Ctrl++ and +, // but Ctrl++ is more specific than +, so we should skip the last one bool ambiguous = false; - for (int shortcut : std::as_const(result)) { - if (int(shortcut & ~Qt::KeyboardModifierMask) == qtKey && (shortcut & mods) == mods) { + for (auto keyCombination : std::as_const(result)) { + if (keyCombination.key() == qtKey + && (keyCombination.keyboardModifiers() & mods) == mods) { ambiguous = true; break; } @@ -691,7 +712,7 @@ QList<int> QXkbCommon::possibleKeys(xkb_state *state, const QKeyEvent *event, if (ambiguous) continue; - result += (qtKey + int(mods)); + result += QKeyCombination::fromCombined(qtKey + int(mods)); } } @@ -723,13 +744,15 @@ void QXkbCommon::verifyHasLatinLayout(xkb_keymap *keymap) // selected layouts is irrelevant. Properly functioning desktop environments // handle this behind the scenes, even if no latin key based layout has been // explicitly listed in the selected layouts. - qCDebug(lcXkbcommon, "no keyboard layouts with latin keys present"); + qCDebug(lcQpaKeyMapper, "no keyboard layouts with latin keys present"); } xkb_keysym_t QXkbCommon::lookupLatinKeysym(xkb_state *state, xkb_keycode_t keycode) { xkb_layout_index_t layout; xkb_keysym_t sym = XKB_KEY_NoSymbol; + if (!state) + return sym; xkb_keymap *keymap = xkb_state_get_keymap(state); const xkb_layout_index_t layoutCount = xkb_keymap_num_layouts_for_key(keymap, keycode); const xkb_layout_index_t currentLayout = xkb_state_key_get_layout(state, keycode); @@ -795,7 +818,7 @@ void QXkbCommon::setXkbContext(QPlatformInputContext *inputContext, struct xkb_c QMetaMethod method = inputContext->metaObject()->method(methodIndex); Q_ASSERT(method.isValid()); if (!method.isValid()) - qCWarning(lcXkbcommon) << normalizedSignature << "not found on" << inputContextClassName; + qCWarning(lcQpaKeyMapper) << normalizedSignature << "not found on" << inputContextClassName; return method; }(); diff --git a/src/gui/platform/unix/qxkbcommon_p.h b/src/gui/platform/unix/qxkbcommon_p.h index adc96b2ad4..a40d794451 100644 --- a/src/gui/platform/unix/qxkbcommon_p.h +++ b/src/gui/platform/unix/qxkbcommon_p.h @@ -23,12 +23,12 @@ #include <xkbcommon/xkbcommon.h> +#include <qpa/qplatformkeymapper.h> + #include <memory> QT_BEGIN_NAMESPACE -Q_DECLARE_LOGGING_CATEGORY(lcXkbcommon) - class QEvent; class QKeyEvent; class QPlatformInputContext; @@ -44,26 +44,67 @@ public: static int keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers); static int keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers modifiers, xkb_state *state, xkb_keycode_t code, - bool superAsMeta = false, bool hyperAsMeta = false); + bool superAsMeta = true, bool hyperAsMeta = true); // xkbcommon_* API is part of libxkbcommon internals, with modifications as // described in the header of the implementation file. static void xkbcommon_XConvertCase(xkb_keysym_t sym, xkb_keysym_t *lower, xkb_keysym_t *upper); static xkb_keysym_t qxkbcommon_xkb_keysym_to_upper(xkb_keysym_t ks); - static Qt::KeyboardModifiers modifiers(struct xkb_state *state); + static Qt::KeyboardModifiers modifiers(struct xkb_state *state, xkb_keysym_t keysym = XKB_KEY_VoidSymbol); - static QList<int> possibleKeys(xkb_state *state, const QKeyEvent *event, - bool superAsMeta = false, bool hyperAsMeta = false); + static QList<int> possibleKeys(xkb_state *state, + const QKeyEvent *event, bool superAsMeta = false, bool hyperAsMeta = false); + static QList<QKeyCombination> possibleKeyCombinations(xkb_state *state, + const QKeyEvent *event, bool superAsMeta = false, bool hyperAsMeta = false); static void verifyHasLatinLayout(xkb_keymap *keymap); static xkb_keysym_t lookupLatinKeysym(xkb_state *state, xkb_keycode_t keycode); static bool isLatin1(xkb_keysym_t sym) { - return sym <= 0xff; + return sym >= 0x20 && sym <= 0xff; } static bool isKeypad(xkb_keysym_t sym) { - return sym >= XKB_KEY_KP_Space && sym <= XKB_KEY_KP_9; + switch (sym) { + case XKB_KEY_KP_Space: + case XKB_KEY_KP_Tab: + case XKB_KEY_KP_Enter: + case XKB_KEY_KP_F1: + case XKB_KEY_KP_F2: + case XKB_KEY_KP_F3: + case XKB_KEY_KP_F4: + case XKB_KEY_KP_Home: + case XKB_KEY_KP_Left: + case XKB_KEY_KP_Up: + case XKB_KEY_KP_Right: + case XKB_KEY_KP_Down: + case XKB_KEY_KP_Prior: + case XKB_KEY_KP_Next: + case XKB_KEY_KP_End: + case XKB_KEY_KP_Begin: + case XKB_KEY_KP_Insert: + case XKB_KEY_KP_Delete: + case XKB_KEY_KP_Equal: + case XKB_KEY_KP_Multiply: + case XKB_KEY_KP_Add: + case XKB_KEY_KP_Separator: + case XKB_KEY_KP_Subtract: + case XKB_KEY_KP_Decimal: + case XKB_KEY_KP_Divide: + case XKB_KEY_KP_0: + case XKB_KEY_KP_1: + case XKB_KEY_KP_2: + case XKB_KEY_KP_3: + case XKB_KEY_KP_4: + case XKB_KEY_KP_5: + case XKB_KEY_KP_6: + case XKB_KEY_KP_7: + case XKB_KEY_KP_8: + case XKB_KEY_KP_9: + return true; + default: + return false; + } } static void setXkbContext(QPlatformInputContext *inputContext, struct xkb_context *context); diff --git a/src/gui/platform/wasm/qlocalfileapi.cpp b/src/gui/platform/wasm/qlocalfileapi.cpp index b56490eba4..76b99361c4 100644 --- a/src/gui/platform/wasm/qlocalfileapi.cpp +++ b/src/gui/platform/wasm/qlocalfileapi.cpp @@ -8,29 +8,62 @@ QT_BEGIN_NAMESPACE namespace LocalFileApi { namespace { +std::string qtFilterListToFileInputAccept(const QStringList &filterList) +{ + QStringList transformed; + for (const auto &filter : filterList) { + const auto type = Type::fromQt(filter); + if (type && type->accept()) { + const auto &extensions = type->accept()->mimeType().extensions(); + std::transform(extensions.begin(), extensions.end(), std::back_inserter(transformed), + [](const Type::Accept::MimeType::Extension &extension) { + return extension.value().toString(); + }); + } + } + return transformed.join(QStringLiteral(",")).toStdString(); +} + std::optional<emscripten::val> qtFilterListToTypes(const QStringList &filterList) { using namespace qstdweb; using namespace emscripten; - auto types = emscripten::val::array(); for (const auto &fileFilter : filterList) { auto type = Type::fromQt(fileFilter); - if (type) - types.call<void>("push", type->asVal()); + if (type) { + auto jsType = emscripten::val::object(); + jsType.set("description", type->description().toString().toStdString()); + if (type->accept()) { + jsType.set("accept", ([&mimeType = type->accept()->mimeType()]() { + val acceptDict = val::object(); + + QList<emscripten::val> extensions; + extensions.reserve(mimeType.extensions().size()); + std::transform( + mimeType.extensions().begin(), mimeType.extensions().end(), + std::back_inserter(extensions), + [](const Type::Accept::MimeType::Extension &extension) { + return val(extension.value().toString().toStdString()); + }); + acceptDict.set("application/octet-stream", + emscripten::val::array(extensions.begin(), + extensions.end())); + return acceptDict; + })()); + } + types.call<void>("push", std::move(jsType)); + } } return types["length"].as<int>() == 0 ? std::optional<emscripten::val>() : types; } -} +} // namespace Type::Type(QStringView description, std::optional<Accept> accept) - : m_storage(emscripten::val::object()) + : m_description(description.trimmed()), m_accept(std::move(accept)) { - m_storage.set("description", description.trimmed().toString().toStdString()); - if (accept) - m_storage.set("accept", accept->asVal()); } Type::~Type() = default; @@ -69,12 +102,7 @@ std::optional<Type> Type::fromQt(QStringView type) return Type(description, std::move(*accept)); } -emscripten::val Type::asVal() const -{ - return m_storage; -} - -Type::Accept::Accept() : m_storage(emscripten::val::object()) { } +Type::Accept::Accept() = default; Type::Accept::~Accept() = default; @@ -100,39 +128,25 @@ std::optional<Type::Accept> Type::Accept::fromQt(QStringView qtRepresentation) internalMatch = internalRegex.matchView(qtRepresentation, internalMatch.capturedEnd()); } - accept.addMimeType(mimeType); + accept.setMimeType(mimeType); return accept; } -void Type::Accept::addMimeType(MimeType mimeType) -{ - // The mime type provided here does not seem to have any effect at the result at all. - m_storage.set("application/octet-stream", mimeType.asVal()); -} - -emscripten::val Type::Accept::asVal() const +void Type::Accept::setMimeType(MimeType mimeType) { - return m_storage; + m_mimeType = std::move(mimeType); } -Type::Accept::MimeType::MimeType() : m_storage(emscripten::val::array()) { } +Type::Accept::MimeType::MimeType() = default; Type::Accept::MimeType::~MimeType() = default; void Type::Accept::MimeType::addExtension(Extension extension) { - m_storage.call<void>("push", extension.asVal()); + m_extensions.push_back(std::move(extension)); } -emscripten::val Type::Accept::MimeType::asVal() const -{ - return m_storage; -} - -Type::Accept::MimeType::Extension::Extension(QStringView extension) - : m_storage(extension.toString().toStdString()) -{ -} +Type::Accept::MimeType::Extension::Extension(QStringView extension) : m_value(extension) { } Type::Accept::MimeType::Extension::~Extension() = default; @@ -161,15 +175,10 @@ Type::Accept::MimeType::Extension::fromQt(QStringView qtRepresentation) return std::nullopt; } -emscripten::val Type::Accept::MimeType::Extension::asVal() const -{ - return m_storage; -} - emscripten::val makeOpenFileOptions(const QStringList &filterList, bool acceptMultiple) { auto options = emscripten::val::object(); - if (auto typeList = LocalFileApi::qtFilterListToTypes(filterList)) { + if (auto typeList = qtFilterListToTypes(filterList); typeList) { options.set("types", std::move(*typeList)); options.set("excludeAcceptAllOption", true); } @@ -186,12 +195,17 @@ emscripten::val makeSaveFileOptions(const QStringList &filterList, const std::st if (!suggestedName.empty()) options.set("suggestedName", emscripten::val(suggestedName)); - if (auto typeList = LocalFileApi::qtFilterListToTypes(filterList)) + if (auto typeList = qtFilterListToTypes(filterList)) options.set("types", emscripten::val(std::move(*typeList))); return options; } -} // namespace LocalFileApi +std::string makeFileInputAccept(const QStringList &filterList) +{ + return qtFilterListToFileInputAccept(filterList); +} + +} // namespace LocalFileApi QT_END_NAMESPACE diff --git a/src/gui/platform/wasm/qlocalfileapi_p.h b/src/gui/platform/wasm/qlocalfileapi_p.h index a8e7f666f9..1398d674d8 100644 --- a/src/gui/platform/wasm/qlocalfileapi_p.h +++ b/src/gui/platform/wasm/qlocalfileapi_p.h @@ -24,7 +24,8 @@ QT_BEGIN_NAMESPACE namespace LocalFileApi { -class Q_CORE_EXPORT Type { +class Q_AUTOTEST_EXPORT Type +{ public: class Accept { public: @@ -36,12 +37,12 @@ public: ~Extension(); - emscripten::val asVal() const; + const QStringView &value() const { return m_value; } private: explicit Extension(QStringView extension); - emscripten::val m_storage; + QStringView m_value; }; MimeType(); @@ -49,37 +50,43 @@ public: void addExtension(Extension type); - emscripten::val asVal() const; + const std::vector<Extension> &extensions() const { return m_extensions; } private: - emscripten::val m_storage; + std::vector<Extension> m_extensions; }; static std::optional<Accept> fromQt(QStringView type); ~Accept(); - void addMimeType(MimeType mimeType); + void setMimeType(MimeType mimeType); - emscripten::val asVal() const; + const MimeType &mimeType() const { return m_mimeType; } private: Accept(); - emscripten::val m_storage; + MimeType m_mimeType; }; Type(QStringView description, std::optional<Accept> accept); ~Type(); static std::optional<Type> fromQt(QStringView type); - emscripten::val asVal() const; + const QStringView &description() const { return m_description; } + const std::optional<Accept> &accept() const { return m_accept; } private: - emscripten::val m_storage; + QStringView m_description; + std::optional<Accept> m_accept; }; -Q_CORE_EXPORT emscripten::val makeOpenFileOptions(const QStringList &filterList, bool acceptMultiple); -Q_CORE_EXPORT emscripten::val makeSaveFileOptions(const QStringList &filterList, const std::string& suggestedName); +Q_AUTOTEST_EXPORT emscripten::val makeOpenFileOptions(const QStringList &filterList, + bool acceptMultiple); +Q_AUTOTEST_EXPORT emscripten::val makeSaveFileOptions(const QStringList &filterList, + const std::string &suggestedName); + +Q_AUTOTEST_EXPORT std::string makeFileInputAccept(const QStringList &filterList); } // namespace LocalFileApi QT_END_NAMESPACE diff --git a/src/gui/platform/wasm/qwasmlocalfileaccess.cpp b/src/gui/platform/wasm/qwasmlocalfileaccess.cpp index 1b797be9fe..a946cda043 100644 --- a/src/gui/platform/wasm/qwasmlocalfileaccess.cpp +++ b/src/gui/platform/wasm/qwasmlocalfileaccess.cpp @@ -9,6 +9,8 @@ #include <emscripten/html5.h> #include <emscripten/val.h> +#include <QtCore/qregularexpression.h> + QT_BEGIN_NAMESPACE namespace QWasmLocalFileAccess { @@ -29,7 +31,7 @@ void showOpenViaHTMLPolyfill(const QStringList &accept, FileSelectMode fileSelec emscripten::val input = document.call<emscripten::val>("createElement", std::string("input")); input.set("type", "file"); input.set("style", "display:none"); - // input.set("accept", emscripten::val(accept)); + input.set("accept", LocalFileApi::makeFileInputAccept(accept)); Q_UNUSED(accept); input.set("multiple", emscripten::val(fileSelectMode == FileSelectMode::MultipleFiles)); @@ -124,7 +126,7 @@ void readFiles(const qstdweb::FileList &fileList, } // Read file data into caller-provided buffer - file.stream(buffer, [=]() { + file.stream(buffer, [readFile = readFile.get(), fileIndex, fileDataReady]() { fileDataReady(); (*readFile)(fileIndex + 1); }); @@ -132,15 +134,28 @@ void readFiles(const qstdweb::FileList &fileList, (*readFile)(0); } + +QStringList makeFilterList(const std::string &qtAcceptList) +{ + // copy of qt_make_filter_list() from qfiledialog.cpp + auto filter = QString::fromStdString(qtAcceptList); + if (filter.isEmpty()) + return QStringList(); + QString sep(";;"); + if (!filter.contains(sep) && filter.contains(u'\n')) + sep = u'\n'; + + return filter.split(sep); +} } -void downloadDataAsFile(const QByteArray &data, const std::string &fileNameHint) +void downloadDataAsFile(const char *content, size_t size, const std::string &fileNameHint) { // Save a file by creating programmatically clicking a download // link to an object url to a Blob containing a copy of the file // content. The copy is made so that the passed in content buffer // can be released as soon as this function returns. - qstdweb::Blob contentBlob = qstdweb::Blob::copyFrom(data.constData(), data.size()); + qstdweb::Blob contentBlob = qstdweb::Blob::copyFrom(content, size); emscripten::val document = emscripten::val::global("document"); emscripten::val window = qstdweb::window(); emscripten::val contentUrl = window["URL"].call<emscripten::val>("createObjectURL", contentBlob.val()); @@ -157,12 +172,12 @@ void downloadDataAsFile(const QByteArray &data, const std::string &fileNameHint) window["URL"].call<emscripten::val>("revokeObjectURL", contentUrl); } -void openFiles(const QStringList &accept, FileSelectMode fileSelectMode, +void openFiles(const std::string &accept, FileSelectMode fileSelectMode, const std::function<void (int fileCount)> &fileDialogClosed, const std::function<char *(uint64_t size, const std::string& name)> &acceptFile, const std::function<void()> &fileDataReady) { - FileDialog::showOpen(accept, fileSelectMode, { + FileDialog::showOpen(makeFilterList(accept), fileSelectMode, { .thenFunc = [=](emscripten::val result) { auto files = qstdweb::FileList(result); fileDialogClosed(files.length()); @@ -174,7 +189,7 @@ void openFiles(const QStringList &accept, FileSelectMode fileSelectMode, }); } -void openFile(const QStringList &accept, +void openFile(const std::string &accept, const std::function<void (bool fileSelected)> &fileDialogClosed, const std::function<char *(uint64_t size, const std::string& name)> &acceptFile, const std::function<void()> &fileDataReady) @@ -195,6 +210,11 @@ void saveDataToFileInChunks(emscripten::val fileHandle, const QByteArray &data) std::function<void(val result)> continuation; }; + static constexpr size_t desiredChunkSize = 1024u; +#if defined(__EMSCRIPTEN_SHARED_MEMORY__) + qstdweb::Uint8Array chunkArray(desiredChunkSize); +#endif + auto state = std::make_shared<State>(); state->written = 0u; state->continuation = [=](val) mutable { @@ -204,11 +224,30 @@ void saveDataToFileInChunks(emscripten::val fileHandle, const QByteArray &data) state.reset(); return; } - static constexpr size_t desiredChunkSize = 1024u; + const auto currentChunkSize = std::min(remaining, desiredChunkSize); - Promise::make(writable, QStringLiteral("write"), { - .thenFunc = state->continuation, - }, val(typed_memory_view(currentChunkSize, data.constData() + state->written))); + +#if defined(__EMSCRIPTEN_SHARED_MEMORY__) + // If shared memory is used, WebAssembly.Memory is instantiated with the 'shared' + // option on. Passing a typed_memory_view to SharedArrayBuffer to + // FileSystemWritableFileStream.write is disallowed by security policies, so we + // need to make a copy of the data to a chunk array buffer. + Promise::make( + writable, QStringLiteral("write"), + { + .thenFunc = state->continuation, + }, + chunkArray.copyFrom(data.constData() + state->written, currentChunkSize) + .val() + .call<emscripten::val>("subarray", emscripten::val(0), + emscripten::val(currentChunkSize))); +#else + Promise::make(writable, QStringLiteral("write"), + { + .thenFunc = state->continuation, + }, + val(typed_memory_view(currentChunkSize, data.constData() + state->written))); +#endif state->written += currentChunkSize; }; @@ -220,7 +259,7 @@ void saveDataToFileInChunks(emscripten::val fileHandle, const QByteArray &data) void saveFile(const QByteArray &data, const std::string &fileNameHint) { if (!FileDialog::canShowSave()) { - downloadDataAsFile(data, fileNameHint); + downloadDataAsFile(data.constData(), data.size(), fileNameHint); return; } @@ -231,6 +270,20 @@ void saveFile(const QByteArray &data, const std::string &fileNameHint) }); } +void saveFile(const char *content, size_t size, const std::string &fileNameHint) +{ + if (!FileDialog::canShowSave()) { + downloadDataAsFile(content, size, fileNameHint); + return; + } + + FileDialog::showSave(fileNameHint, { + .thenFunc = [=](emscripten::val result) { + saveDataToFileInChunks(result, QByteArray(content, size)); + }, + }); +} + } // namespace QWasmLocalFileAccess QT_END_NAMESPACE diff --git a/src/gui/platform/wasm/qwasmlocalfileaccess_p.h b/src/gui/platform/wasm/qwasmlocalfileaccess_p.h index 040fe3c47a..77b14577f7 100644 --- a/src/gui/platform/wasm/qwasmlocalfileaccess_p.h +++ b/src/gui/platform/wasm/qwasmlocalfileaccess_p.h @@ -25,17 +25,18 @@ namespace QWasmLocalFileAccess { enum class FileSelectMode { SingleFile, MultipleFiles }; -Q_CORE_EXPORT void openFiles(const QStringList &accept, FileSelectMode fileSelectMode, +Q_CORE_EXPORT void openFiles(const std::string &accept, FileSelectMode fileSelectMode, const std::function<void (int fileCount)> &fileDialogClosed, const std::function<char *(uint64_t size, const std::string& name)> &acceptFile, const std::function<void()> &fileDataReady); -Q_CORE_EXPORT void openFile(const QStringList &accept, +Q_CORE_EXPORT void openFile(const std::string &accept, const std::function<void (bool fileSelected)> &fileDialogClosed, const std::function<char *(uint64_t size, const std::string& name)> &acceptFile, const std::function<void()> &fileDataReady); Q_CORE_EXPORT void saveFile(const QByteArray &data, const std::string &fileNameHint); +Q_CORE_EXPORT void saveFile(const char *content, size_t size, const std::string &fileNameHint); } // namespace QWasmLocalFileAccess diff --git a/src/gui/platform/wasm/qwasmnativeinterface.cpp b/src/gui/platform/wasm/qwasmnativeinterface.cpp new file mode 100644 index 0000000000..7313629a8d --- /dev/null +++ b/src/gui/platform/wasm/qwasmnativeinterface.cpp @@ -0,0 +1,17 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + + + +#include <QtGui/private/qguiapplication_p.h> +#include <qpa/qplatformintegration.h> +#include <qpa/qplatformwindow_p.h> + + +QT_BEGIN_NAMESPACE + +using namespace QNativeInterface::Private; + +QT_DEFINE_PRIVATE_NATIVE_INTERFACE(QWasmWindow); + +QT_END_NAMESPACE diff --git a/src/gui/platform/windows/qwindowsguieventdispatcher.cpp b/src/gui/platform/windows/qwindowsguieventdispatcher.cpp index f70655380d..c2f0efe96e 100644 --- a/src/gui/platform/windows/qwindowsguieventdispatcher.cpp +++ b/src/gui/platform/windows/qwindowsguieventdispatcher.cpp @@ -197,3 +197,5 @@ const char *QWindowsGuiEventDispatcher::windowsMessageName(UINT msg) } QT_END_NAMESPACE + +#include "moc_qwindowsguieventdispatcher_p.cpp" diff --git a/src/gui/platform/windows/qwindowsmimeconverter.cpp b/src/gui/platform/windows/qwindowsmimeconverter.cpp index d7998a3eb7..49d524cb99 100644 --- a/src/gui/platform/windows/qwindowsmimeconverter.cpp +++ b/src/gui/platform/windows/qwindowsmimeconverter.cpp @@ -25,6 +25,22 @@ QT_BEGIN_NAMESPACE conversions between Windows Clipboard and MIME formats, you can convert proprietary clipboard formats to MIME formats. + Construct an instance of your converter implementation after instantiating + QGuiApplication: + + \code + int main(int argc, char **argv) + { + QGuiApplication app(argc, argv); + JsonMimeConverter jsonConverter; + } + \endcode + + Destroying the instance will unregister the converter and remove support + for the conversion. It is also valid to heap-allocate the converter + instance; Qt takes ownership and will delete the converter object during + QGuiApplication shut-down. + Qt has predefined support for the following Windows Clipboard formats: \table @@ -112,6 +128,8 @@ QT_BEGIN_NAMESPACE The instance is automatically registered, and will be called to convert data during clipboard or drag'n'drop operations. + + Call this constructor after QGuiApplication has been created. */ QWindowsMimeConverter::QWindowsMimeConverter() { diff --git a/src/gui/platform/windows/qwindowsmimeconverter.h b/src/gui/platform/windows/qwindowsmimeconverter.h index 95722be109..145355fe15 100644 --- a/src/gui/platform/windows/qwindowsmimeconverter.h +++ b/src/gui/platform/windows/qwindowsmimeconverter.h @@ -1,4 +1,4 @@ -// Copyright (C) 2020 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QWINDOWSMIMECONVERTER_P_H @@ -20,6 +20,7 @@ class QVariant; class Q_GUI_EXPORT QWindowsMimeConverter { + Q_DISABLE_COPY(QWindowsMimeConverter) public: QWindowsMimeConverter(); virtual ~QWindowsMimeConverter(); diff --git a/src/gui/platform/windows/qwindowsnativeinterface.cpp b/src/gui/platform/windows/qwindowsnativeinterface.cpp index 86c6593f5d..44f230e1d3 100644 --- a/src/gui/platform/windows/qwindowsnativeinterface.cpp +++ b/src/gui/platform/windows/qwindowsnativeinterface.cpp @@ -89,14 +89,20 @@ QOpenGLContext *QNativeInterface::QWGLContext::fromNative(HGLRC context, HWND wi QT_DEFINE_PRIVATE_NATIVE_INTERFACE(QWindowsApplication); /*! - \class QNativeInterface::Private::QWindowsScreen - \since 6.5 - \internal - \brief Native interface to QScreen, to be retrieved from QPlatformIntegration. + \class QNativeInterface::QWindowsScreen + \since 6.7 + \brief Native interface to a screen. + + Accessed through QScreen::nativeInterface(). \inmodule QtGui \ingroup native-interfaces + \ingroup native-interfaces-qscreen */ -QT_DEFINE_PRIVATE_NATIVE_INTERFACE(QWindowsScreen); +/*! + * \fn HWMONITOR QNativeInterface::QWindowsScreen::handle() const; + * \return The underlying HWMONITOR of the screen. + */ +QT_DEFINE_NATIVE_INTERFACE(QWindowsScreen); /*! \enum QNativeInterface::Private::QWindowsApplication::TouchWindowTouchType @@ -175,15 +181,7 @@ QT_DEFINE_PRIVATE_NATIVE_INTERFACE(QWindowsScreen); \value DarkModeStyle The Windows Vista style will be turned off and a simple dark style will be used. - \sa isDarkMode(), setDarkModeHandling() -*/ - -/*! - \fn bool QNativeInterface::Private::QWindowsApplication::isDarkMode() const = 0 - \internal - - Returns \c true if Windows 10 is configured to use dark mode for - applications. + \sa setDarkModeHandling() */ /*! diff --git a/src/gui/platform/windows/qwindowsthemecache.cpp b/src/gui/platform/windows/qwindowsthemecache.cpp new file mode 100644 index 0000000000..3bb92e67ca --- /dev/null +++ b/src/gui/platform/windows/qwindowsthemecache.cpp @@ -0,0 +1,79 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qwindowsthemecache_p.h" +#include <QtCore/qdebug.h> +#include <QtCore/qhash.h> + +QT_BEGIN_NAMESPACE + +// Theme names matching the QWindowsVistaStylePrivate::Theme enumeration. +constexpr const wchar_t *themeNames[] = { + L"BUTTON", L"COMBOBOX", L"EDIT", L"HEADER", L"LISTVIEW", + L"MENU", L"PROGRESS", L"REBAR", L"SCROLLBAR", L"SPIN", + L"TAB", L"TASKDIALOG", L"TOOLBAR", L"TOOLTIP", L"TRACKBAR", + L"WINDOW", L"STATUS", L"TREEVIEW" +}; + +typedef std::array<HTHEME, std::size(themeNames)> ThemeArray; +typedef QHash<HWND, ThemeArray> ThemesCache; +Q_GLOBAL_STATIC(ThemesCache, themesCache); + +QString QWindowsThemeCache::themeName(int theme) +{ + return theme >= 0 && theme < int(std::size(themeNames)) + ? QString::fromWCharArray(themeNames[theme]) : QString(); +} + +HTHEME QWindowsThemeCache::createTheme(int theme, HWND hwnd) +{ + if (Q_UNLIKELY(theme < 0 || theme >= int(std::size(themeNames)) || !hwnd)) { + qWarning("Invalid parameters #%d, %p", theme, hwnd); + return nullptr; + } + + // Get or create themes array for this window. + ThemesCache *cache = themesCache(); + auto it = cache->find(hwnd); + if (it == cache->end()) + it = cache->insert(hwnd, ThemeArray {}); + + // Get or create theme data + ThemeArray &themes = *it; + if (!themes[theme]) { + const wchar_t *name = themeNames[theme]; + themes[theme] = OpenThemeData(hwnd, name); + if (Q_UNLIKELY(!themes[theme])) + qErrnoWarning("OpenThemeData() failed for theme %d (%s).", + theme, qPrintable(themeName(theme))); + } + return themes[theme]; +} + +static void clearThemes(ThemeArray &themes) +{ + for (auto &theme : themes) { + if (theme) { + CloseThemeData(theme); + theme = nullptr; + } + } +} + +void QWindowsThemeCache::clearThemeCache(HWND hwnd) +{ + ThemesCache *cache = themesCache(); + auto it = cache->find(hwnd); + if (it == cache->end()) + return; + clearThemes(*it); +} + +void QWindowsThemeCache::clearAllThemeCaches() +{ + ThemesCache *cache = themesCache(); + for (auto &themeArray : *cache) + clearThemes(themeArray); +} + +QT_END_NAMESPACE diff --git a/src/gui/platform/windows/qwindowsthemecache_p.h b/src/gui/platform/windows/qwindowsthemecache_p.h new file mode 100644 index 0000000000..beb724dc5c --- /dev/null +++ b/src/gui/platform/windows/qwindowsthemecache_p.h @@ -0,0 +1,35 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QWINDOWSTHEME_CACHE_P_H +#define QWINDOWSTHEME_CACHE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include "QtGui/private/qtguiglobal_p.h" + +#include <QtCore/qt_windows.h> +#include <uxtheme.h> + +QT_BEGIN_NAMESPACE + +namespace QWindowsThemeCache +{ + Q_GUI_EXPORT QString themeName(int theme); + Q_GUI_EXPORT HTHEME createTheme(int theme, HWND hwnd); + Q_GUI_EXPORT void clearThemeCache(HWND hwnd); + Q_GUI_EXPORT void clearAllThemeCaches(); +} + +QT_END_NAMESPACE + +#endif // QWINDOWSTHEME_CACHE_P_H |