guido 0.12.0

A reactive Rust GUI library using wgpu for Wayland layer shell widgets
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
# Layout

Guido uses a flexbox-style layout system for arranging widgets. The `Flex` layout handles rows and columns with spacing and alignment options.

![Flex Layout](../images/flex_layout.png)

## Basic Layout

### Row (Horizontal)

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container()
    .layout(Flex::row())
    .children([
        text("Left"),
        text("Center"),
        text("Right"),
    ])
# ;
# }
```

### Column (Vertical)

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container()
    .layout(Flex::column())
    .children([
        text("Top"),
        text("Middle"),
        text("Bottom"),
    ])
# ;
# }
```

## Spacing

Add space between children:

```rust,ignore
# // not compiled: `[...]` stands in for the children; this block is about
# // the layout above them.
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container()
    .layout(Flex::row().spacing(8.0))
    .children([...])
# ;
# }
```

## Main Axis Alignment

Control distribution along the layout direction:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
Flex::row().main_alignment(MainAlignment::Center)
# ;
# }
```

### Options

| Alignment | Description |
|-----------|-------------|
| `Start` | Pack at the beginning |
| `Center` | Center in available space |
| `End` | Pack at the end |
| `SpaceBetween` | Equal space between, none at edges |
| `SpaceAround` | Equal space around each item |
| `SpaceEvenly` | Equal space including edges |

The three spacing alignments share out the space the flex is given. Along a
scrolled axis there is no limit and so nothing to share: the children sit
together as with `Start`, and the content is as long as they are.

A child that is hidden, or half a pixel or less along the flex's direction,
keeps no space around it. When no child keeps space, `SpaceAround` and
`SpaceEvenly` centre what they hold, and `SpaceBetween` places what it
holds at the start.

### Visual Examples

```text
Start:        [A][B][C]
Center:          [A][B][C]
End:                      [A][B][C]
SpaceBetween: [A]      [B]      [C]
SpaceAround:   [A]    [B]    [C]
SpaceEvenly:    [A]   [B]   [C]
```

## Cross Axis Alignment

Control alignment perpendicular to the layout direction:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
Flex::row().cross_alignment(CrossAlignment::Center)
# ;
# }
```

### Options

| Alignment | Description |
|-----------|-------------|
| `Start` | Align to start of cross axis |
| `Center` | Center on cross axis |
| `End` | Align to end of cross axis |
| `Stretch` | Stretch to fill cross axis |
| `Baseline` | Line children up on the baseline of their text |

### Baseline

Text of different sizes in a row does not read as one line when the tops are
aligned — a 24px label and a 12px one float at unrelated heights. `Baseline`
puts them on the line they are written on:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container()
    .layout(Flex::row().spacing(8.0).cross_alignment(CrossAlignment::Baseline))
    .children([
        container().child(text("28").font_size(24.0)),
        container().child(text("°C").font_size(12.0)),
    ])
# ;
# }
```

A box reports no baseline of its own, so it is aligned by its bottom edge,
which is what CSS does with it. A container takes the baseline of its
content, so wrapping a text for styling does not lose the alignment. The row
is deep enough for what the alignment moves down: a label pushed down beside
a tall icon keeps its descenders inside the row. In a column there is no
shared line to sit on, and `Baseline` behaves as `Start`.

### Visual Example (Row)

```text
Start:    ┌───┐┌─┐┌──┐
          │ A ││B││ C│
          └───┘│ │└──┘
               └─┘

Center:        ┌─┐
          ┌───┐│B│┌──┐
          │ A │└─┘│ C│
          └───┘   └──┘

End:           ┌─┐
               │B│
          ┌───┐└─┘┌──┐
          │ A │   │ C│
          └───┘   └──┘

Stretch:  ┌───┐┌─┐┌──┐
          │   ││ ││  │
          │ A ││B││ C│
          │   ││ ││  │
          └───┘└─┘└──┘
```

## Centring

`center()` centres the children on both axes. It is shorthand for
`main_alignment(MainAlignment::Center)` together with
`cross_alignment(CrossAlignment::Center)`, and composes with the rest:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container()
    .layout(Flex::column().spacing(4.0).center())
    .children([text("Locked"), text("Type your password")])
# ;
# }
```

## Complete Example

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container()
    .layout(
        Flex::row()
            .spacing(16.0)
            .main_alignment(MainAlignment::SpaceBetween)
            .cross_alignment(CrossAlignment::Center)
    )
    .padding(20.0)
    .children([
        container().child(text("Left").font_size(24.0)),
        container()
            .layout(Flex::column().spacing(4.0))
            .children([
                text("Center"),
                text("Items"),
            ]),
        container().child(text("Right").font_size(24.0)),
    ])
# ;
# }
```

## Nested Layouts

Combine rows and columns for complex layouts:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main_content() -> Container { container() }
# fn sidebar() -> Container { container() }
# fn main() {
container()
    .layout(Flex::column().spacing(16.0))
    .children([
        // Header row
        container()
            .layout(Flex::row().main_alignment(MainAlignment::SpaceBetween))
            .children([
                text("Logo"),
                text("Menu"),
            ]),
        // Content row
        container()
            .layout(Flex::row().spacing(16.0))
            .children([
                sidebar(),
                main_content(),
            ]),
        // Footer row
        container()
            .layout(Flex::row().main_alignment(MainAlignment::Center))
            .child(text("Footer")),
    ])
# ;
# }
```

## Size Constraints

Control how children size within layouts:

### Fixed Size

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container()
    .width(200.0)
    .height(100.0)
# ;
# }
```

### Minimum/Maximum

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container().width(at_least(100.0).at_most(300.0))
# ;
# }
```

### At Least

Request at least a certain size:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container()
    .width(at_least(200.0))  // At least 200px, can grow
# ;
# }
```

A minimum is a floor, never a cap, as CSS `min-width` is: content wider than
200 makes the container wider. A child that takes everything it is offered —
a `fill()` child, or a text input — takes everything the container is offered
too, so a field meant to be a set width says so with an exact length, or
bounds it with `at_least(200.0).at_most(400.0)`.

### Fill Available Space

Make a container expand to fill all available space:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container()
    .height(fill())  // Fills available height
    .width(fill())   // Fills available width
# ;
# }
```

This is particularly useful for root containers that should fill their surface, or for creating layouts where children are centered within the full available space:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
container()
    .height(fill())
    .layout(Flex::row().center())
    .child(text("Centered in available space"))
# ;
# }
```

Along a scrolled axis there is no end to fill to, so `fill()` there is the
container's content size. A scroller that should give it the visible area says
so with `Scroll::vertical().fill_viewport(true)` — see the Container chapter.

## Children That Do Not Fit

When a row's children are wider than the row, the children that can be
narrower give the room up, in proportion to their widths — CSS's
`flex-shrink`. None goes below its minimum:

- a container with an exact `width` keeps it, however deep it sits — an icon
  inside a padded button is not squashed by a long title beside the button
- a text that ellipsizes can go down to the `…`, one that wraps to its widest
  word, and one that does neither keeps its whole width
- a container that clips (`Overflow::Hidden`) or scrolls can go down to
  nothing, or to the minimum it declares with `at_least`; any other container
  goes no lower than what it holds, plus its padding

A child held at its minimum leaves the rest of the overflow to the others.
When even the minimums do not fit, the row runs past its end, as it does when
nothing in it can shrink. A row that fits is laid out at its children's natural
sizes, exactly as before. Columns follow the same rule along their height:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn header() -> Container { container().height(30.0) }
# fn rows() -> Container { container() }
# fn main() {
// The list takes what the header leaves, rather than the column's whole height.
container()
    .height(300.0)
    .layout(Flex::column())
    .child(header())
    .child(container().scroll(Scroll::vertical()).child(rows()))
# ;
# }
```

`fill()` children are not part of this: they take what is left once the others
are laid out, and nothing is left when the others overflow.

## Layout Without Explicit Flex

Containers without `.layout()` use `Flex::column()`:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
// Same as .layout(Flex::column())
container()
    .children([
        text("first"),
        text("second"),
    ])
# ;
# }
```

## Stacking Children (ZStack)

`ZStack` places every child at the same position, stacked along the Z axis —
later children paint on top, and take the pointer first. A layer that draws
where the pointer is — a fill, a border, a blur — or listens there covers what
is beneath it, even a translucent scrim; one that only lays out its content
lets the pointer through:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
# fn background() -> Container { container() }
# fn content() -> Container { container() }
container()
    .layout(ZStack::new())
    .child(background())
    .child(content())   // drawn on top
# ;
# }
```

### Leaders and Followers

The stack takes the size of its largest child, but a child that declares
`fill()` on an axis **follows** the stack instead of leading it: it never
contributes to that axis and is laid out against the size the other children
established.

That is how a decoration is sized to its sibling without measuring anything:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn card_content() -> Container { container() }
# fn main() {
container()
    .layout(ZStack::new())
    // Follower: exactly as wide and tall as the card below it
    .child(
        container()
            .width(fill())
            .height(fill())
            .background(Color::rgb(0.2, 0.2, 0.3))
    )
    // Leader: the stack is as big as this card
    .child(card_content())
# ;
# }
```

Without this rule the background would expand to all the available space,
and sizing it to its sibling would need a [WidgetRef](../advanced/widget-ref.md)
round-trip — one frame behind, and zero on the first frame.

A hidden child leads nothing, just as it takes no space in a flex: while an overlay
beside a `fill()` background is hidden, the stack is sized as if it were not
there.

The two axes are decided independently, which is what makes the status-bar
case work: a child with only `.height(fill())` fills the bar height and still
leads the width.

```rust
# extern crate guido;
# use guido::prelude::*;
# fn now_playing() -> Container { container() }
# fn visualizer() -> Container { container() }
# fn main() {
container()
    .height(fill())
    .layout(ZStack::new())
    // Follows both axes
    .child(container().width(fill()).height(fill()).child(visualizer()))
    // Fills the bar height, leads the width
    .child(container().height(fill()).child(now_playing()))
# ;
# }
```

When *every* child fills an axis there is no size to follow, and the stack
takes all the space it was offered on that axis — or, along a scrolled axis,
where there is no end to the space, the size of its largest child.

### Positioning Inside the Stack

Every child sits at the stack's origin. To place a follower elsewhere, let it
fill both axes and use its own layout:

```rust
# extern crate guido;
# use guido::prelude::*;
# fn icon_widget() -> Container { container() }
# fn main() {
container()
    .layout(ZStack::new())
    .child(icon_widget())
    // Alert dot pinned to the icon's top-right corner
    .child(
        container()
            .width(fill())
            .height(fill())
            .layout(Flex::row().main_alignment(MainAlignment::End))
            .child(
                container()
                    .width(4)
                    .height(4)
                    .corners(2)
                    .background(Color::RED)
            )
    )
# ;
# }
```

## API Reference

### Flex Builder

```text
Flex::row() -> Flex;                    // Horizontal layout
Flex::column() -> Flex                 // Vertical layout
.spacing(f32) -> Flex                  // Space between children
.main_alignment(MainAlignment) -> Flex
.cross_alignment(CrossAlignment) -> Flex
```

### MainAlignment

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
MainAlignment::Start;
MainAlignment::Center;
MainAlignment::End;
MainAlignment::SpaceBetween;
MainAlignment::SpaceAround;
MainAlignment::SpaceEvenly
# ;
# }
```

### CrossAlignment

```rust
# extern crate guido;
# use guido::prelude::*;
# fn main() {
CrossAlignment::Start;
CrossAlignment::Center;
CrossAlignment::End;
CrossAlignment::Stretch
# ;
# }
```

### ZStack

```rust,ignore
# // not compiled: a signature listing — these declarations have no bodies.
# extern crate guido;
# use guido::prelude::*;
# fn main() {
ZStack::new() -> ZStack                // Children stacked at the same origin
# ;
# }
```