refactor: MainWindow use QSystemTrayIcon in place of TrayIcon
removes the TrayIcon class
This commit is contained in:
parent
1a35a8bdf7
commit
d5e651bae9
4 changed files with 12 additions and 138 deletions
|
|
@ -83,6 +83,7 @@ MainWindow::MainWindow(ConfigScopes &configScopes, AppConfig &appConfig)
|
||||||
m_ClientConnection(this, appConfig),
|
m_ClientConnection(this, appConfig),
|
||||||
m_TlsUtility(appConfig),
|
m_TlsUtility(appConfig),
|
||||||
m_WindowSaveTimer(this),
|
m_WindowSaveTimer(this),
|
||||||
|
m_trayIcon{new QSystemTrayIcon(this)},
|
||||||
m_guiDupeChecker{new QLocalServer(this)},
|
m_guiDupeChecker{new QLocalServer(this)},
|
||||||
m_actionAbout{new QAction(this)},
|
m_actionAbout{new QAction(this)},
|
||||||
m_actionClearSettings{new QAction(tr("Clear settings"), this)},
|
m_actionClearSettings{new QAction(tr("Clear settings"), this)},
|
||||||
|
|
@ -293,7 +294,7 @@ void MainWindow::connectSlots()
|
||||||
|
|
||||||
// Mac os tray will only show a menu
|
// Mac os tray will only show a menu
|
||||||
#ifndef Q_OS_MAC
|
#ifndef Q_OS_MAC
|
||||||
connect(&m_TrayIcon, &TrayIcon::activated, this, &MainWindow::onTrayIconActivated);
|
connect(m_trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::onTrayIconActivated);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
connect(
|
connect(
|
||||||
|
|
@ -567,8 +568,9 @@ void MainWindow::moveEvent(QMoveEvent *event)
|
||||||
|
|
||||||
void MainWindow::open()
|
void MainWindow::open()
|
||||||
{
|
{
|
||||||
|
auto trayMenu = new QMenu(this);
|
||||||
QList<QAction *> trayActions{m_actionStartCore, m_actionStopCore, nullptr, m_actionQuit};
|
trayMenu->addActions({m_actionStartCore, m_actionStopCore, m_actionQuit});
|
||||||
|
trayMenu->insertSeparator(m_actionQuit);
|
||||||
|
|
||||||
#ifdef Q_OS_MAC
|
#ifdef Q_OS_MAC
|
||||||
// Duplicate quit needed for mac os tray menu
|
// Duplicate quit needed for mac os tray menu
|
||||||
|
|
@ -577,12 +579,13 @@ void MainWindow::open()
|
||||||
connect(actionTrayQuit, &QAction::triggered, this, &MainWindow::close);
|
connect(actionTrayQuit, &QAction::triggered, this, &MainWindow::close);
|
||||||
|
|
||||||
m_actionRestore->setText(tr("Open Deskflow"));
|
m_actionRestore->setText(tr("Open Deskflow"));
|
||||||
trayActions.insert(3, m_actionRestore);
|
trayMenu->addActions({m_actionRestore, actionTrayQuit});
|
||||||
trayActions.append(nullptr);
|
trayMenu->insertSeparator(actionTrayQuit);
|
||||||
trayActions.append(actionTrayQuit);
|
|
||||||
#endif
|
#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()) {
|
if (!m_AppConfig.enableUpdateCheck().has_value()) {
|
||||||
m_AppConfig.setEnableUpdateCheck(messages::showUpdateCheckOption(this));
|
m_AppConfig.setEnableUpdateCheck(messages::showUpdateCheckOption(this));
|
||||||
|
|
@ -701,7 +704,7 @@ void MainWindow::setIcon()
|
||||||
icon.addFile(kIconFile);
|
icon.addFile(kIconFile);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
m_TrayIcon.setIcon(icon);
|
m_trayIcon->setIcon(icon);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::handleLogLine(const QString &line)
|
void MainWindow::handleLogLine(const QString &line)
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,6 @@
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
|
||||||
#include "ServerConfig.h"
|
#include "ServerConfig.h"
|
||||||
#include "TrayIcon.h"
|
|
||||||
#include "VersionChecker.h"
|
#include "VersionChecker.h"
|
||||||
#include "common/ipc.h"
|
#include "common/ipc.h"
|
||||||
#include "gui/config/AppConfig.h"
|
#include "gui/config/AppConfig.h"
|
||||||
|
|
@ -193,7 +192,7 @@ private:
|
||||||
void showAndActivate();
|
void showAndActivate();
|
||||||
|
|
||||||
VersionChecker m_VersionChecker;
|
VersionChecker m_VersionChecker;
|
||||||
deskflow::gui::TrayIcon m_TrayIcon;
|
QSystemTrayIcon *m_trayIcon = nullptr;
|
||||||
QAbstractButton *m_pCancelButton = nullptr;
|
QAbstractButton *m_pCancelButton = nullptr;
|
||||||
bool m_SecureSocket = false;
|
bool m_SecureSocket = false;
|
||||||
bool m_SaveWindow = false;
|
bool m_SaveWindow = false;
|
||||||
|
|
|
||||||
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#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<QAction *> &actions)
|
|
||||||
{
|
|
||||||
m_pTrayIconMenu = std::make_unique<QMenu>();
|
|
||||||
|
|
||||||
for (auto action : actions) {
|
|
||||||
if (action) {
|
|
||||||
m_pTrayIconMenu->addAction(action);
|
|
||||||
} else {
|
|
||||||
m_pTrayIconMenu->addSeparator();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
m_pTrayIcon = std::make_unique<QSystemTrayIcon>();
|
|
||||||
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
|
|
||||||
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <QMenu>
|
|
||||||
#include <QObject>
|
|
||||||
#include <QSystemTrayIcon>
|
|
||||||
#include <QTimer>
|
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
|
||||||
|
|
||||||
namespace deskflow::gui {
|
|
||||||
|
|
||||||
class TrayIcon : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
public:
|
|
||||||
explicit TrayIcon() = default;
|
|
||||||
|
|
||||||
void create(const QList<QAction *> &actions);
|
|
||||||
void setIcon(const QIcon &icon);
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void activated(QSystemTrayIcon::ActivationReason reason);
|
|
||||||
|
|
||||||
private:
|
|
||||||
void showRetryLoop();
|
|
||||||
|
|
||||||
std::unique_ptr<QSystemTrayIcon> m_pTrayIcon;
|
|
||||||
std::unique_ptr<QMenu> m_pTrayIconMenu;
|
|
||||||
std::function<void()> m_init;
|
|
||||||
std::function<void(const QIcon &icon)> m_setIcon;
|
|
||||||
QIcon m_icon;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace deskflow::gui
|
|
||||||
Loading…
Reference in a new issue