foxygit / TV-Launcher Log in
commits tags

/qml/SettingsCard.qml · 6.11 KB

raw
import QtQuick
import TvLauncher

// Rad på inställningssidorna: ikon, rubrik, undertext och valfri
// av/på-knapp, bock eller text till höger.
Rectangle {
    id: card

    property bool focusable: true
    property string icon
    property string appIcon            // ikon via image://icon (appar)
    property string title
    property string subtitle
    property string valueText
    property var toggle: undefined     // true/false visar en av/på-knapp
    property bool checked: false        // visar en bock
    property bool busy: false
    // Val som bläddras med vänster/höger: [{ value, label }]
    property var options: []
    property var value
    readonly property int optionIndex: options.findIndex(o => o.value === value)

    signal chosen(var value)

    function cycle(d) {
        if (options.length === 0) return false
        const i = ((optionIndex < 0 ? 0 : optionIndex) + d + options.length) % options.length
        card.chosen(options[i].value)
        return true
    }

    signal activated()
    signal menuRequested()

    width: parent ? parent.width : Theme.dp(800)
    implicitHeight: Math.max(Theme.dp(88), col.implicitHeight + Theme.dp(32))
    height: implicitHeight
    radius: Theme.r(10)
    color: activeFocus ? Theme.cardFocus : Theme.card
    border.width: activeFocus ? Theme.dp(3) : 0
    border.color: Theme.accent
    activeFocusOnTab: true

    Keys.onReturnPressed: options.length > 0 ? cycle(1) : card.activated()
    Keys.onEnterPressed: options.length > 0 ? cycle(1) : card.activated()
    Keys.onSpacePressed: card.activated()
    Keys.onLeftPressed: (event) => event.accepted = cycle(-1)
    Keys.onRightPressed: (event) => event.accepted = cycle(1)
    Keys.onMenuPressed: card.menuRequested()

    Item {
        id: ic
        visible: card.icon !== "" || card.appIcon !== ""
        anchors { left: parent.left; leftMargin: Theme.dp(26); verticalCenter: parent.verticalCenter }
        width: card.appIcon !== "" ? Theme.dp(52) : Theme.dp(40); height: width
        Icon {
            anchors.fill: parent
            visible: card.appIcon === ""
            name: card.icon
        }
        Image {
            id: appImg
            anchors.fill: parent
            visible: card.appIcon !== "" && status === Image.Ready && implicitWidth > 1
            sourceSize: Qt.size(128, 128)
            asynchronous: true
            source: card.appIcon !== "" ? "image://icon/" + encodeURIComponent(card.appIcon) : ""
        }
        Rectangle {
            anchors.fill: parent
            visible: card.appIcon !== "" && !appImg.visible
            radius: Theme.r(8)
            color: Qt.darker(Theme.colorFor(card.title), 1.15)
            Text {
                anchors.centerIn: parent
                text: card.title.charAt(0).toUpperCase()
                color: Theme.text
                font.pixelSize: Theme.dp(28)
                font.weight: Font.DemiBold
            }
        }
    }

    Column {
        id: col
        anchors {
            left: ic.visible ? ic.right : parent.left
            leftMargin: Theme.dp(24)
            right: rightRow.left
            rightMargin: Theme.dp(20)
            verticalCenter: parent.verticalCenter
        }
        spacing: Theme.dp(6)
        Text {
            width: parent.width
            text: card.title
            color: Theme.text
            elide: Text.ElideRight
            font.family: Theme.fontFamily
            font.pixelSize: Theme.dp(28)
        }
        Text {
            width: parent.width
            visible: text !== ""
            text: card.subtitle
            color: Theme.textDim
            wrapMode: Text.Wrap
            font.family: Theme.fontFamily
            font.pixelSize: Theme.dp(22)
        }
    }

    Row {
        id: rightRow
        anchors { right: parent.right; rightMargin: Theme.dp(26); verticalCenter: parent.verticalCenter }
        spacing: Theme.dp(16)

        Text {
            anchors.verticalCenter: parent.verticalCenter
            visible: card.busy
            text: "…"
            color: Theme.textDim
            font.pixelSize: Theme.dp(32)
            SequentialAnimation on opacity {
                running: card.busy; loops: Animation.Infinite
                NumberAnimation { to: 0.2; duration: 500 }
                NumberAnimation { to: 1; duration: 500 }
            }
        }
        Text {
            anchors.verticalCenter: parent.verticalCenter
            visible: card.valueText !== ""
            text: card.valueText
            color: Theme.textDim
            font.family: Theme.fontFamily
            font.pixelSize: Theme.dp(24)
        }
        Text {
            anchors.verticalCenter: parent.verticalCenter
            visible: card.options.length > 0
            text: "◀   " + (card.optionIndex >= 0 ? card.options[card.optionIndex].label : "") + "   ▶"
            color: card.activeFocus ? Theme.text : Theme.textDim
            font.family: Theme.fontFamily
            font.pixelSize: Theme.dp(24)
        }
        Icon {
            anchors.verticalCenter: parent.verticalCenter
            visible: card.checked
            width: Theme.dp(34); height: width
            name: "check"
        }
        Rectangle {
            anchors.verticalCenter: parent.verticalCenter
            visible: card.toggle !== undefined
            width: Theme.dp(72); height: Theme.dp(38)
            radius: height / 2
            color: card.toggle === true ? Theme.accent : Theme.track
            Rectangle {
                width: parent.height - Theme.dp(8); height: width
                radius: width / 2
                y: Theme.dp(4)
                x: card.toggle === true ? parent.width - width - Theme.dp(4) : Theme.dp(4)
                color: "white"
                Behavior on x { NumberAnimation { duration: 120 } }
            }
        }
    }

    MouseArea {
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onClicked: (mouse) => {
            card.forceActiveFocus()
            if (mouse.button === Qt.RightButton) card.menuRequested()
            else if (card.options.length > 0) card.cycle(1)
            else card.activated()
        }
    }
}