diff --git a/src/context.rs b/src/context.rs index 8d78db6..f09167d 100644 --- a/src/context.rs +++ b/src/context.rs @@ -118,8 +118,11 @@ impl Context { // Create the window layout let root_layout = self.tree[root].layout_key.unwrap(); - let mut style = Style::default(); - style.size = Size::from_points(size.width as _, size.height as _); + let style = Style { + size: Size::from_points(size.width as _, size.height as _), + ..Default::default() + }; + let window = self.taffy.new_with_children(style, &[root_layout]).unwrap(); // Compute the layout of the taffy tree. @@ -130,7 +133,7 @@ impl Context { for item in self.tree.iter_mut(root) { match item { ItemMut::Node { node, level: _ } => { - let mut layout = self.taffy.layout(node.layout_key.unwrap()).unwrap().clone(); + let mut layout = *self.taffy.layout(node.layout_key.unwrap()).unwrap(); if let Some(parent_layout) = stack.last() { layout.location.x += parent_layout.location.x; layout.location.y += parent_layout.location.y; diff --git a/src/element/attribute.rs b/src/element/attribute.rs index ed55611..51e22cd 100644 --- a/src/element/attribute.rs +++ b/src/element/attribute.rs @@ -5,6 +5,8 @@ use taffy::{ style::{AlignItems, Dimension, FlexDirection, JustifyContent, LengthPercentage}, }; +type Handler = Box, event::MouseEvent)>; + #[derive(Clone, Copy, PartialEq, Eq)] /// Element attribute kind. pub enum AttributeKind { @@ -56,13 +58,13 @@ pub enum AttributeValue { JustifyContent(JustifyContent), /// Click handler attribute value. - OnClick(Box, event::MouseEvent)>), + OnClick(Handler), /// Mouse in handler attribute value. - OnMouseIn(Box, event::MouseEvent)>), + OnMouseIn(Handler), // Mouse out handler attribute value. - OnMouseOut(Box, event::MouseEvent)>), + OnMouseOut(Handler), /// Color attribute value. Color(Color4f), diff --git a/src/node.rs b/src/node.rs index 653f33a..bd93cc6 100644 --- a/src/node.rs +++ b/src/node.rs @@ -148,7 +148,7 @@ impl Node { *data = Some(TextData { text_blob, font }); data.as_ref().unwrap().text_blob.as_ref().unwrap() }; - let bounds = text_blob.bounds().clone(); + let bounds = *text_blob.bounds(); // TODO this is a measure func for paragraphs taffy diff --git a/src/render.rs b/src/render.rs index 44504e3..63ce12f 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,5 +1,6 @@ use crate::{Context, NodeKey, Window}; use slotmap::{DefaultKey, SlotMap}; + use std::{ collections::HashMap, future::Future, @@ -13,14 +14,19 @@ use winit::{ window::WindowId, }; +#[derive(Clone, Copy, Debug)] +pub struct UpdateError; + +type Update = Box) + Send>; + pub struct Updater { tx: mpsc::Sender>, } impl Updater { /// Send an update to the UI tree. - pub fn update(&self, f: Box) + Send>) -> Result<(), ()> { - self.tx.send(UserEvent::Update(f)).map_err(|_| ()) + pub fn update(&self, f: Update) -> Result<(), UpdateError> { + self.tx.send(UserEvent::Update(f)).map_err(|_| UpdateError) } } @@ -41,13 +47,13 @@ impl Scope { } /// Send an update to the UI tree. - pub fn update(&self, f: Box) + Send>) -> Result<(), ()> { + pub fn update(&self, f: Update) -> Result<(), UpdateError> { self.updater.update(f) } } pub(crate) enum UserEvent { - Update(Box) + Send>), + Update(Update), FrameRequest, } diff --git a/src/window/builder.rs b/src/window/builder.rs index 77efbe7..8afd369 100644 --- a/src/window/builder.rs +++ b/src/window/builder.rs @@ -66,7 +66,7 @@ impl Builder { }) .unwrap() }) - .map_err(|error| Error::Display(error))?; + .map_err(Error::Display)?; let window = window.ok_or(Error::Window)?; let raw_window_handle = window.raw_window_handle(); diff --git a/src/window/mod.rs b/src/window/mod.rs index 58f19a9..81490b5 100644 --- a/src/window/mod.rs +++ b/src/window/mod.rs @@ -73,6 +73,8 @@ impl Window { root: NodeKey, event: WindowEvent, ) -> Result, Error> { + // TODO + #[allow(deprecated)] match event { WindowEvent::CloseRequested => { return Ok(Some(ControlFlow::Exit)); @@ -200,25 +202,21 @@ impl Window { .. } => { if let Some(pos) = self.cursor_pos { - match delta { - MouseScrollDelta::PixelDelta(px_delta) => { - let pos = Point::new(pos.x, pos.y); - if let Some(target) = tree.tree.target_with_filter( - root, - Point::new(pos.x, pos.y), - |node| { + if let MouseScrollDelta::PixelDelta(px_delta) = delta { + let pos = Point::new(pos.x, pos.y); + if let Some(target) = + tree.tree + .target_with_filter(root, Point::new(pos.x, pos.y), |node| { if let NodeData::Element(ref elem) = node.data { elem.overflow_y() == Some(Overflow::Scroll) } else { false } - }, - ) { - let mut node = tree.node(target); - node.scroll(Size::new(px_delta.x, px_delta.y)); - } + }) + { + let mut node = tree.node(target); + node.scroll(Size::new(px_delta.x, px_delta.y)); } - _ => {} } } }