foxygit / TV-Launcher Log in
commits tags

/qml/AppearanceDialog.qml · 7.99 KB

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

// "Utseende och förhandsvisning" för en app: egen bild och text i
// förhandsvisningen överst på hemskärmen, om ikonen ska synas och rutans färg.
// Förhandsvisningen i dialogen uppdateras medan man ändrar.
Popup {
    id: dlg

    property string appId: ""
    property var base: ({})
    property string image: ""
    property string heroText: ""
    property bool showIcon: true
    property string tileColor: ""

    readonly property var draft: Object.assign({}, base, {
        heroImage: image, heroText: heroText.trim(), heroShowIcon: showIcon, color: tileColor
    })

    function openFor(app) {
        base = apps.get(app.id)
        appId = app.id
        image = base.heroImage || ""
        heroText = base.heroText || ""
        showIcon = base.heroShowIcon !== false
        tileColor = base.color || ""
        imageField.text = image
        textField.text = heroText
        open()
    }

    function save() {
        apps.patch(appId, {
            heroImage: image.trim(),
            heroText: heroText.trim(),
            heroShowIcon: showIcon ? "" : false,   // tomt = standard (visa)
            color: tileColor
        })
        close()
    }

    parent: Overlay.overlay
    anchors.centerIn: parent
    width: Math.min(parent.width - Theme.dp(100), Theme.dp(1600))
    modal: true
    focus: true
    padding: Theme.dp(36)
    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 }

    onOpened: imageButton.forceActiveFocus()

    component Label2: Text {
        Layout.preferredWidth: Theme.dp(200)
        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
        }
    }

    ImagePicker {
        id: imagePicker
        onPicked: (path) => { imageField.text = path; dlg.image = path }
        onClosed: imageButton.forceActiveFocus()
    }

    contentItem: ColumnLayout {
        spacing: Theme.dp(16)

        Text {
            text: "Utseende: " + (dlg.base.name || "")
            color: Theme.text
            font.family: Theme.fontFamily
            font.pixelSize: Theme.dp(36)
        }

        // Förhandsvisning: hemskärmens header i liten skala + rutan.
        RowLayout {
            Layout.fillWidth: true
            spacing: Theme.dp(28)

            Item {
                id: previewBox
                Layout.fillWidth: true
                Layout.preferredHeight: Math.min(width * Theme.dp(600) / dlg.parent.width, Theme.dp(280))
                clip: true

                Backdrop { anchors.fill: parent }
                Rectangle {
                    anchors.fill: parent
                    gradient: Gradient {
                        orientation: Gradient.Horizontal
                        GradientStop { position: 0.0; color: "#b0000000" }
                        GradientStop { position: 0.45; color: "#60000000" }
                        GradientStop { position: 0.75; color: "#00000000" }
                    }
                }
                HeroView {
                    width: dlg.parent.width
                    height: previewBox.height / scale
                    transformOrigin: Item.TopLeft
                    scale: previewBox.width / dlg.parent.width
                    app: dlg.draft
                }
                Rectangle {
                    anchors.fill: parent
                    color: "transparent"
                    radius: Theme.r(10)
                    border.color: Theme.border
                    border.width: Theme.dp(1)
                }
            }

            Item {
                Layout.alignment: Qt.AlignVCenter
                Layout.preferredWidth: Theme.tile(340) * 0.8
                Layout.preferredHeight: Theme.tile(300) * 0.8
                AppTile {
                    scale: 0.8
                    transformOrigin: Item.TopLeft
                    enabled: false
                    name: dlg.base.name || ""
                    subtitle: "Rutan"
                    icon: dlg.base.icon || ""
                    customColor: dlg.tileColor
                    isWeb: dlg.base.type === "web"
                }
            }
        }

        RowLayout {
            Layout.fillWidth: true
            spacing: Theme.dp(16)
            Label2 { text: "Bild" }
            Field {
                id: imageField
                placeholderText: "Sökväg (~/Bilder/...) eller webbadress (https://...)"
                onTextChanged: dlg.image = text
                KeyNavigation.right: imageButton
                KeyNavigation.down: textField
            }
            TvButton {
                id: imageButton
                text: "Välj bild …"
                onClicked: imagePicker.open()
                KeyNavigation.left: imageField
                KeyNavigation.right: clearImageButton
                KeyNavigation.down: textField
            }
            TvButton {
                id: clearImageButton
                text: "Ingen bild"
                onClicked: { imageField.text = ""; dlg.image = "" }
                KeyNavigation.left: imageButton
                KeyNavigation.down: textField
            }
        }

        RowLayout {
            Layout.fillWidth: true
            spacing: Theme.dp(16)
            Label2 { text: "Text" }
            Field {
                id: textField
                placeholderText: "Egen text under namnet (tomt = adress eller kommando)"
                onTextChanged: dlg.heroText = text
                KeyNavigation.up: imageButton
                KeyNavigation.down: iconCard
            }
        }

        SettingsCard {
            id: iconCard
            Layout.fillWidth: true
            icon: "image"
            title: "Visa ikonen i förhandsvisningen"
            toggle: dlg.showIcon
            onActivated: dlg.showIcon = !dlg.showIcon
            KeyNavigation.up: textField
            KeyNavigation.down: colorCard
        }

        SettingsCard {
            id: colorCard
            Layout.fillWidth: true
            icon: "palette"
            title: "Rutans färg"
            subtitle: "Automatisk följer inställningen under Tema"
            options: Theme.tileColorChoices
            value: dlg.tileColor
            onChosen: (v) => dlg.tileColor = v
            KeyNavigation.up: iconCard
            KeyNavigation.down: saveButton
        }

        RowLayout {
            Layout.topMargin: Theme.dp(8)
            spacing: Theme.dp(20)
            TvButton {
                id: saveButton
                text: "Spara"
                onClicked: dlg.save()
                KeyNavigation.up: colorCard
                KeyNavigation.right: cancelButton
            }
            TvButton {
                id: cancelButton
                text: "Avbryt"
                onClicked: dlg.close()
                KeyNavigation.up: colorCard
                KeyNavigation.right: resetButton
            }
            TvButton {
                id: resetButton
                text: "Återställ"
                onClicked: {
                    imageField.text = ""; textField.text = ""
                    dlg.image = ""; dlg.heroText = ""; dlg.showIcon = true; dlg.tileColor = ""
                }
                KeyNavigation.up: colorCard
            }
            Item { Layout.fillWidth: true }
        }
    }
}