Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/config/ConfigPanel.qml

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

Item {
    id: root
    required property Theme theme
    required property Config configService
    readonly property real naturalHeight: Math.max(sectionList.height, form.contentHeight)
    property int controlsGeneration: 0
    readonly property Item initialFocus: {
        if (controlsGeneration === 0)
            return null;
        const section = configService.section;
        const row = (section ? rowRepeater.itemAt(section.focusRow) : null) as FormRow;
        return row ? row.focusTarget : sectionButtons.itemAt(configService.sections.indexOf(section));
    }

    Connections {
        target: root.configService
        function onSectionChanged() {
            form.displayedSection = null;
            Qt.callLater(form.restoreScroll);
        }
    }

    component FormRow: Column {
        id: row
        required property ConfigRow modelData
        required property int index
        readonly property bool multiline: modelData.kind === ConfigRow.Text && modelData.lines > 1
        readonly property Item focusTarget: modelData.kind === ConfigRow.Choice ? choices.itemAt(root.configService.section.focusOption) : (modelData.kind === ConfigRow.Action ? action : (multiline ? (areaLoader.item as PopupTextArea)?.input ?? null : field.input))
        function rememberFocus(option: int): void {
            root.configService.section.focusRow = index;
            root.configService.section.focusOption = option;
        }
        width: rows.width
        spacing: root.theme.spaceSm
        visible: modelData.visible

        NuguText {
            width: parent.width
            visible: row.modelData.kind === ConfigRow.Choice || row.modelData.kind === ConfigRow.Text || row.modelData.kind === ConfigRow.Secret
            theme: root.theme
            text: row.modelData.label
        }
        Flow {
            width: parent.width
            spacing: root.theme.spaceSm
            visible: row.modelData.kind === ConfigRow.Choice
            Repeater {
                id: choices
                model: row.modelData.options
                delegate: PopupButton {
                    required property ConfigOption modelData
                    required property int index
                    theme: root.theme
                    text: modelData.label
                    accessibleName: modelData.label + " " + row.modelData.label
                    checked: row.modelData.value === modelData.value
                    enabled: row.modelData.enabled && !row.modelData.busy
                    onClicked: row.modelData.edited(modelData.value)
                    onControlFocusedChanged: if (controlFocused) {
                        row.rememberFocus(index);
                        Util.revealIn(this, form);
                    }
                }
            }
        }
        PopupButton {
            id: action
            visible: row.modelData.kind === ConfigRow.Action
            theme: root.theme
            text: row.modelData.label
            enabled: row.modelData.enabled && !row.modelData.busy
            onClicked: row.modelData.activated()
            onControlFocusedChanged: if (controlFocused) {
                row.rememberFocus(0);
                Util.revealIn(this, form);
            }
        }
        // Only a multi-line row creates its area: an invisible editor would still
        // sit in the keyboard focus chain and steal Tab from the field below.
        Loader {
            id: areaLoader

            width: parent.width
            active: row.multiline
            visible: active
            sourceComponent: PopupTextArea {
                id: area

                width: parent.width
                theme: root.theme
                lines: row.modelData.lines
                accessibleName: row.modelData.label
                text: row.modelData.value
                enabled: row.modelData.enabled && !row.modelData.busy
                onEdited: value => row.modelData.edited(value)
                Connections {
                    target: area.input
                    function onActiveFocusChanged() {
                        if (area.input.activeFocus) {
                            row.rememberFocus(0);
                            Util.revealIn(area, form);
                        }
                    }
                }
            }
        }
        PopupTextField {
            id: field
            width: parent.width
            visible: !row.multiline && (row.modelData.kind === ConfigRow.Text || row.modelData.kind === ConfigRow.Secret)
            theme: root.theme
            accessibleName: row.modelData.label
            text: row.modelData.value
            echoMode: row.modelData.kind === ConfigRow.Secret ? TextInput.Password : TextInput.Normal
            enabled: row.modelData.enabled && !row.modelData.busy
            onEdited: value => row.modelData.edited(value)
            Connections {
                target: field.input
                function onActiveFocusChanged() {
                    if (field.input.activeFocus) {
                        row.rememberFocus(0);
                        Util.revealIn(field, form);
                    }
                }
            }
        }
        NuguText {
            width: parent.width
            visible: row.modelData.progress >= 0
            theme: root.theme
            variant: "meta"
            text: Math.round(Math.max(0, Math.min(1, row.modelData.progress)) * 100) + "%"
        }
        NuguText {
            width: parent.width
            visible: row.modelData.kind === ConfigRow.Status || row.modelData.status.length > 0
            theme: root.theme
            variant: "meta"
            wrapMode: Text.Wrap
            text: row.modelData.status || row.modelData.label
        }
        NuguText {
            width: parent.width
            visible: row.modelData.error.length > 0
            theme: root.theme
            color: root.theme.warning
            wrapMode: Text.Wrap
            text: row.modelData.error
        }
    }

    Column {
        id: sectionList
        width: 120
        spacing: root.theme.spaceSm
        Repeater {
            id: sectionButtons
            onItemAdded: root.controlsGeneration++
            model: root.configService.sections
            delegate: PopupButton {
                id: sectionButton
                required property ConfigSection modelData
                required property int index
                width: sectionList.width
                theme: root.theme
                text: modelData.label
                checked: root.configService.selectedSection === modelData.name
                onClicked: root.configService.selectSection(modelData.name)
                Keys.onDownPressed: {
                    const next = sectionButtons.itemAt((index + 1) % sectionButtons.count);
                    if (next) {
                        next.forceActiveFocus();
                        root.configService.selectSection(root.configService.sections[(index + 1) % sectionButtons.count].name);
                    }
                }
                Keys.onUpPressed: {
                    const previous = sectionButtons.itemAt((index + sectionButtons.count - 1) % sectionButtons.count);
                    if (previous) {
                        previous.forceActiveFocus();
                        root.configService.selectSection(root.configService.sections[(index + sectionButtons.count - 1) % sectionButtons.count].name);
                    }
                }
            }
        }
    }

    Flickable {
        id: form
        property ConfigSection displayedSection: null
        function restoreScroll(): void {
            const section = root.configService.section;
            if (!section)
                return;
            contentY = Math.max(0, Math.min(section.scrollY, contentHeight - height));
            displayedSection = section;
        }
        onContentYChanged: if (displayedSection && displayedSection === root.configService.section)
            displayedSection.scrollY = contentY
        Component.onCompleted: Qt.callLater(restoreScroll)
        anchors.left: sectionList.right
        anchors.leftMargin: root.theme.spaceLg
        anchors.right: parent.right
        height: parent.height
        contentWidth: width
        contentHeight: rows.height
        clip: true
        boundsBehavior: Flickable.StopAtBounds
        Column {
            id: rows
            width: form.width
            spacing: root.theme.spaceMd
            Repeater {
                id: rowRepeater
                onItemAdded: root.controlsGeneration++
                model: root.configService.section ? root.configService.section.rows : []
                delegate: FormRow {}
            }
        }
    }
}