From 09e95226237bc10756b12cb45df4cec106b0d862 Mon Sep 17 00:00:00 2001 From: Henry-Hiles Date: Wed, 18 Nov 2026 00:19:01 -0500 Subject: [PATCH] add brightness and volume sliders --- README.md | 10 ++++-- package.nix | 4 +-- src/Lock.qml | 2 +- src/Shade.qml | 43 +++++++++++++++++++++- src/Slider.qml | 60 +++++++++++++++++++++++++++++++ src/Status.qml | 97 +++++++++++++++++++++++++++++++++++++++++++++++--- src/qmldir | 1 + 7 files changed, 206 insertions(+), 11 deletions(-) create mode 100644 src/Slider.qml diff --git a/README.md b/README.md index bddda7f..0f0474b 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ down from it. The bar shows the time, cellular, Wi-Fi, Bluetooth and battery. The sheet has: +- Brightness and volume sliders. Tap the speaker icon to mute, and the arrow next to it picks the + output, out of the ones that are plugged in. - Wi-Fi, Bluetooth, cellular and mobile data toggles. The arrow on Wi-Fi opens `nmgui`, and on Bluetooth it opens `adw-bluetooth`. - Location, which decides whether apps get your location through GeoClue. The bar shows an icon @@ -45,14 +47,16 @@ spawn-at-startup "quadshell" The lockscreen shows whatever image swaybg is showing (its `-i` argument), blurred. -It brings `nmcli`, `mmcli`, `nmgui` and `adw-bluetooth` with it. The system needs: +It brings `nmcli`, `mmcli`, `pw-dump` and `nmgui` with it. The system needs: -- niri, NetworkManager and ModemManager running -- systemd-logind, which the flashlight uses to set the LED without root +- niri, NetworkManager, ModemManager and PipeWire running +- systemd-logind, which the flashlight and brightness slider use to set the LED and backlight without + root - The `login` PAM service, which checks the lockscreen's password - The Papirus-Dark icon theme and the Inter font - An on-screen keyboard that uses text-input-v3, like Stevia, to type the password - No other notification daemon, like swaync or mako, as only one can run at a time +- adw-bluetooth for the Bluetooth arrow, with its daemon: `services.adw-bluetooth.enable = true;` - GeoClue, with quadshell as its agent, for the location toggle: ```nix diff --git a/package.nix b/package.nix index 2986b85..1b4c6de 100644 --- a/package.nix +++ b/package.nix @@ -6,8 +6,8 @@ glib, networkmanager, modemmanager, + pipewire, nmgui, - adw-bluetooth, coreutils, gawk, gnugrep, @@ -32,8 +32,8 @@ writeShellApplication { runtimeInputs = [ networkmanager modemmanager + pipewire # pw-dump, for which outputs are plugged in nmgui - adw-bluetooth coreutils gawk gnugrep diff --git a/src/Lock.qml b/src/Lock.qml index b38987e..57951b4 100644 --- a/src/Lock.qml +++ b/src/Lock.qml @@ -49,7 +49,7 @@ Scope { } LockSurface { - lock: root + lock: root // qmllint disable incompatible-type } } diff --git a/src/Shade.qml b/src/Shade.qml index 1391ad4..fd702fc 100644 --- a/src/Shade.qml +++ b/src/Shade.qml @@ -17,7 +17,7 @@ Item { // 0 is closed, 1 is open property real progress: 0 readonly property bool sheetShown: progress > 0 - // Which row of options is dropped down: "", "rotate" or "power" + // Which row of options is dropped down: "", "rotate", "output" or "power" property string expanded: "" function open() { @@ -159,6 +159,47 @@ Item { ] } + Slider { + visible: Status.backlight !== "" + Layout.fillWidth: true + icon: Status.brightnessIcon + value: Status.brightness + onMoved: value => Status.setBrightness(value) + } + + RowLayout { + visible: Status.sink !== null + Layout.fillWidth: true + spacing: 8 + + Slider { + Layout.fillWidth: true + icon: Status.volumeIcon + value: Status.volume + active: !Status.muted + iconTappable: true + onMoved: value => Status.setVolume(value) + onIconTapped: Status.setMuted(!Status.muted) + } + + RoundButton { + visible: Status.sinks.length > 1 + icon: root.expanded === "output" ? "go-up-symbolic" : "go-down-symbolic" + active: root.expanded === "output" + onClicked: root.expanded = root.expanded === "output" ? "" : "output" + } + } + + Options { + visible: root.expanded === "output" + Layout.fillWidth: true + model: Status.sinks.map(sink => ({ + name: sink.description || sink.nickname || sink.name, + active: sink === Status.sink, + action: () => Status.setSink(sink), + })) + } + GridLayout { Layout.fillWidth: true columns: 2 diff --git a/src/Slider.qml b/src/Slider.qml new file mode 100644 index 0000000..3106bc4 --- /dev/null +++ b/src/Slider.qml @@ -0,0 +1,60 @@ +import QtQuick + +Rectangle { + id: slider + + property string icon + // From 0 to 1 + property real value + // Greys out the fill, like when the volume is muted + property bool active: true + // Tapping the icon emits iconTapped, rather than setting the value + property bool iconTappable: false + + signal moved(real value) + signal iconTapped + + implicitHeight: 40 + radius: height / 2 + color: Theme.surface + + Rectangle { + width: slider.height + slider.value * (slider.width - slider.height) + height: parent.height + radius: slider.radius + color: slider.active ? Theme.accent : Theme.surfaceHigh + } + + ShellIcon { + anchors { + left: parent.left + leftMargin: (slider.height - implicitSize) / 2 + verticalCenter: parent.verticalCenter + } + name: slider.icon + implicitSize: 18 + } + + MouseArea { + anchors.fill: parent + preventStealing: true + + property bool onIcon + + function set(x) { + slider.moved(Math.max(0, Math.min(1, (x - slider.height / 2) / (slider.width - slider.height)))); + } + + onPressed: mouse => { + onIcon = slider.iconTappable && mouse.x < slider.height; + if (!onIcon) set(mouse.x); + } + onPositionChanged: mouse => { + onIcon = false; + set(mouse.x); + } + onReleased: { + if (onIcon) slider.iconTapped(); + } + } +} diff --git a/src/Status.qml b/src/Status.qml index d094302..fb4d7f9 100644 --- a/src/Status.qml +++ b/src/Status.qml @@ -5,6 +5,7 @@ import Quickshell.Io import Quickshell.Networking import Quickshell.Bluetooth import Quickshell.Services.UPower +import Quickshell.Services.Pipewire Singleton { id: root @@ -33,6 +34,10 @@ Singleton { property bool dataActive property real cellularSignal property bool flashlight + // The first backlight, and its brightness from 0 to 1 + property string backlight + property int maxBrightness + property real brightness // From quadshell-location-agent, which only comes with the package property bool locationAvailable property bool location @@ -45,6 +50,23 @@ Singleton { readonly property string flashlightLed: "white:flash" + readonly property string brightnessIcon: brightness > 0.67 ? "display-brightness-high-symbolic" + : brightness > 0.33 ? "display-brightness-medium-symbolic" + : "display-brightness-low-symbolic" + + readonly property var sink: Pipewire.defaultAudioSink + readonly property var allSinks: Pipewire.nodes.values.filter(n => n.isSink && !n.isStream && n.audio) // qmllint disable missing-property + property var unavailableOutputs: [] + // WirePlumber won't switch to an unavailable one, it just stays on the current sink + readonly property var sinks: allSinks.filter(n => + !unavailableOutputs.includes(`${n.properties["device.id"]}:${n.properties["card.profile.device"]}`)) + readonly property real volume: sink?.audio?.volume ?? 0 + readonly property bool muted: sink?.audio?.muted ?? false + readonly property string volumeIcon: muted || volume === 0 ? "audio-volume-muted-symbolic" + : volume > 0.67 ? "audio-volume-high-symbolic" + : volume > 0.33 ? "audio-volume-medium-symbolic" + : "audio-volume-low-symbolic" + function signalLevel(strength) { if (strength > 0.8) return "excellent"; if (strength > 0.6) return "good"; @@ -73,12 +95,38 @@ Singleton { "sh", dataConnection, on ? "yes" : "no", on ? "up" : "down"]); } + // logind lets the session set these without root + function logindBrightness(subsystem, device, value) { + return ["sh", "-c", `busctl call org.freedesktop.login1 \ + "/org/freedesktop/login1/session/$(loginctl show-user "$USER" -p Display --value)" \ + org.freedesktop.login1.Session SetBrightness ssu "$1" "$2" "$3"`, + "sh", subsystem, device, String(value)]; + } + function setFlashlight(on) { flashlight = on; - run(["sh", "-c", `busctl call org.freedesktop.login1 \ - "/org/freedesktop/login1/session/$(loginctl show-user "$USER" -p Display --value)" \ - org.freedesktop.login1.Session SetBrightness ssu leds "$1" "$2"`, - "sh", flashlightLed, on ? "255" : "0"]); + run(logindBrightness("leds", flashlightLed, on ? 255 : 0)); + } + + function setBrightness(value) { + if (!backlight) return; + // All the way down would turn the screen off + brightness = Math.max(0.02, value); + if (!brightnessSetter.running) brightnessSetter.start(); + } + + function setVolume(value) { + if (!sink?.audio) return; + sink.audio.volume = value; + if (value > 0) sink.audio.muted = false; + } + + function setMuted(on) { + if (sink?.audio) sink.audio.muted = on; + } + + function setSink(node) { + Pipewire.preferredDefaultAudioSink = node; } function setLocation(on) { @@ -117,6 +165,25 @@ Singleton { dataActive = values.active === "yes"; cellularSignal = (parseInt(values.signal) || 0) / 100; flashlight = (parseInt(values.flashlight) || 0) > 0; + backlight = values.backlight ?? ""; + maxBrightness = parseInt(values.maxBrightness) || 0; + // Not while it's being dragged, as this could be from before + if (maxBrightness > 0 && !brightnessSetter.running) brightness = (parseInt(values.brightness) || 0) / maxBrightness; + + try { + // Like headphones that aren't plugged in, as "device:index", which sinks have as + // device.id and card.profile.device + const unavailable = []; + for (const device of JSON.parse(values.devices || "[]")) { + for (const route of device.info?.params?.EnumRoute ?? []) { + if (route.direction !== "Output" || route.available !== "no") continue; + for (const index of route.devices ?? []) unavailable.push(`${device.id}:${index}`); + } + } + unavailableOutputs = unavailable; + } catch (e) { + console.warn("Couldn't read PipeWire devices:", e); + } try { const outputs = Object.values(JSON.parse(values.outputs)); @@ -141,6 +208,14 @@ Singleton { fi echo "signal=$(mmcli -m any -K 2>/dev/null | awk -F': *' '/signal-quality.value/ { print $2 }')" echo "flashlight=$(cat /sys/class/leds/${root.flashlightLed}/brightness)" + echo "devices=$(pw-dump -N Device | tr -d '\\n')" + for d in /sys/class/backlight/*; do + [ -e "$d/brightness" ] || continue + echo "backlight=$(basename "$d")" + echo "brightness=$(cat "$d/brightness")" + echo "maxBrightness=$(cat "$d/max_brightness")" + break + done echo "outputs=$(niri msg -j outputs)" `] stdout: StdioCollector { @@ -173,6 +248,20 @@ Singleton { } } + // Dragging the slider would start a process for every step, so this sends the latest at most + // this often + Timer { + id: brightnessSetter + interval: 50 + onTriggered: Quickshell.execDetached(root.logindBrightness("backlight", root.backlight, + Math.round(root.brightness * root.maxBrightness))) + } + + // Audio properties are only kept up to date for tracked nodes + PwObjectTracker { + objects: [root.sink, ...root.allSinks] + } + Timer { id: refreshSoon interval: 400 diff --git a/src/qmldir b/src/qmldir index 5998574..b7320d1 100644 --- a/src/qmldir +++ b/src/qmldir @@ -10,4 +10,5 @@ Panel Panel.qml RoundButton RoundButton.qml Shade Shade.qml ShellIcon ShellIcon.qml +Slider Slider.qml Tile Tile.qml