stackotter / swift-cross-ui

A cross-platform declarative UI framework, inspired by SwiftUI.
https://stackotter.github.io/swift-cross-ui/documentation/swiftcrossui/
MIT License
651 stars 36 forks source link

Create API for limiting sliders to ranges within their set bounds #85

Open stackotter opened 7 months ago

stackotter commented 7 months ago

RandomNumberGeneratorExample has a minimum slider and a maximum slider which are both from 0 to 100. In addition to their hard bounds (which don't change), there's an extra constraint that the minimum slider can't have a value greater than the maximum slider, and another that thte maximum slider can't have a value lower than the minimum slider.

Here's how this is achieved (for the minimum slider) with the current API,

Slider(
    state.$minNum.onChange { newValue in
        if newValue > state.maxNum {
            state.minNum = state.maxNum
        }
    },
    minimum: 0,
    maximum: 100
)

And here's how I could imagine it working with a declarative-style API,

Slider(state.$minNum, minimum: 0, maximum: 100)
    .limited(to: state.minNum...)

I'd love to hear alternative solutions if anyone has ideas!

This API would have overloads for ClosedRange, PartialRangeFrom, and PartialRangeThrough.