foxygit / TV-Launcher Log in
commits tags

/qml/AppEditor.qml · 9.54 KB

raw
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import TvLauncher

// Dialog för att lägga till och redigera program och webbappar.
Popup {
    id: editor

    property string appId: ""
    property string error: ""

    parent: Overlay.overlay
    anchors.centerIn: parent
    width: Math.min(parent.width - Theme.dp(120), Theme.dp(1300))
    modal: true
    focus: true
    padding: Theme.dp(40)
    closePolicy: Popup.CloseOnEscape

    background: Rectangle {
        color: Theme.bg
        radius: Theme.r(18)
        border.color: Theme.borderSoft
        border.width: Theme.dp(1)
    }
    Overlay.modal: Rectangle { color: Theme.scrim }

    function openFor(app) {
        appId = app.id || ""
        error = ""
        nameField.text = app.name || ""
        typeCycler.value = app.type === "web" ? "web" : "app"
        commandField.text = app.command || ""
        urlField.text = app.url || ""
        engineCycler.value = app.engine === "chromium" ? "chromium" : "builtin"
        const ua = app.userAgent || ""
        uaCycler.customValue = (ua !== "" && ua !== apps.tvUserAgent) ? ua : ""
        uaCycler.value = ua
        const cats = apps.knownCategories.slice()
        if (app.category && cats.indexOf(app.category) < 0)
            cats.push(app.category)
        categoryCycler.options = cats.map(c => ({ value: c, label: c }))
        categoryCycler.value = app.category || (typeCycler.value === "web" ? "Webb" : "Program")
        iconField.text = app.icon || ""
        controllerCycler.value = app.controllerMode || "auto"
        open()
    }

    function save() {
        const isWeb = typeCycler.value === "web"
        if (nameField.text.trim() === "") { error = "Ange ett namn."; nameField.forceActiveFocus(); return }
        if (isWeb && urlField.text.trim() === "") { error = "Ange en adress."; urlField.forceActiveFocus(); return }
        if (!isWeb && commandField.text.trim() === "") { error = "Ange ett kommando."; commandField.forceActiveFocus(); return }
        apps.save({
            id: appId,
            name: nameField.text,
            type: typeCycler.value,
            command: isWeb ? "" : commandField.text,
            url: isWeb ? urlField.text : "",
            engine: engineCycler.value,
            userAgent: isWeb ? uaCycler.value : "",
            category: categoryCycler.value,
            controllerMode: controllerCycler.value === "auto" ? "" : controllerCycler.value,
            icon: iconField.text
        })
        close()
    }

    onOpened: nameField.forceActiveFocus()

    component Label2: Text {
        Layout.preferredWidth: Theme.dp(220)
        color: Theme.textDim
        font.family: Theme.fontFamily
        font.pixelSize: Theme.dp(24)
    }

    component Field: TextField {
        Layout.fillWidth: true
        Layout.preferredHeight: Theme.dp(60)
        color: Theme.text
        placeholderTextColor: Theme.placeholder
        selectionColor: Theme.accent
        font.family: Theme.fontFamily
        font.pixelSize: Theme.dp(24)
        leftPadding: Theme.dp(16)
        background: Rectangle {
            radius: Theme.r(8)
            color: parent.activeFocus ? Theme.surfaceHi : Theme.surface
            border.width: parent.activeFocus ? Theme.dp(3) : Theme.dp(1)
            border.color: parent.activeFocus ? Theme.accent : Theme.border
        }
    }


    contentItem: RowLayout {
        spacing: Theme.dp(40)

        ColumnLayout {
            Layout.fillWidth: true
            spacing: Theme.dp(16)

            Text {
                text: editor.appId === "" ? "Lägg till" : "Redigera"
                color: Theme.text
                font.family: Theme.fontFamily
                font.pixelSize: Theme.dp(36)
                Layout.bottomMargin: Theme.dp(10)
            }

            RowLayout {
                Label2 { text: "Namn" }
                Field {
                    id: nameField
                    placeholderText: "t.ex. Jellyfin"
                    KeyNavigation.down: typeCycler
                    onAccepted: typeCycler.forceActiveFocus()
                }
            }
            RowLayout {
                Label2 { text: "Typ" }
                OptionCycler {
                    id: typeCycler
                    Layout.fillWidth: true
                    options: [{ value: "app", label: "Program" }, { value: "web", label: "Webbapp" }]
                    KeyNavigation.up: nameField
                    KeyNavigation.down: commandField
                }
            }
            RowLayout {
                visible: typeCycler.value === "app"
                Label2 { text: "Kommando" }
                Field {
                    id: commandField
                    placeholderText: "t.ex. retroarch  eller  mpv --fs ~/Musik"
                    KeyNavigation.up: typeCycler
                    KeyNavigation.down: urlField
                }
            }
            RowLayout {
                visible: typeCycler.value === "web"
                Label2 { text: "Adress" }
                Field {
                    id: urlField
                    placeholderText: "t.ex. http://192.168.1.10:8096"
                    inputMethodHints: Qt.ImhUrlCharactersOnly
                    KeyNavigation.up: commandField
                    KeyNavigation.down: engineCycler
                }
            }
            RowLayout {
                visible: typeCycler.value === "web"
                Label2 { text: "Motor" }
                OptionCycler {
                    id: engineCycler
                    Layout.fillWidth: true
                    options: [
                        { value: "builtin", label: "Inbyggd (snabb, styrs med kontrollen)" },
                        { value: "chromium", label: "Chromium (för DRM: Netflix, Spotify ...)" }
                    ]
                    KeyNavigation.up: urlField
                    KeyNavigation.down: uaCycler
                }
            }
            RowLayout {
                visible: typeCycler.value === "web"
                Label2 { text: "Visa som" }
                OptionCycler {
                    id: uaCycler
                    property string customValue: ""
                    Layout.fillWidth: true
                    options: {
                        const o = [{ value: "", label: "Vanlig webbläsare" },
                                   { value: apps.tvUserAgent, label: "Smart-TV (t.ex. youtube.com/tv)" }]
                        if (customValue !== "")
                            o.push({ value: customValue, label: "Egen user agent" })
                        return o
                    }
                    KeyNavigation.up: engineCycler
                    KeyNavigation.down: categoryCycler
                }
            }
            RowLayout {
                Label2 { text: "Kategori" }
                OptionCycler {
                    id: categoryCycler
                    Layout.fillWidth: true
                    KeyNavigation.up: uaCycler
                    KeyNavigation.down: controllerCycler
                }
            }
            RowLayout {
                Label2 { text: "Handkontroll" }
                OptionCycler {
                    id: controllerCycler
                    Layout.fillWidth: true
                    options: [
                        { value: "auto", label: "Automatiskt" },
                        { value: "keyboard", label: "Mus och tangentbord" },
                        { value: "gamepad", label: "Handkontroll (spel)" }
                    ]
                    KeyNavigation.up: categoryCycler
                    KeyNavigation.down: iconField
                }
            }
            RowLayout {
                Label2 { text: "Ikon" }
                Field {
                    id: iconField
                    placeholderText: typeCycler.value === "web" ? "Tomt = hämtas från webbplatsen"
                                                                : "Ikonnamn (t.ex. retroarch) eller sökväg"
                    KeyNavigation.up: controllerCycler
                    KeyNavigation.down: saveButton
                }
            }

            Text {
                visible: editor.error !== ""
                text: editor.error
                color: Theme.danger
                font.family: Theme.fontFamily
                font.pixelSize: Theme.dp(22)
            }

            RowLayout {
                Layout.topMargin: Theme.dp(16)
                spacing: Theme.dp(20)
                TvButton {
                    id: saveButton
                    text: "Spara"
                    onClicked: editor.save()
                    KeyNavigation.up: iconField
                    KeyNavigation.right: cancelButton
                }
                TvButton {
                    id: cancelButton
                    text: "Avbryt"
                    onClicked: editor.close()
                    KeyNavigation.up: iconField
                    KeyNavigation.right: deleteButton
                }
                TvButton {
                    id: deleteButton
                    visible: editor.appId !== ""
                    danger: true
                    text: "Ta bort"
                    onClicked: { apps.remove(editor.appId); editor.close() }
                    KeyNavigation.up: iconField
                }
                Item { Layout.fillWidth: true }
            }
        }

        // Förhandsvisning av rutan.
        AppTile {
            Layout.alignment: Qt.AlignTop
            Layout.topMargin: Theme.dp(80)
            enabled: false
            name: nameField.text || "Namn"
            icon: iconField.text
            isWeb: typeCycler.value === "web"
        }
    }
}