From 6adf78c511151de0cde431a31a7f0cf6d52b60cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Fri, 1 Dec 2023 10:01:10 +0100 Subject: [PATCH] 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. Change-Id: Idd08a90a3747546fd9825431ab7a4f5b5fa40784 Reviewed-by: Oliver Eftevaag Reviewed-by: Doris Verria (cherry picked from commit 9590ed6ba6dfc565832acddb69c69a3c8bf5e2e6) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/platforms/cocoa/qcocoaservices.h | 4 +++ src/plugins/platforms/cocoa/qcocoaservices.mm | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/plugins/platforms/cocoa/qcocoaservices.h b/src/plugins/platforms/cocoa/qcocoaservices.h index 20d9b677608..a0aec6f16b5 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 68daa660efb..4566cbb8f72 100644 --- a/src/plugins/platforms/cocoa/qcocoaservices.mm +++ b/src/plugins/platforms/cocoa/qcocoaservices.mm @@ -4,9 +4,11 @@ #include "qcocoaservices.h" #include +#include #include #include +#include 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