layuit 3.0.0

A UI layout library for Rust
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
//! Size constraint nodes.
//!
//! [`Margin`] wraps a single child in a runtime-configurable margin.
//!
//! [`Minimum`] wraps a single child with a configurable minimum size, allowing for the child to be
//! precisely aligned or given a fixed size. It can also be used without a child to create an empty,
//! fixed-size space.
//!
//! ## Alignment caveats with [`Margin`]
//!
//! When using [`Center`] on the child, the child will be aligned to the center of the space *after*
//! the restriction from the margin is applied. All other alignments hug the edges, giving the
//! correct appearances.
//!
//! To avoid the alignment shift while keeping centering, have symmetrical margins or use
//! [`Minimum`] with the intended area.
//! 
//! ## Negative margins
//! 
//! [`Margin`] supports negative values for all of its margins. A negative value causes the node to
//! grow its minimum size by the absolute value, but does no shrinking. This can be used to make a
//! node additively larger than its minimum size, rather than statically.
//! 
//! ```rust
//! use layuit::padding::{Margin, Spacer};
//! use layuit::stack::VStack;
//! use layuit::Rect;
//! 
//! let mut tree = layuit::ui!(
//!     %%,
//!     +|+ VStack::new() => [
//!         -|- Margin::new().with_equal(-4.0) => [
//!             +|+ Spacer::sized((10.0, 10.0))
//!         ],
//!         -|- Margin::new().with_equal(4.0) => [
//!             +|+ Spacer::sized((10.0, 10.0))
//!         ],
//!     ]
//! );
//! 
//! tree.calculate_layout(Rect::new(0.0, 0.0, 20.0, 40.0));
//! 
//! // Both margins have size 18.0, but only the first spacer grows to size 18.0
//! ```
//!
//! [`Center`]: Alignment::Center

use crate::{Alignment, NodeCache, NodeIndex, OwnedIndex, Rect, UiNode, UiTree};

/// Maintains a margin around a singular child.
///
/// Using a negative margin will expand the minimum size but not restrict the space of the child.
/// This can be used to make the child occupy more space than it would otherwise.
///
/// The space is restricted by the margin before alignment is applied, so if the child's alignment
/// is [`Center`] the child will be centered based on the restricted space, resulting in an
/// unexpected shift if `top != bottom` or `left != right`. All other alignments are unaffected.
///
/// Maintains the margin as a minimum size even if `child` is `None`, however, once a child is
/// assigned, it cannot be removed. If you intend to use the margin as a minimum size, you should
/// use [`Minimum`] with no child instead.
///
/// [`Center`]: Alignment::Center
pub struct Margin {
    /// The left margin amount.
    pub left: f32,
    /// The right margin amount.
    pub right: f32,
    /// The top margin amount.
    pub top: f32,
    /// The bottom margin amount.
    pub bottom: f32,

    align: (Alignment, Alignment),
    child: Option<OwnedIndex>,
}

impl Margin {
    /// Creates a new `Margin` with no child, no margin, and default alignment.
    pub fn new() -> Self {
        Self {
            left: 0.0,
            right: 0.0,
            top: 0.0,
            bottom: 0.0,
            align: Default::default(),
            child: None,
        }
    }

    /// Bind a child node.
    ///
    /// # Panics
    /// If there is already a child node.
    pub fn with_child(mut self, index: OwnedIndex) -> Self {
        assert!(self.child.is_none());
        self.child = Some(index);
        self
    }

    /// Set the horizontal and vertical alignment.
    pub fn with_align(mut self, align: (Alignment, Alignment)) -> Self {
        self.align = align;
        self
    }

    /// Set the left, right, top, and bottom margins.
    pub fn with_margins(mut self, left: f32, right: f32, top: f32, bottom: f32) -> Self {
        self.left = left;
        self.right = right;
        self.top = top;
        self.bottom = bottom;
        self
    }

    /// Set the left and right margins to the same value.
    pub fn with_equal_x(mut self, lr: f32) -> Self {
        self.left = lr;
        self.right = lr;
        self
    }

    /// Set the top and bottom margins to the same value.
    pub fn with_equal_y(mut self, tb: f32) -> Self {
        self.top = tb;
        self.bottom = tb;
        self
    }

    /// Set both the left and right margins, and both the top and bottom margins.
    pub fn with_equal_xy(mut self, lr: f32, tb: f32) -> Self {
        self.left = lr;
        self.right = lr;
        self.top = tb;
        self.bottom = tb;
        self
    }

    /// Set the left, right, top, and bottom margins to the same value.
    pub fn with_equal(mut self, margin: f32) -> Self {
        self.left = margin;
        self.right = margin;
        self.top = margin;
        self.bottom = margin;
        self
    }

    /// Set the left margin.
    pub fn with_left(mut self, left: f32) -> Self {
        self.left = left;
        self
    }

    /// Set the right margin.
    pub fn with_right(mut self, right: f32) -> Self {
        self.right = right;
        self
    }

    /// Set the top margin.
    pub fn with_top(mut self, top: f32) -> Self {
        self.top = top;
        self
    }

    /// Set the bottom margin.
    pub fn with_bottom(mut self, bottom: f32) -> Self {
        self.bottom = bottom;
        self
    }

    /// Get the tree index of the child.
    pub fn get_child(&self) -> Option<NodeIndex> {
        self.child.as_ref().map(OwnedIndex::shareable)
    }
}

impl Default for Margin {
    fn default() -> Self {
        Self::new()
    }
}

impl UiNode for Margin {
    fn get_align(&self) -> (Alignment, Alignment) {
        self.align
    }

    fn get_align_mut(&mut self) -> (&mut Alignment, &mut Alignment) {
        (&mut self.align.0, &mut self.align.1)
    }

    fn calculate_min_size(&self, tree: &UiTree) -> (f32, f32) {
        let left = self.left.abs();
        let right = self.right.abs();
        let top = self.top.abs();
        let bottom = self.bottom.abs();
        if let Some(child) = &self.child {
            let child = tree
                .get_cache(child.shareable())
                .expect("Child not in cache");
            let (w, h) = child.min_size;
            (w + left + right, h + top + bottom)
        } else {
            (left + right, top + bottom)
        }
    }

    fn calculate_rects(&self, cache: &NodeCache, tree: &UiTree) -> Vec<Rect> {
        let Some(child_idx) = self.child.as_ref() else {
            return vec![];
        };

        let child_idx = child_idx.shareable();

        let left = self.left.max(0.0);
        let right = self.right.max(0.0);
        let top = self.top.max(0.0);
        let bottom = self.bottom.max(0.0);

        let child_min = tree
            .get_cache(child_idx)
            .expect("Child not in cache")
            .min_size;
        let child = tree.get_node(child_idx).expect("Child not in cache");

        let space = Rect::new(
            cache.rect.x + left,
            cache.rect.y + top,
            cache.rect.w - left - right,
            cache.rect.h - top - bottom,
        )
        .align(child.get_align(), child_min);

        vec![space]
    }

    fn get_children(&self) -> Vec<NodeIndex> {
        self.child.iter().map(OwnedIndex::shareable).collect()
    }
}

/// Maintains an additional constraint to minimum size.
///
/// Maintains a minimum size even if `child` is `None`, however, once a child is
/// assigned, it cannot be removed.
pub struct Minimum {
    /// The minimum size to maintain.
    pub min_override: (f32, f32),

    child: Option<OwnedIndex>,
    align: (Alignment, Alignment),
}

impl Minimum {
    /// Creates a new `Minimum` with no child, no minimum override, and default alignment.
    pub fn new() -> Self {
        Self {
            min_override: (0.0, 0.0),
            child: None,
            align: Default::default(),
        }
    }

    /// Bind a child node to the node.
    ///
    /// # Panics
    /// If there is already a child node.
    pub fn with_child(mut self, index: OwnedIndex) -> Self {
        assert!(self.child.is_none());
        self.child = Some(index);
        self
    }

    /// Set the horizontal and vertical alignment.
    pub fn with_align(mut self, align: (Alignment, Alignment)) -> Self {
        self.align = align;
        self
    }

    /// Set the minimum size.
    pub fn with_min(mut self, min: (f32, f32)) -> Self {
        self.min_override = min;
        self
    }

    /// Bind a child node to the node.
    ///
    /// # Panics
    /// If there is already a child node.
    pub fn add_child(&mut self, index: OwnedIndex) {
        assert!(self.child.is_none());
        self.child = Some(index);
    }

    /// Get the tree index of the child.
    pub fn get_child(&self) -> Option<NodeIndex> {
        self.child.as_ref().map(OwnedIndex::shareable)
    }
}

impl Default for Minimum {
    fn default() -> Self {
        Self::new()
    }
}

impl UiNode for Minimum {
    fn get_align(&self) -> (Alignment, Alignment) {
        self.align
    }

    fn get_align_mut(&mut self) -> (&mut Alignment, &mut Alignment) {
        (&mut self.align.0, &mut self.align.1)
    }

    fn calculate_min_size(&self, tree: &UiTree) -> (f32, f32) {
        if let Some(child) = self.child.as_ref() {
            let child = tree
                .get_cache(child.shareable())
                .expect("Child not in cache");
            let (w, h) = child.min_size;
            (w.max(self.min_override.0), h.max(self.min_override.1))
        } else {
            self.min_override
        }
    }

    fn calculate_rects(&self, cache: &NodeCache, tree: &UiTree) -> Vec<Rect> {
        if let Some(child) = &self.child {
            let child = child.shareable();

            let child_min = tree.get_cache(child).expect("Child not in cache").min_size;
            let child = tree.get_node(child).expect("Child not in cache");
            let space = cache.rect.align(child.get_align(), child_min);
            vec![space]
        } else {
            vec![]
        }
    }

    fn get_children(&self) -> Vec<NodeIndex> {
        self.child.iter().map(OwnedIndex::shareable).collect()
    }
}

/// Behaves identically to a childless [`Minimum`]. It has a fixed size.
pub struct Spacer {
    size: (f32, f32),

    align: (Alignment, Alignment),
}

impl Spacer {
    /// Creates a new `Spacer` with no size and default alignment.
    pub fn new() -> Self {
        Self {
            size: (0.0, 0.0),
            align: Default::default(),
        }
    }

    /// Creates a new `Spacer` with the given size and default alignment.
    ///
    /// Equivalent to `Spacer::new().with_size(size)`.
    pub fn sized(size: (f32, f32)) -> Self {
        Self {
            size,
            align: Default::default(),
        }
    }

    /// Set the horizontal and vertical alignment.
    pub fn with_align(mut self, align: (Alignment, Alignment)) -> Self {
        self.align = align;
        self
    }

    /// Set the size.
    ///
    /// # Panics
    /// If `size` is negative
    pub fn with_size(mut self, size: (f32, f32)) -> Self {
        assert!(size.0 >= 0.0 && size.1 >= 0.0);
        self.size = size;
        self
    }

    /// Set the size.
    ///
    /// # Panics
    /// If `size` is negative
    pub fn set_size(&mut self, size: (f32, f32)) {
        assert!(size.0 >= 0.0 && size.1 >= 0.0);
        self.size = size;
    }

    /// Get the size.
    pub fn get_size(&self) -> (f32, f32) {
        self.size
    }
}

impl Default for Spacer {
    fn default() -> Self {
        Self::new()
    }
}

impl UiNode for Spacer {
    fn get_align(&self) -> (Alignment, Alignment) {
        self.align
    }

    fn get_align_mut(&mut self) -> (&mut Alignment, &mut Alignment) {
        (&mut self.align.0, &mut self.align.1)
    }

    fn calculate_min_size(&self, _tree: &UiTree) -> (f32, f32) {
        self.size
    }
}