repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/NotificationCard.qml

Raw
import QtQuick
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)

    // 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"
    }

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton
        enabled: card.compact
        onClicked: 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.modelData.appIcon || ""
            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 : 46) - 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 - parent.spacing
                    elide: Text.ElideRight
                }
                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.modelData.image || ""
                visible: !card.compact && status === Image.Ready
                width: Math.min(parent.width, 220)
                fillMode: Image.PreserveAspectFit
                sourceSize.width: 220
            }

            Flow {
                width: parent.width
                spacing: 6
                visible: !card.compact && actionModel.length > 0
                property var actionModel: (card.modelData.actionRows || []).filter((a) => a.kind !== "close")
                Repeater {
                    model: parent.actionModel
                    delegate: Rectangle {
                        id: actionBtn
                        required property var modelData
                        height: 26
                        width: Math.max(58, actLabel.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);
                        }
                        CardText {
                            id: actLabel
                            anchors.centerIn: parent
                            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 }
                        }
                    }
                }
            }
        }

        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 }
}