Add SystemTrayIcon

This commit is contained in:
delia 2025-04-29 20:43:13 +02:00
parent 4f586cdc0c
commit 9f0b2f9a00
4 changed files with 51 additions and 6 deletions

View File

@ -64,9 +64,13 @@ void initLogging ()
int main (int argc, char* argv[]) {
initLogging ();
g_instanceManager = new SingleInstanceManager("linux-wallpaperengine");
if (argc <= 1) {
QApplication qapp(argc, argv);
globalApp = &qapp;
g_instanceManager = new SingleInstanceManager("linux-wallpaperengine");
if (!g_instanceManager->tryListen()) {
std::cout << "App is already running!!!!\n";
return 0;
@ -79,9 +83,6 @@ int main (int argc, char* argv[]) {
wallpaperPaths.push_back(entry.path());
}
QApplication qapp(argc, argv);
globalApp = &qapp;
// Signal for properly close the app
std::signal (SIGINT, signalhandler);
std::signal (SIGTERM, signalhandler);

View File

@ -19,6 +19,12 @@ void SingleInstanceManager::cleanUpServer() {
}
bool SingleInstanceManager::tryListen() {
QLocalSocket socket;
socket.connectToServer(m_serverName);
if (!socket.waitForConnected(100)) {
QLocalServer::removeServer(m_serverName);
} else return false;
if (!server->listen(m_serverName)) return false;
connect(server, &QLocalServer::newConnection, this, [this]() {

View File

@ -1,6 +1,7 @@
#include "UIWindow.h"
#include "Qt/SingleInstanceManager.h"
#include <QtConcurrent/qtconcurrentrun.h>
#include <cstddef>
#include <iostream>
#include <qapplication.h>
#include <qboxlayout.h>
@ -11,9 +12,11 @@
#include <qlayout.h>
#include <qlineedit.h>
#include <qlocalsocket.h>
#include <qmenu.h>
#include <qnamespace.h>
#include <qprocess.h>
#include <qpushbutton.h>
#include <qsystemtrayicon.h>
#include <qwidget.h>
#include <qwindowdefs.h>
#include <QByteArray>
@ -75,7 +78,6 @@ void UIWindow::setupUIWindow(std::vector<std::string> wallpaperPaths) {
QAbstractButton::connect(button, &QPushButton::clicked, [button, this]() {
QString clickedPath = button->property("path").toString();
std::cout << clickedPath.toStdString() << "\n";
button->setEnabled(false);
this->selectedWallpapers[this->screenSelector->currentText().toStdString()] = clickedPath.toStdString();
@ -103,7 +105,9 @@ void UIWindow::setupUIWindow(std::vector<std::string> wallpaperPaths) {
});
QObject::connect(instanceGuard, &SingleInstanceManager::messageReceived, [this](const QByteArray& msg) {
qDebug() << msg;
if (msg == "show") {
if (this->isHidden()) show();
}
});
container->setLayout(buttonLayout);
@ -148,6 +152,26 @@ void UIWindow::setupUIWindow(std::vector<std::string> wallpaperPaths) {
// update Buttons
updateSelectedButton();
// SYSTEM TRAY
auto* trayIcon = new QSystemTrayIcon(QIcon::fromTheme("folder"));
auto* trayMenu = new QMenu();
auto* showAction = new QAction("Show");
connect(showAction, &QAction::triggered, [this]() {
this->show();
});
auto* closeAction = new QAction("close");
connect(closeAction, &QAction::triggered, [this]() {
this->qapp->quit();
});
trayMenu->addActions({showAction, closeAction});
trayIcon->setContextMenu(trayMenu);
trayIcon->setToolTip("Linux-Wallpaperengine");
trayIcon->show();
}
void UIWindow::showEvent(QShowEvent* event) {
@ -156,6 +180,11 @@ void UIWindow::showEvent(QShowEvent* event) {
});
}
void UIWindow::closeEvent(QCloseEvent* event) {
this->hide();
event->ignore();
}
void UIWindow::startNewWallpaperEngine() {
if (wallpaperEngine->state() == QProcess::Running) {
// Stop WallpaperProcess
@ -169,6 +198,7 @@ void UIWindow::startNewWallpaperEngine() {
QStringList args;
for (auto wallpaper : this->selectedWallpapers) {
if (wallpaper.first == "" || wallpaper.second == "") continue;
args.push_back("--screen-root");
args.push_back(QString::fromStdString(wallpaper.first));
if (!extraFlags[wallpaper.first].empty()) {

View File

@ -20,6 +20,10 @@
#include <QLineEdit>
#include <QLabel>
#include <QMovie>
#include <QCloseEvent>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#include <QtConcurrent/QtConcurrent>
#include <string>
#include <vector>
@ -34,11 +38,14 @@ class UIWindow : public QWidget {
void setupUIWindow(std::vector<std::string> wallpaperPaths);
private:
// Components
QApplication* qapp;
SingleInstanceManager* instanceGuard;
QComboBox* screenSelector;
QLineEdit* extraFlagsInput;
QGridLayout* buttonLayout;
// Important Fields
std::map<std::string, std::string> selectedWallpapers;
std::map<std::string, std::vector<std::string>> extraFlags;
QProcess* wallpaperEngine;
@ -49,4 +56,5 @@ class UIWindow : public QWidget {
protected:
void showEvent(QShowEvent* event) override;
void closeEvent(QCloseEvent* event) override;
};