tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1077HTML 1008Markdown 12TOML 83209 Definitions

⎇ 859cc921b3adb8e3b184e6fc0cf0c40b6c5a625b src/style_helpers.rs Rust · 413 lines · 15.0 KB History Who touched what raw

Grid/refactor coordinate handling (#301) 859cc921b · Nico Burns ·

1//! Helper functions which it make it easier to create instances of types in the `style` and `geometry` modules.
2use crate::{
3    geometry::{MinMax, Point, Rect, Size},
4    style::LengthPercentage,
5};
6
7#[cfg(feature = "grid")]
8use crate::style::{GridTrackRepetition, NonRepeatedTrackSizingFunction, TrackSizingFunction};
9
10/// Returns an auto-repeated track definition
11#[cfg(feature = "grid")]
12pub fn repeat(
13    repetition_kind: GridTrackRepetition,
14    track_list: Vec<NonRepeatedTrackSizingFunction>,
15) -> TrackSizingFunction {
16    TrackSizingFunction::AutoRepeat(repetition_kind, track_list)
17}
18
19/// Specifies a grid line to place a grid item between in CSS Grid Line coordinates:
20///  - Positive indicies count upwards from the start (top or left) of the explicit grid
21///  - Negative indicies count downwards from the end (bottom or right) of the explicit grid
22///  - ZERO IS INVALID index, and will be treated as a GridPlacement::Auto.
23pub fn line<T: TaffyGridLine>(index: i16) -> T {
24    T::from_line_index(index)
25}
26/// Trait to abstract over grid line values
27pub trait TaffyGridLine {
28    /// Converts an i16 into Self
29    fn from_line_index(index: i16) -> Self;
30}
31
32/// Returns a GridPlacement::Span
33pub fn span<T: TaffyGridSpan>(span: u16) -> T {
34    T::from_span(span)
35}
36/// Trait to abstract over grid span values
37pub trait TaffyGridSpan {
38    /// Converts an iu6 into Self
39    fn from_span(span: u16) -> Self;
40}
41
42/// Returns a MinMax with min value of min and max value of max
43pub fn minmax<Min, Max, Output: From<MinMax<Min, Max>>>(min: Min, max: Max) -> Output {
44    MinMax { min, max }.into()
45}
46
47/// Returns the zero value for that type
48pub const fn zero<T: TaffyZero>() -> T {
49    T::ZERO
50}
51
52/// Trait to abstract over zero values
53pub trait TaffyZero {
54    /// The zero value for type implementing TaffyZero
55    const ZERO: Self;
56}
57impl TaffyZero for f32 {
58    const ZERO: f32 = 0.0;
59}
60impl<T: TaffyZero> TaffyZero for Option<T> {
61    const ZERO: Option<T> = Some(T::ZERO);
62}
63impl<T: TaffyZero> TaffyZero for Point<T> {
64    const ZERO: Point<T> = Point { x: T::ZERO, y: T::ZERO };
65}
66impl<T: TaffyZero> Point<T> {
67    /// Returns a Point where both the x and y values are the zero value of the contained type
68    /// (e.g. 0.0, Some(0.0), or Dimension::Points(0.0))
69    pub const fn zero() -> Self {
70        zero::<Self>()
71    }
72}
73impl<T: TaffyZero> TaffyZero for Size<T> {
74    const ZERO: Size<T> = Size { width: T::ZERO, height: T::ZERO };
75}
76impl<T: TaffyZero> Size<T> {
77    /// Returns a Size where both the width and height values are the zero value of the contained type
78    /// (e.g. 0.0, Some(0.0), or Dimension::Points(0.0))
79    pub const fn zero() -> Self {
80        zero::<Self>()
81    }
82}
83impl<T: TaffyZero> TaffyZero for Rect<T> {
84    const ZERO: Rect<T> = Rect { left: T::ZERO, right: T::ZERO, top: T::ZERO, bottom: T::ZERO };
85}
86impl<T: TaffyZero> Rect<T> {
87    /// Returns a Size where the left, right, top, and bottom values are all the zero value of the contained type
88    /// (e.g. 0.0, Some(0.0), or Dimension::Points(0.0))
89    pub const fn zero() -> Self {
90        zero::<Self>()
91    }
92}
93
94/// Returns the auto value for that type
95pub const fn auto<T: TaffyAuto>() -> T {
96    T::AUTO
97}
98
99/// Trait to abstract over auto values
100pub trait TaffyAuto {
101    /// The auto value for type implementing TaffyZero
102    const AUTO: Self;
103}
104impl<T: TaffyAuto> TaffyAuto for Option<T> {
105    const AUTO: Option<T> = Some(T::AUTO);
106}
107impl<T: TaffyAuto> TaffyAuto for Point<T> {
108    const AUTO: Point<T> = Point { x: T::AUTO, y: T::AUTO };
109}
110impl<T: TaffyAuto> Point<T> {
111    /// Returns a Point where both the x and y values are the auto value of the contained type
112    /// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
113    pub const fn auto() -> Self {
114        auto::<Self>()
115    }
116}
117impl<T: TaffyAuto> TaffyAuto for Size<T> {
118    const AUTO: Size<T> = Size { width: T::AUTO, height: T::AUTO };
119}
120impl<T: TaffyAuto> Size<T> {
121    /// Returns a Size where both the width and height values are the auto value of the contained type
122    /// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
123    pub const fn auto() -> Self {
124        auto::<Self>()
125    }
126}
127impl<T: TaffyAuto> TaffyAuto for Rect<T> {
128    const AUTO: Rect<T> = Rect { left: T::AUTO, right: T::AUTO, top: T::AUTO, bottom: T::AUTO };
129}
130impl<T: TaffyAuto> Rect<T> {
131    /// Returns a Size where the left, right, top, and bottom values are all the auto value of the contained type
132    /// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
133    pub const fn auto() -> Self {
134        auto::<Self>()
135    }
136}
137
138/// Returns the auto value for that type
139pub const fn min_content<T: TaffyMinContent>() -> T {
140    T::MIN_CONTENT
141}
142
143/// Trait to abstract over min_content values
144pub trait TaffyMinContent {
145    /// The min_content value for type implementing TaffyZero
146    const MIN_CONTENT: Self;
147}
148impl<T: TaffyMinContent> TaffyMinContent for Option<T> {
149    const MIN_CONTENT: Option<T> = Some(T::MIN_CONTENT);
150}
151impl<T: TaffyMinContent> TaffyMinContent for Point<T> {
152    const MIN_CONTENT: Point<T> = Point { x: T::MIN_CONTENT, y: T::MIN_CONTENT };
153}
154impl<T: TaffyMinContent> Point<T> {
155    /// Returns a Point where both the x and y values are the min_content value of the contained type
156    /// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
157    pub const fn min_content() -> Self {
158        min_content::<Self>()
159    }
160}
161impl<T: TaffyMinContent> TaffyMinContent for Size<T> {
162    const MIN_CONTENT: Size<T> = Size { width: T::MIN_CONTENT, height: T::MIN_CONTENT };
163}
164impl<T: TaffyMinContent> Size<T> {
165    /// Returns a Size where both the width and height values are the min_content value of the contained type
166    /// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
167    pub const fn min_content() -> Self {
168        min_content::<Self>()
169    }
170}
171impl<T: TaffyMinContent> TaffyMinContent for Rect<T> {
172    const MIN_CONTENT: Rect<T> =
173        Rect { left: T::MIN_CONTENT, right: T::MIN_CONTENT, top: T::MIN_CONTENT, bottom: T::MIN_CONTENT };
174}
175impl<T: TaffyMinContent> Rect<T> {
176    /// Returns a Size where the left, right, top, and bottom values are all the min_content value of the contained type
177    /// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
178    pub const fn min_content() -> Self {
179        min_content::<Self>()
180    }
181}
182
183/// Returns the auto value for that type
184pub const fn max_content<T: TaffyMaxContent>() -> T {
185    T::MAX_CONTENT
186}
187
188/// Trait to abstract over max_content values
189pub trait TaffyMaxContent {
190    /// The max_content value for type implementing TaffyZero
191    const MAX_CONTENT: Self;
192}
193impl<T: TaffyMaxContent> TaffyMaxContent for Option<T> {
194    const MAX_CONTENT: Option<T> = Some(T::MAX_CONTENT);
195}
196impl<T: TaffyMaxContent> TaffyMaxContent for Point<T> {
197    const MAX_CONTENT: Point<T> = Point { x: T::MAX_CONTENT, y: T::MAX_CONTENT };
198}
199impl<T: TaffyMaxContent> Point<T> {
200    /// Returns a Point where both the x and y values are the max_content value of the contained type
201    /// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
202    pub const fn max_content() -> Self {
203        max_content::<Self>()
204    }
205}
206impl<T: TaffyMaxContent> TaffyMaxContent for Size<T> {
207    const MAX_CONTENT: Size<T> = Size { width: T::MAX_CONTENT, height: T::MAX_CONTENT };
208}
209impl<T: TaffyMaxContent> Size<T> {
210    /// Returns a Size where both the width and height values are the max_content value of the contained type
211    /// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
212    pub const fn max_content() -> Self {
213        max_content::<Self>()
214    }
215}
216impl<T: TaffyMaxContent> TaffyMaxContent for Rect<T> {
217    const MAX_CONTENT: Rect<T> =
218        Rect { left: T::MAX_CONTENT, right: T::MAX_CONTENT, top: T::MAX_CONTENT, bottom: T::MAX_CONTENT };
219}
220impl<T: TaffyMaxContent> Rect<T> {
221    /// Returns a Size where the left, right, top, and bottom values are all the max_content value of the contained type
222    /// (e.g. Dimension::Auto or LengthPercentageAuto::Auto)
223    pub const fn max_content() -> Self {
224        max_content::<Self>()
225    }
226}
227
228/// Returns a value of the inferred type which represent a constant of points
229pub fn fit_content<T: TaffyFitContent>(argument: LengthPercentage) -> T {
230    T::fit_content(argument)
231}
232
233/// Trait to create constant points values from plain numbers
234pub trait TaffyFitContent {
235    /// Converts into a LengthPercentage into Self
236    fn fit_content(argument: LengthPercentage) -> Self;
237}
238impl<T: TaffyFitContent> TaffyFitContent for Point<T> {
239    fn fit_content(argument: LengthPercentage) -> Self {
240        Point { x: T::fit_content(argument), y: T::fit_content(argument) }
241    }
242}
243impl<T: TaffyFitContent> Point<T> {
244    /// Returns a Point where both the x and y values are the constant points value of the contained type
245    /// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
246    pub fn fit_content(argument: LengthPercentage) -> Self {
247        fit_content(argument)
248    }
249}
250impl<T: TaffyFitContent> TaffyFitContent for Size<T> {
251    fn fit_content(argument: LengthPercentage) -> Self {
252        Size { width: T::fit_content(argument), height: T::fit_content(argument) }
253    }
254}
255impl<T: TaffyFitContent> Size<T> {
256    /// Returns a Size where both the width and height values are the constant fit_content value of the contained type
257    /// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
258    pub fn fit_content(argument: LengthPercentage) -> Self {
259        fit_content(argument)
260    }
261}
262impl<T: TaffyFitContent> TaffyFitContent for Rect<T> {
263    fn fit_content(argument: LengthPercentage) -> Self {
264        Rect {
265            left: T::fit_content(argument),
266            right: T::fit_content(argument),
267            top: T::fit_content(argument),
268            bottom: T::fit_content(argument),
269        }
270    }
271}
272impl<T: TaffyFitContent> Rect<T> {
273    /// Returns a Rect where the left, right, top and bottom values are all constant fit_content value of the contained type
274    /// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
275    pub fn fit_content(argument: LengthPercentage) -> Self {
276        fit_content(argument)
277    }
278}
279
280/// Returns a value of the inferred type which represent a constant of points
281pub fn points<Input: Into<f32> + Copy, T: FromPoints>(points: Input) -> T {
282    T::from_points(points)
283}
284
285/// Trait to create constant points values from plain numbers
286pub trait FromPoints {
287    /// Converts into an Into<f32> into Self
288    fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self;
289}
290impl FromPoints for f32 {
291    fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
292        points.into()
293    }
294}
295impl FromPoints for Option<f32> {
296    fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
297        Some(points.into())
298    }
299}
300impl<T: FromPoints> FromPoints for Point<T> {
301    fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
302        Point { x: T::from_points(points.into()), y: T::from_points(points.into()) }
303    }
304}
305impl<T: FromPoints> Point<T> {
306    /// Returns a Point where both the x and y values are the constant points value of the contained type
307    /// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
308    pub fn points<Input: Into<f32> + Copy>(points_value: Input) -> Self {
309        points::<Input, Self>(points_value)
310    }
311}
312impl<T: FromPoints> FromPoints for Size<T> {
313    fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
314        Size { width: T::from_points(points.into()), height: T::from_points(points.into()) }
315    }
316}
317impl<T: FromPoints> Size<T> {
318    /// Returns a Size where both the width and height values are the constant points value of the contained type
319    /// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
320    pub fn points<Input: Into<f32> + Copy>(points_value: Input) -> Self {
321        points::<Input, Self>(points_value)
322    }
323}
324impl<T: FromPoints> FromPoints for Rect<T> {
325    fn from_points<Input: Into<f32> + Copy>(points: Input) -> Self {
326        Rect {
327            left: T::from_points(points.into()),
328            right: T::from_points(points.into()),
329            top: T::from_points(points.into()),
330            bottom: T::from_points(points.into()),
331        }
332    }
333}
334impl<T: FromPoints> Rect<T> {
335    /// Returns a Rect where the left, right, top and bottom values are all constant points value of the contained type
336    /// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
337    pub fn points<Input: Into<f32> + Copy>(points_value: Input) -> Self {
338        points::<Input, Self>(points_value)
339    }
340}
341
342/// Returns a value of the inferred type which represent a constant of points
343pub fn percent<Input: Into<f32> + Copy, T: FromPercent>(percent: Input) -> T {
344    T::from_percent(percent)
345}
346
347/// Trait to create constant percent values from plain numbers
348pub trait FromPercent {
349    /// Converts into an Into<f32> into Self
350    fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self;
351}
352impl FromPercent for f32 {
353    fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
354        percent.into()
355    }
356}
357impl FromPercent for Option<f32> {
358    fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
359        Some(percent.into())
360    }
361}
362impl<T: FromPercent> FromPercent for Point<T> {
363    fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
364        Point { x: T::from_percent(percent.into()), y: T::from_percent(percent.into()) }
365    }
366}
367impl<T: FromPercent> Point<T> {
368    /// Returns a Point where both the x and y values are the constant percent value of the contained type
369    /// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
370    pub fn percent<Input: Into<f32> + Copy>(percent_value: Input) -> Self {
371        percent::<Input, Self>(percent_value)
372    }
373}
374impl<T: FromPercent> FromPercent for Size<T> {
375    fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
376        Size { width: T::from_percent(percent.into()), height: T::from_percent(percent.into()) }
377    }
378}
379impl<T: FromPercent> Size<T> {
380    /// Returns a Size where both the width and height values are the constant percent value of the contained type
381    /// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
382    pub fn percent<Input: Into<f32> + Copy>(percent_value: Input) -> Self {
383        percent::<Input, Self>(percent_value)
384    }
385}
386impl<T: FromPercent> FromPercent for Rect<T> {
387    fn from_percent<Input: Into<f32> + Copy>(percent: Input) -> Self {
388        Rect {
389            left: T::from_percent(percent.into()),
390            right: T::from_percent(percent.into()),
391            top: T::from_percent(percent.into()),
392            bottom: T::from_percent(percent.into()),
393        }
394    }
395}
396impl<T: FromPercent> Rect<T> {
397    /// Returns a Rect where the left, right, top and bottom values are all constant percent value of the contained type
398    /// (e.g. 2.1, Some(2.1), or Dimension::Points(2.1))
399    pub fn percent<Input: Into<f32> + Copy>(percent_value: Input) -> Self {
400        percent::<Input, Self>(percent_value)
401    }
402}
403
404/// Returns a value of the inferred type which represents a flex fraction
405pub fn flex<Input: Into<f32> + Copy, T: FromFlex>(flex: Input) -> T {
406    T::from_flex(flex)
407}
408
409/// Trait to create constant percent values from plain numbers
410pub trait FromFlex {
411    /// Converts into an Into<f32> into Self
412    fn from_flex<Input: Into<f32> + Copy>(flex: Input) -> Self;
413}