repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nugu/NotificationDaemon.qml

Raw
import QtQuick
import Quickshell
import Quickshell.Services.Notifications
import "."

Scope {
    id: root

    required property QtObject theme

    property int count: server.trackedNotifications.values.length
    property var history: []
    readonly property int historyLimit: 80

    // This object is loaded only by explicit IPC after Mako is stopped for a
    // controlled test. Creating NotificationServer eagerly would fight Mako on DBus.
    NotificationServer {
        id: server
        keepOnReload: true
        persistenceSupported: true
        bodySupported: true
        bodyMarkupSupported: true
        bodyHyperlinksSupported: true
        bodyImagesSupported: true
        actionsSupported: true
        actionIconsSupported: true
        imageSupported: true

        onNotification: notification => {
            notification.tracked = true
            root.remember(notification)
            root.armExpiry(notification)
        }
    }

    function remember(notification: var): void {
        const next = [notification].concat(history.filter(item => item !== notification))
        history = next.slice(0, historyLimit)
    }

    function timeoutFor(notification: var): int {
        if (notification.urgency === NotificationUrgency.Critical) return 0
        if (notification.urgency === NotificationUrgency.Low) return 2500
        if (notification.expireTimeout > 0) return Math.round(notification.expireTimeout * 1000)
        return 6500
    }

    function armExpiry(notification: var): void {
        const timeout = timeoutFor(notification)
        if (timeout <= 0) return
        expiryTimer.createObject(root, { "target": notification, "interval": timeout })
    }

    function dismiss(notification: var): void {
        if (notification) notification.dismiss()
    }

    function dismissAll(): void {
        for (const notification of server.trackedNotifications.values) notification.dismiss()
    }

    function restoreLast(): void {
        const notification = history.find(item => item && !item.tracked)
        if (notification) {
            notification.tracked = true
            armExpiry(notification)
        }
    }

    Component {
        id: expiryTimer
        Timer {
            property var target: null
            running: true
            repeat: false
            onTriggered: if (target && target.tracked) target.expire()
        }
    }

    Repeater {
        model: server.trackedNotifications

        NotificationToast {
            required property var modelData
            required property int index
            visible: index < 4
            theme: root.theme
            notification: modelData
            stackIndex: index
            onDismissRequested: notification => root.dismiss(notification)
        }
    }
}