From 83aa943fd95d716c7fb75ac63cd0fb5869268861 Mon Sep 17 00:00:00 2001 From: Flummi Date: Sat, 8 Jun 2024 18:41:43 +0200 Subject: [PATCH] added contextmenu and hide tray icon --- package/contents/ui/lib/helper.js | 6 ++-- package/contents/ui/main.qml | 60 ++++++++++++++++++++++++++++--- package/metadata.json | 2 +- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/package/contents/ui/lib/helper.js b/package/contents/ui/lib/helper.js index b660ee5..38d66c9 100644 --- a/package/contents/ui/lib/helper.js +++ b/package/contents/ui/lib/helper.js @@ -1,12 +1,14 @@ const batteryLevel = (batteryStatus, batteryPercent) => ({ "BATTERY_UNAVAILABLE": "not connected", - "BATTERY_CHARGING": "charging (" + batteryPercent + "%)" + "BATTERY_CHARGING": "charging (" + batteryPercent + "%)", + "HEADSET_UNAVAILABLE": "no headset connected" })[batteryStatus] ?? batteryPercent + "%"; const batteryIcon = batteryStatus => ({ "BATTERY_UNAVAILABLE": "action-unavailable", "BATTERY_AVAILABLE": "audio-headphones-symbolic", - "BATTERY_CHARGING": "reload" + "BATTERY_CHARGING": "freon-voltage-symbolic", + "HEADSET_UNAVAILABLE": "dialog-error" })[batteryStatus] ?? "audio-headphones-symbolic"; const hex2rgb = (hex, h = hex.replace('#', '0x')) => ({ r: h >> 16 & 255, g: h >> 8 & 255, b: h & 255 }); diff --git a/package/contents/ui/main.qml b/package/contents/ui/main.qml index d228e8e..cdf40ce 100644 --- a/package/contents/ui/main.qml +++ b/package/contents/ui/main.qml @@ -3,13 +3,15 @@ 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: main + id: root + Plasmoid.status: PlasmaCore.Types.ActiveStatus readonly property int pollingrate: Plasmoid.configuration.pollingrate readonly property int batteryheight: Plasmoid.configuration.batteryheight @@ -26,6 +28,10 @@ PlasmoidItem { 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 @@ -34,14 +40,32 @@ PlasmoidItem { interval: pollingrate * 1e3 onNewData: (_, data) => { 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) { 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; @@ -63,8 +87,8 @@ PlasmoidItem { toolTipSubText: "polling rate: " + pollingrate + " seconds" preferredRepresentation: compactRepresentation - fullRepresentation: FullRepresentation{} compactRepresentation: CompactRepresentation{} + fullRepresentation: Item{} Notification { id: notification @@ -73,4 +97,32 @@ PlasmoidItem { 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 + } + ] } diff --git a/package/metadata.json b/package/metadata.json index 347152b..d06ac77 100644 --- a/package/metadata.json +++ b/package/metadata.json @@ -11,7 +11,7 @@ }], "Id": "org.kde.plasma.headsetcontrol-battery-widget", "License": "", - "Version": "1.6", + "Version": "1.7", "EnabledByDefault": true, "Website": "https://git.lat/Flummi/headsetcontrol-battery-widget", "BugReportUrl": "https://git.lat/Flummi/headsetcontrol-battery-widget/issues",