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 <oliver.eftevaag@qt.io>
Reviewed-by: Doris Verria <doris.verria@qt.io>
(cherry picked from commit 9590ed6ba6dfc565832acddb69c69a3c8bf5e2e6)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Tor Arne Vestbø 2023-12-01 10:01:10 +01:00 committed by Qt Cherry-pick Bot
parent 8156334674
commit 6adf78c511
2 changed files with 37 additions and 0 deletions

View File

@ -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

View File

@ -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