replace trayicon activation with menu actions (#4632)

This commit is contained in:
ebbit1q 2023-08-06 23:55:02 +02:00 committed by GitHub
parent 176c52daf2
commit 244cb847fb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 19 deletions

View file

@ -62,6 +62,7 @@
#include <QSystemTrayIcon>
#include <QThread>
#include <QTimer>
#include <QWindow>
#include <QtConcurrent>
#include <QtNetwork>
@ -671,6 +672,9 @@ void MainWindow::retranslateUi()
aUpdate->setText(tr("Check for Client Updates"));
aCheckCardUpdates->setText(tr("Check for Card Updates..."));
aViewLog->setText(tr("View &Debug Log"));
aShow->setText(tr("Show/Hide"));
tabSupervisor->retranslateUi();
}
@ -721,6 +725,9 @@ void MainWindow::createActions()
aViewLog = new QAction(this);
connect(aViewLog, SIGNAL(triggered()), this, SLOT(actViewLog()));
aShow = new QAction(this);
connect(aShow, SIGNAL(triggered()), this, SLOT(actShow()));
#if defined(__APPLE__) /* For OSX */
aSettings->setMenuRole(QAction::PreferencesRole);
aExit->setMenuRole(QAction::QuitRole);
@ -846,7 +853,6 @@ MainWindow::MainWindow(QWidget *parent)
aFullScreen->setChecked(static_cast<bool>(windowState() & Qt::WindowFullScreen));
if (QSystemTrayIcon::isSystemTrayAvailable()) {
createTrayActions();
createTrayIcon();
}
@ -931,27 +937,32 @@ MainWindow::~MainWindow()
void MainWindow::createTrayIcon()
{
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(closeAction);
trayIconMenu->addAction(aShow);
trayIconMenu->addSeparator();
trayIconMenu->addAction(aSettings);
trayIconMenu->addAction(aCheckCardUpdates);
trayIconMenu->addAction(aAbout);
trayIconMenu->addSeparator();
trayIconMenu->addAction(aExit);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setIcon(QPixmap("theme:cockatrice"));
trayIcon->show();
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
void MainWindow::actShow()
{
if (reason == QSystemTrayIcon::DoubleClick) {
if ((windowState() & Qt::WindowMinimized) == 0) {
// wait 50 msec before actually checking the active window, this is because the trayicon menu will actually take
// focus and we have to wait for the focus to come back to the application
QTimer::singleShot(50, this, [this]() {
if (isActiveWindow()) {
showMinimized();
} else {
showNormal();
activateWindow();
}
}
});
}
void MainWindow::promptForgotPasswordChallenge()
@ -962,12 +973,6 @@ void MainWindow::promptForgotPasswordChallenge()
dlg.getPlayerName(), dlg.getEmail());
}
void MainWindow::createTrayActions()
{
closeAction = new QAction(tr("&Exit"), this);
connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
}
void MainWindow::closeEvent(QCloseEvent *event)
{
// workaround Qt bug where closeEvent gets called twice