diff --git a/examples/ui.rs b/examples/ui.rs index aac6f6b..2f8aa58 100644 --- a/examples/ui.rs +++ b/examples/ui.rs @@ -1,17 +1,20 @@ use std::cell::RefCell; -use taffy::prelude::Size; use viewbuilder::ui::{Scope, View}; fn app<'cx>(cx: &'cx Scope) -> View<'cx> { cx.enter((), || { - let count = cx.use_state(|| RefCell::new(0)); + let count = cx.use_hook(|| RefCell::new(0)); View::default() - .size(Size::from_points(100., 100.)) - .on_click(|| { + .text(count.borrow().to_string()) + .view(View::default().text("Less!").on_click(|| { + dbg!(count.borrow()); + *count.borrow_mut() -= 1; + })) + .view(View::default().text("More!").on_click(|| { dbg!(count.borrow()); *count.borrow_mut() += 1; - }) + })) }) } @@ -19,5 +22,5 @@ fn main() -> Result<(), viewbuilder::Error> { let cx = Scope::default(); let view = app(&cx); - viewbuilder::run((), |cx| view.to_element().build(cx)) + viewbuilder::run((), |cx| view.to_element(cx).build(cx)) } diff --git a/src/lib.rs b/src/lib.rs index df46016..7ae9859 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,6 @@ //! ``` //! -use bumpalo::Bump; use thiserror::Error; mod context; diff --git a/src/node.rs b/src/node.rs index ecdf807..7329a4a 100644 --- a/src/node.rs +++ b/src/node.rs @@ -209,3 +209,9 @@ impl From for Node { Self::text(value) } } + +impl From> for Node { + fn from(value: Cow<'static, str>) -> Self { + Self::text(value) + } +} diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 523f488..c118709 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -1,5 +1,5 @@ mod scope; -use std::{rc::Rc, cell::RefCell, marker::PhantomData}; +use std::{cell::RefCell, marker::PhantomData, rc::Rc}; pub use scope::Scope; @@ -30,4 +30,4 @@ impl<'cx> Drop for HandlerCell<'cx> { fn drop(&mut self) { self.f.borrow_mut().take(); } -} \ No newline at end of file +} diff --git a/src/ui/scope.rs b/src/ui/scope.rs index ea4bb7d..c8be6f2 100644 --- a/src/ui/scope.rs +++ b/src/ui/scope.rs @@ -1,4 +1,7 @@ -use std::{cell::{RefCell, UnsafeCell}, any::Any}; +use std::{ + any::Any, + cell::{RefCell, UnsafeCell}, +}; use super::View; @@ -13,11 +16,11 @@ impl Scope { app(self); } - pub fn enter(&self, id: I, f: impl FnOnce() -> T) -> T { + pub fn enter(&self, _id: I, f: impl FnOnce() -> T) -> T { f() } - pub fn use_state(&self, f: impl FnOnce() -> T) -> &mut T { + pub fn use_hook(&self, f: impl FnOnce() -> T) -> &mut T { let mut states = self.states.borrow_mut(); if let Some(state) = states.get(self.pos) { diff --git a/src/ui/view.rs b/src/ui/view.rs index 687b9bf..b1812c8 100644 --- a/src/ui/view.rs +++ b/src/ui/view.rs @@ -1,12 +1,15 @@ -use taffy::{prelude::Size, style::Dimension}; -use crate::Element; +use std::borrow::Cow; + use super::HandlerCell; +use crate::{Context, Element}; +use taffy::{prelude::Size, style::Dimension}; #[derive(Default)] pub struct View<'cx> { handler: Option>, size: Option>, children: Vec, + text_nodes: Vec>, } impl<'cx> View<'cx> { @@ -25,9 +28,14 @@ impl<'cx> View<'cx> { self } - pub fn to_element(&self) -> Element { + pub fn text(mut self, text: impl Into>) -> Self { + self.text_nodes.push(text.into()); + self + } + + pub fn to_element(&self, cx: &mut Context) -> Element { let mut elem = Element::new(); - + if let Some(ref handler) = self.handler { let f = handler.handler(); elem.on_click(Box::new(move |_, _| { @@ -41,6 +49,15 @@ impl<'cx> View<'cx> { elem.size(size); } + for child in &self.children { + let mut child_elem = child.to_element(cx); + elem.child(child_elem.build(cx)); + } + + for text_node in &self.text_nodes { + elem.child(cx.insert(text_node.clone())); + } + elem } }