Fix set_bounds for Chart/Indicator widgets (drew at 0,0 after a move)

ChartWidget and IndicatorWidget draw through an inner XGraph /
IndicatorWidget with its own state + PlotView, created at the
construction bounds. The Widget::set_bounds default only updated the
outer state, so after demo/layout called set_bounds(40,40,...) all
250 Indicator + 36 Chart widgets still drew at the top-left corner.
The runner masked it (whole-window draw metric + clicks landing
inside both rects); the pixel-diff crop exposed it (IndUpDownRatio
and IndUpDownVolumeRatio looked blank only because their 150x36
crop did not overlap the misplaced drawing at all).

Fix: Widget::set_bounds now calls a sync_bounds() hook (default
no-op); Chart/Indicator override it to propagate bounds to the
inner state + view. XGraph already synced its view; XSpreadsheet
draws from state only. Regression test
set_bounds_moves_widgets_to_their_rect places every widget at
(40,40) on a Canvas and asserts it paints inside its rect and
nothing at the origin (verified: fails without the fix).
This commit is contained in:
Muse committed 2026-11-29 07:47:36 +00:00
1 parent aa019fa3bc
commit bf50641662
3 files changed
+69

No files matched your search

+11
View File
@@ -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;
+10
View File
@@ -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
}
@@ -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]