From a670a0540cd1b64943c003ced8367916251004c1 Mon Sep 17 00:00:00 2001 From: Matt Hunzinger Date: Sun, 24 Sep 2023 15:25:25 -0400 Subject: [PATCH] Create NodeKey struct --- README.md | 2 +- examples/animation.rs | 4 ++-- examples/counter.rs | 4 ++-- src/element.rs | 9 ++++----- src/event.rs | 8 ++++---- src/lib.rs | 9 +++++---- src/node.rs | 6 +++--- src/render.rs | 6 +++--- src/tree/iter.rs | 9 ++++----- src/tree/iter_mut.rs | 7 +++---- src/tree/mod.rs | 29 +++++++++++++++-------------- src/tree/node_ref.rs | 5 ++--- 12 files changed, 48 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 02dfdf6..3aa6a2a 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ fn button( tree: &mut Tree, label: &'static str, mut handler: impl FnMut(&mut Tree) + 'static, -) -> ElementKey { +) -> NodeKey { Element::new() .on_click(Box::new(move |tree, _event| handler(tree))) .background_color(Color4f::new(1., 1., 0., 1.)) diff --git a/examples/animation.rs b/examples/animation.rs index 4a790c1..6227a50 100644 --- a/examples/animation.rs +++ b/examples/animation.rs @@ -28,7 +28,7 @@ async fn main() { tokio::spawn(async move { let mut start = Instant::now(); loop { - let min = 0.; + let _min = 0.; let max = 500.; let elapsed = Instant::now() - start; @@ -43,7 +43,7 @@ async fn main() { } tx.send(UserEvent(Box::new(move |tree| { - tree.element(root) + tree.node(root) .set_size(Size::from_points(size as f32, size as f32)) }))) .unwrap(); diff --git a/examples/counter.rs b/examples/counter.rs index d7e0416..7f7b5a5 100644 --- a/examples/counter.rs +++ b/examples/counter.rs @@ -2,14 +2,14 @@ use skia_safe::Color4f; use std::rc::Rc; use std::sync::atomic::{AtomicI64, Ordering}; use taffy::style::FlexDirection; -use viewbuilder::ElementKey; +use viewbuilder::NodeKey; use viewbuilder::{Element, Tree}; fn button( tree: &mut Tree, label: &'static str, mut handler: impl FnMut(&mut Tree) + 'static, -) -> ElementKey { +) -> NodeKey { Element::new() .on_click(Box::new(move |tree, _event| handler(tree))) .background_color(Color4f::new(1., 1., 0., 1.)) diff --git a/src/element.rs b/src/element.rs index f21c327..ad7f3ca 100644 --- a/src/element.rs +++ b/src/element.rs @@ -1,6 +1,5 @@ -use crate::{event, node::NodeData, Node, Tree}; +use crate::{event, node::NodeData, Node, NodeKey, Tree}; use skia_safe::Color4f; -use slotmap::DefaultKey; use taffy::{ prelude::Size, style::{AlignItems, Dimension, FlexDirection, JustifyContent}, @@ -9,7 +8,7 @@ use taffy::{ /// Element of a user interface. pub struct Element { data: Option, - children: Option>, + children: Option>, } impl Default for Element { @@ -28,7 +27,7 @@ impl Element { } /// Add a child to the element. - pub fn child(&mut self, key: DefaultKey) -> &mut Self { + pub fn child(&mut self, key: NodeKey) -> &mut Self { if let Some(ref mut children) = self.children { children.push(key); } else { @@ -93,7 +92,7 @@ impl Element { } /// Build the element and insert it into the tree. - pub fn build(&mut self, tree: &mut Tree) -> DefaultKey { + pub fn build(&mut self, tree: &mut Tree) -> NodeKey { let mut elem = Node::new(NodeData::Element(self.data.take().unwrap())); elem.children = self.children.take(); diff --git a/src/event.rs b/src/event.rs index 5ff1941..2fc9d3e 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,4 +1,4 @@ -use slotmap::DefaultKey; +use crate::NodeKey; pub enum Event { Click(Click), @@ -7,13 +7,13 @@ pub enum Event { } pub struct Click { - pub target: DefaultKey, + pub target: NodeKey, } pub struct MouseIn { - pub target: DefaultKey, + pub target: NodeKey, } pub struct MouseOut { - pub target: DefaultKey, + pub target: NodeKey, } diff --git a/src/lib.rs b/src/lib.rs index 524ded5..c02e77e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,6 @@ //! This crate provides an HTML-like render API for the backend of a UI. //! It supports layout, drawing, and accessability. -use slotmap::DefaultKey; - pub mod node; pub use node::Node; @@ -22,13 +20,16 @@ pub use render::Renderer; pub mod event; pub use event::Event; -pub type ElementKey = slotmap::DefaultKey; +slotmap::new_key_type! { + /// Key to access a node in a tree. + pub struct NodeKey; +} /// Run the user interface tree. /// /// This will create a new window and render the tree, /// propagating events and re-rendering as they occuring. -pub fn run(tree: Tree, root: DefaultKey) { +pub fn run(tree: Tree, root: NodeKey) { let renderer = Renderer::new(); renderer.run(tree, root) } diff --git a/src/node.rs b/src/node.rs index e1063cb..f2f9464 100644 --- a/src/node.rs +++ b/src/node.rs @@ -1,4 +1,4 @@ -use crate::element::ElementData; +use crate::{element::ElementData, NodeKey}; use accesskit::NodeBuilder; use skia_safe::{Canvas, Color4f, Font, FontStyle, Paint, Rect, TextBlob, Typeface}; use slotmap::DefaultKey; @@ -33,10 +33,10 @@ pub struct Node { pub data: NodeData, /// Parent node id. - pub parent: Option, + pub parent: Option, /// Child node ids. - pub children: Option>, + pub children: Option>, /// Layout key for the taffy node. pub layout_key: Option, diff --git a/src/render.rs b/src/render.rs index 9564733..a8150f7 100644 --- a/src/render.rs +++ b/src/render.rs @@ -16,7 +16,7 @@ use skia_safe::{ gpu::{self, gl::FramebufferInfo, BackendRenderTarget, SurfaceOrigin}, Color, ColorType, Surface, }; -use slotmap::DefaultKey; + use std::{ ffi::CString, num::NonZeroU32, @@ -30,7 +30,7 @@ use winit::{ window::{Window, WindowBuilder}, }; -use crate::{event, Tree}; +use crate::{event, NodeKey, Tree}; pub struct UserEvent(pub Box); @@ -179,7 +179,7 @@ impl Renderer { } } - pub fn run(mut self, mut tree: Tree, root: DefaultKey) { + pub fn run(mut self, mut tree: Tree, root: NodeKey) { let mut previous_frame_start = Instant::now(); let mut hover_target = None; diff --git a/src/tree/iter.rs b/src/tree/iter.rs index db6d82c..e9185c8 100644 --- a/src/tree/iter.rs +++ b/src/tree/iter.rs @@ -1,15 +1,14 @@ use super::Nodes; -use crate::{node::NodeKind, Node}; -use slotmap::DefaultKey; +use crate::{node::NodeKind, Node, NodeKey}; enum Operation { - Key(DefaultKey), + Key(NodeKey), Pop(NodeKind), } pub enum Item<'a> { Node { - key: DefaultKey, + key: NodeKey, node: &'a Node, level: usize, }, @@ -26,7 +25,7 @@ pub struct Iter<'a> { } impl<'a> Iter<'a> { - pub(crate) fn new(tree: &'a Nodes, root: DefaultKey) -> Self { + pub(crate) fn new(tree: &'a Nodes, root: NodeKey) -> Self { Iter { tree, stack: vec![Operation::Key(root)], diff --git a/src/tree/iter_mut.rs b/src/tree/iter_mut.rs index e2cd411..e0f26ee 100644 --- a/src/tree/iter_mut.rs +++ b/src/tree/iter_mut.rs @@ -1,10 +1,9 @@ use super::Nodes; -use crate::{node::NodeKind, Node}; -use slotmap::DefaultKey; +use crate::{node::NodeKind, Node, NodeKey}; use std::marker::PhantomData; enum Operation { - Key(DefaultKey), + Key(NodeKey), Pop(NodeKind), } @@ -21,7 +20,7 @@ pub struct IterMut<'a> { } impl<'a> IterMut<'a> { - pub(crate) fn new(tree: &'a mut Nodes, root: DefaultKey) -> Self { + pub(crate) fn new(tree: &'a mut Nodes, root: NodeKey) -> Self { IterMut { tree, stack: vec![Operation::Key(root)], diff --git a/src/tree/mod.rs b/src/tree/mod.rs index 57f9503..33b7530 100644 --- a/src/tree/mod.rs +++ b/src/tree/mod.rs @@ -2,12 +2,12 @@ use crate::{ element::ElementData, event, node::{NodeData, NodeKind}, - Event, Node, + Event, Node, NodeKey, }; use accesskit::{NodeClassSet, NodeId, TreeUpdate}; use kurbo::Point; use skia_safe::Canvas; -use slotmap::{DefaultKey, SlotMap}; +use slotmap::SlotMap; use std::{borrow::Cow, num::NonZeroU128}; use taffy::{prelude::Size, style_helpers::TaffyMaxContent, Taffy}; @@ -21,7 +21,7 @@ mod node_ref; pub use self::node_ref::NodeRef; pub(crate) struct Inner { - pub(crate) changes: Vec, + pub(crate) changes: Vec, next_id: NonZeroU128, unused_ids: Vec, taffy: Taffy, @@ -57,7 +57,7 @@ pub struct Tree { } impl Tree { - pub fn send(&mut self, key: DefaultKey, event: Event) { + pub fn send(&mut self, key: NodeKey, event: Event) { let node = &mut self.nodes.nodes[key]; let handler = if let NodeData::Element(ref mut elem) = node.data { match event { @@ -102,7 +102,7 @@ impl Tree { } } - pub fn display(&self, root: DefaultKey) -> String { + pub fn display(&self, root: NodeKey) -> String { let mut s = String::new(); for item in self.nodes.iter(root) { @@ -149,17 +149,18 @@ impl Tree { s } - pub fn insert(&mut self, node: impl Into) -> DefaultKey { + pub fn insert(&mut self, node: impl Into) -> NodeKey { let key = self.nodes.nodes.insert(node.into()); self.inner.changes.push(key); key } - pub fn element(&mut self, key: DefaultKey) -> NodeRef { + /// Get a reference to the node stored under the given key. + pub fn node(&mut self, key: NodeKey) -> NodeRef { NodeRef { key, tree: self } } - pub fn set_text(&mut self, key: DefaultKey, content: impl Into>) { + pub fn set_text(&mut self, key: NodeKey, content: impl Into>) { if let NodeData::Text(ref mut dst) = self.nodes.nodes[key].data { *dst = content.into(); } else { @@ -167,7 +168,7 @@ impl Tree { } } - pub fn layout(&mut self, root: DefaultKey) { + pub fn layout(&mut self, root: NodeKey) { if self.inner.changes.is_empty() { return; } @@ -245,7 +246,7 @@ impl Tree { tree_update } - pub fn paint(&mut self, root: DefaultKey, canvas: &mut Canvas) { + pub fn paint(&mut self, root: NodeKey, canvas: &mut Canvas) { for item in self.nodes.iter_mut(root) { if let iter_mut::ItemMut::Node { node, level: _ } = item { node.paint(canvas); @@ -254,7 +255,7 @@ impl Tree { self.inner.changes.clear(); } - pub fn target(&self, root: DefaultKey, point: Point) -> Option { + pub fn target(&self, root: NodeKey, point: Point) -> Option { self.nodes .iter(root) .filter_map(|item| { @@ -289,15 +290,15 @@ impl Tree { #[derive(Default)] pub struct Nodes { - pub nodes: SlotMap, + pub nodes: SlotMap, } impl Nodes { - pub fn iter(&self, root: DefaultKey) -> Iter { + pub fn iter(&self, root: NodeKey) -> Iter { Iter::new(self, root) } - pub fn iter_mut(&mut self, root: DefaultKey) -> IterMut { + pub fn iter_mut(&mut self, root: NodeKey) -> IterMut { IterMut::new(self, root) } } diff --git a/src/tree/node_ref.rs b/src/tree/node_ref.rs index 26546ef..f76e4cc 100644 --- a/src/tree/node_ref.rs +++ b/src/tree/node_ref.rs @@ -1,11 +1,10 @@ -use crate::{element::ElementData, node::NodeData, Node, Tree}; +use crate::{element::ElementData, node::NodeData, Node, NodeKey, Tree}; use skia_safe::Color4f; -use slotmap::DefaultKey; use taffy::{prelude::Size, style::Dimension}; /// Reference to an element in a tree. pub struct NodeRef<'a> { - pub(crate) key: DefaultKey, + pub(crate) key: NodeKey, pub(crate) tree: &'a mut Tree, }