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 QtGraphicalEffects 1.12
import org.kde.plasma.components 2.0 as PlasmaComponents 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.core 2.0 as PlasmaCore
import org.kde.plasma.extras 2.0 as PlasmaExtras import org.kde.plasma.extras 2.0 as PlasmaExtras
import org.kde.notificationmanager 1.1 as Notifications 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 // 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 // https://invent.kde.org/plasma/plasma-workspace/-/blob/master/applets/notifications/package/contents/ui/NotificationItem.qml
Item { Item {
id: notificationItem
property var notification property var notification
anchors.left: parent.left anchors.left: parent.left
@ -52,10 +54,8 @@ Item {
border.color: "#bdbdbd" border.color: "#bdbdbd"
border.width: 1 border.width: 1
ColumnLayout {
RowLayout {
id: notifLayout id: notifLayout
height: textLayout.height
anchors { anchors {
left: parent.left left: parent.left
leftMargin: units.gridUnit * 0.5 leftMargin: units.gridUnit * 0.5
@ -63,8 +63,9 @@ Item {
rightMargin: units.gridUnit * 0.5 rightMargin: units.gridUnit * 0.5
verticalCenter: parent.verticalCenter verticalCenter: parent.verticalCenter
} }
RowLayout {
Layout.fillWidth: true
spacing: units.smallSpacing / 2 spacing: units.smallSpacing / 2
// notif body // notif body
ColumnLayout { ColumnLayout {
id: textLayout 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) // swipe gesture for dismissing notification (left/right)
MouseArea { MouseArea {
id: dismissSwipe id: dismissSwipe
anchors.fill: parent anchors.fill: parent
drag.axis: Drag.XAxis drag.axis: Drag.XAxis
drag.target: rect drag.target: rect
onPressed: {
let pos = mapToItem(actionsflow, mouse.x, mouse.y);
if (actionsflow.childAt(pos.x, pos.y)) {
mouse.accepted = false;
}
}
onReleased: { onReleased: {
if (Math.abs(rect.x) > width / 2) { // dismiss notification when finished swipe if (Math.abs(rect.x) > width / 2) { // dismiss notification when finished swipe
notifModel.close(notification.id); notifModel.close(notification.id);