Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
cyypherus committed Oct 8, 2024
1 parent 22b3a83 commit 56db748
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
constraints::{Constraint, SizeConstraints},
drawable::Drawable,
models::*,
NodeWith,
Node, NodeWith,
};
use core::f32;
use std::{any::Any, rc::Rc};
Expand Down Expand Up @@ -61,7 +61,7 @@ impl<A, B> Layout<A, B> {

impl<A> Layout<A, ()> {
/// Creates a new [`Layout<A, B>`].
pub fn new(tree: impl Fn(&mut A) -> NodeWith<A, ()> + 'static) -> Self {
pub fn new(tree: impl Fn(&mut A) -> Node<A> + 'static) -> Self {
Self {
tree: Box::new(move |a, _| tree(a)),
}
Expand Down
11 changes: 10 additions & 1 deletion src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,23 @@ pub fn stack<A, B>(elements: Vec<NodeWith<A, B>>) -> NodeWith<A, B> {
/// })
///}
/// ```
pub fn draw<A>(drawable: impl Fn(Area, &mut A) + 'static) -> NodeWith<A, ()> {
pub fn draw<A>(drawable: impl Fn(Area, &mut A) + 'static) -> Node<A> {
NodeWith {
inner: NodeValue::Draw(Drawable {
area: Area::default(),
draw: Rc::new(move |area, a, _| drawable(area, a)),
}),
}
}
/// Defines a node that can be drawn
pub fn draw_with<A, B>(drawable: impl Fn(Area, &mut A, &mut B) + 'static) -> NodeWith<A, B> {
NodeWith {
inner: NodeValue::Draw(Drawable {
area: Area::default(),
draw: Rc::new(move |area, a, b| drawable(area, a, b)),
}),
}
}
/// Defines an empty space which is laid out the same as any other node.
pub fn space<A, B>() -> NodeWith<A, B> {
NodeWith {
Expand Down
31 changes: 12 additions & 19 deletions src/tests/layout_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,30 +673,23 @@ mod tests {
struct B {
c: C,
}
type TupleA<'a> = (&'a mut A, &'a mut B);
// type TupleB<'a> = (&'a mut A, &'a mut C);

fn layout<'a>(_: &mut TupleA<'_>) -> NodeWith<TupleA<'a>, ()> {
fn layout(_: &mut A, _: &mut B) -> NodeWith<A, B> {
stack(vec![
draw(|area, _: &mut TupleA| {
draw_with(|area, _, _| {
assert_eq!(area, Area::new(0., 0., 100., 100.));
}),
// TODO: This sort of partial scoping seems to be impossible to implement with the current API
// I would like to find a way to make it possible! but how??
//
// The Any type is used because rust recursive polymorphism isn't really supported
//
// UI frameworks like egui offer an &mut <UIHandle> when you create your UI elements
// & you often have some of your own app state to pass through the layout tree
//
// You can easily scope when your state is just &mut B, but you can't scope B in &mut (&mut A, &mut B)
// because the closure can't return a reference to a temporary tuple
//
//
// scope(|t: &mut TupleA| &mut (&mut *t.0, &mut *t.1), |_| space()),
scope_with(
|t: &mut A| &mut *t,
|t: &mut B| &mut t.c,
|_a, _b| {
draw_with(|area, _a: &mut A, _c: &mut C| {
assert_eq!(area, Area::new(0., 0., 100., 100.));
})
},
),
])
}
let mut tuple: TupleA = (&mut A, &mut B { c: C });
Layout::new(layout).draw(Area::new(0., 0., 100., 100.), &mut tuple);
Layout::new_with(layout).draw_with(Area::new(0., 0., 100., 100.), &mut A, &mut B { c: C });
}
}

0 comments on commit 56db748

Please sign in to comment.