foxygit / TV-Launcher Log in
commits tags

/qml/SliderCard.qml · 2.52 KB

raw
import QtQuick
import TvLauncher

// Reglage som styrs med vänster/höger (t.ex. volym).
Rectangle {
    id: card

    property bool focusable: true
    property string icon
    property string title
    property real value: 0          // 0..1
    property real step: 0.05
    property string valueText: Math.round(value * 100) + " %"

    signal moved(real value)
    signal activated()

    width: parent ? parent.width : Theme.dp(800)
    implicitHeight: Theme.dp(120)
    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

    function nudge(d) {
        const v = Math.max(0, Math.min(1, Math.round((value + d) / step) * step))
        card.moved(v)
    }
    Keys.onLeftPressed: nudge(-step)
    Keys.onRightPressed: nudge(step)
    Keys.onReturnPressed: card.activated()
    Keys.onEnterPressed: card.activated()

    Icon {
        id: ic
        visible: card.icon !== ""
        anchors { left: parent.left; leftMargin: Theme.dp(26); verticalCenter: parent.verticalCenter }
        width: Theme.dp(40); height: width
        name: card.icon
    }

    Text {
        id: titleText
        anchors { left: ic.visible ? ic.right : parent.left; leftMargin: Theme.dp(24); top: parent.top; topMargin: Theme.dp(20) }
        text: card.title
        color: Theme.text
        font.family: Theme.fontFamily
        font.pixelSize: Theme.dp(28)
    }
    Text {
        anchors { right: parent.right; rightMargin: Theme.dp(26); verticalCenter: titleText.verticalCenter }
        text: "◀  " + card.valueText + "  ▶"
        color: card.activeFocus ? Theme.text : Theme.textDim
        font.family: Theme.fontFamily
        font.pixelSize: Theme.dp(24)
    }

    Rectangle {
        id: track
        anchors { left: titleText.left; right: parent.right; rightMargin: Theme.dp(26); bottom: parent.bottom; bottomMargin: Theme.dp(24) }
        height: Theme.dp(8)
        radius: height / 2
        color: Theme.track
        Rectangle {
            width: parent.width * card.value
            height: parent.height
            radius: parent.radius
            color: Theme.accent
        }
    }

    MouseArea {
        anchors.fill: parent
        onClicked: (mouse) => {
            card.forceActiveFocus()
            const p = mapToItem(track, mouse.x, mouse.y)
            if (p.x >= 0 && p.x <= track.width)
                card.moved(Math.round(p.x / track.width / card.step) * card.step)
        }
    }
}