From 5a004b8be5c945b7bcf840b9c3aafecc6d79c02e Mon Sep 17 00:00:00 2001 From: Johannes Kresner Date: Sat, 29 Aug 2026 15:24:20 +0200 Subject: [PATCH] feat: add cpu + temp + ram to bar --- .../quickshell/components/Bar/Bar.qml | 152 +++++++++++++++++- 1 file changed, 150 insertions(+), 2 deletions(-) diff --git a/mutable_config/quickshell/components/Bar/Bar.qml b/mutable_config/quickshell/components/Bar/Bar.qml index f263bd0..767c564 100644 --- a/mutable_config/quickshell/components/Bar/Bar.qml +++ b/mutable_config/quickshell/components/Bar/Bar.qml @@ -112,7 +112,7 @@ PanelWindow { Text { id: clockText anchors.centerIn: parent - text: Qt.formatDateTime(clock.date, "dddd, MMMM d · hh:mm") + text: Qt.formatDateTime(clock.date, "hh:mm") color: Theme.colors.text font.family: Theme.font.family font.bold: true @@ -123,7 +123,6 @@ PanelWindow { // Hidden entirely when none of the tray icons have anything to show. Rectangle { id: rightPill - visible: wifiPoll.connected || bluetoothPoll.connectedDevices.length > 0 || battery.isPresent anchors.right: parent.right anchors.verticalCenter: parent.verticalCenter height: parent.height @@ -138,6 +137,28 @@ PanelWindow { anchors.centerIn: parent spacing: Theme.geometry.spacing + StatIndicator { + glyph: "" // nf-fa-microchip + text: sysStats.cpuPercent + "%" + tooltip: "CPU usage: " + sysStats.cpuPercent + "%" + tooltipWindow: barWindow.trayTooltip + } + + StatIndicator { + visible: sysStats.cpuTemp.length > 0 + glyph: "" // nf-fa-thermometer_half + text: sysStats.cpuTemp + "°" + tooltip: "CPU temp: " + sysStats.cpuTemp + "°C" + tooltipWindow: barWindow.trayTooltip + } + + StatIndicator { + glyph: "" // nf-fa-server + text: sysStats.ramPercent + "%" + tooltip: "RAM usage: " + sysStats.ramPercent + "%" + tooltipWindow: barWindow.trayTooltip + } + TrayIcon { glyph: "" // nf-fa-wifi active: wifiPoll.connected @@ -177,6 +198,85 @@ PanelWindow { property QtObject battery: UPower.displayDevice + // --- System stats (CPU%, CPU temp, RAM%) read straight from /proc and /sys --- + QtObject { + id: sysStats + property string cpuPercent: "0" + property string cpuTemp: "" + property string ramPercent: "0" + + // Previous /proc/stat totals, for computing CPU% as a delta between polls. + property real prevTotal: -1 + property real prevIdle: -1 + } + + FileView { id: statFile; path: "/proc/stat" } + FileView { id: memInfoFile; path: "/proc/meminfo" } + + Timer { + interval: 1000 + running: true + repeat: true + triggeredOnStart: true + onTriggered: { + statFile.reload(); + memInfoFile.reload(); + + // First line: "cpu user nice system idle iowait irq softirq ..." + let fields = statFile.text().split("\n")[0].trim().split(/\s+/).slice(1).map(Number); + let idle = fields[3] + fields[4]; + let total = fields.reduce((a, b) => a + b, 0); + + if (sysStats.prevTotal >= 0) { + let totalDelta = total - sysStats.prevTotal; + let idleDelta = idle - sysStats.prevIdle; + sysStats.cpuPercent = totalDelta > 0 + ? Math.round(100 * (totalDelta - idleDelta) / totalDelta).toString() + : "0"; + } + sysStats.prevTotal = total; + sysStats.prevIdle = idle; + + let mem = {}; + for (const line of memInfoFile.text().split("\n")) { + let match = line.match(/^(\w+):\s+(\d+)/); + if (match) mem[match[1]] = Number(match[2]); + } + sysStats.ramPercent = mem.MemTotal > 0 + ? Math.round(100 * (mem.MemTotal - mem.MemAvailable) / mem.MemTotal).toString() + : "0"; + } + } + + // Discover the CPU temp sensor's hwmon path once (the hwmonN numbering + // isn't stable across boots), then poll that fixed path via FileView. + FileView { id: cpuTempFile; watchChanges: false } + + Process { + id: hwmonDiscovery + command: ["bash", "-c", "for h in /sys/class/hwmon/hwmon*; do n=$(cat \"$h/name\" 2>/dev/null); if [ \"$n\" = k10temp ] || [ \"$n\" = coretemp ] || [ \"$n\" = zenpower ]; then echo \"$h/temp1_input\"; break; fi; done"] + running: true + stdout: StdioCollector { + onStreamFinished: { + let path = this.text.trim(); + if (path.length > 0) cpuTempFile.path = path; + } + } + } + + Timer { + interval: 1000 + running: true + repeat: true + triggeredOnStart: true + onTriggered: { + if (cpuTempFile.path.length === 0) return; + cpuTempFile.reload(); + let millideg = Number(cpuTempFile.text().trim()); + if (millideg > 0) sysStats.cpuTemp = Math.round(millideg / 1000).toString(); + } + } + // --- WiFi status (polled via nmcli; requires NetworkManager) --- QtObject { id: wifiPoll @@ -241,6 +341,54 @@ PanelWindow { } } + // Inline component for a compact icon+text stat readout (CPU, temp, RAM). + component StatIndicator: Item { + id: statIndicator + property string glyph: "" + property alias text: statText.text + property string tooltip: "" + property QtObject tooltipWindow + + width: statRow.width + height: 24 + + Row { + id: statRow + anchors.verticalCenter: parent.verticalCenter + spacing: 4 + + Text { + anchors.verticalCenter: parent.verticalCenter + text: statIndicator.glyph + font.family: Theme.font.family + font.pixelSize: 13 + color: Theme.colors.text + } + + Text { + id: statText + anchors.verticalCenter: parent.verticalCenter + color: Theme.colors.text + font.family: Theme.font.family + font.pixelSize: 12 + } + } + + MouseArea { + anchors.fill: parent + hoverEnabled: true + onEntered: { + if (statIndicator.tooltipWindow && statIndicator.tooltip.length > 0) { + let pos = statIndicator.mapToGlobal(statIndicator.width / 2, statIndicator.height + Theme.geometry.spacing / 2); + statIndicator.tooltipWindow.showAt(pos.x, pos.y, statIndicator.tooltip); + } + } + onExited: { + if (statIndicator.tooltipWindow) statIndicator.tooltipWindow.hide(); + } + } + } + // Inline component for a hoverable tray icon. The tooltip itself is // rendered by the shared TrayTooltip overlay window (see tooltipWindow) // so it isn't clipped by this bar's small surface.