Add Widget and Node

This commit is contained in:
Joscha 2024-03-07 13:58:10 +01:00
parent eb04b3fb50
commit bea5e03834
5 changed files with 102 additions and 1 deletions

View file

@ -0,0 +1,30 @@
use taffy::{NodeId, Style, TaffyResult, TaffyTree};
use crate::Widget;
pub struct Node {
layout: Style,
children: Vec<NodeId>,
widget: Option<Box<dyn Widget>>,
}
impl Node {
pub fn empty() -> Self {
Self {
layout: Style::default(),
children: vec![],
widget: None,
}
}
pub fn widget<W: Widget + 'static>(mut self, widget: W) -> Self {
self.widget = Some(Box::new(widget));
self
}
pub fn register(self, tree: &mut TaffyTree<Box<dyn Widget>>) -> TaffyResult<NodeId> {
let id = tree.new_with_children(self.layout, &self.children)?;
tree.set_node_context(id, self.widget)?;
Ok(id)
}
}