From 7d9800374f7153677c600c5bcb1e5c038eaaa786 Mon Sep 17 00:00:00 2001 From: Damocles Date: Thu, 23 Apr 2026 23:22:55 +0200 Subject: [PATCH] extract loadColor gradient function into Theme service --- shell/applets/CpuApplet.qml | 12 ++---------- shell/applets/DiskApplet.qml | 10 +--------- shell/applets/GpuApplet.qml | 14 +++----------- shell/applets/TemperatureApplet.qml | 10 +--------- shell/services/Theme.qml | 9 +++++++++ 5 files changed, 16 insertions(+), 39 deletions(-) diff --git a/shell/applets/CpuApplet.qml b/shell/applets/CpuApplet.qml index 82528d6..d6e590a 100644 --- a/shell/applets/CpuApplet.qml +++ b/shell/applets/CpuApplet.qml @@ -12,14 +12,6 @@ Column { property bool active: true - function _loadColor(pct) { - const t = Math.max(0, Math.min(100, pct)) / 100; - const a = t < 0.5 ? S.Theme.base0B : S.Theme.base0A; - const b = t < 0.5 ? S.Theme.base0A : S.Theme.base08; - const u = t < 0.5 ? t * 2 : (t - 0.5) * 2; - return Qt.rgba(a.r + (b.r - a.r) * u, a.g + (b.g - a.g) * u, a.b + (b.b - a.b) * u, 1); - } - // Per-core rows Repeater { model: root.cores.length @@ -30,7 +22,7 @@ Column { readonly property int _u: root.cores[index]?.usage ?? 0 readonly property real _f: root.cores[index]?.freq_ghz ?? 0 - readonly property color _barColor: root._loadColor(_u) + readonly property color _barColor: S.Theme.loadColor(_u) readonly property bool _throttled: { const maxF = root.coreMaxFreq[index] ?? 0; return maxF > 0 && _f < maxF * 0.85 && _u >= 60; @@ -218,7 +210,7 @@ Column { anchors.rightMargin: 12 anchors.verticalCenter: parent.verticalCenter text: modelData.cpu.toFixed(1) + "%" - color: root._loadColor(modelData.cpu) + color: S.Theme.loadColor(modelData.cpu) font.pixelSize: S.Theme.fontSize - 2 font.family: S.Theme.fontFamily width: 36 diff --git a/shell/applets/DiskApplet.qml b/shell/applets/DiskApplet.qml index 16dee79..40ca390 100644 --- a/shell/applets/DiskApplet.qml +++ b/shell/applets/DiskApplet.qml @@ -17,14 +17,6 @@ Column { return bytes + "B"; } - function _barColor(pct) { - const t = Math.max(0, Math.min(100, pct)) / 100; - const a = t < 0.5 ? S.Theme.base0B : S.Theme.base0A; - const b = t < 0.5 ? S.Theme.base0A : S.Theme.base08; - const u = t < 0.5 ? t * 2 : (t - 0.5) * 2; - return Qt.rgba(a.r + (b.r - a.r) * u, a.g + (b.g - a.g) * u, a.b + (b.b - a.b) * u, 1); - } - Repeater { model: root.mounts @@ -64,7 +56,7 @@ Column { Rectangle { width: parent.width * (modelData.pct / 100) height: parent.height - color: root._barColor(modelData.pct) + color: S.Theme.loadColor(modelData.pct) radius: 2 Behavior on width { NumberAnimation { diff --git a/shell/applets/GpuApplet.qml b/shell/applets/GpuApplet.qml index 7b2272f..3a313fb 100644 --- a/shell/applets/GpuApplet.qml +++ b/shell/applets/GpuApplet.qml @@ -8,14 +8,6 @@ Column { property bool active: true - function _loadColor(pct) { - const t = Math.max(0, Math.min(100, pct)) / 100; - const a = t < 0.5 ? S.Theme.base0B : S.Theme.base0A; - const b = t < 0.5 ? S.Theme.base0A : S.Theme.base08; - const u = t < 0.5 ? t * 2 : (t - 0.5) * 2; - return Qt.rgba(a.r + (b.r - a.r) * u, a.g + (b.g - a.g) * u, a.b + (b.b - a.b) * u, 1); - } - function _fmt(gb) { return gb >= 10 ? gb.toFixed(1) + "G" : gb.toFixed(2) + "G"; } @@ -41,7 +33,7 @@ Column { anchors.rightMargin: 12 anchors.verticalCenter: parent.verticalCenter text: S.SystemStats.gpuUsage + "%" - color: root._loadColor(S.SystemStats.gpuUsage) + color: S.Theme.loadColor(S.SystemStats.gpuUsage) font.pixelSize: S.Theme.fontSize font.family: S.Theme.fontFamily font.bold: true @@ -70,7 +62,7 @@ Column { Rectangle { width: parent.width * Math.min(1, S.SystemStats.gpuUsage / 100) height: parent.height - color: root._loadColor(S.SystemStats.gpuUsage) + color: S.Theme.loadColor(S.SystemStats.gpuUsage) radius: 3 Behavior on width { enabled: root.active @@ -113,7 +105,7 @@ Column { const offset = maxSamples - d.length; for (let i = 0; i < d.length; i++) { const barH = Math.max(1, height * d[i] / 100); - const col = root._loadColor(d[i]); + const col = S.Theme.loadColor(d[i]); ctx.fillStyle = col.toString(); ctx.fillRect((offset + i) * bw, height - barH, Math.max(1, bw - 0.5), barH); } diff --git a/shell/applets/TemperatureApplet.qml b/shell/applets/TemperatureApplet.qml index bc7ed73..7ff7e44 100644 --- a/shell/applets/TemperatureApplet.qml +++ b/shell/applets/TemperatureApplet.qml @@ -21,14 +21,6 @@ Column { } } - function _tempColor(celsius) { - const t = Math.max(0, Math.min(100, celsius)) / 100; - const a = t < 0.5 ? S.Theme.base0B : S.Theme.base0A; - const b = t < 0.5 ? S.Theme.base0A : S.Theme.base08; - const u = t < 0.5 ? t * 2 : (t - 0.5) * 2; - return Qt.rgba(a.r + (b.r - a.r) * u, a.g + (b.g - a.g) * u, a.b + (b.b - a.b) * u, 1); - } - // Header - current temp Item { width: root.width @@ -250,7 +242,7 @@ Column { anchors.rightMargin: 12 anchors.verticalCenter: parent.verticalCenter text: modelData.celsius + "\u00B0C" - color: root._tempColor(modelData.celsius) + color: S.Theme.loadColor(modelData.celsius) font.pixelSize: S.Theme.fontSize - 2 font.family: S.Theme.fontFamily font.bold: _isActive diff --git a/shell/services/Theme.qml b/shell/services/Theme.qml index c806a10..05311e2 100644 --- a/shell/services/Theme.qml +++ b/shell/services/Theme.qml @@ -39,6 +39,15 @@ QtObject { property bool _reducedMotionConfig: false readonly property bool reducedMotion: _reducedMotionConfig || PowerProfileService.powerSaver + // Green -> yellow -> red gradient for 0-100% load/usage values + function loadColor(pct) { + const t = Math.max(0, Math.min(100, pct)) / 100; + const a = t < 0.5 ? root.base0B : root.base0A; + const b = t < 0.5 ? root.base0A : root.base08; + const u = t < 0.5 ? t * 2 : (t - 0.5) * 2; + return Qt.rgba(a.r + (b.r - a.r) * u, a.g + (b.g - a.g) * u, a.b + (b.b - a.b) * u, 1); + } + property FileView _themeFile: FileView { path: (Quickshell.env("XDG_CONFIG_HOME") || (Quickshell.env("HOME") + "/.config")) + "/nova-shell/theme.json" watchChanges: true