support for action buttons in notifications

This commit is contained in:
Marco Martin 2020-11-18 18:18:24 +01:00 committed by Bhushan Shah
parent 94609d2590
commit 894d4a3b72

View file

@ -21,6 +21,7 @@ import QtQuick.Layouts 1.1
import QtGraphicalEffects 1.12
import org.kde.plasma.components 2.0 as PlasmaComponents
import org.kde.plasma.components 3.0 as PlasmaComponents3
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.notificationmanager 1.1 as Notifications
@ -32,6 +33,7 @@ import "../components"
// meant to be temporary, until the notifications components in plasma-workspace are available to used
// https://invent.kde.org/plasma/plasma-workspace/-/blob/master/applets/notifications/package/contents/ui/NotificationItem.qml
Item {
id: notificationItem
property var notification
anchors.left: parent.left
@ -52,10 +54,8 @@ Item {
border.color: "#bdbdbd"
border.width: 1
RowLayout {
ColumnLayout {
id: notifLayout
height: textLayout.height
anchors {
left: parent.left
leftMargin: units.gridUnit * 0.5
@ -63,8 +63,9 @@ Item {
rightMargin: units.gridUnit * 0.5
verticalCenter: parent.verticalCenter
}
RowLayout {
Layout.fillWidth: true
spacing: units.smallSpacing / 2
// notif body
ColumnLayout {
id: textLayout
@ -132,13 +133,54 @@ Item {
}
}
Flow {
id: actionsflow
Layout.fillWidth: true
spacing: units.smallSpacing
layoutDirection: Qt.RightToLeft
Repeater {
id: actionRepeater
model: {
var buttons = [];
var actionNames = (notificationItem.notification.actionNames || []);
var actionLabels = (notificationItem.notification.actionLabels || []);
// HACK We want the actions to be right-aligned but Flow also reverses
for (var i = actionNames.length - 1; i >= 0; --i) {
buttons.push({
actionName: actionNames[i],
label: actionLabels[i]
});
}
return buttons;
}
PlasmaComponents3.ToolButton {
flat: false
// why does it spit "cannot assign undefined to string" when a notification becomes expired?
text: modelData.label || ""
onClicked: {
notifModel.invokeAction(notificationItem.notification.notificationId, modelData.actionName);
}
}
}
}
}
// swipe gesture for dismissing notification (left/right)
MouseArea {
id: dismissSwipe
anchors.fill: parent
drag.axis: Drag.XAxis
drag.target: rect
onPressed: {
let pos = mapToItem(actionsflow, mouse.x, mouse.y);
if (actionsflow.childAt(pos.x, pos.y)) {
mouse.accepted = false;
}
}
onReleased: {
if (Math.abs(rect.x) > width / 2) { // dismiss notification when finished swipe
notifModel.close(notification.id);