use super::{ AbsoluteLength, Bounds, DefiniteLength, Edges, Layout, Length, Pixels, Point, Result, Size, Style, }; use collections::HashMap; use std::fmt::Debug; use taffy::{ geometry::Size as TaffySize, style::AvailableSpace as TaffyAvailableSpace, tree::{Measurable, MeasureFunc, NodeId}, Taffy, }; pub struct TaffyLayoutEngine { taffy: Taffy, children_to_parents: HashMap, absolute_layouts: HashMap, } impl TaffyLayoutEngine { pub fn new() -> Self { TaffyLayoutEngine { taffy: Taffy::new(), children_to_parents: HashMap::default(), absolute_layouts: HashMap::default(), } } pub fn request_layout( &mut self, style: Style, rem_size: Pixels, children: &[LayoutId], ) -> Result { let style = style.to_taffy(rem_size); if children.is_empty() { Ok(self.taffy.new_leaf(style)?.into()) } else { let parent_id = self .taffy // This is safe because LayoutId is repr(transparent) to taffy::tree::NodeId. .new_with_children(style, unsafe { std::mem::transmute(children) })? .into(); for child_id in children { self.children_to_parents.insert(*child_id, parent_id); } Ok(parent_id) } } pub fn request_measured_layout( &mut self, style: Style, rem_size: Pixels, measure: impl Fn(Size>, Size) -> Size + Send + Sync + 'static, ) -> Result { let style = style.to_taffy(rem_size); let measurable = Box::new(Measureable(measure)) as Box; Ok(self .taffy .new_leaf_with_measure(style, MeasureFunc::Boxed(measurable))? .into()) } pub fn layout(&mut self, id: LayoutId) -> Result { if let Some(layout) = self.absolute_layouts.get(&id).cloned() { return Ok(layout); } let mut relative_layout: Layout = self.taffy.layout(id.into()).map(Into::into)?; if let Some(parent_id) = self.children_to_parents.get(&id).copied() { let parent_layout = self.layout(parent_id)?; relative_layout.bounds.origin += parent_layout.bounds.origin; } self.absolute_layouts.insert(id, relative_layout.clone()); Ok(relative_layout) } } #[derive(Copy, Clone, Eq, PartialEq, Debug)] #[repr(transparent)] pub struct LayoutId(NodeId); impl std::hash::Hash for LayoutId { fn hash(&self, state: &mut H) { u64::from(self.0).hash(state); } } impl From for LayoutId { fn from(node_id: NodeId) -> Self { Self(node_id) } } impl From for NodeId { fn from(layout_id: LayoutId) -> NodeId { layout_id.0 } } #[derive(Copy, Clone, Debug)] pub enum AvailableSpace { /// The amount of space available is the specified number of pixels Definite(Pixels), /// The amount of space available is indefinite and the node should be laid out under a min-content constraint MinContent, /// The amount of space available is indefinite and the node should be laid out under a max-content constraint MaxContent, } struct Measureable(F); impl taffy::tree::Measurable for Measureable where F: Send + Sync + Fn(Size>, Size) -> Size, { fn measure( &self, known_dimensions: TaffySize>, available_space: TaffySize, ) -> TaffySize { let known_dimensions: Size> = known_dimensions.into(); let known_dimensions: Size> = known_dimensions.map(|d| d.map(Into::into)); let available_space = available_space.into(); let size = (self.0)(known_dimensions, available_space); size.into() } } trait ToTaffy { fn to_taffy(&self, rem_size: Pixels) -> Output; } impl ToTaffy for Style { fn to_taffy(&self, rem_size: Pixels) -> taffy::style::Style { taffy::style::Style { display: self.display, overflow: self.overflow.clone().into(), scrollbar_width: self.scrollbar_width, position: self.position, inset: self.inset.to_taffy(rem_size), size: self.size.to_taffy(rem_size), min_size: self.min_size.to_taffy(rem_size), max_size: self.max_size.to_taffy(rem_size), aspect_ratio: self.aspect_ratio, margin: self.margin.to_taffy(rem_size), padding: self.padding.to_taffy(rem_size), border: self.border_widths.to_taffy(rem_size), align_items: self.align_items, align_self: self.align_self, align_content: self.align_content, justify_content: self.justify_content, gap: self.gap.to_taffy(rem_size), flex_direction: self.flex_direction, flex_wrap: self.flex_wrap, flex_basis: self.flex_basis.to_taffy(rem_size), flex_grow: self.flex_grow, flex_shrink: self.flex_shrink, ..Default::default() // Ignore grid properties for now } } } // impl ToTaffy for Bounds { // type Output = taffy::prelude::Bounds; // fn to_taffy( // &self, // rem_size: Pixels, // ) -> taffy::prelude::Bounds { // taffy::prelude::Bounds { // origin: self.origin.to_taffy(rem_size), // size: self.size.to_taffy(rem_size), // } // } // } impl ToTaffy for Length { fn to_taffy(&self, rem_size: Pixels) -> taffy::prelude::LengthPercentageAuto { match self { Length::Definite(length) => length.to_taffy(rem_size), Length::Auto => taffy::prelude::LengthPercentageAuto::Auto, } } } impl ToTaffy for Length { fn to_taffy(&self, rem_size: Pixels) -> taffy::prelude::Dimension { match self { Length::Definite(length) => length.to_taffy(rem_size), Length::Auto => taffy::prelude::Dimension::Auto, } } } impl ToTaffy for DefiniteLength { fn to_taffy(&self, rem_size: Pixels) -> taffy::style::LengthPercentage { match self { DefiniteLength::Absolute(length) => match length { AbsoluteLength::Pixels(pixels) => { taffy::style::LengthPercentage::Length(pixels.into()) } AbsoluteLength::Rems(rems) => { taffy::style::LengthPercentage::Length((*rems * rem_size).into()) } }, DefiniteLength::Fraction(fraction) => { taffy::style::LengthPercentage::Percent(*fraction) } } } } impl ToTaffy for DefiniteLength { fn to_taffy(&self, rem_size: Pixels) -> taffy::style::LengthPercentageAuto { match self { DefiniteLength::Absolute(length) => match length { AbsoluteLength::Pixels(pixels) => { taffy::style::LengthPercentageAuto::Length(pixels.into()) } AbsoluteLength::Rems(rems) => { taffy::style::LengthPercentageAuto::Length((*rems * rem_size).into()) } }, DefiniteLength::Fraction(fraction) => { taffy::style::LengthPercentageAuto::Percent(*fraction) } } } } impl ToTaffy for DefiniteLength { fn to_taffy(&self, rem_size: Pixels) -> taffy::style::Dimension { match self { DefiniteLength::Absolute(length) => match length { AbsoluteLength::Pixels(pixels) => taffy::style::Dimension::Length(pixels.into()), AbsoluteLength::Rems(rems) => { taffy::style::Dimension::Length((*rems * rem_size).into()) } }, DefiniteLength::Fraction(fraction) => taffy::style::Dimension::Percent(*fraction), } } } impl ToTaffy for AbsoluteLength { fn to_taffy(&self, rem_size: Pixels) -> taffy::style::LengthPercentage { match self { AbsoluteLength::Pixels(pixels) => taffy::style::LengthPercentage::Length(pixels.into()), AbsoluteLength::Rems(rems) => { taffy::style::LengthPercentage::Length((*rems * rem_size).into()) } } } } impl From> for Point where T: Into, { fn from(point: taffy::geometry::Point) -> Point { Point { x: point.x.into(), y: point.y.into(), } } } impl Into> for Point where T: Into, { fn into(self) -> taffy::geometry::Point { taffy::geometry::Point { x: self.x.into(), y: self.y.into(), } } } impl + Clone + Debug, U> ToTaffy> for Size { fn to_taffy(&self, rem_size: Pixels) -> taffy::geometry::Size { taffy::geometry::Size { width: self.width.to_taffy(rem_size).into(), height: self.height.to_taffy(rem_size).into(), } } } impl ToTaffy> for Edges where T: ToTaffy + Clone + Debug, { fn to_taffy(&self, rem_size: Pixels) -> taffy::geometry::Rect { taffy::geometry::Rect { top: self.top.to_taffy(rem_size).into(), right: self.right.to_taffy(rem_size).into(), bottom: self.bottom.to_taffy(rem_size).into(), left: self.left.to_taffy(rem_size).into(), } } } impl, U: Clone + Debug> From> for Size { fn from(taffy_size: taffy::geometry::Size) -> Self { Size { width: taffy_size.width.into(), height: taffy_size.height.into(), } } } impl + Clone + Debug, U> From> for taffy::geometry::Size { fn from(size: Size) -> Self { taffy::geometry::Size { width: size.width.into(), height: size.height.into(), } } } // impl From>> for Size> { // fn from(value: TaffySize>) -> Self { // Self { // width: value.width.map(Into::into), // height: value.height.map(Into::into), // } // } // } // impl From> for Size { // fn from(taffy_size: TaffySize) -> Self { // Size { // width: From::from(taffy_size.width), // height: From::from(taffy_size.height), // } // } // } impl From for AvailableSpace { fn from(space: TaffyAvailableSpace) -> Self { match space { TaffyAvailableSpace::Definite(value) => AvailableSpace::Definite(Pixels(value)), TaffyAvailableSpace::MinContent => AvailableSpace::MinContent, TaffyAvailableSpace::MaxContent => AvailableSpace::MaxContent, } } } impl From<&taffy::tree::Layout> for Layout { fn from(layout: &taffy::tree::Layout) -> Self { Layout { order: layout.order, bounds: Bounds { origin: layout.location.into(), size: layout.size.into(), }, } } }