From 18e203348b088ce06c3ca02c5433e25847fafe29 Mon Sep 17 00:00:00 2001 From: David Vaquero Date: Thu, 5 Nov 2026 12:36:59 +0100 Subject: [PATCH 1/2] feat: add system tray support --- app/CMakeLists.txt | 5 ++-- app/qml/Main.qml | 6 ++++ app/src/appcontroller.cpp | 61 +++++++++++++++++++++++++++++++++++++++ app/src/appcontroller.h | 27 +++++++++++++++++ app/src/main.cpp | 13 +++++++-- 5 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 app/src/appcontroller.cpp create mode 100644 app/src/appcontroller.h diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 8797488..aa9b5ca 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -1,8 +1,9 @@ -find_package(Qt6 6.5 REQUIRED COMPONENTS Core Gui Qml Quick QuickControls2 DBus Svg) +find_package(Qt6 6.5 REQUIRED COMPONENTS Core Gui Widgets Qml Quick QuickControls2 DBus Svg) qt_standard_project_setup(REQUIRES 6.5) qt_add_executable(mouse-master src/main.cpp + src/appcontroller.cpp src/appcontroller.h src/daemonclient.cpp src/daemonclient.h src/system.cpp src/system.h src/apps.cpp src/apps.h @@ -43,6 +44,6 @@ qt_add_resources(mouse-master "rules" ) target_link_libraries(mouse-master PRIVATE - Qt6::Core Qt6::Gui Qt6::Qml Qt6::Quick Qt6::QuickControls2 Qt6::DBus Qt6::Svg) + Qt6::Core Qt6::Gui Qt6::Widgets Qt6::Qml Qt6::Quick Qt6::QuickControls2 Qt6::DBus Qt6::Svg) install(TARGETS mouse-master RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) diff --git a/app/qml/Main.qml b/app/qml/Main.qml index 45c610e..a5b4b4f 100644 --- a/app/qml/Main.qml +++ b/app/qml/Main.qml @@ -12,6 +12,12 @@ C.ApplicationWindow { minimumHeight: 640 visible: true title: "Mouse Master" + onClosing: function (close) { + if (AppController.hideOnClose()) { + close.accepted = false + window.hide() + } + } color: Theme.window Settings { diff --git a/app/src/appcontroller.cpp b/app/src/appcontroller.cpp new file mode 100644 index 0000000..8391c51 --- /dev/null +++ b/app/src/appcontroller.cpp @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +#include "appcontroller.h" + +#include +#include +#include +#include + +namespace { +const QString AppId = QStringLiteral("io.github.burakgon.MouseMaster"); +const QString AppName = QStringLiteral("Mouse Master"); +} + +AppController::AppController(QObject *parent) + : QObject(parent) +{ + m_trayAvailable = QSystemTrayIcon::isSystemTrayAvailable(); + m_tray.setIcon(QIcon::fromTheme(AppId, QIcon(QStringLiteral(":/icons/app/io.github.burakgon.MouseMaster.svg")))); + m_tray.setToolTip(AppName); + + auto *open = m_menu.addAction(QStringLiteral("Open Mouse Master")); + connect(open, &QAction::triggered, this, &AppController::activate); + m_menu.addSeparator(); + auto *quitAction = m_menu.addAction(QStringLiteral("Quit")); + connect(quitAction, &QAction::triggered, this, &AppController::quit); + m_tray.setContextMenu(&m_menu); + connect(&m_tray, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) { + if (reason == QSystemTrayIcon::Trigger || reason == QSystemTrayIcon::DoubleClick) { + activate(); + } + }); + + if (m_trayAvailable) { + m_tray.show(); + } +} + +void AppController::setWindow(QQuickWindow *window) +{ + m_window = window; +} + +bool AppController::hideOnClose() const +{ + return m_trayAvailable; +} + +void AppController::activate() +{ + if (!m_window) { + return; + } + m_window->show(); + m_window->raise(); + m_window->requestActivate(); +} + +void AppController::quit() +{ + QCoreApplication::quit(); +} diff --git a/app/src/appcontroller.h b/app/src/appcontroller.h new file mode 100644 index 0000000..b2a9c26 --- /dev/null +++ b/app/src/appcontroller.h @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +#pragma once + +#include +#include +#include + +class QQuickWindow; + +class AppController : public QObject +{ + Q_OBJECT +public: + explicit AppController(QObject *parent = nullptr); + + void setWindow(QQuickWindow *window); + + Q_INVOKABLE bool hideOnClose() const; + Q_INVOKABLE void activate(); + Q_INVOKABLE void quit(); + +private: + QMenu m_menu; + QSystemTrayIcon m_tray; + QQuickWindow *m_window = nullptr; + bool m_trayAvailable = false; +}; diff --git a/app/src/main.cpp b/app/src/main.cpp index 1d567f8..2e8dffc 100644 --- a/app/src/main.cpp +++ b/app/src/main.cpp @@ -2,10 +2,11 @@ #include #include #include -#include +#include #include #include #include +#include #include #include #include @@ -13,6 +14,7 @@ #include #include "iconprovider.h" +#include "appcontroller.h" namespace { const QString AppId = QStringLiteral("io.github.burakgon.MouseMaster"); @@ -44,13 +46,14 @@ public Q_SLOTS: int main(int argc, char *argv[]) { - QGuiApplication app(argc, argv); + QApplication app(argc, argv); QGuiApplication::setApplicationName(QStringLiteral("mouse-master")); QGuiApplication::setApplicationDisplayName(QStringLiteral("Mouse Master")); QGuiApplication::setApplicationVersion(QStringLiteral(MOUSE_MASTER_VERSION)); QGuiApplication::setOrganizationName(QStringLiteral("mouse-master")); QGuiApplication::setDesktopFileName(AppId); QGuiApplication::setWindowIcon(QIcon::fromTheme(AppId, QIcon(QStringLiteral(":/icons/app/io.github.burakgon.MouseMaster.svg")))); + AppController appController; auto bus = QDBusConnection::sessionBus(); SingleInstance instance; @@ -67,11 +70,17 @@ int main(int argc, char *argv[]) QQuickStyle::setStyle(QStringLiteral("Basic")); QQmlApplicationEngine engine; + engine.rootContext()->setContextProperty(QStringLiteral("AppController"), &appController); instance.engine = &engine; engine.addImageProvider(QStringLiteral("lucide"), new LucideIconProvider); engine.addImageProvider(QStringLiteral("theme"), new ThemeIconProvider); QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed, &app, [] { QCoreApplication::exit(1); }, Qt::QueuedConnection); engine.loadFromModule("MouseMaster", "Main"); + if (!engine.rootObjects().isEmpty()) { + if (auto window = qobject_cast(engine.rootObjects().constFirst())) { + appController.setWindow(window); + } + } // Development aid: MM_SCREENSHOT=/path.png renders the window to a file and quits // (MM_SCREENSHOT_DELAY in milliseconds, default 1500). Used for docs and design reviews. From ed779d5858cb8d9d502327142abb4bbdaf0cc855 Mon Sep 17 00:00:00 2001 From: David Vaquero Date: Thu, 5 Nov 2026 13:31:55 +0100 Subject: [PATCH 2/2] Add autostart and minimized startup options --- app/qml/Main.qml | 2 +- app/qml/components/ScrollPage.qml | 7 +-- app/qml/pages/SettingsPage.qml | 30 +++++++++++ app/src/appcontroller.cpp | 90 ++++++++++++++++++++++++++++++- app/src/appcontroller.h | 22 +++++++- app/src/main.cpp | 13 ++++- docs/configuration.md | 7 +++ 7 files changed, 160 insertions(+), 11 deletions(-) diff --git a/app/qml/Main.qml b/app/qml/Main.qml index a5b4b4f..c80d454 100644 --- a/app/qml/Main.qml +++ b/app/qml/Main.qml @@ -10,7 +10,7 @@ C.ApplicationWindow { height: 820 minimumWidth: 940 minimumHeight: 640 - visible: true + visible: false title: "Mouse Master" onClosing: function (close) { if (AppController.hideOnClose()) { diff --git a/app/qml/components/ScrollPage.qml b/app/qml/components/ScrollPage.qml index 949f1c6..b38a0e2 100644 --- a/app/qml/components/ScrollPage.qml +++ b/app/qml/components/ScrollPage.qml @@ -9,6 +9,7 @@ C.ScrollView { property int maxContentWidth: 820 property int margins: 32 contentWidth: availableWidth + contentHeight: column.implicitHeight + root.margins * 2 clip: true C.ScrollBar.vertical.policy: C.ScrollBar.AsNeeded @@ -20,10 +21,4 @@ C.ScrollView { y: root.margins spacing: 20 } - Item { - // bottom padding - y: column.y + column.height - width: 1 - height: root.margins - } } diff --git a/app/qml/pages/SettingsPage.qml b/app/qml/pages/SettingsPage.qml index 939f446..c7dc193 100644 --- a/app/qml/pages/SettingsPage.qml +++ b/app/qml/pages/SettingsPage.qml @@ -42,6 +42,36 @@ ScrollPage { } } + Card { + Layout.fillWidth: true + ColumnLayout { + width: parent.width + spacing: 4 + SectionTitle { text: "Application" } + Item { Layout.preferredHeight: 6 } + SettingRow { + iconName: "monitor" + title: "Start Mouse Master at login" + description: "Open the app automatically when your desktop session starts." + Toggle { + checked: AppController.startOnLogin + onToggled: if (!AppController.setStartOnLogin(checked)) Notify.show("Could not update the login startup entry", true) + } + } + SettingRow { + iconName: "minimize-2" + title: "Start minimized" + description: "At login, keep the window hidden in the system tray." + divider: false + Toggle { + enabled: AppController.trayAvailable && AppController.startOnLogin + checked: AppController.startMinimized + onToggled: if (!AppController.setStartMinimized(checked)) Notify.show("Could not save the startup preference", true) + } + } + } + } + Card { Layout.fillWidth: true enabled: Daemon.available diff --git a/app/src/appcontroller.cpp b/app/src/appcontroller.cpp index 8391c51..5f95f97 100644 --- a/app/src/appcontroller.cpp +++ b/app/src/appcontroller.cpp @@ -3,17 +3,29 @@ #include #include +#include +#include +#include #include #include +#include +#include +#include namespace { const QString AppId = QStringLiteral("io.github.burakgon.MouseMaster"); const QString AppName = QStringLiteral("Mouse Master"); } -AppController::AppController(QObject *parent) +AppController::AppController(bool startMinimizedRequested, QObject *parent) : QObject(parent) + , m_startMinimizedRequested(startMinimizedRequested) { + QSettings settings; + settings.beginGroup(QStringLiteral("ui")); + m_startOnLogin = settings.value(QStringLiteral("startOnLogin"), false).toBool(); + m_startMinimized = settings.value(QStringLiteral("startMinimized"), false).toBool(); + m_trayAvailable = QSystemTrayIcon::isSystemTrayAvailable(); m_tray.setIcon(QIcon::fromTheme(AppId, QIcon(QStringLiteral(":/icons/app/io.github.burakgon.MouseMaster.svg")))); m_tray.setToolTip(AppName); @@ -42,7 +54,7 @@ void AppController::setWindow(QQuickWindow *window) bool AppController::hideOnClose() const { - return m_trayAvailable; + return m_trayAvailable && !m_quitting; } void AppController::activate() @@ -57,5 +69,79 @@ void AppController::activate() void AppController::quit() { + m_quitting = true; QCoreApplication::quit(); } + +bool AppController::writeAutostartEntry(bool enabled, bool startMinimized) const +{ + const QString path = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + + QStringLiteral("/autostart/") + AppId + QStringLiteral(".desktop"); + if (!enabled) { + return !QFile::exists(path) || QFile::remove(path); + } + + const QFileInfo info(path); + if (!QDir().mkpath(info.absolutePath())) { + return false; + } + QSaveFile file(path); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { + return false; + } + const QByteArray contents = QStringLiteral( + "[Desktop Entry]\n" + "Type=Application\n" + "Name=Mouse Master\n" + "Comment=Configure Logitech mice\n" + "Exec=mouse-master%1\n" + "Icon=io.github.burakgon.MouseMaster\n" + "Terminal=false\n" + "X-GNOME-Autostart-enabled=true\n") + .arg(startMinimized ? QStringLiteral(" --start-minimized") : QString()) + .toUtf8(); + if (file.write(contents) != contents.size()) { + return false; + } + return file.commit(); +} + +void AppController::persistPreferences() const +{ + QSettings settings; + settings.beginGroup(QStringLiteral("ui")); + settings.setValue(QStringLiteral("startOnLogin"), m_startOnLogin); + settings.setValue(QStringLiteral("startMinimized"), m_startMinimized); + settings.endGroup(); + settings.sync(); +} + +bool AppController::setStartOnLogin(bool enabled) +{ + if (m_startOnLogin == enabled) { + return true; + } + if (!writeAutostartEntry(enabled, m_startMinimized)) { + Q_EMIT preferencesChanged(); + return false; + } + m_startOnLogin = enabled; + persistPreferences(); + Q_EMIT preferencesChanged(); + return true; +} + +bool AppController::setStartMinimized(bool enabled) +{ + if (m_startMinimized == enabled) { + return true; + } + if (m_startOnLogin && !writeAutostartEntry(true, enabled)) { + Q_EMIT preferencesChanged(); + return false; + } + m_startMinimized = enabled; + persistPreferences(); + Q_EMIT preferencesChanged(); + return true; +} diff --git a/app/src/appcontroller.h b/app/src/appcontroller.h index b2a9c26..991c4b6 100644 --- a/app/src/appcontroller.h +++ b/app/src/appcontroller.h @@ -10,18 +10,38 @@ class QQuickWindow; class AppController : public QObject { Q_OBJECT + Q_PROPERTY(bool trayAvailable READ trayAvailable CONSTANT) + Q_PROPERTY(bool startOnLogin READ startOnLogin NOTIFY preferencesChanged) + Q_PROPERTY(bool startMinimized READ startMinimized NOTIFY preferencesChanged) public: - explicit AppController(QObject *parent = nullptr); + explicit AppController(bool startMinimizedRequested, QObject *parent = nullptr); + + bool trayAvailable() const { return m_trayAvailable; } + bool startOnLogin() const { return m_startOnLogin; } + bool startMinimized() const { return m_startMinimized; } void setWindow(QQuickWindow *window); + bool shouldStartMinimized() const { return m_startMinimizedRequested && m_trayAvailable; } + Q_INVOKABLE bool setStartOnLogin(bool enabled); + Q_INVOKABLE bool setStartMinimized(bool enabled); Q_INVOKABLE bool hideOnClose() const; Q_INVOKABLE void activate(); Q_INVOKABLE void quit(); +Q_SIGNALS: + void preferencesChanged(); + private: + bool writeAutostartEntry(bool enabled, bool startMinimized) const; + void persistPreferences() const; + QMenu m_menu; QSystemTrayIcon m_tray; QQuickWindow *m_window = nullptr; bool m_trayAvailable = false; + bool m_startOnLogin = false; + bool m_startMinimized = false; + bool m_startMinimizedRequested = false; + bool m_quitting = false; }; diff --git a/app/src/main.cpp b/app/src/main.cpp index 2e8dffc..60c20da 100644 --- a/app/src/main.cpp +++ b/app/src/main.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include #include @@ -53,7 +55,14 @@ int main(int argc, char *argv[]) QGuiApplication::setOrganizationName(QStringLiteral("mouse-master")); QGuiApplication::setDesktopFileName(AppId); QGuiApplication::setWindowIcon(QIcon::fromTheme(AppId, QIcon(QStringLiteral(":/icons/app/io.github.burakgon.MouseMaster.svg")))); - AppController appController; + QCommandLineParser parser; + parser.addHelpOption(); + parser.addVersionOption(); + const QCommandLineOption startMinimizedOption(QStringLiteral("start-minimized"), QStringLiteral("Start with the window hidden in the system tray.")); + parser.addOption(startMinimizedOption); + parser.process(app); + + AppController appController(parser.isSet(startMinimizedOption)); auto bus = QDBusConnection::sessionBus(); SingleInstance instance; @@ -79,6 +88,8 @@ int main(int argc, char *argv[]) if (!engine.rootObjects().isEmpty()) { if (auto window = qobject_cast(engine.rootObjects().constFirst())) { appController.setWindow(window); + const bool capture = qEnvironmentVariableIsSet("MM_SCREENSHOT") || qEnvironmentVariableIsSet("MM_RECORD"); + window->setVisible(capture || !appController.shouldStartMinimized()); } } diff --git a/docs/configuration.md b/docs/configuration.md index 22eb385..d28703b 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -7,6 +7,13 @@ Mouse Master keeps its settings in `~/.config/mouse-master/config.toml`. The app Hardware settings are optional: a setting that is not present is never touched, so the device keeps whatever it had. +## Application startup + +The Settings page can create a per-user desktop autostart entry. Both options are off by +default. Enable **Start Mouse Master at login** to launch the app with your desktop session; +then enable **Start minimized** to keep its window hidden in the system tray. The minimized +option is available only when a system tray is detected and app autostart is enabled. + ## General ```toml