backer 0.12.0

A library for straight-forward UI layout.
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
use crate::{
    passes::{collect, perform_layout_passes},
    tree::TreeNode,
    types::*,
};
use std::ops::RangeBounds;

macro_rules! container_doc {
    () => {
        r#"
Container nodes, by default, will only take up enough space to fit their contents.

If you want the container to take up as much space as is available you can use an `expand` modifier,
or add an unconstrained node to it's contents.

Unconstrained nodes can be conceptualized as "pushing" outwards & expanding their container,
or pushing against other unconstrained nodes with equal force.
        "#
    };
}

impl<'a, D, S> Layout<'a, D, S> {
    /// Performs layout passes & collect all the draw values from draw nodes.
    pub fn draw(&mut self, available_area: Area, state: &mut S) -> Vec<D> {
        perform_layout_passes(self, available_area, state);
        collect(self, state)
    }
    /// Layout the node & return the minimum height allowed described by the layout tree's constraints
    pub fn min_height(&mut self, available_area: Area, state: &mut S) -> Option<f32> {
        perform_layout_passes(self, available_area, state);
        self.constraints().height.lower
    }
    /// Layout the node & return the minimum width allowed by the layout tree's constraints
    pub fn min_width(&mut self, available_area: Area, state: &mut S) -> Option<f32> {
        perform_layout_passes(self, available_area, state);
        self.constraints().width.lower
    }
}

impl<'a, D: 'a, S: 'a> Layout<'a, D, S> {
    /// Transforms the draw output of every node in the tree from type `D` to type `B`.
    pub fn map<B: 'a>(self, f: impl Fn(D) -> B + 'a) -> Layout<'a, B, S> {
        use std::rc::Rc;
        let f = Rc::new(f);
        self.map_tree(|node, children| {
            let mapped_layout = match node.layout {
                LayoutType::Draw(Some(draw_fn)) => {
                    let f = f.clone();
                    LayoutType::Draw(Some(Box::new(move |area, s| {
                        draw_fn(area, s).into_iter().map(|draw| f(draw)).collect()
                    })))
                }
                LayoutType::Draw(None) => LayoutType::Draw(None),
                LayoutType::Column {
                    spacing,
                    x_align,
                    y_align,
                } => LayoutType::Column {
                    spacing,
                    x_align,
                    y_align,
                },
                LayoutType::Row {
                    spacing,
                    x_align,
                    y_align,
                } => LayoutType::Row {
                    spacing,
                    x_align,
                    y_align,
                },
                LayoutType::Stack { x_align, y_align } => LayoutType::Stack { x_align, y_align },
                LayoutType::Padding {
                    leading,
                    trailing,
                    top,
                    bottom,
                } => LayoutType::Padding {
                    leading,
                    trailing,
                    top,
                    bottom,
                },
                LayoutType::Offset { x, y } => LayoutType::Offset { x, y },
                LayoutType::Space => LayoutType::Space,
                LayoutType::Empty => LayoutType::Empty,
            };
            Layout {
                layout: mapped_layout,
                constraints: node.constraints,
                dynamic_constraints: node.dynamic_constraints,
                layer: node.layer,
                resolved: node.resolved,
                allocated: node.allocated,
                children,
            }
        })
    }
}

struct NodeBuilder<'a, D, S = ()> {
    layout: LayoutType<'a, D, S>,
    constraints: Constraints,
    dynamic_constraints: DynamicConstraints<'a, S>,
    children: Vec<Layout<'a, D, S>>,
}

impl<'a, D, S> NodeBuilder<'a, D, S> {
    fn new(layout: LayoutType<'a, D, S>) -> Self {
        Self {
            layout,
            constraints: Constraints::default(),
            dynamic_constraints: DynamicConstraints::default(),
            children: Vec::new(),
        }
    }

    fn children(mut self, children: Vec<Layout<'a, D, S>>) -> Self {
        self.children = children
            .into_iter()
            .filter(|child| !matches!(child.layout, LayoutType::Empty))
            .collect();
        self
    }

    fn child(self, child: Layout<'a, D, S>) -> Self {
        self.children(vec![child])
    }

    fn build(self) -> Layout<'a, D, S> {
        Layout {
            layout: self.layout,
            constraints: self.constraints,
            dynamic_constraints: self.dynamic_constraints,
            layer: None,
            resolved: None,
            allocated: None,
            children: self.children,
        }
    }
}

/// Creates a node that can be drawn.
///
/// This node produces a value from a laid-out `Area` that will be collected when calling `Layout::draw`
pub fn draw<'a, D, S>(data: impl FnOnce(Area, &mut S) -> Vec<D> + 'a) -> Layout<'a, D, S> {
    NodeBuilder::new(LayoutType::Draw(Some(Box::new(data)))).build()
}

/// Creates a vertical sequence of elements
///
#[doc = container_doc!()]
pub fn column<'a, D, S>(elements: Vec<Layout<'a, D, S>>) -> Layout<'a, D, S> {
    NodeBuilder::new(LayoutType::Column {
        spacing: 0.,
        x_align: None,
        y_align: None,
    })
    .children(elements)
    .build()
}

/// Creates a vertical sequence of elements with the specified spacing between each element.
///
#[doc = container_doc!()]
pub fn column_spaced<'a, D, S>(spacing: f32, elements: Vec<Layout<'a, D, S>>) -> Layout<'a, D, S> {
    NodeBuilder::new(LayoutType::Column {
        spacing,
        x_align: None,
        y_align: None,
    })
    .children(elements)
    .build()
}

/// Creates a vertical sequence of elements with the specified alignment applied to each immediate child.
///
#[doc = container_doc!()]
pub fn column_aligned<'a, D, S>(align: Align, elements: Vec<Layout<'a, D, S>>) -> Layout<'a, D, S> {
    let (x_align, y_align) = align.axis_aligns();
    NodeBuilder::new(LayoutType::Column {
        spacing: 0.,
        x_align,
        y_align,
    })
    .children(elements)
    .build()
}

/// Creates a horizontal sequence of elements with the specified spacing between each element and the specified alignment applied to each immediate child.
///
#[doc = container_doc!()]
pub fn column_spaced_aligned<'a, D, S>(
    spacing: f32,
    align: Align,
    elements: Vec<Layout<'a, D, S>>,
) -> Layout<'a, D, S> {
    let (x_align, y_align) = align.axis_aligns();
    NodeBuilder::new(LayoutType::Column {
        spacing,
        x_align,
        y_align,
    })
    .children(elements)
    .build()
}

/// Creates a horizontal sequence of elements
///
#[doc = container_doc!()]
pub fn row<'a, D, S>(elements: Vec<Layout<'a, D, S>>) -> Layout<'a, D, S> {
    NodeBuilder::new(LayoutType::Row {
        spacing: 0.0,
        x_align: None,
        y_align: None,
    })
    .children(elements)
    .build()
}

/// Creates a horizontal sequence of elements with the specified spacing between each element.
///
#[doc = container_doc!()]
pub fn row_spaced<'a, D, S>(spacing: f32, elements: Vec<Layout<'a, D, S>>) -> Layout<'a, D, S> {
    NodeBuilder::new(LayoutType::Row {
        spacing,
        x_align: None,
        y_align: None,
    })
    .children(elements)
    .build()
}

/// Creates a horizontal sequence of elements with the specified alignment applied to each immediate child.
///
#[doc = container_doc!()]
pub fn row_aligned<'a, D, S>(align: Align, elements: Vec<Layout<'a, D, S>>) -> Layout<'a, D, S> {
    let (x_align, y_align) = align.axis_aligns();
    NodeBuilder::new(LayoutType::Row {
        spacing: 0.0,
        x_align,
        y_align,
    })
    .children(elements)
    .build()
}

/// Creates a horizontal sequence of elements with the specified spacing between each element and the specified alignment applied to each immediate child.
///
#[doc = container_doc!()]
pub fn row_spaced_aligned<'a, D, S>(
    spacing: f32,
    align: Align,
    elements: Vec<Layout<'a, D, S>>,
) -> Layout<'a, D, S> {
    let (x_align, y_align) = align.axis_aligns();
    NodeBuilder::new(LayoutType::Row {
        spacing,
        x_align,
        y_align,
    })
    .children(elements)
    .build()
}

/// Creates a sequence of elements to be laid out on top of each other.
///
#[doc = container_doc!()]
pub fn stack<'a, D, S>(elements: Vec<Layout<'a, D, S>>) -> Layout<'a, D, S> {
    NodeBuilder::new(LayoutType::Stack {
        x_align: None,
        y_align: None,
    })
    .children(elements)
    .build()
}

/// Creates a sequence of elements to be laid out on top of each other with the specified alignment applied to each immediate child.
///
#[doc = container_doc!()]
pub fn stack_aligned<'a, D, S>(align: Align, elements: Vec<Layout<'a, D, S>>) -> Layout<'a, D, S> {
    let (x_align, y_align) = align.axis_aligns();
    NodeBuilder::new(LayoutType::Stack { x_align, y_align })
        .children(elements)
        .build()
}

/// Creates an empty space which is laid out the same as any other node.
///
/// To add spacing between each item in a row or column you can also use
/// [`row_spaced`] & [`column_spaced`]
pub fn space<'a, D, S>() -> Layout<'a, D, S> {
    NodeBuilder::new(LayoutType::Space).build()
}

/// Nothing! This will not have any impact on layout - useful for conditionally
/// adding elements to a layout in the case where nothing should be added.
pub fn empty<'a, D, S>() -> Layout<'a, D, S> {
    NodeBuilder::new(LayoutType::Empty).build()
}

impl<'a, D, S> Layout<'a, D, S> {
    /// Adds padding to the node on all edges
    pub fn pad(self, amount: f32) -> Layout<'a, D, S> {
        NodeBuilder::new(LayoutType::Padding {
            leading: amount,
            trailing: amount,
            top: amount,
            bottom: amount,
        })
        .child(self)
        .build()
    }

    /// Adds horizontal padding to the node (leading & trailing)
    pub fn pad_x(self, amount: f32) -> Self {
        NodeBuilder::new(LayoutType::Padding {
            leading: amount,
            trailing: amount,
            top: 0.,
            bottom: 0.,
        })
        .child(self)
        .build()
    }

    /// Adds vertical padding to the node (top & bottom)
    pub fn pad_y(self, amount: f32) -> Self {
        NodeBuilder::new(LayoutType::Padding {
            leading: 0.,
            trailing: 0.,
            top: amount,
            bottom: amount,
        })
        .child(self)
        .build()
    }

    /// Adds padding to the node along the top edge
    pub fn pad_top(self, amount: f32) -> Self {
        NodeBuilder::new(LayoutType::Padding {
            leading: 0.,
            trailing: 0.,
            top: amount,
            bottom: 0.,
        })
        .child(self)
        .build()
    }

    /// Adds padding to the node along the bottom edge
    pub fn pad_bottom(self, amount: f32) -> Self {
        NodeBuilder::new(LayoutType::Padding {
            leading: 0.,
            trailing: 0.,
            top: 0.,
            bottom: amount,
        })
        .child(self)
        .build()
    }

    /// Adds padding to the node along the leading edge
    pub fn pad_leading(self, amount: f32) -> Self {
        NodeBuilder::new(LayoutType::Padding {
            leading: amount,
            trailing: 0.,
            top: 0.,
            bottom: 0.,
        })
        .child(self)
        .build()
    }

    /// Adds padding to the node along the trailing edge
    pub fn pad_trailing(self, amount: f32) -> Self {
        NodeBuilder::new(LayoutType::Padding {
            leading: 0.,
            trailing: amount,
            top: 0.,
            bottom: 0.,
        })
        .child(self)
        .build()
    }

    /// Offsets the node along the x & y axis.
    /// This is an absolute offset that simply shifts nodes away from their calculated position
    /// This won't impact layout besides child nodes also being offset
    pub fn offset(self, x: f32, y: f32) -> Layout<'a, D, S> {
        NodeBuilder::new(LayoutType::Offset { x, y })
            .child(self)
            .build()
    }

    /// Offsets the node along the x axis.
    /// This is an absolute offset that simply shifts nodes away from their calculated position
    /// This won't impact layout besides child nodes also being offset
    pub fn offset_x(self, x: f32) -> Layout<'a, D, S> {
        NodeBuilder::new(LayoutType::Offset { x, y: 0. })
            .child(self)
            .build()
    }

    /// Offsets the node along the y axis.
    /// This is an absolute offset that simply shifts nodes away from their calculated position
    /// This won't impact layout besides child nodes also being offset
    pub fn offset_y(self, y: f32) -> Layout<'a, D, S> {
        NodeBuilder::new(LayoutType::Offset { x: 0., y })
            .child(self)
            .build()
    }
}

impl<'a, D, S> Layout<'a, D, S> {
    /// Specifies the z layer of the node.
    /// Layers are global, & all children of a node will inherit their parent's layer.
    pub fn layer(mut self, layer: i32) -> Layout<'a, D, S> {
        self.layer = Some(layer);
        self
    }

    /// Specifies an explicit width for a node
    pub fn width(mut self, width: f32) -> Self {
        self.constraints.width.lower = Some(width);
        self.constraints.width.upper = Some(width);
        self
    }

    /// Specifies an explicit height for a node
    pub fn height(mut self, height: f32) -> Self {
        self.constraints.height.lower = Some(height);
        self.constraints.height.upper = Some(height);
        self
    }

    /// Expands the node along the x axis, ignoring child sizes.
    ///
    /// Prevents containers from hugging / shrink-wrapping their contents.
    /// This is mutually exclusive with explicit width constraints.
    pub fn expand_x(mut self) -> Self {
        self.constraints.expand_x = true;
        self
    }

    /// Expands the node along the y axis, ignoring child sizes.
    ///
    /// Prevents containers from hugging / shrink-wrapping their contents.
    /// This is mutually exclusive with explicit height constraints.
    pub fn expand_y(mut self) -> Self {
        self.constraints.expand_y = true;
        self
    }

    /// Expands the node along both axes, ignoring child sizes.
    ///
    /// Prevents containers from hugging / shrink-wrapping their contents.
    /// This is mutually exclusive with explicit size constraints.
    pub fn expand(self) -> Self {
        self.expand_x().expand_y()
    }

    /// Makes the node invisible to its container's sizing.
    ///
    /// The container will size itself as if this node doesn't exist.
    /// The node still receives space & participates in drawing normally.
    pub fn inert(self) -> Self {
        self.inert_x().inert_y()
    }

    /// Makes the node invisible to its container's sizing along the x axis.
    ///
    /// The container will size its width as if this node doesn't exist.
    pub fn inert_x(mut self) -> Self {
        self.constraints.transparent_x = true;
        self
    }

    /// Makes the node invisible to its container's sizing along the y axis.
    ///
    /// The container will size its height as if this node doesn't exist.
    pub fn inert_y(mut self) -> Self {
        self.constraints.transparent_y = true;
        self
    }

    /// Specifies bounds on a node's width
    pub fn width_range<R>(mut self, range: R) -> Self
    where
        R: RangeBounds<f32>,
    {
        let (width_min, width_max) = Self::extract_bounds(range);
        self.constraints.width.lower = width_min;
        self.constraints.width.upper = width_max;
        self
    }

    /// Specifies bounds on a node's height
    pub fn height_range<R>(mut self, range: R) -> Self
    where
        R: RangeBounds<f32>,
    {
        let (height_min, height_max) = Self::extract_bounds(range);
        self.constraints.height.lower = height_min;
        self.constraints.height.upper = height_max;
        self
    }

    fn extract_bounds<R: RangeBounds<f32>>(range: R) -> (Option<f32>, Option<f32>) {
        let min = match range.start_bound() {
            std::ops::Bound::Included(bound) | std::ops::Bound::Excluded(bound) => Some(*bound),
            std::ops::Bound::Unbounded => None,
        };
        let max = match range.end_bound() {
            std::ops::Bound::Included(bound) | std::ops::Bound::Excluded(bound) => Some(*bound),
            std::ops::Bound::Unbounded => None,
        };
        (min, max)
    }

    /// Specifies an alignment along the x and/or y axis.
    ///
    /// The alignment of a node defines how it will be placed when there is less *or* more space available than it requires along a given axis.
    /// If this function doesn't seem to have any effect on the layout ensure that one of the mentioned conditions is true.
    pub fn align(mut self, align: Align) -> Self {
        let (x_align, y_align) = align.axis_aligns();
        if let Some(x) = x_align {
            self.constraints.x_align = Some(x);
        }
        if let Some(y) = y_align {
            self.constraints.y_align = Some(y);
        }
        self
    }

    /// Constrains the node's width as a function of available height.
    ///
    /// Generally you should prefer simple size constraints whenever possible.
    ///
    /// **This is primarily for UI elements such as text** where node width must depend on available height.
    pub fn dynamic_width(mut self, f: impl Fn(f32, &mut S) -> f32 + 'a) -> Layout<'a, D, S> {
        self.dynamic_constraints.width = Some(Box::new(f));
        self
    }
    /// Constrains the node's height as a function of available width.
    ///
    /// Generally you should prefer simple size constraints whenever possible.
    ///
    /// **This is primarily for UI elements such as text** where node height must depend on available width.
    pub fn dynamic_height(mut self, f: impl Fn(f32, &mut S) -> f32 + 'a) -> Layout<'a, D, S> {
        self.dynamic_constraints.height = Some(Box::new(f));
        self
    }

    /// Constrains the node's width to a multiple of it's height
    pub fn aspect_width(mut self, ratio: f32) -> Self {
        self.dynamic_constraints.width = Some(Box::new(move |height, _| height * ratio));
        self
    }

    /// Constrains the node's height to a multiple of it's width
    pub fn aspect_height(mut self, ratio: f32) -> Self {
        self.dynamic_constraints.height = Some(Box::new(move |width, _| width / ratio));
        self
    }
}