foxygit / TV-Launcher Log in
commits tags

/qml/CardList.qml · 1.76 KB

raw
import QtQuick
import TvLauncher

// Skrollbar kolumn med kort. Upp/ner flyttar mellan barn som har
// "focusable: true" och skrollar så att det markerade kortet syns.
Flickable {
    id: fl

    default property alias content: col.data
    property alias spacing: col.spacing

    contentHeight: col.implicitHeight + Theme.dp(40)
    clip: true
    boundsBehavior: Flickable.StopAtBounds

    function cards() {
        const out = []
        for (let i = 0; i < col.children.length; ++i) {
            const c = col.children[i]
            if (c.visible && c.focusable === true)
                out.push(c)
        }
        return out
    }

    function focusFirst() {
        const cs = cards()
        if (cs.length > 0) { cs[0].forceActiveFocus(); ensureVisible(cs[0]) }
        else fl.forceActiveFocus()
    }

    function ensureVisible(item) { ensureRange(item.y, item.height) }

    // y är relativt kolumnens topp.
    function ensureRange(y, h) {
        const m = Theme.dp(20)
        if (y - m < contentY)
            contentY = Math.max(0, y - m)
        else if (y + h + m > contentY + height)
            contentY = Math.max(0, Math.min(contentHeight - height, y + h + m - height))
    }

    function move(d) {
        const cs = cards()
        let i = cs.findIndex(c => c.activeFocus)
        if (i < 0) { focusFirst(); return true }
        const t = cs[i + d]
        if (!t) return false
        t.forceActiveFocus()
        ensureVisible(t)
        return true
    }

    Keys.onUpPressed: (event) => event.accepted = move(-1)
    Keys.onDownPressed: (event) => event.accepted = move(1)

    Behavior on contentY { NumberAnimation { duration: 150; easing.type: Easing.OutCubic } }

    Column {
        id: col
        width: fl.width
        spacing: Theme.dp(12)
    }
}