diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-12-01 10:01:10 +0100 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-12-02 11:21:59 +0100 |
commit | 9590ed6ba6dfc565832acddb69c69a3c8bf5e2e6 (patch) | |
tree | 379595cde265c14fcbc57cb7bf055b65c0699d93 | |
parent | 1202594db6b8b5ec801989d06004c39b725a67a0 (diff) |
macOS: Implement QPlatformServiceColorPicker via NSColorSampler
The default behavior for QColorDialog is to use the native dialog,
but for clients that implement their own color dialog we should
provide the plumbing to do the actual color picking. The fallback
path for this on macOS involves grabbing the screen, which brings
up the permission dialog. The NSColorSampler API does not have this
issue.
[ChangeLog][macOS] Non native color dialogs now use native color
picking when picking colors from the screen.
Pick-to: 6.6
Change-Id: Idd08a90a3747546fd9825431ab7a4f5b5fa40784
Reviewed-by: Oliver Eftevaag <oliver.eftevaag@qt.io>
Reviewed-by: Doris Verria <doris.verria@qt.io>
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoaservices.h | 4 | ||||
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoaservices.mm | 33 |
2 files changed, 37 insertions, 0 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoaservices.h b/src/plugins/platforms/cocoa/qcocoaservices.h index 20d9b67760..a0aec6f16b 100644 --- a/src/plugins/platforms/cocoa/qcocoaservices.h +++ b/src/plugins/platforms/cocoa/qcocoaservices.h @@ -11,8 +11,12 @@ QT_BEGIN_NAMESPACE class QCocoaServices : public QPlatformServices { public: + bool hasCapability(Capability capability) const override; + bool openUrl(const QUrl &url) override; bool openDocument(const QUrl &url) override; + + QPlatformServiceColorPicker *colorPicker(QWindow *parent) override; }; QT_END_NAMESPACE diff --git a/src/plugins/platforms/cocoa/qcocoaservices.mm b/src/plugins/platforms/cocoa/qcocoaservices.mm index 68daa660ef..4566cbb8f7 100644 --- a/src/plugins/platforms/cocoa/qcocoaservices.mm +++ b/src/plugins/platforms/cocoa/qcocoaservices.mm @@ -4,9 +4,11 @@ #include "qcocoaservices.h" #include <AppKit/NSWorkspace.h> +#include <AppKit/NSColorSampler.h> #include <Foundation/NSURL.h> #include <QtCore/QUrl> +#include <QtGui/private/qcoregraphics_p.h> QT_BEGIN_NAMESPACE @@ -20,4 +22,35 @@ bool QCocoaServices::openDocument(const QUrl &url) return openUrl(url); } +class QCocoaColorPicker : public QPlatformServiceColorPicker +{ +public: + QCocoaColorPicker() : m_colorSampler([NSColorSampler new]) {} + ~QCocoaColorPicker() { [m_colorSampler release]; } + + void pickColor() override + { + [m_colorSampler showSamplerWithSelectionHandler:^(NSColor *selectedColor) { + emit colorPicked(qt_mac_toQColor(selectedColor)); + }]; + } +private: + NSColorSampler *m_colorSampler = nullptr; +}; + + +QPlatformServiceColorPicker *QCocoaServices::colorPicker(QWindow *parent) +{ + Q_UNUSED(parent); + return new QCocoaColorPicker; +} + +bool QCocoaServices::hasCapability(Capability capability) const +{ + switch (capability) { + case ColorPicking: return true; + default: return false; + } +} + QT_END_NAMESPACE |