Skip to content

Commit

Permalink
Create NodeKey struct
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Sep 24, 2023
1 parent f9d82e4 commit a670a05
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.))
Expand Down
4 changes: 2 additions & 2 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.))
Expand Down
9 changes: 4 additions & 5 deletions src/element.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -9,7 +8,7 @@ use taffy::{
/// Element of a user interface.
pub struct Element {
data: Option<ElementData>,
children: Option<Vec<DefaultKey>>,
children: Option<Vec<NodeKey>>,
}

impl Default for Element {
Expand All @@ -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 {
Expand Down Expand Up @@ -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();

Expand Down
8 changes: 4 additions & 4 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use slotmap::DefaultKey;
use crate::NodeKey;

pub enum Event {
Click(Click),
Expand All @@ -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,
}
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
}
6 changes: 3 additions & 3 deletions src/node.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -33,10 +33,10 @@ pub struct Node {
pub data: NodeData,

/// Parent node id.
pub parent: Option<DefaultKey>,
pub parent: Option<NodeKey>,

/// Child node ids.
pub children: Option<Vec<DefaultKey>>,
pub children: Option<Vec<NodeKey>>,

/// Layout key for the taffy node.
pub layout_key: Option<DefaultKey>,
Expand Down
6 changes: 3 additions & 3 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -30,7 +30,7 @@ use winit::{
window::{Window, WindowBuilder},
};

use crate::{event, Tree};
use crate::{event, NodeKey, Tree};

pub struct UserEvent(pub Box<dyn FnOnce(&mut Tree) + Send>);

Expand Down Expand Up @@ -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;
Expand Down
9 changes: 4 additions & 5 deletions src/tree/iter.rs
Original file line number Diff line number Diff line change
@@ -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,
},
Expand All @@ -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)],
Expand Down
7 changes: 3 additions & 4 deletions src/tree/iter_mut.rs
Original file line number Diff line number Diff line change
@@ -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),
}

Expand All @@ -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)],
Expand Down
29 changes: 15 additions & 14 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -21,7 +21,7 @@ mod node_ref;
pub use self::node_ref::NodeRef;

pub(crate) struct Inner {
pub(crate) changes: Vec<DefaultKey>,
pub(crate) changes: Vec<NodeKey>,
next_id: NonZeroU128,
unused_ids: Vec<NodeId>,
taffy: Taffy,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -149,25 +149,26 @@ impl Tree {
s
}

pub fn insert(&mut self, node: impl Into<Node>) -> DefaultKey {
pub fn insert(&mut self, node: impl Into<Node>) -> 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<Cow<'static, str>>) {
pub fn set_text(&mut self, key: NodeKey, content: impl Into<Cow<'static, str>>) {
if let NodeData::Text(ref mut dst) = self.nodes.nodes[key].data {
*dst = content.into();
} else {
todo!()
}
}

pub fn layout(&mut self, root: DefaultKey) {
pub fn layout(&mut self, root: NodeKey) {
if self.inner.changes.is_empty() {
return;
}
Expand Down Expand Up @@ -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);
Expand All @@ -254,7 +255,7 @@ impl Tree {
self.inner.changes.clear();
}

pub fn target(&self, root: DefaultKey, point: Point) -> Option<DefaultKey> {
pub fn target(&self, root: NodeKey, point: Point) -> Option<NodeKey> {
self.nodes
.iter(root)
.filter_map(|item| {
Expand Down Expand Up @@ -289,15 +290,15 @@ impl Tree {

#[derive(Default)]
pub struct Nodes {
pub nodes: SlotMap<DefaultKey, Node>,
pub nodes: SlotMap<NodeKey, Node>,
}

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)
}
}
5 changes: 2 additions & 3 deletions src/tree/node_ref.rs
Original file line number Diff line number Diff line change
@@ -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,
}

Expand Down

0 comments on commit a670a05

Please sign in to comment.