waterui-layout 0.5.1

Layout components for WaterUI
Documentation
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! Padding layouts that inset a child by fixed edge distances.

use alloc::{vec, vec::Vec};
use nami::{Computed, Signal, signal::IntoComputed, watcher::BoxWatcherGuard};
use waterui_core::{AnyView, View, layout::LayoutInvalidationCallback};

use crate::{
    HorizontalAlignment, Layout, PlacedSubview, Point, ProposalSize, Rect, Size, StretchAxis,
    SubView, SubviewPlacement, VerticalAlignment, container::FixedContainer,
};

/// Layout that insets its single child by the configured edge values.
///
/// The insets are reactive, so a change to them moves the padded content
/// without the subtree being rebuilt.
#[derive(Debug, Clone)]
pub struct PaddingLayout {
    edges: Computed<EdgeInsets>,
}

impl Layout for PaddingLayout {
    /// Padding is transparent to its content: it insets the child within
    /// whatever bounds it is given, so the child's axis is the answer.
    fn stretch_axis(&self, children: &[StretchAxis]) -> StretchAxis {
        children.first().copied().unwrap_or_default()
    }

    fn size_that_fits(&self, proposal: ProposalSize, children: &[&dyn SubView]) -> Size {
        let edges = self.edges.get();
        // The horizontal and vertical space consumed by padding.
        let horizontal_padding = edges.leading + edges.trailing;
        let vertical_padding = edges.top + edges.bottom;

        // Reduce the proposed size for the child by the padding amount.
        let child_proposal = ProposalSize {
            width: proposal.width.map(|w| (w - horizontal_padding).max(0.0)),
            height: proposal.height.map(|h| (h - vertical_padding).max(0.0)),
        };

        // Measure the child
        let child_size = children
            .first()
            .map_or(Size::zero(), |c| c.measure(child_proposal).size);

        // A greedy child reports an infinite extent, which cannot be added to the
        // insets, so resolve it against the offer instead. With nothing offered
        // there is nothing to fill, and the inset alone is the answer — never a
        // negative extent, which is what subtracting the insets from a missing
        // proposal used to produce.
        let child_width = if child_size.width.is_infinite() {
            (proposal.width.unwrap_or(0.0) - horizontal_padding).max(0.0)
        } else {
            child_size.width
        };

        let child_height = if child_size.height.is_infinite() {
            (proposal.height.unwrap_or(0.0) - vertical_padding).max(0.0)
        } else {
            child_size.height
        };

        // The final size is the child's size plus the padding.
        Size::new(
            child_width + horizontal_padding,
            child_height + vertical_padding,
        )
    }

    fn place(
        &self,
        bounds: Rect,
        _proposal: ProposalSize,
        children: &[&dyn SubView],
    ) -> Vec<SubviewPlacement> {
        if children.is_empty() {
            return vec![];
        }

        let edges = self.edges.get();
        // Create the child's frame by insetting the parent's bound by the padding amount.
        let child_origin = Point::new(bounds.x() + edges.leading, bounds.y() + edges.top);

        let horizontal_padding = edges.leading + edges.trailing;
        let vertical_padding = edges.top + edges.bottom;

        let child_size = Size::new(
            (bounds.width() - horizontal_padding).max(0.0),
            (bounds.height() - vertical_padding).max(0.0),
        );

        // Placement proposes the region the child is placed in: the bounds
        // less the insets, so a padded child stretched by its container lays
        // out across the room it actually has.
        let child_proposal = ProposalSize::new(Some(child_size.width), Some(child_size.height));

        vec![SubviewPlacement::new(
            Rect::new(child_origin, child_size),
            child_proposal,
        )]
    }

    fn explicit_horizontal(
        &self,
        alignment: HorizontalAlignment,
        _bounds: Rect,
        children: &[PlacedSubview<'_>],
    ) -> Option<f32> {
        children
            .first()
            .and_then(|child| child.explicit_horizontal(alignment))
    }

    fn explicit_vertical(
        &self,
        alignment: VerticalAlignment,
        _bounds: Rect,
        children: &[PlacedSubview<'_>],
    ) -> Option<f32> {
        children
            .first()
            .and_then(|child| child.explicit_vertical(alignment))
    }

    fn watch_invalidation(&self, invalidate: LayoutInvalidationCallback) -> Vec<BoxWatcherGuard> {
        vec![self.edges.watch(move |_| invalidate())]
    }
}

/// Insets applied to the four edges of a rectangle.
#[derive(Debug, Clone, PartialEq)]
pub struct EdgeInsets {
    top: f32,
    bottom: f32,
    leading: f32,
    trailing: f32,
}

nami::impl_constant!(EdgeInsets);

/// Equal insets on every edge from one number — `.padding_with(16.0)` —
/// for every numeric type `f64` converts from, which is the set the former
/// `From<T: Into<f64>>` blanket covered. The blanket had to go so the tuple
/// and array conversions below could exist beside it (coherence forbids a
/// concrete `From<(f32, f32)>` next to a blanket over `Into<f64>`).
macro_rules! from_scalar {
    (lossless: $($ty:ty),+ $(,)?) => {
        $(
            impl From<$ty> for EdgeInsets {
                fn from(value: $ty) -> Self {
                    Self::all(f32::from(value))
                }
            }
        )+
    };
    (lossy: $($ty:ty),+ $(,)?) => {
        $(
            #[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
            impl From<$ty> for EdgeInsets {
                fn from(value: $ty) -> Self {
                    Self::all(value as f32)
                }
            }
        )+
    };
}

from_scalar!(lossless: f32, i8, i16, u8, u16);
from_scalar!(lossy: f64, i32, u32);

/// `(vertical, horizontal)` — the [`EdgeInsets::symmetric`] order:
/// `.padding_with((8.0, 16.0))`. The `f64` form is what an unsuffixed
/// literal pair infers to as a constant signal.
macro_rules! from_pair {
    ($($ty:ty),+ $(,)?) => {
        $(
            #[allow(clippy::cast_possible_truncation)]
            impl From<($ty, $ty)> for EdgeInsets {
                fn from((vertical, horizontal): ($ty, $ty)) -> Self {
                    Self::symmetric(vertical as f32, horizontal as f32)
                }
            }
        )+
    };
}

from_pair!(f32, f64);

/// `[top, bottom, leading, trailing]` — the [`EdgeInsets::new`] order:
/// `.padding_with([4.0, 12.0, 16.0, 16.0])`.
macro_rules! from_quad {
    ($($ty:ty),+ $(,)?) => {
        $(
            #[allow(clippy::cast_possible_truncation)]
            impl From<[$ty; 4]> for EdgeInsets {
                fn from([top, bottom, leading, trailing]: [$ty; 4]) -> Self {
                    Self::new(top as f32, bottom as f32, leading as f32, trailing as f32)
                }
            }
        )+
    };
}

from_quad!(f32, f64);

impl core::ops::Add for EdgeInsets {
    type Output = Self;

    /// Stacks two sets of insets, edge by edge.
    fn add(self, rhs: Self) -> Self {
        Self {
            top: self.top + rhs.top,
            bottom: self.bottom + rhs.bottom,
            leading: self.leading + rhs.leading,
            trailing: self.trailing + rhs.trailing,
        }
    }
}

impl Default for EdgeInsets {
    fn default() -> Self {
        Self::all(0.0)
    }
}

impl EdgeInsets {
    /// Creates an [`EdgeInsets`] value with explicit edges.
    #[must_use]
    pub const fn new(top: f32, bottom: f32, leading: f32, trailing: f32) -> Self {
        Self {
            top,
            bottom,
            leading,
            trailing,
        }
    }

    /// Returns equal insets on every edge.
    #[must_use]
    pub const fn all(value: f32) -> Self {
        Self {
            top: value,
            bottom: value,
            leading: value,
            trailing: value,
        }
    }

    /// Returns symmetric vertical and horizontal insets.
    #[must_use]
    pub const fn symmetric(vertical: f32, horizontal: f32) -> Self {
        Self {
            top: vertical,
            bottom: vertical,
            leading: horizontal,
            trailing: horizontal,
        }
    }

    /// Returns the top inset.
    #[must_use]
    pub const fn top(&self) -> f32 {
        self.top
    }

    /// Returns the bottom inset.
    #[must_use]
    pub const fn bottom(&self) -> f32 {
        self.bottom
    }

    /// Returns the leading (left in LTR) inset.
    #[must_use]
    pub const fn leading(&self) -> f32 {
        self.leading
    }

    /// Returns the trailing (right in LTR) inset.
    #[must_use]
    pub const fn trailing(&self) -> f32 {
        self.trailing
    }
}

/// View wrapper that applies [`PaddingLayout`] to a single child.
#[derive(Debug)]
pub struct Padding {
    layout: PaddingLayout,
    content: AnyView,
}

impl Padding {
    /// Wraps a view with custom `edges`.
    pub fn new(edges: impl IntoComputed<EdgeInsets>, content: impl View + 'static) -> Self {
        Self {
            layout: PaddingLayout {
                edges: edges.into_computed(),
            },
            content: AnyView::new(content),
        }
    }

    /// Consumes the padding and returns the edge insets and content.
    pub fn into_inner(self) -> (Computed<EdgeInsets>, AnyView) {
        (self.layout.edges, self.content)
    }
}

impl View for Padding {
    fn body(self, _env: &waterui_core::Environment) -> impl View {
        FixedContainer::new(self.layout, vec![self.content])
    }

    /// Resolves to `FixedContainer` over the same layout and single child;
    /// reports what that container would.
    fn stretch_axis(&self) -> StretchAxis {
        self.layout.stretch_axis(&[self.content.stretch_axis()])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::StretchAxis;
    use crate::ViewDimensions;
    use crate::measure_layout;
    use alloc::rc::Rc;
    use core::cell::Cell;
    use nami::binding;

    struct MockSubView {
        size: Size,
    }

    impl SubView for MockSubView {
        fn measure(&self, _proposal: ProposalSize) -> ViewDimensions {
            ViewDimensions::new(self.size)
        }
        fn stretch_axis(&self) -> StretchAxis {
            StretchAxis::None
        }
        fn priority(&self) -> i32 {
            0
        }
    }

    struct GuidedSubview {
        size: Size,
        horizontal_guide: f32,
        vertical_guide: f32,
    }

    impl SubView for GuidedSubview {
        fn measure(&self, _proposal: ProposalSize) -> ViewDimensions {
            ViewDimensions::new(self.size)
                .with_horizontal(HorizontalAlignment::Leading, self.horizontal_guide)
                .with_vertical(VerticalAlignment::Top, self.vertical_guide)
        }

        fn stretch_axis(&self) -> StretchAxis {
            StretchAxis::None
        }

        fn priority(&self) -> i32 {
            0
        }
    }

    #[test]
    fn test_padding_size() {
        let layout = PaddingLayout {
            edges: EdgeInsets::all(10.0).into_computed(),
        };

        let mut child = MockSubView {
            size: Size::new(50.0, 30.0),
        };
        let children: Vec<&dyn SubView> = vec![&mut child];

        let size = layout.size_that_fits(ProposalSize::UNSPECIFIED, &children);

        // Size = child size + padding on all sides
        assert!((size.width - 70.0).abs() < f32::EPSILON); // 50 + 10 + 10
        assert!((size.height - 50.0).abs() < f32::EPSILON); // 30 + 10 + 10
    }

    #[test]
    fn test_padding_placement_bounded_proposal() {
        let layout = PaddingLayout {
            edges: EdgeInsets::new(10.0, 20.0, 15.0, 25.0).into_computed(),
        };

        let mut child = MockSubView {
            size: Size::new(50.0, 30.0),
        };
        let children: Vec<&dyn SubView> = vec![&mut child];

        let bounds = Rect::new(Point::new(0.0, 0.0), Size::new(100.0, 100.0));
        let proposal = ProposalSize::new(Some(bounds.width()), Some(bounds.height()));
        let placements = layout.place(bounds, proposal, &children);

        // Child origin is offset by leading and top
        assert!((placements[0].frame.x() - 15.0).abs() < f32::EPSILON);
        assert!((placements[0].frame.y() - 10.0).abs() < f32::EPSILON);

        // Child size is bounds minus padding
        assert!((placements[0].frame.width() - 60.0).abs() < f32::EPSILON); // 100 - 15 - 25
        assert!((placements[0].frame.height() - 70.0).abs() < f32::EPSILON); // 100 - 10 - 20
    }

    #[test]
    fn reactive_insets_invalidate_and_change_measurement() {
        let inset = binding(EdgeInsets::all(10.0));
        let layout = PaddingLayout {
            edges: inset.clone().into_computed(),
        };

        let invalidations = Rc::new(Cell::new(0));
        let counted = Rc::clone(&invalidations);
        let _guards = layout.watch_invalidation(Rc::new(move || {
            counted.set(counted.get() + 1);
        }));

        let child = MockSubView {
            size: Size::new(50.0, 30.0),
        };
        let children: Vec<&dyn SubView> = vec![&child];
        assert_eq!(
            layout
                .size_that_fits(ProposalSize::UNSPECIFIED, &children)
                .width,
            70.0
        );

        inset.set(EdgeInsets::all(20.0));

        assert_eq!(invalidations.get(), 1);
        assert_eq!(
            layout
                .size_that_fits(ProposalSize::UNSPECIFIED, &children)
                .width,
            90.0
        );
    }

    #[test]
    fn test_padding_offsets_explicit_guides() {
        let layout = PaddingLayout {
            edges: EdgeInsets::new(10.0, 0.0, 15.0, 0.0).into_computed(),
        };
        let child = GuidedSubview {
            size: Size::new(50.0, 30.0),
            horizontal_guide: 8.0,
            vertical_guide: 6.0,
        };
        let children: Vec<&dyn SubView> = vec![&child];

        let dimensions = measure_layout(&layout, ProposalSize::UNSPECIFIED, &children);

        assert_eq!(
            dimensions.explicit_horizontal(HorizontalAlignment::Leading),
            Some(23.0)
        );
        assert_eq!(
            dimensions.explicit_vertical(VerticalAlignment::Top),
            Some(16.0)
        );
    }
}