Skip to main content

cranpose_ui/modifier/
size.rs

1use cranpose_foundation::plain_element;
2use cranpose_ui_layout::IntrinsicSize;
3
4use super::{DimensionConstraint, Modifier, Size, inspector_metadata};
5use crate::modifier_nodes::{IntrinsicSizeElement, SizeElement};
6
7impl Modifier {
8    /// Declare the preferred size of the content to be exactly `size`.
9    ///
10    /// The incoming measurement constraints may override this value, forcing the content
11    /// to be either smaller or larger.
12    ///
13    /// Matches Kotlin: `Modifier.size(size: Dp)`
14    ///
15    /// Example: `Modifier::empty().size(Size { width: 100.0, height: 200.0 })`
16    pub fn size(self, size: Size) -> Self {
17        let width = size.width;
18        let height = size.height;
19        let modifier =
20            Self::from_dyn_element(plain_element(SizeElement::new(Some(width), Some(height))))
21                .with_inspector_metadata(inspector_metadata("size", move |info| {
22                    info.add_dimension("width", DimensionConstraint::Points(width));
23                    info.add_dimension("height", DimensionConstraint::Points(height));
24                }));
25        self.then(modifier)
26    }
27
28    /// Declare the preferred size of the content to be exactly `width`dp by `height`dp.
29    ///
30    /// Convenience method for `size(Size { width, height })`.
31    ///
32    /// Example: `Modifier::empty().size_points(100.0, 200.0)`
33    pub fn size_points(self, width: f32, height: f32) -> Self {
34        self.size(Size { width, height })
35    }
36
37    /// Declare the preferred width of the content to be exactly `width`dp.
38    ///
39    /// The incoming measurement constraints may override this value, forcing the content
40    /// to be either smaller or larger.
41    ///
42    /// Matches Kotlin: `Modifier.width(width: Dp)`
43    ///
44    /// Example: `Modifier::empty().width(100.0).height(200.0)`
45    pub fn width(self, width: f32) -> Self {
46        let modifier = Self::from_dyn_element(plain_element(SizeElement::new(Some(width), None)))
47            .with_inspector_metadata(inspector_metadata("width", move |info| {
48                info.add_dimension("width", DimensionConstraint::Points(width));
49            }));
50        self.then(modifier)
51    }
52
53    /// Declare the preferred height of the content to be exactly `height`dp.
54    ///
55    /// The incoming measurement constraints may override this value, forcing the content
56    /// to be either smaller or larger.
57    ///
58    /// Matches Kotlin: `Modifier.height(height: Dp)`
59    ///
60    /// Example: `Modifier::empty().width(100.0).height(200.0)`
61    pub fn height(self, height: f32) -> Self {
62        let modifier = Self::from_dyn_element(plain_element(SizeElement::new(None, Some(height))))
63            .with_inspector_metadata(inspector_metadata("height", move |info| {
64                info.add_dimension("height", DimensionConstraint::Points(height));
65            }));
66        self.then(modifier)
67    }
68
69    /// Declare the width of the content based on its intrinsic size.
70    ///
71    /// Matches Kotlin: `Modifier.width(IntrinsicSize)`
72    pub fn width_intrinsic(self, intrinsic: IntrinsicSize) -> Self {
73        let modifier =
74            Self::from_dyn_element(plain_element(IntrinsicSizeElement::width(intrinsic)))
75                .with_inspector_metadata(inspector_metadata("widthIntrinsic", move |info| {
76                    info.add_dimension("width", DimensionConstraint::Intrinsic(intrinsic));
77                }));
78        self.then(modifier)
79    }
80
81    /// Declare the height of the content based on its intrinsic size.
82    ///
83    /// Matches Kotlin: `Modifier.height(IntrinsicSize)`
84    pub fn height_intrinsic(self, intrinsic: IntrinsicSize) -> Self {
85        let modifier =
86            Self::from_dyn_element(plain_element(IntrinsicSizeElement::height(intrinsic)))
87                .with_inspector_metadata(inspector_metadata("heightIntrinsic", move |info| {
88                    info.add_dimension("height", DimensionConstraint::Intrinsic(intrinsic));
89                }));
90        self.then(modifier)
91    }
92
93    /// Declare the size of the content to be exactly `size`, ignoring incoming constraints.
94    ///
95    /// The incoming measurement constraints will not override this value. If the content
96    /// chooses a size that does not satisfy the incoming constraints, the parent layout
97    /// will be reported a size coerced in the constraints.
98    ///
99    /// Matches Kotlin: `Modifier.requiredSize(size: Dp)`
100    pub fn required_size(self, size: Size) -> Self {
101        let modifier = Self::from_dyn_element(plain_element(SizeElement::with_constraints(
102            Some(size.width),
103            Some(size.width),
104            Some(size.height),
105            Some(size.height),
106            false,
107        )));
108        self.then(modifier)
109    }
110
111    /// Declare the width of the content to be exactly `width`, ignoring
112    /// incoming constraints; the height follows them.
113    ///
114    /// Matches Kotlin: `Modifier.requiredWidth(width: Dp)`
115    pub fn required_width(self, width: f32) -> Self {
116        let modifier = Self::from_dyn_element(plain_element(SizeElement::with_constraints(
117            Some(width),
118            Some(width),
119            None,
120            None,
121            false,
122        )));
123        self.then(modifier)
124    }
125
126    /// Declare the height of the content to be exactly `height`, ignoring
127    /// incoming constraints; the width follows them.
128    ///
129    /// Matches Kotlin: `Modifier.requiredHeight(height: Dp)`
130    pub fn required_height(self, height: f32) -> Self {
131        let modifier = Self::from_dyn_element(plain_element(SizeElement::with_constraints(
132            None,
133            None,
134            Some(height),
135            Some(height),
136            false,
137        )));
138        self.then(modifier)
139    }
140
141    /// Keep the width of the content between `min` and `max`, as far as the
142    /// incoming constraints allow. `f32::INFINITY` leaves the upper side open.
143    ///
144    /// Matches Kotlin: `Modifier.widthIn(min: Dp, max: Dp)`
145    ///
146    /// Example: `Modifier::empty().width_in(48.0, f32::INFINITY)`
147    pub fn width_in(self, min: f32, max: f32) -> Self {
148        let modifier = Self::from_dyn_element(plain_element(SizeElement::with_constraints(
149            bound(min),
150            bound(max),
151            None,
152            None,
153            true,
154        )))
155        .with_inspector_metadata(inspector_metadata("widthIn", move |info| {
156            info.add_dimension("minWidth", DimensionConstraint::Points(min));
157        }));
158        self.then(modifier)
159    }
160
161    /// Keep the height of the content between `min` and `max`, as far as the
162    /// incoming constraints allow. `f32::INFINITY` leaves the upper side open.
163    /// A text field or a chip that a finger has to find gets its 24 points
164    /// this way without growing to the 48 of
165    /// [`minimum_interactive_component_size`](Self::minimum_interactive_component_size).
166    ///
167    /// Matches Kotlin: `Modifier.heightIn(min: Dp, max: Dp)`
168    ///
169    /// Example: `Modifier::empty().height_in(24.0, f32::INFINITY)`
170    pub fn height_in(self, min: f32, max: f32) -> Self {
171        let modifier = Self::from_dyn_element(plain_element(SizeElement::with_constraints(
172            None,
173            None,
174            bound(min),
175            bound(max),
176            true,
177        )))
178        .with_inspector_metadata(inspector_metadata("heightIn", move |info| {
179            info.add_dimension("minHeight", DimensionConstraint::Points(min));
180        }));
181        self.then(modifier)
182    }
183
184    /// Keep the size of the content between `min` and `max` on each side, as
185    /// far as the incoming constraints allow. `f32::INFINITY` on a side of
186    /// `max` leaves it open.
187    ///
188    /// Matches Kotlin: `Modifier.sizeIn(minWidth, minHeight, maxWidth, maxHeight)`
189    ///
190    /// Example: `Modifier::empty().size_in(Size::new(24.0, 24.0), Size::new(f32::INFINITY, 40.0))`
191    pub fn size_in(self, min: Size, max: Size) -> Self {
192        let modifier = Self::from_dyn_element(plain_element(SizeElement::with_constraints(
193            bound(min.width),
194            bound(max.width),
195            bound(min.height),
196            bound(max.height),
197            true,
198        )))
199        .with_inspector_metadata(inspector_metadata("sizeIn", move |info| {
200            info.add_dimension("minWidth", DimensionConstraint::Points(min.width));
201            info.add_dimension("minHeight", DimensionConstraint::Points(min.height));
202        }));
203        self.then(modifier)
204    }
205}
206
207/// A finite side of a size bound; an infinite or zero bound is no bound.
208fn bound(value: f32) -> Option<f32> {
209    (value.is_finite() && value > 0.0).then_some(value)
210}