@@ -1500,28 +1500,56 @@ 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+
1534+ m_ui->tabWidget ->setCurrentIndex (i);
1535+ });
1536+ }
1537+
1538+ menu->addSeparator ();
1539+ }
1540+
1541+ menu->addAction (m_ui->actionLockAllDatabases );
15101542
15111543#ifdef Q_OS_MACOS
1512- auto actionQuit = new QAction (tr (" Quit KeePassXC" ), menu);
1513- connect (actionQuit, SIGNAL (triggered ()), SLOT (appExit ()));
1514- menu->addAction (actionQuit);
1544+ auto actionQuit = new QAction (tr (" Quit KeePassXC" ), menu);
1545+ connect (actionQuit, SIGNAL (triggered ()), SLOT (appExit ()));
1546+ menu->addAction (actionQuit);
15151547#else
1516- menu->addAction (m_ui->actionQuit );
1548+ menu->addAction (m_ui->actionQuit );
15171549#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- }
1550+ auto * oldContextMenu = m_trayIcon->contextMenu ();
1551+ m_trayIcon->setContextMenu (menu);
1552+ delete oldContextMenu;
15251553
15261554 bool showUnlocked = m_ui->tabWidget ->hasLockableDatabases ();
15271555 m_trayIcon->setIcon (icons ()->trayIcon (showUnlocked));
@@ -1540,6 +1568,7 @@ void MainWindow::updateTrayIcon()
15401568 } else {
15411569 if (m_trayIcon) {
15421570 m_trayIcon->hide ();
1571+ delete m_trayIcon->contextMenu ();
15431572 delete m_trayIcon;
15441573 }
15451574 }
0 commit comments