tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Definiciones

⎇ f60ac6d66a6ef25060b5a053b541ed3b7de8e3af taffy_stylo/src/borrowed.rs Rust · 168 líneas · 5.4 KB Historia Quién tocó qué crudo

Add borrowed version of taffy_stylo interop f60ac6d66 · Nico Burns ·

1use crate::{t2s, stylo};
2
3pub struct TaffyStyloStyleRef<'a>(pub &'a stylo::ComputedValues);
4
5impl<'a> From<&'a stylo::ComputedValues> for TaffyStyloStyleRef<'a> {
6    fn from(value: &'a stylo::ComputedValues) -> Self {
7        Self(value)
8    }
9}
10
11impl 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
112impl 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
148impl 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_grow.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}