monospace 0.1.0

The interactive full-screen terminal application: the chrome around a diagram being edited
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
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
//! The drawing the last frame produced, and the session it was drawn from.
//!
//! **The session is read on every frame and changed only by an action.** Every frame draws it into
//! the window the interior gives; the region's contextual actions change it, all carried out in
//! `crate::callbacks`: **Delete** performs one removal, **Move** and **Resize** displace and
//! resize the selected figure, **`SetStroke`** gives it another stroke, and the Edit menu's **Undo**
//! and **Redo** walk the history those commands recorded. The application keeps no history of its
//! own: the session is the history.
//!
//! **The selection follows the diagram.** A selection names a figure the session holds, or nothing
//! is selected: [`DiagramState::perform`] reconciles the two after every command, so a command that
//! takes the selected figure away drops the identity without having to name the selection.
//!
//! **The view survives a frame and the offset does not** (D1 of #176): each axis's scroll offset is
//! derived from the origin and the content, so a content change that moves the drawing's first cell
//! does not carry the view with it.

use monospace_core::{
    Buffer, CellReader, Direction, Footprint, FootprintSink, GlyphCatalog, Offset, Orientation,
    OwnerReader, Pos, ShapeId, Size, Stroke, SymbolStyleName, Terminal, Window,
};
use monospace_diagram::{Endpoint, Shape};
use monospace_editing::{Command, Session};
use rat_widget::event::ScrollOutcome;
use rat_widget::scrolled::{ScrollAreaState, ScrollState};
use ratatui_core::layout::{Position, Rect};

use crate::chrome::Target;
use crate::vocabulary::Reply;

/// The drawing the last frame produced, and the session it was drawn from.
pub(crate) struct DiagramState {
    /// The scroll origin: the position in the plane the interior's top-left corner reaches.
    ///
    /// **What survives a frame** (D1): each axis's scroll offset is derived from it and the content
    /// rather than kept beside it, so a content change that moves the drawing's first cell does not
    /// carry the view with it — a person's place is a place in the plane. It starts at `(0, 0)`,
    /// which is the first screen, and every frame puts it at the nearest value the content allows.
    origin: Pos,

    /// The window the last frame drew into, kept so that a click is answered from the picture
    /// the person saw rather than from a drawing made when the click arrives (D4). Empty until
    /// the first frame is drawn.
    buffer: Buffer,

    /// The footprint the last frame's drawing left, measured on its way into `buffer`.
    ///
    /// **Measured while the stamps pass rather than asked of the cells afterwards**: a stamp the
    /// window discarded and a stamp the composition found already decided both count, and the
    /// buffer cannot answer for either. `None` until the first frame is drawn.
    footprint: Option<Footprint>,

    /// The vertical bar's own state: where its thumb stands, and how far the content lets it travel.
    ///
    /// **Kept beside the origin and derived from it** (D1): `redraw` writes the page, the reach and
    /// the offset of every frame, so a content change moves the thumb with the drawing rather than
    /// leaving it where the frame before it stood. The state is the helper's because the bar that
    /// draws it and the press it answers are the helper's too.
    vscroll: ScrollState,

    /// The horizontal bar's own state, derived from the origin as [`Self::vscroll`] is.
    hscroll: ScrollState,

    /// The interior the last frame drew the diagram into, as the screen holds it. A click arrives
    /// as a column and a row of the screen, and this is what says whether it landed inside the
    /// window and what its offset from the window's corner is. Empty until the first frame is
    /// drawn, and empty for a screen too short to hold a region.
    interior: Rect,

    /// The region's own rectangle, border included, as the last frame drew it. **A press on the
    /// border is the region's without being the window's** (rule 2 of #189), so the outer
    /// rectangle is kept beside the interior rather than derived from it.
    region: Rect,

    /// The shape a click selected, or `None` while no cell of the last frame is inverted.
    ///
    /// **An identity rather than the cells it inverted** (D2): what is inverted is recomputed from
    /// the drawing of each frame, so a resize gives the window another size and the selection
    /// follows the shape rather than a rectangle left where it was. A walk over the history clears
    /// the selection (#297).
    ///
    /// **An identity names a figure the diagram holds, or nothing is selected**: `perform`
    /// reconciles the selection with the diagram after every command, so one that takes the figure
    /// away drops it without naming the selection.
    selected: Option<ShapeId>,

    /// The active sub-mode, or `None` if in normal mode.
    sub_mode: Option<SubMode>,

    /// **The session is read on every frame and changed by the region's actions and the menu's two
    /// walks**: Delete performs its one command, Move, Resize and `SetStroke` change the selected
    /// figure, Undo and Redo give a step back, and every other slice reads it.
    session: Session,
}

pub(crate) use monospace_diagram::End;

/// An inline sub-mode of the diagram region.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum SubMode {
    /// Choosing a stroke for the selected figure.
    StrokeSelect,
    /// Moving the selected figure through the layer order.
    OrderMode,
    /// Inspecting and editing a single connector endpoint.
    Endpoint(End),
    /// Choosing a terminal style for the active connector endpoint.
    TerminalSelect(End),
    /// Choosing a leaving direction for the active detached connector endpoint.
    LeavingSelect(End),
    /// Choosing a target shape and an attachment point for the active detached endpoint.
    AttachSelect { end: End, target: ShapeId },
}

impl SubMode {
    /// The parent sub-mode to return to when leaving this sub-mode, or `None` if leaving returns to
    /// normal selection.
    #[must_use]
    pub(crate) fn parent(self) -> Option<Self> {
        match self {
            Self::TerminalSelect(end)
            | Self::LeavingSelect(end)
            | Self::AttachSelect { end, .. } => Some(Self::Endpoint(end)),
            Self::StrokeSelect | Self::OrderMode | Self::Endpoint(_) => None,
        }
    }
}

impl DiagramState {
    /// A state over `session` that has not drawn a frame yet.
    #[must_use]
    pub(crate) fn new(session: Session) -> Self {
        let origin = Pos { x: 0, y: 0 };

        Self {
            origin,
            buffer: Buffer::new(Window {
                origin,
                size: Size {
                    width: 0,
                    height: 0,
                },
            }),
            footprint: None,
            vscroll: ScrollState::new(),
            hscroll: ScrollState::new(),
            interior: Rect::new(0, 0, 0, 0),
            region: Rect::new(0, 0, 0, 0),
            selected: None,
            sub_mode: None,
            session,
        }
    }

    /// The session this diagram state holds.
    pub(crate) fn session(&self) -> &Session {
        &self.session
    }

    /// The glyph the last drawing put at `at`, or `None` where it put none: a position outside the
    /// window, a cell no shape wrote, or a cell this catalog does not answer.
    ///
    /// **The click's offset is the window's to translate**: `CellReader::cell` takes a position,
    /// and the window turns the column and the row the terminal reports into one.
    pub(crate) fn glyph<'a>(&'a self, at: Offset, catalog: &'a GlyphCatalog) -> Option<&'a str> {
        self.buffer
            .window()
            .position(at)
            .and_then(|position| self.buffer.cell(position))
            .and_then(|cell| cell.glyph_str(catalog))
    }

    /// The footprint the last frame's drawing reaches, or `None` before the first [`redraw`].
    ///
    /// **The drawing's and not the window's**: the sink measures every stamp before the buffer
    /// consults its window, so a diagram larger than the interior reaches past it and one drawn at
    /// a scroll origin away from `(0, 0)` is measured where it stands. `min` is the top-left corner
    /// in the plane, which is what bounds the origin; the click paths above stay in offsets and are
    /// unaffected.
    ///
    /// **The last frame's, measured when that frame was drawn**: it is re-measured by [`redraw`],
    /// and it changes when the session does rather than when the screen does. Its reader is the
    /// status bar, which is why it is read once the region is drawn and not while the state is
    /// built.
    ///
    /// [`redraw`]: Self::redraw
    pub(crate) fn footprint(&self) -> Option<Footprint> {
        self.footprint
    }

    /// The scroll origin the last frame drew at.
    ///
    /// **The status line reads it on every frame** to report the viewport position in the idle
    /// context, and the tests verify where scrolling moves it.
    pub(crate) fn origin(&self) -> Pos {
        self.origin
    }

    /// The shape a click selected, or `None` while no shape is selected.
    ///
    /// **The identity and not the cells it covers**: a resize, a scroll or a command that moves
    /// the shape changes what is inverted without changing this (D2).
    pub(crate) fn selected(&self) -> Option<ShapeId> {
        self.selected
    }

    /// The selected identity and the shape it names, or `None` if nothing is selected or the
    /// selected id is not in the diagram.
    ///
    /// **One answer rather than two reads**: a caller that needs the identity and the shape asks
    /// once, so the two cannot name different figures.
    pub(crate) fn selected_entry(&self) -> Option<(ShapeId, &Shape)> {
        let id = self.selected?;
        self.session.diagram().get(id).map(|shape| (id, shape))
    }

    /// The shape currently selected, or `None` if nothing is selected or the selected id is not in
    /// the diagram.
    pub(crate) fn selected_shape(&self) -> Option<&Shape> {
        self.selected_entry().map(|(_, shape)| shape)
    }

    /// The active sub-mode, or `None` if in normal mode.
    pub(crate) fn sub_mode(&self) -> Option<SubMode> {
        self.sub_mode
    }

    /// Sets the active sub-mode.
    pub(crate) fn set_sub_mode(&mut self, sub_mode: Option<SubMode>) {
        self.sub_mode = sub_mode;
    }

    /// Whether `shape` can serve as an attachment target for a connector endpoint.
    ///
    /// Connectors attach to boxes and lines.
    #[must_use]
    pub(crate) fn is_attachable(shape: &Shape) -> bool {
        matches!(shape, Shape::Box { .. } | Shape::Line { .. })
    }

    /// The identities of all attachable figures in the diagram, in drawing order.
    #[must_use]
    pub(crate) fn attach_candidates(&self) -> Vec<ShapeId> {
        self.session
            .diagram()
            .shapes()
            .filter(|(_, shape)| Self::is_attachable(shape))
            .map(|(id, _)| id)
            .collect()
    }

    /// Enters the attach target selector for the active detached connector endpoint, focusing the
    /// first attachable candidate in drawing order. Returns `true` if the sub-mode was entered.
    pub(crate) fn enter_attach_select(&mut self) -> bool {
        let Some(SubMode::Endpoint(end)) = self.sub_mode else {
            return false;
        };
        let Some(&first) = self.attach_candidates().first() else {
            return false;
        };
        self.sub_mode = Some(SubMode::AttachSelect { end, target: first });
        true
    }

    /// Focuses the next attachable candidate in drawing order, wrapping to the first. Returns
    /// `true` if the focused target changed.
    pub(crate) fn focus_next_attach_target(&mut self) -> bool {
        let Some(SubMode::AttachSelect {
            target: current, ..
        }) = self.sub_mode
        else {
            return false;
        };
        let candidates = self.attach_candidates();
        if candidates.is_empty() {
            return false;
        }
        let next = match candidates.iter().position(|&id| id == current) {
            Some(index) => candidates[(index + 1) % candidates.len()],
            None => candidates[0],
        };
        self.focus_attach_target(next)
    }

    /// Focuses the previous attachable candidate in drawing order, wrapping to the last. Returns
    /// `true` if the focused target changed.
    pub(crate) fn focus_previous_attach_target(&mut self) -> bool {
        let Some(SubMode::AttachSelect {
            target: current, ..
        }) = self.sub_mode
        else {
            return false;
        };
        let candidates = self.attach_candidates();
        if candidates.is_empty() {
            return false;
        }
        let prev = match candidates.iter().position(|&id| id == current) {
            Some(index) => candidates[(index + candidates.len() - 1) % candidates.len()],
            None => *candidates.last().unwrap_or(&candidates[0]),
        };
        self.focus_attach_target(prev)
    }

    /// Sets the focused attach target to `target` if `target` is an attachable shape and different
    /// from the currently focused target. Returns `true` if the focused target changed.
    pub(crate) fn focus_attach_target(&mut self, target: ShapeId) -> bool {
        let Some(SubMode::AttachSelect {
            end,
            target: current,
        }) = self.sub_mode
        else {
            return false;
        };
        if current == target {
            return false;
        }
        let is_attachable = self
            .session
            .diagram()
            .get(target)
            .is_some_and(Self::is_attachable);
        if !is_attachable {
            return false;
        }
        self.sub_mode = Some(SubMode::AttachSelect { end, target });
        true
    }

    /// Makes `selection` the shape a click selected, and says whether the screen has to be painted
    /// again.
    ///
    /// **A click that selects what is already selected changed nothing**, and neither did one on a
    /// cell nobody owns with no selection to drop: the answer is `false` and the frame is not owed.
    ///
    /// **Changing or dropping the selection ends any active sub-mode**: the sub-mode belongs to the
    /// figure that was selected, so another selection or no selection starts fresh.
    pub(crate) fn select(&mut self, selection: Option<ShapeId>) -> bool {
        if self.selected == selection {
            return false;
        }

        self.selected = selection;
        self.sub_mode = None;
        true
    }

    /// Selects the next figure in drawing order (`Diagram::shapes()`, back-to-front), wrapping from
    /// the front-most figure back to the first. When nothing is selected, selects the first
    /// (back-most) figure. Returns `true` if the selection changed.
    pub(crate) fn select_next(&mut self) -> bool {
        let ids: Vec<ShapeId> = self.session.diagram().shapes().map(|(id, _)| id).collect();
        if ids.is_empty() {
            return false;
        }
        let next = match self
            .selected
            .and_then(|current| ids.iter().position(|&id| id == current))
        {
            Some(index) => ids[(index + 1) % ids.len()],
            None => ids[0],
        };
        self.select(Some(next))
    }

    /// Selects the previous figure in drawing order (`Diagram::shapes()`, front-to-back), wrapping
    /// from the first (back-most) figure to the last (front-most). When nothing is selected,
    /// selects the last (front-most) figure. Returns `true` if the selection changed.
    pub(crate) fn select_previous(&mut self) -> bool {
        let ids: Vec<ShapeId> = self.session.diagram().shapes().map(|(id, _)| id).collect();
        if ids.is_empty() {
            return false;
        }
        let prev = if let Some(index) = self
            .selected
            .and_then(|current| ids.iter().position(|&id| id == current))
        {
            ids[(index + ids.len() - 1) % ids.len()]
        } else {
            let Some(&prev) = ids.last() else {
                return false;
            };
            prev
        };
        self.select(Some(prev))
    }

    /// The canvas position at the center of the viewport window.
    ///
    /// Computed from the current scroll origin and half the buffer window dimensions.
    pub(crate) fn viewport_center(&self) -> Pos {
        let window = self.buffer.window();
        Pos {
            x: self.origin.x + window.size.width.cast_signed() / 2,
            y: self.origin.y + window.size.height.cast_signed() / 2,
        }
    }

    /// Adds `shape` to the session, selects the newly assigned identity, and returns it.
    pub(crate) fn add_shape(&mut self, shape: Shape) -> ShapeId {
        let (session, id) = self.session.add(shape);
        self.session = session;
        self.select(Some(id));
        id
    }

    /// Clones the currently selected shape, adds it to the session at the front of the drawing
    /// order, selects the newly assigned identity, and returns it.
    pub(crate) fn duplicate_selected(&mut self) -> Option<ShapeId> {
        let shape = self.selected_shape()?.clone();
        Some(self.add_shape(shape))
    }

    /// Creates a 5×5 box with `"light"` stroke at the viewport center and selects it.
    pub(crate) fn create_box(&mut self) -> ShapeId {
        let center = self.viewport_center();
        self.add_shape(Shape::Box {
            at: center,
            size: Size {
                width: 5,
                height: 5,
            },
            stroke: Stroke::from("light"),
            fill: None,
            text: None,
        })
    }

    /// Creates a vertical line of length 5 with `"light"` stroke at the viewport center and selects
    /// it.
    pub(crate) fn create_line(&mut self) -> ShapeId {
        let center = self.viewport_center();
        self.add_shape(Shape::Line {
            at: center,
            len: 5,
            orientation: Orientation::Vertical,
            stroke: Stroke::from("light"),
        })
    }

    /// Creates a detached connector with `"light"` stroke at the viewport center and selects it.
    pub(crate) fn create_connector(&mut self) -> ShapeId {
        let center = self.viewport_center();
        self.add_shape(Shape::Connector {
            from: Endpoint::absolute(
                center,
                Direction::Right,
                Terminal::Symbol {
                    style: SymbolStyleName::from("outline"),
                },
            ),
            to: Endpoint::absolute(
                Pos {
                    x: center.x + 5,
                    y: center.y + 5,
                },
                Direction::Left,
                Terminal::Symbol {
                    style: SymbolStyleName::from("solid"),
                },
            ),
            stroke: Stroke::from("light"),
        })
    }

    /// The shape the drawing of the last frame records at `at`, or `None` where nobody holds the
    /// cell.
    ///
    /// **The answer is `OwnerReader::owner`'s and its `None` is passed on whole**: the window may
    /// not hold the offset, no shape may have written the cell, or no shape may have decided it.
    ///
    /// **The window is asked first and the record second**: `OwnerReader::owner` takes a position,
    /// and the click arrives as an offset from the interior's corner, so `Window::position` is what
    /// turns one into the other. The window's origin is the scroll origin, so nothing is added to
    /// the column and the row the terminal reports.
    pub(crate) fn owner(&self, at: Offset) -> Option<ShapeId> {
        self.buffer
            .window()
            .position(at)
            .and_then(|position| self.buffer.owner(position))
    }

    /// Performs `command` on the session, recording a step when it changes the diagram.
    ///
    /// **A command that changes nothing records no step** (#285): the session is handed back as it
    /// was, and the action is still carried out, which is why the caller still owes the frame.
    ///
    /// **The session is the only thing performed on, and the application keeps no copy of it.**
    /// Every command a slice adds goes through here, so the layer below stays the one that decides
    /// what a step is.
    ///
    /// **Callers hand it a command they have already written.** The state does not name a wrapped
    /// meaning for each one: the client builds the `Command` and this is the one place the session
    /// is changed by it.
    ///
    /// **A selection that named a figure the command took away stops being the selection.** The
    /// question is the diagram's — is the figure still there? — and not the command's name, so
    /// every command that removes a figure drops it without having to say so.
    pub(crate) fn perform(&mut self, command: Command) {
        self.session = self.session.perform(command);
        self.reconcile_selection();
    }

    /// Drops the selection when the diagram no longer holds the figure it names.
    ///
    /// **The selection is a reference and a command may take its referent away without naming the
    /// selection.** Asking the diagram whether the figure is still there is what makes the rule
    /// hold for every command that removes a figure, not only the one whose name says remove.
    ///
    /// **A command that leaves the figure in place changes nothing here**: Move, Resize, `SetStroke`
    /// and `SetText` rewrite the figure under the same identity, and the selection survives them as
    /// it did.
    fn reconcile_selection(&mut self) {
        let Some(selected) = self.selected else {
            return;
        };

        if self.session.diagram().get(selected).is_none() {
            self.select(None);
        }
    }

    /// Gives back the step behind the session, if there is one.
    ///
    /// **The walk is the session's and the application keeps no copy of it** (§3 of the application
    /// model): one `Session::undo` moves the position, and the next frame is drawn from what the
    /// session holds then. At the oldest position nothing comes back, and the caller still owes the
    /// frame because its action was carried out.
    ///
    /// **The walk clears the selection** (#297): walking history returns the application to `IDLE`
    /// and makes the restored drawing a clean starting point. Any active sub-mode drops with it.
    pub(crate) fn undo(&mut self) {
        if self.session.can_undo() {
            self.session = self.session.undo();
            self.select(None);
        }
    }

    /// Gives back the step ahead of the session, if there is one.
    ///
    /// **The walk is the session's, here too**: one `Session::redo` moves the position, and the
    /// application keeps no copy of where the walk stands. At the newest position nothing comes
    /// back, and the caller still owes the frame.
    ///
    /// **The walk clears the selection, here too** (#297): the restored drawing starts with
    /// nothing selected and any active sub-mode dropped.
    pub(crate) fn redo(&mut self) {
        if self.session.can_redo() {
            self.session = self.session.redo();
            self.select(None);
        }
    }

    /// Whether a step is behind the session.
    ///
    /// **This read exists for the tests**: no frame asks it, because every entry is drawn alike at
    /// every position of the history (D2 of #181). The tests ask it to tell a walk from a press
    /// that performed none.
    #[cfg(test)]
    pub(crate) fn can_undo(&self) -> bool {
        self.session.can_undo()
    }

    /// Whether a step is ahead of the session.
    ///
    /// **This read exists for the tests**, for the same reason [`Self::can_undo`] does.
    #[cfg(test)]
    pub(crate) fn can_redo(&self) -> bool {
        self.session.can_redo()
    }

    /// Whether the shape that owns `at` of the last frame's drawing is the selected shape or
    /// the focused attach target in [`SubMode::AttachSelect`].
    ///
    /// **This is where the inversion is decided, and it is decided from the record** (D1): a
    /// position nobody owns is never the selection, and an identity no cell records inverts
    /// nothing because no cell asks about it (rule 7).
    pub(crate) fn is_cell_selected(&self, at: Offset) -> bool {
        let Some(owner) = self.owner(at) else {
            return false;
        };
        self.selected() == Some(owner)
            || matches!(self.sub_mode, Some(SubMode::AttachSelect { target, .. }) if target == owner)
    }

    /// The offset into the last frame's drawing that the screen position `at` reaches, or `None`
    /// where the position is outside the interior.
    ///
    /// **The interior's corner is taken off, and the window's origin takes no part.** A click
    /// arrives as a column and a row of the screen, and the offset the window turns into the
    /// position `OwnerReader::owner` asks about is counted from the corner of the window the person
    /// saw — the scroll origin belongs to the plane rather than to the screen, and adding it here
    /// would ask about another cell (rule 1).
    pub(crate) fn offset_of(&self, at: Position) -> Option<Offset> {
        if !self.interior.contains(at) {
            return None;
        }

        Some(Offset {
            x: at.x - self.interior.x,
            y: at.y - self.interior.y,
        })
    }

    /// What a press at `at` lands on, as far as the region is concerned.
    ///
    /// **The interior is asked about before the border**: the window is inside the region's own
    /// rectangle, so the cell comes first and the frame drawn around it is the border — the
    /// keyboard's without being a cell of the drawing (rule 2 of #189).
    pub(crate) fn target_at(&self, at: Position) -> Target {
        if let Some(offset) = self.offset_of(at) {
            Target::Interior(offset)
        } else if self.region.contains(at) {
            Target::Border
        } else {
            Target::Nothing
        }
    }

    /// The region's own rectangle, border included, as the last frame drew it.
    ///
    /// **The outer rectangle and not the window** (rule 2 of #189): a press on the border gives the
    /// region the keyboard without landing in the drawing, and a press outside the rectangle
    /// belongs to another leaf or to none. Empty until the first frame is drawn.
    pub(crate) fn region(&self) -> Rect {
        self.region
    }

    /// The two bars' own state over `area`, for the widget that draws them and the one that reads a
    /// press on them.
    ///
    /// **The region is the area, because the bars sit on its border** (D4): the helper lays each of
    /// them on the block's own row or column, between the corners, so what it answers a press
    /// against is the rectangle the region drew rather than the window.
    pub(crate) fn scroll_area(&mut self, area: Rect) -> ScrollAreaState<'_> {
        ScrollAreaState::new()
            .area(area)
            .v_scroll(&mut self.vscroll)
            .h_scroll(&mut self.hscroll)
    }

    /// The same two states, for the widget that draws the bars into the border's run.
    ///
    /// **The area each bar is drawn into is the area a press is answered against**: the helper writes
    /// it as it draws, so a press is read against the bar the last frame put on the screen.
    pub(crate) fn scroll_states(&mut self) -> (&mut ScrollState, &mut ScrollState) {
        (&mut self.hscroll, &mut self.vscroll)
    }

    /// Applies what the bars answered, and says whether the view moved (D3).
    ///
    /// **The answer is the helper's, step and all**: a wheel turn is the delta it names and a press
    /// on the bar is the offset it names, so the step is a tenth of the window rather than a rule of
    /// this application's.
    ///
    /// **The origin is derived from the offset and clamped** (D1): `set_offset` puts the value it is
    /// given there without limiting it, and a drag that leaves the bar's track names an offset past
    /// the content's span, so the offset names an origin and the range decides where that lands.
    ///
    /// **A frame is owed only when the origin moved**: a turn the content refuses, and a press where
    /// the view already stands, changed nothing — but the bars took the event, so the answer is
    /// [`Reply::Consumed`] and the region's click does not read the same press again.
    pub(crate) fn scrolled(&mut self, answer: ScrollOutcome) -> Reply {
        let changed = match answer {
            ScrollOutcome::Up(n) => self.vscroll.scroll_up(n),
            ScrollOutcome::Down(n) => self.vscroll.scroll_down(n),
            ScrollOutcome::Left(n) => self.hscroll.scroll_left(n),
            ScrollOutcome::Right(n) => self.hscroll.scroll_right(n),
            ScrollOutcome::VPos(at) => self.vscroll.set_offset(at),
            ScrollOutcome::HPos(at) => self.hscroll.set_offset(at),
            // **`Changed` is the helper's own repaint signal and the application does not take it**:
            // it carries no offset, and what this application owes a frame for is an origin that
            // moved (D3), so there is nothing here to apply. Neither `ScrollAreaState` nor
            // `ScrollState` answers it today — a step, an offset or nothing at all is all they say —
            // and the assertion is what makes a helper that starts saying it a failing test rather
            // than a frame lost in silence.
            ScrollOutcome::Changed => {
                debug_assert!(false, "the bars answered with a repaint and no offset");
                false
            }
            ScrollOutcome::Unchanged => return Reply::Consumed,
            ScrollOutcome::Continue => return Reply::Ignored,
        };

        if !changed {
            return Reply::Consumed;
        }

        let (across, down) = axes(self.measure(), self.interior);
        let origin = Pos {
            x: across.at(self.hscroll.offset()),
            y: down.at(self.vscroll.offset()),
        };
        let moved = origin != self.origin;
        self.origin = origin;

        if moved {
            Reply::Repaint
        } else {
            Reply::Consumed
        }
    }

    /// Draws the session's diagram into `interior` — the window, as the screen holds it — at the
    /// scroll origin, replacing whatever the frame before drew.
    ///
    /// **The window is kept whole rather than as a size**, because the two questions later frames
    /// ask of it are both about the whole: how large it is, and where its corner is on the screen.
    ///
    /// **The origin is put where the content allows before anything is drawn** (D1): the measurement
    /// is [taken first](Self::measure) because the window a frame draws through depends on the
    /// drawing rather than the other way round, and a content change that no longer holds the origin
    /// is clamped by the frame that follows it rather than by the scroll that follows that.
    ///
    /// **The region is kept beside it** (rule 2 of #189): a press on the border gives the region
    /// the keyboard without landing in the window, so the outer rectangle is a fact of the frame
    /// the region answered and not one the interior can be asked for.
    pub(crate) fn redraw(&mut self, region: Rect, interior: Rect) {
        let (across, down) = axes(self.measure(), interior);
        self.origin = Pos {
            x: across.clamp(self.origin.x),
            y: down.clamp(self.origin.y),
        };
        across.place(&mut self.hscroll, self.origin.x);
        down.place(&mut self.vscroll, self.origin.y);

        let mut buffer = Buffer::new(Window {
            origin: self.origin,
            size: Size {
                width: interior.width,
                height: interior.height,
            },
        });
        let footprint = {
            let mut sink = FootprintSink::new(&mut buffer);
            self.session.diagram().draw(&mut sink);
            sink.footprint()
        };
        self.buffer = buffer;
        self.footprint = footprint;
        self.interior = interior;
        self.region = region;
    }

    /// How far the session's diagram reaches, measured on a drawing that keeps nothing (D2).
    ///
    /// **The window is empty, so no cell of the traversal survives it**: what the answer holds is
    /// the stamps themselves, which is what lets the origin be chosen before the frame is drawn.
    /// What it costs is a second traversal of the shapes per frame, and the first one writes
    /// nothing.
    fn measure(&self) -> Option<Footprint> {
        let mut buffer = Buffer::new(Window {
            origin: Pos { x: 0, y: 0 },
            size: Size {
                width: 0,
                height: 0,
            },
        });
        let mut sink = FootprintSink::new(&mut buffer);
        self.session.diagram().draw(&mut sink);

        sink.footprint()
    }
}

/// One axis of the content the view may show: the first and last cell it reaches in the plane, and
/// the window it is shown through (D1).
///
/// **The content is the drawing's footprint union the first screen** — the window at `(0, 0)`,
/// which is where the view opens. So the content reaches left of or above the origin only where the
/// drawing does, and its last cell is never before the first screen's: a drawing away from `(0, 0)`
/// still opens on the first screen, and one that stamped nothing leaves the content as that screen,
/// which does not scroll.
#[derive(Debug, Clone, Copy)]
struct Axis {
    /// The content's first cell in the plane.
    first: i32,

    /// The content's last cell in the plane.
    last: i32,

    /// The interior's size along this axis.
    window: i32,
}

impl Axis {
    /// The content `footprint` and the first screen make on one axis, `reach` being the footprint's
    /// first and last cell there and `None` while nothing has been drawn.
    ///
    /// **A window of no size is no screen, and so no content**: the first screen's last cell is the
    /// window's own minus one, which is before `(0, 0)` when there is no window, and that is what
    /// leaves the span at zero rather than at the one cell the corner alone would make.
    fn of(reach: Option<(i16, i16)>, window: u16) -> Self {
        let window = i32::from(window);
        let (first, last) = match reach {
            Some((first, last)) => (i32::from(first), i32::from(last)),
            None => (0, window - 1),
        };

        Self {
            first: first.min(0),
            last: last.max(window - 1),
            window,
        }
    }

    /// The first and last origin D1 allows, both included.
    ///
    /// **Computed in `i32`**, because `last - window + 1` leaves an `i16` at the plane's own corner,
    /// and the `max` is what keeps a content smaller than the window still opening at `(0, 0)`.
    fn origins(&self) -> (i32, i32) {
        (self.first, (self.last - self.window + 1).max(0))
    }

    /// `origin` put at the nearest value of that range, which is what every frame does with it.
    fn clamp(&self, origin: i16) -> i16 {
        let (first, last) = self.origins();
        let at = i32::from(origin).clamp(first, last);

        i16::try_from(at).expect("a cell of the content fits the plane it was measured in")
    }

    /// How far past the window the content reaches, which is how far the view can travel.
    fn span(&self) -> usize {
        usize::try_from((self.last - self.first + 1 - self.window).max(0))
            .expect("a count of cells is a length")
    }

    /// Where `origin` stands, counted from the content's first cell.
    fn offset(&self, origin: i16) -> usize {
        usize::try_from(i32::from(origin) - self.first)
            .expect("the origin lies in the content, so it is never before it")
    }

    /// Writes `origin` and the content into a bar's own state, which is what the bar draws and what
    /// a press on it is answered against.
    fn place(&self, state: &mut ScrollState, origin: i16) {
        state.set_page_len(usize::try_from(self.window).expect("a window's size is a length"));
        state.set_max_offset(self.span());
        state.set_offset(self.offset(origin));
    }

    /// The origin the bar's `offset` names, put at the nearest value the range allows.
    ///
    /// **The bar's answer is not clamped before it arrives**: the helper maps the track's cells over
    /// the content's reach — `pos = row - base - 1` over `span = length - 2` — so a press on the
    /// track's last cell names the span itself, which is the furthest offset there is, and only a
    /// drag that carries the pointer past the track names more: nothing stops it at the area the
    /// drag began in. `set_offset` writes what it is given without limiting it, so the offset names
    /// an origin and the origin's range is what says where a view of nothing can still stand.
    fn at(&self, offset: usize) -> i16 {
        let at = self
            .first
            .saturating_add(i32::try_from(offset).unwrap_or(i32::MAX));
        let (first, last) = self.origins();
        let at = at.clamp(first, last);

        i16::try_from(at).expect("a cell of the content fits the plane it was measured in")
    }
}

/// The two axes of the content, from a footprint and the window it is shown through.
fn axes(footprint: Option<Footprint>, interior: Rect) -> (Axis, Axis) {
    (
        Axis::of(footprint.map(|it| (it.min.x, it.max.x)), interior.width),
        Axis::of(footprint.map(|it| (it.min.y, it.max.y)), interior.height),
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::num::NonZeroU16;

    /// A description whose only shape is a light 4×3 box at `(2, 1)`.
    const A_BOX: &str = r#"{
        "next_id": 2,
        "shapes": [ { "kind": "box", "id": 1, "at": { "x": 2, "y": 1 },
                      "size": { "width": 4, "height": 3 }, "stroke": "light" } ]
    }"#;

    /// The description #176's examples argue D1 with: a light `4×3` box at `(0, 0)`, a double `2×2`
    /// box at `(10, 1)` and a seven-cell line at `(2, 4)`, so the drawing reaches `(11, 4)`.
    ///
    /// **A window of `5×3` is the interior of a small region**, which is the size the examples use:
    /// the drawing is larger than it on both axes, so the origin has room to move and the far end of
    /// that room is `(7, 2)`.
    const THE_EXAMPLE: &str = r#"{
        "next_id": 4,
        "shapes": [
            { "kind": "box", "id": 1, "at": { "x": 0, "y": 0 },
              "size": { "width": 4, "height": 3 }, "stroke": "light" },
            { "kind": "box", "id": 2, "at": { "x": 10, "y": 1 },
              "size": { "width": 2, "height": 2 }, "stroke": "double" },
            { "kind": "line", "id": 3, "at": { "x": 2, "y": 4 },
              "len": 7, "orientation": "horizontal", "stroke": "light" }
        ]
    }"#;

    /// The region the example is shown through, and its interior: `5×3` at `(1, 1)` of the screen.
    const EXAMPLE_REGION: Rect = Rect::new(0, 0, 7, 5);
    const EXAMPLE_INTERIOR: Rect = Rect::new(1, 1, 5, 3);

    /// The diagram state over the diagram `json` describes.
    fn a_diagram_over(json: &str) -> DiagramState {
        let diagram =
            monospace_description::parse(json).expect("the test's description is readable");

        DiagramState::new(Session::new(diagram))
    }

    /// The glyph the last frame drew at `(x, y)` of its window, or `" "` where it drew none.
    fn at(state: &DiagramState, x: u16, y: u16) -> String {
        let catalog = monospace_glyph_sets::all();

        state
            .glyph(Offset { x, y }, &catalog)
            .unwrap_or(" ")
            .to_string()
    }

    /// Every glyph of the last frame's window, row by row.
    fn window_of(state: &DiagramState) -> Vec<String> {
        (0..EXAMPLE_INTERIOR.height)
            .map(|y| {
                (0..EXAMPLE_INTERIOR.width)
                    .map(|x| at(state, x, y))
                    .collect()
            })
            .collect()
    }

    /// **A state that has not drawn a frame has measured nothing**, and the first `redraw` is what
    /// measures the drawing: the box of `A_BOX` reaches `(5, 3)` from `(2, 1)`.
    #[test]
    fn a_state_that_has_not_drawn_a_frame_has_no_footprint() {
        let diagram =
            monospace_description::parse(A_BOX).expect("the test's description is readable");
        let mut state = DiagramState::new(Session::new(diagram));

        assert_eq!(
            state.footprint(),
            None,
            "a state that has drawn no frame reached somewhere"
        );

        state.redraw(Rect::new(0, 0, 20, 10), Rect::new(0, 0, 20, 10));

        assert_eq!(
            state.footprint(),
            Some(Footprint {
                min: Pos { x: 2, y: 1 },
                max: Pos { x: 5, y: 3 },
            }),
            "the first frame did not measure the drawing"
        );
    }

    /// The footprint of the last frame is the drawing's and not the interior's: the box of
    /// `A_BOX` stands at `(2, 1)` and is four by three, so it reaches `(5, 3)` whether the interior
    /// holds it or not.
    ///
    /// **The two interiors are the case**: one holds a single cell of the box and the other holds
    /// all of it, and the answer does not move. A record taken from the cells would give `(2, 1)`
    /// in the first, because the one cell the window holds is the box's corner.
    #[test]
    fn the_last_frames_footprint_is_the_diagrams() {
        let diagram =
            monospace_description::parse(A_BOX).expect("the test's description is readable");
        let mut state = DiagramState::new(Session::new(diagram));

        state.redraw(Rect::new(0, 0, 3, 2), Rect::new(0, 0, 1, 1));

        assert_eq!(
            state.footprint(),
            Some(Footprint {
                min: Pos { x: 2, y: 1 },
                max: Pos { x: 5, y: 3 },
            }),
            "the interior decided how far the drawing reaches"
        );

        state.redraw(Rect::new(0, 0, 20, 10), Rect::new(0, 0, 20, 10));

        assert_eq!(
            state.footprint(),
            Some(Footprint {
                min: Pos { x: 2, y: 1 },
                max: Pos { x: 5, y: 3 },
            }),
            "a larger interior changed the diagram's footprint"
        );
    }

    /// **The view opens at `(0, 0)`, whatever the drawing reaches** (rule 1). The example's content
    /// runs from `0` to `11` across and from `0` to `4` down, so the origin may lie between `0` and
    /// `7` and between `0` and `2` — and the first frame puts it at the near end of both.
    #[test]
    fn a_frame_opens_at_the_origin() {
        let mut state = a_diagram_over(THE_EXAMPLE);

        state.redraw(EXAMPLE_REGION, EXAMPLE_INTERIOR);

        assert_eq!(
            state.origin(),
            Pos { x: 0, y: 0 },
            "the first frame did not open on the first screen"
        );
        assert_eq!(
            at(&state, 0, 0),
            "┌",
            "the drawing's own corner is not in the window"
        );
    }

    /// **A drawing inside the first screen does not scroll** (rule 1). The content is the union of
    /// the drawing and the window at `(0, 0)`, so a box that fits leaves neither axis any room to
    /// travel and the frame opens where the drawing is.
    #[test]
    fn a_drawing_inside_the_first_screen_does_not_scroll() {
        let mut state = a_diagram_over(A_BOX);

        state.redraw(Rect::new(0, 0, 20, 10), Rect::new(1, 1, 18, 6));

        assert_eq!(
            state.origin(),
            Pos { x: 0, y: 0 },
            "a drawing inside the first screen opened somewhere else"
        );
        assert_eq!(
            state.hscroll.max_offset(),
            0,
            "the content is wider than the window it fits"
        );
        assert_eq!(
            state.vscroll.max_offset(),
            0,
            "the content is taller than the window it fits"
        );
    }

    /// **A window of no size has nothing to scroll** (D1). The content is the first screen, and a
    /// screen of no cells is no content at all: the region of a terminal too small to hold one draws
    /// no border and answers no click, so a bar claiming a reach of one there would be a bar that
    /// cannot be drawn and cannot be moved.
    #[test]
    fn a_window_of_no_size_has_nothing_to_scroll() {
        assert_eq!(
            Axis::of(None, 0).span(),
            0,
            "a window of no size claims a cell the view can travel"
        );
        assert_eq!(
            Axis::of(None, 18).span(),
            0,
            "a screen with nothing drawn claims a cell to travel"
        );
    }

    /// **The frame draws at the origin the content allows** (rule 2). At `(7, 2)`, the last value
    /// D1 allows for a `5×3` window, the window's right edge is the drawing's and its bottom edge is
    /// the drawing's: the double box's last row and the line's last two cells are all that is left.
    #[test]
    fn a_frame_draws_at_the_origin_the_content_allows() {
        let mut state = a_diagram_over(THE_EXAMPLE);
        state.origin = Pos { x: 7, y: 2 };

        state.redraw(EXAMPLE_REGION, EXAMPLE_INTERIOR);

        assert_eq!(
            state.origin(),
            Pos { x: 7, y: 2 },
            "the frame moved an origin the content holds"
        );
        assert_eq!(
            window_of(&state).join("\n"),
            "   ╚╝\n     \n──   ",
            "the window does not hold what the drawing reaches at that origin"
        );
    }

    /// **A frame at the end of the content owes no frame** (rule 2). An origin the content does not
    /// hold is put at the nearest value it does by the frame that draws it, and the frame after it
    /// draws the same picture: the clamp is not a change the next frame has to be owed for.
    #[test]
    fn a_frame_at_the_end_of_the_content_owes_no_frame() {
        let mut state = a_diagram_over(THE_EXAMPLE);
        state.origin = Pos { x: 20, y: 20 };

        state.redraw(EXAMPLE_REGION, EXAMPLE_INTERIOR);
        let clamped = window_of(&state);

        assert_eq!(
            state.origin(),
            Pos { x: 7, y: 2 },
            "the frame did not put the origin at the furthest the content allows"
        );

        state.redraw(EXAMPLE_REGION, EXAMPLE_INTERIOR);

        assert_eq!(
            state.origin(),
            Pos { x: 7, y: 2 },
            "the frame that followed moved the view"
        );
        assert_eq!(
            window_of(&state),
            clamped,
            "the frame that followed drew another picture"
        );
    }

    /// **The origin is what survives a frame, and not the offset** (rule 2). The finding box is
    /// moved two cells further out and the content still holds the origin, so the view stays where
    /// the person left it. The figures away from the origin are then taken out, and the origin the
    /// content no longer holds is put at the nearest value it does.
    #[test]
    fn a_content_change_keeps_the_origin() {
        let mut state = a_diagram_over(THE_EXAMPLE);
        let finding = ShapeId::new(NonZeroU16::new(2).expect("2 is not zero"));
        let line = ShapeId::new(NonZeroU16::new(3).expect("3 is not zero"));
        state.origin = Pos { x: 7, y: 2 };
        state.redraw(EXAMPLE_REGION, EXAMPLE_INTERIOR);

        state.perform(Command::Move {
            id: finding,
            by: (2, 0).into(),
        });
        state.redraw(EXAMPLE_REGION, EXAMPLE_INTERIOR);

        assert_eq!(
            state.origin(),
            Pos { x: 7, y: 2 },
            "a change to the drawing carried the view with it"
        );

        state.perform(Command::Remove { id: finding });
        state.perform(Command::Remove { id: line });
        state.redraw(EXAMPLE_REGION, EXAMPLE_INTERIOR);

        assert_eq!(
            state.origin(),
            Pos { x: 0, y: 0 },
            "an origin the content no longer holds was kept"
        );
    }

    /// **The removal is the session's, and it records the step #181 gives back.** One command is
    /// one step: there is something behind the position and nothing ahead of it.
    #[test]
    fn a_removal_leaves_the_session_one_step_further_on() {
        let diagram =
            monospace_description::parse(A_BOX).expect("the test's description is readable");
        let mut state = DiagramState::new(Session::new(diagram));
        let shape = ShapeId::new(NonZeroU16::new(1).expect("1 is not zero"));

        assert!(
            !state.session.can_undo(),
            "a fresh session has a step behind it"
        );

        state.perform(Command::Remove { id: shape });

        assert!(state.session.can_undo(), "the removal recorded no step");
        assert!(
            !state.session.can_redo(),
            "the removal recorded more than one step"
        );
        assert!(
            state.session.diagram().get(shape).is_none(),
            "the session still holds the removed shape"
        );
        assert_eq!(
            state.selected(),
            None,
            "the removal kept the identity it took out"
        );
    }

    /// **A command that removes the selected figure drops the selection**, and the sub-mode that
    /// belonged to it goes with the identity: the figure the selection named is no longer in the
    /// diagram, so there is nothing left to select.
    #[test]
    fn a_command_that_removes_the_selected_figure_drops_the_selection() {
        let selected = ShapeId::new(NonZeroU16::new(1).expect("1 is not zero"));

        for sub_mode in [
            SubMode::StrokeSelect,
            SubMode::OrderMode,
            SubMode::Endpoint(End::From),
            SubMode::Endpoint(End::To),
            SubMode::TerminalSelect(End::From),
            SubMode::TerminalSelect(End::To),
            SubMode::LeavingSelect(End::From),
            SubMode::LeavingSelect(End::To),
            SubMode::AttachSelect {
                end: End::From,
                target: ShapeId::new(NonZeroU16::new(2).expect("2 is not zero")),
            },
        ] {
            let mut state = a_diagram_over(THE_EXAMPLE);
            state.select(Some(selected));
            state.set_sub_mode(Some(sub_mode));
            state.perform(Command::Remove { id: selected });

            assert_eq!(
                state.selected(),
                None,
                "the selection named a figure the diagram lost"
            );
            assert_eq!(
                state.sub_mode(),
                None,
                "the sub-mode survived the figure it belonged to"
            );
        }
    }

    #[test]
    fn sub_mode_parent_returns_expected_parent() {
        assert_eq!(
            SubMode::TerminalSelect(End::From).parent(),
            Some(SubMode::Endpoint(End::From))
        );
        assert_eq!(
            SubMode::TerminalSelect(End::To).parent(),
            Some(SubMode::Endpoint(End::To))
        );
        assert_eq!(
            SubMode::LeavingSelect(End::From).parent(),
            Some(SubMode::Endpoint(End::From))
        );
        assert_eq!(
            SubMode::LeavingSelect(End::To).parent(),
            Some(SubMode::Endpoint(End::To))
        );
        assert_eq!(SubMode::StrokeSelect.parent(), None);
        assert_eq!(SubMode::OrderMode.parent(), None);
        assert_eq!(SubMode::Endpoint(End::From).parent(), None);
        assert_eq!(SubMode::Endpoint(End::To).parent(), None);
    }

    /// **A command that removes another figure keeps the selection.** The rule asks whether the
    /// diagram still holds the figure the selection names, so a removal that takes another one away
    /// changes nothing here. This is what distinguishes the state's rule from a cleanup tied to the
    /// command's name: `Delete` could never produce this case.
    #[test]
    fn a_command_that_removes_another_figure_keeps_the_selection() {
        let mut state = a_diagram_over(THE_EXAMPLE);
        let selected = ShapeId::new(NonZeroU16::new(1).expect("1 is not zero"));
        let other = ShapeId::new(NonZeroU16::new(2).expect("2 is not zero"));

        state.select(Some(selected));
        state.perform(Command::Remove { id: other });

        assert!(
            state.session.diagram().get(other).is_none(),
            "the session still holds the figure the command removed"
        );
        assert_eq!(
            state.selected(),
            Some(selected),
            "the selection named a figure the diagram still holds"
        );
    }

    /// **The movement is the session's too, and it records one step.** The figure stays under its
    /// identity and there is a step behind the position, which is what `Undo` gives back.
    #[test]
    fn a_move_leaves_the_session_one_step_further_on() {
        let diagram =
            monospace_description::parse(A_BOX).expect("the test's description is readable");
        let mut state = DiagramState::new(Session::new(diagram));
        let shape = ShapeId::new(NonZeroU16::new(1).expect("1 is not zero"));

        state.perform(Command::Move {
            id: shape,
            by: (1, 0).into(),
        });

        assert!(state.session.can_undo(), "the move recorded no step");
        assert!(
            !state.session.can_redo(),
            "the move recorded more than one step"
        );
        assert!(
            state.session.diagram().get(shape).is_some(),
            "the session lost the shape it moved"
        );
    }

    /// **The resize is the session's too, and it records one step.**
    #[test]
    fn a_resize_leaves_the_session_one_step_further_on() {
        let diagram =
            monospace_description::parse(A_BOX).expect("the test's description is readable");
        let mut state = DiagramState::new(Session::new(diagram));
        let shape = ShapeId::new(NonZeroU16::new(1).expect("1 is not zero"));

        state.perform(Command::Resize {
            id: shape,
            by: (1, 0).into(),
        });

        assert!(state.session.can_undo(), "the resize recorded no step");
        assert!(
            !state.session.can_redo(),
            "the resize recorded more than one step"
        );
        let Some(monospace_diagram::Shape::Box { size, .. }) = state.session.diagram().get(shape)
        else {
            panic!("expected box");
        };
        assert_eq!(size.width, 5);
    }

    /// **The walk stops at both ends of the history.** An undo at the oldest position and a redo at
    /// the newest give nothing back, and the step between them is the one the removal recorded.
    #[test]
    fn undo_and_redo_stop_at_the_ends_of_the_history() {
        let diagram =
            monospace_description::parse(A_BOX).expect("the test's description is readable");
        let mut state = DiagramState::new(Session::new(diagram));
        let shape = ShapeId::new(NonZeroU16::new(1).expect("1 is not zero"));

        state.undo();

        assert!(
            state.session.diagram().get(shape).is_some(),
            "the undo at the oldest position took the box out"
        );

        state.perform(Command::Remove { id: shape });

        assert!(
            state.session.diagram().get(shape).is_none(),
            "the removal kept the box"
        );
        assert!(!state.can_redo(), "the removal left a step ahead");

        state.undo();

        assert!(
            state.session.diagram().get(shape).is_some(),
            "the undo did not give the box back"
        );
        assert!(!state.can_undo(), "one step back left another behind it");
        assert!(state.can_redo(), "the undo left nothing to redo");

        state.redo();

        assert!(
            state.session.diagram().get(shape).is_none(),
            "the redo did not take the box out again"
        );
        assert!(
            !state.can_redo(),
            "one step forward left another ahead of it"
        );
        assert!(state.can_undo(), "the redo left nothing to undo");

        state.redo();

        assert!(
            state.session.diagram().get(shape).is_none(),
            "the redo at the newest position took the box out of nothing"
        );
        assert!(
            !state.can_redo(),
            "the redo at the newest position recorded a step"
        );
    }

    #[test]
    fn the_session_is_exposed() {
        let diagram =
            monospace_description::parse(A_BOX).expect("the test's description is readable");
        let state = DiagramState::new(Session::new(diagram));
        assert!(!state.session().can_undo());
    }

    /// **A walk clears the selection and any active sub-mode** (#297).
    #[test]
    fn undo_and_redo_clear_the_selection_and_sub_mode() {
        let diagram =
            monospace_description::parse(A_BOX).expect("the test's description is readable");
        let mut state = DiagramState::new(Session::new(diagram));
        let shape = ShapeId::new(NonZeroU16::new(1).expect("1 is not zero"));

        state.perform(Command::Move {
            id: shape,
            by: (1, 0).into(),
        });
        state.select(Some(shape));
        state.set_sub_mode(Some(SubMode::StrokeSelect));

        state.undo();

        assert_eq!(state.selected(), None, "the undo kept the selection");
        assert_eq!(state.sub_mode(), None, "the undo kept the sub-mode");

        state.select(Some(shape));
        state.set_sub_mode(Some(SubMode::StrokeSelect));

        state.redo();

        assert_eq!(state.selected(), None, "the redo kept the selection");
        assert_eq!(state.sub_mode(), None, "the redo kept the sub-mode");

        // When can_undo or can_redo is false, no action is performed and selection is preserved.
        state.select(Some(shape));
        state.set_sub_mode(Some(SubMode::StrokeSelect));

        assert!(!state.can_redo());
        state.redo();
        assert_eq!(
            state.selected(),
            Some(shape),
            "ineffective redo cleared the selection"
        );
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::StrokeSelect),
            "ineffective redo cleared the sub-mode"
        );

        state.undo();
        assert_eq!(state.selected(), None);

        state.select(Some(shape));
        state.set_sub_mode(Some(SubMode::StrokeSelect));

        assert!(!state.can_undo());
        state.undo();
        assert_eq!(
            state.selected(),
            Some(shape),
            "ineffective undo cleared the selection"
        );
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::StrokeSelect),
            "ineffective undo cleared the sub-mode"
        );
    }

    #[test]
    fn select_next_and_select_previous_cycle_in_drawing_order_and_wrap() {
        let mut empty = DiagramState::new(Session::new(monospace_diagram::Diagram::new()));
        assert!(!empty.select_next());
        assert!(!empty.select_previous());
        assert_eq!(empty.selected(), None);

        let single_diagram =
            monospace_description::parse(A_BOX).expect("the test's description is readable");
        let mut single = DiagramState::new(Session::new(single_diagram));
        let id1 = ShapeId::new(NonZeroU16::new(1).expect("1 is not zero"));
        assert!(single.select_next());
        assert_eq!(single.selected(), Some(id1));
        assert!(!single.select_next());
        assert_eq!(single.selected(), Some(id1));
        assert!(!single.select_previous());
        assert_eq!(single.selected(), Some(id1));

        let three_shapes = r#"{
            "next_id": 4,
            "shapes": [
                { "kind": "box", "id": 1, "at": { "x": 0, "y": 0 }, "size": { "width": 4, "height": 3 }, "stroke": "light" },
                { "kind": "box", "id": 2, "at": { "x": 6, "y": 0 }, "size": { "width": 4, "height": 3 }, "stroke": "light" },
                { "kind": "box", "id": 3, "at": { "x": 12, "y": 0 }, "size": { "width": 4, "height": 3 }, "stroke": "light" }
            ]
        }"#;
        let diagram = monospace_description::parse(three_shapes).expect("valid description");
        let mut state = DiagramState::new(Session::new(diagram));
        let id2 = ShapeId::new(NonZeroU16::new(2).expect("2 is not zero"));
        let id3 = ShapeId::new(NonZeroU16::new(3).expect("3 is not zero"));

        assert!(state.select_next());
        assert_eq!(state.selected(), Some(id1));
        assert!(state.select_next());
        assert_eq!(state.selected(), Some(id2));
        assert!(state.select_next());
        assert_eq!(state.selected(), Some(id3));
        assert!(state.select_next());
        assert_eq!(state.selected(), Some(id1));

        assert!(state.select_previous());
        assert_eq!(state.selected(), Some(id3));
        assert!(state.select_previous());
        assert_eq!(state.selected(), Some(id2));
        assert!(state.select_previous());
        assert_eq!(state.selected(), Some(id1));

        state.select(None);
        assert!(state.select_previous());
        assert_eq!(state.selected(), Some(id3));
    }

    #[test]
    fn create_box_creates_default_5x5_box_at_viewport_center_and_selects_it() {
        let mut state = DiagramState::new(Session::new(monospace_diagram::Diagram::new()));
        state.redraw(Rect::new(0, 0, 20, 10), Rect::new(0, 0, 20, 10));
        assert_eq!(state.viewport_center(), Pos { x: 10, y: 5 });

        let box_id = state.create_box();
        assert_eq!(state.selected(), Some(box_id));
        assert_eq!(state.sub_mode(), None);

        let shape = state.selected_shape().expect("box is in diagram");
        assert_eq!(
            shape,
            &Shape::Box {
                at: Pos { x: 10, y: 5 },
                size: Size {
                    width: 5,
                    height: 5
                },
                stroke: Stroke::from("light"),
                fill: None,
                text: None,
            }
        );

        // Undo removes the box
        state.session = state.session.undo();
        assert_eq!(state.session.diagram().shape_count(), 0);
    }

    #[test]
    fn create_line_creates_vertical_line_on_scrolled_viewport_and_selects_it() {
        let mut state = a_diagram_over(THE_EXAMPLE);
        // Scrolled viewport where origin != (0, 0)
        state.origin = Pos { x: 6, y: 2 };
        state.redraw(EXAMPLE_REGION, EXAMPLE_INTERIOR);
        let center = state.viewport_center();
        assert_eq!(
            center,
            Pos {
                x: state.origin().x + EXAMPLE_INTERIOR.width.cast_signed() / 2,
                y: state.origin().y + EXAMPLE_INTERIOR.height.cast_signed() / 2,
            }
        );

        let prior_count = state.session.diagram().shape_count();
        let line_id = state.create_line();
        assert_eq!(state.selected(), Some(line_id));
        assert_eq!(state.sub_mode(), None);
        assert_eq!(state.session.diagram().shape_count(), prior_count + 1);

        let shape = state.selected_shape().expect("line is in diagram");
        assert_eq!(
            shape,
            &Shape::Line {
                at: center,
                len: 5,
                orientation: Orientation::Vertical,
                stroke: Stroke::from("light"),
            }
        );

        // Undo removes the line
        state.session = state.session.undo();
        assert_eq!(state.session.diagram().shape_count(), prior_count);
    }

    #[test]
    fn create_connector_creates_detached_connector_with_terminals_and_selects_it() {
        use monospace_diagram::EndpointAt;

        let mut state = DiagramState::new(Session::new(monospace_diagram::Diagram::new()));
        state.redraw(Rect::new(0, 0, 16, 8), Rect::new(0, 0, 16, 8));
        let center = state.viewport_center();
        assert_eq!(center, Pos { x: 8, y: 4 });

        let conn_id = state.create_connector();
        assert_eq!(state.selected(), Some(conn_id));
        assert_eq!(state.sub_mode(), None);

        let shape = state.selected_shape().expect("connector is in diagram");
        let Shape::Connector { from, to, stroke } = shape else {
            panic!("expected connector shape");
        };
        assert_eq!(
            from.at,
            EndpointAt::Absolute {
                at: Pos { x: 8, y: 4 },
                leaving: Direction::Right,
            }
        );
        assert_eq!(
            from.terminal,
            Terminal::Symbol {
                style: SymbolStyleName::from("outline"),
            }
        );
        assert_eq!(
            to.at,
            EndpointAt::Absolute {
                at: Pos { x: 13, y: 9 },
                leaving: Direction::Left,
            }
        );
        assert_eq!(
            to.terminal,
            Terminal::Symbol {
                style: SymbolStyleName::from("solid"),
            }
        );
        assert_eq!(*stroke, Stroke::from("light"));

        // Undo restores prior diagram
        state.session = state.session.undo();
        assert_eq!(state.session.diagram().shape_count(), 0);
    }

    #[test]
    fn entering_attach_select_focuses_the_first_candidate_in_drawing_order() {
        let text = r#"{
            "next_id": 4,
            "shapes": [
                { "kind": "line", "id": 1, "at": { "x": 0, "y": 0 }, "len": 3, "orientation": "horizontal", "stroke": "light" },
                { "kind": "box", "id": 2, "at": { "x": 5, "y": 5 }, "size": { "width": 4, "height": 3 }, "stroke": "light" },
                { "kind": "connector", "id": 3,
                  "from": { "at": { "kind": "point", "x": 10, "y": 10 }, "leaving": "right", "terminal": { "kind": "arm" } },
                  "to": { "at": { "kind": "point", "x": 15, "y": 10 }, "leaving": "left", "terminal": { "kind": "arm" } },
                  "stroke": "light" }
            ]
        }"#;
        let mut state = a_diagram_over(text);
        let id1 = ShapeId::new(NonZeroU16::new(1).unwrap());
        let id3 = ShapeId::new(NonZeroU16::new(3).unwrap());

        state.select(Some(id3));
        state.set_sub_mode(Some(SubMode::Endpoint(End::From)));

        assert!(state.enter_attach_select());
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::AttachSelect {
                end: End::From,
                target: id1,
            })
        );

        // When there are no attachable candidates
        let text_no_candidates = r#"{
            "next_id": 2,
            "shapes": [
                { "kind": "connector", "id": 1,
                  "from": { "at": { "kind": "point", "x": 0, "y": 0 }, "leaving": "right", "terminal": { "kind": "arm" } },
                  "to": { "at": { "kind": "point", "x": 5, "y": 0 }, "leaving": "left", "terminal": { "kind": "arm" } },
                  "stroke": "light" }
            ]
        }"#;
        let mut state_none = a_diagram_over(text_no_candidates);
        state_none.select(Some(ShapeId::new(NonZeroU16::new(1).unwrap())));
        state_none.set_sub_mode(Some(SubMode::Endpoint(End::From)));
        assert!(!state_none.enter_attach_select());
        assert_eq!(state_none.sub_mode(), Some(SubMode::Endpoint(End::From)));
    }

    #[test]
    fn attach_select_tab_and_shift_tab_wrap_over_boxes_and_lines_only() {
        let text = r#"{
            "next_id": 5,
            "shapes": [
                { "kind": "box", "id": 1, "at": { "x": 0, "y": 0 }, "size": { "width": 4, "height": 3 }, "stroke": "light" },
                { "kind": "connector", "id": 2,
                  "from": { "at": { "kind": "point", "x": 5, "y": 0 }, "leaving": "right", "terminal": { "kind": "arm" } },
                  "to": { "at": { "kind": "point", "x": 10, "y": 0 }, "leaving": "left", "terminal": { "kind": "arm" } },
                  "stroke": "light" },
                { "kind": "line", "id": 3, "at": { "x": 15, "y": 0 }, "len": 4, "orientation": "horizontal", "stroke": "light" },
                { "kind": "box", "id": 4, "at": { "x": 20, "y": 0 }, "size": { "width": 3, "height": 3 }, "stroke": "light" }
            ]
        }"#;
        let mut state = a_diagram_over(text);
        let id1 = ShapeId::new(NonZeroU16::new(1).unwrap());
        let id2 = ShapeId::new(NonZeroU16::new(2).unwrap());
        let id3 = ShapeId::new(NonZeroU16::new(3).unwrap());
        let id4 = ShapeId::new(NonZeroU16::new(4).unwrap());

        state.select(Some(id2));
        state.set_sub_mode(Some(SubMode::AttachSelect {
            end: End::From,
            target: id1,
        }));

        // Forward wrap: 1 -> 3 -> 4 -> 1 (skipping connector 2)
        assert!(state.focus_next_attach_target());
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::AttachSelect {
                end: End::From,
                target: id3,
            })
        );
        assert!(state.focus_next_attach_target());
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::AttachSelect {
                end: End::From,
                target: id4,
            })
        );
        assert!(state.focus_next_attach_target());
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::AttachSelect {
                end: End::From,
                target: id1,
            })
        );

        // Backward wrap: 1 -> 4 -> 3 -> 1
        assert!(state.focus_previous_attach_target());
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::AttachSelect {
                end: End::From,
                target: id4,
            })
        );
        assert!(state.focus_previous_attach_target());
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::AttachSelect {
                end: End::From,
                target: id3,
            })
        );
        assert!(state.focus_previous_attach_target());
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::AttachSelect {
                end: End::From,
                target: id1,
            })
        );
    }

    #[test]
    fn attach_select_ignores_a_connector_or_empty_cell_as_a_target() {
        let text = r#"{
            "next_id": 3,
            "shapes": [
                { "kind": "box", "id": 1, "at": { "x": 0, "y": 0 }, "size": { "width": 4, "height": 3 }, "stroke": "light" },
                { "kind": "connector", "id": 2,
                  "from": { "at": { "kind": "point", "x": 5, "y": 0 }, "leaving": "right", "terminal": { "kind": "arm" } },
                  "to": { "at": { "kind": "point", "x": 10, "y": 0 }, "leaving": "left", "terminal": { "kind": "arm" } },
                  "stroke": "light" }
            ]
        }"#;
        let mut state = a_diagram_over(text);
        let id1 = ShapeId::new(NonZeroU16::new(1).unwrap());
        let id2 = ShapeId::new(NonZeroU16::new(2).unwrap());
        let non_existent = ShapeId::new(NonZeroU16::new(99).unwrap());

        state.select(Some(id2));
        state.set_sub_mode(Some(SubMode::AttachSelect {
            end: End::From,
            target: id1,
        }));

        // Ignored when target is a connector
        assert!(!state.focus_attach_target(id2));
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::AttachSelect {
                end: End::From,
                target: id1,
            })
        );

        // Ignored when target does not exist
        assert!(!state.focus_attach_target(non_existent));
        assert_eq!(
            state.sub_mode(),
            Some(SubMode::AttachSelect {
                end: End::From,
                target: id1,
            })
        );
    }

    #[test]
    fn attach_select_focus_does_not_touch_the_session_or_the_selection() {
        let text = r#"{
            "next_id": 4,
            "shapes": [
                { "kind": "box", "id": 1, "at": { "x": 0, "y": 0 }, "size": { "width": 4, "height": 3 }, "stroke": "light" },
                { "kind": "box", "id": 2, "at": { "x": 10, "y": 0 }, "size": { "width": 4, "height": 3 }, "stroke": "light" },
                { "kind": "connector", "id": 3,
                  "from": { "at": { "kind": "point", "x": 5, "y": 0 }, "leaving": "right", "terminal": { "kind": "arm" } },
                  "to": { "at": { "kind": "point", "x": 10, "y": 0 }, "leaving": "left", "terminal": { "kind": "arm" } },
                  "stroke": "light" }
            ]
        }"#;
        let mut state = a_diagram_over(text);
        let id3 = ShapeId::new(NonZeroU16::new(3).unwrap());

        state.select(Some(id3));
        state.set_sub_mode(Some(SubMode::Endpoint(End::From)));

        let initial_shapes = state.session().diagram().shape_count();
        let can_undo = state.can_undo();
        let selected = state.selected();

        assert!(state.enter_attach_select());
        assert!(state.focus_next_attach_target());
        assert!(state.focus_previous_attach_target());

        assert_eq!(state.session().diagram().shape_count(), initial_shapes);
        assert_eq!(state.can_undo(), can_undo);
        assert_eq!(state.selected(), selected);
    }

    #[test]
    fn attach_select_parent_is_endpoint() {
        let target = ShapeId::new(NonZeroU16::new(1).expect("1 is not zero"));
        assert_eq!(
            SubMode::AttachSelect {
                end: End::From,
                target,
            }
            .parent(),
            Some(SubMode::Endpoint(End::From))
        );
        assert_eq!(
            SubMode::AttachSelect {
                end: End::To,
                target,
            }
            .parent(),
            Some(SubMode::Endpoint(End::To))
        );
    }

    #[test]
    fn attach_select_ends_on_undo() {
        let mut state = a_diagram_over(THE_EXAMPLE);
        let center = state.viewport_center();
        let _conn_id = state.add_shape(Shape::Connector {
            from: Endpoint::absolute(
                center,
                Direction::Right,
                Terminal::Symbol {
                    style: SymbolStyleName::from("outline"),
                },
            ),
            to: Endpoint::absolute(
                Pos {
                    x: center.x + 5,
                    y: center.y + 5,
                },
                Direction::Left,
                Terminal::Symbol {
                    style: SymbolStyleName::from("solid"),
                },
            ),
            stroke: Stroke::from("light"),
        });

        assert!(state.can_undo());
        let target = ShapeId::new(NonZeroU16::new(1).unwrap());
        state.set_sub_mode(Some(SubMode::AttachSelect {
            end: End::From,
            target,
        }));

        state.undo();
        assert_eq!(state.selected(), None);
        assert_eq!(state.sub_mode(), None);
    }

    #[test]
    fn is_cell_selected_inverts_the_attach_target_and_the_selected_connector() {
        let text = r#"{
            "next_id": 4,
            "shapes": [
                { "kind": "connector", "id": 1,
                  "from": { "at": { "kind": "point", "x": 0, "y": 0 }, "leaving": "right", "terminal": { "kind": "arm" } },
                  "to": { "at": { "kind": "point", "x": 4, "y": 0 }, "leaving": "left", "terminal": { "kind": "arm" } },
                  "stroke": "light" },
                { "kind": "box", "id": 2, "at": { "x": 10, "y": 0 }, "size": { "width": 4, "height": 3 }, "stroke": "light" },
                { "kind": "line", "id": 3, "at": { "x": 20, "y": 0 }, "len": 3, "orientation": "horizontal", "stroke": "light" }
            ]
        }"#;
        let mut state = a_diagram_over(text);
        state.redraw(Rect::new(0, 0, 30, 10), Rect::new(0, 0, 30, 10));

        let id1 = ShapeId::new(NonZeroU16::new(1).unwrap());
        let id2 = ShapeId::new(NonZeroU16::new(2).unwrap());
        let id3 = ShapeId::new(NonZeroU16::new(3).unwrap());

        state.select(Some(id1));
        state.set_sub_mode(Some(SubMode::AttachSelect {
            end: End::From,
            target: id2,
        }));

        // The selected connector's cell is inverted
        assert!(state.is_cell_selected(Offset { x: 0, y: 0 }));
        assert_eq!(state.owner(Offset { x: 0, y: 0 }), Some(id1));

        // The focused attach target box's cell is inverted
        assert!(state.is_cell_selected(Offset { x: 10, y: 0 }));
        assert_eq!(state.owner(Offset { x: 10, y: 0 }), Some(id2));

        // Other shapes are NOT inverted
        assert!(!state.is_cell_selected(Offset { x: 20, y: 0 }));
        assert_eq!(state.owner(Offset { x: 20, y: 0 }), Some(id3));

        // Empty cells are NOT inverted
        assert!(!state.is_cell_selected(Offset { x: 28, y: 8 }));
        assert_eq!(state.owner(Offset { x: 28, y: 8 }), None);
    }

    #[test]
    fn duplicate_selected_clones_box_line_and_connector_properties_in_place() {
        let text = r#"{
            "next_id": 4,
            "shapes": [
                { "kind": "box", "id": 1, "at": { "x": 1, "y": 1 }, "size": { "width": 6, "height": 3 }, "stroke": "double", "fill": "░", "text": "API" },
                { "kind": "line", "id": 2, "at": { "x": 10, "y": 2 }, "len": 7, "orientation": "vertical", "stroke": "heavy" },
                { "kind": "connector", "id": 3,
                  "from": { "at": { "kind": "reference", "shape": 1, "anchor": "right", "offset": { "dx": 0, "dy": 0 }, "out": 0 }, "terminal": { "kind": "arm" } },
                  "to": { "at": { "kind": "point", "x": 10, "y": 3 }, "leaving": "left", "terminal": { "kind": "symbol", "style": "solid" } },
                  "stroke": "double" }
            ]
        }"#;
        let mut state = a_diagram_over(text);

        // When no shape is selected, duplicate_selected returns None.
        assert_eq!(state.selected(), None);
        assert_eq!(state.duplicate_selected(), None);

        let box_id = ShapeId::new(NonZeroU16::new(1).unwrap());
        let line_id = ShapeId::new(NonZeroU16::new(2).unwrap());
        let conn_id = ShapeId::new(NonZeroU16::new(3).unwrap());

        // 1. Duplicate the Box (#1 -> #4): exact properties preserved, placed at front, selected.
        state.select(Some(box_id));
        let original_box = state.selected_shape().unwrap().clone();
        let dup_box_id = state.duplicate_selected().expect("box duplicated");
        assert_eq!(dup_box_id, ShapeId::new(NonZeroU16::new(4).unwrap()));
        assert_eq!(state.selected(), Some(dup_box_id));
        assert_eq!(state.selected_shape(), Some(&original_box));
        assert_eq!(
            state.session.diagram().shapes().last().map(|(id, _)| id),
            Some(dup_box_id)
        );

        // Existing connector #3 still references original box #1, not duplicate #4.
        let (_, conn_shape) = state
            .session
            .diagram()
            .shapes()
            .find(|(id, _)| *id == conn_id)
            .expect("connector #3 exists");
        let Shape::Connector { from, .. } = conn_shape else {
            panic!("expected connector");
        };
        assert_eq!(
            from.at,
            monospace_diagram::EndpointAt::Reference(monospace_diagram::Reference {
                id: box_id,
                anchor: monospace_diagram::Anchor::Right,
                offset: monospace_diagram::Delta { dx: 0, dy: 0 },
                out: 0,
            })
        );

        // 2. Duplicate the Line (#2 -> #5): exact properties preserved, placed at front, selected.
        state.select(Some(line_id));
        let original_line = state.selected_shape().unwrap().clone();
        let dup_line_id = state.duplicate_selected().expect("line duplicated");
        assert_eq!(dup_line_id, ShapeId::new(NonZeroU16::new(5).unwrap()));
        assert_eq!(state.selected(), Some(dup_line_id));
        assert_eq!(state.selected_shape(), Some(&original_line));
        assert_eq!(
            state.session.diagram().shapes().last().map(|(id, _)| id),
            Some(dup_line_id)
        );

        // 3. Duplicate the Connector (#3 -> #6): retains Reference and Absolute endpoints and Terminals.
        state.select(Some(conn_id));
        let original_conn = state.selected_shape().unwrap().clone();
        let dup_conn_id = state.duplicate_selected().expect("connector duplicated");
        assert_eq!(dup_conn_id, ShapeId::new(NonZeroU16::new(6).unwrap()));
        assert_eq!(state.selected(), Some(dup_conn_id));
        assert_eq!(state.selected_shape(), Some(&original_conn));
        assert_eq!(
            state.session.diagram().shapes().last().map(|(id, _)| id),
            Some(dup_conn_id)
        );
    }
}