Skip to main content

cranpose_ui/modifier/
fill.rs

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