Skip to content

Commit

Permalink
Change run function to reduce boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Sep 24, 2023
1 parent 0f0f8a4 commit 785e652
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 30 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```

Expand Down
21 changes: 11 additions & 10 deletions examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
15 changes: 8 additions & 7 deletions examples/hello.rs
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 6 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//! It supports layout, drawing, and accessability.

pub mod node;

pub use node::Node;

pub mod tree;
Expand All @@ -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)
}
11 changes: 6 additions & 5 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<NodeKey>,
pub(crate) parent: Option<NodeKey>,

/// Child node ids.
pub children: Option<Vec<NodeKey>>,
pub(crate) children: Option<Vec<NodeKey>>,

/// Layout key for the taffy node.
pub layout_key: Option<DefaultKey>,
pub(crate) layout_key: Option<DefaultKey>,

/// Absolute layout of the node, relative to the window.
pub layout: Option<Layout>,
pub(crate) layout: Option<Layout>,
}

impl Node {
Expand Down

0 comments on commit 785e652

Please sign in to comment.