taut 0.3.0

A Rust GUI library for Windows, Linux, macOS, iOS and the web that aims to be small, fast and accessible, with zero dependencies and nothing compiled in that your program doesn't use.
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
//! The web: the interface in a page's `<canvas>`, built for `wasm32-unknown-unknown` with no
//! crates. The loader (`web.js`, [`crate::web::LOADER`]) makes the page's elements, hands each
//! DOM event to a function exported here (`taut_*`), and does what the functions imported here
//! ask (in the module `taut`): show pixels, set the cursor, measure and draw text, keep the
//! clipboard, storage and the reader's elements.
//!
//! The CPU draws every pixel, as everywhere; what changed goes to the canvas through
//! `putImageData`. Nothing runs while nothing happens: a frame is asked for
//! (`requestAnimationFrame`) only when something changed or the interface asked for a timer.
//! Input that may need the person's own gesture (a copy, a file picker, a link) is run at once,
//! inside the DOM event; the rest waits for the next frame.
use crate::color::Color;
use crate::event::{Cursor, Event, Key};
use crate::geom::{Rect, Size, pt};
use crate::platform::{Clipboard, Window};
use crate::preferences::Preferences;
use crate::surface::Surface;
use crate::theme::ContrastColors;
use crate::ui::Ui;
use crate::web_input::{Did, Pointer};
use crate::web_mirror::Mirror;
use std::cell::RefCell;
use std::path::PathBuf;
use std::time::Duration;

#[link(wasm_import_module = "taut")]
unsafe extern "C" {
    fn present(pixels: *const u8, x: u32, y: u32, width: u32, height: u32);
    fn wake(after_ms: f64);
    fn set_cursor(shape: u32);
    fn set_title(text: *const u8, length: usize);
    fn text_field(x: f32, y: f32, width: f32, height: f32, on: u32);
    fn clipboard_set(text: *const u8, length: usize);
    fn mirror(json: *const u8, length: usize);
    fn log(text: *const u8, length: usize);
    fn now() -> f64;
    fn clock() -> f64;
    fn zone() -> f64;
    fn storage_get(key: *const u8, length: usize) -> i32;
    fn storage_set(key: *const u8, key_length: usize, value: *const u8, value_length: usize) -> u32;
    fn open_link(link: *const u8, length: usize);
    fn ask_file(accept: *const u8, length: usize, saving: u32);
    fn download(name: *const u8, name_length: usize, bytes: *const u8, length: usize);
    fn font_file(nth: u32, face: *mut u32) -> usize;
    fn take(into: *mut u8);
    pub(crate) fn text_metrics(font: *const u8, length: usize, sizes: *mut f32);
    pub(crate) fn text_measure(font: *const u8, length: usize, text: *const u8, text_length: usize, stops: *mut f32, count: usize) -> f32;
    pub(crate) fn text_draw(font: *const u8, length: usize, text: *const u8, text_length: usize, place: *mut f32) -> usize;
}

/// The bytes the loader made last (a line's pixels, a stored value, a font), `length` of them.
pub(crate) fn took(length: usize) -> Vec<u8> {
    let mut bytes = vec![0u8; length];
    // SAFETY: the loader writes exactly the `length` bytes it said it had.
    unsafe { take(bytes.as_mut_ptr()) };
    bytes
}

/// Says something on the browser's console.
pub(crate) fn say(text: &str) {
    // SAFETY: the loader reads `length` bytes of UTF-8.
    unsafe { log(text.as_ptr(), text.len()) };
}

/// Milliseconds since 1970, UTC, by the browser's clock.
pub(crate) fn unix_ms() -> f64 {
    // SAFETY: a plain question.
    unsafe { clock() }
}

/// Seconds since 1970, UTC, by the browser's clock.
pub(crate) fn unix_seconds() -> u64 {
    (unix_ms() / 1000.0).max(0.0) as u64
}

/// How far the person's time zone is ahead of UTC, in seconds.
pub(crate) fn zone_ahead() -> i64 {
    // SAFETY: a plain question.
    (unsafe { zone() } * 60.0) as i64
}

/// The value kept under `key` in the page's `localStorage`.
pub(crate) fn stored(key: &str) -> Option<String> {
    // SAFETY: the loader reads `length` bytes, and answers with the length of what it has.
    let length = unsafe { storage_get(key.as_ptr(), key.len()) };
    let length = usize::try_from(length).ok()?;
    String::from_utf8(took(length)).ok()
}

/// Keeps `value` under `key` in `localStorage`; false if the browser refused (full, or
/// storage off).
pub(crate) fn store(key: &str, value: &str) -> bool {
    // SAFETY: the loader reads both strings.
    unsafe { storage_set(key.as_ptr(), key.len(), value.as_ptr(), value.len()) != 0 }
}

/// Opens a link in a new tab (from a press, or the browser may block it).
pub(crate) fn open(link: &str) {
    // SAFETY: the loader reads `length` bytes.
    unsafe { open_link(link.as_ptr(), link.len()) };
}

/// Has the browser save `bytes` as a download named `name`.
pub(crate) fn save_as(name: &str, bytes: &[u8]) {
    // SAFETY: the loader reads both.
    unsafe { download(name.as_ptr(), name.len(), bytes.as_ptr(), bytes.len()) };
}

/// The font files the page gave the loader (`fonts`), each with the face it is for: 0
/// regular, 1 bold, 2 mono, 3 italic.
fn page_fonts() -> Vec<(u32, Vec<u8>)> {
    let mut fonts = Vec::new();
    for nth in 0..4 {
        let mut face = 0;
        // SAFETY: the loader writes the face and answers with the file's length.
        let length = unsafe { font_file(nth, &mut face) };
        if length > 0 {
            fonts.push((face, took(length)));
        }
    }
    fonts
}

/// The text renderer of a page: the page's font files where it gave the loader some (the same
/// letters as on every system; the browser's for what they lack), else the browser's.
pub(crate) fn system_text() -> Box<dyn crate::text::TextSystem> {
    let mut files = page_fonts().into_iter().filter_map(|(face, bytes)| crate::font_file::FontFile::read(bytes).map(|file| (face, file)));
    let browser = Box::new(crate::web_text::SystemText::default());
    let Some((_, regular)) = files.next() else { return browser };
    let mut fonts = crate::font_file::AppFonts::new(regular);
    for (face, file) in files {
        fonts = match face {
            1 => fonts.bold(file),
            2 => fonts.mono(file),
            _ => fonts.italic(file),
        };
    }
    Box::new(fonts.fallback(browser))
}

// ---- the clipboard ----

/// The clipboard as a page has it: what was copied or pasted last (`CLIPBOARD`). A copy goes to
/// the system clipboard too (the loader hands it on in the browser's copy event, or through
/// `navigator.clipboard`); a paste with Control+V brings the system's text first (`taut_paste`).
struct PageClipboard;

impl Clipboard for PageClipboard {
    fn get(&mut self) -> Option<String> {
        CLIPBOARD.with_borrow(|text| (!text.is_empty()).then(|| text.clone()))
    }
    fn set(&mut self, text: &str) {
        CLIPBOARD.set(text.into());
        // SAFETY: the loader reads `length` bytes.
        unsafe { clipboard_set(text.as_ptr(), text.len()) };
    }
}

thread_local! {
    /// The page's interface, once `run` made it.
    static HOST: RefCell<Option<Host>> = const { RefCell::new(None) };
    /// The clipboard's text: what was copied or pasted last.
    static CLIPBOARD: RefCell<String> = const { RefCell::new(String::new()) };
    /// Files picked, by the name they are given (`/picked/<name>`), and their bytes.
    static PICKED: RefCell<Vec<(PathBuf, Vec<u8>)>> = const { RefCell::new(Vec::new()) };
}

/// The bytes of a file the person picked (see [`crate::web::picked`]).
pub(crate) fn picked(path: &std::path::Path) -> Option<Vec<u8>> {
    PICKED.with_borrow(|picked| picked.iter().find(|(name, _)| name == path).map(|(_, bytes)| bytes.clone()))
}

/// The page and what keeps the interface shown in it.
struct Host {
    app: Box<dyn FnMut(&mut Ui)>,
    ui: Ui,
    surface: Surface,
    /// The pixels of a changed place, as `putImageData` takes them.
    rgba: Vec<u8>,
    size: Size,
    scale: f32,
    /// The interface must run, and draw after.
    rerun: bool,
    drawn: bool,
    started: f64,
    mirror: Mirror,
    /// Which nodes the mirror last heard of (`access_version`): the same, none are looked at.
    told: u64,
    cursor: Cursor,
    ime: Option<Rect>,
    title: String,
    /// The console was told that second windows don't show.
    told_windows: bool,
}

impl Host {
    /// Tells the interface the time, by the page's clock.
    fn tick(&mut self) {
        // SAFETY: a plain question.
        let ms = (unsafe { now() } - self.started).max(0.0);
        self.ui.set_time(Duration::from_secs_f64(ms / 1000.0));
        self.rerun |= self.ui.wake_in() == Some(Duration::ZERO);
    }

    /// Runs the interface if it must, and does what it asked of the page.
    fn describe(&mut self) {
        self.tick();
        if !self.rerun || self.size.w <= 0.0 {
            return;
        }
        self.ui.resize(self.size, self.scale);
        self.ui.run(&mut self.app);
        let _ = self.ui.take_closing();
        (self.rerun, self.drawn) = (false, false);
        if !self.told_windows && self.ui.windows().next().is_some() {
            say("taut: a page shows one window; second windows are not shown on the web");
            self.told_windows = true;
        }
        self.follow();
        let (version, announced) = (self.ui.access_version(), self.ui.announced());
        if version == self.told && announced.is_empty() {
            return;
        }
        self.told = version;
        if let Some(json) = self.mirror.update(self.ui.access_nodes(), announced) {
            // SAFETY: the loader reads `length` bytes of UTF-8.
            unsafe { mirror(json.as_ptr(), json.len()) };
        }
    }

    /// After a run: the title, the cursor, where text is typed, a file asked for.
    fn follow(&mut self) {
        if let Some(title) = self.ui.take_title().filter(|title| *title != self.title) {
            // SAFETY: the loader reads `length` bytes.
            unsafe { set_title(title.as_ptr(), title.len()) };
            self.title = title;
        }
        if std::mem::replace(&mut self.cursor, self.ui.cursor()) != self.cursor {
            // SAFETY: a plain request.
            unsafe { set_cursor(self.cursor as u32) };
        }
        let ime = self.ui.ime_area();
        if ime != self.ime || ime.is_some() {
            let place = ime.unwrap_or_default();
            // SAFETY: a plain request.
            unsafe { text_field(place.x, place.y, place.w, place.h, ime.is_some() as u32 + self.ui.password_focused() as u32) };
        }
        self.ime = ime;
        if let Some(asked) = self.ui.take_file_dialog_request() {
            let accept = asked.endings.join(",");
            // SAFETY: the loader reads `length` bytes.
            unsafe { ask_file(accept.as_ptr(), accept.len(), asked.saving as u32) };
            if asked.saving {
                self.ui.file_dialog_answered(Some(PathBuf::from("/downloads").join(asked.name)));
                self.rerun = true;
            }
        }
    }

    /// Draws what changed and hands it to the canvas.
    fn draw(&mut self) {
        if self.drawn {
            return;
        }
        self.drawn = true;
        for [left, top, right, bottom] in self.surface.update(&mut self.ui) {
            let (width, height) = (right - left, bottom - top);
            self.rgba.clear();
            for row in top..bottom {
                let from = row * self.surface.width;
                self.rgba.extend(self.surface.pixels[from + left..from + right].iter().flat_map(|pixel| straight(*pixel)));
            }
            // SAFETY: `width * height * 4` bytes, read at once.
            unsafe { present(self.rgba.as_ptr(), left as u32, top as u32, width as u32, height as u32) };
        }
        let _ = self.ui.changed();
    }

    /// Asks for the next frame: now if the interface must run, when its timer is due, or none
    /// (nor while the page has given no size yet).
    fn schedule(&self) {
        let after = match (self.rerun || !self.drawn, self.ui.wake_in()) {
            _ if self.size.w <= 0.0 => -1.0,
            (true, _) => 0.0,
            (false, Some(wait)) => wait.as_secs_f64() * 1000.0,
            (false, None) => -1.0,
        };
        // SAFETY: a plain request.
        unsafe { wake(after) };
    }

    /// Hands an input event to the interface.
    fn hear(&mut self, event: Event) {
        self.rerun |= self.ui.event(event);
    }
}

/// A pixel of the interface (`0xAARRGGBB`, premultiplied) as a canvas takes it: red, green,
/// blue, alpha, straight.
fn straight(pixel: u32) -> [u8; 4] {
    let alpha = pixel >> 24;
    let [blue, green, red, _] = pixel.to_le_bytes();
    if alpha == 255 || alpha == 0 {
        return [red, green, blue, alpha as u8];
    }
    let part = |channel: u8| ((u32::from(channel) * 255 + alpha / 2) / alpha).min(255) as u8;
    [part(red), part(green), part(blue), alpha as u8]
}

/// Works with the page's interface, if there is one and it is not busy (an event from inside a
/// call is dropped: the loader holds such events back).
fn with_host(work: impl FnOnce(&mut Host)) {
    HOST.with(|host| {
        let Ok(mut host) = host.try_borrow_mut() else { return };
        if let Some(host) = host.as_mut() {
            work(host);
        }
    });
}

/// Runs the interface at once (for input that may need the person's gesture to be the
/// browser's: a copy, a picker, a link), then asks for a frame to draw it.
fn now_and_later(host: &mut Host) {
    host.describe();
    host.schedule();
}

/// Opens the page's interface: it runs `app` from now on, each time something happens. The
/// window is the element the loader was given (the whole page by default): `window`'s size and
/// place are not used, its title is the page's. Returns at once: the page keeps the interface,
/// and the browser calls it.
pub fn run(window: Window, app: impl FnMut(&mut Ui) + 'static) {
    std::panic::set_hook(Box::new(|told| say(&format!("taut: {told}"))));
    let mut ui = Ui::new(system_text(), Box::new(PageClipboard));
    if window.transparent {
        ui.theme.bg = Color::NONE;
    }
    ui.set_screen_reader(true);
    ui.set_native_file_dialog(true);
    // SAFETY: plain requests and questions.
    let started = unsafe { now() };
    unsafe { set_title(window.title.as_ptr(), window.title.len()) };
    let host = Host {
        app: Box::new(app),
        ui,
        surface: Surface::default(),
        rgba: Vec::new(),
        size: Size { w: 0.0, h: 0.0 },
        scale: 1.0,
        rerun: true,
        drawn: false,
        started,
        mirror: Mirror::default(),
        told: 0,
        cursor: Cursor::Arrow,
        ime: None,
        title: window.title.into(),
        told_windows: false,
    };
    HOST.set(Some(host));
    // SAFETY: a plain request.
    unsafe { wake(0.0) };
}

/// Wakes the interface: it runs again at the next frame. (The page has one thread: this is
/// for a callback the app set up, as a [`Waker`] on another system is for a thread.)
#[derive(Clone, Copy, Debug, Default)]
pub struct Waker;

impl Waker {
    /// Has the interface run once more, soon.
    pub fn wake(&self) {
        with_host(|host| {
            host.rerun = true;
            host.schedule();
        });
    }
}

// ---- what the loader calls ----

/// Room for `length` bytes the loader writes (a string, a file): handed back with the call
/// that uses them, which frees them.
#[unsafe(no_mangle)]
pub extern "C" fn taut_alloc(length: usize) -> *mut u8 {
    let mut room = std::mem::ManuallyDrop::new(Vec::<u8>::with_capacity(length.max(1)));
    room.as_mut_ptr()
}

/// The bytes at `at` that `taut_alloc` made room for, taken back.
fn given(at: *mut u8, length: usize) -> Vec<u8> {
    // SAFETY: `at` came from `taut_alloc(length)`, and the loader wrote `length` bytes there.
    unsafe { Vec::from_raw_parts(at, length, length.max(1)) }
}

/// Text the loader wrote at `at`.
fn given_text(at: *mut u8, length: usize) -> String {
    String::from_utf8(given(at, length)).unwrap_or_default()
}

/// The page's element is this size, in CSS pixels, at this many device pixels to one. (No
/// size: the canvas lost its pixels, and the next size is drawn whole.)
#[unsafe(no_mangle)]
pub extern "C" fn taut_size(width: f32, height: f32, scale: f32) {
    with_host(|host| {
        if width <= 0.0 {
            host.surface = Surface::default();
        }
        (host.size, host.scale, host.rerun) = (Size { w: width.max(0.0), h: height.max(0.0) }, scale.max(0.25), true);
        host.schedule();
    });
}

/// A frame: the interface runs if it must, and what changed is drawn.
#[unsafe(no_mangle)]
pub extern "C" fn taut_frame() {
    with_host(|host| {
        host.describe();
        host.draw();
        host.schedule();
    });
}

/// What the person asked the browser for: dark, more contrast (2 forced colours), less motion,
/// and an accent colour (`0xRRGGBB`, or -1 for none).
#[unsafe(no_mangle)]
pub extern "C" fn taut_looks(dark: u32, contrast: u32, still: u32, accent: i32) {
    let dark = dark != 0;
    let high_contrast = (contrast != 0).then_some(if dark { ContrastColors::DARK } else { ContrastColors::LIGHT });
    let accent = u32::try_from(accent).ok().map(Color::rgb);
    let preferences = Preferences { dark, accent, high_contrast, reduced_motion: still != 0, ..Preferences::default() };
    with_host(|host| {
        host.rerun |= host.ui.set_preferences(preferences);
        host.schedule();
    });
}

/// A pointer event (see `web_input::Pointer`; places in CSS pixels).
#[unsafe(no_mangle)]
#[allow(clippy::too_many_arguments)]
pub extern "C" fn taut_pointer(did: u32, kind: u32, id: u32, primary: u32, x: f32, y: f32, button: i32, buttons: u32, pressure: f32, tilt_x: f32, tilt_y: f32, twist: f32, mods: u32) {
    let did = Did::of(did);
    let event = Pointer { did, kind, id, primary: primary != 0, at: pt(x, y), button, buttons, pressure, tilt: [tilt_x, tilt_y], twist, mods: crate::web_input::mods(mods) };
    with_host(|host| {
        crate::web_input::pointer(event, cfg!(feature = "touch")).into_iter().for_each(|event| host.hear(event));
        match did {
            Did::Down | Did::Up => now_and_later(host),
            _ => host.schedule(),
        }
    });
}

/// The wheel turned (`delta_mode` as the DOM counts it), or a touchpad pinched (Control held).
#[unsafe(no_mangle)]
pub extern "C" fn taut_wheel(x: f32, y: f32, dx: f32, dy: f32, delta_mode: u32, mods: u32) {
    with_host(|host| {
        let (right, down) = crate::web_input::wheel(dx, dy, delta_mode, [host.size.w, host.size.h]);
        host.hear(Event::Move(pt(x, y)));
        let zoom = mods & crate::web_input::CTRL != 0;
        host.hear(if zoom { Event::Zoom(crate::touch::wheel_zoom(-down / crate::web_input::NOTCH)) } else { Event::Wheel(right, down) });
        host.schedule();
    });
}

/// A key went down (`down`, or repeats) or up, by its DOM `key` and `code`; `field` says the
/// text field has the keyboard (its input events carry what is typed). Answers 1 where the
/// browser should not act on it too.
#[unsafe(no_mangle)]
pub extern "C" fn taut_key(down: u32, key: *mut u8, key_length: usize, code: *mut u8, code_length: usize, mods: u32, field: u32) -> u32 {
    let (name, code) = (given_text(key, key_length), given_text(code, code_length));
    let held = crate::web_input::mods(mods);
    let key = crate::web_input::key(&name, &code, held);
    let mut stops = false;
    with_host(|host| {
        host.hear(Event::ModifiersChanged(held));
        let Some(key) = key else { return };
        if down == 0 {
            host.hear(Event::KeyUp(key));
            return host.schedule();
        }
        host.hear(Event::Key(key, held));
        if let Some(text) = crate::web_input::typed(&name, held).filter(|_| field == 0) {
            host.hear(Event::Text(text.into()));
        }
        stops = crate::web_input::stops(Some(key), held);
        now_and_later(host);
    });
    stops as u32
}

/// Text typed into the text field, or committed by an input method.
#[unsafe(no_mangle)]
pub extern "C" fn taut_text(text: *mut u8, length: usize) {
    let text = given_text(text, length);
    with_host(|host| {
        if !text.is_empty() {
            host.hear(Event::Text(text));
        }
        now_and_later(host);
    });
}

/// The system clipboard's text, from the browser's paste event: kept as the clipboard's, then
/// Control+V is pressed.
#[unsafe(no_mangle)]
pub extern "C" fn taut_paste(text: *mut u8, length: usize) {
    CLIPBOARD.set(given_text(text, length));
    with_host(|host| {
        host.hear(Event::Key(Key::Char('v'), crate::event::Mods::CTRL));
        now_and_later(host);
    });
}

/// The system clipboard's text, read as the page gained the keyboard (where the browser lets the
/// page read it without asking): what a menu's Paste pastes.
#[unsafe(no_mangle)]
pub extern "C" fn taut_clipboard(text: *mut u8, length: usize) {
    CLIPBOARD.set(given_text(text, length));
}

/// The page gained (1) or lost the keyboard.
#[unsafe(no_mangle)]
pub extern "C" fn taut_focus(on: u32) {
    with_host(|host| {
        host.hear(Event::Focused(on != 0));
        host.schedule();
    });
}

/// A screen reader acted on a node's element (its key as the mirror wrote it): 0 pressed it,
/// 1 moved to it (it is shown, and takes the keyboard where it can).
#[unsafe(no_mangle)]
pub extern "C" fn taut_reader(key: *mut u8, length: usize, action: u32) {
    let Some(id) = crate::web_mirror::id_of(&given_text(key, length)) else { return };
    with_host(|host| {
        match action {
            0 => host.ui.press_for_reader(id),
            _ => host.ui.show_for_reader(id),
        }
        host.rerun = true;
        now_and_later(host);
    });
}

/// A file the person picked (its name and bytes), or none (`length` of the name 0: the picker
/// was closed).
#[unsafe(no_mangle)]
pub extern "C" fn taut_file(name: *mut u8, name_length: usize, bytes: *mut u8, length: usize) {
    let name = given_text(name, name_length);
    let bytes = given(bytes, length);
    let file = (!name.is_empty()).then(|| PathBuf::from("/picked").join(name));
    if let Some(file) = &file {
        PICKED.with_borrow_mut(|picked| {
            picked.retain(|(kept, _)| kept != file);
            picked.push((file.clone(), bytes));
        });
    }
    with_host(|host| {
        host.ui.file_dialog_answered(file);
        host.rerun = true;
        host.schedule();
    });
}

/// The page closes: the app hears it as its window's close (and may keep what it must).
#[unsafe(no_mangle)]
pub extern "C" fn taut_close() {
    with_host(|host| {
        host.ui.request_close();
        host.rerun = true;
        host.describe();
    });
}