cranpose_ui/modifier/fill.rs
1//! Fill modifier implementation following Jetpack Compose's layout/Size.kt (fillMax* modifiers)
2//!
3//! Reference: /media/huge/composerepo/compose/foundation/foundation-layout/src/commonMain/kotlin/androidx/compose/foundation/layout/Size.kt
4
5use super::{inspector_metadata, DimensionConstraint, Modifier};
6use crate::modifier_nodes::FillElement;
7
8impl Modifier {
9 /// Have the content fill the maximum available width.
10 ///
11 /// The [fraction] parameter allows filling only a portion of the available width (0.0 to 1.0).
12 ///
13 /// Matches Kotlin: `Modifier.fillMaxWidth(fraction: Float)`
14 ///
15 /// Example: `Modifier::empty().fill_max_width()`
16 pub fn fill_max_width(self) -> Self {
17 self.fill_max_width_fraction(1.0)
18 }
19
20 /// Fill a fraction of the maximum available width.
21 ///
22 /// Example: `Modifier::empty().fill_max_width_fraction(0.5)`
23 pub fn fill_max_width_fraction(self, fraction: f32) -> Self {
24 let clamped = fraction.clamp(0.0, 1.0);
25 let modifier = Self::with_element(FillElement::width(clamped)).with_inspector_metadata(
26 inspector_metadata("fillMaxWidth", move |info| {
27 info.add_dimension("width", DimensionConstraint::Fraction(clamped));
28 }),
29 );
30 self.then(modifier)
31 }
32
33 /// Have the content fill the maximum available height.
34 ///
35 /// The [fraction] parameter allows filling only a portion of the available height (0.0 to 1.0).
36 ///
37 /// Matches Kotlin: `Modifier.fillMaxHeight(fraction: Float)`
38 ///
39 /// Example: `Modifier::empty().fill_max_height()`
40 pub fn fill_max_height(self) -> Self {
41 self.fill_max_height_fraction(1.0)
42 }
43
44 /// Fill a fraction of the maximum available height.
45 ///
46 /// Example: `Modifier::empty().fill_max_height_fraction(0.5)`
47 pub fn fill_max_height_fraction(self, fraction: f32) -> Self {
48 let clamped = fraction.clamp(0.0, 1.0);
49 let modifier = Self::with_element(FillElement::height(clamped)).with_inspector_metadata(
50 inspector_metadata("fillMaxHeight", move |info| {
51 info.add_dimension("height", DimensionConstraint::Fraction(clamped));
52 }),
53 );
54 self.then(modifier)
55 }
56
57 /// Have the content fill the maximum available size (both width and height).
58 ///
59 /// The [fraction] parameter allows filling only a portion of the available size (0.0 to 1.0).
60 ///
61 /// Matches Kotlin: `Modifier.fillMaxSize(fraction: Float)`
62 ///
63 /// Example: `Modifier::empty().fill_max_size()`
64 pub fn fill_max_size(self) -> Self {
65 self.fill_max_size_fraction(1.0)
66 }
67
68 /// Fill a fraction of the maximum available size.
69 ///
70 /// Example: `Modifier::empty().fill_max_size_fraction(0.8)`
71 pub fn fill_max_size_fraction(self, fraction: f32) -> Self {
72 let clamped = fraction.clamp(0.0, 1.0);
73 let modifier = Self::with_element(FillElement::size(clamped)).with_inspector_metadata(
74 inspector_metadata("fillMaxSize", move |info| {
75 info.add_dimension("width", DimensionConstraint::Fraction(clamped));
76 info.add_dimension("height", DimensionConstraint::Fraction(clamped));
77 }),
78 );
79 self.then(modifier)
80 }
81}