Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ These shortcuts are available in GUI mode:

#### On KDE Plasma desktop

To make configuration easier, there's a [file](docs/shortcuts-config/flameshot-shortcuts-kde.khotkeys) in the repository that more or less automates this process. This file will assign the following hotkeys by default:
To make configuration easier, there's a [file](docs/shortcuts-config/flameshot-shortcuts-kde.kksrc) in the repository that more or less automates this process. This file will assign the following hotkeys by default:

| Keys | Description |
|--- |--- |
Expand Down
13 changes: 8 additions & 5 deletions src/core/flameshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include "qhotkey.h"
#endif

#if defined(Q_OS_MACOS)
#include <CoreGraphics/CoreGraphics.h>
#endif

#include "config/configresolver.h"
#include "config/configwindow.h"
#include "core/qguiappcurrentscreen.h"
Expand Down Expand Up @@ -54,11 +58,10 @@ Flameshot::Flameshot()
qApp->setStyleSheet(StyleSheet);

#if defined(Q_OS_MACOS)
// Try to take a test screenshot, MacOS will request a "Screen Recording"
// permissions on the first run. Otherwise it will be hidden under the
// CaptureWidget
QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen();
currentScreen->grabWindow(0, 0, 0, 1, 1);
// Request Screen Recording permission via the proper CoreGraphics API
if (!CGPreflightScreenCaptureAccess()) {
CGRequestScreenCaptureAccess();
}
#endif
#if (defined(Q_OS_MACOS) || defined(Q_OS_WIN))
// Set global shortcuts for MacOS or Windows
Expand Down
14 changes: 0 additions & 14 deletions src/core/flameshotdaemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,7 @@ void FlameshotDaemon::createPin(const QPixmap& capture, QRect geometry)

void FlameshotDaemon::copyToClipboard(const QPixmap& capture)
{
#if defined(Q_OS_MACOS) && defined(USE_KDSINGLEAPPLICATION)
auto kdsa = KDSingleApplication(QStringLiteral("org.flameshot.Flameshot"));
if (kdsa.isPrimaryInstance() && instance()) {
#else
if (instance()) {
#endif
instance()->attachScreenshotToClipboard(capture);
return;
}
Expand All @@ -156,9 +151,7 @@ void FlameshotDaemon::copyToClipboard(const QPixmap& capture)

#if defined(USE_KDSINGLEAPPLICATION) && \
(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
#if defined(Q_OS_WIN)
auto kdsa = KDSingleApplication(QStringLiteral("org.flameshot.Flameshot"));
#endif
stream << QStringLiteral("attachScreenshotToClipboard") << capture;
kdsa.sendMessage(data);
#else
Expand All @@ -174,21 +167,14 @@ void FlameshotDaemon::copyToClipboard(const QPixmap& capture)
void FlameshotDaemon::copyToClipboard(const QString& text,
const QString& notification)
{
#if defined(Q_OS_MACOS) && defined(USE_KDSINGLEAPPLICATION)
auto kdsa = KDSingleApplication(QStringLiteral("org.flameshot.Flameshot"));
if (kdsa.isPrimaryInstance() && instance()) {
#else
if (instance()) {
#endif
instance()->attachTextToClipboard(text, notification);
return;
}

#if defined(USE_KDSINGLEAPPLICATION) && \
(defined(Q_OS_MACOS) || defined(Q_OS_WIN))
#if defined(Q_OS_WIN)
auto kdsa = KDSingleApplication(QStringLiteral("org.flameshot.Flameshot"));
#endif
QByteArray data;
QDataStream stream(&data, QIODevice::WriteOnly);
stream << QStringLiteral("attachTextToClipboard") << text << notification;
Expand Down
60 changes: 15 additions & 45 deletions src/utils/screenshotsaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,45 +111,6 @@ QString ShowSaveFileDialog(const QString& title, const QString& directory)
}
}

void saveJpegToClipboardMacOS(const QPixmap& capture)
{
// Convert QPixmap to JPEG data
QByteArray jpegData;
QBuffer buffer(&jpegData);
buffer.open(QIODevice::WriteOnly);

QImageWriter imageWriter(&buffer, "jpeg");

// Set JPEG quality to whatever is in settings
imageWriter.setQuality(ConfigHandler().jpegQuality());
if (!imageWriter.write(capture.toImage())) {
qWarning() << "Failed to write image to JPEG format.";
return;
}

// Save JPEG data to a temporary file
QTemporaryFile tempFile;
if (!tempFile.open()) {
qWarning() << "Failed to open temporary file for writing.";
return;
}
tempFile.write(jpegData);
tempFile.close();

// Use osascript to copy the contents of the file to clipboard
QProcess process;
QString script =
QString("set the clipboard to (read (POSIX file \"%1\") as «class PNGf»)")
.arg(tempFile.fileName());
process.start("osascript", QStringList() << "-e" << script);
if (!process.waitForFinished()) {
qWarning() << "Failed to execute AppleScript.";
}

// Clean up
tempFile.remove();
}

void saveToClipboardMime(const QPixmap& capture, const QString& imageType)
{
QByteArray array;
Expand All @@ -169,7 +130,14 @@ void saveToClipboardMime(const QPixmap& capture, const QString& imageType)

auto* mimeData = new QMimeData();

#ifdef USE_WAYLAND_CLIPBOARD
#if defined(Q_OS_MACOS)
// setImageData provides the image in a native pasteboard format
// (public.tiff) that macOS can always access. Also include the
// original format bytes for apps that prefer PNG/JPEG.
mimeData->setImageData(formattedPixmap.toImage());
mimeData->setData("image/" + imageType, array);
QApplication::clipboard()->setMimeData(mimeData);
#elif defined(USE_WAYLAND_CLIPBOARD)
if (QGuiApplication::platformName() == "wayland") {
mimeData->setImageData(formattedPixmap.toImage());
mimeData->setData(QStringLiteral("x-kde-force-image-copy"),
Expand Down Expand Up @@ -205,14 +173,15 @@ void saveToClipboard(const QPixmap& capture)
} else {
AbstractLogger() << QObject::tr("Capture saved to clipboard.");
}
if (ConfigHandler().useJpgForClipboard()) {
#ifdef Q_OS_MACOS
saveJpegToClipboardMacOS(capture);
#if defined(Q_OS_MACOS)
// On macOS, setPixmap uses lazy clipboard which fails when the
// process exits ("Cannot keep promise"). Always use serialized bytes.
saveToClipboardMime(capture,
ConfigHandler().useJpgForClipboard() ? "jpeg" : "png");
#else
if (ConfigHandler().useJpgForClipboard()) {
saveToClipboardMime(capture, "jpeg");
#endif
} else {
// Need to send message before copying to clipboard
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
if (DesktopInfo().waylandDetected()) {
saveToClipboardMime(capture, "png");
Expand All @@ -223,6 +192,7 @@ void saveToClipboard(const QPixmap& capture)
QApplication::clipboard()->setPixmap(capture);
#endif
}
#endif
}

class ClipboardWatcherMimeData : public QMimeData
Expand Down
Loading