tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 717HTML 669Markdown 12TOML 62372 Sutichaykuna

⎇ 9915b967e9809dc57f210de8fe5f7510e66806d6 taffy_stylo/src/arc.rs Rust · 261 siq'ikuna · 8.4 KB Ñawpa willay Pi imata llamkarqan ch'uya

Stylo integration (#693) 9915b967e · Nico Burns ·

1use crate::convert;
2use servo_arc::Arc;
3use style::properties::generated::ComputedValues;
4
5pub struct TaffyStyloStyle(pub Arc<ComputedValues>);
6
7impl From<Arc<ComputedValues>> for TaffyStyloStyle {
8    fn from(value: Arc<ComputedValues>) -> Self {
9        Self(Arc::clone(&value))
10    }
11}
12
13impl taffy::CoreStyle for TaffyStyloStyle {
14    #[inline]
15    fn box_generation_mode(&self) -> taffy::BoxGenerationMode {
16        convert::box_generation_mode(self.0.get_box().display)
17    }
18
19    #[inline]
20    fn is_block(&self) -> bool {
21        convert::is_block(self.0.get_box().display)
22    }
23
24    #[inline]
25    fn box_sizing(&self) -> taffy::BoxSizing {
26        convert::box_sizing(self.0.get_position().box_sizing)
27    }
28
29    #[inline]
30    fn overflow(&self) -> taffy::Point<taffy::Overflow> {
31        let box_styles = self.0.get_box();
32        taffy::Point { x: convert::overflow(box_styles.overflow_x), y: convert::overflow(box_styles.overflow_y) }
33    }
34
35    #[inline]
36    fn scrollbar_width(&self) -> f32 {
37        0.0
38    }
39
40    #[inline]
41    fn position(&self) -> taffy::Position {
42        convert::position(self.0.get_box().position)
43    }
44
45    #[inline]
46    fn inset(&self) -> taffy::Rect<taffy::LengthPercentageAuto> {
47        let position_styles = self.0.get_position();
48        taffy::Rect {
49            left: convert::length_percentage_auto(&position_styles.left),
50            right: convert::length_percentage_auto(&position_styles.right),
51            top: convert::length_percentage_auto(&position_styles.top),
52            bottom: convert::length_percentage_auto(&position_styles.bottom),
53        }
54    }
55
56    #[inline]
57    fn size(&self) -> taffy::Size<taffy::Dimension> {
58        let position_styles = self.0.get_position();
59        taffy::Size {
60            width: convert::dimension(&position_styles.width),
61            height: convert::dimension(&position_styles.height),
62        }
63    }
64
65    #[inline]
66    fn min_size(&self) -> taffy::Size<taffy::Dimension> {
67        let position_styles = self.0.get_position();
68        taffy::Size {
69            width: convert::dimension(&position_styles.min_width),
70            height: convert::dimension(&position_styles.min_height),
71        }
72    }
73
74    #[inline]
75    fn max_size(&self) -> taffy::Size<taffy::Dimension> {
76        let position_styles = self.0.get_position();
77        taffy::Size {
78            width: convert::max_size_dimension(&position_styles.max_width),
79            height: convert::max_size_dimension(&position_styles.max_height),
80        }
81    }
82
83    #[inline]
84    fn aspect_ratio(&self) -> Option<f32> {
85        convert::aspect_ratio(self.0.get_position().aspect_ratio)
86    }
87
88    #[inline]
89    fn margin(&self) -> taffy::Rect<taffy::LengthPercentageAuto> {
90        let margin_styles = self.0.get_margin();
91        taffy::Rect {
92            left: convert::length_percentage_auto(&margin_styles.margin_left),
93            right: convert::length_percentage_auto(&margin_styles.margin_right),
94            top: convert::length_percentage_auto(&margin_styles.margin_top),
95            bottom: convert::length_percentage_auto(&margin_styles.margin_bottom),
96        }
97    }
98
99    #[inline]
100    fn padding(&self) -> taffy::Rect<taffy::LengthPercentage> {
101        let padding_styles = self.0.get_padding();
102        taffy::Rect {
103            left: convert::length_percentage(&padding_styles.padding_left.0),
104            right: convert::length_percentage(&padding_styles.padding_right.0),
105            top: convert::length_percentage(&padding_styles.padding_top.0),
106            bottom: convert::length_percentage(&padding_styles.padding_bottom.0),
107        }
108    }
109
110    #[inline]
111    fn border(&self) -> taffy::Rect<taffy::LengthPercentage> {
112        let border_styles = self.0.get_border();
113        taffy::Rect {
114            left: taffy::LengthPercentage::Length(border_styles.border_left_width.to_f32_px()),
115            right: taffy::LengthPercentage::Length(border_styles.border_right_width.to_f32_px()),
116            top: taffy::LengthPercentage::Length(border_styles.border_top_width.to_f32_px()),
117            bottom: taffy::LengthPercentage::Length(border_styles.border_bottom_width.to_f32_px()),
118        }
119    }
120}
121
122impl taffy::FlexboxContainerStyle for TaffyStyloStyle {
123    #[inline]
124    fn flex_direction(&self) -> taffy::FlexDirection {
125        convert::flex_direction(self.0.get_position().flex_direction)
126    }
127
128    #[inline]
129    fn flex_wrap(&self) -> taffy::FlexWrap {
130        convert::flex_wrap(self.0.get_position().flex_wrap)
131    }
132
133    #[inline]
134    fn gap(&self) -> taffy::Size<taffy::LengthPercentage> {
135        let position_styles = self.0.get_position();
136        taffy::Size { width: convert::gap(&position_styles.column_gap), height: convert::gap(&position_styles.row_gap) }
137    }
138
139    #[inline]
140    fn align_content(&self) -> Option<taffy::AlignContent> {
141        convert::content_alignment(self.0.get_position().align_content.0)
142    }
143
144    #[inline]
145    fn align_items(&self) -> Option<taffy::AlignItems> {
146        convert::item_alignment(self.0.get_position().align_items.0)
147    }
148
149    #[inline]
150    fn justify_content(&self) -> Option<taffy::JustifyContent> {
151        convert::content_alignment(self.0.get_position().justify_content.0)
152    }
153}
154
155impl taffy::FlexboxItemStyle for TaffyStyloStyle {
156    #[inline]
157    fn flex_basis(&self) -> taffy::Dimension {
158        convert::flex_basis(&self.0.get_position().flex_basis)
159    }
160
161    #[inline]
162    fn flex_grow(&self) -> f32 {
163        self.0.get_position().flex_grow.0
164    }
165
166    #[inline]
167    fn flex_shrink(&self) -> f32 {
168        self.0.get_position().flex_shrink.0
169    }
170
171    #[inline]
172    fn align_self(&self) -> Option<taffy::AlignSelf> {
173        convert::item_alignment(self.0.get_position().align_self.0 .0)
174    }
175}
176
177impl taffy::GridContainerStyle for TaffyStyloStyle {
178    type TemplateTrackList<'a> = Vec<taffy::TrackSizingFunction> where Self: 'a;
179    type AutoTrackList<'a> = Vec<taffy::NonRepeatedTrackSizingFunction> where Self: 'a;
180
181    #[inline]
182    fn grid_template_rows(&self) -> Self::TemplateTrackList<'_> {
183        convert::grid_template_tracks(&self.0.get_position().grid_template_rows)
184    }
185
186    #[inline]
187    fn grid_template_columns(&self) -> Self::TemplateTrackList<'_> {
188        convert::grid_template_tracks(&self.0.get_position().grid_template_columns)
189    }
190
191    #[inline]
192    fn grid_auto_rows(&self) -> Self::AutoTrackList<'_> {
193        convert::grid_auto_tracks(&self.0.get_position().grid_auto_rows)
194    }
195
196    #[inline]
197    fn grid_auto_columns(&self) -> Self::AutoTrackList<'_> {
198        convert::grid_auto_tracks(&self.0.get_position().grid_auto_columns)
199    }
200
201    #[inline]
202    fn grid_auto_flow(&self) -> taffy::GridAutoFlow {
203        convert::grid_auto_flow(self.0.get_position().grid_auto_flow)
204    }
205
206    #[inline]
207    fn gap(&self) -> taffy::Size<taffy::LengthPercentage> {
208        let position_styles = self.0.get_position();
209        taffy::Size { width: convert::gap(&position_styles.column_gap), height: convert::gap(&position_styles.row_gap) }
210    }
211
212    #[inline]
213    fn align_content(&self) -> Option<taffy::AlignContent> {
214        convert::content_alignment(self.0.get_position().align_content.0)
215    }
216
217    #[inline]
218    fn justify_content(&self) -> Option<taffy::JustifyContent> {
219        convert::content_alignment(self.0.get_position().justify_content.0)
220    }
221
222    #[inline]
223    fn align_items(&self) -> Option<taffy::AlignItems> {
224        convert::item_alignment(self.0.get_position().align_items.0)
225    }
226
227    #[inline]
228    fn justify_items(&self) -> Option<taffy::AlignItems> {
229        convert::item_alignment(self.0.get_position().justify_items.computed.0)
230    }
231}
232
233impl taffy::GridItemStyle for TaffyStyloStyle {
234    #[inline]
235    fn grid_row(&self) -> taffy::Line<taffy::GridPlacement> {
236        let position_styles = self.0.get_position();
237        taffy::Line {
238            start: convert::grid_line(&position_styles.grid_row_start),
239            end: convert::grid_line(&position_styles.grid_row_end),
240        }
241    }
242
243    #[inline]
244    fn grid_column(&self) -> taffy::Line<taffy::GridPlacement> {
245        let position_styles = self.0.get_position();
246        taffy::Line {
247            start: convert::grid_line(&position_styles.grid_column_start),
248            end: convert::grid_line(&position_styles.grid_column_end),
249        }
250    }
251
252    #[inline]
253    fn align_self(&self) -> Option<taffy::AlignSelf> {
254        convert::item_alignment(self.0.get_position().align_self.0 .0)
255    }
256
257    #[inline]
258    fn justify_self(&self) -> Option<taffy::AlignSelf> {
259        convert::item_alignment(self.0.get_position().justify_self.0 .0)
260    }
261}