From 7cf704d4509a74bff26a27c60680ce50928af9e0 Mon Sep 17 00:00:00 2001 From: end-4 <97237370+end-4@users.noreply.github.com> Date: Thu, 19 Mar 2026 12:39:12 +0100 Subject: [PATCH] refactor GameModeToggle's conf opt fetching --- .../modules/common/models/NestableObject.qml | 6 ++ .../models/hyprland/HyprlandConfigOption.qml | 63 +++++++++++++++++++ .../models/quickToggles/GameModeToggle.qml | 15 +++-- .../quickshell/ii/services/HyprlandConfig.qml | 16 ++++- 4 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 dots/.config/quickshell/ii/modules/common/models/NestableObject.qml create mode 100644 dots/.config/quickshell/ii/modules/common/models/hyprland/HyprlandConfigOption.qml diff --git a/dots/.config/quickshell/ii/modules/common/models/NestableObject.qml b/dots/.config/quickshell/ii/modules/common/models/NestableObject.qml new file mode 100644 index 000000000..50af20cb0 --- /dev/null +++ b/dots/.config/quickshell/ii/modules/common/models/NestableObject.qml @@ -0,0 +1,6 @@ +import QtQuick + +// QtObject that allows stuff to be freely declared inside +QtObject { + default property list data +} diff --git a/dots/.config/quickshell/ii/modules/common/models/hyprland/HyprlandConfigOption.qml b/dots/.config/quickshell/ii/modules/common/models/hyprland/HyprlandConfigOption.qml new file mode 100644 index 000000000..0422aab02 --- /dev/null +++ b/dots/.config/quickshell/ii/modules/common/models/hyprland/HyprlandConfigOption.qml @@ -0,0 +1,63 @@ +pragma ComponentBehavior: Bound +import QtQml +import QtQuick +import Quickshell.Io +import qs.services +import "../" + +NestableObject { + id: root + + required property string key + property alias fetching: fetchProc.running + property bool set + property var value + + Component.onCompleted: fetch() + + Connections { + target: HyprlandConfig + function onReloaded() { + root.fetch(); + } + } + + function fetch() { + fetchProc.command = fetchProc.baseCommand.concat([root.key]); + fetchProc.running = true; + } + + function setValue(newValue) { + HyprlandConfig.set(root.key, newValue) + } + + function reset() { + HyprlandConfig.reset(root.key) + } + + Process { + id: fetchProc + property list baseCommand: ["hyprctl", "getoption", "-j"] + stdout: StdioCollector { + onStreamFinished: { + if (text == "no such option") + return; + try { + const obj = JSON.parse(text); + // Note that the value is returned as "": + // It's the only field that isn't always in the same key so we put it in an else + for (const key in obj) { + if (key == "option") + continue; + else if (key == "set") + root.set = obj[key]; + else + root.value = obj[key]; + } + } catch (e) { + console.log(`[HyprlandConfigOption] Failed to fetch option "${root.key}":\n - Output: ${text.trim()}\n - Error: ${e}`); + } + } + } + } +} diff --git a/dots/.config/quickshell/ii/modules/common/models/quickToggles/GameModeToggle.qml b/dots/.config/quickshell/ii/modules/common/models/quickToggles/GameModeToggle.qml index a1d6db88e..79dd98665 100644 --- a/dots/.config/quickshell/ii/modules/common/models/quickToggles/GameModeToggle.qml +++ b/dots/.config/quickshell/ii/modules/common/models/quickToggles/GameModeToggle.qml @@ -1,11 +1,12 @@ import QtQuick import Quickshell.Io +import qs.modules.common.models.hyprland import qs.services QuickToggleModel { id: root name: Translation.tr("Game mode") - toggled: toggled + toggled: !confOpt.value icon: "gamepad" mainAction: () => { @@ -34,13 +35,11 @@ QuickToggleModel { ]); } } - Process { - id: fetchActiveState - running: true - command: ["bash", "-c", `test "$(hyprctl getoption animations:enabled -j | jq ".int")" -ne 0`] - onExited: (exitCode, exitStatus) => { - root.toggled = exitCode !== 0; // Inverted because enabled = nonzero exit - } + + HyprlandConfigOption { + id: confOpt + key: "animations:enabled" } + tooltipText: Translation.tr("Game mode") } diff --git a/dots/.config/quickshell/ii/services/HyprlandConfig.qml b/dots/.config/quickshell/ii/services/HyprlandConfig.qml index 50cbe9d9b..cf5eb0e30 100644 --- a/dots/.config/quickshell/ii/services/HyprlandConfig.qml +++ b/dots/.config/quickshell/ii/services/HyprlandConfig.qml @@ -3,7 +3,7 @@ pragma ComponentBehavior: Bound import QtQuick import Quickshell -import Quickshell.Io +import Quickshell.Hyprland import qs.modules.common import qs.modules.common.functions @@ -14,6 +14,8 @@ import qs.modules.common.functions Singleton { id: root + signal reloaded() + readonly property string configuratorScriptPath: Quickshell.shellPath("scripts/hyprland/hyprconfigurator.py") readonly property string shellOverridesPath: FileUtils.trimFileProtocol(`${Directories.config}/hypr/hyprland/shellOverrides/main.conf`) @@ -39,7 +41,7 @@ Singleton { ]) } - function resetMany(keys: var) { + function resetMany(keys: list) { let args = "" for (let i = 0; i < keys.length; i++) { args += `--reset "${keys[i]}" ` @@ -48,4 +50,14 @@ Singleton { `${root.configuratorScriptPath} --file ${root.shellOverridesPath} ${args}` // ]) } + + Connections { + target: Hyprland + + function onRawEvent(event) { + if (event.name == "configreloaded") { + root.reloaded() + } + } + } }