headsetcontrol-battery-widget/package/contents/ui/main.qml

77 lines
2.6 KiB
QML
Raw Normal View History

2024-03-07 11:44:01 +00:00
import QtQuick
import QtQuick.Layouts
import org.kde.plasma.plasmoid
import org.kde.kirigami as Kirigami
import org.kde.plasma.plasma5support as Plasma5Support
2024-01-17 19:06:16 +00:00
2024-03-07 11:44:01 +00:00
import org.kde.notification
2024-01-25 15:21:39 +00:00
2024-01-20 02:03:58 +00:00
import "./lib/helper.js" as Helper
2024-03-07 11:44:01 +00:00
PlasmoidItem {
2024-01-17 19:06:16 +00:00
id: main
2024-06-06 23:56:43 +00:00
readonly property int pollingrate: Plasmoid.configuration.pollingrate
readonly property int batteryheight: Plasmoid.configuration.batteryheight
readonly property string colorEmpty: Plasmoid.configuration.colorEmpty
readonly property string colorHalf: Plasmoid.configuration.colorHalf
readonly property string colorFull: Plasmoid.configuration.colorFull
2024-01-22 02:08:31 +00:00
2024-06-06 23:56:43 +00:00
readonly property bool notifications: Plasmoid.configuration.notifications
readonly property int batteryThreshold: Plasmoid.configuration.batteryThreshold
2024-01-25 15:21:39 +00:00
2024-06-06 23:56:43 +00:00
readonly property bool debug_active: Plasmoid.configuration.debug_active
readonly property int debug_charge: Plasmoid.configuration.debug_charge
2024-01-22 02:08:31 +00:00
2024-06-06 23:56:43 +00:00
property string batteryStatus: "BATTERY_UNAVAILABLE"
property int batteryPercent: 0
property bool notification_sent: false
2024-01-17 19:06:16 +00:00
2024-03-07 11:44:01 +00:00
Plasma5Support.DataSource {
2024-01-17 19:06:16 +00:00
id: hsSource
engine: "executable"
2024-06-06 23:56:43 +00:00
connectedSources: ["headsetcontrol -o json"]
2024-01-19 00:54:33 +00:00
interval: pollingrate * 1e3
2024-06-06 23:56:43 +00:00
onNewData: (_, data) => {
const res = JSON.parse(data.stdout);
batteryStatus = res.devices[0].battery.status;
batteryPercent = res.devices[0].battery.level;
if(debug_active) {
batteryStatus = "BATTERY_AVAILABLE";
2024-01-25 15:21:39 +00:00
batteryPercent = debug_charge;
2024-06-06 23:56:43 +00:00
}
2024-01-25 15:21:39 +00:00
// send notification if necessary
if(batteryPercent <= batteryThreshold && batteryPercent >= 0 && !notification_sent && notifications) {
notification_sent = true;
notification.text = "Battery Level is low (" + batteryPercent + "%)";
notification.title = "Headset Battery Level Alert";
notification.iconName = "notification-battery-low";
notification.sendEvent();
}
2024-01-25 15:53:07 +00:00
// reset notifications if battery level is over batteryThreshold
if((batteryPercent > batteryThreshold || batteryPercent < 0) && notification_sent && notifications) {
2024-01-25 15:21:39 +00:00
notification_sent = false;
2024-01-25 15:53:07 +00:00
}
2024-01-25 15:21:39 +00:00
return;
2024-01-17 19:06:16 +00:00
}
}
2024-06-06 23:56:43 +00:00
toolTipMainText: "battery level: " + Helper.batteryLevel(batteryStatus, batteryPercent)
2024-03-07 11:44:01 +00:00
toolTipSubText: "polling rate: " + pollingrate + " seconds"
preferredRepresentation: compactRepresentation
fullRepresentation: FullRepresentation{}
compactRepresentation: CompactRepresentation{}
2024-01-25 15:21:39 +00:00
Notification {
id: notification
componentName: "plasma_workspace"
eventId: "notification"
2024-01-25 15:53:07 +00:00
flags: Notification.DefaultEvent
urgency: Notification.CriticalUrgency
2024-01-25 15:21:39 +00:00
}
2024-01-17 19:06:16 +00:00
}