From 244cb847fbd5b79eeb3457f9fbfbfaa4b1c043d6 Mon Sep 17 00:00:00 2001 From: ebbit1q Date: Sun, 6 Aug 2023 23:55:02 +0200 Subject: [PATCH] replace trayicon activation with menu actions (#4632) --- cockatrice/src/window_main.cpp | 35 +++++++++++++++++++--------------- cockatrice/src/window_main.h | 7 +++---- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index 55cb330e..8c7a51b1 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -62,6 +62,7 @@ #include #include #include +#include #include #include @@ -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(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 diff --git a/cockatrice/src/window_main.h b/cockatrice/src/window_main.h index c17ef498..bc5acb2e 100644 --- a/cockatrice/src/window_main.h +++ b/cockatrice/src/window_main.h @@ -84,7 +84,7 @@ private slots: void forgotPasswordSuccess(); void forgotPasswordError(); void promptForgotPasswordReset(); - void iconActivated(QSystemTrayIcon::ActivationReason reason); + void actShow(); void promptForgotPasswordChallenge(); void showWindowIfHidden(); @@ -114,7 +114,6 @@ private: void createMenus(); void createTrayIcon(); - void createTrayActions(); int getNextCustomSetPrefix(QDir dataDir); inline QString getCardUpdaterBinaryName() { @@ -125,8 +124,8 @@ private: QList tabMenus; QMenu *cockatriceMenu, *dbMenu, *helpMenu, *trayIconMenu; QAction *aConnect, *aDisconnect, *aSinglePlayer, *aWatchReplay, *aDeckEditor, *aFullScreen, *aSettings, *aExit, - *aAbout, *aTips, *aCheckCardUpdates, *aRegister, *aForgotPassword, *aUpdate, *aViewLog, *closeAction; - QAction *aManageSets, *aEditTokens, *aOpenCustomFolder, *aOpenCustomsetsFolder, *aAddCustomSet; + *aAbout, *aTips, *aCheckCardUpdates, *aRegister, *aForgotPassword, *aUpdate, *aViewLog, *aManageSets, + *aEditTokens, *aOpenCustomFolder, *aOpenCustomsetsFolder, *aAddCustomSet, *aShow; TabSupervisor *tabSupervisor; WndSets *wndSets; RemoteClient *client;