Phase 3: RangeWidget min/max crossing no longer panics the designer
This commit is contained in:
1 parent
294edc495e
commit
f769bdcb86
3 files changed
+39
-2
No files matched your search
@@ -100,8 +100,17 @@ impl WidgetState {
|
||||
}
|
||||
|
||||
/// Clamp a value into `[min, max]` (C++ `std::max(min, std::min(max, v))`).
|
||||
/// A degenerate range (`max <= min`) pins the value to `min`: `f64::clamp`
|
||||
/// panics when min > max, and the Inspector lets the user cross the
|
||||
/// bounds (e.g. minimum 80 with maximum 0), which aborted the designer
|
||||
/// from inside a GTK callback. Pinning to `min` matches the C++ widgets
|
||||
/// (value == min -> fraction 0.0; see `RangeWidget::frac`).
|
||||
pub fn clamp_value(&self, v: f64) -> f64 {
|
||||
v.clamp(self.min, self.max)
|
||||
if self.max <= self.min {
|
||||
self.min
|
||||
} else {
|
||||
v.clamp(self.min, self.max)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -176,7 +176,8 @@ pub trait Widget {
|
||||
let s = self.state_mut();
|
||||
s.min = min;
|
||||
s.max = max;
|
||||
s.value = s.value.clamp(min, max);
|
||||
let v = s.value;
|
||||
s.value = s.clamp_value(v);
|
||||
}
|
||||
fn step(&self) -> f64 {
|
||||
self.state().step
|
||||
|
||||
@@ -240,3 +240,30 @@ fn indicator_widget_colours_and_view() {
|
||||
w2.set_candle_down_color(Srgb::new(255, 0, 0));
|
||||
assert_ne!(draw_inner(&w2), h0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_degenerate_min_max_does_not_panic() {
|
||||
// Phase 3 regression: the Inspector lets the user cross minimum/maximum
|
||||
// (e.g. minimum 80 with maximum 0); f64::clamp panics when min > max,
|
||||
// which aborted the designer from inside a GTK callback. A degenerate
|
||||
// range pins the value to min and reports fraction 0.0 (C++ behaviour).
|
||||
let mut r = RangeWidget::new("GtkProgressBar", "ProgressBar", 0, 0, 200, 30);
|
||||
r.set_min(80.0);
|
||||
r.set_max(0.0);
|
||||
assert_eq!(r.get_position(), 80.0);
|
||||
assert_eq!(r.get_fraction(), 0.0);
|
||||
r.set_position(45.0);
|
||||
assert_eq!(r.get_position(), 80.0);
|
||||
r.set_fraction(0.5);
|
||||
assert_eq!(r.get_position(), 80.0);
|
||||
// Trait set_range with crossed bounds must not panic either.
|
||||
r.set_range(80.0, 0.0);
|
||||
assert_eq!(r.get_position(), 80.0);
|
||||
assert_eq!(r.get_fraction(), 0.0);
|
||||
// Un-crossing the bounds restores normal clamping.
|
||||
r.set_min(0.0);
|
||||
r.set_max(100.0);
|
||||
r.set_position(60.0);
|
||||
assert_eq!(r.get_position(), 60.0);
|
||||
assert_eq!(r.get_fraction(), 0.6);
|
||||
}
|
||||
Reference in new issue
Block a user