diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 1ad8e983b4..795cc342b2 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -192,15 +192,16 @@ QMenu *findSubMenu(const QString &name, const QMenu &menu) return nullptr; } -QMenu *createSubMenus(QString *name, QMenu *menu) +std::pair createSubMenus(QString *name, QMenu *menu) { QStringList path = name->split('|'); if (path.size() == 1) - return menu; + return {nullptr, menu}; *name = path.takeLast(); QMenu *parentMenu = menu; + QMenu *rootMenu = nullptr; for (const auto &subMenuName : path) { QMenu *subMenu = findSubMenu(subMenuName, *parentMenu); @@ -210,10 +211,12 @@ QMenu *createSubMenus(QString *name, QMenu *menu) parentMenu->addMenu(subMenu); } + if (parentMenu == menu) + rootMenu = subMenu; parentMenu = subMenu; } - return parentMenu; + return {rootMenu, parentMenu}; } // WORKAROUND: setWindowFlags() hides the window. @@ -1558,7 +1561,8 @@ void MainWindow::addCommandsToItemMenu(ClipboardBrowser *c) for (const auto &command : commands) { QString name = command.name; - QMenu *currentMenu = createSubMenus(&name, m_menuItem); + QMenu *_rootMenu, *currentMenu; + std::tie(_rootMenu, currentMenu) = createSubMenus(&name, m_menuItem); auto act = new CommandAction(command, name, currentMenu); c->addAction(act); @@ -1592,14 +1596,18 @@ void MainWindow::addCommandsToTrayMenu(const QVariantMap &clipboardData, QList uniqueShortcuts = getUniqueShortcuts( command.globalShortcuts, &usedShortcuts); act->setShortcuts(uniqueShortcuts); - actions->append(act); + if (rootMenu) + actions->append(rootMenu->menuAction()); + else + actions->append(act); addMenuMatchCommand(&m_trayMenuMatchCommands, command.matchCmd, act); diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index b50f5c7b11..a3dc6f923e 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -3987,6 +3987,41 @@ void Tests::automaticCommandIgnoreSpecialFormat() WAIT_ON_OUTPUT("separator" << "," << "read" << "0" << "1" << "2" << "3", "SHOULD NOT BE IGNORED,CMD2,CMD1,"); } +void Tests::globalCommandInMenu() +{ + const auto script = R"( + setCommands([ + { isGlobalShortcut: true, name: 'test', cmd: 'copyq add test' }, + ]) + )"; + RUN(script, ""); + WAIT_ON_OUTPUT("commands().length", "1\n"); + RUN("menu", ""); + RUN("keys" << trayMenuId << "DOWN" << "ENTER", ""); + RUN("keys" << clipboardBrowserId, ""); + WAIT_ON_OUTPUT("read(0)", "test"); + + RUN("setCommands([])", ""); + WAIT_ON_OUTPUT("commands().length", "0\n"); + + // Test sub-menus + const auto script2 = R"( + setCommands([ + { isGlobalShortcut: true, name: 'test|test1|test2', cmd: 'copyq add test2' }, + ]) + )"; + RUN(script2, ""); + WAIT_ON_OUTPUT("commands().length", "1\n"); + RUN("menu", ""); + RUN("keys" << trayMenuId << "DOWN" << "DOWN" << "ENTER", ""); + waitFor(100); + RUN("keys" << trayMenuId << "ENTER", ""); + waitFor(100); + RUN("keys" << trayMenuId << "ENTER", ""); + RUN("keys" << clipboardBrowserId, ""); + WAIT_ON_OUTPUT("read(0)", "test2"); +} + void Tests::scriptCommandLoaded() { const auto script = R"( diff --git a/src/tests/tests.h b/src/tests/tests.h index acbbc52b38..040aaf87aa 100644 --- a/src/tests/tests.h +++ b/src/tests/tests.h @@ -247,6 +247,8 @@ private slots: void automaticCommandStoreSpecialFormat(); void automaticCommandIgnoreSpecialFormat(); + void globalCommandInMenu(); + void scriptCommandLoaded(); void scriptCommandAddFunction(); void scriptCommandOverrideFunction();