Major Controller refactor (#2295)
* Change Controller interface to mimic the CLI Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Remove obsolete handleCaptureTaken Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Extract system tray icon into separate class The implementation is not complete and full of bugs Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Remove Controller::handleCaptureFailed Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix a QObject connection Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Controller: remove unused includes Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Make check for updates work again Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Move functionality to daemon and tray icon Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Rename SystemTray to TrayIcon Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add missing trayicon.* files Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add missing QDesktopWidget Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Fix syntax errors Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Add missing include Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Include missing QOperatingSystemVersion on Mac Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Move update checking to daemon Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Remove obsolete method Controller::doLater Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Some cleanup Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Rename Controller to Flameshot Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com> * Final touches Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>
This commit is contained in:
183
src/widgets/trayicon.cpp
Normal file
183
src/widgets/trayicon.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
#include "trayicon.h"
|
||||
|
||||
#include "src/core/flameshot.h"
|
||||
#include "src/core/flameshotdaemon.h"
|
||||
#include "src/utils/globalvalues.h"
|
||||
|
||||
#include "src/utils/confighandler.h"
|
||||
#include <QApplication>
|
||||
#include <QMenu>
|
||||
#include <QTimer>
|
||||
#include <QUrl>
|
||||
#include <QVersionNumber>
|
||||
|
||||
#if defined(Q_OS_MACOS)
|
||||
#include <QOperatingSystemVersion>
|
||||
#endif
|
||||
|
||||
TrayIcon::TrayIcon(QObject* parent)
|
||||
: QSystemTrayIcon(parent)
|
||||
{
|
||||
initMenu();
|
||||
|
||||
setToolTip(QStringLiteral("Flameshot"));
|
||||
#if defined(Q_OS_MACOS)
|
||||
// Because of the following issues on MacOS "Catalina":
|
||||
// https://bugreports.qt.io/browse/QTBUG-86393
|
||||
// https://developer.apple.com/forums/thread/126072
|
||||
auto currentMacOsVersion = QOperatingSystemVersion::current();
|
||||
if (currentMacOsVersion >= currentMacOsVersion.MacOSBigSur) {
|
||||
setContextMenu(m_menu);
|
||||
}
|
||||
#else
|
||||
setContextMenu(m_menu);
|
||||
#endif
|
||||
QIcon icon =
|
||||
QIcon::fromTheme("flameshot-tray", QIcon(GlobalValues::iconPathPNG()));
|
||||
setIcon(icon);
|
||||
|
||||
#if defined(Q_OS_MACOS)
|
||||
if (currentMacOsVersion < currentMacOsVersion.MacOSBigSur) {
|
||||
// Because of the following issues on MacOS "Catalina":
|
||||
// https://bugreports.qt.io/browse/QTBUG-86393
|
||||
// https://developer.apple.com/forums/thread/126072
|
||||
auto trayIconActivated = [this](QSystemTrayIcon::ActivationReason r) {
|
||||
if (m_menu->isVisible()) {
|
||||
m_menu->hide();
|
||||
} else {
|
||||
m_menu->popup(QCursor::pos());
|
||||
}
|
||||
};
|
||||
connect(this, &QSystemTrayIcon::activated, this, trayIconActivated);
|
||||
}
|
||||
#else
|
||||
connect(this, &TrayIcon::activated, this, [this](ActivationReason r) {
|
||||
if (r == Trigger) {
|
||||
startGuiCapture();
|
||||
}
|
||||
});
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
// Ensure proper removal of tray icon when program quits on Windows.
|
||||
connect(qApp, &QCoreApplication::aboutToQuit, this, &TrayIcon::hide);
|
||||
#endif
|
||||
|
||||
show(); // TODO needed?
|
||||
|
||||
if (ConfigHandler().showStartupLaunchMessage()) {
|
||||
showMessage(
|
||||
"Flameshot",
|
||||
QObject::tr(
|
||||
"Hello, I'm here! Click icon in the tray to take a screenshot or "
|
||||
"click with a right button to see more options."),
|
||||
icon,
|
||||
3000);
|
||||
}
|
||||
|
||||
connect(ConfigHandler::getInstance(),
|
||||
&ConfigHandler::fileChanged,
|
||||
this,
|
||||
[this]() {});
|
||||
}
|
||||
|
||||
TrayIcon::~TrayIcon()
|
||||
{
|
||||
delete m_menu;
|
||||
}
|
||||
|
||||
QAction* TrayIcon::appUpdates()
|
||||
{
|
||||
return m_appUpdates;
|
||||
}
|
||||
|
||||
void TrayIcon::initMenu()
|
||||
{
|
||||
m_menu = new QMenu();
|
||||
|
||||
QAction* captureAction = new QAction(tr("&Take Screenshot"), this);
|
||||
connect(captureAction, &QAction::triggered, this, [this]() {
|
||||
#if defined(Q_OS_MACOS)
|
||||
auto currentMacOsVersion = QOperatingSystemVersion::current();
|
||||
if (currentMacOsVersion >= currentMacOsVersion.MacOSBigSur) {
|
||||
startGuiCapture();
|
||||
} else {
|
||||
// It seems it is not relevant for MacOS BigSur (Wait 400 ms to hide
|
||||
// the QMenu)
|
||||
QTimer::singleShot(400, this, [this]() { startGuiCapture(); });
|
||||
}
|
||||
#else
|
||||
// Wait 400 ms to hide the QMenu
|
||||
QTimer::singleShot(400, this, [this]() {
|
||||
startGuiCapture();
|
||||
});
|
||||
#endif
|
||||
});
|
||||
QAction* launcherAction = new QAction(tr("&Open Launcher"), this);
|
||||
connect(launcherAction,
|
||||
&QAction::triggered,
|
||||
Flameshot::instance(),
|
||||
&Flameshot::launcher);
|
||||
QAction* configAction = new QAction(tr("&Configuration"), this);
|
||||
connect(configAction,
|
||||
&QAction::triggered,
|
||||
Flameshot::instance(),
|
||||
&Flameshot::config);
|
||||
QAction* infoAction = new QAction(tr("&About"), this);
|
||||
connect(
|
||||
infoAction, &QAction::triggered, Flameshot::instance(), &Flameshot::info);
|
||||
|
||||
m_appUpdates = new QAction(tr("Check for updates"), this);
|
||||
connect(m_appUpdates,
|
||||
&QAction::triggered,
|
||||
FlameshotDaemon::instance(),
|
||||
&FlameshotDaemon::checkForUpdates);
|
||||
|
||||
connect(FlameshotDaemon::instance(),
|
||||
&FlameshotDaemon::newVersionAvailable,
|
||||
this,
|
||||
[this](QVersionNumber version) {
|
||||
QString newVersion =
|
||||
tr("New version %1 is available").arg(version.toString());
|
||||
m_appUpdates->setText(newVersion);
|
||||
});
|
||||
|
||||
QAction* quitAction = new QAction(tr("&Quit"), this);
|
||||
connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
|
||||
|
||||
// recent screenshots
|
||||
QAction* recentAction = new QAction(tr("&Latest Uploads"), this);
|
||||
connect(recentAction,
|
||||
&QAction::triggered,
|
||||
Flameshot::instance(),
|
||||
&Flameshot::history);
|
||||
|
||||
m_menu->addAction(captureAction);
|
||||
m_menu->addAction(launcherAction);
|
||||
m_menu->addSeparator();
|
||||
m_menu->addAction(recentAction);
|
||||
m_menu->addSeparator();
|
||||
m_menu->addAction(configAction);
|
||||
m_menu->addSeparator();
|
||||
m_menu->addAction(m_appUpdates);
|
||||
m_menu->addAction(infoAction);
|
||||
m_menu->addSeparator();
|
||||
m_menu->addAction(quitAction);
|
||||
}
|
||||
|
||||
void TrayIcon::enableCheckUpdatesAction(bool enable)
|
||||
{
|
||||
if (m_appUpdates != nullptr) {
|
||||
m_appUpdates->setVisible(enable);
|
||||
m_appUpdates->setEnabled(enable);
|
||||
}
|
||||
if (enable) {
|
||||
FlameshotDaemon::instance()->getLatestAvailableVersion();
|
||||
}
|
||||
}
|
||||
|
||||
void TrayIcon::startGuiCapture()
|
||||
{
|
||||
auto* widget = Flameshot::instance()->gui();
|
||||
FlameshotDaemon::instance()->showUpdateNotificationIfAvailable(widget);
|
||||
}
|
||||
Reference in New Issue
Block a user