quickshell/modules/bar/Battery.qml

125 lines
4.5 KiB
QML
Raw Normal View History

import Quickshell.Services.UPower
import QtQuick
2026-01-19 00:06:43 +01:00
import Quickshell.Widgets
import Qt5Compat.GraphicalEffects
import Quickshell
import qs
import qs.reusables
import qs.settings
Loader {
id: batLoader
active: UPower.displayDevice.isLaptopBattery
2026-01-18 23:18:42 +01:00
anchors.verticalCenter: parent.verticalCenter
2026-01-18 23:18:42 +01:00
sourceComponent: Rectangle {
id: container
2026-01-19 22:20:11 +01:00
border.color: clickHandler.containsMouse ? Colors.color8 : Colors.color7
border.width: 1
2026-01-18 23:18:42 +01:00
radius: implicitHeight / 2
color: Colors.color0
anchors.verticalCenter: parent.verticalCenter
implicitWidth: root.implicitWidth + 20
implicitHeight: Settings.config.barHeight - 10
Item {
id: root
2026-01-19 00:06:43 +01:00
anchors.centerIn: parent
2026-01-16 14:44:10 +01:00
2026-01-18 23:18:42 +01:00
property bool frame1: UPower.displayDevice.percentage <= 0.16
property bool frame2: UPower.displayDevice.percentage < 0.32
property bool frame3: UPower.displayDevice.percentage < 0.48
property bool frame4: UPower.displayDevice.percentage < 0.74
property bool frame5: UPower.displayDevice.percentage < 0.90
property bool frame6: UPower.displayDevice.percentage <= 1
2026-01-16 14:44:10 +01:00
2026-01-18 23:18:42 +01:00
function getBatteryIcon() {
if (UPower.displayDevice.state == UPowerDeviceState.Charging) {
return "battery_android_frame_bolt";
}
if (frame1) {
return "battery_android_frame_1";
}
if (frame2) {
return "battery_android_frame_2";
}
if (frame3) {
return "battery_android_frame_3";
}
if (frame4) {
return "battery_android_frame_4";
}
if (frame5) {
return "battery_android_frame_5";
}
if (frame6) {
return "battery_android_frame_full";
}
2026-01-16 14:44:10 +01:00
}
2026-01-19 00:06:43 +01:00
function getProfileIcon() {
if (PowerProfiles.profile.toString() == "2") {
return "power-profile-performance-symbolic";
}
if (PowerProfiles.profile.toString() == "1") {
return "power-profile-balanced-symbolic";
}
if (PowerProfiles.profile.toString() == "0") {
return "power-profile-power-saver-symbolic";
}
}
2026-01-16 14:44:10 +01:00
2026-01-18 23:18:42 +01:00
implicitWidth: batRow.width
implicitHeight: Settings.config.barHeight
2026-01-18 23:18:42 +01:00
Row {
id: batRow
anchors.verticalCenter: parent.verticalCenter
spacing: 5
CustomText {
id: batText
text: Math.round(UPower.displayDevice.percentage * 100) + "%"
}
CustomIcon {
id: batIcon
text: root.getBatteryIcon()
}
2026-01-19 00:06:43 +01:00
Item {
anchors.verticalCenter: parent.verticalCenter
implicitWidth: 12
implicitHeight: 12
IconImage {
id: rawProfileIcon
anchors.fill: parent
source: Quickshell.iconPath(root.getProfileIcon())
visible: false
2026-01-19 00:06:43 +01:00
}
ColorOverlay {
anchors.fill: parent
source: rawProfileIcon
color: Colors.foreground
2026-01-19 00:06:43 +01:00
}
}
}
MouseArea {
2026-01-19 22:20:11 +01:00
id: clickHandler
2026-01-19 00:06:43 +01:00
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
acceptedButtons: Qt.LeftButton | Qt.RightButton
2026-01-19 22:20:11 +01:00
hoverEnabled: true
2026-01-19 00:06:43 +01:00
onClicked: mouse => {
const modes = [PowerProfile.PowerSaver, PowerProfile.Balanced, PowerProfile.Performance];
let current = PowerProfiles.profile;
let currentIndex = modes.indexOf(current);
let nextIndex = (currentIndex + 1) % modes.length;
let prevIndex = (currentIndex - 1) % modes.length;
if (mouse.button == Qt.LeftButton)
PowerProfiles.profile = modes[nextIndex];
if (mouse.button == Qt.RightButton)
PowerProfiles.profile = modes[prevIndex];
}
}
}
2026-01-16 15:15:44 +01:00
}
}