@@ -1500,28 +1500,55 @@ void MainWindow::updateTrayIcon()
15001500 if (config ()->get (Config::GUI_ShowTrayIcon).toBool ()) {
15011501 if (!m_trayIcon) {
15021502 m_trayIcon = new QSystemTrayIcon (this );
1503- auto * menu = new QMenu (this );
15041503
1505- auto * actionToggle = new QAction (tr (" Toggle window" ), menu);
1506- menu->addAction (actionToggle);
1507- actionToggle->setIcon (icons ()->icon (" keepassxc-monochrome-dark" ));
1504+ connect (m_trayIcon,
1505+ SIGNAL (activated (QSystemTrayIcon::ActivationReason)),
1506+ SLOT (trayIconTriggered (QSystemTrayIcon::ActivationReason)));
1507+ }
15081508
1509- menu->addAction (m_ui->actionLockAllDatabases );
1509+ auto * menu = new QMenu (this );
1510+
1511+ auto * actionToggle = new QAction (tr (" Toggle window" ), menu);
1512+ menu->addAction (actionToggle);
1513+ actionToggle->setIcon (icons ()->icon (" keepassxc-monochrome-dark" ));
1514+ connect (actionToggle, SIGNAL (triggered ()), SLOT (toggleWindow ()));
1515+
1516+ if (m_ui->tabWidget ->count () > 1 ) {
1517+ menu->addSeparator ();
1518+ auto * databaseGroup = new QActionGroup (menu);
1519+
1520+ for (auto i = 0 ; i != m_ui->tabWidget ->count (); ++i) {
1521+ const auto & tabName = m_ui->tabWidget ->tabName (i);
1522+ auto * action = new QAction (tabName, menu);
1523+ action->setCheckable (true );
1524+ action->setChecked (i == m_ui->tabWidget ->currentIndex ());
1525+ action->setActionGroup (databaseGroup);
1526+ menu->addAction (action);
1527+
1528+ connect (action, &QAction::triggered, this , [this , i]() {
1529+ // This has to be checked as this connection will exist until
1530+ // the next updateTrayIcon regardless of the tab count.
1531+ if (i > m_ui->tabWidget ->count ())
1532+ return ;
1533+ m_ui->tabWidget ->setCurrentIndex (i);
1534+ });
1535+ }
1536+
1537+ menu->addSeparator ();
1538+ }
1539+
1540+ menu->addAction (m_ui->actionLockAllDatabases );
15101541
15111542#ifdef Q_OS_MACOS
1512- auto actionQuit = new QAction (tr (" Quit KeePassXC" ), menu);
1513- connect (actionQuit, SIGNAL (triggered ()), SLOT (appExit ()));
1514- menu->addAction (actionQuit);
1543+ auto actionQuit = new QAction (tr (" Quit KeePassXC" ), menu);
1544+ connect (actionQuit, SIGNAL (triggered ()), SLOT (appExit ()));
1545+ menu->addAction (actionQuit);
15151546#else
1516- menu->addAction (m_ui->actionQuit );
1547+ menu->addAction (m_ui->actionQuit );
15171548#endif
1518- m_trayIcon->setContextMenu (menu);
1519-
1520- connect (m_trayIcon,
1521- SIGNAL (activated (QSystemTrayIcon::ActivationReason)),
1522- SLOT (trayIconTriggered (QSystemTrayIcon::ActivationReason)));
1523- connect (actionToggle, SIGNAL (triggered ()), SLOT (toggleWindow ()));
1524- }
1549+ auto * oldContextMenu = m_trayIcon->contextMenu ();
1550+ m_trayIcon->setContextMenu (menu);
1551+ delete oldContextMenu;
15251552
15261553 bool showUnlocked = m_ui->tabWidget ->hasLockableDatabases ();
15271554 m_trayIcon->setIcon (icons ()->trayIcon (showUnlocked));
@@ -1540,6 +1567,7 @@ void MainWindow::updateTrayIcon()
15401567 } else {
15411568 if (m_trayIcon) {
15421569 m_trayIcon->hide ();
1570+ delete m_trayIcon->contextMenu ();
15431571 delete m_trayIcon;
15441572 }
15451573 }
0 commit comments