-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #348 from shenlebantongying/staged
Use alternative clipboard change monitor on macOS
- Loading branch information
Showing
6 changed files
with
114 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include "gd_clipboard.h" | ||
#include <QGuiApplication> | ||
|
||
gd_clipboard::gd_clipboard(QObject *parent) | ||
: QObject{parent}, | ||
sysClipboard(QGuiApplication::clipboard()) { | ||
|
||
connect(&m_monitoringTimer, &QTimer::timeout, this, [this]() { | ||
updateClipboard(); | ||
}); | ||
} | ||
|
||
QString gd_clipboard::text() const { | ||
return m_currentContent; | ||
} | ||
|
||
void gd_clipboard::updateClipboard() { | ||
const QString newContent = this->sysClipboard->text().trimmed(); | ||
|
||
// get rid of change if new clipboard content is equal to previous version | ||
if (newContent == m_currentContent || | ||
newContent.length() == 0) { // avoid spaces | ||
return; | ||
} | ||
|
||
m_currentContent = newContent; | ||
emit changed(QClipboard::Clipboard); | ||
} | ||
|
||
void gd_clipboard::stop() { | ||
m_monitoringTimer.stop(); | ||
} | ||
|
||
void gd_clipboard::start() { | ||
m_monitoringTimer.start(1000); // 1s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include <QClipboard> | ||
#include <QObject> | ||
#include <QString> | ||
#include <QTimer> | ||
|
||
/** | ||
A custom clipboard monitor that read keep reading clipboard in background. | ||
QClipboard on macOS only triggers when the app is focused. | ||
Code was inspired by | ||
https://github.com/KDE/kdeconnect-kde/blob/v22.12.2/plugins/clipboard/clipboardlistener.cpp | ||
*/ | ||
|
||
class gd_clipboard : public QObject { | ||
Q_OBJECT | ||
public: | ||
explicit gd_clipboard(QObject * parent = nullptr); | ||
|
||
[[nodiscard]] QString text() const; | ||
|
||
void stop(); | ||
|
||
void start(); | ||
|
||
private: | ||
|
||
QClipboard * sysClipboard; | ||
QString curClipboardContent; | ||
|
||
QTimer m_monitoringTimer; | ||
QString m_currentContent; | ||
void updateClipboard(); | ||
|
||
signals: | ||
void changed(QClipboard::Mode mode); | ||
}; | ||
|