Skip to content

Commit

Permalink
nuon.trilayout: Remove double layouting calls
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Nov 23, 2024
1 parent c626ddf commit c22782e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
7 changes: 7 additions & 0 deletions nuon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Widget<MSG> + 'a>);
Expand Down
30 changes: 12 additions & 18 deletions nuon/src/widget/trilayout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,32 @@ impl<'a, MSG> Widget<MSG> 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);
Expand Down

0 comments on commit c22782e

Please sign in to comment.