Skip to content

Commit

Permalink
nuon: Make widgets fully inmutable
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Dec 7, 2024
1 parent fa60bc6 commit 21abef4
Show file tree
Hide file tree
Showing 10 changed files with 10 additions and 49 deletions.
2 changes: 1 addition & 1 deletion neothesia/src/scene/playing_scene/top_bar/widget/looper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<MSG> Widget<MSG> for Looper<MSG> {
}

fn update(
&mut self,
&self,
event: Event,
layout: &Node,
tree: &mut Tree<Self::State>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl<MSG: Clone> Widget<MSG> for ProgressBar<MSG> {
}

fn update(
&mut self,
&self,
event: Event,
layout: &Node,
tree: &mut Tree<Self::State>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ impl<MSG: Clone> Widget<MSG> for SpeedPill<MSG> {
&self.children
}

fn children_mut(&mut self) -> &mut [Element<MSG>] {
&mut self.children
}

fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
let minus = self.children[0].as_widget().layout(
tree.children[0].remap_mut(),
Expand Down
29 changes: 7 additions & 22 deletions nuon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,13 @@ pub trait WidgetAny<MSG> {
fn state(&self) -> Box<dyn Any>;

fn children(&self) -> &[Element<MSG>];
fn children_mut(&mut self) -> &mut [Element<MSG>];
fn children_tree(&self) -> Vec<Tree>;

fn diff(&self, tree: &mut Tree);

fn layout(&self, tree: &mut Tree, avalilable: &ParentLayout, ctx: &LayoutCtx) -> Node;
fn render(&self, renderer: &mut dyn Renderer, layout: &Node, tree: &Tree, ctx: &RenderCtx);
fn update(
&mut self,
event: input::Event,
layout: &Node,
tree: &mut Tree,
ctx: &mut UpdateCtx<MSG>,
);
fn update(&self, event: input::Event, layout: &Node, tree: &mut Tree, ctx: &mut UpdateCtx<MSG>);
}

impl<MSG, W: Widget<MSG>> WidgetAny<MSG> for W {
Expand All @@ -132,10 +125,6 @@ impl<MSG, W: Widget<MSG>> WidgetAny<MSG> for W {
Widget::children(self)
}

fn children_mut(&mut self) -> &mut [Element<MSG>] {
Widget::children_mut(self)
}

fn children_tree(&self) -> Vec<Tree> {
Widget::children_tree(self)
}
Expand All @@ -153,7 +142,7 @@ impl<MSG, W: Widget<MSG>> WidgetAny<MSG> for W {
}

fn update(
&mut self,
&self,
event: input::Event,
layout: &Node,
tree: &mut Tree,
Expand All @@ -174,10 +163,6 @@ pub trait Widget<MSG> {
&[]
}

fn children_mut(&mut self) -> &mut [Element<MSG>] {
&mut []
}

fn children_tree(&self) -> Vec<Tree> {
self.children()
.iter()
Expand All @@ -204,7 +189,7 @@ pub trait Widget<MSG> {
}

fn update(
&mut self,
&self,
event: input::Event,
layout: &Node,
tree: &mut Tree<Self::State>,
Expand Down Expand Up @@ -236,20 +221,20 @@ pub fn default_render<MSG, W: Widget<MSG> + ?Sized>(
}

pub fn default_update<MSG, W: Widget<MSG> + ?Sized>(
this: &mut W,
this: &W,
event: input::Event,
layout: &Node,
tree: &mut Tree<W::State>,
ctx: &mut UpdateCtx<MSG>,
) {
for ((ch, layout), tree) in this
.children_mut()
.iter_mut()
.children()
.iter()
.zip(layout.children.iter())
.zip(tree.children.iter_mut())
.rev()
{
ch.as_widget_mut().update(event.clone(), layout, tree, ctx);
ch.as_widget().update(event.clone(), layout, tree, ctx);

if ctx.is_event_captured() {
return;
Expand Down
2 changes: 1 addition & 1 deletion nuon/src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<MSG: Clone> Widget<MSG> for Button<MSG> {
}

fn update(
&mut self,
&self,
event: Event,
layout: &Node,
tree: &mut Tree<Self::State>,
Expand Down
4 changes: 0 additions & 4 deletions nuon/src/widget/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ impl<MSG> Widget<MSG> for Column<MSG> {
&self.children
}

fn children_mut(&mut self) -> &mut [Element<MSG>] {
&mut self.children
}

fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
column_layout(self, tree, parent, ctx, self.gap)
}
Expand Down
4 changes: 0 additions & 4 deletions nuon/src/widget/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ impl<MSG> Widget<MSG> for Container<MSG> {
&self.child
}

fn children_mut(&mut self) -> &mut [Element<MSG>] {
&mut self.child
}

fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
let parent = &ParentLayout {
x: parent.x + self.x,
Expand Down
4 changes: 0 additions & 4 deletions nuon/src/widget/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ impl<MSG> Widget<MSG> for Row<MSG> {
&self.children
}

fn children_mut(&mut self) -> &mut [Element<MSG>] {
&mut self.children
}

fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
row_layout(self, tree, parent, ctx, self.gap)
}
Expand Down
4 changes: 0 additions & 4 deletions nuon/src/widget/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ impl<MSG> Widget<MSG> for Stack<MSG> {
&self.children
}

fn children_mut(&mut self) -> &mut [Element<MSG>] {
&mut self.children
}

fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
stack_layout(self, tree, parent, ctx)
}
Expand Down
4 changes: 0 additions & 4 deletions nuon/src/widget/trilayout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ impl<MSG> Widget<MSG> for TriLayout<MSG> {
&self.children
}

fn children_mut(&mut self) -> &mut [Element<MSG>] {
&mut self.children
}

fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
let start = self.children[0].as_widget().layout(
&mut tree.children[0],
Expand Down

0 comments on commit 21abef4

Please sign in to comment.