Skip to content

Commit

Permalink
Trigger global shortcut as application one if needed
Browse files Browse the repository at this point in the history
If global shortcut for a menu command is triggered when the main window
is active, the command will be executed as if it has been trigger from
menu - i.e. with item selection and item data available.

Fixes #1435

Signed-off-by: Lukas Holecek <hluk@email.cz>
  • Loading branch information
hluk committed Jul 14, 2020
1 parent 3f0c3e7 commit 9d08411
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/app/clipboardserver.cpp
Expand Up @@ -727,10 +727,19 @@ void ClipboardServer::shortcutActivated(QxtGlobalShortcut *shortcut)
const QMap<QxtGlobalShortcut*, Command>::const_iterator it =
m_shortcutActions.constFind(shortcut);
if ( it != m_shortcutActions.constEnd() ) {
QVariantMap data;
const QString shortcutText = portableShortcutText(shortcut->shortcut());
data.insert(mimeShortcut, shortcutText.toUtf8());
m_wnd->action(data, it.value(), QModelIndex());
const Command &command = it.value();

// If global shortcut for a menu command is triggered when the main window
// is active, the command will be executed as if it has been trigger from
// menu - i.e. with item selection and item data available.
if ( command.inMenu && m_wnd->isActiveWindow() && m_wnd->triggerMenuCommand(command, shortcutText) ) {
COPYQ_LOG("Global shortcut command triggered as a menu command");
} else {
QVariantMap data;
data.insert(mimeShortcut, shortcutText.toUtf8());
m_wnd->action(data, command, QModelIndex());
}
}
#endif
}
20 changes: 20 additions & 0 deletions src/gui/mainwindow.cpp
Expand Up @@ -3548,6 +3548,26 @@ Action *MainWindow::action(const QVariantMap &data, const Command &cmd, const QM
return nullptr;
}

bool MainWindow::triggerMenuCommand(const Command &command, const QString &triggeredShortcut)
{
updateShortcuts();

Command cmd = command;
for (auto act : m_menuItem->findChildren<CommandAction*>()) {
if ( !act->isEnabled() || !act->isVisible() )
continue;

const Command &menuCommand = act->command();
// Ignore outputTab value overridden in the action.
cmd.outputTab = menuCommand.outputTab;
if (cmd == menuCommand) {
onItemCommandActionTriggered(act, triggeredShortcut);
return true;
}
}
return false;
}

void MainWindow::runInternalAction(Action *action)
{
m_sharedData->actions->internalAction(action);
Expand Down
2 changes: 2 additions & 0 deletions src/gui/mainwindow.h
Expand Up @@ -356,6 +356,8 @@ class MainWindow final : public QMainWindow
const Command &cmd,
const QModelIndex &outputIndex);

bool triggerMenuCommand(const Command &command, const QString &triggeredShortcut);

void runInternalAction(Action *action);
bool isInternalActionId(int id) const;

Expand Down

0 comments on commit 9d08411

Please sign in to comment.