Skip to content

Commit

Permalink
Fix showing submenus for commands in tray menu
Browse files Browse the repository at this point in the history
Fixes #2730
  • Loading branch information
hluk committed Jul 12, 2024
1 parent 64d9802 commit cc91989
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,16 @@ QMenu *findSubMenu(const QString &name, const QMenu &menu)
return nullptr;
}

QMenu *createSubMenus(QString *name, QMenu *menu)
std::pair<QMenu*, QMenu*> 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);
Expand All @@ -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.
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -1592,14 +1596,18 @@ void MainWindow::addCommandsToTrayMenu(const QVariantMap &clipboardData, QList<Q

for (const auto &command : commands) {
QString name = command.name;
QMenu *currentMenu = createSubMenus(&name, m_trayMenu);
QMenu *rootMenu, *currentMenu;
std::tie(rootMenu, currentMenu) = createSubMenus(&name, m_trayMenu);
auto act = new CommandAction(command, name, currentMenu);

const QList<QKeySequence> 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);

Expand Down
35 changes: 35 additions & 0 deletions src/tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"(
Expand Down
2 changes: 2 additions & 0 deletions src/tests/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ private slots:
void automaticCommandStoreSpecialFormat();
void automaticCommandIgnoreSpecialFormat();

void globalCommandInMenu();

void scriptCommandLoaded();
void scriptCommandAddFunction();
void scriptCommandOverrideFunction();
Expand Down

0 comments on commit cc91989

Please sign in to comment.