diff --git a/modules/PowerProfile.qml b/modules/PowerProfile.qml index 41977a9..f7af01a 100644 --- a/modules/PowerProfile.qml +++ b/modules/PowerProfile.qml @@ -1,71 +1,29 @@ import QtQuick -import Quickshell.Io import "." as M M.BarIcon { id: root - tooltip: "Power profile: " + (root.profile || "unknown") + tooltip: "Power profile: " + (M.PowerProfileService.profile || "unknown") - property string profile: "" - - color: root.profile === "performance" ? M.Theme.base09 : root.profile === "power-saver" ? M.Theme.base0B : parent?.accentColor ?? M.Theme.base05 + color: M.PowerProfileService.profile === "performance" ? M.Theme.base09 : M.PowerProfileService.profile === "power-saver" ? M.Theme.base0B : parent?.accentColor ?? M.Theme.base05 icon: { - if (root.profile === "performance") + if (M.PowerProfileService.profile === "performance") return "\uF0E7"; - if (root.profile === "power-saver") + if (M.PowerProfileService.profile === "power-saver") return "\uF06C"; - if (root.profile === "balanced") + if (M.PowerProfileService.profile === "balanced") return "\uF24E"; return "\uF0E7"; } - Process { - id: proc - running: true - command: ["powerprofilesctl", "get"] - stdout: StdioCollector { - onStreamFinished: root.profile = text.trim() - } - } - // Event-driven: watch power-profiles-daemon DBus changes - Process { - id: ppMonitor - running: true - command: ["sh", "-c", "dbus-monitor --system \"interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path='/net/hadess/PowerProfiles'\" 2>/dev/null"] - stdout: SplitParser { - splitMarker: "\n" - onRead: _debounce.restart() - } - } - Timer { - id: _debounce - interval: 300 - onTriggered: proc.running = true - } - Timer { - interval: 60000 - running: true - repeat: true - onTriggered: proc.running = true - } - - Process { - id: setter - property string next: "" - command: ["powerprofilesctl", "set", next] - onRunningChanged: if (!running && next !== "") - proc.running = true - } - MouseArea { anchors.fill: parent cursorShape: Qt.PointingHandCursor onClicked: { const cycle = ["performance", "balanced", "power-saver"]; - const idx = cycle.indexOf(root.profile); - setter.next = cycle[(idx + 1) % cycle.length]; - setter.running = true; + const idx = cycle.indexOf(M.PowerProfileService.profile); + M.PowerProfileService.set(cycle[(idx + 1) % cycle.length]); } } } diff --git a/modules/PowerProfileService.qml b/modules/PowerProfileService.qml new file mode 100644 index 0000000..185745f --- /dev/null +++ b/modules/PowerProfileService.qml @@ -0,0 +1,52 @@ +pragma Singleton + +import QtQuick +import Quickshell.Io + +QtObject { + id: root + + property string profile: "" + readonly property bool powerSaver: profile === "power-saver" + + property var _proc: Process { + running: true + command: ["powerprofilesctl", "get"] + stdout: StdioCollector { + onStreamFinished: root.profile = text.trim() + } + } + + property var _monitor: Process { + running: true + command: ["sh", "-c", "dbus-monitor --system \"interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',path='/net/hadess/PowerProfiles'\" 2>/dev/null"] + stdout: SplitParser { + splitMarker: "\n" + onRead: _debounce.restart() + } + } + + property var _debounce: Timer { + interval: 300 + onTriggered: root._proc.running = true + } + + property var _poll: Timer { + interval: 60000 + running: true + repeat: true + onTriggered: root._proc.running = true + } + + function set(p) { + _setter.next = p; + _setter.running = true; + } + + property var _setter: Process { + property string next: "" + command: ["powerprofilesctl", "set", next] + onRunningChanged: if (!running && next !== "") + root._proc.running = true + } +} diff --git a/modules/Theme.qml b/modules/Theme.qml index 13ac23c..8da5f42 100644 --- a/modules/Theme.qml +++ b/modules/Theme.qml @@ -35,7 +35,8 @@ QtObject { property int groupSpacing: 6 property int radius: 4 property int screenRadius: 15 - property bool reducedMotion: false + property bool _reducedMotionConfig: false + readonly property bool reducedMotion: _reducedMotionConfig || PowerProfileService.powerSaver property FileView _themeFile: FileView { path: (Quickshell.env("XDG_CONFIG_HOME") || (Quickshell.env("HOME") + "/.config")) + "/nova-shell/theme.json" @@ -77,6 +78,6 @@ QtObject { if (data.screenRadius !== undefined) root.screenRadius = data.screenRadius; if (data.reducedMotion !== undefined) - root.reducedMotion = data.reducedMotion; + root._reducedMotionConfig = data.reducedMotion; } } diff --git a/modules/qmldir b/modules/qmldir index 705bf23..d363a44 100644 --- a/modules/qmldir +++ b/modules/qmldir @@ -33,6 +33,7 @@ PowerProfile 1.0 PowerProfile.qml IdleInhibitor 1.0 IdleInhibitor.qml Notifications 1.0 Notifications.qml singleton NiriIpc 1.0 NiriIpc.qml +singleton PowerProfileService 1.0 PowerProfileService.qml singleton SystemStats 1.0 SystemStats.qml ProcessList 1.0 ProcessList.qml singleton NotifService 1.0 NotifService.qml