ailloli_ui_widgets/layout/layout_ext.rs
1//! Shared size/flex builder traits, view finalization, and implementation macros.
2
3use ailloli_ui_core::geometry::{Constraints, Size};
4use ailloli_ui_core::style::{
5 resolve_widget_size, AlignItems, FlexItemStyle, FlexStyle, LayoutSizeHint, LayoutStyle, Length,
6};
7use ailloli_ui_runtime::component::{IntoView, View};
8
9/// Resolves declarative sizing and constraints around an intrinsic size.
10///
11/// All sizes are logical pixels. Resolution, min/max precedence, and non-finite
12/// handling are delegated to [`ailloli_ui_core::style::resolve_widget_size`].
13///
14/// # Examples
15///
16/// ```
17/// use ailloli_ui_core::{Constraints, Size};
18/// use ailloli_ui_core::style::LayoutStyle;
19/// use ailloli_ui_widgets::layout::layout_ext::apply_layout_size;
20/// let size = apply_layout_size(Size::new(10.0, 20.0), LayoutStyle::new().width(40.0), Constraints::loose(100.0, 100.0));
21/// assert_eq!(size, Size::new(40.0, 20.0));
22/// ```
23pub fn apply_layout_size(intrinsic: Size, layout: LayoutStyle, constraints: Constraints) -> Size {
24 resolve_widget_size(intrinsic, layout, constraints)
25}
26
27/// Copies declarative flex-item style onto a built [`View`].
28///
29/// Existing view flex metadata is replaced; size hints are preserved as their
30/// default through [`finish_view_sized`].
31///
32/// # Examples
33///
34/// ```
35/// use ailloli_ui_core::style::FlexItemStyle;
36/// use ailloli_ui_runtime::component::{IntoView, View};
37/// use ailloli_ui_widgets::{layout::finish_view, text::Text};
38/// let view: View<()> = Text::new("grow").into_view();
39/// let view = finish_view(view, FlexItemStyle::default().flex_grow(2.0));
40/// assert_eq!(view.flex_item.flex_grow, 2.0);
41/// ```
42pub fn finish_view<A>(view: View<A>, flex_item: FlexItemStyle) -> View<A> {
43 finish_view_sized(view, flex_item, LayoutSizeHint::default())
44}
45
46/// Copies flex-item style and declarative sizing hints onto a built [`View`].
47///
48/// # Examples
49///
50/// ```
51/// use ailloli_ui_core::style::{FlexItemStyle, LayoutSizeHint, Length};
52/// use ailloli_ui_runtime::component::{IntoView, View};
53/// use ailloli_ui_widgets::{layout::layout_ext::finish_view_sized, text::Text};
54/// let view: View<()> = Text::new("fill").into_view();
55/// let view = finish_view_sized(view, FlexItemStyle::default(), LayoutSizeHint { width: Length::Fill, height: Length::Auto });
56/// assert_eq!(view.size_hint.width, Length::Fill);
57/// ```
58pub fn finish_view_sized<A>(
59 view: View<A>,
60 flex_item: FlexItemStyle,
61 size_hint: LayoutSizeHint,
62) -> View<A> {
63 view.with_flex_item(flex_item).with_size_hint(size_hint)
64}
65
66/// Mutable access to a declarative widget's [`LayoutStyle`].
67///
68/// This low-level escape hatch supports wrapper composition; normal callers
69/// can prefer the consuming builder methods generated for widgets.
70///
71/// # Examples
72///
73/// ```
74/// use ailloli_ui_core::style::Length;
75/// use ailloli_ui_widgets::layout::{Container, LayoutExt};
76/// let mut container: Container<()> = Container::new();
77/// LayoutExt::layout_mut(&mut container).width = Length::px(80.0);
78/// assert_eq!(LayoutExt::layout_mut(&mut container).width, Length::px(80.0));
79/// ```
80pub trait LayoutExt: Sized {
81 /// Returns the widget's mutable declarative size/inset state.
82 ///
83 /// # Examples
84 ///
85 /// ```
86 /// use ailloli_ui_core::style::Length;
87 /// use ailloli_ui_widgets::layout::{Container, LayoutExt};
88 /// let mut container: Container<()> = Container::new();
89 /// container.layout_mut().height = Length::px(24.0);
90 /// assert_eq!(container.layout_mut().height, Length::px(24.0));
91 /// ```
92 fn layout_mut(&mut self) -> &mut LayoutStyle;
93}
94
95/// Flex-item builders returning a [`View`] for already-built views/components.
96///
97/// These methods replace one metadata field and preserve the retained subtree.
98/// Numeric grow/shrink values are stored according to [`FlexItemStyle`]'s
99/// normalization rules.
100///
101/// # Examples
102///
103/// ```
104/// use ailloli_ui_runtime::component::{IntoView, View};
105/// use ailloli_ui_widgets::{layout::FlexItemExt, text::Text};
106/// let base: View<()> = Text::new("grow").into_view();
107/// let grown: View<()> = base.flex_grow();
108/// assert_eq!(grown.flex_item.flex_grow, 1.0);
109/// ```
110pub trait FlexItemExt<A>: IntoView<A> + Sized {
111 /// Sets flex-grow weight to one.
112 ///
113 /// # Examples
114 ///
115 /// ```
116 /// use ailloli_ui_runtime::component::{IntoView, View};
117 /// use ailloli_ui_widgets::{layout::FlexItemExt, text::Text};
118 /// let view: View<()> = Text::new("grow").into_view().flex_grow();
119 /// assert_eq!(view.flex_item.flex_grow, 1.0);
120 /// ```
121 fn flex_grow(self) -> View<A> {
122 apply_flex_item(self, |item| item.flex_grow(1.0))
123 }
124
125 /// Sets the dimensionless flex-grow weight.
126 ///
127 /// # Examples
128 ///
129 /// ```
130 /// use ailloli_ui_runtime::component::{IntoView, View};
131 /// use ailloli_ui_widgets::{layout::FlexItemExt, text::Text};
132 /// let view: View<()> = Text::new("grow").into_view().flex_grow_by(3.0);
133 /// assert_eq!(view.flex_item.flex_grow, 3.0);
134 /// ```
135 fn flex_grow_by(self, value: f32) -> View<A> {
136 apply_flex_item(self, |item| item.flex_grow(value))
137 }
138
139 /// Sets the dimensionless flex-shrink weight.
140 ///
141 /// # Examples
142 ///
143 /// ```
144 /// use ailloli_ui_runtime::component::{IntoView, View};
145 /// use ailloli_ui_widgets::{layout::FlexItemExt, text::Text};
146 /// let view: View<()> = Text::new("shrink").into_view().flex_shrink(2.0);
147 /// assert_eq!(view.flex_item.flex_shrink, 2.0);
148 /// ```
149 fn flex_shrink(self, value: f32) -> View<A> {
150 apply_flex_item(self, |item| item.flex_shrink(value))
151 }
152
153 /// Sets the preferred main-axis flex basis.
154 ///
155 /// # Examples
156 ///
157 /// ```
158 /// use ailloli_ui_core::style::Length;
159 /// use ailloli_ui_runtime::component::{IntoView, View};
160 /// use ailloli_ui_widgets::{layout::FlexItemExt, text::Text};
161 /// let view: View<()> = Text::new("basis").into_view().flex_basis(40.0);
162 /// assert_eq!(view.flex_item.flex_basis, Length::px(40.0));
163 /// ```
164 fn flex_basis(self, value: impl Into<Length>) -> View<A> {
165 apply_flex_item(self, |item| item.flex_basis(value))
166 }
167
168 /// Overrides the parent cross-axis alignment for this view.
169 ///
170 /// # Examples
171 ///
172 /// ```
173 /// use ailloli_ui_core::style::AlignItems;
174 /// use ailloli_ui_runtime::component::{IntoView, View};
175 /// use ailloli_ui_widgets::{layout::FlexItemExt, text::Text};
176 /// let view: View<()> = Text::new("end").into_view().align_self(AlignItems::End);
177 /// assert_eq!(view.flex_item.align_self, Some(AlignItems::End));
178 /// ```
179 fn align_self(self, value: AlignItems) -> View<A> {
180 apply_flex_item(self, |item| item.align_self(value))
181 }
182
183 /// Marks width as parent-fill in the view size hint.
184 ///
185 /// # Examples
186 ///
187 /// ```
188 /// use ailloli_ui_core::style::Length;
189 /// use ailloli_ui_runtime::component::{IntoView, View};
190 /// use ailloli_ui_widgets::{layout::FlexItemExt, text::Text};
191 /// let view: View<()> = Text::new("wide").into_view().fill_width();
192 /// assert_eq!(view.size_hint.width, Length::Fill);
193 /// ```
194 fn fill_width(self) -> View<A> {
195 apply_size_hint(self, |hint| LayoutSizeHint {
196 width: Length::Fill,
197 ..hint
198 })
199 }
200
201 /// Marks height as parent-fill in the view size hint.
202 ///
203 /// # Examples
204 ///
205 /// ```
206 /// use ailloli_ui_core::style::Length;
207 /// use ailloli_ui_runtime::component::{IntoView, View};
208 /// use ailloli_ui_widgets::{layout::FlexItemExt, text::Text};
209 /// let view: View<()> = Text::new("tall").into_view().fill_height();
210 /// assert_eq!(view.size_hint.height, Length::Fill);
211 /// ```
212 fn fill_height(self) -> View<A> {
213 apply_size_hint(self, |hint| LayoutSizeHint {
214 height: Length::Fill,
215 ..hint
216 })
217 }
218
219 /// Marks both axes as parent-fill in the view size hint.
220 ///
221 /// # Examples
222 ///
223 /// ```
224 /// use ailloli_ui_core::style::Length;
225 /// use ailloli_ui_runtime::component::{IntoView, View};
226 /// use ailloli_ui_widgets::{layout::FlexItemExt, text::Text};
227 /// let view: View<()> = Text::new("fill").into_view().fill();
228 /// assert_eq!((view.size_hint.width, view.size_hint.height), (Length::Fill, Length::Fill));
229 /// ```
230 fn fill(self) -> View<A> {
231 apply_size_hint(self, |_| LayoutSizeHint {
232 width: Length::Fill,
233 height: Length::Fill,
234 })
235 }
236}
237
238impl<T, A> FlexItemExt<A> for T where T: IntoView<A> {}
239
240/// Converts a child and mutates exactly its flex-item metadata.
241fn apply_flex_item<A, F>(child: impl IntoView<A>, f: F) -> View<A>
242where
243 F: FnOnce(FlexItemStyle) -> FlexItemStyle,
244{
245 let mut view = child.into_view();
246 view.flex_item = f(view.flex_item);
247 view
248}
249
250/// Converts a child and mutates exactly its declarative size hint.
251fn apply_size_hint<A, F>(child: impl IntoView<A>, f: F) -> View<A>
252where
253 F: FnOnce(LayoutSizeHint) -> LayoutSizeHint,
254{
255 let mut view = child.into_view();
256 view.size_hint = f(view.size_hint);
257 view
258}
259
260#[macro_export]
261/// Implements [`LayoutExt`] and consuming size/flex-item
262/// builders for a generic widget with `layout` and `flex_item` fields.
263///
264/// The target type must have one type parameter and fields of types
265/// [`LayoutStyle`] and [`FlexItemStyle`], respectively.
266///
267/// # Examples
268///
269/// ```
270/// use std::marker::PhantomData;
271/// use ailloli_ui_core::style::{FlexItemStyle, LayoutStyle, Length};
272/// use ailloli_ui_widgets::impl_layout_builders;
273///
274/// struct Example<A> {
275/// layout: LayoutStyle,
276/// flex_item: FlexItemStyle,
277/// action: PhantomData<A>,
278/// }
279/// impl_layout_builders!(Example);
280/// let widget = Example::<()> {
281/// layout: LayoutStyle::default(),
282/// flex_item: FlexItemStyle::default(),
283/// action: PhantomData,
284/// }.width(80.0);
285/// assert_eq!(widget.layout.width, Length::Px(80.0));
286/// ```
287macro_rules! impl_layout_builders {
288 ($ty:ident) => {
289 impl<A: 'static> $crate::layout::layout_ext::LayoutExt for $ty<A> {
290 fn layout_mut(&mut self) -> &mut ailloli_ui_core::style::LayoutStyle {
291 &mut self.layout
292 }
293 }
294
295 impl<A: 'static> $ty<A> {
296 /// Sets preferred width from a logical-pixel or [length](ailloli_ui_core::style::Length) value.
297 ///
298 /// # Examples
299 ///
300 /// ```
301 /// use ailloli_ui_widgets::layout::Container;
302 /// let widget: Container<()> = Container::new().width(120.0);
303 /// let _ = widget;
304 /// ```
305 pub fn width(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
306 self.layout.width = value.into();
307 self
308 }
309
310 /// Sets preferred height from a logical-pixel or length value.
311 ///
312 /// # Examples
313 ///
314 /// ```
315 /// use ailloli_ui_widgets::layout::Container;
316 /// let widget: Container<()> = Container::new().height(40.0);
317 /// let _ = widget;
318 /// ```
319 pub fn height(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
320 self.layout.height = value.into();
321 self
322 }
323
324 /// Sets the minimum width bound.
325 ///
326 /// # Examples
327 ///
328 /// ```
329 /// use ailloli_ui_widgets::layout::Container;
330 /// let widget: Container<()> = Container::new().min_width(40.0);
331 /// let _ = widget;
332 /// ```
333 pub fn min_width(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
334 self.layout.min_width = value.into();
335 self
336 }
337
338 /// Sets the maximum width bound.
339 ///
340 /// # Examples
341 ///
342 /// ```
343 /// use ailloli_ui_widgets::layout::Container;
344 /// let widget: Container<()> = Container::new().max_width(320.0);
345 /// let _ = widget;
346 /// ```
347 pub fn max_width(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
348 self.layout.max_width = value.into();
349 self
350 }
351
352 /// Sets the minimum height bound.
353 ///
354 /// # Examples
355 ///
356 /// ```
357 /// use ailloli_ui_widgets::layout::Container;
358 /// let widget: Container<()> = Container::new().min_height(24.0);
359 /// let _ = widget;
360 /// ```
361 pub fn min_height(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
362 self.layout.min_height = value.into();
363 self
364 }
365
366 /// Sets the maximum height bound.
367 ///
368 /// # Examples
369 ///
370 /// ```
371 /// use ailloli_ui_widgets::layout::Container;
372 /// let widget: Container<()> = Container::new().max_height(240.0);
373 /// let _ = widget;
374 /// ```
375 pub fn max_height(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
376 self.layout.max_height = value.into();
377 self
378 }
379
380 /// Sets both preferred axes to parent fill.
381 ///
382 /// # Examples
383 ///
384 /// ```
385 /// use ailloli_ui_widgets::layout::Container;
386 /// let widget: Container<()> = Container::new().fill();
387 /// let _ = widget;
388 /// ```
389 pub fn fill(mut self) -> Self {
390 self.layout.width = ailloli_ui_core::style::Length::Fill;
391 self.layout.height = ailloli_ui_core::style::Length::Fill;
392 self
393 }
394
395 /// Sets preferred width to parent fill.
396 ///
397 /// # Examples
398 ///
399 /// ```
400 /// use ailloli_ui_widgets::layout::Container;
401 /// let widget: Container<()> = Container::new().fill_width();
402 /// let _ = widget;
403 /// ```
404 pub fn fill_width(mut self) -> Self {
405 self.layout.width = ailloli_ui_core::style::Length::Fill;
406 self
407 }
408
409 /// Sets preferred height to parent fill.
410 ///
411 /// # Examples
412 ///
413 /// ```
414 /// use ailloli_ui_widgets::layout::Container;
415 /// let widget: Container<()> = Container::new().fill_height();
416 /// let _ = widget;
417 /// ```
418 pub fn fill_height(mut self) -> Self {
419 self.layout.height = ailloli_ui_core::style::Length::Fill;
420 self
421 }
422
423 /// Sets uniform outer margin in logical pixels.
424 ///
425 /// # Examples
426 ///
427 /// ```
428 /// use ailloli_ui_widgets::layout::Container;
429 /// let widget: Container<()> = Container::new().margin(8.0);
430 /// let _ = widget;
431 /// ```
432 pub fn margin(mut self, value: f32) -> Self {
433 self.layout = self.layout.margin(value);
434 self
435 }
436
437 /// Sets uniform inner padding in logical pixels.
438 ///
439 /// # Examples
440 ///
441 /// ```
442 /// use ailloli_ui_widgets::layout::Container;
443 /// let widget: Container<()> = Container::new().padding(8.0);
444 /// let _ = widget;
445 /// ```
446 pub fn padding(mut self, value: f32) -> Self {
447 self.layout = self.layout.padding(value);
448 self
449 }
450
451 /// Sets flex-grow weight to one.
452 ///
453 /// # Examples
454 ///
455 /// ```
456 /// use ailloli_ui_widgets::layout::Container;
457 /// let widget: Container<()> = Container::new().flex_grow();
458 /// let _ = widget;
459 /// ```
460 pub fn flex_grow(mut self) -> Self {
461 self.flex_item = self.flex_item.flex_grow(1.0);
462 self
463 }
464
465 /// Sets a dimensionless flex-grow weight.
466 ///
467 /// # Examples
468 ///
469 /// ```
470 /// use ailloli_ui_widgets::layout::Container;
471 /// let widget: Container<()> = Container::new().flex_grow_by(2.0);
472 /// let _ = widget;
473 /// ```
474 pub fn flex_grow_by(mut self, value: f32) -> Self {
475 self.flex_item = self.flex_item.flex_grow(value);
476 self
477 }
478
479 /// Sets a dimensionless flex-shrink weight.
480 ///
481 /// # Examples
482 ///
483 /// ```
484 /// use ailloli_ui_widgets::layout::Container;
485 /// let widget: Container<()> = Container::new().flex_shrink(1.0);
486 /// let _ = widget;
487 /// ```
488 pub fn flex_shrink(mut self, value: f32) -> Self {
489 self.flex_item = self.flex_item.flex_shrink(value);
490 self
491 }
492
493 /// Sets the preferred main-axis flex basis.
494 ///
495 /// # Examples
496 ///
497 /// ```
498 /// use ailloli_ui_widgets::layout::Container;
499 /// let widget: Container<()> = Container::new().flex_basis(80.0);
500 /// let _ = widget;
501 /// ```
502 pub fn flex_basis(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
503 self.flex_item = self.flex_item.flex_basis(value);
504 self
505 }
506
507 /// Overrides cross-axis alignment for this flex item.
508 ///
509 /// # Examples
510 ///
511 /// ```
512 /// use ailloli_ui_core::style::AlignItems;
513 /// use ailloli_ui_widgets::layout::Container;
514 /// let widget: Container<()> = Container::new().align_self(AlignItems::Center);
515 /// let _ = widget;
516 /// ```
517 pub fn align_self(mut self, value: ailloli_ui_core::style::AlignItems) -> Self {
518 self.flex_item = self.flex_item.align_self(value);
519 self
520 }
521 }
522 };
523}
524
525#[macro_export]
526/// Implements [`LayoutExt`] and consuming size/flex-item
527/// builders for a non-generic widget with `layout` and `flex_item` fields.
528///
529/// # Examples
530///
531/// ```
532/// use ailloli_ui_core::style::{FlexItemStyle, LayoutStyle, Length};
533/// use ailloli_ui_widgets::impl_layout_builders_unit;
534///
535/// struct Example {
536/// layout: LayoutStyle,
537/// flex_item: FlexItemStyle,
538/// }
539/// impl_layout_builders_unit!(Example);
540/// let widget = Example {
541/// layout: LayoutStyle::default(),
542/// flex_item: FlexItemStyle::default(),
543/// }.height(32.0);
544/// assert_eq!(widget.layout.height, Length::Px(32.0));
545/// ```
546macro_rules! impl_layout_builders_unit {
547 ($ty:ty) => {
548 impl $crate::layout::layout_ext::LayoutExt for $ty {
549 fn layout_mut(&mut self) -> &mut ailloli_ui_core::style::LayoutStyle {
550 &mut self.layout
551 }
552 }
553
554 impl $ty {
555 /// Sets preferred width from a logical-pixel or length value.
556 ///
557 /// # Examples
558 ///
559 /// ```
560 /// use ailloli_ui_core::IconId;
561 /// use ailloli_ui_widgets::primitives::Icon;
562 /// let widget = Icon::new(IconId::Close).width(24.0);
563 /// let _ = widget;
564 /// ```
565 pub fn width(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
566 self.layout.width = value.into();
567 self
568 }
569
570 /// Sets preferred height from a logical-pixel or length value.
571 ///
572 /// # Examples
573 ///
574 /// ```
575 /// use ailloli_ui_core::IconId;
576 /// use ailloli_ui_widgets::primitives::Icon;
577 /// let widget = Icon::new(IconId::Close).height(24.0);
578 /// let _ = widget;
579 /// ```
580 pub fn height(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
581 self.layout.height = value.into();
582 self
583 }
584
585 /// Sets the minimum width bound.
586 ///
587 /// # Examples
588 ///
589 /// ```
590 /// use ailloli_ui_core::IconId;
591 /// use ailloli_ui_widgets::primitives::Icon;
592 /// let widget = Icon::new(IconId::Close).min_width(12.0);
593 /// let _ = widget;
594 /// ```
595 pub fn min_width(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
596 self.layout.min_width = value.into();
597 self
598 }
599
600 /// Sets the maximum width bound.
601 ///
602 /// # Examples
603 ///
604 /// ```
605 /// use ailloli_ui_core::IconId;
606 /// use ailloli_ui_widgets::primitives::Icon;
607 /// let widget = Icon::new(IconId::Close).max_width(32.0);
608 /// let _ = widget;
609 /// ```
610 pub fn max_width(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
611 self.layout.max_width = value.into();
612 self
613 }
614
615 /// Sets the minimum height bound.
616 ///
617 /// # Examples
618 ///
619 /// ```
620 /// use ailloli_ui_core::IconId;
621 /// use ailloli_ui_widgets::primitives::Icon;
622 /// let widget = Icon::new(IconId::Close).min_height(12.0);
623 /// let _ = widget;
624 /// ```
625 pub fn min_height(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
626 self.layout.min_height = value.into();
627 self
628 }
629
630 /// Sets the maximum height bound.
631 ///
632 /// # Examples
633 ///
634 /// ```
635 /// use ailloli_ui_core::IconId;
636 /// use ailloli_ui_widgets::primitives::Icon;
637 /// let widget = Icon::new(IconId::Close).max_height(32.0);
638 /// let _ = widget;
639 /// ```
640 pub fn max_height(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
641 self.layout.max_height = value.into();
642 self
643 }
644
645 /// Sets both preferred axes to parent fill.
646 ///
647 /// # Examples
648 ///
649 /// ```
650 /// use ailloli_ui_core::IconId;
651 /// use ailloli_ui_widgets::primitives::Icon;
652 /// let widget = Icon::new(IconId::Close).fill();
653 /// let _ = widget;
654 /// ```
655 pub fn fill(mut self) -> Self {
656 self.layout.width = ailloli_ui_core::style::Length::Fill;
657 self.layout.height = ailloli_ui_core::style::Length::Fill;
658 self
659 }
660
661 /// Sets preferred width to parent fill.
662 ///
663 /// # Examples
664 ///
665 /// ```
666 /// use ailloli_ui_core::IconId;
667 /// use ailloli_ui_widgets::primitives::Icon;
668 /// let widget = Icon::new(IconId::Close).fill_width();
669 /// let _ = widget;
670 /// ```
671 pub fn fill_width(mut self) -> Self {
672 self.layout.width = ailloli_ui_core::style::Length::Fill;
673 self
674 }
675
676 /// Sets preferred height to parent fill.
677 ///
678 /// # Examples
679 ///
680 /// ```
681 /// use ailloli_ui_core::IconId;
682 /// use ailloli_ui_widgets::primitives::Icon;
683 /// let widget = Icon::new(IconId::Close).fill_height();
684 /// let _ = widget;
685 /// ```
686 pub fn fill_height(mut self) -> Self {
687 self.layout.height = ailloli_ui_core::style::Length::Fill;
688 self
689 }
690
691 /// Sets uniform outer margin in logical pixels.
692 ///
693 /// # Examples
694 ///
695 /// ```
696 /// use ailloli_ui_core::IconId;
697 /// use ailloli_ui_widgets::primitives::Icon;
698 /// let widget = Icon::new(IconId::Close).margin(4.0);
699 /// let _ = widget;
700 /// ```
701 pub fn margin(mut self, value: f32) -> Self {
702 self.layout = self.layout.margin(value);
703 self
704 }
705
706 /// Sets uniform inner padding in logical pixels.
707 ///
708 /// # Examples
709 ///
710 /// ```
711 /// use ailloli_ui_core::IconId;
712 /// use ailloli_ui_widgets::primitives::Icon;
713 /// let widget = Icon::new(IconId::Close).padding(4.0);
714 /// let _ = widget;
715 /// ```
716 pub fn padding(mut self, value: f32) -> Self {
717 self.layout = self.layout.padding(value);
718 self
719 }
720
721 /// Sets flex-grow weight to one.
722 ///
723 /// # Examples
724 ///
725 /// ```
726 /// use ailloli_ui_core::IconId;
727 /// use ailloli_ui_widgets::primitives::Icon;
728 /// let widget = Icon::new(IconId::Close).flex_grow();
729 /// let _ = widget;
730 /// ```
731 pub fn flex_grow(mut self) -> Self {
732 self.flex_item = self.flex_item.flex_grow(1.0);
733 self
734 }
735
736 /// Sets a dimensionless flex-grow weight.
737 ///
738 /// # Examples
739 ///
740 /// ```
741 /// use ailloli_ui_core::IconId;
742 /// use ailloli_ui_widgets::primitives::Icon;
743 /// let widget = Icon::new(IconId::Close).flex_grow_by(2.0);
744 /// let _ = widget;
745 /// ```
746 pub fn flex_grow_by(mut self, value: f32) -> Self {
747 self.flex_item = self.flex_item.flex_grow(value);
748 self
749 }
750
751 /// Sets a dimensionless flex-shrink weight.
752 ///
753 /// # Examples
754 ///
755 /// ```
756 /// use ailloli_ui_core::IconId;
757 /// use ailloli_ui_widgets::primitives::Icon;
758 /// let widget = Icon::new(IconId::Close).flex_shrink(1.0);
759 /// let _ = widget;
760 /// ```
761 pub fn flex_shrink(mut self, value: f32) -> Self {
762 self.flex_item = self.flex_item.flex_shrink(value);
763 self
764 }
765
766 /// Sets the preferred main-axis flex basis.
767 ///
768 /// # Examples
769 ///
770 /// ```
771 /// use ailloli_ui_core::IconId;
772 /// use ailloli_ui_widgets::primitives::Icon;
773 /// let widget = Icon::new(IconId::Close).flex_basis(20.0);
774 /// let _ = widget;
775 /// ```
776 pub fn flex_basis(mut self, value: impl Into<ailloli_ui_core::style::Length>) -> Self {
777 self.flex_item = self.flex_item.flex_basis(value);
778 self
779 }
780
781 /// Overrides cross-axis alignment for this flex item.
782 ///
783 /// # Examples
784 ///
785 /// ```
786 /// use ailloli_ui_core::{style::AlignItems, IconId};
787 /// use ailloli_ui_widgets::primitives::Icon;
788 /// let widget = Icon::new(IconId::Close).align_self(AlignItems::Center);
789 /// let _ = widget;
790 /// ```
791 pub fn align_self(mut self, value: ailloli_ui_core::style::AlignItems) -> Self {
792 self.flex_item = self.flex_item.align_self(value);
793 self
794 }
795 }
796 };
797}
798
799/// Mutable access to row/column [`FlexStyle`].
800///
801/// # Examples
802///
803/// ```
804/// use ailloli_ui_widgets::layout::{FlexLayoutExt, Row};
805/// let mut row: Row<()> = Row::new();
806/// row.flex_mut().gap = 6.0;
807/// assert_eq!(row.flex_mut().gap, 6.0);
808/// ```
809pub trait FlexLayoutExt: Sized {
810 /// Returns mutable direction, gap, and cross-axis alignment state.
811 ///
812 /// # Examples
813 ///
814 /// ```
815 /// use ailloli_ui_core::style::AlignItems;
816 /// use ailloli_ui_widgets::layout::{Column, FlexLayoutExt};
817 /// let mut column: Column<()> = Column::new();
818 /// column.flex_mut().align_items = AlignItems::Center;
819 /// assert_eq!(column.flex_mut().align_items, AlignItems::Center);
820 /// ```
821 fn flex_mut(&mut self) -> &mut FlexStyle;
822}
823
824#[macro_export]
825/// Implements [`FlexLayoutExt`] and consuming
826/// container-flex builders for a generic widget with a `flex` field.
827///
828/// The invocation scope must import [`FlexStyle`] because the generated trait
829/// implementation names that type directly.
830///
831/// # Examples
832///
833/// ```
834/// use std::marker::PhantomData;
835/// use ailloli_ui_core::style::{AlignItems, FlexStyle};
836/// use ailloli_ui_widgets::impl_flex_builders;
837/// use ailloli_ui_widgets::layout::FlexLayoutExt;
838///
839/// struct Example<A> {
840/// flex: FlexStyle,
841/// action: PhantomData<A>,
842/// }
843/// impl_flex_builders!(Example);
844/// let widget = Example::<()> {
845/// flex: FlexStyle::default(),
846/// action: PhantomData,
847/// }.align_items(AlignItems::Center);
848/// assert_eq!(widget.flex.align_items, AlignItems::Center);
849/// ```
850macro_rules! impl_flex_builders {
851 ($ty:ident) => {
852 impl<A: 'static> $crate::layout::layout_ext::FlexLayoutExt for $ty<A> {
853 fn flex_mut(&mut self) -> &mut FlexStyle {
854 &mut self.flex
855 }
856 }
857
858 impl<A: 'static> $ty<A> {
859 /// Sets non-negative inter-child gap in logical pixels.
860 ///
861 /// Negative and `NaN` values resolve to zero through `f32::max`.
862 ///
863 /// # Examples
864 ///
865 /// ```
866 /// use ailloli_ui_widgets::layout::Row;
867 /// let row: Row<()> = Row::new().gap(8.0);
868 /// let _ = row;
869 /// ```
870 pub fn gap(mut self, value: f32) -> Self {
871 self.flex_mut().gap = value.max(0.0);
872 self
873 }
874
875 /// Sets the default child alignment on the cross axis.
876 ///
877 /// # Examples
878 ///
879 /// ```
880 /// use ailloli_ui_core::style::AlignItems;
881 /// use ailloli_ui_widgets::layout::Row;
882 /// let row: Row<()> = Row::new().align_items(AlignItems::Center);
883 /// let _ = row;
884 /// ```
885 pub fn align_items(mut self, value: ailloli_ui_core::style::AlignItems) -> Self {
886 self.flex_mut().align_items = value;
887 self
888 }
889 }
890 };
891}