drawbar 0.7.1

Your Nord's sounds, in a window: browse, edit and send programs, samples and pianos, in the browser or on the desktop
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
//! The right dock: what is picked, and — while one is attached — the instrument.
//!
//! Two dock headers rather than a dock header and two groups. SELECTION heads the dock
//! itself and answers about the rows the browser has picked, whatever is on the bus; it
//! is flat, because a fact, a tag and a dependency are three lines about one selection
//! rather than three panels. INSTRUMENT heads what an attached instrument has to say,
//! so it is absent without one, and ROOM and INFO under it collapse on their own and are
//! kept between sessions beside the docks.
//!
//! Nothing here reads anything the rest of the app has not already been told — a panel
//! with nothing behind it says so rather than filling itself in.

use eframe::egui;

use nord_usb::wire::Dependency;
use nord_usb::{Location, ObjectClass};

use crate::app::{good, ui as ui_text};
use crate::browser::{Act, Browser, Item, Kind};
use crate::device::{fit, occupancy, Device, Fit};
use crate::icon::{painted, Glyph};
use crate::library::{row_of, Row, Where};
use crate::panel::{chip, dock_header, panel_header};
use crate::queue::Queue;
use crate::room;
use crate::shell::Shell;
use crate::strings::{kind_word, place};
use crate::tags::Tags;
use crate::workspace::Workspace;

/// The room a panel's body keeps at each end, and the gap between its parts.
const PAD: i8 = 8;
const GAP: f32 = 6.0;

/// A glyph in a line, and the smaller one a tag's chip wears.
const GLYPH: f32 = 12.0;
const TAG: f32 = 11.0;

/// The mono readout beside a meter, and the words under one.
const MONO: f32 = 10.5;

/// The column a fact's own word takes, so the values under each other line up.
const FACT: f32 = 52.0;

/// The dock's two headers, in the order the design stacks them.
pub fn ui(
    ui: &mut egui::Ui,
    shell: &mut Shell,
    browser: &mut Browser,
    workspace: &Workspace,
    device: &Device,
    queue: &Queue,
) -> Vec<Act> {
    dock_header(ui, "selection");
    let acts = selection(ui, browser, workspace, device, queue);
    if !device.state.connected() {
        return acts;
    }
    dock_header(ui, "instrument");
    panel_header(ui, "room", Some(&mut shell.room_open), None);
    if shell.room_open {
        room_panel(ui, workspace, device, queue);
    }
    panel_header(ui, "info", Some(&mut shell.info_open), None);
    if shell.info_open {
        body(ui, |ui| crate::browser::about(ui, device));
    }
    acts
}

/// What is picked: what it is, what it is labelled with, and what it plays — one run of
/// lines under one header.
fn selection(
    ui: &mut egui::Ui,
    browser: &Browser,
    workspace: &Workspace,
    device: &Device,
    queue: &Queue,
) -> Vec<Act> {
    let mut acts = Vec::new();
    let picked = browser.picked().items().count();
    if picked == 0 {
        body(ui, |ui| faint(ui, "Nothing is picked."));
        return acts;
    }
    let rows: Vec<Row> = browser
        .picked()
        .items()
        .filter_map(|item| row_of(item, workspace, &device.state, queue, browser.tags()))
        .collect();

    body(ui, |ui| {
        about_selection(ui, picked, &rows, workspace, device)
    });
    tags(ui, &browser.picked().locals(), browser.tags(), &mut acts);
    // Only a slot the instrument has been asked about has a dependency list, so the
    // lines are absent rather than empty for everything else.
    let answered = needs(&slots(browser), device);
    if !answered.is_empty() {
        dependencies(ui, &answered, device);
    }
    acts
}

/// One line of the FACTS panel: what it is, what it says, and the whole of it where the
/// short form leaves something out.
pub struct Fact {
    pub what: &'static str,
    pub said: String,
    pub hint: Option<String>,
}

/// What the panel says about the one asset that is picked.
///
/// The row is the table's own, so a fact here is the fact the table shows. `fit` is
/// what the attached instrument makes of it, and it is worth a line only where it
/// refuses: everything else is either silence or the row's own kind.
pub fn facts(row: &Row, fit: &Fit) -> Vec<Fact> {
    let mut said = vec![
        Fact {
            what: "name",
            said: crate::strings::display_name(&row.name).to_string(),
            hint: (crate::strings::display_name(&row.name) != row.name).then(|| row.name.clone()),
        },
        Fact {
            what: "kind",
            said: kind_word(row.kind, row.family),
            hint: None,
        },
        Fact {
            what: "where",
            said: row.where_.short().to_string(),
            hint: Some(row.where_.sentence().to_string()),
        },
        Fact {
            what: "size",
            said: room::measure(row.size),
            hint: None,
        },
    ];
    if let Fit::Refuses(why) = fit {
        said.push(Fact {
            what: "refused",
            said: why.clone(),
            hint: None,
        });
    }
    said
}

/// What the panel says about a selection of several: how many were picked, and what of
/// them the two things worth acting on hold.
///
/// ⚠️ `picked` counts the rows the browser holds; the other two count the assets among
/// them, which a folder or a tag row is not.
pub fn tally(picked: usize, rows: &[Row]) -> String {
    let unsaved = rows.iter().filter(|row| row.unsaved).count();
    let keyboard = rows
        .iter()
        .filter(|row| matches!(row.where_, Where::Both(_) | Where::Keyboard))
        .count();
    format!("{picked} picked, {unsaved} unsaved, {keyboard} on the keyboard")
}

/// The FACTS panel: one picked row read out, or a count of the several that are.
fn about_selection(
    ui: &mut egui::Ui,
    picked: usize,
    rows: &[Row],
    workspace: &Workspace,
    device: &Device,
) {
    let [row] = rows else {
        return void(ui, tally(picked, rows));
    };
    if picked > 1 {
        // A folder picked beside one asset is still several picked.
        return void(ui, tally(picked, rows));
    }
    let held = row
        .item
        .local()
        .and_then(|id| workspace.get(id))
        .map(|entity| fit(&device.state, entity))
        .unwrap_or(Fit::Unattached);
    for fact in facts(row, &held) {
        fact_line(ui, fact);
    }
}

/// One fact: its own word in a fixed column, and the whole of what it says beside it.
///
/// ⚠️ The value wraps rather than truncating. A refusal, a where sentence and a name are
/// each as long as they are, and half of one is not a fact.
fn fact_line(ui: &mut egui::Ui, fact: Fact) {
    ui.horizontal(|ui| {
        ui.add_sized(
            [FACT, ui.spacing().interact_size.y],
            egui::Label::new(egui::RichText::new(fact.what).text_style(ui_text()).weak())
                .halign(egui::Align::LEFT),
        );
        let said =
            ui.add(egui::Label::new(egui::RichText::new(&fact.said).text_style(ui_text())).wrap());
        if let Some(hint) = fact.hint {
            said.on_hover_text(hint);
        }
    });
}

/// A line the panel says about the selection as a whole rather than about a field.
fn void(ui: &mut egui::Ui, said: String) {
    ui.label(egui::RichText::new(said).text_style(ui_text()));
}

/// The slots the selection holds. Only a slot has a dependency list on the instrument.
fn slots(browser: &Browser) -> Vec<(ObjectClass, Location)> {
    browser
        .picked()
        .items()
        .filter_map(|item| match item {
            Item::Slot { class, at } => Some((class, at)),
            _ => None,
        })
        .collect()
}

/// One meter per folder the instrument has counted, and the one sentence saying what the
/// queue runs into.
fn room_panel(ui: &mut egui::Ui, workspace: &Workspace, device: &Device, queue: &Queue) {
    body(ui, |ui| {
        let mut drawn = 0;
        for class in device.state.classes() {
            let unit = device.state.allocation_unit(class);
            let banks = device.state.banks(class);
            let Some(held) = room::meter(
                class,
                &device.state.inventory,
                unit,
                banks,
                queue,
                workspace,
            ) else {
                continue;
            };
            drawn += 1;
            ui.horizontal(|ui| {
                ui.label(
                    egui::RichText::new(device.state.folder_name(class)).text_style(ui_text()),
                );
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    // ⚠️ The bar takes the tone and the readout keeps its own ink: the
                    // signal colours do not carry as 10 px figures on the panel.
                    if let Some(said) = occupancy(class, &device.state.inventory, unit) {
                        ui.label(egui::RichText::new(said).monospace().size(MONO).weak());
                    }
                });
            });
            room::bar(ui, held);
            ui.add_space(GAP);
        }
        if drawn == 0 {
            return faint(ui, "Nothing has counted what is on the instrument.");
        }
        if let Some(said) = room::constraint(queue, workspace, &device.state) {
            ui.label(egui::RichText::new(said).text_style(ui_text()).weak());
        }
    });
}

/// The dependency list the instrument gave for `at`, where that is the slot it was last
/// asked about and it named something.
///
/// ⚠️ `DEPENDENCIES` answers for one slot at a time and the cache holds the last answer,
/// so this speaks for the slot that was asked about — that class at that address — and
/// for no other. A selection nothing has asked about has no answer, which is not the
/// same as needing nothing.
fn answer(device: &Device, slot: (ObjectClass, Location)) -> Option<&[Dependency]> {
    if device.state.detail.at != Some(slot) {
        return None;
    }
    let deps = device.state.detail.deps.as_deref()?;
    (!deps.is_empty()).then_some(deps)
}

/// The picked slots the instrument has answered about — what the panel would have to
/// say, and so whether there is a panel at all.
fn needs(picked: &[(ObjectClass, Location)], device: &Device) -> Vec<(ObjectClass, Location)> {
    picked
        .iter()
        .copied()
        .filter(|slot| answer(device, *slot).is_some())
        .collect()
}

/// What the instrument said the picked slots need.
fn dependencies(ui: &mut egui::Ui, answered: &[(ObjectClass, Location)], device: &Device) {
    body(ui, |ui| {
        for (class, at) in answered.iter().copied() {
            let Some(deps) = answer(device, (class, at)) else {
                continue;
            };
            for dep in deps {
                let named = device
                    .state
                    .dependency_name(Some((class, at)), dep.class, dep.id)
                    .filter(|name| !name.is_empty());
                needed(ui, dep.class, named, dep.id);
            }
            ui.label(
                egui::RichText::new(place(class, at))
                    .monospace()
                    .size(MONO)
                    .weak(),
            );
        }
    });
}

/// One library a slot names: the name the instrument gave it, or the bare id nothing has
/// resolved.
fn needed(ui: &mut egui::Ui, class: ObjectClass, named: Option<&str>, id: u32) {
    ui.horizontal(|ui| {
        let quiet = ui.visuals().weak_text_color();
        let lit = good(ui.visuals());
        mark(ui, Kind::from_class(class).glyph(), GLYPH, quiet);
        match named {
            Some(name) => {
                ui.label(egui::RichText::new(name).text_style(ui_text()));
                ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                    ui.label(
                        egui::RichText::new("installed")
                            .text_style(ui_text())
                            .color(lit),
                    );
                });
            }
            None => {
                ui.label(
                    egui::RichText::new(format!("{id:#010x}"))
                        .monospace()
                        .size(MONO)
                        .color(quiet),
                )
                .on_hover_text(format!(
                    "this names a {} the instrument has not listed by id",
                    Kind::from_class(class).chip()
                ));
            }
        }
    });
}

/// The tags the selection wears, as chips: solid where the whole of it wears one and
/// hollow where only some does. A click takes a solid one off all of it and puts a
/// hollow one on all of it.
///
/// ⚠️ Only a **kept** asset can wear one — a tag hangs on a workspace id, and a slot has
/// none — so this is over what of the selection is on this computer.
///
/// ⚠️ Toggling only. A tag is made in the browser's own TAGS section, which is where the
/// list of them lives and where one is renamed and removed, and put on something new
/// from the row's own Tag menu.
fn tags(ui: &mut egui::Ui, picked: &[u64], worn: &Tags, acts: &mut Vec<Act>) {
    let wearing = wearing(picked, worn);
    if wearing.is_empty() {
        return;
    }
    body(ui, |ui| {
        ui.horizontal_wrapped(|ui| {
            for (id, name, on_all) in wearing {
                let visuals = ui.visuals().clone();
                let (tint, fill) = match on_all {
                    true => (visuals.text_color(), Some(visuals.faint_bg_color)),
                    false => (crate::app::caption(&visuals), None),
                };
                let drawn = chip(ui, Glyph::Tag, TAG, name, tint, fill);
                let clicked = ui
                    .interact(drawn.rect, drawn.id.with(id), egui::Sense::click())
                    .on_hover_text(match on_all {
                        true => "on everything picked — click to take it off all of it",
                        false => "on some of what is picked — click to put it on all of it",
                    })
                    .clicked();
                if clicked {
                    let ids = picked.to_vec();
                    acts.push(match on_all {
                        true => Act::Untag { ids, tag: id },
                        false => Act::Tag { ids, tag: id },
                    });
                }
            }
        });
    });
}

/// The tags something picked wears, and whether each is on every one of it.
///
/// ⚠️ Worn tags only, in the list's own order. A tag nothing picked wears is not a state
/// of this selection — the whole list of them is the tree's, and putting a new one on is
/// the row's own Tag menu.
fn wearing<'a>(picked: &[u64], worn: &'a Tags) -> Vec<(u64, &'a str, bool)> {
    worn.all()
        .iter()
        .filter(|tag| picked.iter().any(|id| worn.worn(*id).contains(&tag.id)))
        .map(|tag| (tag.id, tag.name.as_str(), worn.on_all(picked, tag.id)))
        .collect()
}

/// A glyph in a line, claiming its own box so the words after it line up.
fn mark(ui: &mut egui::Ui, glyph: Glyph, size: f32, tint: egui::Color32) {
    let (rect, _) = ui.allocate_exact_size(egui::Vec2::splat(size), egui::Sense::hover());
    painted(ui, glyph, rect, tint);
}

/// A panel's body: padded at each end, with its lines under each other.
fn body<R>(ui: &mut egui::Ui, contents: impl FnOnce(&mut egui::Ui) -> R) -> R {
    egui::Frame::new()
        .inner_margin(egui::Margin::symmetric(PAD, 6))
        .show(ui, |ui| {
            ui.spacing_mut().item_spacing = egui::vec2(GAP, 4.0);
            contents(ui)
        })
        .inner
}

/// The line a panel shows where there is nothing to say.
fn faint(ui: &mut egui::Ui, said: &str) {
    ui.label(
        egui::RichText::new(said)
            .text_style(ui_text())
            .weak()
            .italics(),
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::browser::apply;
    use crate::device::Detail;
    use crate::log::Log;
    use crate::tabs::Tabs;
    use crate::workspace::Fresh;
    use nord_usb::wire::{Dependency, Status};

    fn context() -> egui::Context {
        let ctx = egui::Context::default();
        ctx.all_styles_mut(crate::app::metrics);
        ctx
    }

    /// An instrument that has counted a library, holds a program, and has been asked what
    /// one of its slots plays — one library it named and one it did not.
    fn attached(ctx: &egui::Context) -> (Device, Location) {
        let at = Location { bank: 6, slot: 0 };
        let mut device = Device::new(ctx.clone());
        device.pretend_scanned(ObjectClass::Program, 7, &["Africa Split"]);
        device.pretend_partitions(&crate::device::ELECTRO5);
        device.state.inventory.push(Status {
            class: ObjectClass::Sample,
            count: 84,
            free: 60,
            used: 1472,
            dirty: 0,
            spare: 4,
        });
        device.state.detail = Detail {
            at: Some((ObjectClass::Program, at)),
            info: Some(None),
            deps: Some(vec![
                Dependency {
                    flag: 1,
                    class: ObjectClass::Piano,
                    id: 0x0102_0304,
                    name: "Royal Grand 3D ".into(),
                    location: None,
                },
                Dependency {
                    flag: 1,
                    class: ObjectClass::Sample,
                    id: 0x0999_0999,
                    name: "   ".into(),
                    location: None,
                },
            ]),
        };
        (device, at)
    }

    /// Draw the whole inspector over `picked`, and the two panels that take a selection
    /// of their own.
    ///
    /// Nothing checks pixels. What this catches is a layout that panics or an id that
    /// collides, neither of which a test on the rules would see.
    fn paint(
        shell: &mut Shell,
        device: &Device,
        picked: &[(ObjectClass, Location)],
    ) -> Vec<String> {
        let ctx = context();
        let mut browser = Browser::default();
        for (class, at) in picked.iter().copied() {
            browser.check(Item::Slot { class, at });
        }
        let workspace = Workspace::new(ctx.clone());
        let queue = Queue::default();
        let mut labels = Tags::default();
        let sunday = labels.make("Sunday").unwrap();
        labels.set(7, sunday, true);
        let mut said = Vec::new();
        // Twice: the second pass runs with the widget state the first left behind.
        for _ in 0..2 {
            let output = ctx.run(egui::RawInput::default(), |ctx| {
                egui::SidePanel::right("inspector")
                    .exact_width(crate::shell::INSPECTOR)
                    .show(ctx, |panel| {
                        super::ui(panel, shell, &mut browser, &workspace, device, &queue);
                        dependencies(panel, picked, device);
                        tags(panel, &[7, 8], &labels, &mut Vec::new());
                    });
            });
            said = crate::browser::bench::words(&output);
        }
        said
    }

    /// SELECTION heads the dock whatever is on the bus; INSTRUMENT is only there while
    /// one is attached, because everything under it is something an instrument said.
    #[test]
    fn instrument_is_headed_only_while_one_is_attached() {
        let ctx = context();
        let mut shell = Shell {
            info_open: true,
            ..Shell::default()
        };
        let detached = paint(&mut shell, &Device::new(ctx.clone()), &[]);
        assert!(
            detached.iter().any(|word| word == "SELECTION"),
            "{detached:?}"
        );
        assert!(
            !detached.iter().any(|word| word == "INSTRUMENT"),
            "{detached:?}"
        );

        let (device, at) = attached(&ctx);
        let held = paint(&mut shell, &device, &[(ObjectClass::Program, at)]);
        for header in ["SELECTION", "INSTRUMENT", "ROOM", "INFO"] {
            assert!(held.iter().any(|word| word == header), "{header}: {held:?}");
        }

        // Shut, each panel under INSTRUMENT draws its header and nothing under it.
        let mut shut = Shell {
            room_open: false,
            info_open: false,
            ..Shell::default()
        };
        paint(&mut shut, &device, &[(ObjectClass::Program, at)]);
    }

    /// The facts about one picked asset are the ones its library row already carries,
    /// so the panel and the table cannot disagree.
    #[test]
    fn the_facts_of_one_picked_asset_are_its_rows_own() {
        use nord_format::accept::Family;

        let row = Row {
            item: Item::Local(1),
            kind: Kind::Program,
            family: Some(Family::Stage4),
            name: "Africa Split".into(),
            tags: 1,
            unsaved: true,
            where_: Where::Both(Some(false)),
            at: None,
            size: 2_048,
            needs: crate::library::Needs::Nothing,
        };

        let said = facts(&row, &Fit::Takes);
        let lines: Vec<(&str, &str)> = said
            .iter()
            .map(|fact| (fact.what, fact.said.as_str()))
            .collect();
        assert_eq!(
            lines,
            [
                ("name", "Africa Split"),
                ("kind", "Stage 4 program"),
                ("where", "both ≠"),
                ("size", "2.0 kB"),
            ]
        );
        assert_eq!(
            said[2].hint.as_deref(),
            Some(Where::Both(Some(false)).sentence()),
            "the short word carries the whole of it"
        );

        // The one thing the panel says that the row does not know: what the attached
        // instrument makes of it, and only where that is a refusal.
        let why = "This is a Stage 4 file and the instrument is a Nord Electro 5D.";
        let refused = facts(&row, &Fit::Refuses(why.into()));
        assert_eq!(refused.len(), said.len() + 1);
        assert_eq!(refused[4].what, "refused");
        assert_eq!(refused[4].said, why);
        assert_eq!(
            facts(&row, &Fit::Warn("untried".into())).len(),
            said.len(),
            "an untried write is the queue's warning, not a fact about the asset"
        );
    }

    /// ⚠️ A refusal is a sentence, and a sentence the dock cannot fit on one line is laid
    /// out on more of them rather than cut off at the panel's edge.
    #[test]
    fn a_fact_too_long_for_the_dock_wraps_rather_than_truncating() {
        let ctx = context();
        let lines = |said: &str| {
            let output = ctx.run(egui::RawInput::default(), |ctx| {
                egui::SidePanel::right("inspector")
                    .exact_width(crate::shell::INSPECTOR)
                    .show(ctx, |ui| {
                        fact_line(
                            ui,
                            Fact {
                                what: "refused",
                                said: said.to_string(),
                                hint: None,
                            },
                        );
                    });
            });
            crate::browser::bench::galleys(&output)
                .iter()
                .find(|galley| galley.text() == said)
                .map(|galley| galley.rows.len())
        };

        assert_eq!(lines("2.0 kB"), Some(1), "a short value keeps its one line");
        let why = "This is a Stage 4 file and the instrument is a Nord Electro 5D.";
        assert!(
            lines(why).is_some_and(|rows| rows > 1),
            "the whole refusal is painted: {:?}",
            lines(why)
        );
    }

    /// Several picked is a count rather than a reading-out, and the two counts are the
    /// two that decide what can be done with the set.
    #[test]
    fn a_selection_of_several_says_how_many_and_what_of_them() {
        let row = |name: &str, unsaved: bool, where_: Where| Row {
            item: Item::Local(1),
            kind: Kind::Program,
            family: None,
            name: name.into(),
            tags: 0,
            unsaved,
            where_,
            at: None,
            size: 0,
            needs: crate::library::Needs::Nothing,
        };
        let rows = [
            row("kept", false, Where::Computer),
            row("edited", true, Where::Both(Some(false))),
            row("read off a slot", false, Where::Keyboard),
        ];
        assert_eq!(tally(3, &rows), "3 picked, 1 unsaved, 2 on the keyboard");

        // A folder is picked and is no asset, so it is counted as picked and as nothing
        // else.
        assert_eq!(tally(1, &[]), "1 picked, 0 unsaved, 0 on the keyboard");
    }

    /// ⚠️ A dependency list answers for the slot it was asked about — that class at that
    /// address — and for no other, so a selection elsewhere is *not asked* rather than
    /// *needs nothing*. Every class is addressed in the same banks and slots, so the
    /// sample at a program's own address must not be shown the program's list.
    #[test]
    fn a_selection_the_instrument_was_not_asked_about_shows_nothing() {
        let ctx = context();
        let (device, at) = attached(&ctx);
        assert_eq!(
            needs(&[(ObjectClass::Program, at)], &device),
            [(ObjectClass::Program, at)],
            "the slot it was asked about"
        );
        assert!(
            needs(&[(ObjectClass::Sample, at)], &device).is_empty(),
            "another class at the same address"
        );
        let elsewhere = Location { bank: 0, slot: 0 };
        assert!(
            needs(&[(ObjectClass::Program, elsewhere)], &device).is_empty(),
            "a program nothing has asked about"
        );
        // And the dependency whose name came back blank has nothing but its id to show.
        assert_eq!(
            device
                .state
                .dependency_name(
                    Some((ObjectClass::Program, at)),
                    ObjectClass::Sample,
                    0x0999_0999
                )
                .filter(|name| !name.is_empty()),
            None,
        );
    }

    /// The selection wears chips rather than the whole list: only the tags something
    /// picked wears, solid where every picked asset wears one and hollow where some do.
    /// A selection wearing none is a section that paints nothing where they would be.
    #[test]
    fn the_selections_tags_are_chips_and_nothing_at_all_where_there_are_none() {
        let ctx = context();
        let mut labels = Tags::default();
        let (both, some) = (labels.make("Sunday").unwrap(), labels.make("Loud").unwrap());
        for tag in [both, some] {
            labels.set(7, tag, true);
        }
        labels.set(8, both, true);

        assert_eq!(
            wearing(&[7, 8], &labels),
            [(both, "Sunday", true), (some, "Loud", false)]
        );
        assert!(wearing(&[], &labels).is_empty(), "nothing picked");
        assert!(
            wearing(&[9], &labels).is_empty(),
            "picked, and wearing none"
        );

        let painted = |picked: &[u64]| {
            let output = ctx.run(egui::RawInput::default(), |ctx| {
                egui::SidePanel::right("inspector")
                    .exact_width(crate::shell::INSPECTOR)
                    .show(ctx, |panel| tags(panel, picked, &labels, &mut Vec::new()));
            });
            crate::browser::bench::words(&output)
        };
        let said = painted(&[7, 8]);
        for name in ["Sunday", "Loud"] {
            assert!(said.contains(&name.to_string()), "{name}: {said:?}");
        }
        assert!(painted(&[9]).is_empty(), "{:?}", painted(&[9]));
    }

    /// A tag goes on everything picked and comes off it again, which is the whole of what
    /// the panel asks for.
    #[test]
    fn a_tag_goes_on_the_whole_selection_and_comes_off_it_again() {
        let ctx = context();
        let mut workspace = Workspace::new(ctx.clone());
        let mut device = Device::new(ctx);
        let mut browser = Browser::default();
        let mut queue = Queue::default();
        let mut tabs = Tabs::default();
        let mut log = Log::default();
        let mut shell = Shell::default();
        let ids: Vec<u64> = (0..2)
            .map(|_| workspace.create(Fresh::Program, &mut log).unwrap())
            .collect();

        let mut run = |browser: &mut Browser, acts| {
            apply(
                browser,
                &mut shell,
                acts,
                &mut workspace,
                &mut device,
                &mut tabs,
                &mut queue,
                &mut log,
            )
        };
        run(&mut browser, vec![Act::NewTag("Sunday".into())]);
        let tag = browser.tags().all()[0].id;
        assert!(!browser.tags().on_all(&ids, tag), "hollow to begin with");

        run(
            &mut browser,
            vec![Act::Tag {
                ids: ids.clone(),
                tag,
            }],
        );
        assert!(browser.tags().on_all(&ids, tag), "a hollow one goes on all");

        run(
            &mut browser,
            vec![Act::Untag {
                ids: ids.clone(),
                tag,
            }],
        );
        assert!(!browser.tags().on_all(&ids, tag), "a solid one comes off");
    }
}