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