Skip to main content

gallery/
gallery.rs

1//! A window using every part of the library: menus, built-in parts, text, a list, a table, a
2//! panel, a dialog, and custom drawing.
3//!   cargo run -p taut --example gallery
4#![windows_subsystem = "windows"]
5use taut::{
6    Align, Animation, AppFonts, Cap, CellEditor, CellValue, Chart, ChartView, CodeEditor, Color, Column, ContrastColors, Date, Dock, Drawing, Field, FileDialog, Flow, FontFile, Icon, Inspector, Join,
7    Key, Length, Markdown, Mods, Paint, Panel, Path, Pen, Picture, Point, Role, SecondWindow, Selection, Series, Severity, Span, Syntax, Table, Theme, Time, Transform, TreeItem, TreeView, Ui, Window,
8};
9
10const PAGES: [&str; 9] = ["List", "Table", "Parts", "Drawing", "More", "Dock", "Familiar", "Charts", "Writing"];
11/// How far apart things are.
12const SPACINGS: [&str; 3] = ["Dense", "Usual", "Roomy"];
13/// What the box that commands are found in lists: a page to go to, and two of the menus' rows.
14const COMMANDS: [&str; 11] = [
15    "Go to the list",
16    "Go to the table",
17    "Go to the parts",
18    "Go to the drawing",
19    "Go to more parts",
20    "Go to the dock",
21    "Go to the familiar parts",
22    "Go to the charts",
23    "Go to the writing",
24    "Light colours, or dark",
25    "Rename this window\u{2026}\tF2",
26];
27/// The kinds of file in the table, which a cell of its third column is selected from.
28const KINDS: [&str; 4] = ["text", "picture", "sound", "folder"];
29const COLUMNS: [Column; 4] = [
30    Column::new("Name").px(240.0).editable(CellEditor::Text),
31    Column::new("Size").px(90.0).align(Align::End).editable(CellEditor::Number),
32    Column::new("Kind").px(110.0).editable(CellEditor::Choice(&KINDS)),
33    Column::new("Note").px(560.0).unsorted(),
34];
35const DAYS: [&str; 7] = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
36
37const PLUS: Icon = Icon::line("M8 3.5v9M3.5 8h9");
38const MORE: Icon = Icon::solid("M3.5 6.8a1.2 1.2 0 1 1 0 2.4 1.2 1.2 0 0 1 0-2.4zM8 6.8a1.2 1.2 0 1 1 0 2.4 1.2 1.2 0 0 1 0-2.4zM12.5 6.8a1.2 1.2 0 1 1 0 2.4 1.2 1.2 0 0 1 0-2.4z");
39const STAR: Icon = Icon::line("M8 2.2 9.8 5.9 13.8 6.4 10.9 9.2 11.6 13.2 8 11.3 4.4 13.2 5.1 9.2 2.2 6.4 6.2 5.9z");
40const GRID: Icon = Icon::line("M3 3h4v4H3zM9 3h4v4H9zM3 9h4v4H3zM9 9h4v4H9z");
41const PAGE: Icon = Icon::line("M4.5 2.5h4.5l2.5 2.5v8.5h-7zM9 2.5V5h2.5M6 8h4M6 10.5h4");
42const BELL: Icon = Icon::line("M8 2.5a3.5 3.5 0 0 1 3.5 3.5v2.5l1.2 2H3.3l1.2-2V6A3.5 3.5 0 0 1 8 2.5zM6.6 12.5a1.5 1.5 0 0 0 2.8 0");
43const SYNC: Icon = Icon::line("M2.8 7.2A5.3 5.3 0 0 1 12 4.6l1.2 1.2M13.2 8.8A5.3 5.3 0 0 1 4 11.4l-1.2-1.2M13.4 2.9v3h-3M2.6 13.1v-3h3");
44const DROP: Icon = Icon::line("M8 2.2c2.6 3 4.2 5.3 4.2 7.3a4.2 4.2 0 0 1-8.4 0c0-2 1.6-4.3 4.2-7.3z");
45const COPY: Icon = Icon::line("M5.5 5.5h7v7h-7zM10.5 5.5v-2h-7v7h2");
46/// The panels of the dock, and which of them the person may close.
47const PANELS: [(&str, bool); 4] = [("Files", false), ("Editor", false), ("Preview", true), ("Log", true)];
48
49/// Names enough to want to type one, not look for it.
50const COLOURS: [&str; 40] = [
51    "Amber",
52    "Apricot",
53    "Aquamarine",
54    "Azure",
55    "Beige",
56    "Black",
57    "Blue",
58    "Bronze",
59    "Brown",
60    "Burgundy",
61    "Cerulean",
62    "Charcoal",
63    "Cobalt",
64    "Coral",
65    "Crimson",
66    "Cyan",
67    "Emerald",
68    "Gold",
69    "Green",
70    "Grey",
71    "Indigo",
72    "Ivory",
73    "Jade",
74    "Khaki",
75    "Lavender",
76    "Lilac",
77    "Magenta",
78    "Maroon",
79    "Navy blue",
80    "Ochre",
81    "Olive",
82    "Orange",
83    "Pink",
84    "Purple",
85    "Red",
86    "Scarlet",
87    "Teal",
88    "Turquoise",
89    "Violet",
90    "White",
91];
92
93/// A lamp of the More page's own, shown in an inspector, as a tool shows what is selected.
94#[derive(Clone)]
95pub struct Lamp {
96    name: String,
97    power: f32,
98    on: bool,
99    kind: LampKind,
100    tint: Color,
101    rooms: Vec<String>,
102}
103
104/// The kinds of lamp there are.
105#[derive(Clone, Copy, PartialEq)]
106pub enum LampKind {
107    Spot,
108    Flood,
109    Strip,
110}
111taut::inspect!(enum LampKind { Spot: "Spot", Flood: "Flood", Strip: "Strip" });
112taut::inspect!(Lamp { name: "Name", power: Field::new("Power").unit("W").range(0.0, 2000.0), on: "On", kind: "Kind", tint: "Colour", rooms: "Rooms" });
113
114pub struct App {
115    name: String,
116    presses: u32,
117    /// Which page the tabs show.
118    page: usize,
119    /// How much room a row of the list gets: its number among the choices.
120    room: usize,
121    filter: String,
122    notes: String,
123    /// The dialog for a new name is open, with this in its box.
124    renaming: Option<String>,
125    // The page of parts:
126    light: bool,
127    /// High contrast; or the look the system has, in place of the app's own; and how far
128    /// apart things are: their number among `SPACINGS`.
129    contrast: bool,
130    system: bool,
131    spacing: usize,
132    ticked: bool,
133    on: bool,
134    folded_open: bool,
135    second: bool,
136    level: f32,
137    bold: bool,
138    starred: bool,
139    span: usize,
140    busy: bool,
141    from: f32,
142    to: f32,
143    followed: u32,
144    /// The box that commands are found in is up.
145    finding: bool,
146    colour: usize,
147    wanted: String,
148    // The page of drawing:
149    picture: Option<Picture>,
150    photo: Option<Picture>,
151    drawings: Option<(Drawing, Drawing)>,
152    animation: Option<Animation>,
153    /// The moving picture is asked to play.
154    playing: bool,
155    /// A font file to write everything with, when the program was started with one.
156    letters: Option<FontFile>,
157    place: String,
158    /// Which of the shapes drawn on the Drawing page was clicked last.
159    shape: Option<usize>,
160    // The page of more parts:
161    selected_row: usize,
162    tree_open: [bool; 2],
163    amount: f32,
164    off: bool,
165    count: i32,
166    day: Date,
167    /// A time, a day and a time of it, and a span of days.
168    alarm: Time,
169    meeting: (Date, Time),
170    away: (Date, Date),
171    /// A slider that stops at steps, and one that goes by a curve.
172    marked: f32,
173    zoom: f32,
174    /// The colour that stands out, which the person selects.
175    tint: Color,
176    /// Two boxes of things that are carried from one to the other.
177    drag_lists: [Vec<&'static str>; 2],
178    /// A number dragged within a range, a fruit selected by name, a group drawn faint, and a
179    /// button hidden that keeps its room.
180    within: f32,
181    fruit: &'static str,
182    faint: bool,
183    hide: bool,
184    // The page with the table: its files by name, size and kind.
185    table: Table,
186    selection: Selection,
187    files: Vec<(String, u32, &'static str)>,
188    /// The panel that floats over the page, and the window beside the first.
189    tools: Panel,
190    /// A box to edit code in, with this program's own source in it.
191    code: CodeEditor,
192    /// A lamp, and what its inspector keeps: how wide the names are, what is folded.
193    lamp: Lamp,
194    inspector: Inspector,
195    beside: SecondWindow,
196    /// The dialog that selects a file.
197    file_dialog: FileDialog,
198    /// How the panels of the last page lie, and the tree of files in the first of them.
199    dock: Dock,
200    // The page of charts: the pie's and the scatter's views, the scatter's dots, and the
201    // live chart (its last minute of values, and when the last came).
202    slices: ChartView,
203    view: ChartView,
204    dots: [Vec<f32>; 4],
205    live: bool,
206    load: Vec<f32>,
207    fed: f64,
208    folders: TreeView,
209    // The page of parts known from web pages and phone apps:
210    step: usize,
211    results: usize,
212    query: String,
213    topics: Vec<String>,
214    topic: String,
215    stars: f32,
216    /// A picture of a person, made the first time it is shown.
217    face: Option<Picture>,
218    liked: [bool; 2],
219    loading: bool,
220    banner: bool,
221    styles: [bool; 3],
222    /// The section that is open, of those of which one is open at a time.
223    section: Option<usize>,
224    // The page of writing: a document written in Markdown, read once.
225    writing: Markdown,
226}
227
228pub fn describe(app: &mut App, ui: &mut Ui) {
229    ui.theme = theme(app, ui);
230    if let Some(letters) = app.letters.take() {
231        ui.set_text_system(Box::new(AppFonts::new(letters)));
232    }
233    menu_bar(app, ui);
234    shortcuts(app, ui);
235    top_bar(app, ui);
236    pages(app, ui);
237    tools_panel(app, ui);
238    second_window(app, ui);
239    rename_dialog(app, ui);
240    // Any of the commands, found by typing a part of its name: the pages first.
241    match ui.command_palette(&mut app.finding, &COMMANDS) {
242        // (And said in a corner for a few seconds, where it went.)
243        Some(page) if page < PAGES.len() => (app.page, _) = (page, ui.notice(&format!("This is the page \u{201c}{}\u{201d}", PAGES[page]))),
244        Some(9) => app.light = !app.light,
245        Some(_) => app.renaming = Some(app.name.clone()),
246        None => {}
247    }
248    // The file that is selected gives the window its name.
249    if let Some(file) = ui.file_dialog(&mut app.file_dialog) {
250        app.name = file.file_name().map_or_else(String::new, |name| name.to_string_lossy().into_owned());
251    }
252}
253
254/// The menus across the top. (After a "&" comes the letter that selects a menu or a row from
255/// the keyboard: Alt+F opens File, and there R renames.)
256fn menu_bar(app: &mut App, ui: &mut Ui) {
257    ui.menu_bar();
258    if ui.menu_button("&File") {
259        if ui.menu_item("&Rename this window…\tF2").clicked {
260            app.renaming = Some(app.name.clone());
261        }
262        if ui.submenu("&Go to page") {
263            for (number, name) in PAGES.iter().enumerate() {
264                if ui.menu_item(name).clicked {
265                    app.page = number;
266                }
267            }
268            ui.close();
269        }
270        if ui.menu_item("&Open a file…").clicked {
271            app.file_dialog.pick(std::env::home_dir().unwrap_or_else(|| ".".into()));
272        }
273        ui.separator();
274        if ui.menu_item("&Close the window").clicked {
275            ui.quit();
276        }
277        ui.close();
278    }
279    if ui.menu_button("&View") {
280        ui.menu_checkbox(&mut app.light, "&Light colours");
281        ui.menu_checkbox(&mut app.tools.open, "The &Tools panel");
282        ui.menu_checkbox(&mut app.beside.open, "A &second window");
283        app.finding |= ui.menu_item("&Find a command\u{2026}\tCtrl+K").clicked;
284        // (The window over all of its screen, and back: ticked while it is.)
285        let mut whole = ui.full_screen();
286        if ui.menu_checkbox(&mut whole, "&Over all of the screen\tF11").changed {
287            ui.set_full_screen(whole);
288        }
289        // One of several, with a dot by the one that is selected; and the dock's panels that
290        // may be closed, ticked while they are open.
291        ui.separator();
292        ui.menu_radio(&mut app.room, 0, "Roo&my rows");
293        ui.menu_radio(&mut app.room, 1, "Tig&ht rows");
294        ui.separator();
295        for (name, _) in PANELS.iter().filter(|(_, closes)| *closes) {
296            let mut open = app.dock.is_open(name);
297            if ui.menu_checkbox(&mut open, name).changed {
298                app.dock.set_open(name, open);
299            }
300        }
301        ui.separator();
302        ui.menu_checkbox(&mut app.contrast, "High contrast");
303        ui.menu_checkbox(&mut app.system, "As the system has it");
304        if ui.submenu("Spacing") {
305            for (number, name) in SPACINGS.iter().enumerate() {
306                app.spacing = if ui.menu_checkbox(&mut (app.spacing == number), name).clicked { number } else { app.spacing };
307            }
308            ui.close();
309        }
310        ui.close();
311    }
312    ui.close();
313}
314
315/// The keys, written as the menus show them.
316fn shortcuts(app: &mut App, ui: &mut Ui) {
317    if ui.keys("F2") {
318        app.renaming = Some(app.name.clone());
319    }
320    if ui.keys("F11") {
321        let whole = ui.full_screen();
322        ui.set_full_screen(!whole);
323    }
324    app.finding |= ui.keys("Ctrl+K");
325    if ui.keys("Ctrl+N") {
326        ui.notice("An item was added");
327    }
328    app.starred ^= ui.keys("Ctrl+D");
329}
330
331/// The bar under the menus: words, then a tool bar in the room that is left. A tool's hint
332/// shows its keys; what does not fit goes into the menu at the end of the tool bar.
333fn top_bar(app: &mut App, ui: &mut Ui) {
334    let t = ui.theme;
335    ui.row().pad((10.0, 8.0)).gap(8.0).fill(t.panel).open();
336    ui.label(&format!("{}: 60 items, {} presses counted", app.name, app.presses));
337    tool_bar(app, ui);
338    ui.el().w(Length::Px(170.0)).open();
339    ui.text_box(&mut app.filter, "Filter items");
340    ui.close();
341    ui.select(&mut app.room, &["Roomy", "Tight"]);
342    if ui.button("Start over").clicked {
343        app.presses = 0;
344        ui.announce("Counting from nothing again");
345    }
346    app.presses += ui.primary_button("Count one").clicked as u32;
347    let more = ui.icon_button(MORE, "More");
348    if more.clicked {
349        ui.open_popup(more.id, more.rect);
350    }
351    if ui.menu(more.id) {
352        if ui.menu_item("Rename this window…").clicked {
353            app.renaming = Some(app.name.clone());
354        }
355        app.light ^= ui.menu_item(if app.light { "Dark colours" } else { "Light colours" }).clicked;
356        ui.separator();
357        if ui.menu_item("Copy the notes").clicked {
358            ui.copy(&app.notes);
359        }
360        if ui.menu_item("Close the window").clicked {
361            ui.quit();
362        }
363        ui.close();
364    }
365    ui.close();
366    ui.separator();
367}
368
369/// The pages, selected by tabs.
370fn pages(app: &mut App, ui: &mut Ui) {
371    ui.row().pad((10.0, 4.0, 10.0, 0.0)).open();
372    ui.tabs(&mut app.page, &PAGES);
373    ui.close();
374    ui.separator();
375    match app.page {
376        0 => list(app, ui),
377        1 => table(app, ui),
378        2 => parts(app, ui),
379        3 => drawing(app, ui),
380        4 => more_parts(app, ui),
381        5 => docks(app, ui),
382        6 => web_widgets(app, ui),
383        7 => charts(app, ui),
384        _ => writing(app, ui),
385    }
386}
387
388/// The tools in the bar under the menus: in two groups, one of which stays down while it
389/// is on; and one whose box comes out of it with an arrow, a popover with a colour to pick.
390fn tool_bar(app: &mut App, ui: &mut Ui) {
391    let bar = ui.tool_bar();
392    if ui.tool(PLUS, "Add an item\tCtrl+N").clicked {
393        ui.notice("An item was added");
394    }
395    let tint = ui.tool(DROP, "The colour that stands out");
396    ui.tool_toggle(&mut app.starred, STAR, "Starred\tCtrl+D");
397    ui.tool_group();
398    if ui.tool(COPY, "Copy the notes").clicked {
399        ui.copy(&app.notes);
400    }
401    if ui.tool(SYNC, "Start over").clicked {
402        app.presses = 0;
403    }
404    ui.tool(GRID, "A tool that does nothing");
405    ui.close();
406    // (Opened from the overflow menu, it points at the bar.)
407    if tint.clicked {
408        ui.open_popup(tint.id, if tint.rect.is_empty() { bar.rect } else { tint.rect });
409    }
410    if ui.popover(tint.id) {
411        ui.label("The colour that stands out");
412        ui.color_picker(&mut app.tint);
413        ui.close();
414    }
415}
416
417/// A table of five thousand files: headings that sort, columns that are dragged wider, rows
418/// that are selected one or many at a time, by the pointer or the keys, and cells that are
419/// changed in place. Only the rows in view are described. It is wider than the window, and
420/// scrolled sideways it keeps its first column in view.
421fn table(app: &mut App, ui: &mut Ui) {
422    let t = ui.theme;
423    ui.row().pad((14.0, 8.0)).gap(12.0).open();
424    // (The words wrap in what the tick box leaves.)
425    let summary = format!(
426        "{} files, {} picked. Click a heading to sort by it and drag its edge; the arrows, Control and Shift pick rows; F2 or two clicks change a cell; Control+C copies, Control+V pastes from a spreadsheet, Control+Z undoes.",
427        app.files.len(),
428        app.selection.len()
429    );
430    ui.el().w(Length::Grow(1.0)).text(&summary, t.small_font, t.text_dim).wrap().show();
431    let mut freeze_first = app.table.frozen > 0;
432    if ui.checkbox(&mut freeze_first, "Keep the first column in view").changed {
433        app.table.frozen = freeze_first as usize;
434    }
435    ui.close();
436    let table = ui.table(&mut app.table, &COLUMNS);
437    if table.changed {
438        sort(app);
439    }
440    // Screen readers are told every picked row, in view or not.
441    ui.tell_selection(&app.selection);
442    // Cells the person changed, pasted, or undid: the app puts them into its own files. (It
443    // says what a cell shows, for undoing.)
444    let files = &app.files;
445    let shown = |row: usize, column: usize| [files[row].0.clone(), format!("{} KB", files[row].1), files[row].2.to_string()][column].clone();
446    for change in ui.cell_changes(&table, &COLUMNS, shown) {
447        let file = &mut app.files[change.row];
448        match change.value {
449            CellValue::Text(name) => file.0 = name,
450            CellValue::Number(size) => file.1 = size.clamp(0.0, u32::MAX as f64) as u32,
451            CellValue::Choice(kind) => file.2 = KINDS[kind],
452        }
453    }
454    // Control+C on the table: the selected rows, as text a spreadsheet takes.
455    let (files, selection) = (&app.files, &app.selection);
456    ui.copy_rows(&table, selection.iter().map(|row| [files[row].0.clone(), files[row].1.to_string(), files[row].2.to_string()]));
457    for row in ui.rows(app.files.len(), 24.0) {
458        if ui.selectable_row(row, app.selection.contains(row)).clicked {
459            app.selection.click(row, ui.mods());
460        }
461        let (name, size, kind) = &app.files[row];
462        ui.cell(name);
463        ui.cell(&format!("{size} KB"));
464        ui.cell(kind);
465        ui.note("what does not fit its column is cut off there");
466        ui.close();
467    }
468    ui.close();
469    ui.close();
470}
471
472/// Puts the files in the order the table's headings ask for.
473fn sort(app: &mut App) {
474    let by = app.table.sort.clone();
475    let one = |a: &(String, u32, &str), b: &(String, u32, &str), column: usize| match column {
476        0 => a.0.cmp(&b.0),
477        1 => a.1.cmp(&b.1),
478        _ => a.2.cmp(b.2),
479    };
480    let order = |a: &(String, u32, &str), b: &(String, u32, &str)| by.iter().map(|(column, down)| if *down { one(b, a, *column) } else { one(a, b, *column) }).find(|way| way.is_ne());
481    app.files.sort_by(|a, b| order(a, b).unwrap_or(std::cmp::Ordering::Equal));
482    app.selection.clear();
483}
484
485/// A window of its own beside the first: described here like everything else, and it works
486/// on the same things. Its own close button closes it, and so does its button.
487fn second_window(app: &mut App, ui: &mut Ui) {
488    if !ui.window(&mut app.beside, "taut gallery: a second window") {
489        return;
490    }
491    ui.column().pad(14.0).gap(10.0).open();
492    dim_text(ui, "A second window. What is set here is set in the first one too.");
493    ui.switch(&mut app.light, "Light colours");
494    ui.slider(&mut app.level, 50.0..=100.0);
495    ui.text_box(&mut app.name, "The name of the first window");
496    if ui.button("Close this window").clicked {
497        ui.quit();
498    }
499    ui.close();
500    ui.close();
501}
502
503/// A panel that floats over every page: the person moves it, stretches it, folds it, closes
504/// it (the View menu brings it back).
505fn tools_panel(app: &mut App, ui: &mut Ui) {
506    if !ui.panel(&mut app.tools, "Tools") {
507        return;
508    }
509    dim_text(ui, "Moved by its title, stretched by its edges.");
510    ui.switch(&mut app.light, "Light colours");
511    ui.row().gap(10.0).open();
512    ui.label("The colour that stands out");
513    ui.color_picker(&mut app.tint);
514    ui.close();
515    ui.slider(&mut app.level, 50.0..=100.0);
516    ui.close();
517}
518
519fn list(app: &mut App, ui: &mut Ui) {
520    let (t, room) = (ui.theme, if app.room == 0 { 5.0 } else { 1.0 });
521    ui.column().grow().scroll().pad(10.0).gap(6.0).open();
522    ui.checkbox(&mut app.ticked, "A tick box, which the keyboard can reach with Tab and flip with Space");
523    ui.paragraph("A paragraph breaks into as many lines as the width of the window needs. Under it is a box for several lines of text, then a list that scrolls.");
524    // Spelling checked in the person's language; a word written twice is marked too.
525    ui.check_spelling(None);
526    ui.text_area(&mut app.notes, "Notes", (2, 6));
527    // In groups of ten, each under a heading that stays at the top while its rows scroll
528    // by under it, until the next heading pushes it up.
529    for i in (0..60).filter(|i| format!("item {i}").contains(app.filter.as_str())) {
530        if i % 10 == 0 || !format!("item {}", i - 1).contains(app.filter.as_str()) {
531            ui.row().key(("heading", i / 10)).pad((4.0, 4.0)).fill(t.bg).sticky().open();
532            ui.el().text(&format!("Items {} to {}", i / 10 * 10, i / 10 * 10 + 9), t.small_font, t.text_dim).show();
533            ui.close();
534        }
535        ui.row().key(i).gap(8.0).pad((10.0, room)).fill(t.panel).radius(6.0).open();
536        ui.icon(PAGE);
537        ui.label(&format!("item {i}"));
538        ui.note("a note");
539        ui.spacer();
540        ui.note(&format!("{} KB", 2 + i * 7 % 40));
541        ui.icon_button(MORE, "A button that is only an icon says what it does here");
542        ui.close();
543    }
544    ui.close();
545}
546
547fn parts(app: &mut App, ui: &mut Ui) {
548    let t = ui.theme;
549    ui.column().grow().scroll().pad(14.0).gap(10.0).open();
550    ui.switch(&mut app.light, "Light colours");
551    ui.switch(&mut app.on, "A switch slides over when it is flipped");
552    if ui.fold(&mut app.folded_open, "A heading that folds away what is under it") {
553        ui.radio(&mut app.second, false, "One choice");
554        ui.radio(&mut app.second, true, "Or the other, never both");
555        ui.row().gap(10.0).open();
556        ui.label("A slider");
557        ui.el().w(Length::Px(220.0)).open();
558        ui.slider(&mut app.level, 50.0..=100.0);
559        ui.close();
560        ui.label(&format!("at {:.0}", app.level));
561        ui.close();
562        ui.progress((app.level - 50.0) / 50.0);
563    }
564    ui.separator();
565    // Buttons that stay down, choices in one frame, and a ring that turns while it is asked to.
566    ui.row().gap(10.0).open();
567    ui.toggle(&mut app.bold, "Bold");
568    ui.icon_toggle(&mut app.starred, STAR, "A button that stays down while it is on");
569    ui.segmented(&mut app.span, &["Day", "Week", "Month"]);
570    ui.switch(&mut app.busy, "Busy");
571    if app.busy {
572        ui.spinner();
573    }
574    ui.close();
575    ui.row().gap(10.0).open();
576    ui.label("A span");
577    ui.el().w(Length::Px(220.0)).open();
578    ui.range(&mut app.from, &mut app.to, 0.0..=100.0);
579    ui.close();
580    ui.label(&format!("from {:.0} to {:.0}", app.from, app.to));
581    ui.close();
582    // Words of several kinds that break into lines as one paragraph, with a link among them.
583    ui.wrapping_row().w(Length::Px(420.0)).open();
584    ui.words("Words of ", t.font, t.text);
585    ui.words("several kinds ", if app.bold { t.font.bold() } else { t.font }, t.accent);
586    ui.words("break into lines together, and a ", t.font, t.text);
587    app.followed += ui.link("link").clicked as u32;
588    ui.words(&format!(" among them is pressed: {} times so far.", app.followed), t.font, t.text_dim);
589    ui.close();
590    // A row that goes on to the next line when its room runs out: 6 between its tags, and
591    // 10 between its lines.
592    ui.wrapping_row().w(Length::Px(420.0)).gap(6.0).line_gap(10.0).open();
593    for tag in ["layout", "drawing", "text", "tables", "menus", "docks", "scenes", "screen readers", "fonts", "pictures"] {
594        ui.el().text(tag, t.small_font, t.text_dim).pad((8.0, 3.0)).fill(t.raised).border(t.line, 1.0).radius(10.0).show();
595    }
596    ui.close();
597    // Picture letters in colour, among any others: each one character to the cursor.
598    ui.label("Emoji in colour, in any text: \u{1F642} \u{1F44D}\u{1F3FD} \u{1F469}\u{200D}\u{1F680} \u{2764}\u{FE0F} 2\u{FE0F}\u{20E3}");
599    // One of a long list, found by typing; and a box with what it might become listed under it.
600    ui.row().gap(10.0).open();
601    ui.el().w(Length::Px(200.0)).open();
602    ui.combo(&mut app.colour, &COLOURS);
603    ui.close();
604    ui.el().w(Length::Px(200.0)).open();
605    ui.autocomplete(&mut app.wanted, "Any colour, by name", &COLOURS);
606    ui.close();
607    ui.close();
608    // How the window looks: high contrast, or as the person's system has it; and how far
609    // apart things are.
610    ui.switch(&mut app.contrast, "High contrast");
611    ui.switch(&mut app.system, "As the system has it");
612    dim_text(ui, &system_preferences(ui));
613    ui.segmented(&mut app.spacing, &SPACINGS);
614    ui.close();
615}
616
617/// What an app draws itself: shapes, a picture; and boxes laid out as a grid.
618fn drawing(app: &mut App, ui: &mut Ui) {
619    let t = ui.theme;
620    ui.column().grow().scroll().pad(14.0).gap(12.0).open();
621    ui.note("A chart drawn by the app: a filled shape with a line over it. The slider on the Parts page changes it.");
622    wave_chart(app, ui);
623    shapes(app, ui);
624
625    ui.note("Fills that go from one colour into others: across, down, along any line, outwards. And text too long for its box.");
626    ui.row().gap(12.0).open();
627    ui.el().size(120.0, 28.0).radius(6.0).gradient_across(t.accent, t.green).show();
628    ui.el().size(120.0, 28.0).radius(6.0).gradient(t.raised, t.accent.with_alpha(0.6)).show();
629    ui.el().size(120.0, 28.0).radius(6.0).gradient_along(60.0, &[(0.0, t.red), (0.5, t.amber), (1.0, t.green)]).show();
630    ui.el().size(28.0, 28.0).radius(14.0).gradient_around(&[(0.0, t.text), (0.6, t.accent), (1.0, t.accent.with_alpha(0.0))]).show();
631    ui.el().w(Length::Px(180.0)).clip().text("Text that is too long for its box ends in dots", t.font, t.text).show();
632    ui.close();
633    ui.note("Drawings read from SVG files, sharp at any size: one in its own colours, and an icon of lines in the colour it is given. And a moving picture, read from a GIF file, which plays while it is asked to.");
634    if app.drawings.is_none() {
635        app.drawings = ui.add_drawing(include_str!("../tests/pictures/logo.svg")).zip(ui.add_drawing(include_str!("../tests/pictures/lines.svg")));
636        app.animation = ui.add_animation(include_bytes!("../tests/pictures/moving.gif"));
637    }
638    ui.row().gap(16.0).open();
639    if let Some((logo, lines)) = app.drawings {
640        ui.el().drawing(logo, t.text).size(135.0, 90.0).show();
641        ui.el().drawing(lines, t.text).size(16.0, 16.0).show();
642        ui.el().drawing(lines, t.accent).size(32.0, 32.0).show();
643        ui.el().drawing(lines, t.text).size(64.0, 64.0).show();
644    }
645    // (It moves while it is asked to: a window in which nothing is asked for is at rest.)
646    ui.toggle(&mut app.playing, "Play");
647    if let Some(animation) = app.animation.filter(|_| app.playing) {
648        ui.el().animation(animation).size(96.0, 64.0).show();
649    }
650    ui.close();
651    ui.note("A picture made of pixels, shown larger than it is; and a grid, whose columns line up.");
652    // Sixteen by sixteen pixels: red grows to the right, green downwards.
653    let picture = *app.picture.get_or_insert_with(|| ui.add_picture(16, (0..256u32).map(|i| 0xff00_0080 | (i % 16 * 17) << 16 | (i / 16 * 17) << 8).collect()));
654    // And one read from a JPEG file that is built into the program.
655    let photo = app.photo.or_else(|| ui.add_picture_file(include_bytes!("../tests/pictures/plain.jpg")));
656    app.photo = photo;
657    ui.row().gap(16.0).align(Align::Start, Align::Start).open();
658    ui.el().picture(picture).size(96.0, 96.0).show();
659    if let Some(photo) = photo {
660        ui.el().picture(photo).size(134.0, 90.0).show();
661    }
662    ui.grid(2).w(Length::Px(340.0)).gap(8.0).align(Align::Start, Align::Center).open();
663    ui.label("Name");
664    ui.text_box(&mut app.name, "Name");
665    ui.label("Where it is kept");
666    ui.text_box(&mut app.place, "A folder");
667    ui.label("Rows");
668    ui.select(&mut app.room, &["Roomy", "Tight"]);
669    ui.close();
670    ui.close();
671    ui.close();
672}
673
674/// A wave drawn by the app as a line and as the area under it: as many waves as the slider
675/// on the Parts page says.
676fn wave_chart(app: &App, ui: &mut Ui) {
677    let t = ui.theme;
678    let (wide, high, waves) = (600.0, 150.0, (app.level - 40.0) / 10.0);
679    let points: Vec<(f32, f32)> = (0..=120)
680        .map(|step| {
681            let x = step as f32 * wide / 120.0;
682            (x, high / 2.0 - (x / wide * waves * std::f32::consts::TAU).sin() * (high / 2.0 - 12.0) * (1.0 - x / wide * 0.6))
683        })
684        .collect();
685    // A smooth curve through the points, not straight lines from one to the next.
686    let (mut line, mut area) = (Path::new(), Path::new());
687    line.smooth_through(&points);
688    area.move_to(0.0, high).smooth_through(&points).line_to(wide, high).close();
689    ui.el().flow(Flow::Stack).size(wide, high).fill(t.panel).radius(6.0).clip().open();
690    ui.el().size(wide, high).shape(&area, t.accent.with_alpha(0.2)).show();
691    ui.el().size(wide, high).outline(&line, 2.0, t.accent).show();
692    ui.el().size(10.0, 10.0).radius(5.0).fill(t.amber).offset(wide - 30.0, 14.0).show();
693    ui.close();
694}
695
696/// Lines as people draw them, shapes put together, turned and filled with colours across
697/// them, and a click that selects the shape it falls on.
698fn shapes(app: &mut App, ui: &mut Ui) {
699    let t = ui.theme;
700    ui.note("Lines under pens of every kind: ends cut off, square or round, corners to a point, cut across or round, dashes and dots. Below, shapes of the app's own: a pie, a star the slider on the Parts page turns, a ring, a box with corners of their own, and a line between them with an arrow at its end. A click picks the shape it falls on.");
701    let pens = [
702        Pen::new(5.0).cap(Cap::Flat).join(Join::Mitre(4.0)),
703        Pen::new(5.0).cap(Cap::Square).join(Join::Bevel),
704        Pen::new(5.0),
705        Pen::new(3.0).cap(Cap::Flat).join(Join::Mitre(4.0)).dashes(&[8.0, 4.0], 0.0),
706        Pen::new(4.0).dashes(&[0.0, 9.0], 0.0),
707    ];
708    let zigzag = Path::new().move_to(6.0, 34.0).line_to(26.0, 8.0).line_to(46.0, 34.0).line_to(66.0, 8.0).clone();
709    ui.row().gap(12.0).open();
710    for (nth, pen) in pens.into_iter().enumerate() {
711        ui.el().size(72.0, 42.0).outline(&zigzag, pen, Color::series(nth, t.bg)).show();
712    }
713    ui.close();
714    // The shapes, each in units from the corner of the box they are drawn in.
715    let mut drawn: Vec<(Path, Paint)> = Vec::new();
716    let slices = [0.4, 0.25, 0.2, 0.15];
717    let mut from = -90.0;
718    for share in slices {
719        drawn.push((Path::new().move_to(80.0, 80.0).arc(80.0, 80.0, 60.0, from, share * 360.0).close().clone(), Paint::Color(Color::series(drawn.len(), t.panel))));
720        from += share * 360.0;
721    }
722    let turn = Transform::new().rotate(app.level * 3.6).translate(240.0, 80.0);
723    let star = Path::new().star(0.0, 0.0, 55.0, 22.0, 5).transform(turn).clone();
724    let (along, around) = ([(0.0, t.accent), (1.0, t.green)], [(0.0, t.amber), (1.0, t.red)]);
725    drawn.push((star, Paint::Linear(90.0, &along)));
726    drawn.push((Path::new().circle(390.0, 80.0, 50.0).add(Path::new().circle(390.0, 80.0, 24.0).reverse()).clone(), Paint::Radial(&around)));
727    drawn.push((Path::new().rounded_rect(470.0, 30.0, 110.0, 100.0, (28.0, 4.0, 28.0, 4.0)).clone(), Paint::Color(t.raised)));
728    let wire = Path::new().move_to(140.0, 145.0).cubic_to(260.0, 175.0, 420.0, 120.0, 520.0, 140.0).clone();
729    let (end, way) = wire.point_at(wire.length()).unwrap_or((Point::default(), 0.0));
730    let arrow = Path::svg("M0 0-12-6-12 6Z").transform(Transform::new().rotate(way).translate(end.x, end.y)).clone();
731    // A click selects the shape it falls in, or the line it falls near: the last drawn first.
732    let (wide, high) = (600.0, 170.0);
733    let area = ui.el().flow(Flow::Stack).size(wide, high).fill(t.panel).radius(6.0).clip().sense().open();
734    if let Some(at) = ui.pointer().filter(|_| area.clicked) {
735        let at = Point { x: at.x - area.rect.x, y: at.y - area.rect.y };
736        let on_wire = wire.distance(at) <= 6.0;
737        app.shape = if on_wire { Some(drawn.len()) } else { drawn.iter().rposition(|(shape, _)| shape.contains(at)) };
738    }
739    for (shape, ink) in &drawn {
740        ui.el().size(wide, high).shape(shape, *ink).show();
741    }
742    let selected = app.shape == Some(drawn.len());
743    ui.el().size(wide, high).outline(&wire, if selected { 4.0 } else { 2.0 }, t.text_dim).show();
744    ui.el().size(wide, high).shape(&arrow, t.text_dim).show();
745    // The shape selected: a dashed line round it.
746    if let Some((shape, _)) = app.shape.and_then(|nth| drawn.get(nth)) {
747        ui.el().size(wide, high).outline(shape, Pen::new(2.0).cap(Cap::Flat).dashes(&[6.0, 4.0], 0.0), t.text).show();
748    }
749    ui.close();
750}
751
752/// Panels that share the page: side by side, one over the other, and behind tabs. A tab is
753/// carried to another place and let go on one of the marks there; let go anywhere else, its
754/// panel floats. Two of them may be closed (the View menu opens them again).
755fn docks(app: &mut App, ui: &mut Ui) {
756    ui.dock(&mut app.dock);
757    for (name, closes) in PANELS {
758        let shows = if closes { ui.docked_closable(&mut app.dock, name) } else { ui.docked(&mut app.dock, name) };
759        if !shows {
760            continue;
761        }
762        if name == "Files" {
763            folders(app, ui);
764        } else {
765            ui.label(name);
766            dim_text(
767                ui,
768                "Carry a tab along its strip to put it elsewhere among the tabs, or away to another place, and let it go on a mark. Let go anywhere else, the panel floats; a double press on a tab does that too, and one on the title of a panel that floats puts it back.",
769            );
770            dim_text(ui, "The button at the end of the tabs has the panel fill the dock. Control+Tab goes to the next tab, Control+F6 to the next place.");
771            ui.button("Something to press");
772        }
773        ui.close();
774    }
775    ui.close();
776}
777
778/// A tree of 111,000 files in folders. What is under a folder is handed over when it is
779/// opened, and only the rows in view are described.
780fn folders(app: &mut App, ui: &mut Ui) {
781    for row in ui.tree_view(&mut app.folders, 22.0, folders_under) {
782        ui.tree_row(&mut app.folders, row);
783        ui.close();
784    }
785    ui.close();
786}
787
788/// Charts: a pie whose slices are switched off by their names, a ring, bars that stand on
789/// each other, a scatter that is zoomed by the wheel and moved by dragging, and a chart over
790/// time that is fed new values ten times a second while it is asked to.
791fn charts(app: &mut App, ui: &mut Ui) {
792    let kinds = KINDS.map(|kind| app.files.iter().filter(|file| file.2 == kind).count() as f32);
793    ui.column().grow().scroll().pad(14.0).gap(14.0).open();
794    ui.row().gap(16.0).align(Align::Start, Align::Start).open();
795    ui.chart_view(&mut app.slices, &Chart::of(&[Series::pie("Files", &kinds)]).categories(&KINDS), 300.0, 160.0);
796    ui.chart(&Chart::of(&[Series::ring("Files", &kinds)]).categories(&KINDS), 230.0, 160.0);
797    ui.close();
798    ui.row().gap(16.0).align(Align::Start, Align::Start).open();
799    let (read, written) = ([3.0, 5.0, 2.0, 6.0, 4.0, 1.0, 2.0], [1.0, 2.0, 4.0, 1.0, 3.0, 2.0, 0.5]);
800    ui.chart(&Chart::of(&[Series::bars("Read", &read), Series::bars("Written", &written)]).categories(&DAYS).stacked(), 300.0, 190.0);
801    dim_text(ui, "The wheel zooms the scatter, dragging moves it, two clicks show all of it again; its names switch its rows off and on.");
802    ui.close();
803    let [small_at, small, large_at, large] = &app.dots;
804    // (Each dot's height goes with where it stands across, and a little by chance.)
805    let small: Vec<f32> = small_at.iter().zip(small).map(|(at, chance)| at * 0.5 + chance / 5.0).collect();
806    let large: Vec<f32> = large_at.iter().zip(large).map(|(at, chance)| 30.0 + at * 0.6 + chance / 4.0).collect();
807    let dots = [Series::points("Small", &small).at(small_at), Series::points("Large", &large).at(large_at)];
808    ui.chart_view(&mut app.view, &Chart::of(&dots), 560.0, 220.0);
809    // The last minute, a value every tenth of a second: only what changed is drawn again.
810    ui.toggle(&mut app.live, "Live");
811    let now = taut::platform::unix_time();
812    if app.live && now - app.fed >= 0.1 {
813        let next = app.load.last().copied().unwrap_or(0.5) + ((now * 7.3).sin() * 0.08) as f32;
814        app.load.remove(0);
815        app.load.push(next.clamp(0.0, 1.0));
816        app.fed = now;
817    }
818    if app.live {
819        ui.wake_after(std::time::Duration::from_millis(100));
820    }
821    ui.chart(&Chart::of(&[Series::area("Load", &app.load)]).x_range(-59.9, 0.0).y_range(0.0, 1.0).time(app.fed), 560.0, 160.0);
822    ui.close();
823}
824
825/// What is under an item of the tree of files (`None`: what is at the top): a thousand
826/// folders; in each, two folders and 98 files; in each of those, five files. Each item's
827/// number says where it is.
828fn folders_under(under: Option<usize>) -> Vec<TreeItem> {
829    match under {
830        None => (0..1000).map(|n| TreeItem::with_children(n, format!("Folder {n:03}"))).collect(),
831        Some(top) if top < 1000 => {
832            let inner = (0..2).map(|n| TreeItem::with_children(1000 + top * 2 + n, format!("Inner {n}")));
833            inner.chain((0..98).map(|n| TreeItem::new(10_000 + top * 100 + n, format!("file {n:02}.txt")))).collect()
834        }
835        Some(inner) if inner < 10_000 => (0..5).map(|n| TreeItem::new(1_000_000 + inner * 5 + n, format!("deep {n}.txt"))).collect(),
836        Some(_) => Vec::new(),
837    }
838}
839
840/// The parts every web page and phone app has.
841fn web_widgets(app: &mut App, ui: &mut Ui) {
842    ui.column().grow().pad(14.0).scroll().open();
843    ui.column().max(640.0, f32::INFINITY).gap(14.0).open();
844    navigation(app, ui);
845    tags_and_people(app, ui);
846    cards_and_groups(app, ui);
847    ui.close();
848    ui.close();
849}
850
851/// A banner, a path of places, the steps of a task with the app's own buttons, pages of
852/// results, a box to search with and a button with a menu of other ways.
853fn navigation(app: &mut App, ui: &mut Ui) {
854    if app.banner {
855        app.banner = !ui.banner(Severity::Info, "The parts every web page and phone app has. This strip goes with its cross.").clicked;
856        ui.close();
857    }
858    if let Some(place) = ui.breadcrumbs(&["Home", "Pictures", "Holidays", "2026", "Mountains"]) {
859        ui.notice(&format!("Gone back to place {}", place + 1));
860    }
861    ui.steps(&mut app.step, &["Account", "Address", "Payment", "Done"]);
862    // Back and on through the steps are the app's own buttons.
863    ui.row().gap(8.0).open();
864    ui.set_enabled(app.step > 0);
865    app.step -= ui.button("Back").clicked as usize;
866    ui.set_enabled(app.step < 3);
867    app.step += ui.primary_button("Next").clicked as usize;
868    ui.set_enabled(true);
869    ui.close();
870    ui.pagination(&mut app.results, 24);
871    ui.row().gap(8.0).open();
872    ui.search(&mut app.query, "Search the pictures");
873    let save = ui.split_button("Save");
874    if save.clicked {
875        ui.notice("Saved");
876    }
877    if ui.menu(save.id) {
878        if ui.menu_item("Save as\u{2026}").clicked {
879            ui.notice("Saved under another name");
880        }
881        if ui.menu_item("Save a copy").clicked {
882            ui.notice("A copy was saved");
883        }
884        ui.close();
885    }
886    ui.close();
887}
888
889/// Tags, what shows where there are none yet, stars, and people with a count over an icon.
890fn tags_and_people(app: &mut App, ui: &mut Ui) {
891    let t = ui.theme;
892    ui.tag_input(&mut app.topics, &mut app.topic, "Topics: Enter or a comma after each");
893    // What shows where there is nothing yet: a picture, a sentence and a way on.
894    if app.topics.is_empty() {
895        ui.column().pad(16.0).gap(8.0).align(Align::Start, Align::Center).open();
896        ui.el().icon(STAR, 32.0, t.text_faint).show();
897        ui.label("No topics yet");
898        if ui.button("Add a few").clicked {
899            app.topics = vec!["mountains".into(), "lakes".into()];
900        }
901        ui.close();
902    }
903    ui.row().gap(10.0).open();
904    ui.rating(&mut app.stars, 5, 0.5);
905    ui.note(&format!("{} of 5", app.stars));
906    ui.close();
907    // People: a picture of one, the first letters of the others; and a count over an icon.
908    let face = face(app, ui);
909    ui.row().gap(8.0).open();
910    ui.avatar("Ada Lovelace", Some(face), 36.0);
911    for name in ["Grace Hopper", "Alan Turing", "Katherine Johnson"] {
912        ui.avatar(name, None, 36.0);
913    }
914    ui.el().w(Length::Px(8.0)).show();
915    ui.el().flow(Flow::Stack).open();
916    ui.icon_button(BELL, "Notices, 3 new");
917    let badge = ui.el().place((Align::End, Align::Start)).offset(6.0, -6.0).min(16.0, 16.0).pad((4.0, 0.0)).radius(8.0).fill(t.red);
918    badge.text("3", t.small_font, t.on_accent).text_align(Align::Center).show();
919    ui.close();
920    ui.close();
921}
922
923/// Cards, what stands in while something loads, buttons that stay down in one frame,
924/// sections of which one is open at a time, and keys written as keys.
925fn cards_and_groups(app: &mut App, ui: &mut Ui) {
926    let t = ui.theme;
927    // Cards side by side, each pressed as one; their own buttons are pressed by themselves.
928    ui.row().gap(10.0).align(Align::Start, Align::Start).open();
929    for (number, (name, words)) in [("Mountains", "A walk of four hours, with a lake at the top."), ("The sea", "Two days along the coast, from bay to bay.")].into_iter().enumerate() {
930        let card = ui.card();
931        ui.text(name, t.font.bold(), t.text);
932        dim_text(ui, words);
933        ui.toggle(&mut app.liked[number], "Liked");
934        ui.close();
935        if card.clicked {
936            ui.notice(&format!("Opened \u{201c}{name}\u{201d}"));
937        }
938    }
939    ui.close();
940    ui.switch(&mut app.loading, "Show what stands in while something loads");
941    if app.loading {
942        ui.row().gap(10.0).open();
943        ui.skeleton().size(40.0, 40.0).radius(20.0).show();
944        ui.column().w(Length::Grow(1.0)).gap(8.0).open();
945        ui.skeleton().show();
946        ui.skeleton().w(Length::Px(160.0)).show();
947        ui.close();
948        ui.close();
949    }
950    // A row of buttons that stay down, in one frame: a group to a screen reader.
951    let styles = ui.row().place((Align::Start, Align::Center)).pad(2.0).gap(2.0).fill(t.panel).border(t.line, 1.0).radius(t.radius).open();
952    ui.tell(&styles, Role::Group, None, "Text style");
953    for (on, name) in app.styles.iter_mut().zip(["Bold", "Italic", "Underline"]) {
954        ui.toggle(on, name);
955    }
956    ui.close();
957    // Sections of which one is open at a time.
958    for (number, (title, words)) in [("What it is", "A page of the parts people know."), ("How it goes", "Each part is one call.")].into_iter().enumerate() {
959        let mut open = app.section == Some(number);
960        if ui.fold(&mut open, title) {
961            dim_text(ui, words);
962        }
963        app.section = if open { Some(number) } else { app.section.filter(|was| *was != number) };
964    }
965    ui.row().gap(8.0).open();
966    ui.label("Find");
967    ui.shortcut("Ctrl+F");
968    ui.el().w(Length::Px(12.0)).show();
969    ui.label("Commands");
970    ui.shortcut("Ctrl+K");
971    ui.close();
972    if !app.banner && ui.link("Show the banner again").clicked {
973        app.banner = true;
974    }
975}
976
977/// A quiet line that breaks where the room for it ends.
978fn dim_text(ui: &mut Ui, text: &str) {
979    let t = ui.theme;
980    ui.el().w(Length::Grow(1.0)).text(text, t.small_font, t.text_dim).wrap().show();
981}
982
983/// The page of more parts: three columns, the parts other libraries name, and a code editor.
984fn more_parts(app: &mut App, ui: &mut Ui) {
985    // Sixteen values to a wave, so one falls on each peak and trough and a smooth line
986    // through them is round there, not flattened between two values either side.
987    let wave: Vec<f32> = (0..48).map(|i| (i as f32 * std::f32::consts::PI / 8.0).sin() * app.amount).collect();
988    ui.column().grow().scroll().open();
989    ui.row().pad(14.0).gap(28.0).align(Align::Start, Align::Start).open();
990    rows_tree_and_chart(app, ui, &wave);
991    numbers_and_plots(app, ui, &wave);
992    form_parts(app, ui);
993    ui.close();
994    parts_by_other_names(app, ui);
995    code_and_inspector(app, ui);
996    ui.close();
997}
998
999/// Rows to pick, each with a menu; a tree; and the wave in a chart.
1000fn rows_tree_and_chart(app: &mut App, ui: &mut Ui, wave: &[f32]) {
1001    ui.column().w(Length::Px(240.0)).gap(2.0).open();
1002    dim_text(ui, "Rows to pick. Right-click one for a menu.");
1003    for (number, name) in ["Inbox", "Drafts", "Sent", "Archive"].iter().enumerate() {
1004        let row = ui.selectable(app.selected_row == number, name);
1005        if row.clicked {
1006            app.selected_row = number;
1007        }
1008        if ui.context_menu(&row) {
1009            if ui.menu_item("Pick this one").clicked {
1010                app.selected_row = number;
1011            }
1012            ui.menu_item("Leave it");
1013            ui.close();
1014        }
1015    }
1016    ui.tree(&mut app.tree_open[0], "A tree");
1017    if app.tree_open[0] {
1018        ui.label("A leaf");
1019        ui.tree(&mut app.tree_open[1], "A branch");
1020        if app.tree_open[1] {
1021            ui.label("A leaf, a step further in");
1022            ui.close();
1023        }
1024        ui.close();
1025    }
1026    // The wave in a chart, and half of it, both drawn as smooth curves.
1027    let half: Vec<f32> = wave.iter().map(|value| value / 2.0).collect();
1028    ui.el().h(Length::Px(10.0)).show();
1029    ui.chart(&Chart::of(&[Series::area("Wave", wave).smooth(), Series::line("Half", &half).smooth()]), 240.0, 150.0);
1030    ui.close();
1031}
1032
1033/// A number dragged sideways, the wave it makes as a line and as bars, parts switched off,
1034/// and sliders that stop at steps or go by a curve.
1035fn numbers_and_plots(app: &mut App, ui: &mut Ui, wave: &[f32]) {
1036    ui.column().w(Length::Px(290.0)).gap(10.0).open();
1037    ui.row().gap(10.0).open();
1038    ui.label("A number to drag sideways");
1039    ui.drag_value(&mut app.amount, 0.1);
1040    ui.close();
1041    dim_text(ui, "A wave as tall as that number, as a line and as bars.");
1042    ui.plot_lines(wave, 290.0, 60.0);
1043    ui.plot_bars(&wave[..18], 290.0, 60.0);
1044    ui.checkbox(&mut app.off, "Switch the two parts under this off");
1045    ui.set_enabled(!app.off);
1046    ui.row().gap(12.0).open();
1047    ui.button("A button");
1048    ui.switch(&mut app.on, "A switch");
1049    ui.close();
1050    ui.set_enabled(true);
1051    dim_text(ui, "A slider that stops at steps, and one that goes by a curve: half way is 100%.");
1052    ui.slider_marked(&mut app.marked, 0.0..=10.0, 1.0, "");
1053    ui.slider_curved(&mut app.zoom, 25.0..=400.0, "%");
1054    ui.close();
1055}
1056
1057/// The parts of a form: a number with a unit, a colour, a day, two boxes to carry things
1058/// between, a time, a day and a time, a span of days.
1059fn form_parts(app: &mut App, ui: &mut Ui) {
1060    ui.column().w(Length::Px(250.0)).gap(10.0).open();
1061    ui.row().gap(10.0).open();
1062    ui.label("A number to type");
1063    ui.number_in(&mut app.count, 0..=99, 1, "copies");
1064    ui.close();
1065    ui.row().gap(10.0).open();
1066    ui.label("A colour to pick");
1067    ui.color_picker(&mut app.tint);
1068    ui.close();
1069    ui.row().gap(10.0).open();
1070    ui.label("A day to pick");
1071    ui.date(&mut app.day);
1072    ui.close();
1073    dim_text(ui, "Things to carry from one box to the other.");
1074    ui.row().gap(10.0).align(Align::Start, Align::Start).open();
1075    drag_list(app, ui, 0);
1076    drag_list(app, ui, 1);
1077    ui.close();
1078    dim_text(ui, "A time, a day and a time, and a span of days picked in one calendar.");
1079    ui.row().gap(10.0).open();
1080    ui.time(&mut app.alarm, false);
1081    ui.close();
1082    ui.row().gap(10.0).open();
1083    ui.date_time(&mut app.meeting.0, &mut app.meeting.1);
1084    ui.close();
1085    ui.row().gap(10.0).open();
1086    ui.date_range(&mut app.away.0, &mut app.away.1);
1087    ui.close();
1088    // Numbers, days and times as the person writes them; and the colours picked last, one line
1089    // to keep between runs.
1090    let here = format!("{} · {} · {}", ui.format_number(1_234_567.5, 1), ui.format_date(app.day), ui.format_time(app.alarm, false));
1091    dim_text(ui, &here);
1092    let recent = ui.recent_colors().to_string();
1093    if !recent.is_empty() {
1094        dim_text(ui, &format!("Kept: {recent}"));
1095    }
1096    // With the feature `translations`: the built-in words in another language.
1097    #[cfg(feature = "translations")]
1098    languages(ui);
1099    ui.close();
1100}
1101
1102/// Buttons that put the built-in words (menus, calendar, file dialog) in another language.
1103#[cfg(feature = "translations")]
1104fn languages(ui: &mut Ui) {
1105    ui.wrapping_row().gap(6.0).open();
1106    for (name, code) in [("Deutsch", "de"), ("Español", "es"), ("Français", "fr"), ("日本語", "ja"), ("Português", "pt"), ("中文", "zh")] {
1107        if ui.button(name).clicked {
1108            ui.translate(code);
1109        }
1110    }
1111    ui.close();
1112}
1113
1114/// What other libraries have by these names: a button with a picture, a slider and a bar
1115/// that say their value, a number dragged between two ends, a select of the app's own
1116/// values; a grid with stripes, a group drawn faint, a button hidden that keeps its room.
1117fn parts_by_other_names(app: &mut App, ui: &mut Ui) {
1118    ui.row().pad((14.0, 0.0, 14.0, 14.0)).gap(28.0).align(Align::Start, Align::Start).open();
1119    ui.column().w(Length::Px(290.0)).gap(10.0).open();
1120    ui.row().gap(10.0).open();
1121    ui.button_with_icon(SYNC, "Refresh");
1122    let face = face(app, ui);
1123    ui.button_with_picture(face, "Ada");
1124    ui.close();
1125    ui.slider_with_value(&mut app.level, 50.0..=100.0, "%");
1126    ui.progress_with_percent((app.level - 50.0) / 50.0);
1127    ui.grid(2).gap(10.0).align(Align::Start, Align::Center).open();
1128    ui.label("From 0 to 10");
1129    ui.drag_value_in(&mut app.within, 0.0..=10.0, 0.05);
1130    ui.label("A fruit");
1131    ui.select_value(&mut app.fruit, &[("apple", "Apple"), ("pear", "Pear"), ("plum", "Plum")]);
1132    ui.close();
1133    ui.close();
1134    ui.column().w(Length::Px(290.0)).gap(10.0).open();
1135    let t = ui.theme;
1136    ui.grid(2).gap(12.0).line_gap(4.0).pad(4.0).stripes(t.panel).radius(4.0).open();
1137    for (name, size) in [("notes.txt", "2 KB"), ("photo.png", "3.1 MB"), ("song.ogg", "4.6 MB"), ("todo.md", "1 KB")] {
1138        ui.el().w(Length::Grow(1.0)).text(name, t.font, t.text).show();
1139        ui.note(size);
1140    }
1141    ui.close();
1142    ui.checkbox(&mut app.faint, "Draw the row under this faint");
1143    ui.row().gap(10.0).opacity(if app.faint { 0.4 } else { 1.0 }).open();
1144    ui.button("Not yet");
1145    ui.switch(&mut app.on, "Ready");
1146    ui.close();
1147    ui.checkbox(&mut app.hide, "Hide the middle button");
1148    ui.row().gap(10.0).open();
1149    ui.button("Left");
1150    ui.el().hidden(app.hide).open();
1151    ui.button("Middle");
1152    ui.close();
1153    ui.button("Right");
1154    ui.close();
1155    ui.close();
1156    ui.close();
1157}
1158
1159/// A box to edit code in, with this program's own source in it; and an inspector, with a
1160/// lamp of this page's own in it.
1161fn code_and_inspector(app: &mut App, ui: &mut Ui) {
1162    ui.row().h(Length::Px(320.0)).pad((14.0, 0.0, 14.0, 14.0)).gap(14.0).open();
1163    ui.column().grow().open();
1164    ui.code_editor(&mut app.code, "The gallery's source");
1165    ui.close();
1166    ui.column().w(Length::Px(300.0)).h(Length::Grow(1.0)).open();
1167    ui.inspect(&mut app.inspector, &mut app.lamp);
1168    ui.close();
1169    ui.close();
1170}
1171
1172/// A picture of a person, made the first time it is shown.
1173fn face(app: &mut App, ui: &mut Ui) -> Picture {
1174    *app.face.get_or_insert_with(|| ui.add_picture(32, (0..32 * 32).map(|at| 0xff00_0000 | (150 + at % 32 * 3) << 16 | (110 + at / 32 * 3) << 8 | 140).collect()))
1175}
1176
1177/// One of two boxes whose things are dragged to the other. (The payload: box * 100 + index.)
1178fn drag_list(app: &mut App, ui: &mut Ui, side: usize) {
1179    let t = ui.theme;
1180    let wanted = ui.drag_payload().is_some_and(|what| what as usize / 100 != side);
1181    let around = ui.column().key(side).w(Length::Px(118.0)).min(0.0, 124.0).pad(4.0).gap(2.0).fill(t.panel).border(if wanted { t.accent } else { t.line }, 1.0).radius(6.0).sense().open();
1182    for (number, &name) in app.drag_lists[side].iter().enumerate() {
1183        let thing = ui.selectable(false, name);
1184        if ui.drag_source(&thing, (side * 100 + number) as u64) {
1185            ui.label(name);
1186            ui.close();
1187        }
1188    }
1189    ui.close();
1190    let Some(what) = ui.dropped(&around).map(|what| what as usize).filter(|what| what / 100 != side) else { return };
1191    let thing = app.drag_lists[what / 100].remove(what % 100);
1192    app.drag_lists[side].push(thing);
1193}
1194
1195/// The dialog that asks for a new name, while it is open.
1196fn rename_dialog(app: &mut App, ui: &mut Ui) {
1197    let t = ui.theme;
1198    let Some(name) = &mut app.renaming else { return };
1199    let beside = ui.dialog();
1200    ui.text("Rename this window", t.font.bold(), t.text);
1201    ui.note("The name shows at the start of the bar, and in the window's title.");
1202    let name_box = ui.text_box(name, "Name");
1203    if ui.focus() == 0 {
1204        ui.set_focus(name_box.id); // a dialog that has just opened is typed into right away
1205    }
1206    ui.row().gap(8.0).align(Align::End, Align::Center).open();
1207    let cancel = ui.button("Cancel").clicked;
1208    let keep = ui.primary_button("Rename").clicked || ui.key(Key::Enter, Mods::NONE);
1209    ui.close();
1210    ui.close();
1211    if keep {
1212        app.name = name.clone();
1213        ui.set_title(&app.name);
1214    }
1215    if keep || cancel || beside.clicked || ui.key(Key::Escape, Mods::NONE) {
1216        app.renaming = None;
1217        ui.rerun();
1218    }
1219}
1220
1221/// A number from 0 to 100 that looks random, the same for the same `n` (PCG's hash).
1222fn chance(n: u32) -> f32 {
1223    let x = n.wrapping_mul(747_796_405).wrapping_add(2_891_336_453);
1224    let x = ((x >> ((x >> 28) + 4)) ^ x).wrapping_mul(277_803_737);
1225    ((x >> 22) ^ x) as f32 / u32::MAX as f32 * 100.0
1226}
1227
1228/// The theme: the system's, or the app's own (dark or light, high contrast or accented), at
1229/// the spacing asked for.
1230fn theme(app: &App, ui: &Ui) -> Theme {
1231    let theme = match (app.system, app.contrast, app.light) {
1232        (true, ..) => ui.system_theme(),
1233        (false, true, light) => Theme::high_contrast(if light { ContrastColors::LIGHT } else { ContrastColors::DARK }),
1234        (false, false, true) => Theme::light().accented(app.tint),
1235        (false, false, false) => Theme::dark().accented(app.tint),
1236    };
1237    [theme.dense(), theme, theme.roomy()][app.spacing]
1238}
1239
1240/// What the person asked the system for, said in a line.
1241fn system_preferences(ui: &Ui) -> String {
1242    let colour = ui.system_accent().map_or("no colour of its own".into(), |colour| format!("the colour {colour}"));
1243    let (contrast, motion) = (if ui.high_contrast() { "high contrast" } else { "no high contrast" }, if ui.reduced_motion() { "less movement" } else { "movement" });
1244    format!("The system asks for {} windows, {colour}, {contrast}, {motion}, text \u{d7}{}.", if ui.system_dark() { "dark" } else { "light" }, ui.text_scale())
1245}
1246
1247/// The app as it starts.
1248pub fn app() -> App {
1249    App {
1250        name: "Gallery".into(),
1251        presses: 0,
1252        page: 0,
1253        room: 0,
1254        filter: String::new(),
1255        notes: String::new(),
1256        renaming: None,
1257        light: false,
1258        contrast: false,
1259        system: false,
1260        spacing: 1,
1261        ticked: false,
1262        on: true,
1263        folded_open: true,
1264        second: false,
1265        level: 72.0,
1266        bold: false,
1267        starred: true,
1268        span: 1,
1269        busy: false,
1270        from: 20.0,
1271        to: 70.0,
1272        followed: 0,
1273        finding: false,
1274        colour: 6,
1275        wanted: String::new(),
1276        picture: None,
1277        photo: None,
1278        drawings: None,
1279        animation: None,
1280        playing: false,
1281        // `TAUT_FONT=some.ttf` writes the whole window with that font, in place of the system's.
1282        letters: std::env::var_os("TAUT_FONT").and_then(|file| std::fs::read(file).ok()).and_then(FontFile::read),
1283        place: String::new(),
1284        shape: None,
1285        selected_row: 1,
1286        tree_open: [true, true],
1287        amount: 4.0,
1288        off: true,
1289        count: 12,
1290        day: Date::today(),
1291        alarm: Time::new(7, 30, 0),
1292        meeting: (Date::today(), Time::new(14, 0, 0)),
1293        away: (Date::today(), Date::from_days(Date::today().days() + 6)),
1294        marked: 3.0,
1295        zoom: 100.0,
1296        within: 2.5,
1297        fruit: "pear",
1298        faint: true,
1299        hide: false,
1300        tint: Theme::dark().accent,
1301        drag_lists: [vec!["Apples", "Pears", "Plums"], vec!["Bread"]],
1302        table: Table { frozen: 1, ..Default::default() },
1303        selection: Selection::default(),
1304        files: (0..5000u32).map(|n| (format!("file {n:04}"), n * 7919 % 4096 + 1, ["text", "picture", "sound", "folder"][n as usize * 5 % 4])).collect(),
1305        tools: Panel { open: false, ..Panel::new(520.0, 150.0, 300.0, 220.0) },
1306        code: CodeEditor::new(include_str!("gallery.rs"), &Syntax::RUST),
1307        lamp: Lamp { name: "Desk".into(), power: 40.0, on: true, kind: LampKind::Spot, tint: Color::rgb(0xffcf7a), rooms: vec!["Study".into(), "Hall".into()] },
1308        inspector: Inspector::default(),
1309        beside: SecondWindow { open: false, ..SecondWindow::new(380.0, 240.0) },
1310        file_dialog: FileDialog::default(),
1311        dock: Dock::of(&["Files"]).beside(Dock::of(&["Editor", "Preview"]).above(Dock::of(&["Log"]), 0.7), 0.25),
1312        step: 2,
1313        results: 0,
1314        query: String::new(),
1315        topics: vec!["mountains".into(), "walks".into()],
1316        topic: String::new(),
1317        stars: 3.5,
1318        face: None,
1319        liked: [true, false],
1320        loading: true,
1321        banner: true,
1322        styles: [true, false, false],
1323        section: Some(0),
1324        slices: ChartView::default(),
1325        view: ChartView::default(),
1326        dots: [0, 1, 2, 3].map(|row| (0..80u32).map(|n| chance(n + 80 * row)).collect()),
1327        live: false,
1328        load: (0..600).map(|nth| 0.5 + (nth as f32 / 40.0).sin() * 0.2).collect(),
1329        fed: taut::platform::unix_time(),
1330        folders: TreeView::default(),
1331        writing: Markdown::parse(WRITING),
1332    }
1333}
1334
1335/// What the page of writing shows, written in Markdown.
1336const WRITING: &str = "# Writing
1337
1338A document read from Markdown: words in **bold**, in *slant*, in `code`, ~~struck through~~, and [links](https://commonmark.org) among them, broken into rows as wide as the room. Select any of it with the pointer or with Shift and the arrows, and copy it with Control+C. [Lists](#lists) are further down.
1339
1340## Lists
1341
1342- A point
1343- Another, with more under it:
1344  1. first
1345  2. second
1346- [x] a task that is done
1347- [ ] and one that is not
1348
1349> Quoted from elsewhere, in a quieter colour.
1350
1351```rust
1352fn main() {
1353    let words = \"coloured by the language it names\"; // and a comment
1354    println!(\"{words} {}\", 42);
1355}
1356```
1357
1358| Part | What it does | Costs |
1359|:-----|:------------:|------:|
1360| Rows | break words | a letter as a label does |
1361| Code | colours words | the lines in view |
1362
1363---
1364
1365Only what is in view is described: a long document costs what a screenful of it does.
1366";
1367
1368/// Rich text and a document: a paragraph whose pieces each look their own way, with an
1369/// icon and a link among them; and a page written in Markdown.
1370fn writing(app: &mut App, ui: &mut Ui) {
1371    let t = ui.theme;
1372    ui.column().grow().scroll().pad(14.0).open();
1373    ui.column().max(680.0, f32::INFINITY).gap(14.0).open();
1374    let pieces = [
1375        Span::text("A paragraph of rich text: "),
1376        Span::text("bold").bold(),
1377        Span::text(", "),
1378        Span::text("slanted").italic(),
1379        Span::text(", "),
1380        Span::text("larger").size(t.font.size * 1.4),
1381        Span::text(", in "),
1382        Span::text("colour").color(t.green),
1383        Span::text(", "),
1384        Span::text("marked").background(t.amber.with_alpha(0.35)),
1385        Span::text(", "),
1386        Span::text("code").code(),
1387        Span::text(", "),
1388        Span::icon(STAR),
1389        Span::text(" an icon, and "),
1390        Span::text("a link").link(),
1391        Span::text("."),
1392    ];
1393    if ui.rich(&pieces).is_some() {
1394        ui.notice("The link was pressed");
1395    }
1396    ui.separator();
1397    if let Some(link) = ui.markdown(&app.writing) {
1398        ui.notice(&format!("A link to {link}"));
1399    }
1400    // A line in each of the scripts of India and South-East Asia, shaped by the library.
1401    if cfg!(feature = "brahmic") {
1402        ui.separator();
1403        SYLLABLES.iter().for_each(|line| _ = ui.label(line));
1404    }
1405    ui.close();
1406    ui.close();
1407}
1408
1409/// A line in each of the scripts of India and South-East Asia, in its own language: shown
1410/// with the feature `brahmic`.
1411const SYLLABLES: [&str; 15] = [
1412    "हिन्दी: नमस्ते दुनिया, क्षमा और धर्म",
1413    "বাংলা: আমি বাংলায় কথা বলি",
1414    "ਪੰਜਾਬੀ: ਸਤਿ ਸ੍ਰੀ ਅਕਾਲ",
1415    "ગુજરાતી: કેમ છો? શ્રી ગણેશ",
1416    "ଓଡ଼ିଆ: ନମସ୍କାର, ଭୁବନେଶ୍ୱର",
1417    "தமிழ்: வணக்கம், கொண்டு",
1418    "తెలుగు: నమస్కారం, శ్రీ",
1419    "ಕನ್ನಡ: ನಮಸ್ಕಾರ, ಕರ್ನಾಟಕ",
1420    "മലയാളം: നമസ്കാരം, കേരളം",
1421    "සිංහල: ආයුබෝවන්, ශ්‍රී ලංකා",
1422    "ไทย: สวัสดีครับ น้ำใจ",
1423    "ລາວ: ສະບາຍດີ",
1424    "ខ្មែរ: សួស្ដី ស្ត្រី",
1425    "မြန်မာ: မင်္ဂလာပါ",
1426    "བོད་ཡིག: བཀྲ་ཤིས་བདེ་ལེགས།",
1427];
1428
1429fn main() {
1430    let mut app = app();
1431    taut::run(Window { title: "taut gallery", width: 900.0, height: 600.0, min_width: 280.0, min_height: 220.0, ..Default::default() }, move |ui| describe(&mut app, ui));
1432}
1433
1434/// Where Android starts the app (it is a library there): see `taut::run`.
1435#[cfg(target_os = "android")]
1436#[unsafe(no_mangle)]
1437#[allow(clippy::main_recursion)]
1438extern "C" fn android_main() {
1439    main()
1440}