diff --git a/README.md b/README.md index 175bf13..0537857 100644 --- a/README.md +++ b/README.md @@ -13,10 +13,15 @@ The bar shows the time, cellular, Wi-Fi, Bluetooth and battery. The sheet has: - A flashlight - Rotation, with a choice of orientations - Screenshot and power (suspend, restart, power off) buttons +- Your notifications Drag the bar down to open the sheet, and swipe up anywhere or tap below it to close it. You can also toggle it with `qs ipc call sheet toggle`. +It's also the notification server. New notifications pop up under the bar for a few seconds, or +until you dismiss them if they're urgent. Swipe one up to put it away early. Swipe a notification +sideways to dismiss it, in the sheet or as a popup, and tap it to open it. + ## Using it The flake has a `quadshell` package, and an overlay that adds it to nixpkgs: @@ -36,6 +41,7 @@ It brings `nmcli`, `mmcli`, `nmgui` and `adw-bluetooth` with it. The system need - niri, NetworkManager and ModemManager running - systemd-logind, which the flashlight uses to set the LED without root - The Papirus-Dark icon theme and the Inter font +- No other notification daemon, like swaync or mako, as only one can run at a time ## Developing diff --git a/src/NotificationCard.qml b/src/NotificationCard.qml new file mode 100644 index 0000000..cce69f9 --- /dev/null +++ b/src/NotificationCard.qml @@ -0,0 +1,188 @@ +import QtQuick +import QtQuick.Layouts +import Quickshell.Widgets +import Quickshell.Services.Notifications + +Item { + id: card + + required property Notification notification + property bool popup: false + + signal activated + + readonly property var actions: Array.from(notification?.actions ?? []).filter(a => a.identifier !== "default") + readonly property var defaultAction: Array.from(notification?.actions ?? []).find(a => a.identifier === "default") + + implicitHeight: body.implicitHeight + + Timer { + running: card.popup && card.notification?.urgency !== NotificationUrgency.Critical && !swipe.active + interval: card.notification?.expireTimeout > 0 ? card.notification?.expireTimeout * 1000 : 5000 + onTriggered: Notifications.timeOut(card.notification) + } + + Rectangle { + id: body + + width: parent.width + implicitHeight: layout.implicitHeight + 24 + radius: 20 + color: Theme.background + opacity: 1 - Math.min(1, Math.abs(x) / width) - Math.min(1, -y / height) + + Behavior on x { + enabled: !swipe.active + NumberAnimation { + duration: 180 + easing.type: Easing.OutCubic + } + } + Behavior on y { + enabled: !swipe.active + NumberAnimation { + duration: 180 + easing.type: Easing.OutCubic + } + } + + Timer { + id: leave + interval: 180 + property bool dismiss + onTriggered: dismiss ? card.notification?.dismiss() : Notifications.hidePopup(card.notification) + } + + DragHandler { + id: swipe + target: null + yAxis.enabled: card.popup + + onTranslationChanged: { + const t = activeTranslation; + if (Math.abs(t.x) > Math.abs(t.y)) { + body.x = t.x; + body.y = 0; + } else { + body.x = 0; + body.y = Math.min(0, t.y); + } + } + + onActiveChanged: { + if (active) return; + const v = centroid.velocity; + if (Math.abs(body.x) > body.width / 3 || Math.abs(v.x) > 1000) { + body.x = (body.x || v.x) > 0 ? body.width : -body.width; + leave.dismiss = true; + leave.start(); + } else if (-body.y > body.height / 3 || v.y < -1000) { + body.y = -body.height; + leave.dismiss = false; + leave.start(); + } else { + body.x = 0; + body.y = 0; + } + } + } + + TapHandler { + onTapped: eventPoint => { + const p = actionRow.mapFromItem(body, eventPoint.position); + if (actionRow.visible && actionRow.contains(p)) return; + card.activated(); + if (card.defaultAction) card.defaultAction.invoke(); + else if (card.popup) Notifications.hidePopup(card.notification); + } + } + + RowLayout { + id: layout + + anchors { + left: parent.left + right: parent.right + top: parent.top + margins: 12 + leftMargin: 14 + } + spacing: 12 + + IconImage { + Layout.alignment: Qt.AlignTop + Layout.topMargin: 2 + implicitSize: 32 + source: Notifications.iconFor(card.notification) + visible: source.toString() !== "" + } + + ColumnLayout { + Layout.fillWidth: true + spacing: 2 + + RowLayout { + Layout.fillWidth: true + spacing: 6 + + Text { + Layout.fillWidth: true + text: card.notification?.appName ?? "" + color: Theme.subtext + font.family: Theme.font + font.pixelSize: 11 + elide: Text.ElideRight + } + + Text { + readonly property date time: Notifications.times[card.notification?.id] ?? new Date() + text: Qt.formatTime(time, "HH:mm") + color: Theme.subtext + font.family: Theme.font + font.pixelSize: 11 + } + } + + Text { + Layout.fillWidth: true + visible: text !== "" + text: card.notification?.summary ?? "" + color: Theme.text + font.family: Theme.font + font.pixelSize: 14 + font.weight: Font.DemiBold + textFormat: Text.PlainText + elide: Text.ElideRight + } + + Text { + Layout.fillWidth: true + visible: text !== "" + text: card.notification?.body ?? "" + color: Theme.subtext + font.family: Theme.font + font.pixelSize: 13 + textFormat: Text.PlainText + wrapMode: Text.Wrap + maximumLineCount: card.popup ? 2 : 4 + elide: Text.ElideRight + } + + Options { + id: actionRow + Layout.fillWidth: true + Layout.topMargin: 6 + visible: card.actions.length > 0 + color: Theme.surface + model: card.actions.map(a => ({ + name: a.text, + action: () => { + card.activated(); + a.invoke(); + }, + })) + } + } + } + } +} diff --git a/src/Notifications.qml b/src/Notifications.qml new file mode 100644 index 0000000..fb70f1e --- /dev/null +++ b/src/Notifications.qml @@ -0,0 +1,52 @@ +pragma Singleton +import QtQuick +import Quickshell +import Quickshell.Services.Notifications + +Singleton { + id: root + + readonly property var list: [...server.trackedNotifications.values].reverse() // qmllint disable missing-property + property var popups: [] + property var times: ({}) + + function hidePopup(notification) { + popups = popups.filter(n => n !== notification); + } + + function timeOut(notification) { + if (notification.transient) notification.expire(); + else hidePopup(notification); + } + + function clear() { + for (const notification of list) notification.dismiss(); + } + + function iconFor(notification) { + if (!notification) return ""; + const icon = notification.image || notification.appIcon + || DesktopEntries.heuristicLookup(notification.desktopEntry || notification.appName)?.icon || ""; + if (icon === "") return ""; + if (icon.startsWith("/")) return `file://${icon}`; + if (icon.includes("://")) return icon; + return Quickshell.iconPath(icon, true); + } + + NotificationServer { + id: server + + keepOnReload: true + persistenceSupported: true + bodySupported: true + actionsSupported: true + imageSupported: true + + onNotification: notification => { + notification.tracked = true; + root.times[notification.id] = new Date(); + notification.closed.connect(() => root.hidePopup(notification)); + root.popups = [notification, ...root.popups.filter(n => n !== notification)]; + } + } +} diff --git a/src/Options.qml b/src/Options.qml index 41dbb02..d339f52 100644 --- a/src/Options.qml +++ b/src/Options.qml @@ -1,3 +1,4 @@ +pragma ComponentBehavior: Bound import QtQuick import QtQuick.Layouts @@ -5,6 +6,7 @@ RowLayout { id: options property var model: [] + property color color: Theme.surface spacing: 8 @@ -19,7 +21,7 @@ RowLayout { Layout.fillWidth: true implicitHeight: 40 radius: 20 - color: modelData.active ? Theme.accent : Theme.surface + color: modelData.active ? Theme.accent : options.color Text { anchors.centerIn: parent diff --git a/src/Panel.qml b/src/Panel.qml index 5eb59af..9469d55 100644 --- a/src/Panel.qml +++ b/src/Panel.qml @@ -33,6 +33,10 @@ PanelWindow { // qmllint disable uncreatable-type mask: Region { item: root.sheetShown ? scrim : bar + + Region { + item: popups + } } function open() { @@ -119,12 +123,16 @@ PanelWindow { // qmllint disable uncreatable-type opacity: 0.5 * root.progress TapHandler { + enabled: root.sheetShown onTapped: eventPoint => { - if (eventPoint.position.y > sheet.y + sheet.height) root.close(); + const p = eventPoint.position; + if (p.y > sheet.y + sheet.height && !drawer.contains(drawer.mapFromItem(scrim, p))) root.close(); } } - SheetDrag {} + SheetDrag { + enabled: root.sheetShown + } } Rectangle { @@ -153,6 +161,7 @@ PanelWindow { // qmllint disable uncreatable-type } spacing: 10 + RowLayout { Layout.fillWidth: true spacing: 8 @@ -272,6 +281,131 @@ PanelWindow { // qmllint disable uncreatable-type } } + ColumnLayout { + id: drawer + + anchors { + left: parent.left + right: parent.right + margins: 8 + } + y: sheet.y + sheet.height + 8 + visible: root.sheetShown && Notifications.list.length > 0 + opacity: root.progress + spacing: 8 + + ListView { + Layout.fillWidth: true + // Scrolls rather than going off the bottom of the screen + Layout.preferredHeight: Math.min(contentHeight, root.height - sheet.height - clearAll.implicitHeight - 40) + interactive: contentHeight > height + clip: true + spacing: 8 + model: ScriptModel { + values: Notifications.list + } + + delegate: NotificationCard { + required property var modelData + notification: modelData + width: ListView.view.width + onActivated: root.close() + } + + remove: Transition { + NumberAnimation { + property: "opacity" + to: 0 + duration: 150 + } + } + displaced: Transition { + NumberAnimation { + property: "y" + duration: 180 + easing.type: Easing.OutCubic + } + } + } + + Rectangle { + id: clearAll + + Layout.alignment: Qt.AlignRight + implicitWidth: clearText.implicitWidth + 32 + implicitHeight: 36 + radius: height / 2 + color: Theme.background + + Text { + id: clearText + anchors.centerIn: parent + text: "Clear all" + color: Theme.text + font.family: Theme.font + font.pixelSize: 13 + } + + TapHandler { + onTapped: Notifications.clear() + } + } + } + + ListView { + id: popups + + anchors { + top: bar.bottom + left: parent.left + right: parent.right + margins: 8 + } + height: contentHeight + visible: !root.sheetShown + interactive: false + spacing: 8 + model: ScriptModel { + values: Notifications.popups.slice(0, 3) + } + + delegate: NotificationCard { + required property var modelData + notification: modelData + width: ListView.view.width + popup: true + } + + add: Transition { + NumberAnimation { + property: "y" + from: -80 + duration: 220 + easing.type: Easing.OutCubic + } + NumberAnimation { + property: "opacity" + from: 0 + to: 1 + duration: 220 + } + } + remove: Transition { + NumberAnimation { + property: "opacity" + to: 0 + duration: 150 + } + } + displaced: Transition { + NumberAnimation { + property: "y" + duration: 220 + easing.type: Easing.OutCubic + } + } + } + readonly property var rotations: [ { name: "Portrait", transform: "normal" }, { name: "Landscape", transform: "270" }, diff --git a/src/qmldir b/src/qmldir index 268e016..5cc14d9 100644 --- a/src/qmldir +++ b/src/qmldir @@ -1,6 +1,8 @@ +singleton Notifications Notifications.qml singleton Status Status.qml singleton Theme Theme.qml Bar Bar.qml +NotificationCard NotificationCard.qml Options Options.qml Panel Panel.qml RoundButton RoundButton.qml