Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/launcher/LauncherPanel.qml

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

FocusScope {
    id: root

    required property Theme theme
    required property Launcher launcherService
    // Intent to the host: close this popup after a successful launch.
    signal closeRequested(string reason)
    property var sessionState: ({
            "query": "",
            "selectedId": "",
            "scrollY": 0
        })
    readonly property string query: String(sessionState.query || "")
    // Desktop-entry facts are secondary detail; the disclosure stays as the user left it.
    readonly property bool detailsExpanded: !!sessionState.detailsExpanded
    property bool stateReady: false
    readonly property var results: launcherService ? launcherService.viewRows(query) : []
    readonly property int selectedIndex: selectionIndex()
    // selectedIndex may still be stale for one evaluation when results change.
    readonly property var selectedRow: results[selectedIndex] || ({})
    readonly property var selectedActions: selectedRow.actions || []
    readonly property var selectedDetails: selectedRow.details || []
    readonly property int rowHeight: 52
    readonly property int rowSpacing: 4
    readonly property int rowStride: rowHeight + rowSpacing
    readonly property int rightWidth: Math.min(300, Math.max(260, Math.round(width * 0.36)))

    function updateState(values) {
        Object.assign(sessionState, values);
        // The owner may supply a plain JS object rather than a QtObject.
        sessionStateChanged();
    }

    function selectionIndex() {
        const index = results.findIndex(row => String(row.id) === String(sessionState.selectedId));
        return index >= 0 ? index : (results.length > 0 ? 0 : -1);
    }

    function reconcileSelection() {
        const id = selectedIndex >= 0 ? String(results[selectedIndex].id) : "";
        if (sessionState.selectedId !== id)
            updateState({
                "selectedId": id
            });
        if (stateReady)
            ensureSelectedVisible();
    }

    function setQuery(text) {
        updateState({
            "query": String(text || "")
        });
    }

    function clearSearch() {
        setQuery("");
        searchInput.input.forceActiveFocus();
    }

    function toggleDetails() {
        updateState({
            "detailsExpanded": !detailsExpanded
        });
    }

    function rememberScroll() {
        if (stateReady && sessionState.scrollY !== resultsFlick.contentY)
            updateState({
                "scrollY": resultsFlick.contentY
            });
    }

    function ensureSelectedVisible() {
        if (selectedIndex < 0)
            return;
        const top = selectedIndex * rowStride;
        const bottom = top + rowHeight;
        if (top < resultsFlick.contentY)
            updateState({
                "scrollY": top
            });
        else if (bottom > resultsFlick.contentY + resultsFlick.height)
            updateState({
                "scrollY": bottom - resultsFlick.height
            });
    }

    function revealAction(item) {
        const top = item.mapToItem(detailContent, 0, 0).y;
        if (top < detailFlick.contentY)
            detailFlick.contentY = top;
        else if (top + item.height > detailFlick.contentY + detailFlick.height)
            detailFlick.contentY = top + item.height - detailFlick.height;
    }

    // A launch completes the search: the next open starts empty, frecent apps
    // first. Only dismissal without a launch keeps the query.
    function completeLaunch() {
        updateState({
            "query": "",
            "selectedId": "",
            "scrollY": 0
        });
        closeRequested("launcher-activated");
    }

    function activateSelected() {
        if (!launcherService || results.length === 0)
            return;

        launcherService.activate(selectedRow);
        completeLaunch();
    }

    function activateAction(index) {
        if (!launcherService || results.length === 0 || index < 0 || index >= selectedActions.length)
            return;

        launcherService.activateAction(selectedRow, index);
        completeLaunch();
    }

    function select(index) {
        if (results.length === 0)
            return;

        const i = Math.max(0, Math.min(results.length - 1, Number(index)));
        updateState({
            "selectedId": String(results[i].id)
        });
        ensureSelectedVisible();
    }

    function keyAction(event) {
        if (event.modifiers & Qt.ControlModifier) {
            const n = Number(event.text);
            if (n >= 1 && n <= selectedActions.length) {
                event.accepted = true;
                if (!event.isAutoRepeat)
                    activateAction(n - 1);
            }
        }
    }

    width: parent ? parent.width : 0
    height: parent ? parent.height : 0
    focus: true
    onResultsChanged: selectionTimer.restart()

    Timer {
        id: selectionTimer
        interval: 0
        running: true
        onTriggered: {
            root.reconcileSelection();
            if (!root.stateReady) {
                root.stateReady = true;
                searchInput.input.forceActiveFocus();
                // A restored query starts selected: typing replaces it.
                searchInput.input.selectAll();
            }
        }
    }

    Keys.onUpPressed: event => {
        event.accepted = true;
        select(selectedIndex - 1);
    }
    Keys.onDownPressed: event => {
        event.accepted = true;
        select(selectedIndex + 1);
    }
    Keys.onReturnPressed: event => {
        event.accepted = true;
        if (!event.isAutoRepeat)
            activateSelected();
    }
    Keys.onEnterPressed: event => {
        event.accepted = true;
        if (!event.isAutoRepeat)
            activateSelected();
    }
    Keys.onPressed: event => keyAction(event)

    Shortcut {
        sequences: ["Return", "Enter"]
        enabled: searchInput.input.activeFocus
        autoRepeat: false
        onActivated: root.activateSelected()
    }

    StableListModel {
        id: resultRows
        rows: root.results
    }

    StableListModel {
        id: actionRows
        rows: root.selectedActions
    }

    Item {
        anchors.fill: parent
        anchors.margins: 1

        PopupTextField {
            id: searchInput
            objectName: "searchField"

            theme: root.theme
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: parent.top
            anchors.margins: 8
            height: 38
            icon: ""
            placeholderText: "search apps"
            text: root.query
            onEdited: text => root.setQuery(text)
            Keys.onUpPressed: event => {
                event.accepted = true;
                root.select(root.selectedIndex - 1);
            }
            Keys.onDownPressed: event => {
                event.accepted = true;
                root.select(root.selectedIndex + 1);
            }
            Keys.onPressed: event => root.keyAction(event)
        }

        // Results and the selected app's actions; replaced by one empty state
        // when nothing matches.
        Row {
            visible: root.results.length > 0
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: searchInput.bottom
            anchors.bottom: parent.bottom

            Item {
                width: Math.max(1, parent.width - root.rightWidth)
                height: parent.height

                Flickable {
                    id: resultsFlick
                    objectName: "launcherResultList"

                    anchors.fill: parent
                    anchors.margins: 8
                    contentHeight: resultColumn.implicitHeight
                    contentY: Math.max(0, Math.min(Number(root.sessionState.scrollY || 0), contentHeight - height))
                    onContentYChanged: root.rememberScroll()
                    clip: true
                    boundsBehavior: Flickable.StopAtBounds

                    Column {
                        id: resultColumn

                        width: resultsFlick.width
                        spacing: root.rowSpacing

                        Repeater {
                            model: resultRows

                            delegate: PopupCard {
                                id: rrow

                                required property int index
                                required property var rowData
                                readonly property var modelData: rowData
                                readonly property bool isSelected: index === root.selectedIndex
                                // A desktop entry may name itself by its id; the row names the app.
                                readonly property string title: Human.label(modelData.title, "app")

                                theme: root.theme
                                padding: 0
                                active: isSelected
                                hoverable: true
                                externallyPressed: rrowArea.pressed
                                activeFocusOnTab: isSelected
                                Accessible.role: Accessible.ListItem
                                Accessible.name: rrow.title
                                Accessible.selected: isSelected
                                onActiveFocusChanged: if (activeFocus)
                                    root.select(index)
                                width: parent.width
                                height: root.rowHeight

                                Item {
                                    id: iconSlot

                                    anchors.left: parent.left
                                    anchors.leftMargin: 11
                                    anchors.verticalCenter: parent.verticalCenter
                                    width: 30
                                    height: 30

                                    Rectangle {
                                        anchors.fill: parent
                                        radius: root.theme.radiusTile
                                        color: root.theme ? root.theme.content.backdrop : "#121212"
                                    }

                                    IconImage {
                                        id: appIcon

                                        anchors.fill: parent
                                        anchors.margins: 4
                                        visible: appIcon.status === Image.Ready
                                        source: String(rrow.modelData.icon || "").length > 0 ? Quickshell.iconPath(String(rrow.modelData.icon), true) : ""
                                    }

                                    Text {
                                        anchors.centerIn: parent
                                        visible: !appIcon.visible
                                        text: String(rrow.modelData.glyph || "")
                                        font.family: root.theme.fontIcon
                                        font.pixelSize: 17
                                        color: root.theme ? root.theme.ui.accent : "#d86dd8"
                                    }
                                }

                                Column {
                                    anchors.left: iconSlot.right
                                    anchors.leftMargin: 12
                                    anchors.right: ordinal.left
                                    anchors.rightMargin: 10
                                    anchors.verticalCenter: parent.verticalCenter
                                    spacing: 2

                                    NuguText {
                                        theme: root.theme
                                        selected: rrow.isSelected
                                        text: rrow.title
                                        width: parent.width
                                    }

                                    NuguText {
                                        theme: root.theme
                                        variant: "meta"
                                        selected: rrow.isSelected
                                        text: Human.text(rrow.modelData.subtitle || rrow.modelData.comment || "app", "app")
                                        width: parent.width
                                        opacity: rrow.isSelected ? 0.82 : 0.72
                                    }
                                }

                                NuguText {
                                    id: ordinal

                                    anchors.right: parent.right
                                    anchors.rightMargin: 12
                                    anchors.verticalCenter: parent.verticalCenter
                                    theme: root.theme
                                    variant: "mono"
                                    selected: rrow.isSelected
                                    text: rrow.isSelected ? "enter" : ""
                                    width: 42
                                    horizontalAlignment: Text.AlignRight
                                    opacity: rrow.isSelected ? 0.8 : 0.52
                                }

                                MouseArea {
                                    id: rrowArea
                                    anchors.fill: parent
                                    hoverEnabled: true
                                    cursorShape: Qt.PointingHandCursor
                                    onClicked: {
                                        root.select(rrow.index);
                                        rrow.forceActiveFocus();
                                    }
                                    onDoubleClicked: root.activateSelected()
                                }
                            }
                        }
                    }
                }
            }

            Item {
                width: root.rightWidth
                height: parent.height

                PopupDivider {
                    anchors.left: parent.left
                    anchors.top: parent.top
                    anchors.bottom: parent.bottom
                    theme: root.theme
                }

                Flickable {
                    id: detailFlick
                    objectName: "launcherDetails"

                    anchors.fill: parent
                    anchors.leftMargin: root.theme.spaceLg
                    anchors.rightMargin: root.theme.spaceSm
                    anchors.topMargin: root.theme.spaceSm
                    anchors.bottomMargin: root.theme.spaceSm
                    contentHeight: detailContent.implicitHeight
                    clip: true
                    boundsBehavior: Flickable.StopAtBounds

                    Column {
                        id: detailContent

                        width: detailFlick.width
                        spacing: 6

                        // The selected row already names the app; the pane shows its icon and actions.
                        Item {
                            objectName: "launcherDetailIcon"
                            width: 56
                            height: 56

                            Rectangle {
                                anchors.fill: parent
                                radius: root.theme.radiusTile
                                color: root.theme ? root.theme.content.backdrop : "#121212"
                            }

                            IconImage {
                                id: detailIcon

                                anchors.fill: parent
                                anchors.margins: 8
                                visible: detailIcon.status === Image.Ready
                                source: String(root.selectedRow.icon || "").length > 0 ? Quickshell.iconPath(String(root.selectedRow.icon), true) : ""
                            }

                            Text {
                                anchors.centerIn: parent
                                visible: !detailIcon.visible
                                text: String(root.selectedRow.glyph || "")
                                font.family: root.theme.fontIcon
                                font.pixelSize: 27
                                color: root.theme ? root.theme.ui.accent : "#d86dd8"
                            }
                        }

                        Item {
                            width: 1
                            height: root.theme.spaceMd
                        }

                        PopupButton {
                            id: openButton

                            theme: root.theme
                            width: parent.width
                            height: 42
                            text: "open"
                            role: "primary"
                            enabled: root.results.length > 0 && !!root.launcherService
                            onActiveFocusChanged: if (activeFocus)
                                root.revealAction(openButton)
                            onClicked: root.activateSelected()
                        }

                        Repeater {
                            model: actionRows

                            delegate: PopupButton {
                                id: actionRow

                                required property int index
                                required property var rowData
                                readonly property var modelData: rowData

                                theme: root.theme
                                width: parent.width
                                height: 42
                                text: Human.label(modelData.title, "action")
                                onActiveFocusChanged: if (activeFocus)
                                    root.revealAction(actionRow)
                                onClicked: root.activateAction(index)
                            }
                        }

                        Item {
                            visible: detailsToggle.visible
                            width: 1
                            height: root.theme.spaceSm
                        }

                        PopupButton {
                            id: detailsToggle

                            objectName: "launcherDetailsToggle"
                            visible: root.selectedDetails.length > 0
                            theme: root.theme
                            width: parent.width
                            height: 34
                            text: "details"
                            icon: root.detailsExpanded ? "\uF106" : "\uF107"
                            accessibleName: (root.detailsExpanded ? "collapse " : "expand ") + text
                            tooltip: accessibleName
                            disclosure: true
                            onActiveFocusChanged: if (activeFocus)
                                root.revealAction(detailsToggle)
                            onClicked: root.toggleDetails()
                        }

                        PopupCard {
                            objectName: "launcherEntryDetails"
                            visible: detailsToggle.visible && root.detailsExpanded
                            theme: root.theme
                            padding: 0
                            width: parent.width
                            height: visible ? detailColumn.implicitHeight + 20 : 0

                            Column {
                                id: detailColumn

                                anchors.left: parent.left
                                anchors.right: parent.right
                                anchors.top: parent.top
                                anchors.margins: 10
                                spacing: 6

                                Repeater {
                                    model: root.selectedDetails

                                    delegate: Row {
                                        id: detailRow

                                        required property var modelData

                                        width: parent.width
                                        spacing: 10

                                        NuguText {
                                            theme: root.theme
                                            variant: "meta"
                                            text: detailRow.modelData.label || ""
                                            width: 70
                                            opacity: 0.62
                                        }

                                        NuguText {
                                            theme: root.theme
                                            variant: "meta"
                                            text: Human.text(detailRow.modelData.value || "-")
                                            width: Math.max(1, parent.width - 80)
                                            horizontalAlignment: Text.AlignRight
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Nothing to launch: one sentence, and clearing the search where a query hides the apps.
        Item {
            objectName: "launcherEmptyState"
            visible: root.results.length === 0
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.top: searchInput.bottom
            anchors.bottom: parent.bottom

            Column {
                anchors.centerIn: parent
                width: Math.min(parent.width - root.theme.spaceLg * 4, 420)
                spacing: root.theme.spaceSm

                Text {
                    width: parent.width
                    horizontalAlignment: Text.AlignHCenter
                    font.family: root.theme.fontIcon
                    font.pixelSize: 24
                    color: root.theme.ui.minor
                    text: "\uF002"
                    Accessible.ignored: true
                }

                NuguText {
                    width: parent.width
                    horizontalAlignment: Text.AlignHCenter
                    theme: root.theme
                    variant: "title"
                    text: root.query.length > 0 ? `no apps match "${root.query}"` : "no apps found"
                }

                NuguText {
                    visible: root.query.length === 0
                    width: parent.width
                    horizontalAlignment: Text.AlignHCenter
                    theme: root.theme
                    variant: "meta"
                    color: root.theme.ui.minor
                    text: "installed apps appear here"
                    wrapMode: Text.WordWrap
                }

                PopupButton {
                    visible: root.query.length > 0
                    anchors.horizontalCenter: parent.horizontalCenter
                    theme: root.theme
                    width: implicitWidth
                    text: "clear search"
                    onClicked: root.clearSearch()
                }
            }
        }
    }
}