Skip to content

Commit 6441efc

Browse files
committed
Initial taffy_stylo crate (flexbox only)
1 parent 3a865b4 commit 6441efc

File tree

6 files changed

+593
-0
lines changed

6 files changed

+593
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,5 @@ members = [
101101
"scripts/format-fixtures",
102102
"scripts/import-yoga-tests",
103103
"benches",
104+
"taffy_stylo",
104105
]

taffy_stylo/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "taffy_stylo"
3+
version = "0.0.1"
4+
edition = "2021"
5+
license = "MIT OR Apache-2.0"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
taffy = { path = ".." }
11+
style = { git = "https://github.com/servo/stylo", branch = "2024-05-31", features = ["servo"] }
12+
servo_arc = { git = "https://github.com/servo/stylo", branch = "2024-05-31" }

taffy_stylo/src/arc.rs

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
use crate::{stylo, t2s};
2+
use servo_arc::Arc;
3+
4+
pub struct TaffyStyloStyle(pub Arc<stylo::ComputedValues>);
5+
6+
impl From<Arc<stylo::ComputedValues>> for TaffyStyloStyle {
7+
fn from(value: Arc<stylo::ComputedValues>) -> Self {
8+
Self(Arc::clone(&value))
9+
}
10+
}
11+
12+
impl taffy::CoreStyle for TaffyStyloStyle {
13+
#[inline]
14+
fn box_generation_mode(&self) -> taffy::BoxGenerationMode {
15+
t2s::box_generation_mode(self.0.get_box().display)
16+
}
17+
18+
#[inline]
19+
fn is_block(&self) -> bool {
20+
t2s::is_block(self.0.get_box().display)
21+
}
22+
23+
#[inline]
24+
fn overflow(&self) -> taffy::Point<taffy::Overflow> {
25+
let box_styles = self.0.get_box();
26+
taffy::Point { x: t2s::overflow(box_styles.overflow_x), y: t2s::overflow(box_styles.overflow_y) }
27+
}
28+
29+
#[inline]
30+
fn scrollbar_width(&self) -> f32 {
31+
0.0
32+
}
33+
34+
#[inline]
35+
fn position(&self) -> taffy::Position {
36+
t2s::position(self.0.get_box().position)
37+
}
38+
39+
#[inline]
40+
fn inset(&self) -> taffy::Rect<taffy::LengthPercentageAuto> {
41+
let position_styles = self.0.get_position();
42+
taffy::Rect {
43+
left: t2s::length_percentage_auto(&position_styles.left),
44+
right: t2s::length_percentage_auto(&position_styles.right),
45+
top: t2s::length_percentage_auto(&position_styles.top),
46+
bottom: t2s::length_percentage_auto(&position_styles.bottom),
47+
}
48+
}
49+
50+
#[inline]
51+
fn size(&self) -> taffy::Size<taffy::Dimension> {
52+
let position_styles = self.0.get_position();
53+
taffy::Size { width: t2s::dimension(&position_styles.width), height: t2s::dimension(&position_styles.height) }
54+
}
55+
56+
#[inline]
57+
fn min_size(&self) -> taffy::Size<taffy::Dimension> {
58+
let position_styles = self.0.get_position();
59+
taffy::Size {
60+
width: t2s::dimension(&position_styles.min_width),
61+
height: t2s::dimension(&position_styles.min_height),
62+
}
63+
}
64+
65+
#[inline]
66+
fn max_size(&self) -> taffy::Size<taffy::Dimension> {
67+
let position_styles = self.0.get_position();
68+
taffy::Size {
69+
width: t2s::max_size_dimension(&position_styles.max_width),
70+
height: t2s::max_size_dimension(&position_styles.max_height),
71+
}
72+
}
73+
74+
#[inline]
75+
fn aspect_ratio(&self) -> Option<f32> {
76+
t2s::aspect_ratio(self.0.get_position().aspect_ratio)
77+
}
78+
79+
#[inline]
80+
fn margin(&self) -> taffy::Rect<taffy::LengthPercentageAuto> {
81+
let margin_styles = self.0.get_margin();
82+
taffy::Rect {
83+
left: t2s::length_percentage_auto(&margin_styles.margin_left),
84+
right: t2s::length_percentage_auto(&margin_styles.margin_right),
85+
top: t2s::length_percentage_auto(&margin_styles.margin_top),
86+
bottom: t2s::length_percentage_auto(&margin_styles.margin_bottom),
87+
}
88+
}
89+
90+
#[inline]
91+
fn padding(&self) -> taffy::Rect<taffy::LengthPercentage> {
92+
let padding_styles = self.0.get_padding();
93+
taffy::Rect {
94+
left: t2s::length_percentage(&padding_styles.padding_left.0),
95+
right: t2s::length_percentage(&padding_styles.padding_right.0),
96+
top: t2s::length_percentage(&padding_styles.padding_top.0),
97+
bottom: t2s::length_percentage(&padding_styles.padding_bottom.0),
98+
}
99+
}
100+
101+
#[inline]
102+
fn border(&self) -> taffy::Rect<taffy::LengthPercentage> {
103+
let border_styles = self.0.get_border();
104+
taffy::Rect {
105+
left: taffy::LengthPercentage::Length(border_styles.border_left_width.to_f32_px()),
106+
right: taffy::LengthPercentage::Length(border_styles.border_right_width.to_f32_px()),
107+
top: taffy::LengthPercentage::Length(border_styles.border_top_width.to_f32_px()),
108+
bottom: taffy::LengthPercentage::Length(border_styles.border_bottom_width.to_f32_px()),
109+
}
110+
}
111+
}
112+
113+
impl taffy::FlexboxContainerStyle for TaffyStyloStyle {
114+
#[inline]
115+
fn flex_direction(&self) -> taffy::FlexDirection {
116+
t2s::flex_direction(self.0.get_position().flex_direction)
117+
}
118+
119+
#[inline]
120+
fn flex_wrap(&self) -> taffy::FlexWrap {
121+
t2s::flex_wrap(self.0.get_position().flex_wrap)
122+
}
123+
124+
#[inline]
125+
fn gap(&self) -> taffy::Size<taffy::LengthPercentage> {
126+
let position_styles = self.0.get_position();
127+
taffy::Size {
128+
width: t2s::gap(&position_styles.column_gap),
129+
height: taffy::LengthPercentage::Length(0.0), // TODO: enable row_gap in stylo
130+
}
131+
}
132+
133+
#[inline]
134+
fn align_content(&self) -> Option<taffy::AlignContent> {
135+
t2s::align_content(self.0.get_position().align_content)
136+
}
137+
138+
#[inline]
139+
fn align_items(&self) -> Option<taffy::AlignItems> {
140+
t2s::align_items(self.0.get_position().align_items)
141+
}
142+
143+
#[inline]
144+
fn justify_content(&self) -> Option<taffy::JustifyContent> {
145+
t2s::justify_content(self.0.get_position().justify_content)
146+
}
147+
}
148+
149+
impl taffy::FlexboxItemStyle for TaffyStyloStyle {
150+
#[inline]
151+
fn flex_basis(&self) -> taffy::Dimension {
152+
t2s::flex_basis(&self.0.get_position().flex_basis)
153+
}
154+
155+
#[inline]
156+
fn flex_grow(&self) -> f32 {
157+
self.0.get_position().flex_grow.0
158+
}
159+
160+
#[inline]
161+
fn flex_shrink(&self) -> f32 {
162+
self.0.get_position().flex_shrink.0
163+
}
164+
165+
#[inline]
166+
fn align_self(&self) -> Option<taffy::AlignSelf> {
167+
t2s::align_self(self.0.get_position().align_self)
168+
}
169+
}

taffy_stylo/src/borrowed.rs

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
use crate::{stylo, t2s};
2+
3+
pub struct TaffyStyloStyleRef<'a>(pub &'a stylo::ComputedValues);
4+
5+
impl<'a> From<&'a stylo::ComputedValues> for TaffyStyloStyleRef<'a> {
6+
fn from(value: &'a stylo::ComputedValues) -> Self {
7+
Self(value)
8+
}
9+
}
10+
11+
impl taffy::CoreStyle for TaffyStyloStyleRef<'_> {
12+
#[inline]
13+
fn box_generation_mode(&self) -> taffy::BoxGenerationMode {
14+
t2s::box_generation_mode(self.0.get_box().display)
15+
}
16+
17+
#[inline]
18+
fn is_block(&self) -> bool {
19+
t2s::is_block(self.0.get_box().display)
20+
}
21+
22+
#[inline]
23+
fn overflow(&self) -> taffy::Point<taffy::Overflow> {
24+
let box_styles = self.0.get_box();
25+
taffy::Point { x: t2s::overflow(box_styles.overflow_x), y: t2s::overflow(box_styles.overflow_y) }
26+
}
27+
28+
#[inline]
29+
fn scrollbar_width(&self) -> f32 {
30+
0.0
31+
}
32+
33+
#[inline]
34+
fn position(&self) -> taffy::Position {
35+
t2s::position(self.0.get_box().position)
36+
}
37+
38+
#[inline]
39+
fn inset(&self) -> taffy::Rect<taffy::LengthPercentageAuto> {
40+
let position_styles = self.0.get_position();
41+
taffy::Rect {
42+
left: t2s::length_percentage_auto(&position_styles.left),
43+
right: t2s::length_percentage_auto(&position_styles.right),
44+
top: t2s::length_percentage_auto(&position_styles.top),
45+
bottom: t2s::length_percentage_auto(&position_styles.bottom),
46+
}
47+
}
48+
49+
#[inline]
50+
fn size(&self) -> taffy::Size<taffy::Dimension> {
51+
let position_styles = self.0.get_position();
52+
taffy::Size { width: t2s::dimension(&position_styles.width), height: t2s::dimension(&position_styles.height) }
53+
}
54+
55+
#[inline]
56+
fn min_size(&self) -> taffy::Size<taffy::Dimension> {
57+
let position_styles = self.0.get_position();
58+
taffy::Size {
59+
width: t2s::dimension(&position_styles.min_width),
60+
height: t2s::dimension(&position_styles.min_height),
61+
}
62+
}
63+
64+
#[inline]
65+
fn max_size(&self) -> taffy::Size<taffy::Dimension> {
66+
let position_styles = self.0.get_position();
67+
taffy::Size {
68+
width: t2s::max_size_dimension(&position_styles.max_width),
69+
height: t2s::max_size_dimension(&position_styles.max_height),
70+
}
71+
}
72+
73+
#[inline]
74+
fn aspect_ratio(&self) -> Option<f32> {
75+
t2s::aspect_ratio(self.0.get_position().aspect_ratio)
76+
}
77+
78+
#[inline]
79+
fn margin(&self) -> taffy::Rect<taffy::LengthPercentageAuto> {
80+
let margin_styles = self.0.get_margin();
81+
taffy::Rect {
82+
left: t2s::length_percentage_auto(&margin_styles.margin_left),
83+
right: t2s::length_percentage_auto(&margin_styles.margin_right),
84+
top: t2s::length_percentage_auto(&margin_styles.margin_top),
85+
bottom: t2s::length_percentage_auto(&margin_styles.margin_bottom),
86+
}
87+
}
88+
89+
#[inline]
90+
fn padding(&self) -> taffy::Rect<taffy::LengthPercentage> {
91+
let padding_styles = self.0.get_padding();
92+
taffy::Rect {
93+
left: t2s::length_percentage(&padding_styles.padding_left.0),
94+
right: t2s::length_percentage(&padding_styles.padding_right.0),
95+
top: t2s::length_percentage(&padding_styles.padding_top.0),
96+
bottom: t2s::length_percentage(&padding_styles.padding_bottom.0),
97+
}
98+
}
99+
100+
#[inline]
101+
fn border(&self) -> taffy::Rect<taffy::LengthPercentage> {
102+
let border_styles = self.0.get_border();
103+
taffy::Rect {
104+
left: taffy::LengthPercentage::Length(border_styles.border_left_width.to_f32_px()),
105+
right: taffy::LengthPercentage::Length(border_styles.border_right_width.to_f32_px()),
106+
top: taffy::LengthPercentage::Length(border_styles.border_top_width.to_f32_px()),
107+
bottom: taffy::LengthPercentage::Length(border_styles.border_bottom_width.to_f32_px()),
108+
}
109+
}
110+
}
111+
112+
impl taffy::FlexboxContainerStyle for TaffyStyloStyleRef<'_> {
113+
#[inline]
114+
fn flex_direction(&self) -> taffy::FlexDirection {
115+
t2s::flex_direction(self.0.get_position().flex_direction)
116+
}
117+
118+
#[inline]
119+
fn flex_wrap(&self) -> taffy::FlexWrap {
120+
t2s::flex_wrap(self.0.get_position().flex_wrap)
121+
}
122+
123+
#[inline]
124+
fn gap(&self) -> taffy::Size<taffy::LengthPercentage> {
125+
let position_styles = self.0.get_position();
126+
taffy::Size {
127+
width: t2s::gap(&position_styles.column_gap),
128+
height: taffy::LengthPercentage::Length(0.0), // TODO: enable row_gap in stylo
129+
}
130+
}
131+
132+
#[inline]
133+
fn align_content(&self) -> Option<taffy::AlignContent> {
134+
t2s::align_content(self.0.get_position().align_content)
135+
}
136+
137+
#[inline]
138+
fn align_items(&self) -> Option<taffy::AlignItems> {
139+
t2s::align_items(self.0.get_position().align_items)
140+
}
141+
142+
#[inline]
143+
fn justify_content(&self) -> Option<taffy::JustifyContent> {
144+
t2s::justify_content(self.0.get_position().justify_content)
145+
}
146+
}
147+
148+
impl taffy::FlexboxItemStyle for TaffyStyloStyleRef<'_> {
149+
#[inline]
150+
fn flex_basis(&self) -> taffy::Dimension {
151+
t2s::flex_basis(&self.0.get_position().flex_basis)
152+
}
153+
154+
#[inline]
155+
fn flex_grow(&self) -> f32 {
156+
self.0.get_position().flex_grow.0
157+
}
158+
159+
#[inline]
160+
fn flex_shrink(&self) -> f32 {
161+
self.0.get_position().flex_shrink.0
162+
}
163+
164+
#[inline]
165+
fn align_self(&self) -> Option<taffy::AlignSelf> {
166+
t2s::align_self(self.0.get_position().align_self)
167+
}
168+
}

0 commit comments

Comments
 (0)