mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-12-20 10:45:33 -05:00
Add an option to display a tray icon.
Also implement "Minimize to tray" functionality.
This commit is contained in:
parent
870d7355ca
commit
4cdb9a645d
6 changed files with 111 additions and 1 deletions
|
|
@ -33,6 +33,7 @@ const QString MainWindow::BaseWindowTitle = "KeePassX";
|
|||
|
||||
MainWindow::MainWindow()
|
||||
: m_ui(new Ui::MainWindow())
|
||||
, m_trayIcon(Q_NULLPTR)
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
|
||||
|
|
@ -201,6 +202,8 @@ MainWindow::MainWindow()
|
|||
|
||||
m_actionMultiplexer.connect(m_ui->actionSearch, SIGNAL(triggered()),
|
||||
SLOT(toggleSearch()));
|
||||
|
||||
updateTrayIcon();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
|
|
@ -429,12 +432,26 @@ void MainWindow::closeEvent(QCloseEvent* event)
|
|||
saveWindowInformation();
|
||||
|
||||
event->accept();
|
||||
QApplication::quit();
|
||||
}
|
||||
else {
|
||||
event->ignore();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::changeEvent(QEvent *event)
|
||||
{
|
||||
if ((event->type() == QEvent::WindowStateChange) && isMinimized()
|
||||
&& isTrayIconEnabled() && config()->get("GUI/MinimizeToTray").toBool())
|
||||
{
|
||||
event->ignore();
|
||||
hide();
|
||||
}
|
||||
else {
|
||||
QMainWindow::changeEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::saveWindowInformation()
|
||||
{
|
||||
config()->set("GUI/MainWindowGeometry", saveGeometry());
|
||||
|
|
@ -467,6 +484,35 @@ bool MainWindow::saveLastDatabases()
|
|||
return accept;
|
||||
}
|
||||
|
||||
void MainWindow::updateTrayIcon()
|
||||
{
|
||||
if (isTrayIconEnabled()) {
|
||||
if (!m_trayIcon) {
|
||||
m_trayIcon = new QSystemTrayIcon(filePath()->applicationIcon(), this);
|
||||
|
||||
QMenu* menu = new QMenu(this);
|
||||
|
||||
QAction* actionToggle = new QAction(tr("Toggle window"), menu);
|
||||
menu->addAction(actionToggle);
|
||||
|
||||
menu->addAction(m_ui->actionQuit);
|
||||
|
||||
connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
|
||||
SLOT(trayIconTriggered(QSystemTrayIcon::ActivationReason)));
|
||||
connect(actionToggle, SIGNAL(triggered()), SLOT(toggleWindow()));
|
||||
|
||||
m_trayIcon->setContextMenu(menu);
|
||||
m_trayIcon->show();
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (m_trayIcon) {
|
||||
delete m_trayIcon;
|
||||
m_trayIcon = Q_NULLPTR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::showEntryContextMenu(const QPoint& globalPos)
|
||||
{
|
||||
m_ui->menuEntries->popup(globalPos);
|
||||
|
|
@ -511,4 +557,31 @@ void MainWindow::applySettingsChanges()
|
|||
else {
|
||||
m_inactivityTimer->deactivate();
|
||||
}
|
||||
|
||||
updateTrayIcon();
|
||||
}
|
||||
|
||||
void MainWindow::trayIconTriggered(QSystemTrayIcon::ActivationReason reason)
|
||||
{
|
||||
if (reason == QSystemTrayIcon::Trigger) {
|
||||
toggleWindow();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::toggleWindow()
|
||||
{
|
||||
if (QApplication::activeWindow() == this) {
|
||||
hide();
|
||||
}
|
||||
else {
|
||||
show();
|
||||
raise();
|
||||
activateWindow();
|
||||
}
|
||||
}
|
||||
|
||||
bool MainWindow::isTrayIconEnabled() const
|
||||
{
|
||||
return config()->get("GUI/ShowTrayIcon").toBool()
|
||||
&& QSystemTrayIcon::isSystemTrayAvailable();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue