diff --git a/crates/xframe-widgets/src/state.rs b/crates/xframe-widgets/src/state.rs index a577759..b7e20e7 100644 --- a/crates/xframe-widgets/src/state.rs +++ b/crates/xframe-widgets/src/state.rs @@ -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) + } } } diff --git a/crates/xframe-widgets/src/widget.rs b/crates/xframe-widgets/src/widget.rs index d6cd9aa..7b77364 100644 --- a/crates/xframe-widgets/src/widget.rs +++ b/crates/xframe-widgets/src/widget.rs @@ -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 diff --git a/crates/xframe-widgets/tests/getter_setter_tests.rs b/crates/xframe-widgets/tests/getter_setter_tests.rs index 8fb8013..e3b15d4 100644 --- a/crates/xframe-widgets/tests/getter_setter_tests.rs +++ b/crates/xframe-widgets/tests/getter_setter_tests.rs @@ -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); +}