cranpose_ui/modifier_nodes/
fill.rs1use std::hash::Hash;
5
6use cranpose_foundation::{
7 Constraints, Measurable, ModifierNodeContext, NodeCapabilities, PlainElement, PlainIntrinsic,
8 PlainLayout, PlainLayoutFns, Size,
9};
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13pub enum FillDirection {
14 Horizontal,
16 Vertical,
18 Both,
20}
21
22#[derive(Debug, Clone, PartialEq)]
27pub struct FillElement {
28 direction: FillDirection,
29 fraction: f32,
30}
31
32impl FillElement {
33 pub fn width(fraction: f32) -> Self {
35 Self {
36 direction: FillDirection::Horizontal,
37 fraction,
38 }
39 }
40
41 pub fn height(fraction: f32) -> Self {
43 Self {
44 direction: FillDirection::Vertical,
45 fraction,
46 }
47 }
48
49 pub fn size(fraction: f32) -> Self {
51 Self {
52 direction: FillDirection::Both,
53 fraction,
54 }
55 }
56
57 pub fn direction(&self) -> FillDirection {
59 self.direction
60 }
61
62 pub fn fraction(&self) -> f32 {
64 self.fraction
65 }
66
67 pub(crate) fn fills(&self) -> (bool, bool) {
68 match self.direction {
69 FillDirection::Horizontal => (true, false),
70 FillDirection::Vertical => (false, true),
71 FillDirection::Both => (true, true),
72 }
73 }
74
75 fn filled(&self, extent: f32, other: FillDirection, density: f32) -> f32 {
78 if self.direction == other || !extent.is_finite() {
79 return extent;
80 }
81 cranpose_ui_layout::round_to_px(extent * self.fraction, density)
82 }
83
84 #[inline]
85 pub(crate) fn inner_constraints(&self, constraints: Constraints, density: f32) -> Constraints {
86 let (min_width, max_width) = if self.direction != FillDirection::Vertical
87 && constraints.max_width != f32::INFINITY
88 {
89 let width =
90 cranpose_ui_layout::round_to_px(constraints.max_width * self.fraction, density)
91 .clamp(constraints.min_width, constraints.max_width);
92 (width, width)
93 } else {
94 (constraints.min_width, constraints.max_width)
95 };
96 let (min_height, max_height) = if self.direction != FillDirection::Horizontal
97 && constraints.max_height != f32::INFINITY
98 {
99 let height =
100 cranpose_ui_layout::round_to_px(constraints.max_height * self.fraction, density)
101 .clamp(constraints.min_height, constraints.max_height);
102 (height, height)
103 } else {
104 (constraints.min_height, constraints.max_height)
105 };
106 Constraints {
107 min_width,
108 max_width,
109 min_height,
110 max_height,
111 }
112 }
113
114 #[inline]
115 pub(crate) fn place_size(
116 &self,
117 constraints: Constraints,
118 inner: Constraints,
119 child: Size,
120 ) -> Size {
121 let width = if self.direction != FillDirection::Vertical
122 && constraints.max_width != f32::INFINITY
123 {
124 inner.min_width
125 } else {
126 child.width
127 };
128 let height = if self.direction != FillDirection::Horizontal
129 && constraints.max_height != f32::INFINITY
130 {
131 inner.min_height
132 } else {
133 child.height
134 };
135 Size { width, height }
136 }
137}
138
139impl PlainElement for FillElement {
140 const WORDS: usize = 2;
141 const CAPABILITIES: NodeCapabilities =
142 NodeCapabilities::LAYOUT.union(NodeCapabilities::PARENT_DATA);
143 const LAYOUT: Option<&'static PlainLayoutFns> = Some(&PlainLayoutFns::of::<Self>());
144
145 #[inline]
146 fn to_words(&self, words: &mut [u32]) {
147 words[0] = match self.direction {
148 FillDirection::Horizontal => 0,
149 FillDirection::Vertical => 1,
150 FillDirection::Both => 2,
151 };
152 words[1] = self.fraction.to_bits();
153 }
154
155 #[inline]
156 fn from_words(words: &[u32]) -> Self {
157 let (direction, fraction) = match *words {
158 [direction, fraction, ..] => (direction, fraction),
159 _ => (2, 0),
160 };
161 Self {
162 direction: match direction {
163 0 => FillDirection::Horizontal,
164 1 => FillDirection::Vertical,
165 _ => FillDirection::Both,
166 },
167 fraction: f32::from_bits(fraction),
168 }
169 }
170}
171
172impl PlainLayout for FillElement {
173 fn measure_hold(
177 &self,
178 density: f32,
179 constraints: Constraints,
180 size: Size,
181 wrapped: cranpose_ui_layout::WrappedHold,
182 ) -> Option<cranpose_ui_layout::ConstraintsHold> {
183 Some(cranpose_ui_layout::ConstraintsHold {
184 width: fill_axis_hold(
185 self.direction != FillDirection::Vertical,
186 (constraints.max_width, size.width),
187 (self.fraction, density),
188 wrapped.hold.map(|hold| hold.width),
189 )?,
190 height: fill_axis_hold(
191 self.direction != FillDirection::Horizontal,
192 (constraints.max_height, size.height),
193 (self.fraction, density),
194 wrapped.hold.map(|hold| hold.height),
195 )?,
196 })
197 }
198
199 fn measure(
200 &self,
201 context: &mut dyn ModifierNodeContext,
202 measurable: &dyn Measurable,
203 constraints: Constraints,
204 ) -> cranpose_ui_layout::LayoutModifierMeasureResult {
205 let fill_constraints = self.inner_constraints(constraints, context.density());
206 let placeable = measurable.measure(fill_constraints);
207 let size = self.place_size(
208 constraints,
209 fill_constraints,
210 Size {
211 width: placeable.width(),
212 height: placeable.height(),
213 },
214 );
215 cranpose_ui_layout::LayoutModifierMeasureResult::with_size(size)
216 }
217
218 fn intrinsic(&self, measurable: &dyn Measurable, kind: PlainIntrinsic, density: f32) -> f32 {
219 let other = if kind.is_width() {
220 FillDirection::Horizontal
221 } else {
222 FillDirection::Vertical
223 };
224 kind.at(self.filled(kind.cross(), other, density))
225 .of(measurable)
226 }
227}
228
229fn fill_axis_hold(
232 fills: bool,
233 (max, size): (f32, f32),
234 (fraction, density): (f32, f32),
235 wrapped: Option<cranpose_ui_layout::AxisHold>,
236) -> Option<cranpose_ui_layout::AxisHold> {
237 use cranpose_ui_layout::{AxisHold, BoundRange};
238 if !fills {
239 return wrapped;
240 }
241 if max == f32::INFINITY {
242 let wrapped = wrapped?;
243 return Some(AxisHold {
244 min: wrapped.min,
245 max: wrapped.max.intersect(BoundRange::exactly(f32::INFINITY))?,
246 });
247 }
248 (size == cranpose_ui_layout::round_to_px(max * fraction, density)).then_some(AxisHold {
249 min: BoundRange::up_to(size),
250 max: BoundRange::exactly(max),
251 })
252}