textyle 0.2.0

A text-based declarative UI library inspired by SwiftUI
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
use std::collections::HashSet;

pub mod sizing;
pub mod alignment;
pub mod geometry;

use geometry::Rect;

#[derive(Clone)]
pub enum Layout<Ctx> {
    Text(String),
    Width(usize, Box<Layout<Ctx>>),
    Height(usize, Box<Layout<Ctx>>),
    TopPadding(usize, Box<Layout<Ctx>>),
    RightPadding(usize, Box<Layout<Ctx>>),
    BottomPadding(usize, Box<Layout<Ctx>>),
    LeftPadding(usize, Box<Layout<Ctx>>),
    VCenter(Box<Layout<Ctx>>),
    HCenter(Box<Layout<Ctx>>),
    VBottomAlign(Box<Layout<Ctx>>),
    HRightAlign(Box<Layout<Ctx>>),
    VTopAlign(Box<Layout<Ctx>>),
    HLeftAlign(Box<Layout<Ctx>>),
    Background(char, Box<Layout<Ctx>>),
    Border(usize, char, HashSet<alignment::Edge>, Box<Layout<Ctx>>),

    VerticalStack(alignment::HorizontalAlignment, usize, Vec<Layout<Ctx>>),
    HorizontalStack(alignment::VerticalAlignment, usize, Vec<Layout<Ctx>>),

    DrawCanvas(fn(&mut Ctx, &Rect)->crate::canvas::TextCanvas),
    WithContext(fn(&Ctx)->Layout<Ctx>)
}

#[derive(Clone)]
pub enum SizedNode<Ctx: Clone> {
    Text(String),
    Width(usize, SizedLayout<Ctx>),
    Height(usize, SizedLayout<Ctx>),
    TopPadding(usize, SizedLayout<Ctx>),
    RightPadding(usize, SizedLayout<Ctx>),
    BottomPadding(usize, SizedLayout<Ctx>),
    LeftPadding(usize, SizedLayout<Ctx>),
    VCenter(SizedLayout<Ctx>),
    HCenter(SizedLayout<Ctx>),
    VBottomAlign(SizedLayout<Ctx>),
    HRightAlign(SizedLayout<Ctx>),
    VTopAlign(SizedLayout<Ctx>),
    HLeftAlign(SizedLayout<Ctx>),
    Background(char, SizedLayout<Ctx>),
    Border(usize, char, HashSet<alignment::Edge>, SizedLayout<Ctx>),

    VerticalStack(alignment::HorizontalAlignment, usize, Vec<SizedLayout<Ctx>>),
    HorizontalStack(alignment::VerticalAlignment, usize, Vec<SizedLayout<Ctx>>),

    DrawCanvas(fn(&mut Ctx, &Rect)->crate::canvas::TextCanvas)
}

#[derive(Clone)]
pub struct SizedLayout<Ctx: Clone> {
    pub node: Box<SizedNode<Ctx>>,
    pub sizing: sizing::ItemSizing
}

impl<Ctx: Clone> SizedLayout<Ctx> {
    fn new(node: SizedNode<Ctx>, sizing: sizing::ItemSizing) -> Self {
        SizedLayout { node: Box::new(node), sizing }
    }
}

impl<Ctx: Clone> Layout<Ctx> {
    fn calculate_line_size(&self, line: &str, bounds: &Rect) -> Rect {
        use unicode_segmentation::UnicodeSegmentation;
        let graphemes = line.graphemes(true).collect::<Vec<_>>();
        let rows = ((graphemes.len() as f64) / (bounds.width as f64)).ceil() as usize;

        if rows < 2 {
            Rect::sized(graphemes.len(), 1)
        } else {
            Rect::sized(bounds.width, rows)
        }
    }

    pub fn resolve_size(&self, bounds: &Rect, context: &mut Ctx) -> SizedLayout<Ctx> {
        use Layout::*;
        use sizing::Sizing::*;

        match self {
            Text(t) => {
                let lines = t.lines();

                let mut width = 0usize;
                let mut height = 0usize;
                for line in lines {
                    let sz = self.calculate_line_size(line, bounds);
                    if sz.width > width {
                        width = sz.width;
                    }

                    height += sz.height;
                }

                // TODO: Introduce `Flexible` item sizing to handle better text sizing.
                let sizing = sizing::ItemSizing::new(Static(width), Static(height));

                SizedLayout::new(SizedNode::Text(t.clone()), sizing)
            }
            VCenter(node) => {
                let resolved = node.resolve_size(bounds, context);
                let content_size = resolved.sizing.clone();

                let min_height = content_size.vertical.min_content_size();

                let sizing = sizing::ItemSizing::new(content_size.horizontal, Greedy(min_height));

                SizedLayout::new(SizedNode::VCenter(resolved), sizing)
            }
            VBottomAlign(node) => {
                let resolved = node.resolve_size(bounds, context);
                let content_size = resolved.sizing.clone();

                let min_height = content_size.vertical.min_content_size();

                let sizing = sizing::ItemSizing { horizontal: content_size.horizontal, vertical: Greedy(min_height) };

                SizedLayout::new(SizedNode::VBottomAlign(resolved), sizing)
            }
            HCenter(node) => {
                let resolved = node.resolve_size(bounds, context);
                let content_size = resolved.sizing.clone();

                let min_width = content_size.horizontal.min_content_size();

                let sizing = sizing::ItemSizing { horizontal: Greedy(min_width), vertical: content_size.vertical };

                SizedLayout::new(SizedNode::HCenter(resolved), sizing)
            }
            HRightAlign(node) => {
                let resolved = node.resolve_size(bounds, context);
                let content_size = resolved.sizing.clone();

                let min_width = content_size.horizontal.min_content_size();

                let sizing = sizing::ItemSizing { horizontal: Greedy(min_width), vertical: content_size.vertical };

                SizedLayout::new(SizedNode::HRightAlign(resolved), sizing)
            }
            VTopAlign(node) => {
                let resolved = node.resolve_size(bounds, context);
                let content_size = resolved.sizing.clone();

                let min_height = content_size.vertical.min_content_size();

                let sizing = sizing::ItemSizing { horizontal: content_size.horizontal, vertical: Greedy(min_height) };

                SizedLayout::new(SizedNode::VTopAlign(resolved), sizing)
            }
            HLeftAlign(node) => {
                let resolved = node.resolve_size(bounds, context);
                let content_size = resolved.sizing.clone();

                let min_width = content_size.horizontal.min_content_size();

                let sizing = sizing::ItemSizing { horizontal: Greedy(min_width), vertical: content_size.vertical };

                SizedLayout::new(SizedNode::HLeftAlign(resolved), sizing)
            }
            Width(size, node) => {
                let mut bounds = bounds.clone();
                bounds.width = *size;

                let resolved_content = node.resolve_size(&bounds, context);
                let mut frame = resolved_content.sizing.clone();
                frame.horizontal = Static(*size);

                SizedLayout::new(SizedNode::Width(*size, resolved_content), frame)
            }
            Height(size, node) => {
                let mut bounds = bounds.clone();
                bounds.height = *size;

                let resolved_content = node.resolve_size(&bounds, context);
                let mut frame = resolved_content.sizing.clone();
                frame.vertical = Static(*size);

                SizedLayout::new(SizedNode::Height(*size, resolved_content), frame)
            }
            TopPadding(n, node) | BottomPadding(n, node) => {
                let resolved = node.resolve_size(bounds, context);
                let mut frame = resolved.sizing.clone();
                
                frame.vertical.clamped_add(*n);

                let make_node = |n: usize, node: SizedLayout<Ctx>|{
                    match self {
                        TopPadding(_, _) => SizedNode::TopPadding(n, node),
                        BottomPadding(_, _) => SizedNode::BottomPadding(n, node),
                        _ => unreachable!()
                    }
                };

                if frame.vertical.min_content_size() > bounds.height {
                    // recalculate with less space
                    let mut bounds = bounds.clone();
                    bounds.height = bounds.height.saturating_sub(*n);

                    let resolved_content = node.resolve_size(&bounds, context);
                    let mut frame = resolved_content.sizing.clone();

                    frame.vertical.clamped_add(*n);

                    SizedLayout::new(make_node(*n, resolved_content), frame)
                } else {
                    SizedLayout::new(make_node(*n, resolved), frame)
                }
            }
            LeftPadding(n, node) | RightPadding(n, node) => {
                let resolved = node.resolve_size(bounds, context);
                let mut frame = resolved.sizing.clone();

                let make_node = |n: usize, node: SizedLayout<Ctx>|{
                    match self {
                        LeftPadding(_, _) => SizedNode::LeftPadding(n, node),
                        RightPadding(_, _) => SizedNode::RightPadding(n, node),
                        _ => unreachable!()
                    }
                };
                
                frame.horizontal.clamped_add(*n);
                if frame.horizontal.min_content_size() > bounds.width {
                    // recalculate with less space
                    let mut bounds = bounds.clone();
                    bounds.width = bounds.width.saturating_sub(*n);

                    let resolved_content = node.resolve_size(&bounds, context);
                    frame = resolved_content.sizing.clone();
                    frame.horizontal.clamped_add(*n);

                    let node = make_node(*n, resolved_content);

                    SizedLayout::new(node, frame)
                } else {
                    SizedLayout::new(make_node(*n, resolved), frame)
                }
            }
            Background(c, node) => {
                let resolved_content = node.resolve_size(bounds, context);
                let frame = resolved_content.sizing.clone();

                SizedLayout::new(SizedNode::Background(*c, resolved_content), frame)
            }
            Border(n, c, edges, node) => {
                let outer_bounds = bounds;
                let mut resolved_content = node.resolve_size(outer_bounds, context);
                let mut frame = resolved_content.sizing.clone();

                let mut added_height = 0;
                
                if edges.contains(&alignment::Edge::Top) {
                    frame.vertical.clamped_add(*n);
                    added_height += *n;
                }
                if edges.contains(&alignment::Edge::Bottom) {
                    frame.vertical.clamped_add(*n);
                    added_height += *n;
                }

                if frame.vertical.min_content_size() > outer_bounds.height {
                    // recalculate with less space
                    let mut bounds = outer_bounds.clone();
                    bounds.height = bounds.height.saturating_sub(added_height);

                    resolved_content = node.resolve_size(&bounds, context);
                    frame = resolved_content.sizing.clone();

                    frame.vertical.clamped_add(added_height);
                }

                let mut added_width = 0;

                if edges.contains(&alignment::Edge::Left) {
                    frame.horizontal.clamped_add(*n);
                    added_width += *n;
                }
                if edges.contains(&alignment::Edge::Right) {
                    frame.horizontal.clamped_add(*n);
                    added_width += *n;
                }

                if frame.horizontal.min_content_size() > outer_bounds.width {
                    // recalculate with less space
                    let mut bounds = outer_bounds.clone();
                    bounds.width = bounds.width.saturating_sub(added_width);

                    resolved_content = node.resolve_size(&bounds, context);
                    frame = resolved_content.sizing.clone();

                    frame.horizontal.clamped_add(added_width);
                }

                SizedLayout::new(SizedNode::Border(*n, *c, edges.clone(), resolved_content), frame)
            }

            VerticalStack(alignment, spacing,  nodes) => {
                let spacing_sizing = spacing * nodes.len().saturating_sub(1);
                let mut result = sizing::ItemSizing { horizontal: Static(0), vertical: Static(spacing_sizing) };
                let mut bounds = bounds.clone();
                bounds.height = bounds.height.saturating_sub(spacing_sizing);
                let mut resolved_children: Vec<SizedLayout<_>> = vec![];

                for node in nodes {
                    let resolved_node = node.resolve_size(&bounds, context);
                    let node_sizing = resolved_node.sizing.clone();
                    result.horizontal = match result.horizontal {
                        Static(j) => match node_sizing.horizontal {
                            Static(i) => Static(i.max(j)),
                            Greedy(i) => Greedy(i.max(j))
                        }
                        Greedy(j) => {
                            let i = node_sizing.horizontal.min_content_size();
                            Greedy(i.max(j))
                        }
                    };

                    result.vertical.clamped_accumulate_constrained(&node_sizing.vertical, bounds.height);
                    resolved_children.push(resolved_node);
                }

                SizedLayout::new(SizedNode::VerticalStack(alignment.clone(), *spacing, resolved_children), result)
            }
            HorizontalStack(alignment, spacing, nodes) => {
                let spacing_sizing = spacing * nodes.len().saturating_sub(1);
                let mut result = sizing::ItemSizing { horizontal: Static(spacing_sizing), vertical: Static(0) };let mut bounds = bounds.clone();
                bounds.width -= spacing_sizing;

                let mut resolved_children = vec![];

                for node in nodes {
                    let resolved_node = node.resolve_size(&bounds, context);
                    let node_sizing = resolved_node.sizing.clone();
                    result.vertical = match result.vertical {
                        Static(j) => match node_sizing.vertical {
                            Static(i) => Static(i.max(j)),
                            Greedy(i) => Greedy(i.max(j))
                        }
                        Greedy(j) => {
                            let i = node_sizing.vertical.min_content_size();
                            Greedy(i.max(j))
                        }
                    };

                    result.horizontal.clamped_accumulate_constrained(&node_sizing.horizontal, bounds.width);

                    resolved_children.push(resolved_node);
                }

                SizedLayout::new(SizedNode::HorizontalStack(alignment.clone(), *spacing, resolved_children), result)
            }
            DrawCanvas(action) => {
                SizedLayout::new(
                    SizedNode::DrawCanvas(*action),
                    sizing::ItemSizing::new(
                        sizing::Sizing::Greedy(1),
                        sizing::Sizing::Greedy(1)
                    )
                )
            },
            WithContext(node) => {
                let node = node(context);

                node.resolve_size(bounds, context)
            }
        }
    }
}

impl<Ctx: Clone> Layout<Ctx> {
    pub fn text(content: &str) -> Layout<Ctx> {
        Layout::Text(content.to_string())
    }

    pub fn center(self) -> Layout<Ctx> {
        Layout::VCenter(Box::new(Layout::HCenter(Box::new(self))))
    }

    pub fn center_vertically(self) -> Layout<Ctx> {
        Layout::VCenter(Box::new(self))
    }

    pub fn center_horizontally(self) -> Layout<Ctx> {
        Layout::HCenter(Box::new(self))
    }

    pub fn width(self, n: usize) -> Layout<Ctx> {
        Layout::Width(n, Box::new(self))
    }
    
    pub fn height(self, n: usize) -> Layout<Ctx> {
        Layout::Height(n, Box::new(self))
    }
    
    pub fn padding_top(self, n: usize) -> Layout<Ctx> {
        Layout::TopPadding(n, Box::new(self))
    }
    
    pub fn padding_bottom(self, n: usize) -> Layout<Ctx> {
        Layout::BottomPadding(n, Box::new(self))
    }

    pub fn padding_left(self, n: usize) -> Layout<Ctx> {
        Layout::LeftPadding(n, Box::new(self))
    }
    
    pub fn padding_right(self, n: usize) -> Layout<Ctx> {
        Layout::RightPadding(n, Box::new(self))
    }

    pub fn padding_horizontal(self, n: usize) -> Layout<Ctx> {
        self.padding_left(n).padding_right(n)
    }

    pub fn padding_vertical(self, n: usize) -> Layout<Ctx> {
        self.padding_top(n).padding_bottom(n)
    }

    pub fn padding(self, n: usize) -> Layout<Ctx> {
        self
            .padding_top(n)
            .padding_right(n)
            .padding_bottom(n)
            .padding_left(n)
    }

    pub fn align_right(self) -> Layout<Ctx> {
        Layout::HRightAlign(Box::new(self))
    }

    pub fn align_left(self) -> Layout<Ctx> {
        Layout::HLeftAlign(Box::new(self))
    }

    pub fn align_top(self) -> Layout<Ctx> {
        Layout::VTopAlign(Box::new(self))
    }

    pub fn align_bottom(self) -> Layout<Ctx> {
        Layout::VBottomAlign(Box::new(self))
    }

    pub fn border(self, n: usize, c: char, edges: HashSet<alignment::Edge>) -> Layout<Ctx> {
        Layout::Border(n, c, edges, Box::new(self))
    }

    pub fn background(self, c: char) -> Layout<Ctx> {
        Layout::Background(c, Box::new(self))
    }

    pub fn vertical_stack(nodes: Vec<Layout<Ctx>>) -> Layout<Ctx> {
        Layout::VerticalStack(alignment::HorizontalAlignment::Center, 0, nodes)
    }
    
    pub fn horizontal_stack(nodes: Vec<Layout<Ctx>>) -> Layout<Ctx> {
        Layout::HorizontalStack(alignment::VerticalAlignment::Center, 0, nodes)
    }

    pub fn grid<State, Item: Clone>(items: &geometry::Matrix<Item>, spacing: usize, view: fn(&Item)->Layout<Ctx>) -> Layout<Ctx> {
        let mut rows = vec![];

        let mut row = vec![];
        let mut col_counter = 0;
        for item in items.data() {
            col_counter += 1;

            let cell = view(item).center();
            row.push(cell);

            if col_counter == items.shape().0 {
                rows.push(Layout::HorizontalStack(alignment::VerticalAlignment::Center, spacing, row));
                row = vec![];
                col_counter = 0;
            }
        }

        Layout::VerticalStack(alignment::HorizontalAlignment::Center, spacing, rows)
    }
}