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

129 lines
4.1 KiB
QML

import QtQuick
import QtQuick.Layouts
import org.kde.plasma.plasmoid
import org.kde.kirigami as Kirigami
import org.kde.plasma.plasma5support as Plasma5Support
import org.kde.plasma.core as PlasmaCore
import org.kde.notification
import "./lib/helper.js" as Helper
PlasmoidItem {
id: root
Plasmoid.status: PlasmaCore.Types.ActiveStatus
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
readonly property bool notifications: Plasmoid.configuration.notifications
readonly property int batteryThreshold: Plasmoid.configuration.batteryThreshold
readonly property bool debug_active: Plasmoid.configuration.debug_active
readonly property int debug_charge: Plasmoid.configuration.debug_charge
property string batteryStatus: "BATTERY_UNAVAILABLE"
property int batteryPercent: 0
property bool notification_sent: false
property variant deviceList: []
property int activeDevice: 0
//property variant capabilities: []
Plasma5Support.DataSource {
id: hsSource
engine: "executable"
connectedSources: ["headsetcontrol -o json"]
interval: pollingrate * 1e3
onNewData: (_, data) => {
const res = JSON.parse(data.stdout);
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
if(debug_active) {
batteryStatus = "BATTERY_AVAILABLE";
batteryPercent = debug_charge;
}
// hide trayicon if headset isn't connected
root.Plasmoid.status = batteryStatus == "BATTERY_UNAVAILABLE"
? PlasmaCore.Types.PassiveStatus
: PlasmaCore.Types.ActiveStatus;
// 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();
}
// reset notifications if battery level is over batteryThreshold
if((batteryPercent > batteryThreshold || batteryPercent < 0) && notification_sent && notifications) {
notification_sent = false;
}
return;
}
}
toolTipMainText: "battery level: " + Helper.batteryLevel(batteryStatus, batteryPercent)
toolTipSubText: "polling rate: " + pollingrate + " seconds"
preferredRepresentation: compactRepresentation
compactRepresentation: CompactRepresentation{}
fullRepresentation: Item{}
Notification {
id: notification
componentName: "plasma_workspace"
eventId: "notification"
flags: Notification.DefaultEvent
urgency: Notification.CriticalUrgency
}
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
}
]
}