Skip to main content

cranpose_ui/modifier/
fill.rs

1use super::{DimensionConstraint, Modifier, inspector_metadata};
2use crate::modifier_nodes::FillElement;
3
4thread_local! {
5    static FILL_WIDTH: Modifier = Modifier::with_element(FillElement::width(1.0));
6    static FILL_HEIGHT: Modifier = Modifier::with_element(FillElement::height(1.0));
7    static FILL_SIZE: Modifier = Modifier::with_element(FillElement::size(1.0));
8}
9
10fn fill_modifier(
11    fraction: f32,
12    full: &'static std::thread::LocalKey<Modifier>,
13    element: fn(f32) -> FillElement,
14) -> Modifier {
15    if fraction == 1.0 {
16        full.with(Clone::clone)
17    } else {
18        Modifier::with_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, &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, &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, &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}