notifications!

This commit is contained in:
Flummi 2024-01-25 16:21:39 +01:00
parent f3529afeb1
commit d821e5b5c0
Signed by: Flummi
GPG Key ID: AA2AEF822A6F4817
4 changed files with 78 additions and 9 deletions

View File

@ -22,6 +22,13 @@
<default>#008800</default>
</entry>
<entry name="batteryThreshold" type="Int">
<default>10</default>
</entry>
<entry name="notifications" type="Bool">
<default>true</default>
</entry>
<!-- debug -->
<entry name="debug_active" type="Bool">
<default>false</default>

View File

@ -7,13 +7,10 @@ import org.kde.kirigami 2.14 as Kirigami
import "./lib/helper.js" as Helper
Item {
Layout.minimumWidth: units.iconSizes.medium
Layout.alignment: Qt.AlignHCenter
Rectangle { // battery
id: container
anchors.fill: parent
anchors.rightMargin: 2
anchors.rightMargin: 1
anchors.topMargin: 10 - batteryheight
anchors.bottomMargin: 10 - batteryheight
color: "transparent"

View File

@ -17,6 +17,9 @@ QQC2.Pane {
property alias cfg_colorHalf: colorHalf.color
property alias cfg_colorFull: colorFull.color
property alias cfg_notifications: notifications.checked
property alias cfg_batteryThreshold: batteryThreshold.value
Kirigami.FormLayout {
anchors.fill: parent
wideMode: false
@ -157,5 +160,40 @@ QQC2.Pane {
color: Helper.interpolateColor( 1, [ cfg_colorEmpty.toString(), cfg_colorHalf.toString(), cfg_colorFull.toString() ])
}
}
GridLayout {
columns: 3
rows: 2
rowSpacing: 10
// first row
Text {
text: "notifications:"
color: PlasmaCore.Theme.textColor
}
PlasmaComponents.CheckBox {
id: notifications
}
Text {
text: ""
}
// second row
Text {
text: "battery threshold:"
color: PlasmaCore.Theme.textColor
}
PlasmaComponents.Slider {
id: batteryThreshold
value: 10
minimumValue: 1
maximumValue: 50
stepSize: 1
}
Text {
text: cfg_batteryThreshold
color: PlasmaCore.Theme.textColor
}
}
}
}

View File

@ -4,6 +4,8 @@ import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.kirigami 2.14 as Kirigami
import org.kde.notification 1.0
import "./lib/helper.js" as Helper
Item {
@ -15,10 +17,14 @@ Item {
property string colorHalf: Plasmoid.configuration.colorHalf
property string colorFull: Plasmoid.configuration.colorFull
property bool notifications: Plasmoid.configuration.notifications
property int batteryThreshold: Plasmoid.configuration.batteryThreshold
property bool debug_active: Plasmoid.configuration.debug_active
property int debug_charge: Plasmoid.configuration.debug_charge
property int batteryPercent: -2
property bool notification_sent: false;
PlasmaCore.DataSource {
id: hsSource
@ -27,15 +33,36 @@ Item {
interval: pollingrate * 1e3
onNewData: {
if(debug_active)
return batteryPercent = debug_charge;
return batteryPercent = data['exit code'] > 0
batteryPercent = debug_charge;
else
batteryPercent = data['exit code'] > 0
? -2 // not connected
: data['stdout'];
: +data.stdout;
// 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();
}
else // reset notifications if battery level is over batteryThreshold
notification_sent = false;
return;
}
}
Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation
Plasmoid.fullRepresentation: FullRepresentation{}
Plasmoid.compactRepresentation: CompactRepresentation{}
Notification {
id: notification
componentName: "plasma_workspace"
eventId: "notification"
flags: Notification.Persistent
urgency: Notification.HighUrgency
}
}