Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions plugins/controller-battery-bar/BarWidget.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
pragma ComponentBehavior: Bound

import QtQuick
import QtQuick.Shapes
import Quickshell.Services.UPower
import Caelestia.Config
import qs.components
import qs.components.controls
import qs.services
import "."

// Bar widget: a ring that empties as the controller battery drains, with the
// level inside. Green above 30%, amber down to 10%, red below. Clicking opens
// a popout with the details of every connected controller.
StyledRect {
id: root

property var bar: null
property var screen: null
property var popouts: null

// Same thresholds the desktop widget uses, plus the green band the bar
// wants for "plenty of battery left".
readonly property real warnLevel: 0.3
readonly property real lowLevel: 0.1

readonly property bool horizontal: bar ? bar.isHorizontal !== false : true
readonly property int thickness: bar && bar.barThickness ? bar.barThickness : Tokens.sizes.bar.innerWidth
// Sized against the bar's other icons (about 0.42 of the thickness) instead
// of using the whole bar height, which made it look oversized next to them.
readonly property real ringSize: Math.max(16, Math.round(root.thickness * 0.55))
readonly property real ringThickness: Math.max(1.5, root.ringSize / 11)
readonly property int digits: String(Math.round(root.level * 100)).length
// The number has to fit inside the ring, so it shrinks with the digit count
// ("100" is three characters) instead of using a fixed size that overflows.
readonly property real ringFontSize: Math.max(5, Math.min(Math.round(root.ringSize * 0.40), Math.floor((root.ringSize - root.ringThickness * 2 - 2) / (root.digits * 0.75))))
readonly property var controllers: devices.controllers
readonly property var worst: controllers.length > 0 ? controllers.reduce((a, b) => (b.percentage ?? 0) < (a.percentage ?? 0) ? b : a) : null
readonly property real level: worst ? (worst.percentage ?? 0) : 1
readonly property bool pluggedIn: controllers.some(d => devices.pluggedInFor(d))
readonly property color levelColour: root.pluggedIn ? "#4ade80" : root.level > root.warnLevel ? "#4ade80" : root.level > root.lowLevel ? "#fbbf24" : Colours.palette.m3error
readonly property bool popoutOpen: popouts ? (popouts.currentName === "controllerBatteryBar" && popouts.hasCurrent === true) : false

// Eased copy of the level so the ring glides between readings.
property real shownLevel: level

function centerAlongBar(): real {
const p = root.mapToItem(null, root.width / 2, root.height / 2);
const vertical = Config.bar.position === "left" || Config.bar.position === "right";
return vertical ? p.y : p.x;
}

function setPopout(open: bool): void {
if (!popouts)
return;
if (open)
popouts.currentCenter = root.centerAlongBar();
popouts.currentName = open ? "controllerBatteryBar" : "";
popouts.hasCurrent = open;
}

// Controllers are off most of the time, so the widget only takes a slot
// while one is actually connected.
visible: controllers.length > 0
implicitWidth: root.horizontal ? (root.ringSize + Tokens.padding.small) : root.thickness
implicitHeight: root.horizontal ? root.thickness : (root.ringSize + Tokens.padding.small)
radius: Tokens.rounding.full
color: Qt.alpha(Colours.tPalette.m3surfaceContainer, 0)

Behavior on shownLevel {
NumberAnimation {
duration: 400
easing.type: Easing.OutCubic
}
}

Devices {
id: devices
}

StateLayer {
color: root.levelColour

onClicked: root.setPopout(!root.popoutOpen)
}

Shape {
id: ring

anchors.centerIn: parent
width: root.ringSize
height: root.ringSize
preferredRendererType: Shape.CurveRenderer

ShapePath {
strokeColor: Colours.palette.m3surfaceContainerHighest
strokeWidth: root.ringThickness
fillColor: "transparent"

PathAngleArc {
centerX: ring.width / 2
centerY: ring.height / 2
radiusX: (ring.width - root.ringThickness) / 2
radiusY: (ring.height - root.ringThickness) / 2
startAngle: -90
sweepAngle: 360
}
}

ShapePath {
strokeColor: root.levelColour
strokeWidth: root.ringThickness
fillColor: "transparent"
capStyle: ShapePath.RoundCap

PathAngleArc {
centerX: ring.width / 2
centerY: ring.height / 2
radiusX: (ring.width - root.ringThickness) / 2
radiusY: (ring.height - root.ringThickness) / 2
startAngle: -90
sweepAngle: Math.max(0.5, Math.min(359.9, 360 * root.shownLevel))
}
}
}

StyledText {
id: label

anchors.centerIn: parent
width: root.ringSize - root.ringThickness * 2 - 2
horizontalAlignment: Text.AlignHCenter
text: Math.round(root.level * 100)
color: root.levelColour
font.pixelSize: root.ringFontSize
font.weight: Font.DemiBold
}
}
55 changes: 55 additions & 0 deletions plugins/controller-battery-bar/Devices.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
pragma ComponentBehavior: Bound

import QtQuick
import Quickshell.Services.UPower

// Controller detection for the bar widget.
//
// UPower only exposes a controller while the kernel has a battery for it: a
// wireless Steam controller disappears from the list while the Steam client is
// running, because Steam takes the device over.
QtObject {
id: root

// Also list mice, keyboards, headsets...
property bool showAllPeripherals: false

// First matching rule wins. "match" is tested against the device's sysfs
// path and model. These rules also decide what counts as a controller, so a
// device matching none of them is ignored unless showAllPeripherals is on.
property var nameRules: [
{ match: /steam/i, name: "Steam Controller" },
{ match: /ps-controller|dualsense|dualshock/i, name: "PS5 Controller" },
{ match: /controller|gamepad|joystick/i, name: "Controller" }
]

readonly property var controllers: UPower.devices.values.filter(d => root.isController(d))

function isController(d: var): bool {
if (d.isLaptopBattery || d.type === UPowerDeviceType.LinePower || !d.isPresent)
return false;
return root.showAllPeripherals || d.type === UPowerDeviceType.GamingInput || root.ruleFor(d) !== null;
}

function ruleFor(d: var): var {
const id = (d.nativePath ?? "") + " " + (d.model ?? "");
return root.nameRules.find(r => r.match.test(id)) ?? null;
}

function labelFor(d: var): string {
return root.ruleFor(d)?.name ?? (d.model || qsTr("Controller"));
}

function percentFor(d: var): int {
return Math.round((d.percentage ?? 0) * 100);
}

function chargingFor(d: var): bool {
return d.state === UPowerDeviceState.Charging || d.state === UPowerDeviceState.PendingCharge;
}

// A docked controller is not charging but is still plugged in.
function pluggedInFor(d: var): bool {
return root.chargingFor(d) || d.state === UPowerDeviceState.FullyCharged;
}
}
25 changes: 25 additions & 0 deletions plugins/controller-battery-bar/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
MIT License

Copyright (c) 2026 Alejandro Di Paola

Controller detection is derived from "Controller Battery" by Erik Mettner
(verbrannter-toast), https://github.com/verbrannter-toast, Copyright (c) 2026
Erik Mettner, also released under the MIT License.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading
Loading