Skip to content

Commit

Permalink
nuon: Replace Widget::default_* methods with free standing default_*
Browse files Browse the repository at this point in the history
  • Loading branch information
PolyMeilex committed Dec 7, 2024
1 parent 7c1ae2e commit fa60bc6
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl<MSG: Clone> Widget<MSG> for SpeedPill<MSG> {
tree: &Tree<Self::State>,
ctx: &RenderCtx,
) {
self.render_default(renderer, layout, tree, ctx);
nuon::default_render(self, renderer, layout, tree, ctx);

let pad = 2.0;

Expand Down
99 changes: 45 additions & 54 deletions nuon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,37 +190,7 @@ pub trait Widget<MSG> {
}

fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
let widgets = self.children();
let mut children = Vec::with_capacity(widgets.len());

for (ch, tree) in widgets.iter().zip(tree.children.iter_mut()) {
children.push(ch.as_widget().layout(tree, parent, ctx));
}

Node {
x: parent.x,
y: parent.y,
w: parent.w,
h: parent.h,
children,
}
}

fn render_default(
&self,
renderer: &mut dyn Renderer,
layout: &Node,
tree: &Tree<Self::State>,
ctx: &RenderCtx,
) {
for ((ch, layout), tree) in self
.children()
.iter()
.zip(layout.children.iter())
.zip(tree.children.iter())
{
ch.as_widget().render(renderer, layout, tree, ctx);
}
widget::stack::stack_layout(self, tree, parent, ctx)
}

fn render(
Expand All @@ -230,39 +200,60 @@ pub trait Widget<MSG> {
tree: &Tree<Self::State>,
ctx: &RenderCtx,
) {
self.render_default(renderer, layout, tree, ctx)
default_render(self, renderer, layout, tree, ctx)
}

fn update_default(
fn update(
&mut self,
event: input::Event,
layout: &Node,
tree: &mut Tree<Self::State>,
ctx: &mut UpdateCtx<MSG>,
) {
for ((ch, layout), tree) in self
.children_mut()
.iter_mut()
.zip(layout.children.iter())
.zip(tree.children.iter_mut())
.rev()
{
ch.as_widget_mut().update(event.clone(), layout, tree, ctx);

if ctx.is_event_captured() {
return;
}
}
default_update(self, event, layout, tree, ctx)
}
}

fn update(
&mut self,
event: input::Event,
layout: &Node,
tree: &mut Tree<Self::State>,
ctx: &mut UpdateCtx<MSG>,
) {
self.update_default(event, layout, tree, ctx)
pub use widget::column::column_layout;
pub use widget::row::row_layout;
pub use widget::stack::stack_layout;

pub fn default_render<MSG, W: Widget<MSG> + ?Sized>(
this: &W,
renderer: &mut dyn Renderer,
layout: &Node,
tree: &Tree<W::State>,
ctx: &RenderCtx,
) {
for ((ch, layout), tree) in this
.children()
.iter()
.zip(layout.children.iter())
.zip(tree.children.iter())
{
ch.as_widget().render(renderer, layout, tree, ctx);
}
}

pub fn default_update<MSG, W: Widget<MSG> + ?Sized>(
this: &mut 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()
.zip(layout.children.iter())
.zip(tree.children.iter_mut())
.rev()
{
ch.as_widget_mut().update(event.clone(), layout, tree, ctx);

if ctx.is_event_captured() {
return;
}
}
}

Expand Down
64 changes: 37 additions & 27 deletions nuon/src/widget/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,52 @@ impl<MSG> Widget<MSG> for Column<MSG> {
}

fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
let mut children = Vec::with_capacity(self.children.len());
column_layout(self, tree, parent, ctx, self.gap)
}
}

let mut item_layout = ParentLayout {
x: parent.x,
y: parent.y,
w: parent.w,
h: parent.h,
};
pub fn column_layout<MSG, W: Widget<MSG> + ?Sized>(
this: &W,
tree: &mut Tree<W::State>,
parent: &ParentLayout,
ctx: &LayoutCtx,
gap: f32,
) -> Node {
let mut children = Vec::with_capacity(this.children().len());

let mut total_height = 0.0;
let mut item_layout = ParentLayout {
x: parent.x,
y: parent.y,
w: parent.w,
h: parent.h,
};

for (ch, tree) in self.children.iter().zip(tree.children.iter_mut()) {
let node = ch.as_widget().layout(tree, &item_layout, ctx);
let mut total_height = 0.0;

item_layout.y += node.h;
item_layout.h -= node.h;
for (ch, tree) in this.children().iter().zip(tree.children.iter_mut()) {
let node = ch.as_widget().layout(tree, &item_layout, ctx);

item_layout.y += self.gap;
item_layout.h -= self.gap;
item_layout.y += node.h;
item_layout.h -= node.h;

total_height += node.h;
total_height += self.gap;
item_layout.y += gap;
item_layout.h -= gap;

children.push(node);
}
total_height += node.h;
total_height += gap;

total_height -= self.gap;
total_height = total_height.max(0.0);
children.push(node);
}

Node {
x: parent.x,
y: parent.y,
w: parent.w,
h: total_height,
children,
}
total_height -= gap;
total_height = total_height.max(0.0);

Node {
x: parent.x,
y: parent.y,
w: parent.w,
h: total_height,
children,
}
}

Expand Down
2 changes: 1 addition & 1 deletion nuon/src/widget/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<MSG> Widget<MSG> for Container<MSG> {
if let Some(bg) = self.background {
renderer.quad(layout.x, layout.y, layout.w, layout.h, bg);
}
self.render_default(renderer, layout, tree, ctx);
crate::default_render(self, renderer, layout, tree, ctx);
}
}

Expand Down
64 changes: 37 additions & 27 deletions nuon/src/widget/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,42 +52,52 @@ impl<MSG> Widget<MSG> for Row<MSG> {
}

fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
let mut children = Vec::with_capacity(self.children.len());
row_layout(self, tree, parent, ctx, self.gap)
}
}

let mut item_layout = ParentLayout {
x: parent.x,
y: parent.y,
w: parent.w,
h: parent.h,
};
pub fn row_layout<MSG, W: Widget<MSG> + ?Sized>(
this: &W,
tree: &mut Tree<W::State>,
parent: &ParentLayout,
ctx: &LayoutCtx,
gap: f32,
) -> Node {
let mut children = Vec::with_capacity(this.children().len());

let mut total_width = 0.0;
let mut item_layout = ParentLayout {
x: parent.x,
y: parent.y,
w: parent.w,
h: parent.h,
};

for (ch, tree) in self.children.iter().zip(tree.children.iter_mut()) {
let node = ch.as_widget().layout(tree, &item_layout, ctx);
let mut total_width = 0.0;

item_layout.x += node.w;
item_layout.w -= node.w;
for (ch, tree) in this.children().iter().zip(tree.children.iter_mut()) {
let node = ch.as_widget().layout(tree, &item_layout, ctx);

item_layout.x += self.gap;
item_layout.w -= self.gap;
item_layout.x += node.w;
item_layout.w -= node.w;

total_width += node.w;
total_width += self.gap;
item_layout.x += gap;
item_layout.w -= gap;

children.push(node);
}
total_width += node.w;
total_width += gap;

total_width -= self.gap;
total_width = total_width.max(0.0);
children.push(node);
}

Node {
x: parent.x,
y: parent.y,
w: total_width,
h: parent.h,
children,
}
total_width -= gap;
total_width = total_width.max(0.0);

Node {
x: parent.x,
y: parent.y,
w: total_width,
h: parent.h,
children,
}
}

Expand Down
28 changes: 27 additions & 1 deletion nuon/src/widget/stack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use smallvec::SmallVec;

use crate::{Element, Widget};
use crate::{Element, LayoutCtx, Node, ParentLayout, Tree, Widget};

pub struct Stack<MSG> {
children: SmallVec<[Element<MSG>; 4]>,
Expand Down Expand Up @@ -43,6 +43,32 @@ impl<MSG> Widget<MSG> for Stack<MSG> {
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)
}
}

pub fn stack_layout<MSG, W: Widget<MSG> + ?Sized>(
this: &W,
tree: &mut Tree<W::State>,
parent: &ParentLayout,
ctx: &LayoutCtx,
) -> Node {
let widgets = this.children();
let mut children = Vec::with_capacity(widgets.len());

for (ch, tree) in widgets.iter().zip(tree.children.iter_mut()) {
children.push(ch.as_widget().layout(tree, parent, ctx));
}

Node {
x: parent.x,
y: parent.y,
w: parent.w,
h: parent.h,
children,
}
}

impl<MSG: 'static> From<Stack<MSG>> for Element<MSG> {
Expand Down

0 comments on commit fa60bc6

Please sign in to comment.