Skip to content

Commit

Permalink
Add event handlers
Browse files Browse the repository at this point in the history
Fixes #59
  • Loading branch information
hluk committed Dec 12, 2023
1 parent 5a798ba commit 4407f81
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 16 deletions.
6 changes: 4 additions & 2 deletions src/gui/clipboardbrowserplaceholder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ ClipboardBrowser *ClipboardBrowserPlaceholder::createBrowser()
return nullptr;

std::unique_ptr<ClipboardBrowser> c( new ClipboardBrowser(m_tabName, m_sharedData, this) );
emit browserCreated(c.get());

c->setStoreItems(m_storeItems);
c->setMaxItemCount(m_maxItemCount);
const bool loaded = c->loadItems();

emit browserCreated(c.get());

if ( !c->loadItems() ) {
if (!loaded) {
createLoadButton();
return nullptr;
}
Expand Down
68 changes: 55 additions & 13 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ void disableActionWhenTabGroupSelected(WidgetOrAction *action, MainWindow *windo
action, &WidgetOrAction::setDisabled );
}

void addSelectionData(
QVariantMap *result,
const QModelIndexList &selectedIndexes)
{
QList<QPersistentModelIndex> selected;
selected.reserve(selectedIndexes.size());
for (const auto &index : selectedIndexes)
selected.append(index);
std::sort(selected.begin(), selected.end());
result->insert(mimeSelectedItems, QVariant::fromValue(selected));
}

/// Adds information about current tab and selection if command is triggered by user.
QVariantMap addSelectionData(
const ClipboardBrowser &c,
Expand All @@ -203,12 +215,7 @@ QVariantMap addSelectionData(
}

if ( !selectedIndexes.isEmpty() ) {
QList<QPersistentModelIndex> selected;
selected.reserve(selectedIndexes.size());
for (const auto &index : selectedIndexes)
selected.append(index);
std::sort(selected.begin(), selected.end());
result.insert(mimeSelectedItems, QVariant::fromValue(selected));
addSelectionData(&result, selectedIndexes);
}

return result;
Expand Down Expand Up @@ -1254,12 +1261,40 @@ void MainWindow::onBrowserCreated(ClipboardBrowser *browser)
connect( browser, &ClipboardBrowser::itemWidgetCreated,
this, &MainWindow::onItemWidgetCreated );

connect( browser->model(), &QAbstractItemModel::rowsAboutToBeRemoved,
browser, [this, browser](const QModelIndex &, int first, int last) {
if (isScriptOverridden(ScriptOverrides::OnItemsRemoved))
runScriptEventHandler(QStringLiteral("onItemsRemoved()"), browser, first, last);
} );
connect( browser->model(), &QAbstractItemModel::rowsInserted,
browser, [this, browser](const QModelIndex &, int first, int last) {
if (isScriptOverridden(ScriptOverrides::OnItemsAdded))
runScriptEventHandler(QStringLiteral("onItemsAdded()"), browser, first, last);
} );

if (browserOrNull() == browser) {
const int index = ui->tabWidget->currentIndex();
tabChanged(index, index);
}
}

void MainWindow::runScriptEventHandler(const QString &script, ClipboardBrowser *browser, int firstRow, int lastRow)
{
QModelIndexList indexes;
indexes.reserve(lastRow - firstRow + 1);
for (int row = firstRow; row <= lastRow; ++row) {
const auto index = browser->model()->index(row, 0);
if (index.isValid())
indexes.append(index);
}

QVariantMap data;
data[mimeCurrentTab] = browser->tabName();
addSelectionData(&data, indexes);
const auto action = runScript(script, data);
action->waitForFinished();
}

void MainWindow::onBrowserDestroyed(ClipboardBrowserPlaceholder *placeholder)
{
if (placeholder == getPlaceholder()) {
Expand Down Expand Up @@ -1346,7 +1381,7 @@ void MainWindow::runDisplayCommands()

if ( !isInternalActionId(m_displayActionId) ) {
m_currentDisplayItem = m_displayItemList.takeFirst();
const auto action = runScript("runDisplayCommands()", m_currentDisplayItem.data());
const auto action = runScript(QStringLiteral("runDisplayCommands()"), m_currentDisplayItem.data());
m_displayActionId = action->id();
}

Expand Down Expand Up @@ -1613,7 +1648,7 @@ void MainWindow::runMenuCommandFilters(MenuMatchCommands *menuMatchCommands, QVa
if (isRunning) {
m_sharedData->actions->setActionData(menuMatchCommands->actionId, data);
} else {
const auto act = runScript("runMenuCommandFilters()", data);
const auto act = runScript(QStringLiteral("runMenuCommandFilters()"), data);
menuMatchCommands->actionId = act->id();
}

Expand Down Expand Up @@ -1884,7 +1919,7 @@ void MainWindow::activateMenuItem(ClipboardBrowserPlaceholder *placeholder, cons
if ( m_options.trayItemPaste && !omitPaste && canPaste() ) {
if (isScriptOverridden(ScriptOverrides::Paste)) {
COPYQ_LOG("Pasting item with paste()");
runScript("paste()");
runScript(QStringLiteral("paste()"));
} else if (lastWindow) {
COPYQ_LOG( QStringLiteral("Pasting item from tray menu to: %1")
.arg(lastWindow->getTitle()) );
Expand Down Expand Up @@ -2348,7 +2383,7 @@ void MainWindow::updateCommands(QVector<Command> allCommands, bool forceSave)
reloadBrowsers();
}

runScript("collectOverrides()");
runScript(QStringLiteral("collectOverrides()"));

updateContextMenu(contextMenuUpdateIntervalMsec);
updateTrayMenuCommands();
Expand Down Expand Up @@ -2404,7 +2439,8 @@ const Theme &MainWindow::theme() const
Action *MainWindow::runScript(const QString &script, const QVariantMap &data)
{
auto act = new Action();
act->setCommand(QStringList() << "copyq" << "eval" << "--" << script);
act->setCommand(
{QStringLiteral("copyq"), QStringLiteral("eval"), QStringLiteral("--"), script});
act->setData(data);
runInternalAction(act);
return act;
Expand Down Expand Up @@ -2984,6 +3020,12 @@ void MainWindow::tabChanged(int current, int)
}

setTabOrder(ui->searchBar, c);

if (isScriptOverridden(ScriptOverrides::OnSelectedTabChanged)) {
QVariantMap data;
data[mimeCurrentTab] = c->tabName();
runScript(QStringLiteral("onSelectedTabChanged()"), data);
}
}
}

Expand Down Expand Up @@ -3333,7 +3375,7 @@ void MainWindow::activateCurrentItemHelper()
if (paste) {
if (isScriptOverridden(ScriptOverrides::Paste)) {
COPYQ_LOG("Pasting item with paste()");
runScript("paste()");
runScript(QStringLiteral("paste()"));
} else if (lastWindow) {
COPYQ_LOG( QStringLiteral("Pasting item from main window to: %1")
.arg(lastWindow->getTitle()) );
Expand Down Expand Up @@ -3366,7 +3408,7 @@ void MainWindow::disableClipboardStoring(bool disable)

updateIcon();

runScript("setTitle(); showDataNotification()");
runScript(QStringLiteral("setTitle(); showDataNotification()"));

COPYQ_LOG( QString("Clipboard monitoring %1.")
.arg(m_clipboardStoringDisabled ? "disabled" : "enabled") );
Expand Down
2 changes: 2 additions & 0 deletions src/gui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,8 @@ class MainWindow final : public QMainWindow
void onItemClicked();
void onItemDoubleClicked();

void runScriptEventHandler(const QString &script, ClipboardBrowser *browser, int firstRow, int lastRow);

ConfigurationManager *cm;
Ui::MainWindow *ui;

Expand Down
8 changes: 7 additions & 1 deletion src/scriptable/scriptable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2954,8 +2954,14 @@ void Scriptable::collectOverrides()

QVector<int> overrides;
const auto pasteFn = globalObject.property("paste");
if (pasteFn.property("_copyq").toInt() != 1)
if (pasteFn.property(QStringLiteral("_copyq")).toInt() != 1)
overrides.append(ScriptOverrides::Paste);
if (globalObject.hasOwnProperty(QStringLiteral("onItemsAdded")))
overrides.append(ScriptOverrides::OnItemsAdded);
if (globalObject.hasOwnProperty(QStringLiteral("onItemsRemoved")))
overrides.append(ScriptOverrides::OnItemsRemoved);
if (globalObject.hasOwnProperty(QStringLiteral("onSelectedTabChanged")))
overrides.append(ScriptOverrides::OnSelectedTabChanged);

m_proxy->setScriptOverrides(overrides);
}
Expand Down
3 changes: 3 additions & 0 deletions src/scriptable/scriptoverrides.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
namespace ScriptOverrides {
enum ScriptOverrides {
Paste = 0,
OnItemsAdded = 1,
OnItemsRemoved = 2,
OnSelectedTabChanged = 3,
};
}
80 changes: 80 additions & 0 deletions src/tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3959,6 +3959,86 @@ void Tests::scriptCommandWithError()
m_test->setEnv("COPYQ_TEST_THROW", "0");
}

void Tests::scriptPaste()
{
const auto script = R"(
setCommands([
{
isScript: true,
cmd: 'global.paste = function() { add("PASTE CALLED") }'
},
])
)";
RUN(script, "");
RUN("add(1)", "");
RUN("keys" << clipboardBrowserId << "ENTER", "");
WAIT_ON_OUTPUT("read(0)", "PASTE CALLED");
}

void Tests::scriptOnSelectedTabChanged()
{
const auto script = R"(
setCommands([
{
isScript: true,
cmd: 'global.onSelectedTabChanged = function() { add(selectedTab()) }'
},
])
)";
RUN(script, "");
RUN("add" << "END", "");

const auto tab1 = testTab(1);
const auto tab2 = testTab(2);
RUN("show" << tab1, "");
WAIT_ON_OUTPUT("tab" << tab1 << "read(0)", tab1);
RUN("show" << tab2, "");
WAIT_ON_OUTPUT("tab" << tab2 << "read(0)", tab2);
}

void Tests::scriptOnItemsRemoved()
{
const auto script = R"(
setCommands([
{
isScript: true,
cmd: 'global.onItemsRemoved = function() {'
+ ' items = ItemSelection().current().items();'
+ ' tab(tab()[0]);'
+ ' add("R0:" + str(items[0][mimeText]));'
+ ' add("R1:" + str(items[1][mimeText]));'
+ '}'
},
])
)";
RUN(script, "");
const auto tab1 = testTab(1);
RUN("tab" << tab1 << "add(3,2,1,0)", "");
RUN("tab" << tab1 << "remove(1,2)", "");
WAIT_ON_OUTPUT("separator" << "," << "read(0,1,2,)", "R1:2,R0:1,");
}

void Tests::scriptOnItemsAdded()
{
const auto script = R"(
setCommands([
{
isScript: true,
cmd: 'global.onItemsAdded = function() {'
+ ' if (selectedTab() == tab()[0]) abort();'
+ ' items = ItemSelection().current().items();'
+ ' tab(tab()[0]);'
+ ' add("A:" + str(items[0][mimeText]));'
+ '}'
},
])
)";
RUN(script, "");
const auto tab1 = testTab(1);
RUN("tab" << tab1 << "add(1,0)", "");
WAIT_ON_OUTPUT("separator" << "," << "read(0,1,2)", "A:0,A:1,");
}

void Tests::displayCommand()
{
const auto testMime = COPYQ_MIME_PREFIX "test";
Expand Down
6 changes: 6 additions & 0 deletions src/tests/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ private slots:
void scriptCommandEnhanceFunction();
void scriptCommandEndingWithComment();
void scriptCommandWithError();

void scriptPaste();
void scriptOnSelectedTabChanged();
void scriptOnItemsRemoved();
void scriptOnItemsAdded();

void displayCommand();
void displayCommandForMenu();

Expand Down

0 comments on commit 4407f81

Please sign in to comment.