Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/PopupSlider.qml

Raw
pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Templates as T

FocusScope {
    id: root

    required property Theme theme
    property real value: 0
    property real step: 0.05
    property string accessibleName: ""
    // Opt-in presentation easing; ordinary controls + direct input stay immediate.
    property bool smoothChanges: false
    property bool keyboardEditing: false
    property real presentationValue: 0
    property bool presentationReady: false

    function synchronizeValue(immediate) {
        if (!presentationReady)
            return;
        valueTween.stop();
        if (immediate || !smoothChanges || slider.pressed || keyboardEditing || theme.lowPowerMode) {
            presentationValue = boundedValue;
        } else {
            valueTween.from = presentationValue;
            valueTween.to = boundedValue;
            valueTween.restart();
        }
    }

    onBoundedValueChanged: synchronizeValue(false)
    onSmoothChangesChanged: synchronizeValue(false)
    Component.onCompleted: {
        presentationReady = true;
        synchronizeValue(true);
    }
    // Optional confirmed display levels; ordinary callers retain one fill.
    property var levels: []
    signal moved(real value)

    readonly property real boundedValue: Math.max(0, Math.min(1, value))
    readonly property bool pressed: slider.pressed
    readonly property real position: slider.position
    readonly property real displayedValue: slider.value
    readonly property Item handle: slider.handle

    implicitWidth: 120
    // The full pointer target; the track and handle stay centred and unchanged.
    implicitHeight: theme.targetMin
    activeFocusOnTab: enabled
    opacity: enabled ? 1 : 0.48
    Accessible.name: accessibleName
    Accessible.ignored: true

    // Standalone animation: unlike a Behavior-owned animation, direct input
    // may safely stop it without Qt non-root-animation warnings.
    NumberAnimation {
        id: valueTween
        target: root
        property: "presentationValue"
        duration: 240
        easing.type: Easing.OutCubic
    }

    Connections {
        target: root.theme
        function onLowPowerModeChanged() {
            root.synchronizeValue(root.theme.lowPowerMode);
        }
    }

    // The 2 px focus border buttons use, around the track and its handle.
    Rectangle {
        visible: root.activeFocus
        anchors.fill: parent
        anchors.leftMargin: -root.theme.spaceXs
        anchors.rightMargin: -root.theme.spaceXs
        radius: root.theme.radiusControl
        color: "transparent"
        border.width: 2
        border.color: root.theme.content.focus
    }

    T.Slider {
        id: slider

        anchors.fill: parent
        focus: true
        focusPolicy: Qt.ClickFocus
        hoverEnabled: true
        from: 0
        to: 1
        value: root.presentationValue
        stepSize: root.step
        snapMode: T.Slider.NoSnap
        padding: 0
        live: true
        Accessible.name: root.Accessible.name
        onMoved: {
            valueTween.stop();
            root.presentationValue = value;
            root.moved(value);
        }
        onPressedChanged: if (pressed)
            valueTween.stop()
        onActiveFocusChanged: if (!activeFocus)
            root.keyboardEditing = false
        Keys.onPressed: event => {
            if ([Qt.Key_Left, Qt.Key_Right, Qt.Key_Up, Qt.Key_Down, Qt.Key_Home, Qt.Key_End, Qt.Key_PageUp, Qt.Key_PageDown].indexOf(event.key) >= 0) {
                root.keyboardEditing = true;
                valueTween.stop();
            }
            event.accepted = false;
        }
        Keys.onReleased: event => {
            if ([Qt.Key_Left, Qt.Key_Right, Qt.Key_Up, Qt.Key_Down, Qt.Key_Home, Qt.Key_End, Qt.Key_PageUp, Qt.Key_PageDown].indexOf(event.key) >= 0)
                root.keyboardEditing = false;
            event.accepted = false;
        }

        background: Rectangle {
            id: track

            x: slider.handle.width / 2
            y: (slider.height - height) / 2
            width: Math.max(0, slider.width - slider.handle.width)
            height: slider.hovered || slider.pressed || root.activeFocus ? 8 : 6
            radius: height / 2
            color: root.theme.withAlpha(root.theme.ui.minor, 0.28)

            Rectangle {
                visible: root.levels.length < 2
                width: track.width * slider.position
                height: track.height
                radius: track.radius
                color: root.theme.ui.accent
            }

            Repeater {
                // Keep bands alive across confirmed-value updates so widths ease.
                model: root.levels.length > 1 ? root.levels.length : 0

                delegate: Rectangle {
                    required property int index

                    x: 0
                    y: index * track.height / root.levels.length
                    width: track.width * Math.max(0, Math.min(1, root.levels[index]))
                    Behavior on width {
                        enabled: root.smoothChanges && !slider.pressed && !root.keyboardEditing && !root.theme.lowPowerMode
                        NumberAnimation {
                            duration: 240
                            easing.type: Easing.OutCubic
                        }
                    }
                    height: track.height / root.levels.length
                    color: root.theme.ui.accent
                    Accessible.ignored: true
                }
            }
        }

        // Fixed geometry keeps both the hit target and value mapping stable
        // throughout a drag. Press feedback does not resize the handle.
        handle: Rectangle {
            x: slider.leftPadding + slider.visualPosition * (slider.availableWidth - width)
            y: (slider.height - height) / 2
            implicitWidth: 16
            implicitHeight: 16
            radius: width / 2
            color: slider.pressed ? root.theme.ui.accent : root.theme.content.normal
            border.width: 1
            border.color: root.theme.withAlpha(root.theme.ui.backdrop, 0.7)
        }
    }
}