This repository has been archived on 2026-11-02. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
dotfiles/mutable_config/quickshell/components/Bar/Bar.qml
T
2026-08-30 17:51:19 +02:00

455 lines
16 KiB
QML

import QtQuick
import Quickshell
import Quickshell.Wayland
import Quickshell.Hyprland
import Quickshell.Io
import Quickshell.Services.UPower
import "../Shared"
PanelWindow {
id: barWindow
// Reference to global state
property QtObject rootState
// Shared overlay window that renders tray icon hover tooltips (see
// TrayTooltip.qml) so this bar's own surface can stay exactly as tall
// as the pill itself.
property QtObject trayTooltip
anchors {
top: true
left: true
right: true
}
// Matches hyprland.lua's general.gaps_out (20) so the gap above the bar
// (this margin) and the gap below it (added by Hyprland once the
// exclusiveZone ends) read as the same size.
readonly property int hyprGapsOut: 20
margins {
top: hyprGapsOut
left: hyprGapsOut
right: hyprGapsOut
}
// No separate bottom buffer here — Hyprland's gaps_out already adds its
// own gap between the reserved zone and the tiled window below.
exclusiveZone: implicitHeight + margins.top
implicitHeight: 40
color: "transparent"
WlrLayershell.layer: WlrLayer.Top
WlrLayershell.namespace: "quickshell-bar"
// The clock and tray only make sense once, so they live on whichever
// monitor the mouse currently sits on (Hyprland's focused monitor
// follows the cursor), rather than being duplicated on every screen.
readonly property bool isFocusedMonitor: Hyprland.focusedMonitor && Hyprland.focusedMonitor.name === screen.name
// Left Pill (Workspaces) - tinted with the primary accent.
Pill {
id: leftPill
anchors.left: parent.left
anchors.verticalCenter: parent.verticalCenter
height: parent.height
tint: Theme.colors.primary
glow: true
// Workspaces
Row {
spacing: Theme.geometry.spacing
Repeater {
model: Hyprland.workspaces.values.filter(ws => ws.monitor && ws.monitor.name === barWindow.screen.name)
delegate: Rectangle {
width: 24
height: 24
radius: Theme.geometry.radiusTiny
color: modelData.active ? Theme.colors.primary : Theme.colors.surfaceHover
Behavior on color { ColorAnimation { duration: Theme.animation.durationFast } }
Text {
anchors.centerIn: parent
text: modelData.id
color: modelData.active ? Theme.colors.textOnAccent : Theme.colors.text
font.family: Theme.font.family
font.pixelSize: Theme.type.caption
font.bold: modelData.active
}
MouseArea {
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: modelData.activate()
}
}
}
}
}
// Center Pill (Clock) - tinted with the secondary accent.
Pill {
id: centerPill
visible: barWindow.isFocusedMonitor
anchors.horizontalCenter: parent.horizontalCenter
anchors.verticalCenter: parent.verticalCenter
height: parent.height
tint: Theme.colors.secondary
glow: true
SystemClock {
id: clock
precision: SystemClock.Minutes
}
Text {
id: clockText
text: Qt.formatDateTime(clock.date, "hh:mm")
color: Theme.colors.secondary
font.family: Theme.font.family
font.bold: true
}
}
// Right Pill (System tray: CPU/RAM/temp stats, WiFi, Bluetooth, Battery)
// Tinted with the tertiary accent; each stat/icon also carries its own
// color so the pill reads as a set of distinct signals, not one blob.
Pill {
id: rightPill
visible: barWindow.isFocusedMonitor
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
height: parent.height
tint: Theme.colors.tertiary
StatIndicator {
glyph: "" // nf-fa-microchip
text: sysStats.cpuPercent + "%"
tint: Theme.colors.tertiary
tooltip: "CPU usage: " + sysStats.cpuPercent + "%"
tooltipWindow: barWindow.trayTooltip
}
StatIndicator {
visible: sysStats.cpuTemp.length > 0
glyph: "" // nf-fa-thermometer_half
text: sysStats.cpuTemp + "°"
tint: {
let t = Number(sysStats.cpuTemp);
if (t >= 80) return Theme.colors.alert;
if (t >= 65) return Theme.colors.warning;
return Theme.colors.textMuted;
}
tooltip: "CPU temp: " + sysStats.cpuTemp + "°C"
tooltipWindow: barWindow.trayTooltip
}
StatIndicator {
glyph: "" // nf-fa-server
text: sysStats.ramPercent + "%"
tint: Theme.colors.secondary
tooltip: "RAM usage: " + sysStats.ramPercent + "%"
tooltipWindow: barWindow.trayTooltip
}
TrayIcon {
glyph: "" // nf-fa-wifi
active: wifiPoll.connected
tint: Theme.colors.primary
tooltip: "WiFi: " + wifiPoll.ssid
visible: wifiPoll.connected
tooltipWindow: barWindow.trayTooltip
}
TrayIcon {
glyph: "" // nf-fa-bluetooth_b
active: true
tint: Theme.colors.secondary
tooltip: "Bluetooth: " + bluetoothPoll.connectedDevices
visible: bluetoothPoll.connectedDevices.length > 0
tooltipWindow: barWindow.trayTooltip
}
TrayIcon {
glyph: {
let p = battery.percentage;
if (!battery.isPresent) return ""; // nf-fa-plug (desktop/no battery)
if (p >= 0.9) return ""; // nf-fa-battery_full
if (p >= 0.65) return ""; // nf-fa-battery_three_quarters
if (p >= 0.4) return ""; // nf-fa-battery_half
if (p >= 0.15) return ""; // nf-fa-battery_quarter
return ""; // nf-fa-battery_empty
}
active: battery.state === UPowerDeviceState.Charging
tint: {
let p = battery.percentage;
if (p >= 0.5) return Theme.colors.success;
if (p >= 0.2) return Theme.colors.warning;
return Theme.colors.alert;
}
tooltip: battery.isPresent
? "Battery: " + Math.round(battery.percentage * 100) + "%"
+ (battery.state === UPowerDeviceState.Charging ? " (charging)" : "")
: "No battery"
visible: battery.isPresent
tooltipWindow: barWindow.trayTooltip
}
}
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
property bool connected: false
property string ssid: ""
}
Timer {
interval: 5000
running: true
repeat: true
triggeredOnStart: true
onTriggered: wifiProcess.restart()
}
Process {
id: wifiProcess
command: ["bash", "-c", "nmcli -t -f ACTIVE,SSID dev wifi 2>/dev/null | awk -F: '$1==\"yes\"{print $2; exit}'"]
running: false
function restart() { running = false; running = true; }
stdout: StdioCollector {
onStreamFinished: {
let ssid = this.text.trim();
wifiPoll.connected = ssid.length > 0;
wifiPoll.ssid = ssid;
}
}
}
// --- Bluetooth status (polled via bluetoothctl) ---
QtObject {
id: bluetoothPoll
property bool available: true
property bool powered: false
property string connectedDevices: ""
}
Timer {
interval: 5000
running: true
repeat: true
triggeredOnStart: true
onTriggered: bluetoothProcess.restart()
}
Process {
id: bluetoothProcess
command: ["bash", "-c", "powered=$(bluetoothctl show 2>/dev/null | awk -F': ' '/Powered/{print $2}'); devices=$(bluetoothctl devices Connected 2>/dev/null | sed -E 's/^Device [0-9A-F:]+ //' | paste -sd, -); echo \"$powered|$devices\""]
running: false
function restart() { running = false; running = true; }
stdout: StdioCollector {
onStreamFinished: {
let parts = this.text.trim().split("|");
if (parts[0] === undefined || parts[0] === "") {
bluetoothPoll.available = false;
return;
}
bluetoothPoll.available = true;
bluetoothPoll.powered = parts[0] === "yes";
bluetoothPoll.connectedDevices = parts[1] || "";
}
}
}
// Inline component for a compact icon+text stat readout (CPU, temp, RAM).
// `tint` colors both the glyph and text, so each stat reads as its own
// signal instead of everything sharing one flat color.
component StatIndicator: Item {
id: statIndicator
property string glyph: ""
property alias text: statText.text
property color tint: Theme.colors.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: Theme.type.body
color: statIndicator.tint
Behavior on color { ColorAnimation { duration: Theme.animation.durationFast } }
}
Text {
id: statText
anchors.verticalCenter: parent.verticalCenter
color: statIndicator.tint
font.family: Theme.font.family
font.pixelSize: Theme.type.caption
Behavior on color { ColorAnimation { duration: Theme.animation.durationFast } }
}
}
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. `tint` is this icon's
// own identity color (wifi vs bluetooth vs battery-by-level, etc.).
component TrayIcon: Item {
id: trayIcon
property string glyph: ""
property string tooltip: ""
property bool active: false
property color tint: Theme.colors.primary
property QtObject tooltipWindow
width: 24
height: 24
Rectangle {
anchors.fill: parent
radius: Theme.geometry.radiusTiny
color: hoverArea.containsMouse
? Theme.colors.surfaceHover
: (trayIcon.active ? Qt.alpha(trayIcon.tint, 0.15) : "transparent")
Behavior on color { ColorAnimation { duration: Theme.animation.durationFast } }
Text {
anchors.centerIn: parent
text: trayIcon.glyph
font.family: Theme.font.family
font.pixelSize: Theme.type.body
color: trayIcon.active ? trayIcon.tint : Theme.colors.text
Behavior on color { ColorAnimation { duration: Theme.animation.durationFast } }
}
}
MouseArea {
id: hoverArea
anchors.fill: parent
hoverEnabled: true
onEntered: {
if (trayIcon.tooltipWindow && trayIcon.tooltip.length > 0) {
// Anchor on the icon's horizontal center; the tooltip
// window centers its bubble under this point itself.
let pos = trayIcon.mapToGlobal(trayIcon.width / 2, trayIcon.height + Theme.geometry.spacing / 2);
trayIcon.tooltipWindow.showAt(pos.x, pos.y, trayIcon.tooltip);
}
}
onExited: {
if (trayIcon.tooltipWindow) trayIcon.tooltipWindow.hide();
}
}
}
}