⎇ 2f5d0d1df229df22613a9cd7cd1bd74e3f192d47
benches/helpers/yoga_helpers.rs
History
Who touched what
raw
Use `struct NodeId(u64)` instead of `slotmap::DefaultKey` in all public APIs (#426)
1#![allow(dead_code)]
2use slotmap::{DefaultKey, SlotMap};
3
4pub mod yg {
5 pub use ordered_float::OrderedFloat;
6 use slotmap::{DefaultKey, SlotMap};
7 pub use yoga::types::*;
8 pub use yoga::Node;
9
10 pub type NodeId = DefaultKey;
11 pub type YogaTree = SlotMap<DefaultKey, Node>;
12}
13mod tf {
14 pub use taffy::prelude::*;
15}
16
17pub fn new_default_style_with_children(
18 tree: &mut SlotMap<DefaultKey, yg::Node>,
19 children: Vec<DefaultKey>,
20) -> DefaultKey {
21 let mut node = yg::Node::new();
22 for (i, child) in children.into_iter().enumerate() {
23 node.insert_child(&mut tree[child], i as u32);
24 }
25 tree.insert(node)
26}
27
28pub fn new_with_children(
29 tree: &mut SlotMap<DefaultKey, yg::Node>,
30 style: &tf::Style,
31 children: Vec<DefaultKey>,
32) -> DefaultKey {
33 let mut node = yg::Node::new();
34 apply_taffy_style(&mut node, style);
35 for (i, child) in children.into_iter().enumerate() {
36 node.insert_child(&mut tree[child], i as u32);
37 }
38 tree.insert(node)
39}
40
41fn into_yg_units(dim: impl Into<tf::Dimension>) -> yg::StyleUnit {
42 match dim.into() {
43 tf::Dimension::Auto => yg::StyleUnit::Auto,
44 tf::Dimension::Points(val) => yg::StyleUnit::Point(yg::OrderedFloat(val)),
45 tf::Dimension::Percent(val) => yg::StyleUnit::Percent(yg::OrderedFloat(val)),
46 }
47}
48
49fn into_pixels(dim: impl Into<tf::Dimension>) -> f32 {
50 dim.into().into_option().unwrap_or(0.0)
51}
52
53fn items_into_align(align: Option<tf::AlignSelf>) -> yg::Align {
54 match align {
55 None => yg::Align::Auto,
56 Some(tf::AlignSelf::FlexStart) => yg::Align::FlexStart,
57 Some(tf::AlignSelf::FlexEnd) => yg::Align::FlexEnd,
58 Some(tf::AlignSelf::Center) => yg::Align::Center,
59 Some(tf::AlignSelf::Baseline) => yg::Align::Baseline,
60 Some(tf::AlignSelf::Stretch) => yg::Align::Stretch,
61 Some(tf::AlignSelf::Start) => unimplemented!(),
62 Some(tf::AlignSelf::End) => unimplemented!(),
63 }
64}
65
66fn content_into_align(align: Option<tf::AlignContent>) -> yg::Align {
67 match align {
68 None => yg::Align::Auto,
69 Some(tf::AlignContent::FlexStart) => yg::Align::FlexStart,
70 Some(tf::AlignContent::Start) => yg::Align::FlexStart,
71 Some(tf::AlignContent::FlexEnd) => yg::Align::FlexEnd,
72 Some(tf::AlignContent::End) => yg::Align::FlexEnd,
73 Some(tf::AlignContent::Center) => yg::Align::Center,
74 Some(tf::AlignContent::Stretch) => yg::Align::Stretch,
75 Some(tf::AlignContent::SpaceBetween) => yg::Align::SpaceBetween,
76 Some(tf::AlignContent::SpaceAround) => yg::Align::SpaceAround,
77 Some(tf::AlignContent::SpaceEvenly) => unimplemented!(),
78 }
79}
80
81fn content_into_justify(align: Option<tf::JustifyContent>) -> yg::Justify {
82 match align {
83 None => yg::Justify::FlexStart,
84 Some(tf::JustifyContent::FlexStart) => yg::Justify::FlexStart,
85 Some(tf::JustifyContent::Start) => yg::Justify::FlexStart,
86 Some(tf::JustifyContent::FlexEnd) => yg::Justify::FlexEnd,
87 Some(tf::JustifyContent::End) => yg::Justify::FlexEnd,
88 Some(tf::JustifyContent::Center) => yg::Justify::Center,
89 Some(tf::JustifyContent::SpaceBetween) => yg::Justify::SpaceBetween,
90 Some(tf::JustifyContent::SpaceAround) => yg::Justify::SpaceAround,
91 Some(tf::JustifyContent::Stretch) => unimplemented!(),
92 Some(tf::JustifyContent::SpaceEvenly) => unimplemented!(),
93 }
94}
95
96pub fn apply_taffy_style(node: &mut yg::Node, style: &tf::Style) {
97 // display
98 node.set_display(match style.display {
99 tf::Display::Flex => yg::Display::Flex,
100 tf::Display::Grid => panic!("Yoga does not support CSS Grid"),
101 tf::Display::None => yg::Display::None,
102 });
103
104 // position
105 node.set_position_type(match style.position {
106 tf::Position::Relative => yg::PositionType::Relative,
107 tf::Position::Absolute => yg::PositionType::Absolute,
108 });
109 // inset
110 node.set_position(yg::Edge::Left, into_yg_units(style.inset.left));
111 node.set_position(yg::Edge::Right, into_yg_units(style.inset.right));
112 node.set_position(yg::Edge::Top, into_yg_units(style.inset.top));
113 node.set_position(yg::Edge::Bottom, into_yg_units(style.inset.bottom));
114
115 // sizes
116 node.set_width(into_yg_units(style.size.width));
117 node.set_height(into_yg_units(style.size.height));
118 node.set_min_width(into_yg_units(style.min_size.width));
119 node.set_min_height(into_yg_units(style.min_size.height));
120 node.set_max_width(into_yg_units(style.max_size.width));
121 node.set_max_height(into_yg_units(style.max_size.height));
122
123 // aspect_ratio
124 if let Some(aspect_ratio) = style.aspect_ratio {
125 node.set_aspect_ratio(aspect_ratio);
126 }
127
128 // spacing
129 node.set_padding(yg::Edge::Left, into_yg_units(style.padding.left));
130 node.set_padding(yg::Edge::Right, into_yg_units(style.padding.right));
131 node.set_padding(yg::Edge::Top, into_yg_units(style.padding.top));
132 node.set_padding(yg::Edge::Bottom, into_yg_units(style.padding.bottom));
133 node.set_margin(yg::Edge::Left, into_yg_units(style.margin.left));
134 node.set_margin(yg::Edge::Right, into_yg_units(style.margin.right));
135 node.set_margin(yg::Edge::Top, into_yg_units(style.margin.top));
136 node.set_margin(yg::Edge::Bottom, into_yg_units(style.margin.bottom));
137 node.set_border(yg::Edge::Left, into_pixels(style.border.left));
138 node.set_border(yg::Edge::Right, into_pixels(style.border.right));
139 node.set_border(yg::Edge::Top, into_pixels(style.border.top));
140 node.set_border(yg::Edge::Bottom, into_pixels(style.border.bottom));
141
142 // alignment
143 node.set_align_items(items_into_align(style.align_items));
144 node.set_align_self(items_into_align(style.align_self));
145 node.set_align_content(content_into_align(style.align_content));
146 node.set_justify_content(content_into_justify(style.justify_content));
147
148 // gap
149 node.set_column_gap(into_pixels(style.gap.width));
150 node.set_row_gap(into_pixels(style.gap.height));
151
152 // flex
153 node.set_flex_direction(match style.flex_direction {
154 tf::FlexDirection::Row => yg::FlexDirection::Row,
155 tf::FlexDirection::Column => yg::FlexDirection::Column,
156 tf::FlexDirection::RowReverse => yg::FlexDirection::RowReverse,
157 tf::FlexDirection::ColumnReverse => yg::FlexDirection::ColumnReverse,
158 });
159 node.set_flex_wrap(match style.flex_wrap {
160 tf::FlexWrap::NoWrap => yg::Wrap::NoWrap,
161 tf::FlexWrap::Wrap => yg::Wrap::Wrap,
162 tf::FlexWrap::WrapReverse => yg::Wrap::WrapReverse,
163 });
164 node.set_flex_basis(into_yg_units(style.flex_basis));
165 node.set_flex_grow(style.flex_grow);
166 node.set_flex_shrink(style.flex_shrink);
167}