Skip to content

Instantly share code, notes, and snippets.

@Frando
Last active April 23, 2021 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Frando/8dc33d735bf943dd09962983cd044a03 to your computer and use it in GitHub Desktop.
Save Frando/8dc33d735bf943dd09962983cd044a03 to your computer and use it in GitHub Desktop.
tuix tree builder
use tuix::*;
fn main() {
let props = WindowDescription::new().with_title("Sample app");
let app = Application::new(props, |state, root| {
App::default().build(state, root, |b| b);
});
app.run();
}
#[derive(Debug, Clone, Copy, PartialEq)]
enum AppEvent {
Trigger,
}
#[derive(Default, Debug)]
struct App {
container: Entity,
label: Entity,
color: Color,
i: u32,
}
impl Widget for App {
type Ret = Entity;
fn on_build(&mut self, state: &mut State, entity: Entity) -> Self::Ret {
let mut tree = TreeBuilder::new(state, entity);
tree.push(Element::new())
.build(|el| {
el.set_text("some el")
.set_width(Units::Pixels(100.0))
.set_height(Units::Pixels(30.0))
.set_background_color(Color::rgb(200, 80, 20))
})
.push(Element::new())
.build(|el| el.set_background_color(self.color))
.assign(&mut self.container)
.push(Element::new())
.build(|el| {
el.set_background_color(Color::rgb(0, 0, 0))
.set_child_space(Units::Pixels(20.0))
.set_child_between(Units::Pixels(20.0))
})
.children(|tree| {
tree.push(Button::new().on_press(Event::new(AppEvent::Trigger)))
.build(|el| {
el.set_text("Change color")
.set_background_color(Color::white())
.set_color(Color::black())
})
.push(Button::new())
.build(|el| el.set_text("another"))
.push(Element::new())
.build(|el| {
el.set_background_color(Color::rgb(0, 0, 150))
.set_text(&format!("Number of clicks: {}", self.i))
.set_color(Color::white())
})
.assign(&mut self.label)
});
entity
}
fn on_event(&mut self, state: &mut State, entity: Entity, event: &mut Event) {
if let Some(event) = event.message.downcast::<AppEvent>() {
match event {
AppEvent::Trigger => {
self.i += 1;
let color = match self.i % 3 {
0 => Color::rgb(0, 80, 0),
1 => Color::rgb(80, 0, 0),
2 => Color::rgb(0, 0, 80),
_ => unreachable!(),
};
self.container.set_background_color(state, color);
self.label
.set_text(state, &format!("Number of clicks: {}", self.i));
}
}
}
}
}
struct TreeBuilder<'s> {
state: &'s mut State,
pub root: Entity,
pub current: Entity,
pub last: Entity,
pub parent: Entity,
}
impl<'s> TreeBuilder<'s> {
fn new(state: &'s mut State, root: Entity) -> Self {
Self {
state,
root,
current: root,
last: root,
parent: Entity::default(),
}
}
fn push<W>(&mut self, element: W) -> &mut Self
where
W: Widget<Ret = Entity>,
{
let last = element.build(self.state, self.current, |builder| builder);
self.last = last;
self
}
fn build<F>(&mut self, mut builder: F) -> &mut Self
where
F: FnMut(Builder) -> Builder,
{
builder(Builder::new(self.state, self.last));
self
}
fn assign(&mut self, target: &mut Entity) -> &mut Self {
*target = self.last;
self
}
fn children<F>(&mut self, mut children_fn: F) -> &mut Self
where
F: FnMut(&mut Self) -> &mut Self,
{
self.parent = self.current;
self.current = self.last;
children_fn(self);
self.current = self.parent;
self
}
}
diff --git a/core/src/events/builder.rs b/core/src/events/builder.rs
index ae7df9e..b9a4b67 100644
--- a/core/src/events/builder.rs
+++ b/core/src/events/builder.rs
@@ -12,7 +12,7 @@ pub struct Builder<'a> {
impl<'a> Builder<'a> {
/// Creates a new Builder
- pub(crate) fn new(state: &'a mut State, entity: Entity) -> Self {
+ pub fn new(state: &'a mut State, entity: Entity) -> Self {
Builder { entity, state }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment