Possible bug related with the Slider default Range and Value

Hi everyone,

I have the following code to setup a Slider, pay attention to the default value 33.50 and the range going from 0.0 to 100.0:

extend(ControlManager()) {
            layout {
                slider {
                    label = "Slide me"
                    value = 33.50
                    range = Range(0.0, 100.0)
                    precision = 2
                    events.valueChanged.listen {
                        println("the new value is ${it.newValue}")

The slider works correctly once you actually move it for the first time, but before interacting with it, as you can see in the image, the value according the text is 10.0 and the slider location corresponds with that incorrect value as well.

It seems that unless you move the slider then the value gets clamped to the range inside the Slider class (0.0 to 10.0) and not to the range specified in the code shown before. You have to move it first and then it works correctly.

Here is the Slider class snippet and I think it’s probably due to the cleaning of the variable:

   var range = Range(0.0, 10.0)
        set(value) {
            field = value
            this.value = this.value
    fun clean(value: Double): Double {
        val cleanV = value.coerceIn(range.min, range.max)
        val quantized = String.format("%.0${precision}f", cleanV).replace(",", ".").toDouble()
        return quantized

Please let me know if you understand the issue as I described it, and I hope I got it right. If not, my apologies for any confusion. I’m not really an expert in this, so I’m not sure how to reference the values I received during initialization. That’s why I haven’t tried making a pull request yet. But hey, if everything seems good and someone could walk me through the process, I’d be happy to give it a shot and make my first contribution to the project.

Best regards.

1 Like

Hi! Fortunately the explanation in this case is simple:

The default range of a slider is 0.0 to 10.0. Before changing the range, you attempt to set the value to 33.50. The system clamps it to 10.0. Then you change the range and the value stays at 10.0.

The solution is to set the value after setting the range :slight_smile:

Hi Abe, thank you for your observation.

I feel it was a dumb mistake haha.
I didn’t thought the order of the parameters matter.
However, I’m a bit confused since in this documentation they set the value to 0.50 and then they change the default range. Why is it working? Is it because those example values are within the default anyway and not over like in 1.0 to 100.0?

See here: User Interfaces | OPENRNDR GUIDE

Hi Gonzalo,

What happens in your latest example is:

  1. The value is set to 0.5. This value is inside the default range (0.0, 10.0) so it is not clamped.
  2. The range is changed to (0.0, 1.0). The value is inside that range and stays as 0.5.

In the original example:

  1. The value is set to 33.5. This value is outside the default range (0.0, 10.0) so it is clamped to 10.0.
  2. The range is changed to (0.0, 100.0). The value is inside that range, so it stays as 10.0.

I hope it’s a bit clearer. And yes, I can see how the order impacting the result can be surprising :slight_smile:

All right, I see. Perhaps expanding the explanation of the Range class in the documentation could be helpful.

1 Like

I updated the guide. I moved value under range and added a comment to avoid this issue.

Thank you for your feedback :slight_smile: