Skip to content

Commit fa60bc6

Browse files
committed
nuon: Replace Widget::default_* methods with free standing default_*
1 parent 7c1ae2e commit fa60bc6

File tree

6 files changed

+148
-111
lines changed

6 files changed

+148
-111
lines changed

neothesia/src/scene/playing_scene/top_bar/widget/speed_pill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl<MSG: Clone> Widget<MSG> for SpeedPill<MSG> {
117117
tree: &Tree<Self::State>,
118118
ctx: &RenderCtx,
119119
) {
120-
self.render_default(renderer, layout, tree, ctx);
120+
nuon::default_render(self, renderer, layout, tree, ctx);
121121

122122
let pad = 2.0;
123123

nuon/src/lib.rs

+45-54
Original file line numberDiff line numberDiff line change
@@ -190,37 +190,7 @@ pub trait Widget<MSG> {
190190
}
191191

192192
fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
193-
let widgets = self.children();
194-
let mut children = Vec::with_capacity(widgets.len());
195-
196-
for (ch, tree) in widgets.iter().zip(tree.children.iter_mut()) {
197-
children.push(ch.as_widget().layout(tree, parent, ctx));
198-
}
199-
200-
Node {
201-
x: parent.x,
202-
y: parent.y,
203-
w: parent.w,
204-
h: parent.h,
205-
children,
206-
}
207-
}
208-
209-
fn render_default(
210-
&self,
211-
renderer: &mut dyn Renderer,
212-
layout: &Node,
213-
tree: &Tree<Self::State>,
214-
ctx: &RenderCtx,
215-
) {
216-
for ((ch, layout), tree) in self
217-
.children()
218-
.iter()
219-
.zip(layout.children.iter())
220-
.zip(tree.children.iter())
221-
{
222-
ch.as_widget().render(renderer, layout, tree, ctx);
223-
}
193+
widget::stack::stack_layout(self, tree, parent, ctx)
224194
}
225195

226196
fn render(
@@ -230,39 +200,60 @@ pub trait Widget<MSG> {
230200
tree: &Tree<Self::State>,
231201
ctx: &RenderCtx,
232202
) {
233-
self.render_default(renderer, layout, tree, ctx)
203+
default_render(self, renderer, layout, tree, ctx)
234204
}
235205

236-
fn update_default(
206+
fn update(
237207
&mut self,
238208
event: input::Event,
239209
layout: &Node,
240210
tree: &mut Tree<Self::State>,
241211
ctx: &mut UpdateCtx<MSG>,
242212
) {
243-
for ((ch, layout), tree) in self
244-
.children_mut()
245-
.iter_mut()
246-
.zip(layout.children.iter())
247-
.zip(tree.children.iter_mut())
248-
.rev()
249-
{
250-
ch.as_widget_mut().update(event.clone(), layout, tree, ctx);
251-
252-
if ctx.is_event_captured() {
253-
return;
254-
}
255-
}
213+
default_update(self, event, layout, tree, ctx)
256214
}
215+
}
257216

258-
fn update(
259-
&mut self,
260-
event: input::Event,
261-
layout: &Node,
262-
tree: &mut Tree<Self::State>,
263-
ctx: &mut UpdateCtx<MSG>,
264-
) {
265-
self.update_default(event, layout, tree, ctx)
217+
pub use widget::column::column_layout;
218+
pub use widget::row::row_layout;
219+
pub use widget::stack::stack_layout;
220+
221+
pub fn default_render<MSG, W: Widget<MSG> + ?Sized>(
222+
this: &W,
223+
renderer: &mut dyn Renderer,
224+
layout: &Node,
225+
tree: &Tree<W::State>,
226+
ctx: &RenderCtx,
227+
) {
228+
for ((ch, layout), tree) in this
229+
.children()
230+
.iter()
231+
.zip(layout.children.iter())
232+
.zip(tree.children.iter())
233+
{
234+
ch.as_widget().render(renderer, layout, tree, ctx);
235+
}
236+
}
237+
238+
pub fn default_update<MSG, W: Widget<MSG> + ?Sized>(
239+
this: &mut W,
240+
event: input::Event,
241+
layout: &Node,
242+
tree: &mut Tree<W::State>,
243+
ctx: &mut UpdateCtx<MSG>,
244+
) {
245+
for ((ch, layout), tree) in this
246+
.children_mut()
247+
.iter_mut()
248+
.zip(layout.children.iter())
249+
.zip(tree.children.iter_mut())
250+
.rev()
251+
{
252+
ch.as_widget_mut().update(event.clone(), layout, tree, ctx);
253+
254+
if ctx.is_event_captured() {
255+
return;
256+
}
266257
}
267258
}
268259

nuon/src/widget/column.rs

+37-27
Original file line numberDiff line numberDiff line change
@@ -52,42 +52,52 @@ impl<MSG> Widget<MSG> for Column<MSG> {
5252
}
5353

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

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

64-
let mut total_height = 0.0;
68+
let mut item_layout = ParentLayout {
69+
x: parent.x,
70+
y: parent.y,
71+
w: parent.w,
72+
h: parent.h,
73+
};
6574

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

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

72-
item_layout.y += self.gap;
73-
item_layout.h -= self.gap;
80+
item_layout.y += node.h;
81+
item_layout.h -= node.h;
7482

75-
total_height += node.h;
76-
total_height += self.gap;
83+
item_layout.y += gap;
84+
item_layout.h -= gap;
7785

78-
children.push(node);
79-
}
86+
total_height += node.h;
87+
total_height += gap;
8088

81-
total_height -= self.gap;
82-
total_height = total_height.max(0.0);
89+
children.push(node);
90+
}
8391

84-
Node {
85-
x: parent.x,
86-
y: parent.y,
87-
w: parent.w,
88-
h: total_height,
89-
children,
90-
}
92+
total_height -= gap;
93+
total_height = total_height.max(0.0);
94+
95+
Node {
96+
x: parent.x,
97+
y: parent.y,
98+
w: parent.w,
99+
h: total_height,
100+
children,
91101
}
92102
}
93103

nuon/src/widget/container.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<MSG> Widget<MSG> for Container<MSG> {
110110
if let Some(bg) = self.background {
111111
renderer.quad(layout.x, layout.y, layout.w, layout.h, bg);
112112
}
113-
self.render_default(renderer, layout, tree, ctx);
113+
crate::default_render(self, renderer, layout, tree, ctx);
114114
}
115115
}
116116

nuon/src/widget/row.rs

+37-27
Original file line numberDiff line numberDiff line change
@@ -52,42 +52,52 @@ impl<MSG> Widget<MSG> for Row<MSG> {
5252
}
5353

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

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

64-
let mut total_width = 0.0;
68+
let mut item_layout = ParentLayout {
69+
x: parent.x,
70+
y: parent.y,
71+
w: parent.w,
72+
h: parent.h,
73+
};
6574

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

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

72-
item_layout.x += self.gap;
73-
item_layout.w -= self.gap;
80+
item_layout.x += node.w;
81+
item_layout.w -= node.w;
7482

75-
total_width += node.w;
76-
total_width += self.gap;
83+
item_layout.x += gap;
84+
item_layout.w -= gap;
7785

78-
children.push(node);
79-
}
86+
total_width += node.w;
87+
total_width += gap;
8088

81-
total_width -= self.gap;
82-
total_width = total_width.max(0.0);
89+
children.push(node);
90+
}
8391

84-
Node {
85-
x: parent.x,
86-
y: parent.y,
87-
w: total_width,
88-
h: parent.h,
89-
children,
90-
}
92+
total_width -= gap;
93+
total_width = total_width.max(0.0);
94+
95+
Node {
96+
x: parent.x,
97+
y: parent.y,
98+
w: total_width,
99+
h: parent.h,
100+
children,
91101
}
92102
}
93103

nuon/src/widget/stack.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use smallvec::SmallVec;
22

3-
use crate::{Element, Widget};
3+
use crate::{Element, LayoutCtx, Node, ParentLayout, Tree, Widget};
44

55
pub struct Stack<MSG> {
66
children: SmallVec<[Element<MSG>; 4]>,
@@ -43,6 +43,32 @@ impl<MSG> Widget<MSG> for Stack<MSG> {
4343
fn children_mut(&mut self) -> &mut [Element<MSG>] {
4444
&mut self.children
4545
}
46+
47+
fn layout(&self, tree: &mut Tree<Self::State>, parent: &ParentLayout, ctx: &LayoutCtx) -> Node {
48+
stack_layout(self, tree, parent, ctx)
49+
}
50+
}
51+
52+
pub fn stack_layout<MSG, W: Widget<MSG> + ?Sized>(
53+
this: &W,
54+
tree: &mut Tree<W::State>,
55+
parent: &ParentLayout,
56+
ctx: &LayoutCtx,
57+
) -> Node {
58+
let widgets = this.children();
59+
let mut children = Vec::with_capacity(widgets.len());
60+
61+
for (ch, tree) in widgets.iter().zip(tree.children.iter_mut()) {
62+
children.push(ch.as_widget().layout(tree, parent, ctx));
63+
}
64+
65+
Node {
66+
x: parent.x,
67+
y: parent.y,
68+
w: parent.w,
69+
h: parent.h,
70+
children,
71+
}
4672
}
4773

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

0 commit comments

Comments
 (0)