refactor: extract PowerProfileService singleton; reducedMotion auto-enables on power-saver profile

This commit is contained in:
Damocles 2026-04-15 19:08:46 +02:00
parent c96b023fbe
commit dd5ca9d263
4 changed files with 63 additions and 51 deletions

View file

@ -1,71 +1,29 @@
import QtQuick import QtQuick
import Quickshell.Io
import "." as M import "." as M
M.BarIcon { M.BarIcon {
id: root id: root
tooltip: "Power profile: " + (root.profile || "unknown") tooltip: "Power profile: " + (M.PowerProfileService.profile || "unknown")
property string profile: "" color: M.PowerProfileService.profile === "performance" ? M.Theme.base09 : M.PowerProfileService.profile === "power-saver" ? M.Theme.base0B : parent?.accentColor ?? M.Theme.base05
color: root.profile === "performance" ? M.Theme.base09 : root.profile === "power-saver" ? M.Theme.base0B : parent?.accentColor ?? M.Theme.base05
icon: { icon: {
if (root.profile === "performance") if (M.PowerProfileService.profile === "performance")
return "\uF0E7"; return "\uF0E7";
if (root.profile === "power-saver") if (M.PowerProfileService.profile === "power-saver")
return "\uF06C"; return "\uF06C";
if (root.profile === "balanced") if (M.PowerProfileService.profile === "balanced")
return "\uF24E"; return "\uF24E";
return "\uF0E7"; 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 { MouseArea {
anchors.fill: parent anchors.fill: parent
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
const cycle = ["performance", "balanced", "power-saver"]; const cycle = ["performance", "balanced", "power-saver"];
const idx = cycle.indexOf(root.profile); const idx = cycle.indexOf(M.PowerProfileService.profile);
setter.next = cycle[(idx + 1) % cycle.length]; M.PowerProfileService.set(cycle[(idx + 1) % cycle.length]);
setter.running = true;
} }
} }
} }

View file

@ -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
}
}

View file

@ -35,7 +35,8 @@ QtObject {
property int groupSpacing: 6 property int groupSpacing: 6
property int radius: 4 property int radius: 4
property int screenRadius: 15 property int screenRadius: 15
property bool reducedMotion: false property bool _reducedMotionConfig: false
readonly property bool reducedMotion: _reducedMotionConfig || PowerProfileService.powerSaver
property FileView _themeFile: FileView { property FileView _themeFile: FileView {
path: (Quickshell.env("XDG_CONFIG_HOME") || (Quickshell.env("HOME") + "/.config")) + "/nova-shell/theme.json" path: (Quickshell.env("XDG_CONFIG_HOME") || (Quickshell.env("HOME") + "/.config")) + "/nova-shell/theme.json"
@ -77,6 +78,6 @@ QtObject {
if (data.screenRadius !== undefined) if (data.screenRadius !== undefined)
root.screenRadius = data.screenRadius; root.screenRadius = data.screenRadius;
if (data.reducedMotion !== undefined) if (data.reducedMotion !== undefined)
root.reducedMotion = data.reducedMotion; root._reducedMotionConfig = data.reducedMotion;
} }
} }

View file

@ -33,6 +33,7 @@ PowerProfile 1.0 PowerProfile.qml
IdleInhibitor 1.0 IdleInhibitor.qml IdleInhibitor 1.0 IdleInhibitor.qml
Notifications 1.0 Notifications.qml Notifications 1.0 Notifications.qml
singleton NiriIpc 1.0 NiriIpc.qml singleton NiriIpc 1.0 NiriIpc.qml
singleton PowerProfileService 1.0 PowerProfileService.qml
singleton SystemStats 1.0 SystemStats.qml singleton SystemStats 1.0 SystemStats.qml
ProcessList 1.0 ProcessList.qml ProcessList 1.0 ProcessList.qml
singleton NotifService 1.0 NotifService.qml singleton NotifService 1.0 NotifService.qml