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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
//! The dock shell: the regions the window is cut into, what is collapsed, and the
//! menus, toolbar and status bar that sit around the centre.
//!
//! Panels claim space in the order [`crate::app::DrawbarApp::update`] adds them. The
//! three open docks resize by dragging the edge they show the centre, between the least
//! each is worth opening to and whatever leaves the centre [`CENTRE_WIDE`] by
//! [`CENTRE_TALL`]; a collapsed rail is [`SHUT`] and does not. Each contents pads
//! itself, so a panel header can bleed to both edges.

use eframe::egui;
use nord_usb::ObjectClass;

use crate::app::{accent, bold, ui as ui_text, DrawbarApp, ThemeChoice};
use crate::browser::{new_menu, Act};
use crate::device::occupancy;
use crate::filter::Filter;
use crate::icon::{icon, sized, Glyph};
use crate::log::Level;
use crate::panel::{caps, chevron, dock_header, flat, strip, DOCK, GAP, GLYPH, PAD};
use crate::strings::folder;
use crate::tabs::Spot;

/// The window's own bar: the mark, the menus, the instrument, the theme.
pub const TITLEBAR: f32 = 30.0;

/// The bar of actions under it.
pub const TOOLBAR: f32 = 32.0;

/// One line of plain words at the foot of the window.
pub const STATUS: f32 = 22.0;

/// The bottom dock's body, under its header, and the least it is worth opening to.
pub const DOCK_BODY: f32 = 184.0;
const BODY_LEAST: f32 = 120.0;

/// The side docks open, shut, and the least either is worth opening to.
pub const BROWSER: f32 = 232.0;
pub const INSPECTOR: f32 = 244.0;
pub const SHUT: f32 = 30.0;
const SIDE_LEAST: f32 = 180.0;

/// What the centre keeps however far a dock is dragged. The most a dock may be dragged
/// to is whatever leaves this, so it is read off the room left rather than stored.
const CENTRE_WIDE: f32 = 300.0;
const CENTRE_TALL: f32 = 200.0;

/// The least room the whole shell lays out in: the three docks at the least each opens
/// to, around a centre that still keeps [`CENTRE_WIDE`] by [`CENTRE_TALL`].
///
/// A window is held above this by its own minimum size; a browser tab is any size the
/// device is, so the web build shows [`too_small_notice`] instead of a shell that cannot
/// fit.
pub const LEAST: egui::Vec2 = egui::vec2(
    SIDE_LEAST + CENTRE_WIDE + SIDE_LEAST,
    TITLEBAR + TOOLBAR + STATUS + DOCK + BODY_LEAST + CENTRE_TALL,
);

/// What that notice says, and what `index.html` says before the module has loaded.
const TOO_SMALL: &str = "drawbar needs a larger screen";
const TOO_SMALL_WHY: &str = "It is a desktop application: its panels need more room \
                             than a phone or a narrow window has. Open it on a larger \
                             screen, or make this window wider.";

/// The check beside a menu item, and the height of a control.
const CHECK: f32 = 12.0;
const BUTTON: f32 = 22.0;

/// The omnibox's least width. Below this it is a box nobody can read a name in.
const OMNIBOX: f32 = 300.0;

/// The omnibox's own widget id.
///
/// ⚠️ The controls before it come and go with the instrument. An id counted off its
/// neighbours would change under it as one attaches, and the box would lose the focus
/// and the cursor mid-word.
const SEARCH: &str = "omnibox";

/// The least width a drop-down takes, whatever is in it.
///
/// ⚠️ A menu sizes itself to its widest item, so without this each one is as wide as
/// whatever happens to be enabled — and the longest item, "Inspector panel" with ⌥⌘I,
/// leaves its key text against its label. This is that item with a gap between the two.
const MENU: f32 = 240.0;

/// Which dock a toggle is about.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Dock {
    Browser,
    Inspector,
    Bottom,
}

/// Which of the bottom dock's two pages its body shows.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum Page {
    #[default]
    Queue,
    Log,
}

impl Page {
    fn title(self) -> &'static str {
        match self {
            Page::Queue => "send queue",
            Page::Log => "activity log",
        }
    }

    fn stored(self) -> &'static str {
        match self {
            Page::Queue => "queue",
            Page::Log => "log",
        }
    }
}

/// What is open, and what the omnibox holds.
pub struct Shell {
    pub browser_open: bool,
    pub inspector_open: bool,
    pub dock_open: bool,
    /// The two panels under the right dock's INSTRUMENT header, each collapsed on its
    /// own. SELECTION is flat and has none.
    pub room_open: bool,
    pub info_open: bool,
    /// How far each dock was last dragged. A side dock's is its width, the bottom
    /// dock's is its body under [`DOCK`].
    pub browser_width: f32,
    pub inspector_width: f32,
    pub dock_body: f32,
    pub page: Page,
    /// What has been typed into the omnibox: the name the library's table is narrowed
    /// by.
    pub omnibox: String,
    /// What else the library is narrowed to, as the tree's kind, tag and place rows ask
    /// for it.
    pub filter: Filter,
}

impl Default for Shell {
    fn default() -> Shell {
        Shell {
            browser_open: true,
            inspector_open: true,
            dock_open: false,
            room_open: true,
            info_open: false,
            browser_width: BROWSER,
            inspector_width: INSPECTOR,
            dock_body: DOCK_BODY,
            page: Page::default(),
            omnibox: String::new(),
            filter: Filter::default(),
        }
    }
}

impl Shell {
    /// Where the layout is kept between sessions, beside the browser's own keys.
    pub const KEY: &'static str = "drawbar.docks";

    const VERSION: &'static str = "drawbar docks 5";

    pub fn open(&self, dock: Dock) -> bool {
        match dock {
            Dock::Browser => self.browser_open,
            Dock::Inspector => self.inspector_open,
            Dock::Bottom => self.dock_open,
        }
    }

    pub fn toggle(&mut self, dock: Dock) {
        let held = !self.open(dock);
        match dock {
            Dock::Browser => self.browser_open = held,
            Dock::Inspector => self.inspector_open = held,
            Dock::Bottom => self.dock_open = held,
        }
    }

    /// Open the bottom dock on a page, which is what asking for either page means.
    pub fn show_page(&mut self, page: Page) {
        self.dock_open = true;
        self.page = page;
    }

    /// Put the docks back where they were left.
    ///
    /// ⚠️ A version this build does not know is refused rather than guessed at: half a
    /// layout is a window nobody arranged.
    pub fn restore(&mut self, storage: &dyn eframe::Storage) {
        let Some(text) = storage.get_string(Shell::KEY) else {
            return;
        };
        let mut lines = text.lines();
        if lines.next() != Some(Shell::VERSION) {
            return;
        }
        let mut held = Shell::default();
        for line in lines {
            let mut parts = line.split('\t');
            match (parts.next(), parts.next()) {
                (Some("browser"), Some(open)) => held.browser_open = open == "1",
                (Some("inspector"), Some(open)) => held.inspector_open = open == "1",
                (Some("dock"), Some(open)) => held.dock_open = open == "1",
                (Some("room"), Some(open)) => held.room_open = open == "1",
                (Some("info"), Some(open)) => held.info_open = open == "1",
                (Some("browser_width"), Some(text)) => {
                    held.browser_width = size(text, SIDE_LEAST, BROWSER)
                }
                (Some("inspector_width"), Some(text)) => {
                    held.inspector_width = size(text, SIDE_LEAST, INSPECTOR)
                }
                (Some("dock_body"), Some(text)) => {
                    held.dock_body = size(text, BODY_LEAST, DOCK_BODY)
                }
                (Some("page"), Some(page)) => {
                    held.page = match page == Page::Log.stored() {
                        true => Page::Log,
                        false => Page::Queue,
                    }
                }
                _ => {}
            }
        }
        self.browser_open = held.browser_open;
        self.inspector_open = held.inspector_open;
        self.dock_open = held.dock_open;
        self.room_open = held.room_open;
        self.info_open = held.info_open;
        self.browser_width = held.browser_width;
        self.inspector_width = held.inspector_width;
        self.dock_body = held.dock_body;
        self.page = held.page;
    }

    pub fn keep(&self, storage: &mut dyn eframe::Storage) {
        let bit = |open: bool| match open {
            true => "1",
            false => "0",
        };
        storage.set_string(
            Shell::KEY,
            format!(
                "{}\nbrowser\t{}\ninspector\t{}\ndock\t{}\nroom\t{}\ninfo\t{}\n\
                 browser_width\t{}\ninspector_width\t{}\ndock_body\t{}\npage\t{}\n",
                Shell::VERSION,
                bit(self.browser_open),
                bit(self.inspector_open),
                bit(self.dock_open),
                bit(self.room_open),
                bit(self.info_open),
                self.browser_width,
                self.inspector_width,
                self.dock_body,
                self.page.stored(),
            ),
        );
    }
}

/// A size the last session left. One this build would not have laid out — under the
/// least the dock opens to, or not a number at all — is not a size, so the default
/// stands. The most is the screen's, and egui clamps to it every frame.
fn size(text: &str, least: f32, default: f32) -> f32 {
    match text.parse::<f32>() {
        Ok(size) if size.is_finite() && size >= least => size,
        _ => default,
    }
}

/// Which of a region's edges faces the centre.
enum Side {
    Top,
    Bottom,
    Left,
    Right,
}

/// The 1 px border a region shows the centre, drawn just inside its own rect.
fn edge(ui: &egui::Ui, side: Side) {
    let rect = ui.max_rect();
    let stroke = egui::Stroke::new(1.0_f32, ui.visuals().widgets.noninteractive.bg_stroke.color);
    let painter = ui.painter();
    match side {
        Side::Top => painter.hline(rect.x_range(), rect.top() + 0.5, stroke),
        Side::Bottom => painter.hline(rect.x_range(), rect.bottom() - 0.5, stroke),
        Side::Left => painter.vline(rect.left() + 0.5, rect.y_range(), stroke),
        Side::Right => painter.vline(rect.right() - 0.5, rect.y_range(), stroke),
    };
}

/// A panel frame that paints its fill and claims no margin of its own.
fn bare(fill: egui::Color32) -> egui::Frame {
    egui::Frame::new()
        .fill(fill)
        .inner_margin(egui::Margin::ZERO)
}

/// A 1 px vertical rule, for the gaps between groups of controls.
fn rule(ui: &mut egui::Ui, height: f32) {
    let (rect, _) = ui.allocate_exact_size(egui::vec2(1.0, height), egui::Sense::hover());
    let stroke = egui::Stroke::new(1.0_f32, ui.visuals().widgets.noninteractive.bg_stroke.color);
    ui.painter().vline(rect.center().x, rect.y_range(), stroke);
}

/// A bar's contents, laid out left to right inside its padding.
fn along<R>(ui: &mut egui::Ui, contents: impl FnOnce(&mut egui::Ui) -> R) -> R {
    let mut inner = ui.new_child(
        egui::UiBuilder::new()
            .max_rect(ui.max_rect().shrink2(egui::vec2(PAD, 0.0)))
            .layout(egui::Layout::left_to_right(egui::Align::Center)),
    );
    inner.spacing_mut().item_spacing.x = GAP;
    contents(&mut inner)
}

/// The keys the menus bind.
mod key {
    use eframe::egui::{Key, KeyboardShortcut as Shortcut, Modifiers as With};

    pub const OPEN: Shortcut = Shortcut::new(With::COMMAND, Key::O);
    pub const SAVE: Shortcut = Shortcut::new(With::COMMAND, Key::S);
    pub const EXPORT: Shortcut = Shortcut::new(With::COMMAND.plus(With::SHIFT), Key::E);
    pub const CLOSE: Shortcut = Shortcut::new(With::COMMAND, Key::W);
    pub const QUIT: Shortcut = Shortcut::new(With::COMMAND, Key::Q);
    pub const KEYBOARD: Shortcut = Shortcut::new(With::COMMAND, Key::Num2);
    pub const DOCUMENT: Shortcut = Shortcut::new(With::COMMAND, Key::Num3);
    pub const BROWSER: Shortcut = Shortcut::new(With::COMMAND.plus(With::ALT), Key::B);
    pub const INSPECTOR: Shortcut = Shortcut::new(With::COMMAND.plus(With::ALT), Key::I);
    pub const DOCK: Shortcut = Shortcut::new(With::COMMAND.plus(With::ALT), Key::L);
    pub const RESYNC: Shortcut = Shortcut::new(With::COMMAND, Key::R);
    pub const QUEUE: Shortcut = Shortcut::new(With::COMMAND.plus(With::SHIFT), Key::S);
}

/// Whether this build may bind a key a browser tab keeps for itself.
///
/// ⚠️ ⌘W, ⌘Q and ⌘2–⌘3 reach the tab, not the page. On the web those items work by
/// click alone.
const WINDOWED: bool = !cfg!(target_arch = "wasm32");

/// The user guide, published beside the browser build.
///
/// Relative in a tab, so the guide is the one on whichever host is serving the app; a
/// window has no page to be relative to and reaches for the published one.
#[cfg(target_arch = "wasm32")]
pub(crate) const GUIDE: &str = "docs/";
#[cfg(not(target_arch = "wasm32"))]
pub(crate) const GUIDE: &str = "https://drawbar.app/docs/";

/// The key text beside a menu label — a window's, never a tab's.
fn keyed(ctx: &egui::Context, shortcut: egui::KeyboardShortcut) -> String {
    match WINDOWED {
        true => ctx.format_shortcut(&shortcut),
        false => String::new(),
    }
}

/// Whether a frame of the omnibox has to bring the library forward.
///
/// Any change to what is typed does. The box narrows the library's table and nothing
/// else, so a search run behind a document tab is a search nobody can see — including
/// the first keystroke into an empty box.
fn searched(before: &str, after: &str) -> bool {
    before != after
}

/// One of the title bar's drop-downs, no narrower than [`MENU`] however little is in it.
fn drop_down(ui: &mut egui::Ui, title: &str, contents: impl FnOnce(&mut egui::Ui)) {
    ui.menu_button(title, |ui| {
        ui.set_min_width(MENU);
        contents(ui);
    });
}

/// A menu item, closing the menu when it is picked.
fn item(ui: &mut egui::Ui, label: &str, shortcut: Option<egui::KeyboardShortcut>) -> bool {
    let mut button = egui::Button::new(label);
    if let Some(shortcut) = shortcut {
        button = button.shortcut_text(keyed(ui.ctx(), shortcut));
    }
    let clicked = ui.add(button).clicked();
    if clicked {
        ui.close();
    }
    clicked
}

/// What a click on one of the bottom dock's page titles asks for.
///
/// ⚠️ The title of the page already showing shuts the dock. It is the only way out that
/// is on the dock itself, and a title that answered a click by doing nothing reads as
/// broken.
fn page_click(page: Page, showing: bool) -> Act {
    match showing {
        true => Act::ToggleDock(Dock::Bottom),
        false => Act::ShowPage(page),
    }
}

/// A menu item that also says whether what it names is on.
///
/// ⚠️ A check at the left rather than a selected button or a `selectable_label`: both
/// fill the row with `selection.bg_fill`, which is the instrument's red and reads as a
/// warning across a menu of ordinary items. **Every** checkable item in the app wears
/// this, wherever its menu is drawn.
pub fn marked(
    ui: &mut egui::Ui,
    label: &str,
    on: bool,
    shortcut: Option<egui::KeyboardShortcut>,
) -> bool {
    let tint = match on {
        true => accent(ui.visuals()),
        false => egui::Color32::TRANSPARENT,
    };
    let mut button = egui::Button::image_and_text(sized(Glyph::Check, CHECK, tint), label)
        .image_tint_follows_text_color(false);
    if let Some(shortcut) = shortcut {
        button = button.shortcut_text(keyed(ui.ctx(), shortcut));
    }
    let clicked = ui.add(button).clicked();
    if clicked {
        ui.close();
    }
    clicked
}

/// One of the toolbar's labelled actions.
///
/// ⚠️ An accented action carries the accent in its glyph alone: accent on panel measures
/// 4.1:1, which fails as 11 px text.
fn action(ui: &mut egui::Ui, glyph: Glyph, label: &str, accented: bool) -> egui::Response {
    ui.scope(|ui| {
        flat(ui);
        let visuals = ui.visuals();
        let quiet = visuals.widgets.inactive.fg_stroke.color;
        let (mark, ink) = match accented {
            true => (accent(visuals), visuals.widgets.active.fg_stroke.color),
            false => (quiet, quiet),
        };
        ui.add(
            egui::Button::image_and_text(
                sized(glyph, GLYPH, mark),
                egui::RichText::new(label).text_style(ui_text()).color(ink),
            )
            .image_tint_follows_text_color(false)
            .corner_radius(2.0)
            .min_size(egui::vec2(0.0, BUTTON)),
        )
    })
    .inner
}

/// The New menu behind a glyph. The tool bar's and the tab strip's are one button, so
/// what New offers is one list reached two ways.
pub(crate) fn new_button(ui: &mut egui::Ui, glyph: Glyph, ink: egui::Color32, acts: &mut Vec<Act>) {
    ui.scope(|ui| {
        flat(ui);
        ui.menu_image_button(sized(glyph, GLYPH, ink), |ui| new_menu(ui, acts))
            .response
            .on_hover_text("something new on this computer");
    });
}

/// A 24 × 22 button carrying one glyph. `on` is a toggle whose dock is open, which is
/// the one state that fills without the pointer on it.
fn glyph_button(ui: &mut egui::Ui, glyph: Glyph, on: bool, hint: &str) -> egui::Response {
    ui.scope(|ui| {
        flat(ui);
        let widgets = &mut ui.visuals_mut().widgets;
        if on {
            widgets.inactive.weak_bg_fill = widgets.active.weak_bg_fill;
        }
        let visuals = ui.visuals();
        let ink = match on {
            true => visuals.widgets.active.fg_stroke.color,
            false => visuals.widgets.inactive.fg_stroke.color,
        };
        ui.add(
            egui::Button::image(sized(glyph, GLYPH, ink))
                .image_tint_follows_text_color(false)
                .corner_radius(2.0)
                .min_size(egui::vec2(24.0, BUTTON)),
        )
    })
    .inner
    .on_hover_text(hint)
}

impl DrawbarApp {
    /// Whether an instrument is answering.
    ///
    /// ⚠️ The single gate on everything that only means something with one attached: the
    /// Read and Send actions, the send queue, the inspector's INSTRUMENT group, and the
    /// menu items naming any of them. A control for an instrument that is not there is a
    /// control that can only disappoint.
    pub(crate) fn attached(&self) -> bool {
        self.device.state.connected()
    }

    /// 30 px: the mark, the menu bar, the instrument, the theme.
    pub(crate) fn titlebar(
        &mut self,
        ctx: &egui::Context,
        frame: &mut eframe::Frame,
        acts: &mut Vec<Act>,
    ) {
        let fill = ctx.style().visuals.window_fill;
        egui::TopBottomPanel::top("titlebar")
            .resizable(false)
            .exact_height(TITLEBAR)
            .frame(bare(fill))
            .show(ctx, |ui| {
                edge(ui, Side::Bottom);
                along(ui, |ui| {
                    icon(ui, Glyph::SlidersVertical, 14.0, accent(ui.visuals()));
                    ui.label(egui::RichText::new("drawbar").font(egui::FontId::new(12.0, bold())));
                    self.shortcuts(ui, acts);
                    ui.scope(|ui| {
                        flat(ui);
                        self.menus(ui, frame, acts);
                    });
                    ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                        flat(ui);
                        self.theme_chip(ui, frame);
                        self.instrument_chip(ui);
                    });
                });
            });
    }

    /// The instrument, and that it is answering.
    fn instrument_chip(&self, ui: &mut egui::Ui) {
        let Some(product) = self.device.state.product() else {
            return;
        };
        let visuals = ui.visuals();
        let ink = visuals.widgets.inactive.fg_stroke.color;
        let lit = crate::app::good(visuals);
        egui::Frame::new()
            .inner_margin(egui::Margin::symmetric(6, 2))
            .show(ui, |ui| {
                ui.spacing_mut().item_spacing.x = GAP;
                icon(ui, Glyph::Keyboard, GLYPH, ink);
                ui.label(
                    egui::RichText::new(product)
                        .text_style(ui_text())
                        .color(ink),
                );
                crate::app::dot(ui, lit, 9.0).on_hover_text("attached");
            });
    }

    fn theme_chip(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) {
        let glyph = match ui.visuals().dark_mode {
            true => Glyph::Moon,
            false => Glyph::Sun,
        };
        let ink = ui.visuals().widgets.inactive.fg_stroke.color;
        let picked = ui
            .add(
                egui::Button::image_and_text(
                    sized(glyph, GLYPH, ink),
                    egui::RichText::new(self.theme.label())
                        .text_style(ui_text())
                        .color(ink),
                )
                .image_tint_follows_text_color(false)
                .corner_radius(2.0)
                .min_size(egui::vec2(0.0, BUTTON)),
            )
            .on_hover_text(self.theme.hint())
            .clicked();
        if picked {
            self.pick_theme(ui.ctx(), frame, self.theme.next());
        }
    }

    /// Hold the theme, and write it where the next session reads it.
    fn pick_theme(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame, theme: ThemeChoice) {
        self.theme = theme;
        ctx.set_theme(theme.preference());
        // Written now; eframe's own persistence otherwise waits for another frame.
        if let Some(storage) = frame.storage_mut() {
            storage.set_string(ThemeChoice::KEY, theme.stored().to_string());
        }
    }

    /// Every key a menu item binds, answered whether or not a menu is open.
    fn shortcuts(&mut self, ui: &mut egui::Ui, acts: &mut Vec<Act>) {
        let hit = |shortcut: &egui::KeyboardShortcut| {
            ui.input_mut(|input| input.consume_shortcut(shortcut))
        };
        if hit(&key::OPEN) {
            acts.push(Act::OpenFiles);
        }
        if hit(&key::EXPORT) {
            if let Some(id) = self.tabs.active() {
                acts.push(Act::Export(id));
            }
        }
        if hit(&key::BROWSER) {
            acts.push(Act::ToggleDock(Dock::Browser));
        }
        if hit(&key::INSPECTOR) {
            acts.push(Act::ToggleDock(Dock::Inspector));
        }
        if hit(&key::DOCK) {
            acts.push(Act::ToggleDock(Dock::Bottom));
        }
        // ⚠️ Consumed whether or not one is attached, and before ⌘S below. egui matches a
        // shortcut's modifiers logically, so an unconsumed ⌘⇧S goes on to match ⌘S — and
        // saving is what asking for the queue would have done instead.
        if hit(&key::QUEUE) && self.attached() {
            acts.push(Act::ShowPage(Page::Queue));
        }
        // ⚠️ Likewise: an unconsumed ⌘R reloads the browser tab this build runs in.
        if hit(&key::RESYNC) && self.attached() {
            acts.push(Act::Resync);
        }
        if WINDOWED && hit(&key::CLOSE) {
            acts.push(Act::CloseTab);
        }
        if WINDOWED && hit(&key::QUIT) {
            acts.push(Act::Quit);
        }
        if WINDOWED && self.attached() && hit(&key::KEYBOARD) {
            acts.push(Act::ShowTab(Spot::Keyboard));
        }
        if WINDOWED && hit(&key::DOCUMENT) {
            if let Some(id) = self.tabs.last_document() {
                acts.push(Act::ShowTab(Spot::Document(id)));
            }
        }
        // ⚠️ Never a file export. ⌘S means "save what I did", which for a view of a slot
        // is the write back to that slot. The library and the keyboard are views of
        // what is already there, so neither has anything to save.
        if hit(&key::SAVE) {
            if let Some(id) = self.tabs.active() {
                acts.push(Act::SaveDoc(id));
            }
        }
    }

    fn menus(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, acts: &mut Vec<Act>) {
        drop_down(ui, "File", |ui| self.file_menu(ui, acts));
        drop_down(ui, "View", |ui| self.view_menu(ui, frame, acts));
        drop_down(ui, "Instrument", |ui| self.instrument_menu(ui, acts));
        drop_down(ui, "Help", |ui| {
            if item(ui, "User guide", None) {
                ui.ctx().open_url(egui::OpenUrl::new_tab(GUIDE));
            }
            if item(ui, "What's new", None) {
                self.whats_new(ui.ctx());
            }
            ui.separator();
            if item(ui, "Copy activity log", None) {
                acts.push(Act::CopyLog);
            }
            if item(ui, "About drawbar", None) {
                self.about_open = true;
            }
        });
    }

    fn file_menu(&mut self, ui: &mut egui::Ui, acts: &mut Vec<Act>) {
        if item(ui, "Open…", Some(key::OPEN)) {
            acts.push(Act::OpenFiles);
        }
        ui.menu_button("New", |ui| new_menu(ui, acts));
        ui.separator();
        if let Some(id) = self.tabs.active() {
            if item(ui, "Save", Some(key::SAVE)) {
                acts.push(Act::SaveDoc(id));
            }
            let unsaved = self
                .workspace
                .get(id)
                .is_some_and(crate::workspace::LocalEntity::is_unsaved);
            if unsaved && item(ui, "Revert to saved", None) {
                acts.push(Act::Revert(id));
            }
            if item(ui, "Export…", Some(key::EXPORT)) {
                acts.push(Act::Export(id));
            }
            ui.separator();
        }
        let closable = self
            .tabs
            .showing()
            .is_some_and(|spot| spot != Spot::Library);
        if closable && item(ui, "Close tab", Some(key::CLOSE)) {
            acts.push(Act::CloseTab);
        }
        if WINDOWED && item(ui, "Quit", Some(key::QUIT)) {
            acts.push(Act::Quit);
        }
    }

    /// ⚠️ No Library item. The library is always open and always the first tab, so the
    /// menu would offer a view that is one click away and can never be missing.
    fn view_menu(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame, acts: &mut Vec<Act>) {
        let showing = self.tabs.showing();
        if self.attached()
            && marked(
                ui,
                "Keyboard",
                showing == Some(Spot::Keyboard),
                Some(key::KEYBOARD),
            )
        {
            acts.push(Act::ShowTab(Spot::Keyboard));
        }
        if let Some(id) = self.tabs.last_document() {
            if marked(
                ui,
                "Document",
                showing == Some(Spot::Document(id)),
                Some(key::DOCUMENT),
            ) {
                acts.push(Act::ShowTab(Spot::Document(id)));
            }
        }
        ui.separator();
        for (label, dock, shortcut) in [
            ("Browser panel", Dock::Browser, key::BROWSER),
            ("Inspector panel", Dock::Inspector, key::INSPECTOR),
            ("Bottom dock", Dock::Bottom, key::DOCK),
        ] {
            if marked(ui, label, self.shell.open(dock), Some(shortcut)) {
                acts.push(Act::ToggleDock(dock));
            }
        }
        ui.separator();
        ui.menu_button("Theme", |ui| {
            for choice in [ThemeChoice::System, ThemeChoice::Light, ThemeChoice::Dark] {
                if marked(ui, choice.label(), self.theme == choice, None) {
                    self.pick_theme(ui.ctx(), frame, choice);
                }
            }
        });
    }

    /// ⚠️ Nothing but Connect… until one answers. Every other item here acts on an
    /// instrument, and the send queue is only ever owed to one.
    fn instrument_menu(&mut self, ui: &mut egui::Ui, acts: &mut Vec<Act>) {
        if !self.attached() {
            if item(ui, "Connect…", None) {
                acts.push(Act::Connect);
            }
            return;
        }
        if item(ui, "Disconnect", None) {
            acts.push(Act::Disconnect);
        }
        ui.separator();
        if item(ui, "Read everything", Some(key::RESYNC)) {
            acts.push(Act::Resync);
        }
        if let Some(class) = self.open_class() {
            if item(ui, &format!("Read {} again", folder(class)), None) {
                acts.push(Act::ReadAgain(class));
            }
        }
        ui.separator();
        if item(ui, "Review send queue…", Some(key::QUEUE)) {
            acts.push(Act::ShowPage(Page::Queue));
        }
        let waiting = self.queue.len();
        if waiting > 0 && item(ui, &format!("Send all ({waiting})"), None) {
            acts.push(Act::AskSendAll);
        }
        if waiting > 0 && item(ui, "Clear send queue", None) {
            acts.push(Act::ClearQueue);
        }
        let queued: Vec<u64> = self
            .browser
            .picked()
            .locals()
            .into_iter()
            .filter(|id| self.queue.holds(*id))
            .collect();
        if !queued.is_empty() && item(ui, "Remove from queue", None) {
            acts.extend(queued.into_iter().map(Act::Unqueue));
        }
    }

    /// The class the open document belongs to, for the menu that offers to read it
    /// again. Nothing on this computer came off a slot means nothing to name.
    fn open_class(&self) -> Option<ObjectClass> {
        let id = self.tabs.active()?;
        let (class, _) = self.workspace.get(id)?.origin.slot()?;
        Some(class)
    }

    /// 32 px: three groups of actions, the omnibox, and the three dock toggles.
    pub(crate) fn toolbar(&mut self, ctx: &egui::Context, acts: &mut Vec<Act>) {
        let fill = ctx.style().visuals.panel_fill;
        egui::TopBottomPanel::top("toolbar")
            .resizable(false)
            .exact_height(TOOLBAR)
            .frame(bare(fill))
            .show(ctx, |ui| {
                edge(ui, Side::Bottom);
                along(ui, |ui| {
                    if glyph_button(ui, Glyph::FolderOpen, false, "open files…").clicked() {
                        acts.push(Act::OpenFiles);
                    }
                    let ink = ui.visuals().widgets.inactive.fg_stroke.color;
                    new_button(ui, Glyph::FilePlus2, ink, acts);
                    let open = self.tabs.active();
                    if glyph_button(ui, Glyph::Save, false, "save the open document").clicked() {
                        if let Some(id) = open {
                            acts.push(Act::SaveDoc(id));
                        }
                    }
                    rule(ui, 16.0);
                    self.instrument_actions(ui, acts);

                    self.omnibox(ui, acts);
                    ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                        for (glyph, dock, hint) in [
                            (Glyph::PanelRight, Dock::Inspector, "the inspector"),
                            (Glyph::PanelBottom, Dock::Bottom, "the bottom dock"),
                            (Glyph::PanelLeft, Dock::Browser, "the browser"),
                        ] {
                            if glyph_button(ui, glyph, self.shell.open(dock), hint).clicked() {
                                acts.push(Act::ToggleDock(dock));
                            }
                        }
                    });
                });
            });
    }

    /// Read, and what is owed. Both act on an instrument, so neither is drawn without
    /// one, and the rule that would separate them from the omnibox goes with them.
    fn instrument_actions(&mut self, ui: &mut egui::Ui, acts: &mut Vec<Act>) {
        if !self.attached() {
            return;
        }
        if action(ui, Glyph::RefreshCw, "Read", false).clicked() {
            acts.push(Act::Resync);
        }
        let waiting = self.queue.len();
        let label = match waiting {
            0 => "Send".to_string(),
            n => format!("Send {n}"),
        };
        if action(ui, Glyph::Upload, &label, waiting > 0).clicked() && waiting > 0 {
            acts.push(Act::AskSendAll);
        }
        rule(ui, 16.0);
    }

    /// The name search the library's table is narrowed by.
    fn omnibox(&mut self, ui: &mut egui::Ui, acts: &mut Vec<Act>) {
        // Three toggles at 24, their gaps, and the padding they keep from the edge.
        const TOGGLES: f32 = 3.0 * 24.0 + 3.0 * GAP + PAD;
        let width = (ui.available_width() - TOGGLES).max(OMNIBOX);
        let border = ui.visuals().widgets.noninteractive.bg_stroke.color;
        let paper = ui.visuals().extreme_bg_color;
        let before = self.shell.omnibox.clone();
        ui.scope(|ui| {
            ui.visuals_mut().widgets.inactive.bg_stroke = egui::Stroke::new(1.0_f32, border);
            ui.add_sized(
                egui::vec2(width, BUTTON),
                egui::TextEdit::singleline(&mut self.shell.omnibox)
                    .id(egui::Id::new(SEARCH))
                    .background_color(paper)
                    // What is typed and the hint under it share one line down the middle
                    // of a box a third taller than the text in it.
                    .vertical_align(egui::Align::Center)
                    .hint_text(egui::RichText::new("Search…").text_style(ui_text())),
            );
        });
        if searched(&before, &self.shell.omnibox) {
            acts.push(Act::ShowTab(Spot::Library));
        }
    }

    /// 22 px: what just happened, and how much room is left.
    pub(crate) fn status_bar(&mut self, ctx: &egui::Context, acts: &mut Vec<Act>) {
        let fill = ctx.style().visuals.panel_fill;
        egui::TopBottomPanel::bottom("status")
            .resizable(false)
            .exact_height(STATUS)
            .frame(bare(fill))
            .show(ctx, |ui| {
                edge(ui, Side::Top);
                along(ui, |ui| {
                    let said = match &self.device.state.in_flight {
                        Some(words) => {
                            ui.spinner();
                            egui::RichText::new(&words.doing).size(11.0)
                        }
                        None => {
                            let (level, text) = self.log.status();
                            let tint = level.color(ui.visuals());
                            let glyph = match level {
                                Level::Info => Glyph::CircleCheck,
                                Level::Warn | Level::Error => Glyph::CircleAlert,
                            };
                            icon(ui, glyph, GLYPH, tint);
                            egui::RichText::new(text).size(11.0).color(tint)
                        }
                    };
                    if ui
                        .add(egui::Label::new(said).sense(egui::Sense::click()))
                        .on_hover_text("the whole activity log")
                        .clicked()
                    {
                        acts.push(Act::ShowPage(Page::Log));
                    }
                    ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                        let mut first = true;
                        for class in [ObjectClass::Sample, ObjectClass::Program] {
                            let unit = self.device.state.allocation_unit(class);
                            let Some(room) = occupancy(class, &self.device.state.inventory, unit)
                            else {
                                continue;
                            };
                            if !first {
                                rule(ui, 12.0);
                            }
                            first = false;
                            ui.label(
                                egui::RichText::new(format!("{} {room}", folder(class)))
                                    .monospace()
                                    .size(10.0)
                                    .weak(),
                            );
                        }
                    });
                });
            });
    }

    /// The bottom dock: a header that picks a page, and the page under it.
    pub(crate) fn bottom_dock(&mut self, ctx: &egui::Context, acts: &mut Vec<Act>) {
        let open = self.shell.dock_open;
        let fill = ctx.style().visuals.panel_fill;
        let shut = egui::TopBottomPanel::bottom("dock_shut")
            .resizable(false)
            .exact_height(DOCK)
            .frame(bare(fill));
        let most = (ctx.available_rect().height() - CENTRE_TALL).max(DOCK + BODY_LEAST);
        let full = egui::TopBottomPanel::bottom("dock")
            .resizable(true)
            .default_height(DOCK + self.shell.dock_body)
            .height_range((DOCK + BODY_LEAST)..=most)
            .frame(bare(fill));
        egui::TopBottomPanel::show_animated_between(ctx, open, shut, full, |ui, how| {
            claim(ui);
            edge(ui, Side::Top);
            self.dock_header(ui, acts);
            if how < 1.0 {
                return;
            }
            match self.page() {
                Page::Queue => self.queue_page(ui, acts),
                Page::Log => self.log.ui(ui),
            }
        });
        if let Some(rect) = laid_out(ctx, "dock") {
            self.shell.dock_body = rect.height() - DOCK;
        }
    }

    /// The dock's own header. [`panel_header`]'s geometry, with two titles to pick
    /// between rather than one.
    fn dock_header(&mut self, ui: &mut egui::Ui, acts: &mut Vec<Act>) {
        let waiting = self.queue.len();
        let pages: &[Page] = match self.attached() {
            true => &[Page::Queue, Page::Log],
            false => &[Page::Log],
        };
        let mut picked = None;
        let mut clear = false;
        strip(ui, |ui| {
            flat(ui);
            if chevron(ui, self.shell.dock_open).clicked() {
                acts.push(Act::ToggleDock(Dock::Bottom));
            }
            let ink = crate::app::caption(ui.visuals());
            for page in pages.iter().copied() {
                let on = self.shell.dock_open && self.page() == page;
                if ui
                    .selectable_label(on, caps(page.title()).color(ink))
                    .clicked()
                {
                    picked = Some(page_click(page, on));
                }
            }
            // An edit does not queue itself, so what a send would carry stands beside
            // what it would walk past, in one line, next to the button that closes the
            // gap between them. Each part wears the ink and the words of the mark it
            // stands for, which makes the line the legend for every dot in the window.
            let behind = crate::queue::Behind::of(&self.workspace, &self.device.state, &self.queue);
            ui.scope(|ui| {
                ui.spacing_mut().item_spacing.x = 4.0;
                for (index, (said, mark)) in behind.parts().into_iter().enumerate() {
                    if index > 0 {
                        ui.label(crate::queue::aside("·", crate::app::caption(ui.visuals())));
                    }
                    ui.label(crate::queue::aside(
                        &said,
                        crate::library::mark_ink(mark, ui.visuals()),
                    ))
                    .on_hover_text(crate::library::mark_words(mark));
                }
            });
            if ui
                .add_enabled(
                    behind.changed > 0,
                    egui::Button::new(behind.action()).small(),
                )
                .on_disabled_hover_text("nothing here differs from what the instrument holds")
                .clicked()
            {
                acts.push(Act::QueueChanged);
            }
            ui.with_layout(
                egui::Layout::right_to_left(egui::Align::Center),
                |ui| match self.page() {
                    Page::Log => clear = ui.small_button("Clear").clicked(),
                    Page::Queue => {
                        if waiting > 0 {
                            if action(ui, Glyph::Upload, "Send all", true).clicked() {
                                acts.push(Act::AskSendAll);
                            }
                            if ui
                                .small_button("Clear")
                                .on_hover_text("stop waiting to send any of it")
                                .clicked()
                            {
                                acts.push(Act::ClearQueue);
                            }
                        }
                    }
                },
            );
        });
        acts.extend(picked);
        if clear {
            self.log.clear();
        }
    }

    /// Which page the bottom dock is on. Nothing is owed to an instrument that is not
    /// there, so with none attached the log is the only page there is.
    fn page(&self) -> Page {
        match self.attached() {
            true => self.shell.page,
            false => Page::Log,
        }
    }

    /// Everything owed to the instrument, and what each of it runs into.
    fn queue_page(&mut self, ui: &mut egui::Ui, acts: &mut Vec<Act>) {
        crate::queue::page(
            ui,
            &mut self.queue,
            &self.workspace,
            &self.device.state,
            acts,
        );
    }

    /// The browser dock: this computer and the instrument, under one header.
    pub(crate) fn browser_dock(&mut self, ctx: &egui::Context, acts: &mut Vec<Act>) {
        self.shell.filter.keep_kinds(&crate::browser::kinds_present(
            &self.workspace,
            &self.device.state,
        ));
        let open = self.shell.browser_open;
        let fill = ctx.style().visuals.panel_fill;
        let shut = egui::SidePanel::left("browser_shut")
            .resizable(false)
            .exact_width(SHUT)
            .frame(bare(fill));
        let most =
            (ctx.available_rect().width() - self.inspector_room() - CENTRE_WIDE).max(SIDE_LEAST);
        let full = egui::SidePanel::left("browser")
            .resizable(true)
            .default_width(self.shell.browser_width)
            .width_range(SIDE_LEAST..=most)
            .frame(bare(fill));
        egui::SidePanel::show_animated_between(ctx, open, shut, full, |ui, how| {
            claim(ui);
            edge(ui, Side::Right);
            if how < 1.0 {
                if reopen(ui, Glyph::PanelLeftOpen, "show the browser").clicked() {
                    acts.push(Act::ToggleDock(Dock::Browser));
                }
                return;
            }
            dock_header(ui, "browser");
            acts.extend(self.browser.ui(
                ui,
                &self.workspace,
                &self.device,
                &self.queue,
                &self.shell.filter,
            ));
        });
        if let Some(rect) = laid_out(ctx, "browser") {
            self.shell.browser_width = rect.width();
        }
    }

    /// What the inspector will claim once the browser has taken its own: the browser's
    /// most is read before that dock is added, so it has to be asked for.
    fn inspector_room(&self) -> f32 {
        match self.shell.inspector_open {
            true => self.shell.inspector_width,
            false => SHUT,
        }
    }

    /// The inspector dock: what is picked, and — while one is attached — the instrument.
    pub(crate) fn inspector_dock(&mut self, ctx: &egui::Context, acts: &mut Vec<Act>) {
        let open = self.shell.inspector_open;
        let fill = ctx.style().visuals.panel_fill;
        let shut = egui::SidePanel::right("inspector_shut")
            .resizable(false)
            .exact_width(SHUT)
            .frame(bare(fill));
        let most = (ctx.available_rect().width() - CENTRE_WIDE).max(SIDE_LEAST);
        let full = egui::SidePanel::right("inspector")
            .resizable(true)
            .default_width(self.shell.inspector_width)
            .width_range(SIDE_LEAST..=most)
            .frame(bare(fill));
        egui::SidePanel::show_animated_between(ctx, open, shut, full, |ui, how| {
            claim(ui);
            edge(ui, Side::Left);
            if how < 1.0 {
                if reopen(ui, Glyph::PanelRightOpen, "show the inspector").clicked() {
                    acts.push(Act::ToggleDock(Dock::Inspector));
                }
                return;
            }
            acts.extend(crate::inspector::ui(
                ui,
                &mut self.shell,
                &mut self.browser,
                &self.workspace,
                &self.device,
                &self.queue,
            ));
        });
        if let Some(rect) = laid_out(ctx, "inspector") {
            self.shell.inspector_width = rect.width();
        }
    }
}

/// Claim the whole of the panel being drawn.
///
/// ⚠️ egui remembers a resizable panel's size as the size of what was put into it, so a
/// dock holding less than it shows would come back the least it is allowed to be.
fn claim(ui: &mut egui::Ui) {
    ui.set_min_size(ui.max_rect().size());
}

/// Where a dock ended up this frame. A collapsed dock draws under another id, so what
/// this answers is the size it will open back to.
fn laid_out(ctx: &egui::Context, id: &str) -> Option<egui::Rect> {
    egui::containers::panel::PanelState::load(ctx, egui::Id::new(id)).map(|state| state.rect)
}

/// A shut side dock: the width of one glyph, and the glyph that opens it again.
fn reopen(ui: &mut egui::Ui, glyph: Glyph, hint: &str) -> egui::Response {
    let ink = ui.visuals().widgets.inactive.fg_stroke.color;
    let rect = ui.max_rect();
    let box_ = egui::Rect::from_center_size(
        egui::pos2(rect.center().x, rect.top() + DOCK / 2.0),
        egui::Vec2::splat(GLYPH),
    );
    crate::icon::painted(ui, glyph, box_, ink);
    ui.interact(box_, ui.id().with("reopen"), egui::Sense::click())
        .on_hover_text(hint)
}

/// Whether a screen this size leaves the shell too little room to lay out in.
pub fn too_small(screen: egui::Vec2) -> bool {
    screen.x < LEAST.x || screen.y < LEAST.y
}

/// What a screen too small for the shell shows instead of it.
pub fn too_small_notice(ctx: &egui::Context) {
    let fill = ctx.style().visuals.panel_fill;
    egui::CentralPanel::default()
        .frame(
            egui::Frame::new()
                .fill(fill)
                .inner_margin(egui::Margin::symmetric(16, 0)),
        )
        .show(ctx, |ui| {
            ui.vertical_centered(|ui| {
                ui.add_space(ui.available_height() / 3.0);
                ui.label(egui::RichText::new(TOO_SMALL).size(18.0).strong());
                ui.add_space(GAP);
                ui.label(TOO_SMALL_WHY);
                ui.add_space(GAP * 2.0);
                crate::splash::link(ui, "User guide", GUIDE);
            });
        });
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::Fake;
    use eframe::{App, Storage};

    /// The window the design is drawn to, and the smallest this shell claims to hold.
    const SCREEN: egui::Vec2 = egui::vec2(900.0, 540.0);

    /// The regions, in the order they claim space.
    const REGIONS: [&str; 6] = [
        "titlebar",
        "toolbar",
        "status",
        "dock",
        "browser",
        "inspector",
    ];

    fn app(ctx: &egui::Context, storage: Option<&dyn eframe::Storage>) -> DrawbarApp {
        let mut cc = eframe::CreationContext::_new_kittest(ctx.clone());
        cc.storage = storage;
        DrawbarApp::new(&cc)
    }

    /// An instrument answering, which is what the full layout needs to draw.
    fn attach(app: &mut DrawbarApp) {
        app.device
            .pretend_scanned(ObjectClass::Program, 1, &["Africa Split"]);
    }

    /// What one frame at 900 × 540 laid out and what it wrote.
    struct Painted {
        centre: egui::Rect,
        panels: Vec<(String, egui::Rect)>,
        /// Every string the frame painted, headers and button labels included.
        words: Vec<String>,
    }

    impl Painted {
        fn wrote(&self, word: &str) -> bool {
            self.words.iter().any(|said| said == word)
        }

        fn region(&self, want: &str) -> Option<egui::Rect> {
            self.panels
                .iter()
                .find(|(id, _)| id == want)
                .map(|(_, rect)| *rect)
        }
    }

    /// One frame at 900 × 540, answering with the centre's rect, every panel's, and the
    /// text the frame put on screen.
    fn drawn(ctx: &egui::Context, app: &mut DrawbarApp) -> Painted {
        drawn_at(ctx, app, SCREEN)
    }

    /// One frame on a screen of some other size.
    fn drawn_at(ctx: &egui::Context, app: &mut DrawbarApp, screen: egui::Vec2) -> Painted {
        frame_of(ctx, app, screen, Vec::new())
    }

    /// One frame with something arriving in it.
    fn frame_of(
        ctx: &egui::Context,
        app: &mut DrawbarApp,
        screen: egui::Vec2,
        events: Vec<egui::Event>,
    ) -> Painted {
        let mut frame = eframe::Frame::_new_kittest();
        let input = egui::RawInput {
            events,
            screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, screen)),
            ..Default::default()
        };
        let mut centre = egui::Rect::NOTHING;
        let output = ctx.run(input, |ctx| {
            app.update(ctx, &mut frame);
            // Panels shrink this as they are added; the central panel does not.
            centre = ctx.available_rect();
        });
        let panels = REGIONS
            .iter()
            .filter_map(|id| {
                let state = egui::containers::panel::PanelState::load(ctx, egui::Id::new(*id))?;
                Some((id.to_string(), state.rect))
            })
            .collect();
        Painted {
            centre,
            panels,
            words: crate::tabs::words(&output),
        }
    }

    /// The gate is the screen and the metrics alone: what the reader has shut, and what
    /// the device calls itself, do not make room the shell does not have.
    #[test]
    fn a_screen_short_of_the_least_room_is_gated_in_either_dimension_alone() {
        assert!(!too_small(LEAST), "the least the shell lays out in");
        assert!(!too_small(SCREEN), "the window the design is drawn to");
        assert!(
            too_small(LEAST - egui::vec2(1.0, 0.0)),
            "a point too narrow"
        );
        assert!(too_small(LEAST - egui::vec2(0.0, 1.0)), "a point too short");
        // A phone, either way up, and a canvas with no room at all.
        assert!(too_small(egui::vec2(390.0, 844.0)));
        assert!(too_small(egui::vec2(844.0, 390.0)));
        assert!(too_small(egui::Vec2::ZERO));
    }

    /// What [`LEAST`] claims: at exactly that size the three docks and the bars still
    /// fit, and the centre keeps the room no dock may take from it.
    #[test]
    fn at_the_least_room_it_claims_every_region_fits_and_the_centre_keeps_its_own() {
        let ctx = egui::Context::default();
        let mut app = app(&ctx, None);
        app.shell.dock_open = true;
        attach(&mut app);
        let _ = drawn_at(&ctx, &mut app, LEAST);
        let painted = drawn_at(&ctx, &mut app, LEAST);

        let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, LEAST);
        assert_eq!(painted.panels.len(), REGIONS.len(), "every region drew");
        for (id, rect) in &painted.panels {
            assert!(
                screen.contains_rect(*rect),
                "{id} is outside the window: {rect:?}"
            );
        }
        let centre = painted.centre;
        assert!(centre.width() >= CENTRE_WIDE, "the centre: {centre:?}");
        assert!(centre.height() >= CENTRE_TALL, "the centre: {centre:?}");
    }

    /// ⚠️ The page has to answer a phone before the module that would answer has
    /// loaded, so the threshold and the words are written a second time in `index.html`.
    /// A CSS pixel is the point the shell lays itself out in.
    #[test]
    fn the_page_gates_where_the_shell_does_and_says_the_same_thing() {
        let page = include_str!("../index.html");
        let gate = format!("@media (width < {}px), (height < {}px)", LEAST.x, LEAST.y);
        assert!(page.contains(&gate), "index.html does not gate at `{gate}`");
        assert!(page.contains(TOO_SMALL), "index.html: {TOO_SMALL:?}");
        assert!(
            page.contains(TOO_SMALL_WHY),
            "index.html: {TOO_SMALL_WHY:?}"
        );
    }

    #[test]
    fn the_favicon_is_the_titlebar_mark_in_both_accents() {
        let favicon = include_str!("../favicon.svg");
        let mark = include_str!("../assets/icons/sliders-vertical.svg");
        for line in mark.lines().filter(|l| l.trim_start().starts_with("<line")) {
            assert!(
                favicon.contains(line),
                "favicon.svg lacks `{}`",
                line.trim()
            );
        }
        let hex = |visuals: egui::Visuals| {
            let [r, g, b, _] = accent(&visuals).to_array();
            format!("#{r:02x}{g:02x}{b:02x}")
        };
        let light = format!("stroke=\"{}\"", hex(egui::Visuals::light()));
        let dark = format!("stroke: {};", hex(egui::Visuals::dark()));
        assert!(favicon.contains(&light), "favicon.svg lacks `{light}`");
        assert!(favicon.contains(&dark), "favicon.svg lacks `{dark}`");
    }

    /// The notice is the whole of what a gated frame draws: what is wrong, and the
    /// guide to read while the reader finds a bigger screen.
    #[test]
    fn the_notice_says_what_is_wrong_and_offers_the_guide() {
        let ctx = egui::Context::default();
        let input = egui::RawInput {
            screen_rect: Some(egui::Rect::from_min_size(
                egui::Pos2::ZERO,
                egui::vec2(390.0, 844.0),
            )),
            ..Default::default()
        };
        let output = ctx.run(input, too_small_notice);
        let said = crate::tabs::words(&output);

        assert!(said.iter().any(|word| word == TOO_SMALL), "{said:?}");
        assert!(said.iter().any(|word| word == TOO_SMALL_WHY), "{said:?}");
        assert!(said.iter().any(|word| word == "User guide"), "{said:?}");
    }

    /// Every fixed region fits inside the window the design is drawn to, and the centre
    /// still has room left after all of them have taken theirs.
    #[test]
    fn at_900_by_540_every_region_fits_and_the_centre_survives() {
        let ctx = egui::Context::default();
        let mut app = app(&ctx, None);
        app.shell.dock_open = true;
        attach(&mut app);
        // Twice: the first frame is what the second lays itself out against.
        let _ = drawn(&ctx, &mut app);
        let painted = drawn(&ctx, &mut app);
        let (centre, panels) = (painted.centre, &painted.panels);

        let screen = egui::Rect::from_min_size(egui::Pos2::ZERO, SCREEN);
        assert_eq!(panels.len(), REGIONS.len(), "every region drew: {panels:?}");
        for (id, rect) in panels {
            assert!(
                screen.contains_rect(*rect),
                "{id} is outside the window: {rect:?}"
            );
        }
        let at = |want: &str| painted.region(want).unwrap();
        assert_eq!(at("titlebar").height(), TITLEBAR);
        assert_eq!(at("toolbar").height(), TOOLBAR);
        assert_eq!(at("status").height(), STATUS);
        assert_eq!(at("dock").height(), DOCK + DOCK_BODY);
        assert_eq!(at("browser").width(), BROWSER);
        assert_eq!(at("inspector").width(), INSPECTOR);
        // The dock's header is its top [`DOCK`], inside the window with the rest.
        let header = at("dock").split_top_bottom_at_y(at("dock").top() + DOCK).0;
        assert!(screen.contains_rect(header), "the dock header: {header:?}");

        assert!(centre.width() > 0.0, "the centre: {centre:?}");
        assert!(centre.height() > 0.0, "the centre: {centre:?}");
    }

    /// Flipping the theme moves colours and nothing else: the metrics live on the style
    /// both faces share, so every region lands in the same place.
    #[test]
    fn flipping_the_theme_leaves_every_region_where_it_was() {
        let ctx = egui::Context::default();
        let mut app = app(&ctx, None);
        app.shell.dock_open = true;
        attach(&mut app);

        ctx.set_theme(egui::ThemePreference::Dark);
        let _ = drawn(&ctx, &mut app);
        let dark = drawn(&ctx, &mut app);

        ctx.set_theme(egui::ThemePreference::Light);
        let _ = drawn(&ctx, &mut app);
        let light = drawn(&ctx, &mut app);

        assert_eq!(dark.centre, light.centre);
        assert_eq!(dark.panels, light.panels);
    }

    /// ⚠️ With nothing attached there is nothing to read from, nothing to send to and no
    /// room to report. Every control that acts on an instrument is absent rather than
    /// dead — the inspector's INSTRUMENT group with them — while SELECTION, which is
    /// about what is picked here, stays whatever is on the bus.
    #[test]
    fn no_instrument_means_no_instrument_controls() {
        const ONLY_WITH_ONE: [&str; 6] =
            ["Read", "Send", "SEND QUEUE", "INSTRUMENT", "ROOM", "INFO"];

        let ctx = egui::Context::default();
        let mut app = app(&ctx, None);
        app.shell.dock_open = true;
        let _ = drawn(&ctx, &mut app);
        let alone = drawn(&ctx, &mut app);

        assert!(!app.attached(), "nothing was attached");
        for control in ONLY_WITH_ONE {
            assert!(
                !alone.wrote(control),
                "{control} is painted with none attached"
            );
        }
        assert!(alone.wrote("ACTIVITY LOG"), "the log is the one page left");
        assert!(alone.wrote("SELECTION"), "the inspector still answers");
        assert!(alone.region("inspector").is_some(), "{:?}", alone.panels);

        attach(&mut app);
        let _ = drawn(&ctx, &mut app);
        let answering = drawn(&ctx, &mut app);
        for control in ONLY_WITH_ONE {
            assert!(
                answering.wrote(control),
                "{control} is missing with one attached"
            );
        }
        assert!(answering.wrote("SELECTION"));
        assert_eq!(
            answering.region("inspector"),
            alone.region("inspector"),
            "the dock is the same width either way"
        );
    }

    /// Every change to what is typed brings the library forward, the first keystroke
    /// into an empty box and clearing it included. A frame that typed nothing is not a
    /// change.
    #[test]
    fn a_change_in_the_omnibox_is_what_brings_the_library_forward() {
        assert!(searched("", "a"), "the first keystroke");
        assert!(searched("afr", "afri"));
        assert!(searched("afri", "afr"), "and a backspace");
        assert!(searched("afr", ""), "and clearing it");
        assert!(!searched("afr", "afr"));
        assert!(!searched("", ""));
    }

    /// ⚠️ The omnibox narrows the library's table and nothing else. Typing into it with a
    /// document in front would otherwise search where nobody can see the result.
    #[test]
    fn typing_a_search_with_a_document_in_front_brings_the_library_forward() {
        let ctx = egui::Context::default();
        let mut app = app(&ctx, None);
        let id = app
            .workspace
            .create(crate::workspace::Fresh::Program, &mut app.log)
            .unwrap();
        app.tabs.open(id);
        let _ = drawn(&ctx, &mut app);
        assert_eq!(app.tabs.showing(), Some(Spot::Document(id)));

        ctx.memory_mut(|memory| memory.request_focus(egui::Id::new(SEARCH)));
        let _ = frame_of(
            &ctx,
            &mut app,
            SCREEN,
            vec![egui::Event::Text("afr".into())],
        );
        assert_eq!(app.shell.omnibox, "afr");
        assert_eq!(app.tabs.showing(), Some(Spot::Library));

        // And the frame after it, which typed nothing, leaves the tab where the user put
        // it.
        app.tabs.show(Spot::Document(id));
        let _ = drawn(&ctx, &mut app);
        assert_eq!(app.tabs.showing(), Some(Spot::Document(id)));
    }

    /// ⚠️ egui matches a shortcut's modifiers logically, so an extra Shift is ignored and
    /// a ⌘⇧S nothing consumed goes on to match ⌘S. The gesture that asks to review the
    /// send queue would then mark the open document saved instead, taking the revert it
    /// still had with it.
    #[test]
    fn the_send_queue_shortcut_never_falls_through_to_save() {
        let ctx = egui::Context::default();
        let mut app = app(&ctx, None);
        let id = app
            .workspace
            .create(crate::workspace::Fresh::Program, &mut app.log)
            .unwrap();
        let bytes = app.workspace.get(id).unwrap().bytes.clone();
        let (_, edited) =
            crate::fields::apply(&bytes, &[("center_panel.gain".into(), "96".into())]).unwrap();
        app.workspace.replace_bytes(id, edited, &mut app.log);
        app.tabs.open(id);
        assert!(app.workspace.get(id).unwrap().is_unsaved());

        let pressed = || egui::Event::Key {
            key: egui::Key::S,
            physical_key: None,
            pressed: true,
            repeat: false,
            modifiers: egui::Modifiers::COMMAND.plus(egui::Modifiers::SHIFT),
        };
        let _ = drawn(&ctx, &mut app);
        let _ = frame_of(&ctx, &mut app, SCREEN, vec![pressed()]);
        assert!(!app.attached(), "nothing was attached");
        assert!(
            app.workspace.get(id).unwrap().is_unsaved(),
            "there is no queue to review, and there is no save either"
        );

        attach(&mut app);
        let _ = drawn(&ctx, &mut app);
        let _ = frame_of(&ctx, &mut app, SCREEN, vec![pressed()]);
        assert!(app.shell.dock_open && app.shell.page == Page::Queue);
        assert!(
            app.workspace.get(id).unwrap().is_unsaved(),
            "reviewing the queue is not saving"
        );
    }

    /// ⚠️ ⌘R is the browser tab's own reload. A frame that leaves it unconsumed reloads
    /// the page out from under whatever is open, so the gesture is taken whether or not
    /// there is an instrument to read again.
    #[test]
    fn the_read_everything_shortcut_is_taken_with_nothing_attached() {
        let ctx = egui::Context::default();
        let mut app = app(&ctx, None);
        let pressed = || egui::Event::Key {
            key: egui::Key::R,
            physical_key: None,
            pressed: true,
            repeat: false,
            modifiers: egui::Modifiers::COMMAND,
        };
        let reload = |event: &egui::Event| match event {
            egui::Event::Key { key, .. } => *key == egui::Key::R,
            _ => false,
        };
        let left = |ctx: &egui::Context| ctx.input(|input| input.events.iter().any(reload));

        let _ = drawn(&ctx, &mut app);
        let _ = frame_of(&ctx, &mut app, SCREEN, vec![pressed()]);
        assert!(!app.attached(), "nothing was attached");
        assert!(!left(&ctx), "⌘R reached the tab with nothing attached");

        attach(&mut app);
        let _ = drawn(&ctx, &mut app);
        let _ = frame_of(&ctx, &mut app, SCREEN, vec![pressed()]);
        assert!(!left(&ctx), "⌘R reached the tab with one attached");
    }

    /// What was collapsed comes back collapsed in the next session's window.
    #[test]
    fn the_docks_come_back_where_the_last_session_left_them() {
        let mut store = Fake::default();
        {
            let ctx = egui::Context::default();
            let mut before = app(&ctx, None);
            before.shell.browser_open = false;
            before.shell.show_page(Page::Log);
            before.save(&mut store);
        }
        let ctx = egui::Context::default();
        let after = app(&ctx, Some(&store));
        assert!(!after.shell.browser_open);
        assert!(after.shell.inspector_open);
        assert!(after.shell.dock_open);
        assert_eq!(after.shell.page, Page::Log);
    }

    #[test]
    fn a_dock_toggles_and_a_page_opens_the_dock_it_is_on() {
        let mut shell = Shell::default();
        for dock in [Dock::Browser, Dock::Inspector, Dock::Bottom] {
            let was = shell.open(dock);
            shell.toggle(dock);
            assert_eq!(shell.open(dock), !was, "{dock:?}");
        }
        // Asking for a page is asking to read it, which means opening the dock too.
        shell.dock_open = false;
        shell.show_page(Page::Log);
        assert!(shell.dock_open && shell.page == Page::Log);
    }

    /// ⚠️ The title of the page already showing is the way back out of the dock. A title
    /// that answered a click by doing nothing reads as broken, and the collapse triangle
    /// is 8 px of the header.
    #[test]
    fn the_title_of_the_page_showing_shuts_the_dock() {
        assert!(matches!(
            page_click(Page::Queue, true),
            Act::ToggleDock(Dock::Bottom)
        ));
        assert!(matches!(
            page_click(Page::Queue, false),
            Act::ShowPage(Page::Queue)
        ));
    }

    /// What was collapsed is collapsed again next session, on the page it was left on.
    #[test]
    fn the_layout_comes_back_as_it_was_left() {
        let mut store = Fake::default();
        let before = Shell {
            browser_open: false,
            inspector_open: true,
            dock_open: true,
            room_open: false,
            info_open: true,
            browser_width: 301.0,
            inspector_width: 199.0,
            dock_body: 260.0,
            page: Page::Log,
            omnibox: "typed and not kept".into(),
            filter: Filter::default(),
        };
        before.keep(&mut store);

        let mut after = Shell::default();
        after.restore(&store);
        assert!(!after.browser_open);
        assert!(after.inspector_open);
        assert!(after.dock_open);
        assert!(!after.room_open, "a shut inspector panel comes back shut");
        assert!(after.info_open, "and an open one comes back open");
        assert_eq!(after.browser_width, 301.0);
        assert_eq!(after.inspector_width, 199.0);
        assert_eq!(after.dock_body, 260.0);
        assert_eq!(after.page, Page::Log);
        assert!(after.omnibox.is_empty(), "a search is not a layout");
    }

    /// A stored size this build would never have laid out is not a size, so the dock
    /// opens to the width the design gives it rather than to a sliver or to nonsense.
    #[test]
    fn a_size_outside_what_a_dock_opens_to_comes_back_as_the_default() {
        let mut store = Fake::default();
        store.set_string(
            Shell::KEY,
            format!(
                "{}\nbrowser_width\t12\ninspector_width\twide\ndock_body\tNaN\n",
                Shell::VERSION
            ),
        );
        let mut shell = Shell::default();
        shell.restore(&store);
        assert_eq!(shell.browser_width, BROWSER, "under the least it opens to");
        assert_eq!(shell.inspector_width, INSPECTOR, "not a number");
        assert_eq!(shell.dock_body, DOCK_BODY, "not a size");
    }

    /// However far a dock was dragged last session, the centre keeps its own room: the
    /// most a dock may claim is read off the window every frame.
    #[test]
    fn docks_wider_than_the_window_still_leave_the_centre_its_room() {
        let ctx = egui::Context::default();
        let mut app = app(&ctx, None);
        app.shell.dock_open = true;
        app.shell.browser_width = 5_000.0;
        app.shell.inspector_width = 5_000.0;
        app.shell.dock_body = 5_000.0;
        attach(&mut app);
        let _ = drawn(&ctx, &mut app);
        let painted = drawn(&ctx, &mut app);

        assert!(
            painted.centre.width() >= CENTRE_WIDE,
            "the centre: {:?}",
            painted.centre
        );
        assert!(
            painted.centre.height() >= CENTRE_TALL,
            "the centre: {:?}",
            painted.centre
        );
        assert!(app.shell.browser_width >= SIDE_LEAST);
        assert!(app.shell.dock_body >= BODY_LEAST);
    }

    /// A dock keeps the size it was left at across a frame, so what is written out is
    /// what the window actually showed rather than the design's default.
    #[test]
    fn a_dock_drawn_narrower_writes_the_size_it_was_drawn_at() {
        let ctx = egui::Context::default();
        let mut app = app(&ctx, None);
        app.shell.dock_open = true;
        app.shell.browser_width = 190.0;
        app.shell.dock_body = 130.0;
        attach(&mut app);
        let _ = drawn(&ctx, &mut app);
        let painted = drawn(&ctx, &mut app);

        assert_eq!(painted.region("browser").unwrap().width(), 190.0);
        assert_eq!(painted.region("dock").unwrap().height(), DOCK + 130.0);
        assert_eq!(app.shell.browser_width, 190.0);
        assert_eq!(app.shell.dock_body, 130.0);
    }

    /// A version nobody wrote is a layout nobody can explain, so the defaults stand.
    #[test]
    fn an_unknown_version_leaves_the_default_layout() {
        let mut store = Fake::default();
        store.set_string(
            Shell::KEY,
            "drawbar docks 99\nbrowser\t0\ndock\t1\n".to_string(),
        );
        let mut shell = Shell::default();
        shell.restore(&store);
        assert!(shell.browser_open, "the default stands");
        assert!(!shell.dock_open);
    }

    /// A store holding nothing about the docks is not an error, and neither is one
    /// holding lines this build has no field for.
    #[test]
    fn an_empty_or_partial_store_is_a_store() {
        let mut shell = Shell::default();
        shell.restore(&Fake::default());
        assert!(shell.browser_open && shell.inspector_open && !shell.dock_open);

        let mut store = Fake::default();
        store.set_string(
            Shell::KEY,
            format!("{}\ninspector\t0\nrail\t1\n", Shell::VERSION),
        );
        shell.restore(&store);
        assert!(!shell.inspector_open);
        assert!(shell.browser_open, "a field nobody wrote keeps its default");
    }
}