foxygit / TV-Launcher Log in
commits tags

/qml/OptionCycler.qml · 2.19 KB

raw
import QtQuick
import TvLauncher

// Väljare som fungerar med handkontroll: vänster/höger eller A byter värde.
// Upp/ner lämnas fria för att flytta mellan fält.
FocusScope {
    id: cycler

    property var options: []   // [{ value: ..., label: "..." }]
    property var value
    readonly property int index: {
        for (let i = 0; i < options.length; ++i)
            if (options[i].value === value) return i
        return -1
    }

    implicitHeight: Theme.dp(60)
    activeFocusOnTab: true

    function step(d) {
        if (options.length === 0) return
        const i = index < 0 ? 0 : (index + d + options.length) % options.length
        value = options[i].value
    }

    Keys.onLeftPressed: step(-1)
    Keys.onRightPressed: step(1)
    Keys.onReturnPressed: step(1)
    Keys.onEnterPressed: step(1)

    Rectangle {
        anchors.fill: parent
        radius: Theme.r(8)
        color: cycler.activeFocus ? Theme.surfaceHi : Theme.surface
        border.width: cycler.activeFocus ? Theme.dp(3) : Theme.dp(1)
        border.color: cycler.activeFocus ? Theme.accent : Theme.border

        Text {
            anchors { left: parent.left; leftMargin: Theme.dp(18); verticalCenter: parent.verticalCenter }
            text: "‹"
            color: Theme.textDim
            font.pixelSize: Theme.dp(32)
        }
        Text {
            anchors.centerIn: parent
            width: parent.width - Theme.dp(100)
            horizontalAlignment: Text.AlignHCenter
            elide: Text.ElideRight
            text: cycler.index >= 0 ? cycler.options[cycler.index].label : ""
            color: Theme.text
            font.family: Theme.fontFamily
            font.pixelSize: Theme.dp(24)
        }
        Text {
            anchors { right: parent.right; rightMargin: Theme.dp(18); verticalCenter: parent.verticalCenter }
            text: "›"
            color: Theme.textDim
            font.pixelSize: Theme.dp(32)
        }
    }

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: (mouse) => {
            cycler.forceActiveFocus()
            cycler.step(mouse.button === Qt.RightButton || mouse.x < width / 3 ? -1 : 1)
        }
    }
}