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