3.6 KiB · text History cc97953
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls as QQC2
import org.kde.plasma.components as PlasmaComponents3
import org.kde.kirigami as Kirigami

import "Lambent.js" as L

// One brightness clamp — the global one when `place` is empty — the way
// /cfg/brightness draws it: dimmer, the ladder as a slider, brighter. The
// slider sits on rungs, evenly spaced, because the server's rungs are not
// evenly spaced in 0..255. While the clamp walks toward its target, the
// label shows both ends.
RowLayout {
    id: row

    required property var store
    property string place: ""
    property string title: place !== "" ? place : i18n("Everything")
    property string subtitle: ""

    readonly property var clamp: (store.rev, place !== ""
                                  ? (store.placeClamps[place] || { curr: L.FULL, tgt: L.FULL, rate: 0, unclamped: true })
                                  : store.globalClamp)
    readonly property var ladder: (store.rev, store.ladder)
    readonly property bool onLadder: ladder.length > 1

    spacing: Kirigami.Units.smallSpacing

    ColumnLayout {
        spacing: 0
        Layout.preferredWidth: Kirigami.Units.gridUnit * 6
        Layout.maximumWidth: Kirigami.Units.gridUnit * 6

        PlasmaComponents3.Label {
            Layout.fillWidth: true
            elide: Text.ElideRight
            font.bold: row.place === ""
            text: row.title
        }

        PlasmaComponents3.Label {
            Layout.fillWidth: true
            elide: Text.ElideRight
            font: Kirigami.Theme.smallFont
            opacity: 0.7
            text: {
                var c = row.clamp
                var label = c.curr !== c.tgt
                    ? L.rungName(c.curr, row.ladder) + " → " + L.rungName(c.tgt, row.ladder)
                    : L.rungName(c.tgt, row.ladder)
                return row.subtitle ? label + " · " + row.subtitle : label
            }
        }
    }

    PlasmaComponents3.ToolButton {
        icon.name: "brightness-low"
        enabled: row.store.connected
        onClicked: row.store.clampStep(row.place, false)
        PlasmaComponents3.ToolTip { text: i18n("Dimmer") }
    }

    PlasmaComponents3.Slider {
        id: slider
        Layout.fillWidth: true
        enabled: row.store.connected
        from: 0
        to: row.onLadder ? row.ladder.length - 1 : L.FULL
        stepSize: 1
        snapMode: QQC2.Slider.SnapAlways

        // Follow the server, except under the person's finger.
        Binding on value {
            when: !slider.pressed
            value: row.onLadder ? L.ladderPosition(row.clamp.curr, row.ladder) : row.clamp.curr
            restoreMode: Binding.RestoreNone
        }

        onMoved: {
            var v = row.onLadder ? row.ladder[Math.round(value)].value : Math.round(value)
            row.store.setClamp(row.place, v)
        }

        PlasmaComponents3.ToolTip {
            visible: slider.pressed
            text: row.onLadder ? row.ladder[Math.round(slider.value)].name : L.percent(slider.value) + "%"
        }
    }

    PlasmaComponents3.ToolButton {
        icon.name: "brightness-high"
        enabled: row.store.connected
        onClicked: row.store.clampStep(row.place, true)
        PlasmaComponents3.ToolTip { text: i18n("Brighter") }
    }

    PlasmaComponents3.ToolButton {
        visible: row.place !== ""
        enabled: row.store.connected && !row.clamp.unclamped
        opacity: enabled ? 1 : 0.3
        icon.name: "edit-clear"
        onClicked: row.store.clearClamp(row.place)
        PlasmaComponents3.ToolTip { text: i18n("Clear this place's clamp") }
    }
}