Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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})
8 changes: 7 additions & 1 deletion app/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ C.ApplicationWindow {
height: 820
minimumWidth: 940
minimumHeight: 640
visible: true
visible: false
title: "Mouse Master"
onClosing: function (close) {
if (AppController.hideOnClose()) {
close.accepted = false
window.hide()
}
}
color: Theme.window

Settings {
Expand Down
7 changes: 1 addition & 6 deletions app/qml/components/ScrollPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,10 +21,4 @@ C.ScrollView {
y: root.margins
spacing: 20
}
Item {
// bottom padding
y: column.y + column.height
width: 1
height: root.margins
}
}
30 changes: 30 additions & 0 deletions app/qml/pages/SettingsPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
147 changes: 147 additions & 0 deletions app/src/appcontroller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#include "appcontroller.h"

#include <QAction>
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QIcon>
#include <QQuickWindow>
#include <QSaveFile>
#include <QSettings>
#include <QStandardPaths>

namespace {
const QString AppId = QStringLiteral("io.github.burakgon.MouseMaster");
const QString AppName = QStringLiteral("Mouse Master");
}

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);

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 && !m_quitting;
}

void AppController::activate()
{
if (!m_window) {
return;
}
m_window->show();
m_window->raise();
m_window->requestActivate();
}

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;
}
47 changes: 47 additions & 0 deletions app/src/appcontroller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once

#include <QObject>
#include <QMenu>
#include <QSystemTrayIcon>

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(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;
};
24 changes: 22 additions & 2 deletions app/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusMessage>
#include <QGuiApplication>
#include <QApplication>
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QIcon>
#include <QMouseEvent>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include <QQuickWindow>
#include <QTimer>

#include <memory>

#include "iconprovider.h"
#include "appcontroller.h"

namespace {
const QString AppId = QStringLiteral("io.github.burakgon.MouseMaster");
Expand Down Expand Up @@ -44,13 +48,21 @@ 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"))));
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;
Expand All @@ -67,11 +79,19 @@ 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<QQuickWindow *>(engine.rootObjects().constFirst())) {
appController.setWindow(window);
const bool capture = qEnvironmentVariableIsSet("MM_SCREENSHOT") || qEnvironmentVariableIsSet("MM_RECORD");
window->setVisible(capture || !appController.shouldStartMinimized());
}
}

// 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.
Expand Down
7 changes: 7 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down