Slider: add largest_finite for log-sliders that include infinity

This commit is contained in:
Emil Ernerfeldt 2021-02-12 17:45:27 +01:00
parent 5906bf7a87
commit be8d7b4eef

View file

@ -33,6 +33,10 @@ struct SliderSpec {
/// For logarithmic sliders, the smallest positive value we are interested in.
/// 1 for integer sliders, maybe 1e-6 for others.
smallest_positive: f64,
/// For logarithmic sliders, the largest positive value we are interested in
/// before the slider switches to `INFINITY`, if that is the higher end.
/// Default: INFINITY.
largest_finite: f64,
}
/// Control a number by a horizontal slider.
@ -68,6 +72,7 @@ impl<'a> Slider<'a> {
spec: SliderSpec {
logarithmic: false,
smallest_positive: 1e-6,
largest_finite: f64::INFINITY,
},
clamp_to_range: false,
smart_aim: true,
@ -164,6 +169,14 @@ impl<'a> Slider<'a> {
self
}
/// For logarithmic sliders, the largest positive value we are interested in
/// before the slider switches to `INFINITY`, if that is the higher end.
/// Default: INFINITY.
pub fn largest_finite(mut self, largest_finite: f64) -> Self {
self.spec.largest_finite = largest_finite;
self
}
/// If set to `true`, all incoming and outgoing values will be clamped to the slider range.
/// Default: `false`.
pub fn clamp_to_range(mut self, clamp_to_range: bool) -> Self {
@ -545,7 +558,11 @@ fn range_log10(min: f64, max: f64, spec: &SliderSpec) -> (f64, f64) {
(max.log10() - INF_RANGE_MAGNITUDE, max.log10())
}
} else if max == INFINITY {
(min.log10(), min.log10() + INF_RANGE_MAGNITUDE)
if min < spec.largest_finite {
(min.log10(), spec.largest_finite.log10())
} else {
(min.log10(), min.log10() + INF_RANGE_MAGNITUDE)
}
} else {
(min.log10(), max.log10())
}