diff --git a/crates/xframe-widgets/src/archetypes.rs b/crates/xframe-widgets/src/archetypes.rs index 000cb42..06d08c7 100644 --- a/crates/xframe-widgets/src/archetypes.rs +++ b/crates/xframe-widgets/src/archetypes.rs @@ -39,6 +39,7 @@ macro_rules! archetype_impl { } fn set_bounds(&mut self, x: i32, y: i32, w: i32, h: i32) { self.state.bounds = Rect::new(x, y, w, h); + self.sync_bounds(); } fn click_count(&self) -> u32 { self.click_count @@ -801,6 +802,11 @@ impl ChartWidget { } archetype_impl!(ChartWidget, Archetype::Chart, + fn sync_bounds(&mut self) { + let b = self.state.bounds; + self.graph.state.bounds = b; + self.graph.view.bounds = b; + } fn draw(&self, target: &mut dyn DrawTarget, styles: &mut StyleManager) { if !self.state.visible { return; @@ -855,6 +861,11 @@ impl IndicatorWidget { } archetype_impl!(IndicatorWidget, Archetype::Indicator, + fn sync_bounds(&mut self) { + let b = self.state.bounds; + self.inner.state.bounds = b; + self.inner.view.bounds = b; + } fn draw(&self, target: &mut dyn DrawTarget, styles: &mut StyleManager) { if !self.state.visible { return; diff --git a/crates/xframe-widgets/src/widget.rs b/crates/xframe-widgets/src/widget.rs index 63fcb3c..627c65a 100644 --- a/crates/xframe-widgets/src/widget.rs +++ b/crates/xframe-widgets/src/widget.rs @@ -97,7 +97,17 @@ pub trait Widget { // -- shared accessors (defaulted; every C++ class has these) -- fn set_bounds(&mut self, x: i32, y: i32, w: i32, h: i32) { self.state_mut().bounds = Rect::new(x, y, w, h); + self.sync_bounds(); } + /// Hook run after `set_bounds` updates the widget's own bounds: + /// archetypes that draw through an inner widget / view (Chart + /// delegates to an `XGraph`, Indicator to an inner + /// `IndicatorWidget`, both with their own `state` + `PlotView`) + /// propagate the new bounds there. Default: nothing to sync. + /// (Without this the inner keeps its construction bounds and the + /// widget draws at (0, 0) after a move — the demo, palette and + /// layout all call `set_bounds` after `create`.) + fn sync_bounds(&mut self) {} fn bounds(&self) -> Rect { self.state().bounds } diff --git a/crates/xframe-widgets/tests/widget_tests.rs b/crates/xframe-widgets/tests/widget_tests.rs index c6ff922..454298f 100644 --- a/crates/xframe-widgets/tests/widget_tests.rs +++ b/crates/xframe-widgets/tests/widget_tests.rs @@ -190,6 +190,54 @@ fn every_widget_draws_without_panicking() { } } +/// Regression: `set_bounds` must move the widget to its rect. +/// Chart/Indicator widgets draw through an inner `XGraph` / +/// `IndicatorWidget` (with its own `state` + `PlotView`); those used +/// to keep their construction bounds (0, 0) after a move, so the +/// demo/layout showed them at the top-left corner instead of where +/// they were placed. Place every widget at (40, 40) — the demo +/// geometry — and check it paints inside its rect and nothing paints +/// in the (0, 0, 40, 40) corner it used to occupy. (Canvas starts +/// cleared to 0x202020, so "painted" is any other pixel.) +#[test] +fn set_bounds_moves_widgets_to_their_rect() { + fn painted(target: &Canvas, x: i32, y: i32, w: i32, h: i32) -> usize { + let mut n = 0; + for yy in y..y + h { + for xx in x..x + w { + if target.get_pixel(xx, yy) != 0x202020 { + n += 1; + } + } + } + n + } + for e in REGISTRY { + let mut w = create(e.name).unwrap(); + let (rw, rh) = (e.default_w.min(720), e.default_h.min(520)); + w.set_bounds(40, 40, rw, rh); + let (mut target, mut styles) = canvas(800, 600); + w.draw(&mut target, &mut styles); + let inside = painted(&target, 40, 40, rw, rh); + let at_origin = painted(&target, 0, 0, 40, 40); + if e.name == "GtkInvisible" { + assert_eq!(inside, 0, "GtkInvisible must draw nothing"); + assert_eq!(at_origin, 0, "GtkInvisible must draw nothing"); + } else { + assert!( + inside > 0, + "{} paints nothing inside its rect after set_bounds", + e.name + ); + assert_eq!( + at_origin, 0, + "{} still paints at the origin after set_bounds", + e.name + ); + } + } +} + // ---------------------------------------------------------------- icons #[test]