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