repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/NotificationCard.qml

Raw
import QtQuick
import Quickshell
import Quickshell.Widgets

Rectangle {
    id: card

    required property QtObject theme
    required property var modelData
    property bool compact: false
    signal dismiss(string id)
    signal invokeAction(string id, string actionId)
    signal sendReply(string id, string text)

    readonly property bool canReply: !compact && !!modelData.hasInlineReply

    // notify-send funnels BOTH named freedesktop icons and attached photos into
    // `image` as an image://icon/ provider URL (appIcon stays empty). Split them:
    // a real picture (absolute path = double slash, or raster extension) renders
    // as the large preview; a named icon renders as the small identity glyph.
    readonly property string photoCandidate: String(modelData.image || "")
    readonly property bool imageIsPhoto: photoCandidate.indexOf("image://icon//") === 0
        || /\.(png|jpe?g|gif|webp|bmp)(\?|#|$)/i.test(photoCandidate)
    readonly property string iconSource: modelData.appIcon || (imageIsPhoto ? "" : photoCandidate)
    // image://icon//abs/path -> file:///abs/path so plain Image keeps true aspect
    readonly property string photoSource: !imageIsPhoto ? "" : (photoCandidate.indexOf("image://icon//") === 0 ? "file://" + photoCandidate.substring(13) : photoCandidate)

    // KDE/Plasma progress hint (0-100); -1 when the notification has no value.
    readonly property int progress: modelData.value !== undefined ? Number(modelData.value) : -1
    readonly property bool hasProgress: progress >= 0

    // local text styling, mirrors PopoutHost NuguText variants
    component CardText: Text {
        required property QtObject theme
        property string variant: "body"
        color: !theme ? "transparent" : (variant === "title" ? theme.ui.important_global : (variant === "meta" ? theme.ui.normal : theme.content.normal))
        font.family: variant === "mono" ? "IBM Plex Mono" : "IBM Plex Sans"
        font.pixelSize: variant === "title" ? 15 : (variant === "meta" ? 13 : 14)
        font.bold: variant === "title"
        elide: Text.ElideRight
    }

    function urgencyColor() {
        const u = String(modelData.urgency || "normal");
        if (!theme)
            return "transparent";
        if (u === "critical")
            return theme.error;
        if (u === "low")
            return theme.ui.minor;
        return theme.ui.accent;
    }

    function relTime(ms) {
        const now = Date.now();
        const diff = Math.max(0, now - Number(ms || now));
        const m = Math.floor(diff / 60000);
        if (m < 1)
            return "now";
        if (m < 60)
            return `${m}m`;
        const h = Math.floor(m / 60);
        if (h < 24)
            return `${h}h`;
        return `${Math.floor(h / 24)}d`;
    }

    radius: compact ? 12 : 10
    color: theme ? theme.withAlpha(theme.ui.backdrop, 0.92) : "transparent"
    implicitHeight: cardBody.implicitHeight + (compact ? 16 : 20)
    clip: true

    Rectangle {
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        width: 3
        radius: 2
        color: card.urgencyColor()
        visible: String(card.modelData.urgency || "normal") !== "low"
    }

    // freedesktop "default" action: invoked by clicking the notification body
    // (not rendered as a button). Buttons declared later sit above this area,
    // so clicks on them are unaffected.
    readonly property var defaultAction: (modelData.actionRows || []).find((a) => a.id === "default") || null
    readonly property bool hasDefault: !!defaultAction

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton
        enabled: card.compact || card.hasDefault
        onClicked: {
            if (card.hasDefault)
                card.invokeAction(String(card.modelData.id), "default");
            if (card.compact)
                card.dismiss(String(card.modelData.id));
        }
    }

    Row {
        id: cardBody
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.verticalCenter: parent.verticalCenter
        anchors.leftMargin: 12
        anchors.rightMargin: 10
        spacing: 10

        IconImage {
            anchors.top: card.compact ? undefined : parent.top
            anchors.verticalCenter: card.compact ? parent.verticalCenter : undefined
            width: card.compact ? 20 : 36
            height: width
            source: card.iconSource
            visible: source.toString().length > 0
        }

        Column {
            // size off the card's own (externally-set) width, NOT parent.width:
            // cardBody is a Row whose width derives from content, so reading it
            // here collapses the column to 0 (a binding loop). 22 = left+right margins.
            width: card.width - 22 - (card.compact ? 30 : 56) - closeCol.width
            spacing: card.compact ? 0 : 3

            Row {
                width: parent.width
                spacing: 6
                visible: !card.compact
                CardText {
                    theme: card.theme
                    variant: "meta"
                    text: card.modelData.appName || "unknown"
                    width: parent.width - timeText.width - copyBtn.width - parent.spacing * 2
                    elide: Text.ElideRight
                }
                // subtle copy-body affordance: small and low-key, surfaced only
                // on hover and only when there is a body — deliberately NOT a
                // pill like the server actions below.
                Item {
                    id: copyBtn
                    width: visible ? copyGlyph.implicitWidth + 6 : 0
                    height: timeText.implicitHeight
                    visible: (card.modelData.body || "").length > 0
                    CardText {
                        id: copyGlyph
                        anchors.centerIn: parent
                        theme: card.theme
                        variant: "meta"
                        text: "copy"
                        opacity: copyArea.containsMouse ? 1 : (cardHover.hovered ? 0.5 : 0)
                        Behavior on opacity { NumberAnimation { duration: card.theme && card.theme.lowPowerMode ? 0 : 120 } }
                    }
                    MouseArea {
                        id: copyArea
                        anchors.fill: parent
                        hoverEnabled: true
                        onClicked: card.invokeAction(String(card.modelData.id), "copy-body")
                    }
                }
                CardText {
                    id: timeText
                    theme: card.theme
                    variant: "meta"
                    text: card.relTime(card.modelData.time)
                }
            }

            CardText {
                theme: card.theme
                variant: card.compact ? "body" : "title"
                text: card.modelData.summary || "notification"
                width: parent.width
                elide: Text.ElideRight
            }

            CardText {
                theme: card.theme
                variant: "meta"
                text: card.modelData.body || ""
                width: parent.width
                visible: !card.compact && text.length > 0
                wrapMode: Text.WordWrap
                maximumLineCount: 3
                elide: Text.ElideRight
            }

            Image {
                source: card.photoSource
                visible: !card.compact && status === Image.Ready
                width: Math.min(parent.width, 220)
                fillMode: Image.PreserveAspectFit
                sourceSize.width: 220
            }

            // content progress (KDE int:value); distinct from the toast's
            // auto-dismiss drain. Full card only — the toast shows it inline.
            Item {
                width: parent.width
                height: card.hasProgress && !card.compact ? 8 : 0
                visible: card.hasProgress && !card.compact
                Rectangle {
                    anchors.verticalCenter: parent.verticalCenter
                    width: parent.width
                    height: 4
                    radius: 2
                    color: card.theme ? card.theme.withAlpha(card.theme.ui.minor, 0.25) : "transparent"
                    Rectangle {
                        width: parent.width * Math.max(0, Math.min(100, card.progress)) / 100
                        height: parent.height
                        radius: 2
                        color: card.theme ? card.theme.ui.accent : "transparent"
                        Behavior on width { NumberAnimation { duration: card.theme && card.theme.lowPowerMode ? 0 : 200 } }
                    }
                }
            }

            Flow {
                width: parent.width
                spacing: 6
                visible: !card.compact && actionModel.length > 0
                property var actionModel: (card.modelData.actionRows || []).filter((a) => a.kind !== "close" && a.kind !== "copy-body" && a.id !== "default")
                Repeater {
                    model: parent.actionModel
                    delegate: Rectangle {
                        id: actionBtn
                        required property var modelData
                        // action-icons: identifier doubles as a freedesktop icon
                        // name; resolve it to a real path (empty if absent).
                        readonly property string actIcon: actionBtn.modelData.icon ? Quickshell.iconPath(String(actionBtn.modelData.icon), true) : ""
                        height: 26
                        width: Math.max(58, actRow.implicitWidth + 18)
                        radius: 8
                        color: {
                            if (!card.theme)
                                return "transparent";
                            if (actArea.pressed)
                                return card.theme.withAlpha(card.theme.ui.accent, 0.34);
                            if (actArea.containsMouse)
                                return card.theme.withAlpha(card.theme.ui.accent, 0.24);
                            return card.theme.withAlpha(card.theme.ui.accent, 0.14);
                        }
                        Row {
                            id: actRow
                            anchors.centerIn: parent
                            spacing: 5
                            IconImage {
                                anchors.verticalCenter: parent.verticalCenter
                                width: 16
                                height: 16
                                source: actionBtn.actIcon
                                visible: actionBtn.actIcon.length > 0
                            }
                            CardText {
                                id: actLabel
                                anchors.verticalCenter: parent.verticalCenter
                                theme: card.theme
                                variant: "meta"
                                text: actionBtn.modelData.label || "act"
                            }
                        }
                        MouseArea {
                            id: actArea
                            anchors.fill: parent
                            hoverEnabled: true
                            onClicked: card.invokeAction(String(card.modelData.id), String(actionBtn.modelData.id))
                        }
                        Behavior on color {
                            ColorAnimation { duration: card.theme && card.theme.lowPowerMode ? 0 : 120 }
                        }
                    }
                }
            }

            // inline reply (chat apps advertising hasInlineReply): a quick
            // text field; Enter sends via sendInlineReply() and clears.
            Rectangle {
                width: parent.width
                height: card.canReply ? 30 : 0
                visible: card.canReply
                radius: 10
                color: card.theme ? card.theme.withAlpha(card.theme.ui.minor, 0.14) : "transparent"
                border.width: replyInput.activeFocus ? 1 : 0
                border.color: card.theme ? card.theme.ui.accent : "transparent"

                TextInput {
                    id: replyInput
                    anchors.fill: parent
                    anchors.leftMargin: 10
                    anchors.rightMargin: 10
                    verticalAlignment: TextInput.AlignVCenter
                    color: card.theme ? card.theme.content.normal : "white"
                    selectionColor: card.theme ? card.theme.ui.focus : "#ffffff"
                    selectedTextColor: card.theme ? card.theme.content.backdrop : "black"
                    font.family: "IBM Plex Sans"
                    font.pixelSize: 13
                    clip: true
                    onAccepted: {
                        if (text.length > 0) {
                            card.sendReply(String(card.modelData.id), text);
                            text = "";
                        }
                    }
                }

                CardText {
                    anchors.left: parent.left
                    anchors.leftMargin: 10
                    anchors.verticalCenter: parent.verticalCenter
                    visible: replyInput.text.length === 0
                    theme: card.theme
                    variant: "meta"
                    text: card.modelData.inlineReplyPlaceholder || "Reply…"
                }
            }
        }

        Item {
            id: closeCol
            width: card.compact ? 0 : 24
            height: card.compact ? 0 : 24
            visible: !card.compact
            Rectangle {
                anchors.fill: parent
                radius: 12
                color: closeArea.containsMouse && card.theme ? card.theme.withAlpha(card.theme.ui.minor, 0.25) : "transparent"
                opacity: cardHover.hovered || closeArea.containsMouse ? 1 : 0
                CardText {
                    anchors.centerIn: parent
                    theme: card.theme
                    variant: "mono"
                    text: "×"
                }
                Behavior on opacity { NumberAnimation { duration: 120 } }
            }
            MouseArea {
                id: closeArea
                anchors.fill: parent
                hoverEnabled: true
                onClicked: card.dismiss(String(card.modelData.id))
            }
        }
    }

    HoverHandler { id: cardHover }
}