Skip to content

Commit

Permalink
Create button fn in example
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Sep 24, 2023
1 parent 2afb619 commit c9a7af2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ let root = Element::builder()

viewbuilder::run(tree, root)
```

```rust
fn button(tree: &mut Tree, mut f: impl FnMut(&mut Tree) + 'static) -> DefaultKey {
Element::builder()
.on_click(Box::new(move |tree, _event| f(tree)))
.background_color(Color4f::new(1., 1., 0., 1.))
.child(tree.insert("More!"))
.build(tree)
}
```
37 changes: 17 additions & 20 deletions examples/counter.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
use skia_safe::Color4f;
use slotmap::DefaultKey;
use std::rc::Rc;
use std::sync::atomic::{AtomicI64, Ordering};
use taffy::style::FlexDirection;
use viewbuilder::{node::Element, Tree};

fn button(tree: &mut Tree, mut f: impl FnMut(&mut Tree) + 'static) -> DefaultKey {
Element::builder()
.on_click(Box::new(move |tree, _event| f(tree)))
.background_color(Color4f::new(1., 1., 0., 1.))
.child(tree.insert("More!"))
.build(tree)
}

fn main() {
let mut tree = Tree::default();

Expand All @@ -17,26 +26,14 @@ fn main() {
.child(
Element::builder()
.flex_direction(FlexDirection::Row)
.child(
Element::builder()
.on_click(Box::new(move |tree, _event| {
inc_count.fetch_add(1, Ordering::SeqCst);
tree.set_text(text, inc_count.load(Ordering::SeqCst).to_string())
}))
.background_color(Color4f::new(1., 1., 0., 1.))
.child(tree.insert("More!"))
.build(&mut tree),
)
.child(
Element::builder()
.on_click(Box::new(move |tree, _event| {
dec_count.fetch_sub(1, Ordering::SeqCst);
tree.set_text(text, dec_count.load(Ordering::SeqCst).to_string())
}))
.background_color(Color4f::new(1., 1., 0., 1.))
.child(tree.insert("Less!"))
.build(&mut tree),
)
.child(button(&mut tree, move |tree| {
inc_count.fetch_add(1, Ordering::SeqCst);
tree.set_text(text, inc_count.load(Ordering::SeqCst).to_string())
}))
.child(button(&mut tree, move |tree| {
dec_count.fetch_sub(1, Ordering::SeqCst);
tree.set_text(text, dec_count.load(Ordering::SeqCst).to_string())
}))
.build(&mut tree),
)
.build(&mut tree);
Expand Down

0 comments on commit c9a7af2

Please sign in to comment.