Skip to content

Commit

Permalink
Use overridden paste() when activating items
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Dec 12, 2023
1 parent d22240b commit 5a798ba
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 14 deletions.
44 changes: 34 additions & 10 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "platform/platformclipboard.h"
#include "platform/platformnativeinterface.h"
#include "platform/platformwindow.h"
#include "scriptable/scriptoverrides.h"

#ifdef Q_OS_MAC
# include "platform/mac/foregroundbackgroundfilter.h"
Expand Down Expand Up @@ -1064,6 +1065,17 @@ bool MainWindow::isItemPreviewVisible() const
return m_showItemPreview;
}

void MainWindow::setScriptOverrides(const QVector<int> &overrides)
{
m_overrides = overrides;
std::sort(m_overrides.begin(), m_overrides.end());
}

bool MainWindow::isScriptOverridden(int id) const
{
return std::binary_search(m_overrides.begin(), m_overrides.end(), id);
}

void MainWindow::onAboutToQuit()
{
if (cm)
Expand Down Expand Up @@ -1869,10 +1881,15 @@ void MainWindow::activateMenuItem(ClipboardBrowserPlaceholder *placeholder, cons

PlatformWindowPtr lastWindow = m_windowForMenuPaste;

if ( m_options.trayItemPaste && lastWindow && !omitPaste && canPaste() ) {
COPYQ_LOG( QString("Pasting item from tray menu to \"%1\".")
.arg(lastWindow->getTitle()) );
lastWindow->pasteClipboard();
if ( m_options.trayItemPaste && !omitPaste && canPaste() ) {
if (isScriptOverridden(ScriptOverrides::Paste)) {
COPYQ_LOG("Pasting item with paste()");
runScript("paste()");
} else if (lastWindow) {
COPYQ_LOG( QStringLiteral("Pasting item from tray menu to: %1")
.arg(lastWindow->getTitle()) );
lastWindow->pasteClipboard();
}
}
}

Expand Down Expand Up @@ -2331,6 +2348,8 @@ void MainWindow::updateCommands(QVector<Command> allCommands, bool forceSave)
reloadBrowsers();
}

runScript("collectOverrides()");

updateContextMenu(contextMenuUpdateIntervalMsec);
updateTrayMenuCommands();
emit commandsSaved(commands);
Expand Down Expand Up @@ -3295,8 +3314,8 @@ void MainWindow::activateCurrentItemHelper()

// Perform custom actions on item activation.
PlatformWindowPtr lastWindow = m_windowForMainPaste;
const bool paste = lastWindow && m_options.activatePastes() && canPaste();
const bool activateWindow = paste || (lastWindow && m_options.activateFocuses());
const bool paste = m_options.activatePastes() && canPaste();
const bool activateWindow = m_options.activateFocuses();

// Copy current item or selection to clipboard.
// While clipboard is being set (in separate process)
Expand All @@ -3306,15 +3325,20 @@ void MainWindow::activateCurrentItemHelper()
if ( m_options.activateCloses() )
hideWindow();

if (activateWindow)
if (lastWindow && activateWindow)
lastWindow->raise();

enterBrowseMode();

if (paste) {
COPYQ_LOG( QString("Pasting item from main window to \"%1\".")
.arg(lastWindow->getTitle()) );
lastWindow->pasteClipboard();
if (isScriptOverridden(ScriptOverrides::Paste)) {
COPYQ_LOG("Pasting item with paste()");
runScript("paste()");
} else if (lastWindow) {
COPYQ_LOG( QStringLiteral("Pasting item from main window to: %1")
.arg(lastWindow->getTitle()) );
lastWindow->pasteClipboard();
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <QModelIndex>
#include <QPointer>
#include <QSystemTrayIcon>
#include <QSet>
#include <QTimer>
#include <QVector>

Expand Down Expand Up @@ -411,6 +412,9 @@ class MainWindow final : public QMainWindow
void setItemPreviewVisible(bool visible);
bool isItemPreviewVisible() const;

void setScriptOverrides(const QVector<int> &overrides);
bool isScriptOverridden(int id) const;

signals:
/** Request clipboard change. */
void changeClipboard(const QVariantMap &data, ClipboardMode mode);
Expand Down Expand Up @@ -693,6 +697,8 @@ class MainWindow final : public QMainWindow
bool m_isActiveWindow = false;
bool m_singleClickActivate = 0;
bool m_enteringSearchMode = false;

QVector<int> m_overrides;
};

#endif // MAINWINDOW_H
26 changes: 22 additions & 4 deletions src/scriptable/scriptable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "scriptable/scriptableproxy.h"
#include "scriptable/scriptablesettings.h"
#include "scriptable/scriptabletemporaryfile.h"
#include "scriptable/scriptoverrides.h"

#include <QApplication>
#include <QCryptographicHash>
Expand Down Expand Up @@ -690,24 +691,28 @@ Scriptable::Scriptable(

m_createFn = evaluateStrict(m_engine, QStringLiteral(
"(function(from, name) {"
"return function() {"
"var f = function() {"
"_copyqArguments = arguments;"
"var v = from[name]();"
"_copyqArguments = null;"
"if (_copyqHasUncaughtException) throw _copyqUncaughtException;"
"return v;"
"}"
"};"
"f._copyq = 1;"
"return f;"
"})"
));
m_createFnB = evaluateStrict(m_engine, QStringLiteral(
"(function(from, name) {"
"return function() {"
"var f = function() {"
"_copyqArguments = arguments;"
"var v = from[name]();"
"_copyqArguments = null;"
"if (_copyqHasUncaughtException) throw _copyqUncaughtException;"
"return ByteArray(v);"
"}"
"};"
"f._copyq = 1;"
"return f;"
"})"
));

Expand Down Expand Up @@ -2942,6 +2947,19 @@ QJSValue Scriptable::styles()
return toScriptValue( m_proxy->styles(), this );
}

void Scriptable::collectOverrides()
{
m_skipArguments = 1;
auto globalObject = engine()->globalObject();

QVector<int> overrides;
const auto pasteFn = globalObject.property("paste");
if (pasteFn.property("_copyq").toInt() != 1)
overrides.append(ScriptOverrides::Paste);

m_proxy->setScriptOverrides(overrides);
}

void Scriptable::onExecuteOutput(const QByteArray &output)
{
m_executeStdoutData.append(output);
Expand Down
2 changes: 2 additions & 0 deletions src/scriptable/scriptable.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,8 @@ public slots:

QJSValue styles();

void collectOverrides();

signals:
void finished();
void dataReceived(const QByteArray &data);
Expand Down
6 changes: 6 additions & 0 deletions src/scriptable/scriptableproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2601,6 +2601,12 @@ QStringList ScriptableProxy::styles()
return QStyleFactory::keys();
}

void ScriptableProxy::setScriptOverrides(const QVector<int> &overrides)
{
INVOKE2(setScriptOverrides, (overrides));
m_wnd->setScriptOverrides(overrides);
}

ClipboardBrowser *ScriptableProxy::fetchBrowser(const QString &tabName)
{
if (tabName.isEmpty()) {
Expand Down
2 changes: 2 additions & 0 deletions src/scriptable/scriptableproxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ public slots:

QStringList styles();

void setScriptOverrides(const QVector<int> &overrides);

signals:
void functionCallFinished(int functionCallId, const QVariant &returnValue);
void inputDialogFinished(int dialogId, const NamedValueList &result);
Expand Down
8 changes: 8 additions & 0 deletions src/scriptable/scriptoverrides.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once

namespace ScriptOverrides {
enum ScriptOverrides {
Paste = 0,
};
}

0 comments on commit 5a798ba

Please sign in to comment.