Skip to main content

cranpose_ui/modifier/
fill.rs

1use cranpose_foundation::plain_element;
2
3use super::{DimensionConstraint, Modifier, inspector_metadata};
4use crate::modifier_nodes::FillElement;
5
6fn fill_modifier(
7    fraction: f32,
8    full: fn(&crate::thread_state::UiThreadState) -> &std::cell::OnceCell<Modifier>,
9    element: fn(f32) -> FillElement,
10) -> Modifier {
11    if fraction == 1.0 {
12        crate::thread_state::with(|state| {
13            full(state)
14                .get_or_init(|| Modifier::from_dyn_element(plain_element(element(1.0))))
15                .clone()
16        })
17    } else {
18        Modifier::from_dyn_element(plain_element(element(fraction)))
19    }
20}
21
22impl Modifier {
23    /// Have the content fill the maximum available width.
24    ///
25    /// The `fraction` parameter allows filling only a portion of the available width (0.0 to 1.0).
26    ///
27    /// Matches Kotlin: `Modifier.fillMaxWidth(fraction: Float)`
28    ///
29    /// Example: `Modifier::empty().fill_max_width()`
30    pub fn fill_max_width(self) -> Self {
31        self.fill_max_width_fraction(1.0)
32    }
33
34    /// Fill a fraction of the maximum available width.
35    ///
36    /// Example: `Modifier::empty().fill_max_width_fraction(0.5)`
37    pub fn fill_max_width_fraction(self, fraction: f32) -> Self {
38        let clamped = fraction.clamp(0.0, 1.0);
39        let modifier = fill_modifier(clamped, |state| &state.fill_width, FillElement::width)
40            .with_inspector_metadata(inspector_metadata("fillMaxWidth", move |info| {
41                info.add_dimension("width", DimensionConstraint::Fraction(clamped));
42            }));
43        self.then(modifier)
44    }
45
46    /// Have the content fill the maximum available height.
47    ///
48    /// The `fraction` parameter allows filling only a portion of the available height (0.0 to 1.0).
49    ///
50    /// Matches Kotlin: `Modifier.fillMaxHeight(fraction: Float)`
51    ///
52    /// Example: `Modifier::empty().fill_max_height()`
53    pub fn fill_max_height(self) -> Self {
54        self.fill_max_height_fraction(1.0)
55    }
56
57    /// Fill a fraction of the maximum available height.
58    ///
59    /// Example: `Modifier::empty().fill_max_height_fraction(0.5)`
60    pub fn fill_max_height_fraction(self, fraction: f32) -> Self {
61        let clamped = fraction.clamp(0.0, 1.0);
62        let modifier = fill_modifier(clamped, |state| &state.fill_height, FillElement::height)
63            .with_inspector_metadata(inspector_metadata("fillMaxHeight", move |info| {
64                info.add_dimension("height", DimensionConstraint::Fraction(clamped));
65            }));
66        self.then(modifier)
67    }
68
69    /// Have the content fill the maximum available size (both width and height).
70    ///
71    /// The `fraction` parameter allows filling only a portion of the available size (0.0 to 1.0).
72    ///
73    /// Matches Kotlin: `Modifier.fillMaxSize(fraction: Float)`
74    ///
75    /// Example: `Modifier::empty().fill_max_size()`
76    pub fn fill_max_size(self) -> Self {
77        self.fill_max_size_fraction(1.0)
78    }
79
80    /// Fill a fraction of the maximum available size.
81    ///
82    /// Example: `Modifier::empty().fill_max_size_fraction(0.8)`
83    pub fn fill_max_size_fraction(self, fraction: f32) -> Self {
84        let clamped = fraction.clamp(0.0, 1.0);
85        let modifier = fill_modifier(clamped, |state| &state.fill_size, FillElement::size)
86            .with_inspector_metadata(inspector_metadata("fillMaxSize", move |info| {
87                info.add_dimension("width", DimensionConstraint::Fraction(clamped));
88                info.add_dimension("height", DimensionConstraint::Fraction(clamped));
89            }));
90        self.then(modifier)
91    }
92}