From 9fc0cff6a7f30cf4531d94761bc5f34f2b56c1b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ole-Andr=C3=A9=20Rodlie?= Date: Wed, 25 Dec 2024 19:09:21 +0100 Subject: [PATCH] Update Color Picker Provide a way to pick color in a sandbox (flatpak) and wayland, also avoid elevated permissions on macOS. Keep old code as a fallback. --- src/core/canvasmouseinteractions.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/core/canvasmouseinteractions.cpp b/src/core/canvasmouseinteractions.cpp index 6bb7735a4..09275d021 100644 --- a/src/core/canvasmouseinteractions.cpp +++ b/src/core/canvasmouseinteractions.cpp @@ -518,13 +518,21 @@ void Canvas::drawPathFinish(const qreal invScale) { const QColor Canvas::pickPixelColor(const QPoint &pos) { + // try the "safe" option first + if (QApplication::activeWindow()) { + const auto nPos = QApplication::activeWindow()->mapFromGlobal(pos); + return QApplication::activeWindow()->grab(QRect(QPoint(nPos.x(), nPos.y()), + QSize(1, 1))).toImage().pixel(0, 0); + } + + // "insecure" fallback (will not work in a sandbox or wayland) + // will prompt for permissions on macOS + // Windows and X11 don't care QScreen *screen = QApplication::screenAt(pos); if (!screen) { return QColor(); } WId wid = QApplication::desktop()->winId(); - QImage img = screen->grabWindow(wid, - pos.x(), pos.y(), - 1, 1).toImage(); - return QColor(img.pixel(0, 0)); + const auto pix = screen->grabWindow(wid, pos.x(), pos.y(), 1, 1); + return QColor(pix.toImage().pixel(0, 0)); } void Canvas::applyPixelColor(const QColor &color,