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

View file

@ -84,7 +84,7 @@ private slots:
void forgotPasswordSuccess(); void forgotPasswordSuccess();
void forgotPasswordError(); void forgotPasswordError();
void promptForgotPasswordReset(); void promptForgotPasswordReset();
void iconActivated(QSystemTrayIcon::ActivationReason reason); void actShow();
void promptForgotPasswordChallenge(); void promptForgotPasswordChallenge();
void showWindowIfHidden(); void showWindowIfHidden();
@ -114,7 +114,6 @@ private:
void createMenus(); void createMenus();
void createTrayIcon(); void createTrayIcon();
void createTrayActions();
int getNextCustomSetPrefix(QDir dataDir); int getNextCustomSetPrefix(QDir dataDir);
inline QString getCardUpdaterBinaryName() inline QString getCardUpdaterBinaryName()
{ {
@ -125,8 +124,8 @@ private:
QList<QMenu *> tabMenus; QList<QMenu *> tabMenus;
QMenu *cockatriceMenu, *dbMenu, *helpMenu, *trayIconMenu; QMenu *cockatriceMenu, *dbMenu, *helpMenu, *trayIconMenu;
QAction *aConnect, *aDisconnect, *aSinglePlayer, *aWatchReplay, *aDeckEditor, *aFullScreen, *aSettings, *aExit, QAction *aConnect, *aDisconnect, *aSinglePlayer, *aWatchReplay, *aDeckEditor, *aFullScreen, *aSettings, *aExit,
*aAbout, *aTips, *aCheckCardUpdates, *aRegister, *aForgotPassword, *aUpdate, *aViewLog, *closeAction; *aAbout, *aTips, *aCheckCardUpdates, *aRegister, *aForgotPassword, *aUpdate, *aViewLog, *aManageSets,
QAction *aManageSets, *aEditTokens, *aOpenCustomFolder, *aOpenCustomsetsFolder, *aAddCustomSet; *aEditTokens, *aOpenCustomFolder, *aOpenCustomsetsFolder, *aAddCustomSet, *aShow;
TabSupervisor *tabSupervisor; TabSupervisor *tabSupervisor;
WndSets *wndSets; WndSets *wndSets;
RemoteClient *client; RemoteClient *client;