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

129 lines
4.1 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-06-08 16:41:43 +00:00
import org.kde.plasma.core as PlasmaCore
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-06-08 16:41:43 +00:00
id: root
Plasmoid.status: PlasmaCore.Types.ActiveStatus
2024-01-17 19:06:16 +00:00
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-06-08 16:41:43 +00:00
property variant deviceList: []
property int activeDevice: 0
//property variant capabilities: []
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);
2024-06-08 16:41:43 +00:00
if(res.device_count === 0) {
batteryStatus = "HEADSET_UNAVAILABLE";
batteryPercent = 0;
root.Plasmoid.status = PlasmaCore.Types.PassiveStatus;
return;
}
//if(capabilities.length != res.devices[activeDevice].capabilities_str.length)
// capabilities = res.devices[activeDevice].capabilities_str;
deviceList = res.devices.map(e => e.product);
batteryPercent = res.devices[activeDevice].battery.level;
batteryStatus = batteryPercent > 0 ? res.devices[activeDevice].battery.status : "BATTERY_UNAVAILABLE";
// debug deshmug
2024-06-06 23:56:43 +00:00
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
2024-06-08 16:41:43 +00:00
// hide trayicon if headset isn't connected
root.Plasmoid.status = batteryStatus == "BATTERY_UNAVAILABLE"
? PlasmaCore.Types.PassiveStatus
: PlasmaCore.Types.ActiveStatus;
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
compactRepresentation: CompactRepresentation{}
2024-06-08 16:41:43 +00:00
fullRepresentation: Item{}
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-06-08 16:41:43 +00:00
Instantiator {
model: deviceList
delegate: PlasmaCore.Action {
text: modelData
icon.name: "audio-headphones-symbolic"
/*onTriggered: {
console.log("headset lel");
}*/
}
onObjectAdded: (index, object) => {
Plasmoid.contextualActions.splice(Plasmoid.contextualActions.indexOf(separator2), 0, object)
}
onObjectRemoved: (index, object) => {
Plasmoid.contextualActions.splice(Plasmoid.contextualActions.indexOf(object), 1)
}
}
Plasmoid.contextualActions: [
PlasmaCore.Action {
id: separator1
isSeparator: true
},
PlasmaCore.Action {
id: separator2
isSeparator: true
}
]
2024-01-17 19:06:16 +00:00
}