Skip to main content

cranpose_ui/modifier/
size.rs

1use cranpose_ui_layout::IntrinsicSize;
2
3use super::{DimensionConstraint, Modifier, Size, inspector_metadata};
4use crate::modifier_nodes::{IntrinsicSizeElement, SizeElement};
5
6impl Modifier {
7    /// Declare the preferred size of the content to be exactly `size`.
8    ///
9    /// The incoming measurement constraints may override this value, forcing the content
10    /// to be either smaller or larger.
11    ///
12    /// Matches Kotlin: `Modifier.size(size: Dp)`
13    ///
14    /// Example: `Modifier::empty().size(Size { width: 100.0, height: 200.0 })`
15    pub fn size(self, size: Size) -> Self {
16        let width = size.width;
17        let height = size.height;
18        let modifier = Self::with_element(SizeElement::new(Some(width), Some(height)))
19            .with_inspector_metadata(inspector_metadata("size", move |info| {
20                info.add_dimension("width", DimensionConstraint::Points(width));
21                info.add_dimension("height", DimensionConstraint::Points(height));
22            }));
23        self.then(modifier)
24    }
25
26    /// Declare the preferred size of the content to be exactly `width`dp by `height`dp.
27    ///
28    /// Convenience method for `size(Size { width, height })`.
29    ///
30    /// Example: `Modifier::empty().size_points(100.0, 200.0)`
31    pub fn size_points(self, width: f32, height: f32) -> Self {
32        self.size(Size { width, height })
33    }
34
35    /// Declare the preferred width of the content to be exactly `width`dp.
36    ///
37    /// The incoming measurement constraints may override this value, forcing the content
38    /// to be either smaller or larger.
39    ///
40    /// Matches Kotlin: `Modifier.width(width: Dp)`
41    ///
42    /// Example: `Modifier::empty().width(100.0).height(200.0)`
43    pub fn width(self, width: f32) -> Self {
44        let modifier = Self::with_element(SizeElement::new(Some(width), None))
45            .with_inspector_metadata(inspector_metadata("width", move |info| {
46                info.add_dimension("width", DimensionConstraint::Points(width));
47            }));
48        self.then(modifier)
49    }
50
51    /// Declare the preferred height of the content to be exactly `height`dp.
52    ///
53    /// The incoming measurement constraints may override this value, forcing the content
54    /// to be either smaller or larger.
55    ///
56    /// Matches Kotlin: `Modifier.height(height: Dp)`
57    ///
58    /// Example: `Modifier::empty().width(100.0).height(200.0)`
59    pub fn height(self, height: f32) -> Self {
60        let modifier = Self::with_element(SizeElement::new(None, Some(height)))
61            .with_inspector_metadata(inspector_metadata("height", move |info| {
62                info.add_dimension("height", DimensionConstraint::Points(height));
63            }));
64        self.then(modifier)
65    }
66
67    /// Declare the width of the content based on its intrinsic size.
68    ///
69    /// Matches Kotlin: `Modifier.width(IntrinsicSize)`
70    pub fn width_intrinsic(self, intrinsic: IntrinsicSize) -> Self {
71        let modifier = Self::with_element(IntrinsicSizeElement::width(intrinsic))
72            .with_inspector_metadata(inspector_metadata("widthIntrinsic", move |info| {
73                info.add_dimension("width", DimensionConstraint::Intrinsic(intrinsic));
74            }));
75        self.then(modifier)
76    }
77
78    /// Declare the height of the content based on its intrinsic size.
79    ///
80    /// Matches Kotlin: `Modifier.height(IntrinsicSize)`
81    pub fn height_intrinsic(self, intrinsic: IntrinsicSize) -> Self {
82        let modifier = Self::with_element(IntrinsicSizeElement::height(intrinsic))
83            .with_inspector_metadata(inspector_metadata("heightIntrinsic", move |info| {
84                info.add_dimension("height", DimensionConstraint::Intrinsic(intrinsic));
85            }));
86        self.then(modifier)
87    }
88
89    /// Declare the size of the content to be exactly `size`, ignoring incoming constraints.
90    ///
91    /// The incoming measurement constraints will not override this value. If the content
92    /// chooses a size that does not satisfy the incoming constraints, the parent layout
93    /// will be reported a size coerced in the constraints.
94    ///
95    /// Matches Kotlin: `Modifier.requiredSize(size: Dp)`
96    pub fn required_size(self, size: Size) -> Self {
97        let modifier = Self::with_element(SizeElement::with_constraints(
98            Some(size.width),
99            Some(size.width),
100            Some(size.height),
101            Some(size.height),
102            false,
103        ));
104        self.then(modifier)
105    }
106}