Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/notifications/NotificationGroup.qml

Raw
pragma ComponentBehavior: Bound
import QtQuick
import "../.."
import "../../human.js" as Human

Column {
    id: root

    required property Theme theme
    readonly property Flickable scrollView: scroll
    required property Notifications notificationService
    // groups() reads the service's reactive history state, so this binding
    // follows every mutation and external update while the popup is open.
    readonly property var groups: notificationService.groups()
    readonly property int activeCount: groups.reduce((sum, group) => sum + group.count, 0)
    // Opening focuses the newest card's first action, else do not disturb (FeatureEntry.qml).
    property Item newestCardControl: null
    readonly property Item initialFocus: groups.length > 0 && newestCardControl ? newestCardControl : dndButton
    // The card fits the header, an armed confirmation and history; the entry
    // height caps it (FeatureEntry.qml).
    readonly property real naturalHeight: 28 + spacing + confirmHeight + scroll.contentHeight
    // Clearing discards history for good, so the first press only arms it. The
    // panel lives only while its popup is open, so every dismissal disarms.
    property bool clearArmed: false
    readonly property real confirmHeight: clearArmed ? clearConfirmation.height + spacing : 0

    onActiveCountChanged: if (activeCount === 0)
        clearArmed = false

    function runAction(id, actionId) {
        notificationService.runAction(id, actionId);
    }
    function editReply(id, text) {
        notificationService.setReplyDraft(id, text);
    }
    function sendReply(id, text) {
        notificationService.sendReply(id, text);
    }
    function toggleGroup(appName) {
        notificationService.toggleGroup(appName);
    }
    function toggleDnd() {
        notificationService.toggleDnd();
    }
    function clearAll() {
        clearArmed = false;
        notificationService.discardAllNotifications();
    }
    function cancelClear() {
        clearArmed = false;
        clearButton.forceActiveFocus(Qt.TabFocusReason);
    }

    spacing: 8
    width: parent ? parent.width : 0

    StableListModel {
        id: groupRows
        rows: root.groups
    }

    Item {
        width: parent.width
        height: 28
        // The chip names the popup; the header carries only state.
        NuguText {
            anchors.left: parent.left
            anchors.verticalCenter: parent.verticalCenter
            visible: root.notificationService.unreadCount > 0
            theme: root.theme
            variant: "meta"
            text: `${root.notificationService.unreadCount} unread`
        }
        Row {
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter
            spacing: 8
            PopupButton {
                id: dndButton

                theme: root.theme
                height: 24
                // one noun toggle: the checked frame and the crossed bell tell its state.
                text: "do not disturb"
                icon: root.notificationService.dnd ? "󰂛" : "󰂚"
                tooltip: "hide non-critical toasts"
                checked: root.notificationService.dnd
                onClicked: root.toggleDnd()
            }
            PopupButton {
                id: clearButton

                visible: root.activeCount > 0
                theme: root.theme
                height: 24
                text: "clear all"
                role: "destructive"
                checked: root.clearArmed
                onClicked: root.clearArmed = !root.clearArmed
            }
        }
    }

    // The armed confirmation, below the header so a double click never confirms.
    Item {
        id: clearConfirmation

        objectName: "clearConfirmation"
        visible: root.clearArmed
        width: parent.width
        height: visible ? 40 : 0

        Column {
            anchors.left: parent.left
            anchors.verticalCenter: parent.verticalCenter
            spacing: 2

            NuguText {
                theme: root.theme
                variant: "title"
                text: root.activeCount === 1 ? "clear 1 notification?" : `clear ${root.activeCount} notifications?`
            }

            NuguText {
                theme: root.theme
                variant: "meta"
                text: "this cannot be undone"
                color: root.theme.ui.minor
            }
        }

        Row {
            anchors.right: parent.right
            anchors.verticalCenter: parent.verticalCenter
            spacing: 8

            PopupButton {
                theme: root.theme
                height: 28
                text: "cancel"
                tooltip: "keep notifications"
                onClicked: root.cancelClear()
            }

            PopupButton {
                theme: root.theme
                height: 28
                text: "clear all"
                accessibleName: "confirm clear all"
                tooltip: ""
                role: "destructive"
                primary: true
                onClicked: root.clearAll()
            }
        }
    }

    // History can outgrow the popup; the header stays fixed and everything
    // below scrolls. The Loader hosting this Column forces a concrete
    // root.height, so the viewport can bound on it.
    Item {
        id: scrollArea
        width: parent.width
        height: Math.max(0, root.height - 28 - root.spacing - root.confirmHeight)

        Flickable {
            id: scroll
            anchors.fill: parent
            contentHeight: scrollColumn.height
            clip: true
            interactive: contentHeight > height
            boundsBehavior: Flickable.StopAtBounds
            flickableDirection: Flickable.VerticalFlick

            Column {
                id: scrollColumn
                width: scroll.width
                spacing: 8

                Item {
                    objectName: "notificationsEmptyState"
                    width: parent.width
                    // Compact: the card fits this sentence, not a blank page.
                    height: 120
                    visible: root.groups.length === 0
                    Column {
                        anchors.centerIn: parent
                        spacing: 8
                        NuguText {
                            anchors.horizontalCenter: parent.horizontalCenter
                            theme: root.theme
                            variant: "title"
                            text: "no notifications"
                        }
                        NuguText {
                            anchors.horizontalCenter: parent.horizontalCenter
                            theme: root.theme
                            variant: "meta"
                            text: "you're all caught up"
                        }
                    }
                }

                Repeater {
                    model: groupRows
                    delegate: Column {
                        id: groupDelegate
                        required property var rowData
                        required property int index
                        width: root.width
                        spacing: 6

                        PopupButton {
                            id: groupHeader
                            readonly property int hiddenCount: Math.max(1, (groupDelegate.rowData.count || 1) - (groupDelegate.rowData.previewCount || 1))

                            theme: root.theme
                            width: parent.width
                            height: (groupDelegate.rowData.count || 0) > 1 ? 30 : 0
                            visible: (groupDelegate.rowData.count || 0) > 1
                            readonly property string appName: Human.label(groupDelegate.rowData.appName, "app")
                            text: groupDelegate.rowData.expanded ? `${appName} · ${groupDelegate.rowData.count} — show less` : `${appName} · ${groupDelegate.rowData.count} — show ${hiddenCount} more`
                            checked: groupDelegate.rowData.expanded
                            onClicked: root.toggleGroup(groupDelegate.rowData.appName || "")
                        }

                        StableListModel {
                            id: notificationRows
                            rows: groupDelegate.rowData.visibleNotifications || []
                        }

                        Repeater {
                            model: notificationRows
                            delegate: Item {
                                id: cardWrap
                                required property var rowData
                                required property int index
                                width: root.width
                                implicitHeight: cardInner.implicitHeight
                                height: implicitHeight
                                NotificationCard {
                                    id: cardInner
                                    width: parent.width
                                    theme: root.theme
                                    modelData: cardWrap.rowData
                                    replyDraft: cardWrap.rowData.replyDraft || ""
                                    replyError: cardWrap.rowData.replyError || ""
                                    onDismiss: id => root.runAction(id, "close")
                                    onInvokeAction: (id, actionId) => root.runAction(id, actionId)
                                    onReplyEdited: (id, text) => root.editReply(id, text)
                                    onSendReply: (id, text) => root.sendReply(id, text)
                                }
                                Binding {
                                    when: groupDelegate.index === 0 && cardWrap.index === 0
                                    target: root
                                    property: "newestCardControl"
                                    value: cardInner.primaryControl
                                    restoreMode: Binding.RestoreNone
                                }
                            }
                        }
                    }
                }
            }
        }

        // slim scroll indicator, overlaid (outside the Flickable so it
        // doesn't scroll with content); shown only when content overflows.
        Rectangle {
            anchors.right: parent.right
            width: 3
            radius: width / 2
            visible: scroll.contentHeight > scroll.height + 1
            color: root.theme ? root.theme.withAlpha(root.theme.ui.minor, 0.5) : "transparent"
            height: Math.max(24, scrollArea.height * scrollArea.height / Math.max(1, scroll.contentHeight))
            y: scroll.contentY * scrollArea.height / Math.max(1, scroll.contentHeight)
        }
    }
}