diff --git a/nuon/src/lib.rs b/nuon/src/lib.rs index 45755ff9..144eb3d9 100644 --- a/nuon/src/lib.rs +++ b/nuon/src/lib.rs @@ -98,6 +98,13 @@ impl Node { pub fn contains(&self, x: f32, y: f32) -> bool { self.as_rect().contains((x, y).into()) } + + pub fn for_each_descend_mut(&mut self, cb: &impl Fn(&mut Self)) { + cb(self); + for ch in self.children.iter_mut() { + ch.for_each_descend_mut(cb); + } + } } pub struct Element<'a, MSG>(Box + 'a>); diff --git a/nuon/src/widget/trilayout.rs b/nuon/src/widget/trilayout.rs index ecf58bf9..7fa7a26b 100644 --- a/nuon/src/widget/trilayout.rs +++ b/nuon/src/widget/trilayout.rs @@ -69,38 +69,32 @@ impl<'a, MSG> Widget for TriLayout<'a, MSG> { } if let Some(center) = self.center.as_ref() { - // TODO: This is stupid - let tmp_node = center.as_widget().layout(&LayoutCtx { - x: 0.0, - y: 0.0, + let mut node = center.as_widget().layout(&LayoutCtx { + x: ctx.x, + y: ctx.y, w: ctx.w, h: ctx.h, }); - let node = center.as_widget().layout(&LayoutCtx { - x: ctx.x + (ctx.w / 2.0 - tmp_node.w / 2.0), - y: ctx.y, - w: ctx.w, - h: ctx.h, + let x_offset = ctx.w / 2.0 - node.w / 2.0; + node.for_each_descend_mut(&|node| { + node.x += x_offset; }); children.push(node); } if let Some(end) = self.end.as_ref() { - // TODO: This is stupid - let tmp_node = end.as_widget().layout(&LayoutCtx { - x: 0.0, - y: 0.0, + let mut node = end.as_widget().layout(&LayoutCtx { + x: ctx.x, + y: ctx.y, w: ctx.w, h: ctx.h, }); - let node = end.as_widget().layout(&LayoutCtx { - x: ctx.x + ctx.w - tmp_node.w, - y: ctx.y, - w: ctx.w, - h: ctx.h, + let x_offset = ctx.w - node.w; + node.for_each_descend_mut(&|node| { + node.x += x_offset; }); children.push(node);