From 8ed6ce8ec65afcfff235d10efc329bb21736e5fa Mon Sep 17 00:00:00 2001 From: outfoxxed Date: Thu, 23 Jan 2025 03:49:34 -0800 Subject: [PATCH] Add database selection to tray menu --- src/gui/MainWindow.cpp | 60 +++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 3a19f06683..ab94aef052 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -1500,28 +1500,55 @@ void MainWindow::updateTrayIcon() if (config()->get(Config::GUI_ShowTrayIcon).toBool()) { if (!m_trayIcon) { m_trayIcon = new QSystemTrayIcon(this); - auto* menu = new QMenu(this); - auto* actionToggle = new QAction(tr("Toggle window"), menu); - menu->addAction(actionToggle); - actionToggle->setIcon(icons()->icon("keepassxc-monochrome-dark")); + connect(m_trayIcon, + SIGNAL(activated(QSystemTrayIcon::ActivationReason)), + SLOT(trayIconTriggered(QSystemTrayIcon::ActivationReason))); + } - menu->addAction(m_ui->actionLockAllDatabases); + auto* menu = new QMenu(this); + + auto* actionToggle = new QAction(tr("Toggle window"), menu); + menu->addAction(actionToggle); + actionToggle->setIcon(icons()->icon("keepassxc-monochrome-dark")); + connect(actionToggle, SIGNAL(triggered()), SLOT(toggleWindow())); + + if (m_ui->tabWidget->count() > 1) { + menu->addSeparator(); + auto* databaseGroup = new QActionGroup(menu); + + for (auto i = 0; i != m_ui->tabWidget->count(); ++i) { + const auto& tabName = m_ui->tabWidget->tabName(i); + auto* action = new QAction(tabName, menu); + action->setCheckable(true); + action->setChecked(i == m_ui->tabWidget->currentIndex()); + action->setActionGroup(databaseGroup); + menu->addAction(action); + + connect(action, &QAction::triggered, this, [this, i]() { + // This has to be checked as this connection will exist until + // the next updateTrayIcon regardless of the tab count. + if (i > m_ui->tabWidget->count()) + return; + m_ui->tabWidget->setCurrentIndex(i); + }); + } + + menu->addSeparator(); + } + + menu->addAction(m_ui->actionLockAllDatabases); #ifdef Q_OS_MACOS - auto actionQuit = new QAction(tr("Quit KeePassXC"), menu); - connect(actionQuit, SIGNAL(triggered()), SLOT(appExit())); - menu->addAction(actionQuit); + auto actionQuit = new QAction(tr("Quit KeePassXC"), menu); + connect(actionQuit, SIGNAL(triggered()), SLOT(appExit())); + menu->addAction(actionQuit); #else - menu->addAction(m_ui->actionQuit); + menu->addAction(m_ui->actionQuit); #endif - m_trayIcon->setContextMenu(menu); - - connect(m_trayIcon, - SIGNAL(activated(QSystemTrayIcon::ActivationReason)), - SLOT(trayIconTriggered(QSystemTrayIcon::ActivationReason))); - connect(actionToggle, SIGNAL(triggered()), SLOT(toggleWindow())); - } + auto* oldContextMenu = m_trayIcon->contextMenu(); + m_trayIcon->setContextMenu(menu); + delete oldContextMenu; bool showUnlocked = m_ui->tabWidget->hasLockableDatabases(); m_trayIcon->setIcon(icons()->trayIcon(showUnlocked)); @@ -1540,6 +1567,7 @@ void MainWindow::updateTrayIcon() } else { if (m_trayIcon) { m_trayIcon->hide(); + delete m_trayIcon->contextMenu(); delete m_trayIcon; } }