Merge pull request #62 from quadbyte/battery-widget-rework

Battery widget rework
This commit is contained in:
Lysec 2025-07-31 15:01:26 +02:00 committed by GitHub
commit 9632abc542
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 122 additions and 100 deletions

View file

@ -1,8 +1,9 @@
import QtQuick import QtQuick
import Quickshell
import Quickshell.Services.UPower import Quickshell.Services.UPower
import QtQuick.Layouts import QtQuick.Layouts
import qs.Settings
import qs.Components import qs.Components
import qs.Settings
Item { Item {
id: batteryWidget id: batteryWidget
@ -15,61 +16,42 @@ Item {
// Choose icon based on charge and charging state // Choose icon based on charge and charging state
function batteryIcon() { function batteryIcon() {
if (!show) return ""; if (!show)
return "";
// Show charging icons with lightning when charging if (charging)
if (charging) { return "battery_android_bolt";
if (percent >= 95) return "battery_charging_full";
if (percent >= 80) return "battery_charging_80";
if (percent >= 60) return "battery_charging_60";
if (percent >= 50) return "battery_charging_50";
if (percent >= 30) return "battery_charging_30";
if (percent >= 20) return "battery_charging_20";
return "battery_charging_20"; // Use charging_20 for very low battery
}
// Regular battery icons when not charging if (percent >= 95)
if (percent >= 95) return "battery_full"; return "battery_android_full";
if (percent >= 80) return "battery_80";
if (percent >= 60) return "battery_60"; var step = Math.round(percent / (100 / 6));
if (percent >= 50) return "battery_50"; return "battery_android_" + step
if (percent >= 30) return "battery_30";
if (percent >= 20) return "battery_20";
return "battery_alert";
} }
visible: isReady && battery.isLaptopBattery visible: isReady && battery.isLaptopBattery
width: 22 width: pill.width
height: 36 height: pill.height
RowLayout { PillIndicator {
anchors.fill: parent id: pill
spacing: 4 icon: batteryIcon()
visible: show text: Math.round(batteryWidget.percent) + "%"
Item { pillColor: Theme.surfaceVariant
height: 22 iconCircleColor: Theme.accentPrimary
width: 22 textColor: charging ? Theme.accentPrimary : Theme.textPrimary
Text {
text: batteryIcon()
font.family: "Material Symbols Outlined"
font.pixelSize: 14
color: charging ? Theme.accentPrimary : Theme.textPrimary
verticalAlignment: Text.AlignVCenter
anchors.centerIn: parent
}
MouseArea { MouseArea {
id: batteryMouseArea
anchors.fill: parent anchors.fill: parent
hoverEnabled: true hoverEnabled: true
onEntered: batteryWidget.containsMouse = true onEntered: {
onExited: batteryWidget.containsMouse = false pill.show();
cursorShape: Qt.PointingHandCursor batteryTooltip.tooltipVisible = true;
}
onExited: {
pill.hide();
batteryTooltip.tooltipVisible = false;
} }
} }
}
property bool containsMouse: false
StyledTooltip { StyledTooltip {
id: batteryTooltip id: batteryTooltip
text: { text: {
@ -88,8 +70,9 @@ Item {
} }
return lines.join("\n"); return lines.join("\n");
} }
tooltipVisible: batteryWidget.containsMouse tooltipVisible: false
targetItem: batteryWidget targetItem: pill
delay: 200 delay: 1500
}
} }
} }

View file

@ -13,6 +13,7 @@ Item {
property bool isSettingBrightness: false property bool isSettingBrightness: false
property bool hasPendingSet: false property bool hasPendingSet: false
property int pendingSetValue: -1 property int pendingSetValue: -1
property bool firstChange: true
width: pill.width width: pill.width
height: pill.height height: pill.height
@ -30,11 +31,17 @@ Item {
previousBrightness = brightness previousBrightness = brightness
brightness = val brightness = val
pill.text = brightness + "%" pill.text = brightness + "%"
if (firstChange) {
firstChange = false;
}
else {
pill.show() pill.show()
} }
} }
} }
} }
}
function getBrightness() { function getBrightness() {
if (isSettingBrightness) { if (isSettingBrightness) {
@ -94,14 +101,19 @@ Item {
iconCircleColor: Theme.accentPrimary iconCircleColor: Theme.accentPrimary
iconTextColor: Theme.backgroundPrimary iconTextColor: Theme.backgroundPrimary
textColor: Theme.textPrimary textColor: Theme.textPrimary
MouseArea { MouseArea {
anchors.fill: parent anchors.fill: parent
hoverEnabled: true hoverEnabled: true
onEntered: { onEntered: {
getBrightness() getBrightness()
brightnessTooltip.tooltipVisible = true brightnessTooltip.tooltipVisible = true
pill.show()
}
onExited: {
brightnessTooltip.tooltipVisible = false
pill.hide()
} }
onExited: brightnessTooltip.tooltipVisible = false
onWheel: function(wheel) { onWheel: function(wheel) {
const delta = wheel.angleDelta.y > 0 ? 5 : -5 const delta = wheel.angleDelta.y > 0 ? 5 : -5
@ -114,14 +126,11 @@ Item {
text: "Brightness: " + brightness + "%" text: "Brightness: " + brightness + "%"
tooltipVisible: false tooltipVisible: false
targetItem: pill targetItem: pill
delay: 200 delay: 1500
} }
} }
Component.onCompleted: { Component.onCompleted: {
getBrightness() getBrightness()
if (brightness >= 0) {
pill.show()
}
} }
} }

View file

@ -8,6 +8,7 @@ Item {
id: volumeDisplay id: volumeDisplay
property var shell property var shell
property int volume: 0 property int volume: 0
property bool firstChange: true
width: pillIndicator.width width: pillIndicator.width
height: pillIndicator.height height: pillIndicator.height
@ -23,13 +24,14 @@ Item {
iconCircleColor: Theme.accentPrimary iconCircleColor: Theme.accentPrimary
iconTextColor: Theme.backgroundPrimary iconTextColor: Theme.backgroundPrimary
textColor: Theme.textPrimary textColor: Theme.textPrimary
autoHide: true
StyledTooltip { StyledTooltip {
id: volumeTooltip id: volumeTooltip
text: "Volume: " + volume + "%\nScroll up/down to change volume.\nLeft click to open the input/output selection." text: "Volume: " + volume + "%\nScroll up/down to change volume.\nLeft click to open the input/output selection."
tooltipVisible: !ioSelector.visible && volumeDisplay.containsMouse tooltipVisible: !ioSelector.visible && volumeDisplay.containsMouse
targetItem: pillIndicator targetItem: pillIndicator
delay: 200 delay: 1500
} }
MouseArea { MouseArea {
@ -57,16 +59,21 @@ Item {
pillIndicator.icon = shell.defaultAudioSink && shell.defaultAudioSink.audio && shell.defaultAudioSink.audio.muted pillIndicator.icon = shell.defaultAudioSink && shell.defaultAudioSink.audio && shell.defaultAudioSink.audio.muted
? "volume_off" ? "volume_off"
: (volume === 0 ? "volume_off" : (volume < 30 ? "volume_down" : "volume_up")); : (volume === 0 ? "volume_off" : (volume < 30 ? "volume_down" : "volume_up"));
if (firstChange) {
firstChange = false
}
else {
pillIndicator.show(); pillIndicator.show();
} }
} }
} }
} }
}
Component.onCompleted: { Component.onCompleted: {
if (shell && shell.volume !== undefined) { if (shell && shell.volume !== undefined) {
volume = Math.max(0, Math.min(100, shell.volume)); volume = Math.max(0, Math.min(100, shell.volume));
pillIndicator.show();
} }
} }
@ -75,8 +82,16 @@ Item {
hoverEnabled: true hoverEnabled: true
acceptedButtons: Qt.NoButton acceptedButtons: Qt.NoButton
propagateComposedEvents: true propagateComposedEvents: true
onEntered: volumeDisplay.containsMouse = true onEntered: {
onExited: volumeDisplay.containsMouse = false volumeDisplay.containsMouse = true
pillIndicator.autoHide = false;
pillIndicator.show()
}
onExited: {
volumeDisplay.containsMouse = false
pillIndicator.autoHide = true;
pillIndicator.hide()
}
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onWheel: (wheel) => { onWheel: (wheel) => {
if (!shell) return; if (!shell) return;

View file

@ -15,6 +15,7 @@ Item {
property int pillHeight: 22 property int pillHeight: 22
property int iconSize: 22 property int iconSize: 22
property int pillPaddingHorizontal: 14 property int pillPaddingHorizontal: 14
property bool autoHide: false
// Internal state // Internal state
property bool showPill: false property bool showPill: false
@ -24,8 +25,8 @@ Item {
readonly property int pillOverlap: iconSize / 2 readonly property int pillOverlap: iconSize / 2
readonly property int maxPillWidth: Math.max(1, textItem.implicitWidth + pillPaddingHorizontal * 2 + pillOverlap) readonly property int maxPillWidth: Math.max(1, textItem.implicitWidth + pillPaddingHorizontal * 2 + pillOverlap)
signal shown() signal shown
signal hidden() signal hidden
width: iconSize + (showPill ? maxPillWidth - pillOverlap : 0) width: iconSize + (showPill ? maxPillWidth - pillOverlap : 0)
height: pillHeight height: pillHeight
@ -54,11 +55,17 @@ Item {
Behavior on width { Behavior on width {
enabled: showAnim.running || hideAnim.running enabled: showAnim.running || hideAnim.running
NumberAnimation { duration: 250; easing.type: Easing.OutCubic } NumberAnimation {
duration: 250
easing.type: Easing.OutCubic
}
} }
Behavior on opacity { Behavior on opacity {
enabled: showAnim.running || hideAnim.running enabled: showAnim.running || hideAnim.running
NumberAnimation { duration: 250; easing.type: Easing.OutCubic } NumberAnimation {
duration: 250
easing.type: Easing.OutCubic
}
} }
} }
@ -73,7 +80,10 @@ Item {
anchors.right: parent.right anchors.right: parent.right
Behavior on color { Behavior on color {
ColorAnimation { duration: 200; easing.type: Easing.InOutQuad } ColorAnimation {
duration: 200
easing.type: Easing.InOutQuad
}
} }
Text { Text {
@ -106,11 +116,11 @@ Item {
easing.type: Easing.OutCubic easing.type: Easing.OutCubic
} }
onStarted: { onStarted: {
showPill = true showPill = true;
} }
onStopped: { onStopped: {
delayedHideAnim.start() delayedHideAnim.start();
shown() shown();
} }
} }
@ -118,8 +128,13 @@ Item {
SequentialAnimation { SequentialAnimation {
id: delayedHideAnim id: delayedHideAnim
running: false running: false
PauseAnimation { duration: 2500 } PauseAnimation {
ScriptAction { script: if (shouldAnimateHide) hideAnim.start() } duration: 2500
}
ScriptAction {
script: if (shouldAnimateHide)
hideAnim.start()
}
} }
// Hide animation // Hide animation
@ -143,27 +158,27 @@ Item {
easing.type: Easing.InCubic easing.type: Easing.InCubic
} }
onStopped: { onStopped: {
showPill = false showPill = false;
shouldAnimateHide = false shouldAnimateHide = false;
hidden() hidden();
} }
} }
// Exposed functions // Exposed functions
function show() { function show() {
if (!showPill) { if (!showPill) {
shouldAnimateHide = true shouldAnimateHide = autoHide;
showAnim.start() showAnim.start();
} else { } else {
// Reset hide timer if already shown // Reset hide timer if already shown
hideAnim.stop() hideAnim.stop();
delayedHideAnim.restart() delayedHideAnim.restart();
} }
} }
function hide() { function hide() {
if (showPill) { if (showPill) {
hideAnim.start() hideAnim.start();
} }
} }
} }