From 785e65264bb88f7e7dec2b62c95d17da5560d2ae Mon Sep 17 00:00:00 2001 From: Matt Hunzinger Date: Sun, 24 Sep 2023 16:55:41 -0400 Subject: [PATCH] Change run function to reduce boilerplate --- README.md | 11 ++++++----- examples/counter.rs | 21 +++++++++++---------- examples/hello.rs | 15 ++++++++------- src/lib.rs | 9 ++++++--- src/node.rs | 11 ++++++----- 5 files changed, 37 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 0ad0b72..f776e65 100644 --- a/README.md +++ b/README.md @@ -38,15 +38,16 @@ but you can bring your own state management tools or build your own framework us ### Hello World ```rust -fn main() { - let mut cx = Context::default(); - let root = Element::new() +fn app(cx: &mut Context) -> NodeKey { + Element::new() .align_items(AlignItems::Center) .justify_content(JustifyContent::Center) .child(cx.insert("Hello World!")) - .build(&mut cx); + .build(cx) +} - viewbuilder::run(tree, root) +fn main() { + viewbuilder::run(app) } ``` diff --git a/examples/counter.rs b/examples/counter.rs index 6e3c492..31d3c61 100644 --- a/examples/counter.rs +++ b/examples/counter.rs @@ -17,32 +17,33 @@ fn button( .build(cx) } -fn main() { - let mut cx = Context::default(); - +fn app(cx: &mut Context) -> NodeKey { let inc_count = Rc::new(AtomicI64::new(0)); let dec_count = inc_count.clone(); let text = cx.insert("0"); - let root = Element::new() + + Element::new() .flex_direction(FlexDirection::Column) - .child(Element::new().child(text).build(&mut cx)) + .child(Element::new().child(text).build(cx)) .child( Element::new() .flex_direction(FlexDirection::Row) - .child(button(&mut cx, "More!", move |cx| { + .child(button(cx, "More!", move |cx| { inc_count.fetch_add(1, Ordering::SeqCst); cx.node(text) .set_text(inc_count.load(Ordering::SeqCst).to_string()) })) - .child(button(&mut cx, "Less!", move |cx| { + .child(button(cx, "Less!", move |cx| { dec_count.fetch_sub(1, Ordering::SeqCst); cx.node(text) .set_text(dec_count.load(Ordering::SeqCst).to_string()) })) - .build(&mut cx), + .build(cx), ) - .build(&mut cx); + .build(cx) +} - viewbuilder::run(cx, root) +fn main() { + viewbuilder::run(app) } diff --git a/examples/hello.rs b/examples/hello.rs index 99738b0..dcf5d25 100644 --- a/examples/hello.rs +++ b/examples/hello.rs @@ -1,13 +1,14 @@ use taffy::style::{AlignItems, JustifyContent}; -use viewbuilder::{Context, Element}; +use viewbuilder::{Context, Element, NodeKey}; -fn main() { - let mut tree = Context::default(); - let root = Element::new() +fn app(cx: &mut Context) -> NodeKey { + Element::new() .align_items(AlignItems::Center) .justify_content(JustifyContent::Center) - .child(tree.insert("Hello World!")) - .build(&mut tree); + .child(cx.insert("Hello World!")) + .build(cx) +} - viewbuilder::run(tree, root) +fn main() { + viewbuilder::run(app) } diff --git a/src/lib.rs b/src/lib.rs index 718a109..f1e4e6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,6 @@ //! It supports layout, drawing, and accessability. pub mod node; - pub use node::Node; pub mod tree; @@ -32,7 +31,11 @@ slotmap::new_key_type! { /// /// This will create a new window and render the tree, /// propagating events and re-rendering as they occuring. -pub fn run(tree: Context, root: NodeKey) { +pub fn run(f: impl FnOnce(&mut Context) -> NodeKey) { let renderer = Renderer::new(); - renderer.run(tree, root) + + let mut cx = Context::default(); + let root = f(&mut cx); + + renderer.run(cx, root) } diff --git a/src/node.rs b/src/node.rs index f2f9464..d1c92d9 100644 --- a/src/node.rs +++ b/src/node.rs @@ -28,21 +28,22 @@ pub enum NodeData { Text(Cow<'static, str>), } +/// Node of a tree. pub struct Node { /// Data type of the node. - pub data: NodeData, + pub(crate) data: NodeData, /// Parent node id. - pub parent: Option, + pub(crate) parent: Option, /// Child node ids. - pub children: Option>, + pub(crate) children: Option>, /// Layout key for the taffy node. - pub layout_key: Option, + pub(crate) layout_key: Option, /// Absolute layout of the node, relative to the window. - pub layout: Option, + pub(crate) layout: Option, } impl Node {