From d5e651bae93f8ff8adfcf6ef1da25ae2cf4a8f7b Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Tue, 7 Jan 2025 00:11:58 -0500 Subject: [PATCH] refactor: MainWindow use QSystemTrayIcon in place of TrayIcon removes the TrayIcon class --- src/apps/deskflow-gui/MainWindow.cpp | 19 ++++--- src/apps/deskflow-gui/MainWindow.h | 3 +- src/apps/deskflow-gui/TrayIcon.cpp | 77 ---------------------------- src/apps/deskflow-gui/TrayIcon.h | 51 ------------------ 4 files changed, 12 insertions(+), 138 deletions(-) delete mode 100644 src/apps/deskflow-gui/TrayIcon.cpp delete mode 100644 src/apps/deskflow-gui/TrayIcon.h diff --git a/src/apps/deskflow-gui/MainWindow.cpp b/src/apps/deskflow-gui/MainWindow.cpp index 1443acf3e..a55e21d76 100644 --- a/src/apps/deskflow-gui/MainWindow.cpp +++ b/src/apps/deskflow-gui/MainWindow.cpp @@ -83,6 +83,7 @@ MainWindow::MainWindow(ConfigScopes &configScopes, AppConfig &appConfig) m_ClientConnection(this, appConfig), m_TlsUtility(appConfig), m_WindowSaveTimer(this), + m_trayIcon{new QSystemTrayIcon(this)}, m_guiDupeChecker{new QLocalServer(this)}, m_actionAbout{new QAction(this)}, m_actionClearSettings{new QAction(tr("Clear settings"), this)}, @@ -293,7 +294,7 @@ void MainWindow::connectSlots() // Mac os tray will only show a menu #ifndef Q_OS_MAC - connect(&m_TrayIcon, &TrayIcon::activated, this, &MainWindow::onTrayIconActivated); + connect(m_trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::onTrayIconActivated); #endif connect( @@ -567,8 +568,9 @@ void MainWindow::moveEvent(QMoveEvent *event) void MainWindow::open() { - - QList trayActions{m_actionStartCore, m_actionStopCore, nullptr, m_actionQuit}; + auto trayMenu = new QMenu(this); + trayMenu->addActions({m_actionStartCore, m_actionStopCore, m_actionQuit}); + trayMenu->insertSeparator(m_actionQuit); #ifdef Q_OS_MAC // Duplicate quit needed for mac os tray menu @@ -577,12 +579,13 @@ void MainWindow::open() connect(actionTrayQuit, &QAction::triggered, this, &MainWindow::close); m_actionRestore->setText(tr("Open Deskflow")); - trayActions.insert(3, m_actionRestore); - trayActions.append(nullptr); - trayActions.append(actionTrayQuit); + trayMenu->addActions({m_actionRestore, actionTrayQuit}); + trayMenu->insertSeparator(actionTrayQuit); #endif - m_TrayIcon.create(trayActions); + m_trayIcon->setContextMenu(trayMenu); + // Show will show the tray when it can (if it can) + m_trayIcon->show(); if (!m_AppConfig.enableUpdateCheck().has_value()) { m_AppConfig.setEnableUpdateCheck(messages::showUpdateCheckOption(this)); @@ -701,7 +704,7 @@ void MainWindow::setIcon() icon.addFile(kIconFile); #endif - m_TrayIcon.setIcon(icon); + m_trayIcon->setIcon(icon); } void MainWindow::handleLogLine(const QString &line) diff --git a/src/apps/deskflow-gui/MainWindow.h b/src/apps/deskflow-gui/MainWindow.h index 2452eb3ff..174dc8ca3 100644 --- a/src/apps/deskflow-gui/MainWindow.h +++ b/src/apps/deskflow-gui/MainWindow.h @@ -27,7 +27,6 @@ #include #include "ServerConfig.h" -#include "TrayIcon.h" #include "VersionChecker.h" #include "common/ipc.h" #include "gui/config/AppConfig.h" @@ -193,7 +192,7 @@ private: void showAndActivate(); VersionChecker m_VersionChecker; - deskflow::gui::TrayIcon m_TrayIcon; + QSystemTrayIcon *m_trayIcon = nullptr; QAbstractButton *m_pCancelButton = nullptr; bool m_SecureSocket = false; bool m_SaveWindow = false; diff --git a/src/apps/deskflow-gui/TrayIcon.cpp b/src/apps/deskflow-gui/TrayIcon.cpp deleted file mode 100644 index ddd27b635..000000000 --- a/src/apps/deskflow-gui/TrayIcon.cpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * Copyright (C) 2021 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#include "TrayIcon.h" - -#include "common/constants.h" -#include "gui/Logger.h" - -namespace deskflow::gui { - -const auto kShowRetryInterval = 1000; - -void TrayIcon::setIcon(const QIcon &icon) -{ - m_icon = icon; - if (m_pTrayIcon && !icon.isNull()) { - m_pTrayIcon->setIcon(icon); - } -} - -void TrayIcon::showRetryLoop() -{ - // HACK: apparently this is needed to create a dbus connection, and the hide - // is needed to make use of the object so the dbus connection doesn't get - // optimized away by the compiler. - // TODO: we should verify that this hack actually works. - QSystemTrayIcon trayIcon; - trayIcon.hide(); - - if (QSystemTrayIcon::isSystemTrayAvailable()) { - m_pTrayIcon->show(); - } else { - // on some platforms, it's not always possible to create the tray when the - // app starts, so keep trying until it is possible. - logVerbose(QString("system tray not ready yet, retrying in %1 ms").arg(kShowRetryInterval)); - QTimer::singleShot(kShowRetryInterval, this, &TrayIcon::showRetryLoop); - } -} - -void TrayIcon::create(const QList &actions) -{ - m_pTrayIconMenu = std::make_unique(); - - for (auto action : actions) { - if (action) { - m_pTrayIconMenu->addAction(action); - } else { - m_pTrayIconMenu->addSeparator(); - } - } - - m_pTrayIcon = std::make_unique(); - setIcon(m_icon); - - connect(m_pTrayIcon.get(), &QSystemTrayIcon::activated, this, &TrayIcon::activated); - - m_pTrayIcon->setContextMenu(m_pTrayIconMenu.get()); - m_pTrayIcon->setToolTip(kAppName); - - showRetryLoop(); -} - -} // namespace deskflow::gui diff --git a/src/apps/deskflow-gui/TrayIcon.h b/src/apps/deskflow-gui/TrayIcon.h deleted file mode 100644 index c1fb4be8f..000000000 --- a/src/apps/deskflow-gui/TrayIcon.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Deskflow -- mouse and keyboard sharing utility - * Copyright (C) 2021 Symless Ltd. - * - * This package is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * found in the file LICENSE that should have accompanied this file. - * - * This package is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#pragma once - -#include -#include -#include -#include -#include -#include - -namespace deskflow::gui { - -class TrayIcon : public QObject -{ - Q_OBJECT -public: - explicit TrayIcon() = default; - - void create(const QList &actions); - void setIcon(const QIcon &icon); - -signals: - void activated(QSystemTrayIcon::ActivationReason reason); - -private: - void showRetryLoop(); - - std::unique_ptr m_pTrayIcon; - std::unique_ptr m_pTrayIconMenu; - std::function m_init; - std::function m_setIcon; - QIcon m_icon; -}; - -} // namespace deskflow::gui