added contextmenu and hide tray icon

This commit is contained in:
Flummi 2024-06-08 18:41:43 +02:00
parent 3a1a5588d4
commit 83aa943fd9
Signed by: Flummi
GPG Key ID: AA2AEF822A6F4817
3 changed files with 61 additions and 7 deletions

View File

@ -1,12 +1,14 @@
const batteryLevel = (batteryStatus, batteryPercent) => ({ const batteryLevel = (batteryStatus, batteryPercent) => ({
"BATTERY_UNAVAILABLE": "not connected", "BATTERY_UNAVAILABLE": "not connected",
"BATTERY_CHARGING": "charging (" + batteryPercent + "%)" "BATTERY_CHARGING": "charging (" + batteryPercent + "%)",
"HEADSET_UNAVAILABLE": "no headset connected"
})[batteryStatus] ?? batteryPercent + "%"; })[batteryStatus] ?? batteryPercent + "%";
const batteryIcon = batteryStatus => ({ const batteryIcon = batteryStatus => ({
"BATTERY_UNAVAILABLE": "action-unavailable", "BATTERY_UNAVAILABLE": "action-unavailable",
"BATTERY_AVAILABLE": "audio-headphones-symbolic", "BATTERY_AVAILABLE": "audio-headphones-symbolic",
"BATTERY_CHARGING": "reload" "BATTERY_CHARGING": "freon-voltage-symbolic",
"HEADSET_UNAVAILABLE": "dialog-error"
})[batteryStatus] ?? "audio-headphones-symbolic"; })[batteryStatus] ?? "audio-headphones-symbolic";
const hex2rgb = (hex, h = hex.replace('#', '0x')) => ({ r: h >> 16 & 255, g: h >> 8 & 255, b: h & 255 }); const hex2rgb = (hex, h = hex.replace('#', '0x')) => ({ r: h >> 16 & 255, g: h >> 8 & 255, b: h & 255 });

View File

@ -3,13 +3,15 @@ import QtQuick.Layouts
import org.kde.plasma.plasmoid import org.kde.plasma.plasmoid
import org.kde.kirigami as Kirigami import org.kde.kirigami as Kirigami
import org.kde.plasma.plasma5support as Plasma5Support import org.kde.plasma.plasma5support as Plasma5Support
import org.kde.plasma.core as PlasmaCore
import org.kde.notification import org.kde.notification
import "./lib/helper.js" as Helper import "./lib/helper.js" as Helper
PlasmoidItem { PlasmoidItem {
id: main id: root
Plasmoid.status: PlasmaCore.Types.ActiveStatus
readonly property int pollingrate: Plasmoid.configuration.pollingrate readonly property int pollingrate: Plasmoid.configuration.pollingrate
readonly property int batteryheight: Plasmoid.configuration.batteryheight readonly property int batteryheight: Plasmoid.configuration.batteryheight
@ -26,6 +28,10 @@ PlasmoidItem {
property string batteryStatus: "BATTERY_UNAVAILABLE" property string batteryStatus: "BATTERY_UNAVAILABLE"
property int batteryPercent: 0 property int batteryPercent: 0
property bool notification_sent: false property bool notification_sent: false
property variant deviceList: []
property int activeDevice: 0
//property variant capabilities: []
Plasma5Support.DataSource { Plasma5Support.DataSource {
id: hsSource id: hsSource
@ -34,14 +40,32 @@ PlasmoidItem {
interval: pollingrate * 1e3 interval: pollingrate * 1e3
onNewData: (_, data) => { onNewData: (_, data) => {
const res = JSON.parse(data.stdout); const res = JSON.parse(data.stdout);
batteryPercent = res.devices[0].battery.level;
batteryStatus = batteryPercent > 0 ? res.devices[0].battery.status : "BATTERY_UNAVAILABLE";
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) { if(debug_active) {
batteryStatus = "BATTERY_AVAILABLE"; batteryStatus = "BATTERY_AVAILABLE";
batteryPercent = debug_charge; 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 // send notification if necessary
if(batteryPercent <= batteryThreshold && batteryPercent >= 0 && !notification_sent && notifications) { if(batteryPercent <= batteryThreshold && batteryPercent >= 0 && !notification_sent && notifications) {
notification_sent = true; notification_sent = true;
@ -63,8 +87,8 @@ PlasmoidItem {
toolTipSubText: "polling rate: " + pollingrate + " seconds" toolTipSubText: "polling rate: " + pollingrate + " seconds"
preferredRepresentation: compactRepresentation preferredRepresentation: compactRepresentation
fullRepresentation: FullRepresentation{}
compactRepresentation: CompactRepresentation{} compactRepresentation: CompactRepresentation{}
fullRepresentation: Item{}
Notification { Notification {
id: notification id: notification
@ -73,4 +97,32 @@ PlasmoidItem {
flags: Notification.DefaultEvent flags: Notification.DefaultEvent
urgency: Notification.CriticalUrgency 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
}
]
} }

View File

@ -11,7 +11,7 @@
}], }],
"Id": "org.kde.plasma.headsetcontrol-battery-widget", "Id": "org.kde.plasma.headsetcontrol-battery-widget",
"License": "", "License": "",
"Version": "1.6", "Version": "1.7",
"EnabledByDefault": true, "EnabledByDefault": true,
"Website": "https://git.lat/Flummi/headsetcontrol-battery-widget", "Website": "https://git.lat/Flummi/headsetcontrol-battery-widget",
"BugReportUrl": "https://git.lat/Flummi/headsetcontrol-battery-widget/issues", "BugReportUrl": "https://git.lat/Flummi/headsetcontrol-battery-widget/issues",