From 35c6efdcc568a034da58dbe1849677c44dc8741f Mon Sep 17 00:00:00 2001 From: purian23 Date: Tue, 29 Jul 2025 21:56:41 -0400 Subject: [PATCH] Optimize services to reduce load --- Modules/TopBar/AudioVisualization.qml | 2 +- Services/IdleService.qml | 60 +++++++++++++++++++++++++++ Services/NetworkService.qml | 24 ++++++++++- Services/SysMonitorService.qml | 20 ++++++++- Services/WeatherService.qml | 18 +++++++- Services/qmldir | 16 +++++++ 6 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 Services/IdleService.qml create mode 100644 Services/qmldir diff --git a/Modules/TopBar/AudioVisualization.qml b/Modules/TopBar/AudioVisualization.qml index b68f7528..c9fc4409 100644 --- a/Modules/TopBar/AudioVisualization.qml +++ b/Modules/TopBar/AudioVisualization.qml @@ -67,7 +67,7 @@ Item { id: fallbackTimer running: false - interval: 100 + interval: 256 repeat: true onTriggered: { root.audioLevels = [Math.random() * 40 + 10, Math.random() * 60 + 20, Math.random() * 50 + 15, Math.random() * 35 + 20, Math.random() * 45 + 15, Math.random() * 55 + 25]; diff --git a/Services/IdleService.qml b/Services/IdleService.qml new file mode 100644 index 00000000..1f73a8fd --- /dev/null +++ b/Services/IdleService.qml @@ -0,0 +1,60 @@ +pragma Singleton +pragma ComponentBehavior: Bound + +import QtQuick +import Quickshell +import Quickshell.Io + +Singleton { + id: root + + property bool isIdle: false + property int idleThresholdSeconds: 300 // 5 minutes + property int checkInterval: 30000 // Check every 30 seconds + + signal idleChanged(bool idle) + + function checkIdleState() { + if (idleChecker.running) return + idleChecker.running = true + } + + Timer { + id: idleTimer + interval: root.checkInterval + running: true + repeat: true + onTriggered: root.checkIdleState() + } + + Process { + id: idleChecker + command: ["bash", "-c", "if command -v xprintidle >/dev/null 2>&1; then echo $(( $(xprintidle) / 1000 )); elif command -v qdbus >/dev/null 2>&1; then qdbus org.freedesktop.ScreenSaver /org/freedesktop/ScreenSaver GetSessionIdleTime 2>/dev/null || echo 0; else echo 0; fi"] + running: false + + stdout: SplitParser { + splitMarker: "\n" + onRead: (data) => { + const idleSeconds = parseInt(data.trim()) || 0 + const wasIdle = root.isIdle + root.isIdle = idleSeconds >= root.idleThresholdSeconds + + if (wasIdle !== root.isIdle) { + console.log("IdleService: System idle state changed to:", root.isIdle ? "idle" : "active", "(" + idleSeconds + "s)") + root.idleChanged(root.isIdle) + } + } + } + + onExited: (exitCode) => { + if (exitCode !== 0) { + console.warn("IdleService: Failed to check idle state, exit code:", exitCode) + } + } + } + + Component.onCompleted: { + console.log("IdleService: Initialized with", root.idleThresholdSeconds + "s threshold") + checkIdleState() + } +} \ No newline at end of file diff --git a/Services/NetworkService.qml b/Services/NetworkService.qml index f93d1273..a5da1194 100644 --- a/Services/NetworkService.qml +++ b/Services/NetworkService.qml @@ -9,6 +9,7 @@ import qs.Common Singleton { id: root + property int refCount: 0 property string networkStatus: "disconnected" // "ethernet", "wifi", "disconnected" property string ethernetIP: "" property string ethernetInterface: "" @@ -42,6 +43,25 @@ Singleton { signal networksUpdated() + function addRef() { + refCount++; + console.log("NetworkService: addRef, refCount now:", refCount); + if (refCount === 1) { + // Start monitoring when first consumer appears + networkStatusChecker.running = true; + } + } + + function removeRef() { + refCount = Math.max(0, refCount - 1); + console.log("NetworkService: removeRef, refCount now:", refCount); + if (refCount === 0) { + // Stop monitoring when no consumers + networkStatusChecker.running = false; + autoRefreshTimer.running = false; + } + } + // Load saved preference on startup Component.onCompleted: { // Load preference from Prefs system @@ -58,7 +78,7 @@ Singleton { Process { id: networkStatusChecker command: ["sh", "-c", "nmcli -t -f DEVICE,TYPE,STATE device | grep -E '(ethernet|wifi)' && echo '---' && ip link show | grep -E '^[0-9]+:.*ethernet.*state UP'"] - running: true + running: false stdout: StdioCollector { onStreamFinished: { @@ -519,7 +539,7 @@ Singleton { Timer { id: autoRefreshTimer interval: 20000 - running: root.autoRefreshEnabled + running: root.autoRefreshEnabled && root.refCount > 0 repeat: true onTriggered: scanWifi() } diff --git a/Services/SysMonitorService.qml b/Services/SysMonitorService.qml index 1ea5bd72..08b89b55 100644 --- a/Services/SysMonitorService.qml +++ b/Services/SysMonitorService.qml @@ -4,12 +4,13 @@ pragma ComponentBehavior: Bound import QtQuick import Quickshell import Quickshell.Io +import qs.Services Singleton { id: root property int refCount: 0 - property int updateInterval: 8000 + property int updateInterval: 30000 property int maxProcesses: 100 property bool isUpdating: false @@ -360,11 +361,26 @@ Singleton { Timer { id: updateTimer interval: root.updateInterval - running: root.refCount > 0 + running: root.refCount > 0 && !IdleService.isIdle repeat: true triggeredOnStart: true onTriggered: root.updateAllStats() } + + Connections { + target: IdleService + function onIdleChanged(idle) { + if (idle) { + console.log("SysMonitorService: System idle, pausing monitoring") + } else { + console.log("SysMonitorService: System active, resuming monitoring") + if (root.refCount > 0) { + // Trigger immediate update when coming back from idle + root.updateAllStats() + } + } + } + } readonly property string scriptBody: `set -Eeuo pipefail trap 'echo "ERR at line $LINENO: $BASH_COMMAND (exit $?)" >&2' ERR diff --git a/Services/WeatherService.qml b/Services/WeatherService.qml index a4d90ab8..c1842279 100644 --- a/Services/WeatherService.qml +++ b/Services/WeatherService.qml @@ -5,6 +5,7 @@ import QtQuick import Quickshell import Quickshell.Io import qs.Common +import qs.Services Singleton { id: root @@ -239,7 +240,7 @@ Singleton { Timer { id: updateTimer interval: root.updateInterval - running: root.refCount > 0 + running: root.refCount > 0 && !IdleService.isIdle repeat: true triggeredOnStart: true onTriggered: { @@ -247,6 +248,21 @@ Singleton { } } + Connections { + target: IdleService + function onIdleChanged(idle) { + if (idle) { + console.log("WeatherService: System idle, pausing weather updates") + } else { + console.log("WeatherService: System active, resuming weather updates") + if (root.refCount > 0 && !root.weather.available) { + // Trigger immediate update when coming back from idle if no data + root.fetchWeather() + } + } + } + } + Timer { id: retryTimer interval: root.retryDelay diff --git a/Services/qmldir b/Services/qmldir new file mode 100644 index 00000000..74c80bc4 --- /dev/null +++ b/Services/qmldir @@ -0,0 +1,16 @@ +singleton AppSearchService 1.0 AppSearchService.qml +singleton AudioService 1.0 AudioService.qml +singleton BatteryService 1.0 BatteryService.qml +singleton BluetoothService 1.0 BluetoothService.qml +singleton BrightnessService 1.0 BrightnessService.qml +singleton CalendarService 1.0 CalendarService.qml +singleton FocusedWindowService 1.0 FocusedWindowService.qml +singleton IdleService 1.0 IdleService.qml +singleton MprisController 1.0 MprisController.qml +singleton NetworkService 1.0 NetworkService.qml +singleton NiriService 1.0 NiriService.qml +singleton NotificationService 1.0 NotificationService.qml +singleton SysMonitorService 1.0 SysMonitorService.qml +singleton ToastService 1.0 ToastService.qml +singleton UserInfoService 1.0 UserInfoService.qml +singleton WeatherService 1.0 WeatherService.qml \ No newline at end of file