repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

quickshell/nuguland/TrayPanel.qml

Raw
pragma ComponentBehavior: Bound
import QtQuick
import Quickshell
import "Util.js" as Util

Column {
    id: root

    required property Theme theme
    required property Tray trayService
    property var panelState: trayService ? Util.parseJson(trayService.stateJson(), {
        "items": []
    }) : ({
            "items": []
        })
    readonly property var items: panelState.items || []
    readonly property bool hasItems: items.length > 0

    function refreshFrom(text) {
        panelState = Util.parseJson(text, panelState);
    }

    function primary(id, window, x, y) {
        if (trayService)
            refreshFrom(trayService.primaryActivate(id, window, x, y));
    }

    function menu(id, window, x, y) {
        if (trayService)
            refreshFrom(trayService.displayMenu(id, window, x, y));
    }

    function secondary(id) {
        if (trayService)
            refreshFrom(trayService.secondaryActivate(id));
    }

    spacing: 10
    width: parent ? parent.width : 0

    PopupSection {
        visible: root.hasItems
        width: parent.width
        theme: root.theme
        title: "tray"
        detail: `${root.items.length}`

        Flow {
            objectName: "trayItems"
            width: parent.width
            spacing: 8

            Repeater {
                model: root.items

                delegate: PopupCard {
                    id: itemCell

                    required property var modelData
                    readonly property bool needsAttention: modelData.status === "NeedsAttention"
                    readonly property string tooltipTitle: modelData.tooltipTitle || ""
                    readonly property string description: modelData.tooltipDescription || ""
                    readonly property string statusText: String(modelData.status || "passive").toLowerCase()
                    readonly property string categoryText: String(modelData.category || "application status").replace(/([a-z])([A-Z])/g, "$1 $2").toLowerCase()
                    readonly property string menuText: modelData.onlyMenu ? "menu only" : (modelData.hasMenu ? "menu" : "")

                    function openMenu(x, y) {
                        if (nativeMenu.menu) {
                            nativeMenu.anchor.rect = Qt.rect(Math.round(x), Math.round(y), 1, 1);
                            nativeMenu.open();
                        }
                    }

                    theme: root.theme
                    padding: 0
                    attention: needsAttention
                    hoverable: true
                    externallyPressed: trayArea.pressed
                    width: root.width
                    height: tooltipTitle.length > 0 || description.length > 0 ? 96 : 76
                    role: modelData.title || "tray item"

                    QsMenuAnchor {
                        id: nativeMenu
                        menu: root.trayService ? root.trayService.menuFor(itemCell.modelData.id) : null
                        anchor.item: itemCell
                    }

                    Row {
                        anchors.fill: parent
                        anchors.margins: 12
                        spacing: 12

                        Rectangle {
                            anchors.verticalCenter: parent.verticalCenter
                            width: 40
                            height: 40
                            radius: 12
                            color: root.theme ? root.theme.withAlpha(itemCell.needsAttention ? root.theme.warning : root.theme.ui.accent, 0.16) : "transparent"

                            Image {
                                anchors.centerIn: parent
                                width: 26
                                height: 26
                                sourceSize.width: 26
                                sourceSize.height: 26
                                fillMode: Image.PreserveAspectFit
                                source: itemCell.modelData.icon || ""
                            }
                        }

                        Column {
                            anchors.verticalCenter: parent.verticalCenter
                            width: parent.width - 142
                            spacing: 3

                            NuguText {
                                width: parent.width
                                theme: root.theme
                                variant: "title"
                                text: itemCell.modelData.title || itemCell.modelData.tooltipTitle || itemCell.modelData.id
                                elide: Text.ElideRight
                            }

                            NuguText {
                                visible: itemCell.tooltipTitle.length > 0 && itemCell.tooltipTitle !== itemCell.modelData.title
                                width: parent.width
                                theme: root.theme
                                variant: "meta"
                                text: itemCell.tooltipTitle
                                elide: Text.ElideRight
                            }

                            NuguText {
                                visible: itemCell.description.length > 0
                                width: parent.width
                                theme: root.theme
                                variant: "meta"
                                text: itemCell.description
                                elide: Text.ElideRight
                            }

                            NuguText {
                                width: parent.width
                                theme: root.theme
                                variant: "meta"
                                text: itemCell.menuText.length > 0 ? `${itemCell.categoryText} · ${itemCell.menuText}` : itemCell.categoryText
                                elide: Text.ElideRight
                            }
                        }

                        NuguText {
                            anchors.verticalCenter: parent.verticalCenter
                            width: 66
                            horizontalAlignment: Text.AlignRight
                            theme: root.theme
                            variant: "meta"
                            selected: itemCell.needsAttention
                            text: itemCell.statusText
                        }
                    }

                    MouseArea {
                        id: trayArea
                        anchors.fill: parent
                        acceptedButtons: Qt.LeftButton | Qt.MiddleButton | Qt.RightButton
                        hoverEnabled: true
                        cursorShape: Qt.PointingHandCursor
                        onClicked: event => {
                            if (event.button === Qt.RightButton || (event.button === Qt.LeftButton && itemCell.modelData.onlyMenu))
                                itemCell.openMenu(event.x, event.y);
                            else if (event.button === Qt.MiddleButton)
                                root.secondary(itemCell.modelData.id);
                            else
                                root.primary(itemCell.modelData.id, itemCell.QsWindow.window, event.x, event.y);
                        }
                    }
                }
            }
        }
    }

    Item {
        objectName: "trayEmptyState"
        visible: !root.hasItems
        width: parent.width
        height: 300

        Column {
            anchors.centerIn: parent
            spacing: 12

            Rectangle {
                anchors.horizontalCenter: parent.horizontalCenter
                width: 68
                height: 68
                radius: 34
                color: root.theme ? root.theme.withAlpha(root.theme.ui.accent, 0.16) : "transparent"

                NuguText {
                    anchors.centerIn: parent
                    theme: root.theme
                    variant: "title"
                    selected: true
                    text: "⌁"
                }
            }

            NuguText {
                anchors.horizontalCenter: parent.horizontalCenter
                theme: root.theme
                variant: "title"
                text: "no tray icons"
            }

            NuguText {
                anchors.horizontalCenter: parent.horizontalCenter
                theme: root.theme
                variant: "meta"
                text: "background apps show up here"
            }
        }
    }
}