concinnity-dev 0.19.2

The Concinnity dev tooling library: world authoring, the in-engine editor, the debug server, docs and packaging
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
// src/editor/import_panel.rs
//
// The Import panel: a front end over the same file resolution `cn add <file>`
// uses (`authoring/add.rs::entry_from_path`), so importing from the panel and
// from the CLI produce identical entries. The header takes a source path and
// an Add button (Enter also adds); a status line shows resolution errors; the
// body lists the world's existing file-backed entries (scene / story imports,
// textures, audio, fonts, shaders, ...) and clicking one opens it in the
// standard edit form. Layout half only; `hook/import_edit.rs` owns the actions.

use super::registry::{self, PanelKey};
use super::theme;
use super::widget::{self, place_rounded, point_in};
use crate::components::TextAlign;
use crate::ecs::World;
use crate::ecs::asset_id::AssetId;

const BASE: u32 = registry::base(PanelKey::Import);
pub(crate) const PANEL_BG: AssetId = AssetId(BASE);
pub(crate) const TITLE_LABEL: AssetId = AssetId(BASE + 2);
pub(crate) const CLOSE_BG: AssetId = AssetId(BASE + 3);
pub(crate) const CLOSE_LABEL: AssetId = AssetId(BASE + 4);
pub(crate) const ADD_BG: AssetId = AssetId(BASE + 5);
pub(crate) const ADD_LABEL: AssetId = AssetId(BASE + 6);
pub(crate) const HINT_LABEL: AssetId = AssetId(BASE + 7);
pub(crate) const STATUS_LABEL: AssetId = AssetId(BASE + 8);
pub(crate) const LIST_HEADER: AssetId = AssetId(BASE + 9);
pub(crate) const LIST_TRACK: AssetId = AssetId(BASE + 10);
pub(crate) const LIST_THUMB: AssetId = AssetId(BASE + 11);
pub(crate) const PATH_INPUT: AssetId = AssetId(BASE + 12);
pub(crate) const BROWSE_BG: AssetId = AssetId(BASE + 13);
pub(crate) const BROWSE_LABEL: AssetId = AssetId(BASE + 14);

pub(crate) fn row_bg(i: usize) -> AssetId {
    AssetId(BASE + 0x20 + i as u32)
}
pub(crate) fn row_label(i: usize) -> AssetId {
    AssetId(BASE + 0x40 + i as u32)
}

// The file-backed asset types `entry_from_path` produces, in the order their
// entries list in the panel body. TextLabel is deliberately absent: a text
// import copies the file body into `content` and keeps no path to reopen.
pub(crate) const IMPORT_TYPES: &[&str] = &[
    "SceneImport",
    "StoryImport",
    "File",
    "EnvironmentMap",
    "AudioClip",
    "Font",
    "Shader",
    "LLM",
];

// The arg carrying an import entry's file path (the key varies by type).
pub(crate) fn source_of(args: &serde_json::Value) -> String {
    for key in ["source", "path", "model_path"] {
        if let Some(s) = args.get(key).and_then(|v| v.as_str())
            && !s.is_empty()
        {
            return s.to_string();
        }
    }
    String::new()
}

// Geometry, in window pixels. Every rect derives from the panel origin `o`
// (the title bar's top-left), so dragging the title bar moves the whole panel.
// The default (and minimum) width; the user can widen the panel past this.
pub(crate) const IMPORT_W: f32 = 520.0;
const PAD: f32 = 10.0;
const HEADER_H: f32 = 32.0;
const HINT_H: f32 = 20.0;
// Two lines: an import message carries a path and rarely fits one.
const STATUS_H: f32 = 2.0 * widget::LINE_H + 6.0;
const LIST_HEADER_H: f32 = 24.0;
const BTN_W: f32 = 70.0;
const BROWSE_W: f32 = 84.0;
const GAP: f32 = 6.0;
const ROW_H: f32 = 26.0;
const SCROLLBAR_W: f32 = 5.0;
// Visible import rows at the default height; a longer list scrolls. Resizing the
// panel taller reveals more, up to the injected pool `IMPORT_POOL_MAX`.
pub(crate) const IMPORT_POOL: usize = 10;
pub(crate) const IMPORT_POOL_MAX: usize = 28;
// Row caption budget at the default width; widening the panel grows it.
const MAX_ROW_CHARS: usize = 56;
// Approximate body-font advance, for growing the caption budget with the width.
const CHAR_W: f32 = 8.5;

// The chrome height above the list (everything but the row band and bottom pad).
const CHROME_H: f32 = widget::TITLE_H + HEADER_H + HINT_H + STATUS_H + LIST_HEADER_H;

// The visible row count for panel height `h`, clamped to the injected pool. At
// the default height this is exactly `IMPORT_POOL`.
pub(crate) fn visible_rows(h: f32) -> usize {
    (((h - CHROME_H - PAD) / ROW_H).floor() as usize).clamp(IMPORT_POOL, IMPORT_POOL_MAX)
}

// The caption character budget at width `w` (grows past the default with width).
fn max_row_chars(w: f32) -> usize {
    (MAX_ROW_CHARS as f32 + (w - IMPORT_W) / CHAR_W).max(8.0) as usize
}

const ROW_TINT: [f32; 4] = [0.0, 0.0, 0.0, 0.0];
const ROW_TINT_HOVER: [f32; 4] = theme::HOVER_TINT;
const BTN_TINT: [f32; 4] = [0.20, 0.44, 0.30, 1.0];
const BTN_TINT_HOVER: [f32; 4] = [0.28, 0.56, 0.38, 1.0];
// The Browse button reads as secondary next to the green Add.
const BROWSE_TINT: [f32; 4] = theme::BUTTON_TINT;
const TRACK_TINT: [f32; 4] = [0.12, 0.12, 0.15, 0.9];
const THUMB_TINT: [f32; 4] = [0.40, 0.44, 0.56, 0.95];
const LABEL: [f32; 3] = [0.90, 0.90, 0.92];
const HINT_COLOR: [f32; 3] = [0.55, 0.58, 0.66];
const HEADER_LABEL: [f32; 3] = [0.70, 0.74, 0.84];
const ERROR_LABEL: [f32; 3] = [0.95, 0.55, 0.55];
const NOTICE_LABEL: [f32; 3] = [0.55, 0.85, 0.65];

// The outcome of the last Add, shown on the status line: a resolution failure,
// or a note about what the Add did when it appended no new row.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ImportStatus {
    Error(String),
    Notice(String),
}

impl ImportStatus {
    pub(crate) fn text(&self) -> &str {
        match self {
            Self::Error(s) | Self::Notice(s) => s,
        }
    }

    fn color(&self) -> [f32; 3] {
        match self {
            Self::Error(_) => ERROR_LABEL,
            Self::Notice(_) => NOTICE_LABEL,
        }
    }
}

// One listed import: its entry index and the "name (Type) source" caption.
pub(crate) struct ImportRow {
    pub entry: usize,
    pub caption: String,
}

// The per-frame view the hook assembles.
pub(crate) struct ImportView<'a> {
    pub rows: &'a [ImportRow],
    pub scroll: usize,
    // Whether the path field asserts keyboard focus this frame.
    pub focus: bool,
    // Outcome of the last Add.
    pub status: Option<&'a ImportStatus>,
    pub mouse: [f32; 2],
}

// A resolved Import-panel click.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ImportAction {
    // Give the path field keyboard focus.
    FocusPath,
    // Open the native file picker and put the result in the path field.
    Browse,
    // Resolve the typed path and add its entries.
    Add,
    // Open listed import `i` (a row index into the view) in the edit form.
    Open(usize),
    // A click elsewhere on the panel: swallowed so it cannot reach the world.
    Consume,
}

// Where the panel sits until the user drags it: centred below the top bar,
// offset from the Story panel's anchor so the two do not stack exactly.
pub(crate) fn default_origin(vw: f32) -> [f32; 2] {
    [(vw - IMPORT_W) * 0.5, super::hud::body_top() + 40.0]
}

// The panel's fixed footprint.
pub(crate) fn size() -> [f32; 2] {
    [
        IMPORT_W,
        widget::TITLE_H
            + HEADER_H
            + HINT_H
            + STATUS_H
            + LIST_HEADER_H
            + IMPORT_POOL as f32 * ROW_H
            + PAD,
    ]
}

// The tallest the panel resizes to: the injected row pool shown in full. Width
// is unbounded (capped only by the screen).
pub(crate) fn max_size() -> [f32; 2] {
    [
        f32::INFINITY,
        size()[1] + (IMPORT_POOL_MAX - IMPORT_POOL) as f32 * ROW_H,
    ]
}

// The header row's controls, right to left: Add pinned to the right end, the
// Browse button beside it, and the path field filling the rest.
const CTRL_H: f32 = HEADER_H - 8.0;

pub(crate) fn add_rect(o: [f32; 2], w: f32) -> [f32; 4] {
    [
        o[0] + w - PAD - BTN_W,
        widget::header_y(o, 4.0),
        BTN_W,
        CTRL_H,
    ]
}

// The "Browse..." button, opening the native file picker.
pub(crate) fn browse_rect(o: [f32; 2], w: f32) -> [f32; 4] {
    [
        add_rect(o, w)[0] - GAP - BROWSE_W,
        widget::header_y(o, 4.0),
        BROWSE_W,
        CTRL_H,
    ]
}

pub(crate) fn path_rect(o: [f32; 2], w: f32) -> [f32; 4] {
    let left = o[0] + PAD;
    [
        left,
        widget::header_y(o, 4.0),
        (browse_rect(o, w)[0] - GAP - left).max(0.0),
        CTRL_H,
    ]
}

fn list_top(o: [f32; 2]) -> f32 {
    o[1] + CHROME_H
}

// Visible import row `slot` (0-based within the window) at the effective width `w`.
pub(crate) fn row_rect(o: [f32; 2], w: f32, slot: usize) -> [f32; 4] {
    [
        o[0],
        list_top(o) + slot as f32 * ROW_H,
        w - SCROLLBAR_W - 2.0,
        ROW_H,
    ]
}

// Whether the cursor is over the scrollable import list (for wheel routing).
pub(crate) fn cursor_over_list(mx: f32, my: f32, o: [f32; 2], s: [f32; 2]) -> bool {
    let p = widget::outer_rect(o, s);
    mx >= p[0] && mx < p[0] + p[2] && my >= list_top(o) && my < p[1] + p[3]
}

// Resolve a click at `(mx, my)` against the panel at origin `o`. `None` means
// the click missed the panel. Title-bar presses never reach this: the shared
// routing intercepts them first.
pub(crate) fn hit_test(
    view: &ImportView,
    mx: f32,
    my: f32,
    o: [f32; 2],
    s: [f32; 2],
) -> Option<ImportAction> {
    let w = s[0];
    if point_in(mx, my, add_rect(o, w)) {
        return Some(ImportAction::Add);
    }
    if point_in(mx, my, browse_rect(o, w)) {
        return Some(ImportAction::Browse);
    }
    if point_in(mx, my, path_rect(o, w)) {
        return Some(ImportAction::FocusPath);
    }
    for slot in 0..visible_rows(s[1]) {
        if !point_in(mx, my, row_rect(o, w, slot)) {
            continue;
        }
        let i = view.scroll + slot;
        return Some(if i < view.rows.len() {
            ImportAction::Open(i)
        } else {
            ImportAction::Consume
        });
    }
    point_in(mx, my, widget::outer_rect(o, s)).then_some(ImportAction::Consume)
}

// Position + show the panel (`Some(view)`) at effective size `s`, or blank every
// element (`None`).
pub(crate) fn apply(world: &mut World, view: Option<&ImportView>, o: [f32; 2], s: [f32; 2]) {
    let Some(view) = view else {
        hide_all(world);
        return;
    };
    let w = s[0];
    let rows_shown = visible_rows(s[1]);
    widget::place_panel(world, PANEL_BG, widget::outer_rect(o, s));
    let title = widget::title_rect(o, w);
    widget::place_heading(world, TITLE_LABEL, title, "Import");
    let close_hover = point_in(view.mouse[0], view.mouse[1], widget::close_rect(title));
    widget::place_close(world, CLOSE_BG, CLOSE_LABEL, title, close_hover);

    widget::show_field(world, PATH_INPUT, path_rect(o, w), view.focus);
    let browse = browse_rect(o, w);
    let hover = point_in(view.mouse[0], view.mouse[1], browse);
    let tint = if hover {
        theme::HOVER_TINT
    } else {
        BROWSE_TINT
    };
    place_rounded(world, BROWSE_BG, browse, tint, theme::CONTROL_RADIUS, true);
    if let Some(l) = widget::label_mut(world, BROWSE_LABEL) {
        l.x = browse[0] + browse[2] * 0.5;
        l.y = browse[1] + browse[3] * 0.5 - theme::TEXT_HALF;
        l.align = TextAlign::Center;
        l.color = [1.0, 1.0, 1.0];
        l.visible = true;
        l.content = "Browse...".to_string();
    }
    let add = add_rect(o, w);
    let hover = point_in(view.mouse[0], view.mouse[1], add);
    let tint = if hover { BTN_TINT_HOVER } else { BTN_TINT };
    place_rounded(world, ADD_BG, add, tint, theme::CONTROL_RADIUS, true);
    if let Some(l) = widget::label_mut(world, ADD_LABEL) {
        l.x = add[0] + add[2] * 0.5;
        l.y = add[1] + add[3] * 0.5 - theme::TEXT_HALF;
        l.align = TextAlign::Center;
        l.color = [1.0, 1.0, 1.0];
        l.visible = true;
        l.content = "Add".to_string();
    }
    widget::place_message(
        world,
        HINT_LABEL,
        [
            o[0] + PAD,
            o[1] + widget::TITLE_H + HEADER_H,
            (w - 2.0 * PAD).max(0.0),
            HINT_H,
        ],
        "scenes (glb, fbx) - stories (md) - images - hdr - audio - fonts - shaders - text",
        HINT_COLOR,
        true,
    );
    widget::place_message(
        world,
        STATUS_LABEL,
        [
            o[0] + PAD,
            o[1] + widget::TITLE_H + HEADER_H + HINT_H,
            (w - 2.0 * PAD).max(0.0),
            STATUS_H - 4.0,
        ],
        view.status.map(ImportStatus::text).unwrap_or(""),
        view.status.map_or(ERROR_LABEL, ImportStatus::color),
        view.status.is_some(),
    );
    if let Some(l) = widget::label_mut(world, LIST_HEADER) {
        l.x = o[0] + PAD;
        l.y = o[1] + widget::TITLE_H + HEADER_H + HINT_H + STATUS_H + 2.0;
        l.align = TextAlign::Left;
        l.color = HEADER_LABEL;
        l.visible = true;
        l.content = if view.rows.is_empty() {
            "Imports (none yet)".to_string()
        } else {
            format!("Imports ({})", view.rows.len())
        };
    }

    for slot in 0..IMPORT_POOL_MAX {
        let i = view.scroll + slot;
        if slot >= rows_shown || i >= view.rows.len() {
            widget::set_sprite_visible(world, row_bg(slot), false);
            widget::set_label_visible(world, row_label(slot), false);
            continue;
        }
        let r = row_rect(o, w, slot);
        let hovered = point_in(view.mouse[0], view.mouse[1], r);
        let tint = if hovered { ROW_TINT_HOVER } else { ROW_TINT };
        place_rounded(
            world,
            row_bg(slot),
            theme::highlight_rect(r),
            tint,
            theme::CONTROL_RADIUS,
            true,
        );
        if let Some(l) = widget::label_mut(world, row_label(slot)) {
            l.x = r[0] + PAD;
            l.y = r[1] + ROW_H * 0.5 - theme::TEXT_HALF;
            l.align = TextAlign::Left;
            l.color = LABEL;
            l.visible = true;
            l.content = widget::clip_text(&view.rows[i].caption, max_row_chars(w));
        }
    }

    layout_scrollbar(world, view, o, w, rows_shown);
}

fn layout_scrollbar(world: &mut World, view: &ImportView, o: [f32; 2], w: f32, rows_shown: usize) {
    let total = view.rows.len();
    if total <= rows_shown {
        widget::set_sprite_visible(world, LIST_TRACK, false);
        widget::set_sprite_visible(world, LIST_THUMB, false);
        return;
    }
    let x = o[0] + w - SCROLLBAR_W;
    let top = list_top(o);
    let h = rows_shown as f32 * ROW_H;
    place_rounded(
        world,
        LIST_TRACK,
        [x, top, SCROLLBAR_W, h],
        TRACK_TINT,
        SCROLLBAR_W * 0.5,
        true,
    );
    let thumb_h = (h * rows_shown as f32 / total as f32).max(18.0);
    let max_scroll = (total - rows_shown) as f32;
    let off = (h - thumb_h) * (view.scroll as f32 / max_scroll);
    place_rounded(
        world,
        LIST_THUMB,
        [x, top + off, SCROLLBAR_W, thumb_h],
        THUMB_TINT,
        SCROLLBAR_W * 0.5,
        true,
    );
}

// Hide every panel element, blurring the path field so a hidden field cannot
// keep keyboard focus.
pub(crate) fn hide_all(world: &mut World) {
    widget::hide_all(world, &all_sprite_ids(), &all_label_ids(), &all_field_ids());
}

// Every panel sprite id, in draw (insertion) order: chrome, then the list
// rows, then the scrollbar floating above them.
pub(crate) fn all_sprite_ids() -> Vec<AssetId> {
    let mut ids = vec![PANEL_BG, CLOSE_BG, BROWSE_BG, ADD_BG];
    ids.extend((0..IMPORT_POOL_MAX).map(row_bg));
    ids.extend([LIST_TRACK, LIST_THUMB]);
    ids
}

pub(crate) fn all_label_ids() -> Vec<AssetId> {
    let mut ids = vec![
        TITLE_LABEL,
        CLOSE_LABEL,
        BROWSE_LABEL,
        ADD_LABEL,
        HINT_LABEL,
        STATUS_LABEL,
        LIST_HEADER,
    ];
    ids.extend((0..IMPORT_POOL_MAX).map(row_label));
    ids
}

pub(crate) fn all_field_ids() -> Vec<AssetId> {
    vec![PATH_INPUT]
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::components::{Sprite, TextInput, TextLabel};

    fn injected_world() -> World {
        let mut world = World::new();
        for id in all_sprite_ids() {
            world.add_component(Sprite {
                asset_id: id,
                ..Default::default()
            });
        }
        for id in all_label_ids() {
            world.add_component(TextLabel {
                asset_id: id,
                ..Default::default()
            });
        }
        for id in all_field_ids() {
            world.add_component(TextInput {
                asset_id: id,
                ..Default::default()
            });
        }
        world
    }

    fn rows(n: usize) -> Vec<ImportRow> {
        (0..n)
            .map(|i| ImportRow {
                entry: i,
                caption: format!("import {i}"),
            })
            .collect()
    }

    fn view<'a>(rows: &'a [ImportRow], scroll: usize) -> ImportView<'a> {
        ImportView {
            rows,
            scroll,
            focus: true,
            status: None,
            mouse: [0.0, 0.0],
        }
    }

    #[test]
    fn source_of_reads_the_per_type_path_key() {
        assert_eq!(source_of(&serde_json::json!({"source": "a.glb"})), "a.glb");
        assert_eq!(source_of(&serde_json::json!({"path": "f.ttf"})), "f.ttf");
        assert_eq!(
            source_of(&serde_json::json!({"model_path": "m.gguf"})),
            "m.gguf"
        );
        assert_eq!(source_of(&serde_json::json!({"other": 1})), "");
    }

    // The header packs right to left without overlapping, and the path field
    // keeps a usable width beside the two buttons.
    #[test]
    fn header_controls_pack_without_overlap() {
        let o = [40.0, 40.0];
        let (p, b, a) = (
            path_rect(o, IMPORT_W),
            browse_rect(o, IMPORT_W),
            add_rect(o, IMPORT_W),
        );
        assert_eq!(a[0] + a[2], o[0] + IMPORT_W - PAD, "Add pinned right");
        assert_eq!(b[0] + b[2] + GAP, a[0], "Browse sits left of Add");
        assert_eq!(p[0] + p[2] + GAP, b[0], "the field fills up to Browse");
        assert!(p[2] > 200.0, "the path field stays usable: {}", p[2]);
        for r in [p, b, a] {
            assert_eq!(r[1], widget::header_y(o, 4.0));
            assert_eq!(r[3], CTRL_H);
        }
    }

    #[test]
    fn hit_test_resolves_path_browse_add_and_rows() {
        let r = rows(3);
        let v = view(&r, 0);
        let o = [40.0, 40.0];
        let s = size();
        let a = add_rect(o, IMPORT_W);
        assert_eq!(
            hit_test(&v, a[0] + 5.0, a[1] + 5.0, o, s),
            Some(ImportAction::Add)
        );
        let b = browse_rect(o, IMPORT_W);
        assert_eq!(
            hit_test(&v, b[0] + 5.0, b[1] + 5.0, o, s),
            Some(ImportAction::Browse)
        );
        let p = path_rect(o, IMPORT_W);
        assert_eq!(
            hit_test(&v, p[0] + 5.0, p[1] + 5.0, o, s),
            Some(ImportAction::FocusPath)
        );
        let r1 = row_rect(o, IMPORT_W, 1);
        assert_eq!(
            hit_test(&v, r1[0] + 5.0, r1[1] + 5.0, o, s),
            Some(ImportAction::Open(1))
        );
        let r5 = row_rect(o, IMPORT_W, 5);
        assert_eq!(
            hit_test(&v, r5[0] + 5.0, r5[1] + 5.0, o, s),
            Some(ImportAction::Consume),
            "past the last row the click is swallowed"
        );
        assert_eq!(hit_test(&v, 5000.0, 5000.0, o, s), None);
    }

    #[test]
    fn apply_draws_header_rows_and_empty_list_note() {
        let mut world = injected_world();
        let r = rows(2);
        apply(&mut world, Some(&view(&r, 0)), [20.0, 20.0], size());
        let title = world
            .query::<TextLabel>()
            .find(|l| l.asset_id == TITLE_LABEL)
            .unwrap();
        assert!(title.visible && title.content == "Import");
        let header = world
            .query::<TextLabel>()
            .find(|l| l.asset_id == LIST_HEADER)
            .unwrap();
        assert_eq!(header.content, "Imports (2)");
        let first = world
            .query::<TextLabel>()
            .find(|l| l.asset_id == row_label(0))
            .unwrap();
        assert!(first.visible && first.content == "import 0");
        let input = world
            .query::<TextInput>()
            .find(|t| t.asset_id == PATH_INPUT)
            .unwrap();
        assert!(input.visible && input.focused);
        let browse = world
            .query::<TextLabel>()
            .find(|l| l.asset_id == BROWSE_LABEL)
            .unwrap();
        assert!(browse.visible && browse.content == "Browse...");
        // An empty list keeps the header but no rows.
        apply(&mut world, Some(&view(&[], 0)), [20.0, 20.0], size());
        let header = world
            .query::<TextLabel>()
            .find(|l| l.asset_id == LIST_HEADER)
            .unwrap();
        assert_eq!(header.content, "Imports (none yet)");
        assert!(
            !world
                .query::<Sprite>()
                .find(|s| s.asset_id == row_bg(0))
                .unwrap()
                .visible
        );
    }

    #[test]
    fn long_list_shows_the_scrollbar_and_maps_scrolled_rows() {
        let mut world = injected_world();
        let r = rows(30);
        let v = view(&r, 10);
        apply(&mut world, Some(&v), [20.0, 20.0], size());
        assert!(
            world
                .query::<Sprite>()
                .find(|s| s.asset_id == LIST_THUMB)
                .unwrap()
                .visible
        );
        let o = [20.0, 20.0];
        let r0 = row_rect(o, IMPORT_W, 0);
        assert_eq!(
            hit_test(&v, r0[0] + 5.0, r0[1] + 5.0, o, size()),
            Some(ImportAction::Open(10))
        );
    }

    #[test]
    fn taller_size_reveals_more_rows_up_to_the_pool() {
        // The default height shows exactly IMPORT_POOL rows; taller reveals more,
        // capped at the injected pool.
        assert_eq!(visible_rows(size()[1]), IMPORT_POOL);
        assert_eq!(visible_rows(size()[1] + 5.0 * ROW_H), IMPORT_POOL + 5);
        assert_eq!(visible_rows(max_size()[1]), IMPORT_POOL_MAX);
        assert_eq!(
            visible_rows(10_000.0),
            IMPORT_POOL_MAX,
            "capped at the pool"
        );
    }

    #[test]
    fn wider_width_grows_the_row_caption_budget() {
        assert_eq!(max_row_chars(IMPORT_W), MAX_ROW_CHARS);
        assert!(max_row_chars(IMPORT_W + 200.0) > MAX_ROW_CHARS);
    }

    // A taller panel draws rows past the default pool that a default one would
    // never show.
    #[test]
    fn taller_panel_draws_rows_past_the_default_pool() {
        let mut world = injected_world();
        let r = rows(IMPORT_POOL_MAX);
        let tall = [IMPORT_W, max_size()[1]];
        apply(&mut world, Some(&view(&r, 0)), [20.0, 20.0], tall);
        // The row just past the default pool is visible only because we grew.
        assert!(
            world
                .query::<TextLabel>()
                .find(|l| l.asset_id == row_label(IMPORT_POOL))
                .unwrap()
                .visible
        );
    }

    #[test]
    fn hide_all_blanks_every_element() {
        let mut world = injected_world();
        let r = rows(2);
        apply(&mut world, Some(&view(&r, 0)), [20.0, 20.0], size());
        apply(&mut world, None, [0.0, 0.0], size());
        assert!(world.query::<Sprite>().all(|s| !s.visible));
        assert!(world.query::<TextLabel>().all(|l| !l.visible));
        assert!(world.query::<TextInput>().all(|t| !t.visible));
    }
}