From 4d7d06bb9b92ca202a2652a0385ee6ae9db0bf89 Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Fri, 16 May 2025 22:16:28 -0700 Subject: [PATCH] core/qmlglobal: add clipboard support --- src/core/qmlglobal.cpp | 23 +++++++++++++++++++++++ src/core/qmlglobal.hpp | 13 +++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/core/qmlglobal.cpp b/src/core/qmlglobal.cpp index 7903bf41..71626be6 100644 --- a/src/core/qmlglobal.cpp +++ b/src/core/qmlglobal.cpp @@ -1,6 +1,7 @@ #include "qmlglobal.hpp" #include +#include #include #include #include @@ -17,6 +18,7 @@ #include #include #include +#include #include #include "generation.hpp" @@ -132,6 +134,13 @@ QuickshellGlobal::QuickshellGlobal(QObject* parent): QObject(parent) { QObject::connect(QuickshellTracked::instance(), &QuickshellTracked::screensChanged, this, &QuickshellGlobal::screensChanged); // clang-format on + + QObject::connect( + static_cast(QGuiApplication::instance())->clipboard(), // NOLINT + &QClipboard::changed, + this, + &QuickshellGlobal::onClipboardChanged + ); } qint32 QuickshellGlobal::processId() const { // NOLINT @@ -190,6 +199,20 @@ void QuickshellGlobal::setWatchFiles(bool watchFiles) { // NOLINT QuickshellSettings::instance()->setWatchFiles(watchFiles); } +QString QuickshellGlobal::clipboardText() { + return static_cast(QGuiApplication::instance())->clipboard()->text(); // NOLINT +} + +void QuickshellGlobal::setClipboardText(const QString& text) { + return static_cast(QGuiApplication::instance()) // NOLINT + ->clipboard() + ->setText(text); +} + +void QuickshellGlobal::onClipboardChanged(QClipboard::Mode mode) { + if (mode == QClipboard::Clipboard) emit this->clipboardTextChanged(); +} + QString QuickshellGlobal::dataDir() const { // NOLINT return QsPaths::instance()->shellDataDir().path(); } diff --git a/src/core/qmlglobal.hpp b/src/core/qmlglobal.hpp index 0290024f..8667a80e 100644 --- a/src/core/qmlglobal.hpp +++ b/src/core/qmlglobal.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -10,6 +11,7 @@ #include #include #include +#include #include "qmlscreen.hpp" @@ -108,6 +110,10 @@ class QuickshellGlobal: public QObject { /// If true then the configuration will be reloaded whenever any files change. /// Defaults to true. Q_PROPERTY(bool watchFiles READ watchFiles WRITE setWatchFiles NOTIFY watchFilesChanged); + /// The system clipboard. + /// + /// > [!WARNING] Under wayland the clipboard will be empty unless a quickshell window is focused. + Q_PROPERTY(QString clipboardText READ clipboardText WRITE setClipboardText NOTIFY clipboardTextChanged); /// The per-shell data directory. /// /// Usually `~/.local/share/quickshell/by-shell/` @@ -176,6 +182,9 @@ public: [[nodiscard]] bool watchFiles() const; void setWatchFiles(bool watchFiles); + [[nodiscard]] static QString clipboardText(); + static void setClipboardText(const QString& text); + [[nodiscard]] QString dataDir() const; [[nodiscard]] QString stateDir() const; [[nodiscard]] QString cacheDir() const; @@ -195,6 +204,10 @@ signals: void screensChanged(); void workingDirectoryChanged(); void watchFilesChanged(); + void clipboardTextChanged(); + +private slots: + void onClipboardChanged(QClipboard::Mode mode); private: QuickshellGlobal(QObject* parent = nullptr);