From aa07895a972406b72dcd1740aca5f2d6bf67e5b9 Mon Sep 17 00:00:00 2001 From: end-4 <97237370+end-4@users.noreply.github.com> Date: Fri, 9 May 2025 20:24:39 +0200 Subject: [PATCH] rename material theme service --- .../services/MaterialThemeLoader.qml | 55 +++++++++++++++++++ .config/quickshell/shell.qml | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 .config/quickshell/services/MaterialThemeLoader.qml diff --git a/.config/quickshell/services/MaterialThemeLoader.qml b/.config/quickshell/services/MaterialThemeLoader.qml new file mode 100644 index 000000000..c34441a1b --- /dev/null +++ b/.config/quickshell/services/MaterialThemeLoader.qml @@ -0,0 +1,55 @@ +pragma Singleton +pragma ComponentBehavior: Bound + +import "root:/modules/common" +import QtQuick +import Quickshell +import Quickshell.Io +import Qt.labs.platform + +Singleton { + id: root + property string filePath: `${StandardPaths.standardLocations(StandardPaths.StateLocation)[0]}/user/generated/colors.json` + + function reapplyTheme() { + themeFileView.reload() + } + + function applyColors(fileContent) { + const json = JSON.parse(fileContent) + for (const key in json) { + if (json.hasOwnProperty(key)) { + // Convert snake_case to CamelCase + const camelCaseKey = key.replace(/_([a-z])/g, (g) => g[1].toUpperCase()) + const m3Key = `m3${camelCaseKey}` + Appearance.m3colors[m3Key] = json[key] + } + } + + Appearance.m3colors.darkmode = (Appearance.m3colors.m3background.hslLightness < 0.5) + } + + Timer { + id: delayedFileRead + interval: ConfigOptions.hacks.arbitraryRaceConditionDelay + repeat: false + running: false + onTriggered: { + root.applyColors(themeFileView.text()) + } + } + + FileView { + id: themeFileView + path: root.filePath + watchChanges: true + onFileChanged: { + this.reload() + delayedFileRead.start() + } + onLoadedChanged: { + const fileContent = themeFileView.text() + root.applyColors(fileContent) + } + } +} diff --git a/.config/quickshell/shell.qml b/.config/quickshell/shell.qml index 8de63090f..b0e5250a0 100644 --- a/.config/quickshell/shell.qml +++ b/.config/quickshell/shell.qml @@ -17,7 +17,7 @@ import "./services/" ShellRoot { Component.onCompleted: { - MaterialTheme.reapplyTheme() + MaterialThemeLoader.reapplyTheme() } Bar {}