rust-glass 0.4.0

GPU rendered Liquid Glass style UI framework with an immediate mode API
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
# Guide

How to build an app with rust-glass. Every Rust block on this page is compiled by `cargo test`.

- [Set up a project](#set-up-a-project)
- [The app loop](#the-app-loop)
- [Put things on screen](#put-things-on-screen)
- [Layout](#layout)
- [Scrolling and long lists](#scrolling-and-long-lists)
- [Drag and drop](#drag-and-drop)
- [Widgets](#widgets)
- [Menus, dialogs and windows](#menus-dialogs-and-windows)
- [Images and wallpaper](#images-and-wallpaper)
- [Theme](#theme)
- [Materials](#materials)
- [Custom painting and your own widgets](#custom-painting-and-your-own-widgets)
- [Animation and repainting](#animation-and-repainting)
- [Keyboard, clipboard and links](#keyboard-clipboard-and-links)
- [Accessibility](#accessibility)
- [Testing without a window](#testing-without-a-window)
- [Common problems](#common-problems)

## Set up a project

You need Rust 1.90 or newer and a GPU with Vulkan, DirectX 12 or Metal.

```bash
cargo new hello-glass
cd hello-glass
cargo add rust-glass
```

Replace `src/main.rs` with:

```rust,no_run
use rust_glass::prelude::*;

struct Hello {
    volume: f32,
    wifi: bool,
}

impl App for Hello {
    fn update(&mut self, ctx: &mut Context, _: &FrameInfo) {
        // Something colorful for the glass to bend.
        let screen = ctx.screen;
        ctx.background().gradient(screen.expand(64.0), 0.0, Color::INDIGO, Color::ORANGE, vec2(0.5, 0.5));

        let panel = Rect::from_center_size(screen.center(), vec2(360.0, 220.0));
        Panel::new("main", panel).padding(22.0).show(ctx, |ui| {
            ui.heading("Hello");
            ui.slider(&mut self.volume, 0.0..=1.0);
            ui.toggle(&mut self.wifi, "Wi-Fi");
            if ui.prominent_button("Reset").clicked() {
                self.volume = 0.5;
            }
        });
    }
}

fn main() -> Result<(), String> {
    let options = Options { title: "Hello".into(), size: (640.0, 420.0), ..Default::default() };
    run(options, Hello { volume: 0.3, wifi: true })
}
```

Run it with `cargo run --release`. For debug builds, let Cargo optimize the dependencies. Loading the system fonts and shaping text is slow without it:

```toml
[profile.dev.package."*"]
opt-level = 3
```

## The app loop

Implement `App` for your state and pass it to `run`.

- `setup(&mut self, ctx)` runs once before the first frame. Upload images and change the theme here.
- `update(&mut self, ctx, info)` runs for every frame. It reads input, changes your state and describes the whole screen. When the size of something had to be guessed, it runs again for the same frame without input.
- `file_dropped(&mut self, ctx, path)` runs when a file is dropped on the window.
- `close_requested(&mut self, ctx)` runs when the user closes the window. Return `false` to keep running, for example after `ctx.hide_window()` for an app that lives in the tray. `ctx.show_window()` brings it back.
- `window_created(&mut self, window)` runs once with the `winit` window, for platform calls rust-glass does not make itself. The crate re-exports `winit`.

The API is immediate mode: widgets take a reference to your data, draw it, and return a `Response`. There are no widget objects to keep around and no callbacks.

```rust
use rust_glass::prelude::*;

fn counter(ui: &mut Ui, n: &mut i32) {
    ui.label(&format!("Count: {n}"));
    if ui.button("Add one").clicked() {
        *n += 1;
    }
}
```

The fields of `Options` are `title`, `size` and `min_size` in points, `vsync`, `low_power`, which prefers the integrated GPU on machines with two, `visible` (false starts the app hidden), `icon` for the window and task bar, and `title_bar_follows_theme` and `title_bar_colors` for the title bar on Windows.

`FrameInfo` holds timings of recent frames (`ui_ms`, `encode_ms`, `gpu`, `interval_ms`) in case you want to show them. These are smoothed, so one slow frame barely moves them. `info.last` has the last frame as measured and `info.peak` the slowest values of the last two seconds, which is what to look at for a hitch. `gpu.gpu_ms` is `None` where the GPU cannot measure time. `gpu.redrawn_px` tells how much of the window the last frame drew again.

Set the environment variable `RUST_GLASS_TRACE` to a file name to have every frame written there, with its timings and the time from input to the frame on screen:

```text
t=46.676 frame ui 0.21 encode 0.70 gpu 1.45 interval 16.6 ms, 3 passes, 486 kpx redrawn, 486 kpx captured, press to present 4.3 ms
```

All lengths are in points. `ctx.screen` is the window, `ctx.pixels_per_point` the number of pixels per point.

## Put things on screen

`ctx` is the window. A `Ui` is a region that hands out space to widgets. You get a `Ui` from one of these:

- `region(ctx, id, rect, |ui| ...)`: a part of the window without a surface of its own. Use it for page content.
- `Panel::new(id, rect).show(ctx, |ui| ...)`: a floating glass panel. Use it for sidebars and toolbars.
- `Window::new(title).show(ctx, |ui| ...)`: a glass window the user can drag and fold.
- `popup`, `modal`, `sheet` and `context_menu`, covered below.

`TabBar::new(..).show_at(ctx, id, center)` places a floating tab bar directly.

Layers are stacked by `Order`: background, content, bars, windows, modals, popups, toasts, tooltips. Within one order, the layer that was clicked last is on top.

Glass shows whatever is drawn below it, so put content in a region and float controls above it. A typical screen is a scrolling page with bars on top:

```rust
use rust_glass::prelude::*;

#[derive(Default)]
struct Mail {
    tab: usize,
    folder: usize,
}

impl App for Mail {
    fn update(&mut self, ctx: &mut Context, _: &FrameInfo) {
        let screen = ctx.screen;
        let side = rect(14.0, 14.0, 220.0, screen.height() - 28.0);
        let main = Rect { min: vec2(side.max.x + 14.0, 0.0), max: screen.max };

        region(ctx, "page", main.shrink2(vec2(20.0, 0.0)), |ui| {
            // Content scrolls below the tab bar and ends above it.
            ScrollArea::new().insets(24.0, 110.0).show(ui, |ui| {
                ui.heading("Inbox");
                for i in 1..=30 {
                    ui.label(&format!("Message {i}"));
                    ui.separator();
                }
            });
        });

        Panel::new("side", side).show(ctx, |ui| {
            nav_list(ui, &mut self.folder, &[(Icon::Mail, "Inbox"), (Icon::Trash, "Trash")]);
        });

        let tabs = [(Icon::Mail, "Mail"), (Icon::Calendar, "Calendar"), (Icon::Search, "Search")];
        let center = vec2(main.center().x, screen.max.y - 60.0);
        TabBar::new(&mut self.tab, &tabs).show_at(ctx, Id::new("tabs"), center);
    }
}
```

A button placed on a glass panel draws as a plain fill, because glass on glass muddies both. For blocks of text or groups of controls inside a page, use `ui.card` instead of glass.

## Layout

A `Ui` places widgets from top to bottom. These change that:

```rust
use rust_glass::prelude::*;

fn layout(ui: &mut Ui) {
    // Side by side, centered vertically.
    ui.horizontal(|ui| {
        ui.button("One");
        ui.button("Two");
    });
    // A row in the middle, and one at the right edge.
    ui.horizontal_centered(|ui| ui.button("Middle"));
    ui.horizontal_right(|ui| ui.button("Right"));

    // Three columns of the same width.
    ui.grid(3, 16.0, |ui, column| {
        ui.label(&format!("Column {column}"));
    });

    // A column of fixed width inside a row.
    ui.horizontal(|ui| {
        ui.vertical(180.0, |ui| {
            ui.strong("Title");
            ui.secondary("Subtitle");
        });
        ui.button("Open");
    });

    ui.padded(vec2(20.0, 10.0), |ui| ui.label("Inset"));
    ui.card(|ui| ui.label("On a card"));
    section(ui, "Title", |ui| {
        ui.label("On a card under a title");
    });
    ui.space(12.0);
    ui.separator();

    // Gap between widgets, from here on in this region.
    ui.spacing = 6.0;
}
```

`ui.available_width()` and `ui.available_height()` tell how much room is left. `ui.in_rect(id, rect, |ui| ...)` ignores the flow and lays out in a rectangle you choose. `ui.align = Align::Center` centers the widgets of a vertical layout.

`ui.add_enabled(condition, |ui| ...)` dims everything inside and blocks input while the condition is false.

Most widgets get their identity from their position in the layout. If widgets come and go, give the changing part its own `in_rect` with a stable id, or the state of the widgets after it moves along (a running animation, keyboard focus, an open menu).

## Scrolling and long lists

`ScrollArea::new()` scrolls vertically, `ScrollArea::horizontal()` sideways and `ScrollArea::both()` on two axes. An area fills the remaining height unless you call `.height(h)`. Two scroll areas in one `Ui` need different ids.

```rust
use rust_glass::prelude::*;

fn scrolling(ui: &mut Ui, photos: &[String], rows: &[String]) {
    // A row of cards. Children sit side by side.
    ScrollArea::horizontal().id("photos").height(140.0).fade_sides(24.0, 24.0).show(ui, |ui| {
        for p in photos {
            ui.button(p);
        }
    });

    // A table wider and taller than the view.
    ScrollArea::both().id("table").height(300.0).content_width(1600.0).show(ui, |ui| {
        ui.label("Wide content");
    });

    // A long list: only the rows in view are laid out.
    ScrollArea::new().id("rows").height(400.0).show_rows(ui, 28.0, rows.len(), |ui, range| {
        for i in range {
            ui.label(&rows[i]);
        }
    });
}
```

The wheel scrolls the innermost area under the pointer that scrolls on that axis. A horizontal row inside a page leaves the vertical wheel to the page and takes sideways swipes. Shift with the wheel scrolls sideways.

`show_rows` takes the height of one row and the number of rows, and calls your closure with the range that is visible, plus one row on each side. Each row has to be exactly that tall. The cost of a frame then depends on the height of the view, not on the length of the list, so a hundred thousand rows scroll as smoothly as ten.

`.fade(top, bottom)` and `.fade_sides(left, right)` dissolve content at the edges, `.insets(top, bottom)` keeps room for floating bars, and `scroll_offset` and `set_scroll_offset` in `widgets::scroll` save and restore the position.

For a readable column in a wide window, give a vertical area the full width and `.content_width(w)`. The column is centered and the scroll bar stays at the edge of the window. `.bar_inset(d)` moves the bar away from the edge. Outside a scroll area, `ui.with_max_width(w, ...)` centers a column the same way.

A widget of your own that scrolls registers its rectangle with `ui.scroll_wheel(id, rect, x, y)`, which cuts it to the clip of the `Ui` and returns the wheel movement it receives.

## Drag and drop

`ui.drag_source(id, payload, |ui| ...)` makes its content draggable. A press lifts the content onto a glass plate and its place in the layout fades. Once the pointer moves a few points, the plate follows it. When the button is let go, the plate keeps the speed it had, flies on to the place of the item and sets down there; the item in the list stays hidden until it has landed. An item that can move from one list to another needs the same id in both, so it lands from where it was let go: give it one with `ui.drag_source_id(Id::new(key), payload, |ui| ...)`, which uses the id as it is. `ui.drop_zone::<P, _>(id, min_size, |ui| ...)` lights up while a payload of type `P` is over it and returns the payload in the frame it is dropped. Escape cancels a drag.

```rust
use rust_glass::prelude::*;

struct Board {
    todo: Vec<String>,
    done: Vec<String>,
}

fn board(ui: &mut Ui, b: &mut Board) {
    ui.horizontal(|ui| {
        ui.vertical(240.0, |ui| {
            for (i, task) in b.todo.iter().enumerate() {
                // The id must follow the item, not its position.
                ui.drag_source(("task", task.as_str()), i, |ui| {
                    ui.button(task);
                });
            }
        });
        let (_, dropped) = ui.drop_zone::<usize, _>("done", vec2(240.0, 200.0), |ui| {
            for task in &b.done {
                ui.label(task);
            }
        });
        if let Some(i) = dropped {
            b.done.push(b.todo.remove(i));
        }
    });
}

fn tasks(ui: &mut Ui, list: &mut Vec<String>) {
    // Drag items up and down to change the order. The others make room.
    ui.reorder("tasks", list, |ui, task| {
        ui.label(task);
    });
}
```

The payload can be any type that is `Clone` and `'static`. Zones only react to their own type, so a list of files and a list of colors on one screen do not mix. For your own drop targets, `ctx.dragged::<P>()`, `ctx.drag_over::<P>(rect)` and `ctx.take_drop::<P>(rect)` are the parts the zone is built from.

Files dropped on the window from outside arrive in `App::file_dropped`.

## Widgets

Most widgets have a short form on `Ui` and a builder with more options, added with `ui.add`.

```rust
use rust_glass::prelude::*;

#[derive(Default)]
struct Form {
    name: String,
    password: String,
    notes: String,
    query: String,
    volume: f32,
    steps: f32,
    size: f32,
    count: i32,
    wifi: bool,
    agree: bool,
    choice: usize,
    range: usize,
    quality: usize,
    tags: usize,
}

fn form(ui: &mut Ui, f: &mut Form, color: &mut Color) {
    // Text
    ui.heading("Heading");
    ui.subheading("Subheading");
    ui.label("Body text. Wraps at the width of the region.");
    ui.secondary("Less important");
    ui.caption("Small");

    // Buttons
    ui.horizontal(|ui| {
        ui.button("Regular");
        ui.prominent_button("Primary");
        ui.add(Button::new("Clear").clear());
        ui.add(Button::new("Plain").plain());
        ui.add(Button::new("Delete").icon(Icon::Trash).tinted(Color::RED));
        ui.add(Button::new("Large").large());
        ui.icon_button(Icon::Plus).on_hover_text(ui, "New item");
    });
    if let Some(i) = ButtonGroup::new(&[(Icon::Share, "Share"), (Icon::Copy, "Copy")]).show(ui) {
        println!("clicked {i}");
    }
    let drops = [(Icon::Camera, Color::ORANGE), (Icon::Photo, Color::GREEN)];
    if let Some(i) = SplitButton::new(Icon::Plus, &drops).show(ui) {
        println!("picked {i}");
    }

    // Values
    ui.slider(&mut f.volume, 0.0..=1.0);
    ui.add(Slider::new(&mut f.steps, 0.0..=8.0).step(1.0).width(300.0).tint(Color::PURPLE));
    ui.add(Stepper::new(&mut f.count, 0..=10));
    ui.add(DragValue::new(&mut f.size).range(0.0..=100.0).speed(0.5).suffix(" pt"));
    ui.color_picker(color);

    // Choices
    ui.toggle(&mut f.wifi, "Wi-Fi");
    ui.checkbox(&mut f.agree, "Agree");
    ui.radio(&mut f.choice, 0, "First");
    ui.radio(&mut f.choice, 1, "Second");
    ui.segmented(&mut f.range, &["Day", "Week", "Month"]);
    ui.add(Segmented::new(&mut f.tags, &["All", "Photos", "Links"]).fit());
    ComboBox::new(&mut f.quality, &["Low", "High"]).width(160.0).show(ui);

    // Text input
    ui.text_edit(&mut f.name, "Name");
    ui.search_field(&mut f.query, "Search");
    ui.add(TextEdit::new(&mut f.password).hint("Password").password());
    ui.add(TextEdit::new(&mut f.notes).hint("Notes").rows(4).width(400.0));

    // Status
    ui.progress(0.4);
    ui.add(ProgressBar::new(0.4).height(4.0).tint(Color::GREEN).track(Color::gray(0.8)));
    // The label shrinks until it fits inside the ring with the padding to spare.
    ui.add(Gauge::new(0.7, "Battery").size(80.0).thickness(8.0).label_padding(8.0));
    ui.spinner();

    // Grouping
    section(ui, "Network", |ui| {
        SettingsRow::new("Wi-Fi").icon(Icon::Wifi, Color::BLUE).show(ui, |ui| {
            ui.add(Toggle::new(&mut f.wifi));
        });
    });
    ui.collapsing("Details", |ui| {
        ui.label("Hidden until opened");
    });
    table(ui, &["Name", "Size"], &[vec!["readme".into(), "4 KB".into()]]);
}
```

A `Response` tells what happened: `clicked()`, `changed()`, `hovered()`, and fields such as `held`, `double_clicked`, `secondary_clicked`, `delta` and `rect`.

`Icon::ALL` lists the built in icons. They are vectors and stay sharp at any size.

## Menus, dialogs and windows

`MenuButton` turns into its menu when clicked. Items do not close it on their own, so close it when one was chosen:

```rust
use rust_glass::prelude::*;

fn options(ui: &mut Ui, sort: &mut usize) {
    let mut close = false;
    let menu = MenuButton::new("Options").icon(Icon::Sliders).show(ui, |ui| {
        for (i, name) in ["Sort by name", "Sort by date"].iter().enumerate() {
            if ui.add(MenuItem::new(name).checked(*sort == i)).clicked() {
                *sort = i;
                close = true;
            }
        }
        ui.separator();
        close |= ui.add(MenuItem::new("Delete").icon(Icon::Trash).destructive()).clicked();
    });
    if close {
        ui.ctx.set_popup(menu.id, false);
    }
}
```

The other overlays take a `&mut bool` that says whether they are open. Call them every frame, also while closed, so they can animate out:

```rust
use rust_glass::prelude::*;

#[derive(Default)]
struct Overlays {
    confirm: bool,
    details: bool,
    inspector: bool,
}

fn overlays(ctx: &mut Context, o: &mut Overlays) {
    // The last button is the primary one.
    let buttons = ["Cancel", "Delete"];
    let picked = alert(ctx, Id::new("confirm"), &mut o.confirm, "Delete file?", "It is gone for good.", &buttons);
    if picked == Some(1) {
        ctx.toast(Some(Icon::Check), "Deleted");
    }

    // Rises from the bottom edge and can be flicked away.
    sheet(ctx, Id::new("details"), &mut o.details, 460.0, 320.0, |ui| {
        ui.heading("Details");
    });

    Window::new("Inspector").pos(vec2(300.0, 120.0)).width(260.0).open(&mut o.inspector).show(ctx, |ui| {
        ui.label("Drag me by the title. Double click to fold.");
    });
}
```

`modal` is an `alert` with your own content. Escape closes menus, alerts, modals and sheets.

`context_menu(ui.ctx, &response, width, |ui| ...)` opens a menu on a right click on the widget that returned `response`. `response.on_hover_text(ui, "text")` shows a tooltip.

## Images and wallpaper

`ctx.add_image(width, height, rgba)` uploads 8 bit RGBA pixels and returns an `ImageId`. Upload each image once, for example in `setup`, and free it with `ctx.remove_image(id)` when it is no longer needed. rust-glass does not decode files. The `image` crate does.

```rust
use rust_glass::prelude::*;

struct Gallery {
    photo: ImageId,
}

impl App for Gallery {
    fn setup(&mut self, ctx: &mut Context) {
        let (w, h) = (64, 64);
        let pixels = (0..w * h).flat_map(|i| [(i % w * 4) as u8, (i / w * 4) as u8, 160, 255]).collect();
        self.photo = ctx.add_image(w, h, pixels);
    }

    fn update(&mut self, ctx: &mut Context, _: &FrameInfo) {
        // Wallpaper. The scene is a bit larger than the window, so cover that too.
        let whole = rect(0.0, 0.0, 1.0, 1.0);
        let screen = ctx.screen.expand(64.0);
        ctx.background().image(screen, 0.0, self.photo, whole, Color::WHITE);

        region(ctx, "page", ctx.screen.shrink(40.0), |ui| {
            ui.image(self.photo, vec2(160.0, 160.0), 18.0);
        });
    }
}
```

## Theme

`ctx.theme` is a plain struct. Change it in `setup` or at any time in `update`.

```rust
use rust_glass::prelude::*;

fn configure(ctx: &mut Context) {
    ctx.theme.dark = true;
    ctx.theme.accent = Color::PINK;
    ctx.theme.glass_tint = 0.2;
    ctx.theme.body_size = 15.0;
}
```

| Field | Default | Meaning |
| --- | --- | --- |
| `dark` | `false` | Dark content. Small glass elements decide for themselves from what is behind them. |
| `accent` | `Color::BLUE` | Prominent buttons, links, focus rings. |
| `glass_tint` | `0.35` | 0 is the clearest glass, 1 is fully tinted. Also sets how clear the lifted lenses are. |
| `adaptive` | `true` | Small glass switches between light and dark with its backdrop. |
| `reduce_transparency` | `false` | Denser surfaces. |
| `reduce_motion` | `false` | No overshoot. |
| `increase_contrast` | `false` | Stronger edges. |
| `regular`, `clear`, `thick`, `lens`, `blob`, `blob_labels` | presets | Materials used by the widgets. |
| `control_height`, `spacing` | `36`, `10` | Sizes in points. |
| `body_size`, `caption_size`, `title_size` | `14`, `12`, `24` | Font sizes. |

Text uses the interface font of the platform. `ctx.text.add_font(bytes)` registers a font file, and `ctx.text.sans = "Family".into()` switches to it.

## Materials

A `Material` holds every parameter of the glass. Presets: `REGULAR` for controls and bars, `CLEAR` for controls over photos and video, `THICK` for menus and panels, `LENS` for lifted thumbs, `BLOB` for the tab bar selection.

Start from a preset and change what you need. The Material Lab page of the showcase edits most of this live.

```rust
use rust_glass::prelude::*;

const ICE: Material = Material { blur: 1.5, refraction: 30.0, depth: 22.0, ..Material::CLEAR };

fn toolbar(ctx: &mut Context) {
    let r = rect(40.0, 40.0, 300.0, 60.0);
    Panel::new("bar", r).corner(Corner::Capsule).material(ICE).adaptive().show(ctx, |ui| {
        ui.label("Over a photo");
    });

    // Colored glass. The alpha is the strength.
    let tint = Material::REGULAR.tinted(Color::TEAL.alpha(0.6));
    Panel::new("teal", rect(40.0, 120.0, 300.0, 60.0)).material(tint).show(ctx, |ui| {
        ui.label("Teal");
    });

    // Used by every widget from now on.
    ctx.theme.regular.dispersion = 0.2;
}
```

Parameters that change the look the most:

- `refraction` and `depth`: how far the edge bends light, and how wide the bent zone is.
- `dispersion`: color fringes, as a fraction of the bend.
- `blur`: blur radius of the backdrop.
- `light_range` and `dark_range`: the brightness range the backdrop is squeezed into. A narrow range near white keeps dark text legible over anything.
- `rim`, `dark_edge`, `inner_glow`: light on the edge.
- `shadow`, `shadow_radius`, `shadow_tint`: the shadow, and how much color it carries.
- `glass_tint_response`: how much the `glass_tint` setting applies. Set it to 0 for a lens that sits above its own label, or the label turns milky.
- `liquidity`: how liquid the glass feels in motion, from 0 (rigid) to 1. Thumbs read it from `theme.lens`, the tab bar from `theme.blob`. Liquid glass stretches more as it speeds up and slows down, and wobbles longer before it settles.

A press lights the glass where it touches, the way iOS does. Buttons get it through `PressEffect::apply`; for your own glass, set `glass.glow = Some((position, strength))` or call `.glow(position, strength)`.

`.adaptive()` lets a panel switch between light and dark with its backdrop. Keep that for small panels.

## Custom painting and your own widgets

`ui.painter()` draws into the current region. A widget is a struct with a `ui` method:

```rust
use rust_glass::prelude::*;

struct Stars<'a> {
    rating: &'a mut usize,
}

impl Widget for Stars<'_> {
    fn ui(self, ui: &mut Ui) -> Response {
        let mut r = ui.allocate_response(vec2(150.0, 30.0), Sense::Click);
        if r.clicked()
            && let Some(p) = r.pointer
        {
            *self.rating = (((p.x - r.rect.min.x) / 30.0) as usize + 1).min(5);
            r.changed = true;
        }
        let off = ui.surface.strong_fill_color();
        let mut p = ui.painter();
        for i in 0..5 {
            let center = vec2(r.rect.min.x + 15.0 + 30.0 * i as f32, r.rect.center().y);
            let color = if i < *self.rating { Color::YELLOW } else { off };
            p.icon(center, 24.0, Icon::Star, color);
        }
        r
    }
}

fn rate(ui: &mut Ui, rating: &mut usize) {
    if ui.add(Stars { rating }).changed() {
        ui.ctx.toast(None, format!("{rating} stars"));
    }
}
```

The painter has `fill`, `gradient`, `stroke`, `shadow`, `line`, `arc`, `image`, `icon`, `text`, `text_centered`, `glass` and `blur`. `blur` softens what is already painted in a rectangle, for content that comes into focus as it appears. `ui.surface` gives the text and fill colors that fit what the widget is drawn on.

For a glass element of your own, `ui.glass` measures the backdrop and returns the surface to pick the label color from:

```rust
use rust_glass::prelude::*;

fn badge(ui: &mut Ui, text: &str) {
    let layout = ui.layout_text(text, &TextStyle::new(13.0).weight(Weight::Semibold));
    let r = ui.allocate(layout.size + vec2(28.0, 14.0));
    let id = ui.next_id();
    let material = ui.ctx.theme.regular;
    let surface = ui.glass(id, r, Corner::Capsule, material, true);
    ui.painter().text_centered(r, &layout, surface.text_color(), 1.0);
}

// Two shapes in one element fuse like drops.
fn drops(ui: &mut Ui, gap: f32) {
    let r = ui.allocate(vec2(200.0, 60.0));
    let left = GlassShape::new(rect(r.min.x, r.min.y, 60.0, 60.0), Corner::Capsule);
    let right = GlassShape::new(rect(r.min.x + 60.0 + gap, r.min.y, 60.0, 60.0), Corner::Capsule);
    let mut glass = Glass::group(vec![left, right], ui.ctx.theme.regular).fuse(20.0);
    let id = ui.next_id();
    ui.ctx.prepare_glass(id, &mut glass, true);
    ui.painter().glass(&glass);
}
```

`ctx.get`, `ctx.try_get` and `ctx.set` store small state for a widget under its id and type. State of a widget that is no longer drawn is dropped after a while, so keep anything that matters in your own struct.

## Animation and repainting

The window draws only when there is input or something asked for another frame. `ctx.animate` stores a spring under an id and asks for frames until it settles:

```rust
use rust_glass::prelude::*;

fn bar(ui: &mut Ui, open: bool) {
    let id = ui.make_id("bar");
    let width = ui.ctx.animate(id, if open { 240.0 } else { 80.0 }, Tuning::PRESS);
    let r = ui.allocate(vec2(240.0, 40.0));
    let r = Rect::from_min_size(r.min, vec2(width, r.height()));
    let accent = ui.ctx.theme.accent;
    ui.painter().fill(r, Corner::Capsule, accent);
}
```

Tunings: `SNAP` (no overshoot), `SMOOTH`, `PRESS` and `BOUNCY`, or `Tuning::new(response, damping)`, with the same meaning as a SwiftUI spring.

A spring stops, and the window goes idle, once the rest of its motion stays within `Spring::PRECISION` (0.002) of the target: half a step of an 8 bit color for opacity, a few thousandths of a pixel for a position in points. A spring you keep yourself whose units are larger, such as a speed in points per second, can stop sooner with `step_within`:

```rust
use rust_glass::prelude::*;

fn settle_speed(speed: &mut Spring, dt: f32) -> bool {
    // A tenth of a point per second makes no visible difference.
    speed.step_within(0.0, Tuning::SMOOTH, dt, 0.1)
}
```

`ctx.morph(key, view, rect)` moves an element from one view to another, such as a cover that grows from a grid into a detail page. Call it from whichever view draws the element, with the same key, and draw the element at the rectangle it returns. While the view stays the same the result is `rect`, so scrolling and layout changes follow at once. When the target moves during the trip, the element moves along with it. `ctx.is_morphing(key)` is true while the element is on its way, for example to draw it above the rest of the page.

Call `ctx.request_repaint()` in every frame while something else moves, such as a video. `ctx.dt` is the time since the last frame in seconds and `ctx.time` the time since start.

Something that changes now and then, such as a level meter, a clock or a blinking caret, asks for one frame later with `ctx.request_repaint_after(duration)`. The window sleeps until then. A background thread wakes the window with a `Waker`:

```rust
use rust_glass::prelude::*;
use std::time::Duration;

fn meter(ui: &mut Ui, level: f32) {
    ui.progress(level);
    // 25 readings a second, and no frames in between.
    ui.ctx.request_repaint_after(Duration::from_millis(40));
}

fn start_download(ctx: &Context) {
    let waker = ctx.waker();
    std::thread::spawn(move || {
        // ... the work ...
        if let Some(w) = waker {
            w.wake();
        }
    });
}
```

Only the part of the window that changed is drawn again. Glass keeps the backdrop it captured as long as nothing under it changed, so a meter that moves next to glass redraws the meter and the glass that sees it, not the window. `RUST_GLASS_FULL_REDRAW=1` draws every frame in full, for comparison.

Large blurred glass, such as a sidebar, a sheet or a page that blurs as it appears, draws its smooth inside at half resolution and scales it up. Only the edge, where the glass bends and catches the light, is drawn at full size. The picture stays the same within one step of 8 bit color. `RUST_GLASS_NO_HALF=1` turns it off.

On a GPU that cannot keep up, a window lowers the quality of its glass by itself, and raises it again once there is room. `ctx.quality` sets how far it may go:

```rust
use rust_glass::prelude::*;

fn configure(ctx: &mut Context) {
    // Never simplify, whatever the GPU.
    ctx.quality = Quality::Full;
}

fn status(ui: &mut Ui, info: &FrameInfo) {
    if info.gpu.quality != Quality::Full {
        ui.caption("Glass simplified to keep up");
    }
}
```

| Value | Meaning |
| --- | --- |
| `Auto` | The default for windows. Full quality, one step lower after three large frames over `Quality::AUTO_BUDGET_MS` (14 ms) of GPU time, and back up after 120 frames with plenty of room. Needs a GPU that can time frames. |
| `Full` | Always full quality. `Headless` starts with it. |
| `Reduced` | Glass casts no shadow, so it covers only its body. On integrated GPUs glass costs by the pixels it covers, so this saves the most: about 12 percent of a 4K frame on Intel UHD Graphics, and a fifth of opening a menu or a sheet. |
| `Low` | Also no rainbow edges, no light spilled onto edges, and plain filtered blur. |

`info.gpu.quality` is the step a frame was drawn with.

Widgets that select, such as the rows of `nav_list`, react when the button goes down (`Sense::Press`), not when it is released. When input changes state that was already drawn earlier in the same frame, for example the page next to a sidebar drawn before it, call `ctx.request_relayout()`: the frame runs again before it is shown. `nav_list` does that for you.

To have a page ready before it is shown, draw it inside `ctx.hidden(...)` with the same ids as when it is shown. Text is shaped and widget state is kept, but nothing is painted, nothing reacts and nothing asks for frames:

```rust
use rust_glass::prelude::*;

fn settings_page(ui: &mut Ui) {
    ui.heading("Settings");
}

fn warm_up(ctx: &mut Context, area: Rect) {
    ctx.hidden(|ctx| region(ctx, "settings", area, settings_page));
}
```

The state of widgets that are not drawn is kept for five minutes and 1200 frames, whichever is later (`ctx.set_retention`). Shaped text is kept for 30 seconds and 240 frames (`ctx.text.set_retention`). A window that draws all the time therefore keeps a page it left a moment ago, scroll position included.

## Keyboard, clipboard and links

Tab and Shift+Tab move the focus, and scroll areas scroll to show the focused widget. Space and Enter press, arrow keys move sliders, segments and tabs.

```rust
use rust_glass::input::Key;
use rust_glass::prelude::*;

fn shortcuts(ctx: &mut Context, saved: &mut bool) {
    // Control on Windows and Linux, Command on macOS.
    if ctx.input.modifiers.command && ctx.input.take_key(Key::Char('s')) {
        *saved = true;
        ctx.toast(Some(Icon::Check), "Saved");
    }
}

fn links(ui: &mut Ui) {
    ui.hyperlink("Source code", "https://github.com/MasterKPG/rust-glass");
    if ui.button("Copy").clicked() {
        ui.ctx.copy_text("text for the clipboard");
    }
}

fn quit(ctx: &mut Context) {
    // Handled by the window after this frame.
    ctx.host_commands.push(rust_glass::context::HostCommand::Close);
}
```

`take_key` consumes the press, so a widget drawn later does not see it. `ctx.input` also has the pointer position, buttons, scroll and typed text.

Text fields undo with Ctrl+Z and redo with Ctrl+Shift+Z or Ctrl+Y (Command on macOS). Typing undoes a word at a time, while paste, cut and deleting a selection are steps of their own. When the app changes the text itself, the history of that field starts over.

Input methods for Chinese, Japanese, Korean and other scripts work in every text field. The text being composed shows underlined at the caret, and the candidate window of the system opens next to it.

## Accessibility

Screen readers see every built in widget with its role, name, value and state, and can operate it: press buttons, flip switches, move sliders, pick tabs and segments, and set the text of fields. The tree goes to the platform through [AccessKit](https://accesskit.dev). It is built only while a screen reader listens, so an app without one pays nothing.

It has been checked with Windows UI Automation. VoiceOver on macOS and Orca on Linux have not been tried yet.

Widgets without text of their own, such as a slider, need a name:

```rust
use rust_glass::prelude::*;

fn volume(ui: &mut Ui, v: &mut f32) {
    let r = ui.slider(v, 0.0..=1.0);
    ui.ctx.access_label(r.id, "Volume");
}
```

`SettingsRow` names the controls at its end after its title, so a settings list needs nothing extra. Your own widgets describe themselves with `ui.access`, which returns `None` while no screen reader listens:

```rust
use rust_glass::accessibility::Role;
use rust_glass::prelude::*;

fn star(ui: &mut Ui, on: &mut bool) {
    let r = ui.allocate_response(vec2(28.0, 28.0), Sense::Click);
    if r.clicked {
        *on = !*on;
    }
    if let Some(n) = ui.access(r.id, Role::CheckBox, r.rect) {
        n.label("Favorite").toggled(*on);
    }
    ui.ctx.focusable(r.id);
    ui.painter().icon(r.rect.center(), 20.0, Icon::Star, Color::YELLOW.fade(if *on { 1.0 } else { 0.3 }));
}
```

A click from a screen reader arrives through `Response::clicked` like any other. Steps on sliders arrive as arrow keys. New text for a field arrives through `ctx.take_access_text(id)`, which `TextEdit` handles. In tests, `ctx.set_accessibility(true)` turns the tree on, `ctx.access_nodes()` lists what was described and `Event::Access` sends a request.

## Testing without a window

`Headless` runs an app with scripted input and fixed time steps, and returns pixels. It needs a GPU but no window:

```rust,no_run
use rust_glass::headless::Headless;
use rust_glass::input::Event;
use rust_glass::prelude::*;

fn screenshot(app: &mut impl App) -> Result<Vec<u8>, String> {
    let mut h = Headless::new(1280, 800, 1.0)?;
    app.setup(&mut h.ctx);
    h.settle(app, 2.0);
    h.step(app, 1.0 / 60.0, &[Event::PointerMoved(vec2(100.0, 100.0))]);
    h.step(app, 1.0 / 60.0, &[Event::PointerDown]);
    h.step(app, 1.0 / 60.0, &[Event::PointerUp]);
    h.settle(app, 2.0);
    Ok(h.pixels()) // RGBA, 1280 * 800 * 4 bytes
}
```

`h.set_full_redraw(true)` draws every frame in full, so `h.info.gpu` times the whole window rather than the part that changed. `Headless` draws at `Quality::Full`, so renders are the same on every machine; set `h.ctx.quality` to render another step, and `h.set_half_resolution(false)` to draw large blurred glass at full size everywhere.

For logic only, drive a `Context` with `begin_frame` and `end_frame`. That needs no GPU. [tests/interaction.rs](https://github.com/MasterKPG/rust-glass/blob/main/crates/rust-glass/tests/interaction.rs) does it for the built in widgets.

## Common problems

**The glass looks like a flat gray sheet.** There is nothing behind it. Put an image, a gradient or scrolling content below.

**Glass near the window border looks wrong.** The scene is larger than the window, so glass at the border has something to bend. Extend the wallpaper with `ctx.screen.expand(64.0)`.

**A menu stays open after a click on an item.** Close it with `ui.ctx.set_popup(menu.id, false)`.

**Two panels share their state.** Their ids are equal. Ids of panels, regions and overlays have to be unique in the window. Windows use their title.

**A dialog disappears without an animation.** It is called only while open. Call it every frame and let the `bool` decide.

**An animation stops halfway.** Nothing asks for the next frame. Use `ctx.animate` or call `ctx.request_repaint()`.

**The page shows the old selection for one frame after a click.** The page is drawn before the control that changed it. Call `ctx.request_relayout()` when the selection changes, or draw the control first.

**Something lingers on screen that should be gone.** Run with `RUST_GLASS_FULL_REDRAW=1`. If that fixes it, the part of the window drawn again missed it; please report it with the steps.

**Glass lost its shadow on a slow machine.** The window stepped down its quality to keep up. Set `ctx.quality = Quality::Full` to keep the full look at any frame rate.

**A screen reader reads a control without a name.** It has no text of its own. Name it with `ui.ctx.access_label(response.id, "Name")`.

**A dragged item jumps back to another one.** Its `drag_source` id comes from its position. Use something that belongs to the item, such as its name or key.

**`run` returns an error about the adapter or the surface.** The driver offers none of Vulkan, DirectX 12 and Metal, or the window cannot be drawn to. Update the GPU driver.