Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Sep 29, 2023
1 parent d6351c7 commit 3513e58
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
15 changes: 9 additions & 6 deletions examples/ui.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
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;
})
}))
})
}

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))
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
//! ```
//!

use bumpalo::Bump;
use thiserror::Error;

mod context;
Expand Down
6 changes: 6 additions & 0 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,9 @@ impl<T> From<String> for Node<T> {
Self::text(value)
}
}

impl<T> From<Cow<'static, str>> for Node<T> {
fn from(value: Cow<'static, str>) -> Self {
Self::text(value)
}
}
4 changes: 2 additions & 2 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -30,4 +30,4 @@ impl<'cx> Drop for HandlerCell<'cx> {
fn drop(&mut self) {
self.f.borrow_mut().take();
}
}
}
9 changes: 6 additions & 3 deletions src/ui/scope.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{cell::{RefCell, UnsafeCell}, any::Any};
use std::{
any::Any,
cell::{RefCell, UnsafeCell},
};

use super::View;

Expand All @@ -13,11 +16,11 @@ impl Scope {
app(self);
}

pub fn enter<I, T>(&self, id: I, f: impl FnOnce() -> T) -> T {
pub fn enter<I, T>(&self, _id: I, f: impl FnOnce() -> T) -> T {
f()
}

pub fn use_state<T: 'static>(&self, f: impl FnOnce() -> T) -> &mut T {
pub fn use_hook<T: 'static>(&self, f: impl FnOnce() -> T) -> &mut T {
let mut states = self.states.borrow_mut();

if let Some(state) = states.get(self.pos) {
Expand Down
25 changes: 21 additions & 4 deletions src/ui/view.rs
Original file line number Diff line number Diff line change
@@ -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<HandlerCell<'cx>>,
size: Option<Size<Dimension>>,
children: Vec<Self>,
text_nodes: Vec<Cow<'static, str>>,
}

impl<'cx> View<'cx> {
Expand All @@ -25,9 +28,14 @@ impl<'cx> View<'cx> {
self
}

pub fn to_element<T>(&self) -> Element<T> {
pub fn text(mut self, text: impl Into<Cow<'static, str>>) -> Self {
self.text_nodes.push(text.into());
self
}

pub fn to_element<T>(&self, cx: &mut Context<T>) -> Element<T> {
let mut elem = Element::new();

if let Some(ref handler) = self.handler {
let f = handler.handler();
elem.on_click(Box::new(move |_, _| {
Expand All @@ -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
}
}

0 comments on commit 3513e58

Please sign in to comment.