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
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
//! Tool box widget.
use crate::core::{Color, HorizontalAlignment, ObjectId, Orientation, Point, Rect};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::Signal1;
use crate::widget::metrics::effective_font;
use crate::widget::capability::coercion::{
expect_orientation, expect_string, expect_usize, orientation_to_str,
};
use crate::widget::capability::properties_trait::{base_property_get, base_property_set};
use crate::widget::capability::types::{CapabilityAccessError, CapabilityValue};
use crate::widget::capability::WidgetProperties;
#[cfg(feature = "image")]
use crate::widget::Image;
use crate::widget::{
BaseWidget, Draw, IconItemError, IconSource, SimpleRegistry, Widget, WidgetKind,
};
use crate::{impl_widget_property_hooks, property_names_of};
use std::cell::RefCell;
use std::rc::Rc;
use super::theme_snapshot::ThemeSnapshot;
/// The height of one tab in a vertical tool box, in logical pixels.
const ITEM_HEIGHT: u32 = 32;
/// The width of one tab in a horizontal tool box, in logical pixels.
const ITEM_WIDTH: u32 = 120;
/// The smallest page the content area is allowed to keep.
///
/// # Why the page cannot simply be what is left over
///
/// `content_rect` used to be `rect.height - item_height * items.len()`, floored at zero. At the
/// census size (120 px) with four items that is `120 - 128 = 0`: every item's tab was drawn and
/// the page — the whole point of a tool box — had no pixels at all, so the control rendered as a
/// list of tabs beside nothing. The reference tool box gives the page the remaining body and keeps at
/// least a line of it; this constant is that rule, in the same shape as `meter`'s reserved
/// reading band and `splitter`'s minimum pane.
const MIN_CONTENT_EXTENT: u32 = 24;
/// Tool box widget.
///
/// # Overflow
///
/// The tab strip and the page share one axis, so they cannot both have the room they would like.
/// The page keeps `MIN_CONTENT_EXTENT` and the *strip* gives way: when the tabs no longer fit,
/// the strip is scrolled by whole tabs (see [`ToolBox::scroll_offset`]) rather than letting an
/// item be placed past the control's edge. An item is therefore either drawn whole, inside the
/// strip, or not drawn at all.
pub struct ToolBox {
base: BaseWidget,
items: Vec<ToolBoxItem>,
current_index: usize,
orientation: Orientation,
/// Index of the first tab drawn, i.e. the tab strip's scroll offset in whole tabs.
///
/// Kept in `usize` rather than as a pixel offset because a tab is drawn as a whole unit: a
/// partial tab at the top of the strip would be a tab whose label is cut in half, and the
/// overflow rule above is deliberately "whole tabs, or none".
scroll_offset: usize,
/// The smooth-scroll position in tab units while a selection change animates.
scroll_render: f32,
/// Whether selection changes animate the strip's scroll.
smooth_scroll: bool,
/// Height of one vertical tab. Defaults to [`ITEM_HEIGHT`].
item_height: u32,
/// Emitted with the new page index whenever the current page changes through
/// [`ToolBox::set_current_index`] or user input. `clear()` resets the index to `0`
/// without emitting, because it also empties the page vector.
///
/// This is the *selection* signal. The per-item open state is reported separately by
/// [`Self::item_expanded_changed`], because an item can open or close without the selection
/// moving (`ToolBoxMode::NonExclusive`).
pub current_changed: Signal1<usize>,
/// Emitted with `(item_index, expanded)` whenever an item's page opens or closes.
pub item_expanded_changed: Signal1<(usize, bool)>,
/// How items' open states interact.
mode: ToolBoxMode,
/// Optional shared registry for child widget forwarding.
registry: Option<Rc<RefCell<SimpleRegistry>>>,
/// The item under the pointer, for the tooltip query (C-28).
hovered_item: Option<usize>,
}
/// How a tool box treats its items' open state.
///
/// # Why the modes exist
///
/// A tool box page used to have exactly one state: whichever item `current_index` named was shown.
/// The reference control offers three policies, and they differ in *what closing one page does to
/// the others*, which is a decision only the host can make.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ToolBoxMode {
/// At most one item is open. Opening another collapses the one that was open.
#[default]
Exclusive,
/// Any number of items may be open at once; their pages share the content box.
NonExclusive,
/// At most one item is open, and a press on the open item collapses it — the page behaves like
/// a popup that the user dismisses by pressing its own tab again.
Popup,
}
impl ToolBoxMode {
/// The factory spelling of this mode, matching `TOOL_BOX_PROPERTIES`.
pub fn as_str(self) -> &'static str {
match self {
ToolBoxMode::Exclusive => "exclusive",
ToolBoxMode::NonExclusive => "non_exclusive",
ToolBoxMode::Popup => "popup",
}
}
/// Parses a factory spelling into a mode.
pub fn from_name(name: &str) -> Option<Self> {
Some(match name {
"exclusive" => ToolBoxMode::Exclusive,
"non_exclusive" | "nonexclusive" => ToolBoxMode::NonExclusive,
"popup" => ToolBoxMode::Popup,
_ => return None,
})
}
}
/// Tool box item.
pub struct ToolBoxItem {
text: String,
icon_source: Option<IconSource>,
#[cfg(feature = "image")]
icon: Option<Image>,
tooltip: String,
enabled: bool,
widget: Option<ObjectId>,
/// Whether this item's page is open.
///
/// The flag is the item's own, not derived from `current_index`: in
/// [`ToolBoxMode::NonExclusive`] several items are open at once, so "which page is visible" is a
/// per-item answer. [`ToolBox::set_item_expanded`] is the entry that keeps the pages, the
/// geometry and the signal in step.
expanded: bool,
}
impl ToolBoxItem {
/// Creates a new tool box item.
pub fn new(text: String) -> Self {
Self {
text,
icon_source: None,
#[cfg(feature = "image")]
icon: None,
tooltip: String::new(),
enabled: true,
widget: None,
expanded: false,
}
}
/// Returns text.
pub fn text(&self) -> &str {
&self.text
}
/// Sets text.
///
/// This is the value-level setter only: it cannot request a repaint. A `ToolBoxItem` held by a
/// control should be changed through [`ToolBox::set_item_text`].
pub fn set_text(&mut self, text: impl Into<String>) {
self.text = text.into();
}
/// Returns the typed icon source, if one is assigned.
pub fn icon_source(&self) -> Option<IconSource> {
self.icon_source
}
/// Replaces the typed icon source and clears the legacy image slot.
///
/// An explicit `None` clears **both** forms: leaving the image behind would keep the item's icon
/// slot reserved and painted for an icon the caller just cleared.
pub fn set_icon_source(&mut self, source: Option<IconSource>) {
self.icon_source = source;
#[cfg(feature = "image")]
{
self.icon = None;
}
}
#[cfg(feature = "image")]
/// Returns icon.
pub fn icon(&self) -> Option<&Image> {
self.icon.as_ref()
}
#[cfg(feature = "image")]
/// Sets icon.
///
/// A non-RGBA8 image is normalised once here, so the tile can borrow its bytes instead of
/// transcoding every frame.
pub fn set_icon(&mut self, icon: Option<Image>) {
self.icon = icon.map(crate::widget::icon_source::normalize_rgba8_image);
self.icon_source = None;
}
/// Whether this item reserves an icon tile.
pub fn has_icon(&self) -> bool {
if self.icon_source.is_some() {
return true;
}
#[cfg(feature = "image")]
{
self.icon.is_some()
}
#[cfg(not(feature = "image"))]
{
false
}
}
/// Returns tooltip.
pub fn tooltip(&self) -> &str {
&self.tooltip
}
/// Sets tooltip.
pub fn set_tooltip(&mut self, tooltip: String) {
self.tooltip = tooltip;
}
/// Returns whether item is enabled.
pub fn is_enabled(&self) -> bool {
self.enabled
}
/// Sets enabled state.
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
}
/// Returns widget.
pub fn widget(&self) -> Option<ObjectId> {
self.widget
}
/// Sets widget.
pub fn set_widget(&mut self, widget: Option<ObjectId>) {
self.widget = widget;
}
/// Whether this item's page is open.
pub fn is_expanded(&self) -> bool {
self.expanded
}
/// Sets whether this item's page is open.
///
/// The value-level setter only: it cannot show or hide the page, place it, or tell anyone.
/// Use [`ToolBox::set_item_expanded`] for the container-level entry that does all three.
pub fn set_expanded(&mut self, expanded: bool) {
self.expanded = expanded;
}
}
impl ToolBox {
/// Creates a tool box.
pub fn new(geometry: Rect) -> Self {
Self {
base: BaseWidget::new(WidgetKind::Toolbox, geometry, "ToolBox"),
items: Vec::new(),
current_index: 0,
orientation: Orientation::Vertical,
scroll_offset: 0,
scroll_render: 0.0,
smooth_scroll: false,
item_height: ITEM_HEIGHT,
current_changed: Signal1::new(),
item_expanded_changed: Signal1::new(),
mode: ToolBoxMode::Exclusive,
registry: None,
hovered_item: None,
}
}
/// Adds an item.
///
/// The first item added to an empty tool box is open, which is the state the control has always
/// started in: an empty strip beside an empty page becomes a strip beside a page.
pub fn add_item(&mut self, text: String, widget: Option<ObjectId>) -> usize {
let was_empty = self.items.is_empty();
let mut item = ToolBoxItem::new(text);
item.widget = widget;
item.expanded = was_empty;
if let Some(widget_id) = widget {
self.base.add_child_linked(widget_id);
}
self.items.push(item);
self.clamp_scroll();
self.sync_pages();
self.items.len().saturating_sub(1)
}
/// Inserts an item at position.
/// Inserts an item before the given index.
///
/// An `index` past the end appends rather than panicking, matching
/// [`Self::add_item`] and the rest of the container family (`tab_bar`,
/// `pie_menu` clamp the same way). The inserted item starts collapsed unless it is the first
/// item of an empty tool box (see [`Self::add_item`]).
pub fn insert_item(&mut self, index: usize, text: String, widget: Option<ObjectId>) {
let was_empty = self.items.is_empty();
let index = index.min(self.items.len());
let mut item = ToolBoxItem::new(text);
item.widget = widget;
item.expanded = was_empty;
if let Some(widget_id) = widget {
self.base.add_child_linked(widget_id);
}
self.items.insert(index, item);
if !was_empty && self.current_index >= index {
self.current_index += 1;
}
self.clamp_scroll();
self.sync_pages();
}
/// Removes an item.
pub fn remove_item(&mut self, index: usize) {
if index < self.items.len() {
if let Some(widget_id) = self.items[index].widget {
self.base.remove_child_linked(widget_id);
// See `StackedWidget::remove_widget`: a removed page must not stay visible.
crate::set_widget_visible(widget_id, false);
}
let was_open = self.items[index].expanded;
self.items.remove(index);
if self.current_index >= index && self.current_index > 0 {
self.current_index -= 1;
}
if self.items.is_empty() {
self.current_index = 0;
} else if !self.items.iter().any(|item| item.expanded) {
// The visible page was the one removed. The control has always kept a page on
// screen, so the nearest slot takes over rather than leaving an empty content box.
let next = self.current_index.min(self.items.len() - 1);
self.items[next].expanded = true;
self.current_index = next;
if was_open {
self.item_expanded_changed.emit((next, true));
}
}
self.clamp_scroll();
self.sync_pages();
}
}
/// Returns number of items.
pub fn count(&self) -> usize {
self.items.len()
}
/// Returns the selected item index, or `None` when the toolbox is empty.
pub fn selected_index(&self) -> Option<usize> {
self.items.get(self.current_index).map(|_| self.current_index)
}
/// Returns current item index.
#[deprecated(note = "use `selected_index` (Option) — WDGT-13")]
pub fn current_index(&self) -> usize {
self.current_index
}
/// Sets current item index.
///
/// The new page's tab is scrolled into view if overflow had hidden it; the selection is not
/// silently applied to a page the user cannot see the tab of.
///
/// Selecting also opens the page: in [`ToolBoxMode::Exclusive`] and [`ToolBoxMode::Popup`] every
/// other item closes, and in [`ToolBoxMode::NonExclusive`] the others keep their state. A
/// selection change is reported through [`Self::current_changed`], and an open state that
/// moved through [`Self::item_expanded_changed`].
pub fn set_current_index(&mut self, index: usize) {
if index < self.items.len() && self.current_index != index {
self.current_index = index;
self.scroll_current_into_view();
self.open_exclusive(index);
self.sync_pages();
self.current_changed.emit(index);
self.base.request_redraw();
}
}
/// The policy the items' open states follow.
pub fn mode(&self) -> ToolBoxMode {
self.mode
}
/// Sets the open-state policy.
///
/// Switching to a single-page mode closes every page but the selected one, so the policy is
/// true after the write rather than only for the next press. Switching to
/// [`ToolBoxMode::NonExclusive`] changes nothing: whatever is open stays open.
pub fn set_mode(&mut self, mode: ToolBoxMode) {
if self.mode == mode {
return;
}
self.mode = mode;
if !matches!(mode, ToolBoxMode::NonExclusive) {
let keep = self.current_index.min(self.items.len().saturating_sub(1));
self.open_exclusive(keep);
}
self.sync_pages();
self.base.request_redraw();
}
/// Whether the item at `index` has an open page, or `None` for an unknown index.
pub fn item_expanded(&self, index: usize) -> Option<bool> {
self.items.get(index).map(|item| item.expanded)
}
/// The indices of the open items, in display order.
pub fn expanded_items(&self) -> Vec<usize> {
self.items.iter().enumerate().filter(|(_, item)| item.expanded).map(|(i, _)| i).collect()
}
/// Opens or closes the item at `index`, returning `false` for an unknown index.
///
/// # How the modes read this
///
/// * [`ToolBoxMode::Exclusive`] — opening one item closes the rest; closing the open item is
/// allowed and leaves every page closed.
/// * [`ToolBoxMode::NonExclusive`] — opening and closing are independent; several pages may be
/// open and they share the content box.
/// * [`ToolBoxMode::Popup`] — opening one item closes the rest, and closing the open item is the
/// gesture that dismisses the popup.
///
/// The page's visibility and rectangle are applied from the item flag
/// (`Self::sync_pages`), and a real change is announced through
/// [`Self::item_expanded_changed`]; a repeated write is a no-op.
pub fn set_item_expanded(&mut self, index: usize, expanded: bool) -> bool {
if index >= self.items.len() {
return false;
}
if self.items[index].expanded == expanded {
return true;
}
if expanded {
let previous = self.current_index;
self.current_index = index;
self.open_exclusive(index);
self.items[index].expanded = expanded;
if previous != index {
self.current_changed.emit(index);
}
} else {
self.items[index].expanded = false;
}
self.scroll_current_into_view();
self.sync_pages();
self.base.request_redraw();
self.item_expanded_changed.emit((index, expanded));
true
}
/// Opens `keep` and closes every other item in the modes that allow one page only.
fn open_exclusive(&mut self, keep: usize) {
if matches!(self.mode, ToolBoxMode::NonExclusive) {
if let Some(item) = self.items.get_mut(keep) {
item.expanded = true;
}
return;
}
for (index, item) in self.items.iter_mut().enumerate() {
item.expanded = index == keep;
}
}
/// Applies the page model to the mounted pages: every open item's page is shown and fills its
/// share of [`Self::content_rect`], every other page is hidden.
///
/// # Why this is the control's job
///
/// A tool box shows the pages of its *open* items beside its strip. Linking a page as a child
/// was only half the model: the tree painter draws every visible child, so the closed pages
/// painted over the open one (and over the strip). Hiding them is also what keeps a click from
/// reaching a page the user cannot see.
fn sync_pages(&self) {
#[cfg(not(alloc_frugal))]
{
for item in self.items.iter() {
if let Some(widget_id) = item.widget {
crate::set_widget_visible(widget_id, item.expanded);
}
}
self.place_pages();
}
}
/// The rectangles the open pages occupy inside the content box.
///
/// One open page fills the box, which is the only state the single-page modes produce. In
/// [`ToolBoxMode::NonExclusive`] several pages are open at once and they tile the box along its
/// long axis, each taking an equal share — the same "share the band" rule the strip uses.
fn page_rects(&self, content: Rect) -> Vec<(usize, Rect)> {
if self.mode == ToolBoxMode::Popup {
// C-29: the open page is presented in a floating layer beside its trigger, not in the
// content band. `popup_rect` is the single derivation the painter, the hit test and
// the child placement share.
return match self.popup_rect(content) {
Some(rect) => self
.items
.iter()
.position(|item| item.expanded)
.map(|index| crate::compat::vec![(index, rect)])
.unwrap_or_default(),
None => Vec::new(),
};
}
let open: Vec<usize> = self
.items
.iter()
.enumerate()
.filter(|(_, item)| item.expanded)
.map(|(index, _)| index)
.collect();
if open.is_empty() {
return Vec::new();
}
let count = open.len() as u32;
let share = content.height / count;
open.into_iter()
.enumerate()
.map(|(slot, index)| {
let offset = slot as u32 * share;
let height = if slot as u32 + 1 == count {
content.height.saturating_sub(offset)
} else {
share
};
(
index,
Rect::new(
content.x,
content.y.saturating_add_unsigned(offset),
content.width,
height,
),
)
})
.collect()
}
/// The floating rectangle a [`ToolBoxMode::Popup`] page occupies (C-29).
///
/// Anchored to the open item's own tab and placed just beyond it along the strip's short
/// axis, then clamped inside the content box so it can never leave the control. `None` when no
/// item is open (there is nothing to present) or the mode is not `Popup`.
fn popup_rect(&self, content: Rect) -> Option<Rect> {
if self.mode != ToolBoxMode::Popup {
return None;
}
let open = self.items.iter().position(|item| item.expanded)?;
let anchor = self.item_rect(open)?;
const GAP: i32 = 4;
const POPUP_MAJOR: u32 = 240;
const POPUP_MINOR: u32 = 180;
match self.orientation {
Orientation::Vertical => {
let height = content.height.min(POPUP_MINOR);
let max_y = content.y + content.height.saturating_sub(height) as i32;
let y =
(anchor.y + anchor.height as i32 + GAP).clamp(content.y, max_y.max(content.y));
Some(Rect::new(content.x, y, content.width, height))
}
Orientation::Horizontal => {
let width = content.width.min(POPUP_MAJOR);
let max_x = content.x + content.width.saturating_sub(width) as i32;
let x =
(anchor.x + anchor.width as i32 + GAP).clamp(content.x, max_x.max(content.x));
Some(Rect::new(x, content.y, width, content.height))
}
}
}
/// The item under the pointer, for the tooltip path (C-28).
pub fn hovered_item(&self) -> Option<usize> {
self.hovered_item
}
/// The tooltip of the item under the pointer, if it has one (C-28).
///
/// The text was stored and there was no pointer path or query, so a host could not reach it.
/// An empty tooltip reports `None` rather than `Some("")`, which is the difference between
/// "no tooltip" and "a tooltip that renders nothing".
pub fn hovered_tooltip(&self) -> Option<&str> {
let index = self.hovered_item?;
self.items.get(index).map(|item| item.tooltip.as_str()).filter(|text| !text.is_empty())
}
/// Places the open pages in the content box.
///
/// The box comes from the same `content_rect` the painter uses, so a page and the area drawn
/// around it cannot disagree about where the page is.
fn place_pages(&self) {
#[cfg(not(alloc_frugal))]
{
// One content derivation for every page in this pass (C-P10).
let content = self.content_rect();
for (index, rect) in self.page_rects(content) {
if let Some(widget_id) = self.items[index].widget {
crate::set_widget_geometry(widget_id, rect.x, rect.y, rect.width, rect.height);
}
}
}
}
/// Returns current item widget.
pub fn current_widget(&self) -> Option<ObjectId> {
self.items.get(self.current_index).and_then(|item| item.widget)
}
/// Returns item at index.
pub fn item(&self, index: usize) -> Option<&ToolBoxItem> {
self.items.get(index)
}
/// Returns mutable item at index.
///
/// A mutation made through this reference is **not** followed by a repaint, because the item
/// has no back-reference to the control that paints it. Use [`Self::set_item_text`] or
/// [`Self::set_item_icon_source`] when the change must be visible.
pub fn item_mut(&mut self, index: usize) -> Option<&mut ToolBoxItem> {
self.items.get_mut(index)
}
/// Replaces an item's text and repaints the control.
///
/// # Why this exists
///
/// `ToolBoxItem::set_text` mutates a value that the strip paints, but the item cannot ask the
/// control for a repaint — so a text change made through [`Self::item_mut`] stayed invisible
/// until some unrelated repaint happened. This entry point closes the loop and short-circuits
/// an equal value, so setting the same text does not invalidate a frame.
///
/// Returns whether the text changed.
pub fn set_item_text(&mut self, index: usize, text: impl Into<String>) -> bool {
let text = text.into();
let Some(item) = self.items.get_mut(index) else {
return false;
};
if item.text == text {
return false;
}
item.text = text;
self.base.request_redraw();
true
}
/// Assigns a typed icon source to an existing item index.
pub fn set_item_icon_source(
&mut self,
index: usize,
source: Option<IconSource>,
) -> Result<(), IconItemError> {
let len = self.items.len();
let Some(item) = self.items.get_mut(index) else {
return Err(IconItemError::IndexOutOfBounds { index, len });
};
item.set_icon_source(source);
self.base.request_redraw();
Ok(())
}
/// Returns orientation.
pub fn orientation(&self) -> Orientation {
self.orientation
}
/// Sets orientation.
pub fn set_orientation(&mut self, orientation: Orientation) {
if self.orientation == orientation {
return;
}
self.orientation = orientation;
// The other axis has a different extent and a different tab size, so an offset that was
// legal in one is not necessarily legal in the other.
self.clamp_scroll();
self.place_pages();
self.base.request_redraw();
}
/// Removes all items from the toolbox.
pub fn clear(&mut self) {
for item in self.items.drain(..) {
if let Some(widget_id) = item.widget {
self.base.remove_child_linked(widget_id);
crate::set_widget_visible(widget_id, false);
}
}
self.current_index = 0;
self.scroll_offset = 0;
self.scroll_render = 0.0;
self.base.request_redraw();
}
/// The extent one tab occupies along the strip's axis.
fn item_extent(&self) -> u32 {
match self.orientation {
Orientation::Vertical => self.item_height.max(1),
Orientation::Horizontal => ITEM_WIDTH,
}
}
/// The height of one vertical tab.
pub fn item_height(&self) -> u32 {
self.item_height
}
/// Sets the height of one vertical tab.
///
/// Returns nothing because every positive value is valid; zero is clamped to one. The
/// value is the strip's own metric, not a content measurement, and it defaults to
/// `ITEM_HEIGHT` so an existing toolbox keeps its pixels.
pub fn set_item_height(&mut self, height: u32) {
let height = height.max(1);
if self.item_height == height {
return;
}
self.item_height = height;
self.clamp_scroll();
self.base.request_layout();
self.base.request_redraw();
}
/// Whether scroll changes animate.
pub fn smooth_scroll(&self) -> bool {
self.smooth_scroll
}
/// Enables or disables the scroll tween.
///
/// Off by default, so a selection still snaps exactly as it did. When on, the strip's
/// scroll position eases toward the selected tab over subsequent [`Widget::tick`] frames.
pub fn set_smooth_scroll(&mut self, smooth: bool) {
if self.smooth_scroll == smooth {
return;
}
self.smooth_scroll = smooth;
if !smooth {
self.scroll_render = self.scroll_offset as f32;
}
self.base.request_redraw();
}
/// How much room the tab strip has, along its own axis.
fn strip_extent(&self) -> u32 {
let rect = self.geometry();
match self.orientation {
Orientation::Vertical => rect.height,
Orientation::Horizontal => rect.width,
}
}
/// The largest value [`Self::scroll_offset`] may take. Zero when every tab fits.
///
/// The strip is never allowed to spend the page's own `MIN_CONTENT_EXTENT`: that is what
/// makes "the page keeps at least one line" true at *any* tab count, including the case
/// where the tabs alone are taller than the whole control.
pub fn max_scroll(&self) -> usize {
let extent = self.strip_extent();
let visible =
(extent.saturating_sub(MIN_CONTENT_EXTENT) / self.item_extent().max(1)) as usize;
self.items.len().saturating_sub(visible)
}
/// Returns the tab strip's scroll offset, in whole tabs.
pub fn scroll_offset(&self) -> usize {
self.scroll_offset
}
/// Scrolls the tab strip to `offset`, clamped to [`Self::max_scroll`].
///
/// The clamp is what makes this a total function for a caller: an offset past the end simply
/// shows the last full strip, which is what a scroll-into-view means. Requests a redraw only
/// when the offset actually changes.
pub fn set_scroll_offset(&mut self, offset: usize) {
let clamped = offset.min(self.max_scroll());
if self.scroll_offset != clamped {
self.scroll_offset = clamped;
if !self.smooth_scroll {
self.scroll_render = clamped as f32;
}
self.base.request_redraw();
}
}
/// Re-clamps the scroll offset after the item list or the geometry changed.
///
/// # Why this is needed separately from the setter
///
/// Every other mutator on this control can shrink the strip — removing an item, clearing the
/// list, resizing the control — and a stale offset would then scroll a strip that fits,
/// which shows *fewer* tabs than there is room for. Every one of those paths ends here rather
/// than each remembering the clamp, because a rule that lives in one place cannot be applied
/// in three of four.
fn clamp_scroll(&mut self) {
let max = self.max_scroll();
if self.scroll_offset > max {
self.scroll_offset = max;
}
}
/// Keeps the page's own tab inside the visible strip, scrolling it into view when it is not.
///
/// A selection made by keyboard or by the property contract can name a tab that overflow has
/// scrolled off, and a page whose tab cannot be seen is a page the user cannot tell is open.
fn scroll_current_into_view(&mut self) {
let extent = self.strip_extent();
let visible =
(extent.saturating_sub(MIN_CONTENT_EXTENT) / self.item_extent().max(1)) as usize;
if visible == 0 {
// No tab fits alongside the page's own minimum, so there is nothing to scroll to;
// scrolling would only hide a tab to show another.
return;
}
if self.current_index < self.scroll_offset {
self.scroll_offset = self.current_index;
} else if self.current_index >= self.scroll_offset + visible {
self.scroll_offset = self.current_index + 1 - visible;
}
self.clamp_scroll();
}
/// Returns item rectangle at index.
///
/// # Why the result is intersected with the strip
///
/// The index is offset by the strip's scroll position and the rectangle is then clipped to
/// the strip, so an item that overflow has pushed past the strip's end yields a *partial*
/// rectangle — up to and including an empty one — rather than a rectangle outside the
/// control. That is the invariant the draw path and hit testing both rely on: an item either
/// has pixels inside the strip or is reported as having none. Previously the position was
/// `item_extent * index` with no reference to the control's extent at all, so item 5 of 6 in a
/// 120 px control was painted from y = 160 downward, entirely outside the control.
fn item_rect(&self, index: usize) -> Option<Rect> {
if index >= self.items.len() {
return None;
}
let rect = self.geometry();
let extent = self.item_extent();
// Item positions are relative to the rendered scroll fraction, which equals the
// integer offset when smoothing is off. A negative relative slot is fully hidden.
let relative = index as f32
- if self.smooth_scroll { self.scroll_render } else { self.scroll_offset as f32 };
if relative < 0.0 {
return None;
}
let position = (relative * extent as f32).round() as u32;
match self.orientation {
Orientation::Horizontal => {
let strip = Rect::new(rect.x, rect.y, rect.width, rect.height);
let full = Rect::new(
rect.x.saturating_add_unsigned(position),
rect.y,
ITEM_WIDTH,
rect.height,
);
full.intersection(&strip)
}
Orientation::Vertical => {
let strip = Rect::new(rect.x, rect.y, rect.width, rect.height);
let full = Rect::new(
rect.x,
rect.y.saturating_add_unsigned(position),
rect.width,
self.item_height,
);
full.intersection(&strip)
}
}
}
/// Returns content rectangle.
///
/// The page starts where the *visible* strip ends — not where an unbounded strip would have
/// ended — so the page cannot be pushed off the control by tabs that overflow. It keeps at
/// least [`MIN_CONTENT_EXTENT`], which is what stops a tab list from reducing the page to
/// zero height; the tabs, not the page, are what give way when both cannot fit.
///
/// The scrolling has an important consequence worth stating: because at most
/// `(extent - MIN_CONTENT_EXTENT) / item_extent` tabs are visible at once, the strip's end and
/// the page's start are always inside the control, and their sum can never exceed it.
fn content_rect(&self) -> Rect {
let rect = self.geometry();
let extent = self.item_extent();
// How many whole tabs the strip can show beside the reserved page.
let capacity = self.strip_extent().saturating_sub(MIN_CONTENT_EXTENT) / extent.max(1);
// The strip only occupies as much as it **has** tabs, capped by that capacity. Using
// the capacity alone reserved room for tabs that do not exist: with two items in a
// 300px vertical box the page started at `8 * 32 = 256`, so 192px of the control
// was empty strip and the page was a 44px sliver at the bottom.
let strip_used = (self.items.len() as u32).min(capacity) * extent;
match self.orientation {
Orientation::Horizontal => {
let remaining = rect.width.saturating_sub(strip_used);
let content_width = if self.items.len() as u32 <= capacity {
remaining.max(MIN_CONTENT_EXTENT).min(rect.width)
} else {
remaining
};
Rect::new(
rect.x.saturating_add_unsigned(strip_used),
rect.y,
content_width,
rect.height,
)
}
Orientation::Vertical => {
let remaining = rect.height.saturating_sub(strip_used);
let content_height = if self.items.len() as u32 <= capacity {
remaining.max(MIN_CONTENT_EXTENT).min(rect.height)
} else {
remaining
};
Rect::new(
rect.x,
rect.y.saturating_add_unsigned(strip_used),
rect.width,
content_height,
)
}
}
}
/// Returns index of item at position.
fn item_at_position(&self, pos: Point) -> Option<usize> {
// C-P10: the strip is a uniform run, so the slot is solved arithmetically and only the
// candidate is confirmed against the painted rectangle. A mismatch falls back to the
// neighbouring slots and then to the scan, so the shortcut can only make it faster.
let rect = self.geometry();
let coordinate = match self.orientation {
Orientation::Horizontal => pos.x - rect.x,
Orientation::Vertical => pos.y - rect.y,
};
if coordinate < 0 {
return None;
}
let extent = self.item_extent().max(1) as f32;
let scroll =
if self.smooth_scroll { self.scroll_render } else { self.scroll_offset as f32 };
let slot = (coordinate as f32 / extent + scroll).floor();
if slot >= 0.0 {
let index = slot as usize;
if self.item_rect(index).is_some_and(|item_rect| item_rect.contains(pos)) {
return Some(index);
}
for neighbour in [index.saturating_sub(1), index + 1] {
if self.item_rect(neighbour).is_some_and(|item_rect| item_rect.contains(pos)) {
return Some(neighbour);
}
}
}
for i in 0..self.items.len() {
if let Some(item_rect) = self.item_rect(i) {
if item_rect.contains(pos) {
return Some(i);
}
}
}
None
}
}
// Implement Widget trait
impl Widget for ToolBox {
fn base(&self) -> &BaseWidget {
&self.base
}
fn set_state_theme_hook(&mut self) {
crate::style::reapply_active_theme_state(self);
}
fn base_mut(&mut self) -> &mut BaseWidget {
&mut self.base
}
fn size_hint(&self) -> crate::core::Size {
crate::core::Size::new(200, 250)
}
/// Moves the tool box and re-places the visible pages.
///
/// The content box is a function of the control's rectangle (and of the orientation),
/// so a layout that moves the tool box has to move the pages with it.
fn set_geometry(&mut self, geometry: Rect) {
self.base.set_geometry(geometry);
self.place_pages();
}
/// Eases the strip toward the selected tab when smooth scrolling is on.
fn tick(&mut self, delta_ms: u32) -> bool {
if !self.smooth_scroll {
return false;
}
let target = self.scroll_offset as f32;
let delta = target - self.scroll_render;
if delta.abs() <= 0.01 {
self.scroll_render = target;
return false;
}
let step = (delta_ms as f32 / 120.0).clamp(0.0, 1.0);
self.scroll_render += delta * step;
if (target - self.scroll_render).abs() <= 0.01 {
self.scroll_render = target;
self.base.request_redraw();
return false;
}
self.base.request_redraw();
true
}
fn is_animating(&self) -> bool {
self.smooth_scroll && (self.scroll_render - self.scroll_offset as f32).abs() > 0.01
}
impl_draw_bridge!();
impl_widget_property_hooks!();
/// Resolves a published event name to the control's own signal.
///
/// # Why this is explicit
///
/// `connect_event` validates a name against the capability table and registers a hub slot; only
/// `event_signal_dyn` joins that name to the signal the control actually **emits**. Without an
/// arm a published name is valid and inert, which is the silent failure
/// `tools/check_event_signal_dyn.sh` exists to make impossible: the arm set is compared against
/// the capability's published names, so the two cannot drift.
fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
use crate::signal::EventSignalRef;
match name {
"current_changed" => {
Some(EventSignalRef::mapped("current_changed", &self.current_changed, |v| {
CapabilityValue::UInt(*v as u64)
}))
}
"item_expanded_changed" => Some(EventSignalRef::mapped(
"item_expanded_changed",
&self.item_expanded_changed,
|v| {
CapabilityValue::Tuple(crate::compat::Vec::from([
CapabilityValue::UInt(v.0 as u64),
CapabilityValue::Bool(v.1),
]))
},
)),
_ => None,
}
}
}
/// `ToolBox`'s property contract.
impl WidgetProperties for ToolBox {
fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
match name {
"item_count" => Ok(CapabilityValue::UInt(self.count() as u64)),
"current_index" => Ok(CapabilityValue::UInt(self.selected_index().unwrap_or(0) as u64)),
"orientation" => {
Ok(CapabilityValue::String(orientation_to_str(self.orientation()).to_string()))
}
"mode" => Ok(CapabilityValue::String(self.mode().as_str().to_string())),
_ => base_property_get(self, name),
}
}
fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
match name {
"current_index" => {
self.set_current_index(expect_usize(value)?);
Ok(())
}
"orientation" => {
self.set_orientation(expect_orientation(value)?);
Ok(())
}
"mode" => {
let token = expect_string(value)?;
let mode =
ToolBoxMode::from_name(&token).ok_or(CapabilityAccessError::OutOfRange)?;
self.set_mode(mode);
Ok(())
}
// Derived from the item list.
"item_count" => Err(CapabilityAccessError::ReadOnlyProperty),
_ => base_property_set(self, name, value),
}
}
fn property_names(&self) -> &'static [&'static str] {
// Mirrors `TOOL_BOX_PROPERTIES`.
property_names_of![
"item_count",
"current_index",
"orientation",
"mode",
BASE_PROPERTY_NAMES
]
}
/// Runs one of the commands `tool_box` publishes.
///
/// `add_item` takes the item's label and `remove_item` takes the index to
/// remove, so neither can complete without a payload: they are refused as
/// [`CapabilityAccessError::OutOfRange`], meaning the name is valid and the
/// argument is what is missing.
fn command(&mut self, name: &str) -> Result<(), CapabilityAccessError> {
match name {
"add_item" | "remove_item" => Err(CapabilityAccessError::OutOfRange),
// Any other `set_foo` name carries its value through the property route,
// so the shared default reports that a payload is needed rather than
// claiming the control has never heard of it.
_ if name.starts_with("set_") => Err(CapabilityAccessError::OutOfRange),
_ => Err(CapabilityAccessError::UnknownCommand),
}
}
}
impl ToolBox {
/// The tint for an item's embedded icon tile, derived from the resolved style.
///
/// # Why this is a named function (issue §9 item #7)
///
/// The tile colour used to be one of two hardcoded RGB literals, so an embedded icon ignored the
/// active theme while every other pixel of the item honoured it. Deriving it from the control's
/// own `border_color` / `background_color` gives embedded icons the same colour source as the rest
/// of the control, and naming the derivation makes it testable without driving a real `Image`.
///
/// The selected tile keeps the full ink colour; an unselected tile is that ink blended halfway
/// toward the surface, so it reads as the same colour family at lower emphasis.
///
/// The typed `IconSource` slot is available even when the legacy image feature is disabled, so
/// the derivation must be available in every profile that can paint toolbox items.
pub(crate) fn icon_tile_color(ink: Color, surface: Color, is_current: bool) -> Color {
if is_current {
ink
} else {
ink.blend(&surface, 0.5)
}
}
/// Sets the shared widget registry for child forwarding.
pub fn set_registry(&mut self, registry: Rc<RefCell<SimpleRegistry>>) {
self.registry = Some(registry);
self.base.request_redraw();
}
/// Returns the shared widget registry, if set.
pub fn registry(&self) -> Option<&Rc<RefCell<SimpleRegistry>>> {
self.registry.as_ref()
}
}
impl EventHandler for ToolBox {
fn handle_event(&mut self, event: &Event) {
self.base.handle_event(event);
if !self.base.is_enabled() {
return;
}
match event {
Event::MousePress { pos, button, .. } if *button == 1 => {
if let Some(index) = self.item_at_position(*pos) {
if self.items[index].enabled {
// The three modes differ in what a press *means*: a selection in the
// single-page modes, a toggle of that page in non-exclusive mode, and a
// dismissal when the open page's own tab is pressed in popup mode.
match self.mode {
ToolBoxMode::Exclusive => self.set_current_index(index),
ToolBoxMode::NonExclusive | ToolBoxMode::Popup => {
let expanded = self.items[index].expanded;
self.set_item_expanded(index, !expanded);
}
}
}
}
}
Event::MouseMove { pos } => {
// C-28: track the hovered item so a host can drive the existing TooltipManager.
let hovered = self.item_at_position(*pos);
if hovered != self.hovered_item {
self.hovered_item = hovered;
self.base.request_redraw();
}
}
Event::MouseLeave { .. } => {
if self.hovered_item.take().is_some() {
self.base.request_redraw();
}
}
Event::KeyPress { key, .. } => {
// A tab that overflow has scrolled off the strip is still reachable by the arrow
// key that walks toward it: `set_current_index` scrolls the strip to keep the
// selection's tab visible, so the two agree on where the strip sits and the
// keyboard cannot select a page whose tab is nowhere on screen.
let next = match (self.orientation, key) {
// Vertical: Up/Down; Horizontal: Left/Right
(Orientation::Vertical, 38) | (Orientation::Horizontal, 37) => {
// Up/Left
if self.current_index > 0 {
Some(self.current_index - 1)
} else {
None
}
}
(Orientation::Vertical, 40) | (Orientation::Horizontal, 39) => {
// Down/Right
let next = self.current_index + 1;
if next < self.items.len() {
Some(next)
} else {
None
}
}
_ => None,
};
if let Some(idx) = next {
self.set_current_index(idx);
}
}
_ => {}
}
// C-P10: one content derivation for the guard and for the page hit test, and the popup's
// own floating rect when that mode is active.
let content = self.content_rect();
let allow_child_event = match event {
Event::MousePress { pos, .. }
| Event::MouseRelease { pos, .. }
| Event::MouseMove { pos } => {
self.popup_rect(content).unwrap_or(content).contains(*pos)
}
_ => true,
};
// Forward content events only to the open page that owns the point; a coordinate-free event
// goes to the selected page, which is the one a keyboard path is about.
if allow_child_event {
if let Some(ref reg) = self.registry {
match event {
Event::MousePress { pos, .. }
| Event::MouseRelease { pos, .. }
| Event::MouseMove { pos }
| Event::MouseDoubleClick { pos, .. }
| Event::ContextMenu { pos } => {
let hit = self
.page_rects(content)
.into_iter()
.find(|(_, rect)| rect.contains(*pos))
.and_then(|(index, _)| self.items[index].widget);
if let Some(widget_id) = hit {
reg.borrow_mut().forward_event(widget_id, event);
}
}
_ => {
if let Some(widget_id) = self.current_widget() {
reg.borrow_mut().forward_event(widget_id, event);
}
}
}
}
}
}
}
impl Draw for ToolBox {
fn draw(&mut self, context: &mut RenderContext) {
// The page is drawn **before** the tabs, and the tabs are drawn only where `item_rect`
// says they are. Both are consequences of the same rule: the strip and the page share the
// control's axis, so anything the tabs are allowed to paint must already be inside the
// strip, and the page must not paint over the tabs that sit above it.
let content_rect = self.content_rect();
// Every colour below used to be a literal, so a themed toolbox kept a white
// page and light tabs inside a dark window. The style is read once and each
// literal becomes that field's fallback, so nothing that was visible before
// becomes invisible now.
let style = self.base.style();
// Draw content background.
//
// `tool_box` is absent from the role table, so it classifies as `Surface` and the
// active theme writes the *window fill* into `style.background_color`. Taking that
// value at face value painted the content area in the window's own colour — the two
// rectangles in the snapshot were byte-identical, so an empty toolbox read as a bare
// border with a hole in it. `splitter`, `stacked_widget`, `mdi_area` and `carousel`
// all detect exactly this case and re-derive a visible step; this one did not.
let (window_fill, accent) = {
// One theme read for the frame (C-P01): the window fill and the accent token used to
// be a single manager visit here, but the resolved style was a second one.
let theme = ThemeSnapshot::capture("tool_box");
match theme.palette {
Some(palette) => (palette.background, palette.accent),
None => (Color::WHITE, Color::rgb(100, 100, 200)),
}
};
let page = match style.background_color {
Some(resolved) if resolved != window_fill => resolved,
_ => window_fill.blend(&style.text_color.unwrap_or(Color::BLACK), 0.08),
};
// The selected tab is a state highlight derived from the theme's accent token rather
// than a hardcoded light-blue literal. The literal was invisible-proof in a light
// theme (it is what the no-theme fallback still uses) but painted a glaring pale block
// inside a dark one. A tint of the accent over the page keeps the selection visible in
// both appearances, and the accent itself draws the selected frame.
let selected_background = page.blend(&accent, 0.18);
let selected_border = accent;
// Draw content background
context.fill_rect(
Rect::new(content_rect.x, content_rect.y, content_rect.width, content_rect.height),
page,
);
// Draw content border
context.draw_rect(
Rect::new(content_rect.x, content_rect.y, content_rect.width, content_rect.height),
style
.border_color
.filter(|resolved| *resolved != page)
.unwrap_or_else(|| page.blend(&Color::BLACK, 0.25)),
);
// Every open page is drawn, clipped to its own share of the content box. In the single-page
// modes that is exactly the one page the selection names; in non-exclusive mode it is each
// open page in its own band. This happens *before* the tab strip so the tabs stay on top of
// their own surface: a page that painted a border or a background of its own could still
// reach into the strip, and the tabs are the control's chrome rather than its content.
//
// C-29: `Popup` mode draws its page in the floating rect and adds the layer's own frame,
// so the presentation actually reads as a popup rather than as the ordinary page band.
for (index, page_rect) in self.page_rects(content_rect) {
if let Some(widget_id) = self.items[index].widget {
if self.mode == ToolBoxMode::Popup {
context.fill_rect(page_rect, page);
context.draw_rect(page_rect, style.border_color.unwrap_or(selected_border));
}
if let Some(ref reg) = self.registry {
// Two acquisitions are deliberate, not an oversight: the guard must not be held
// across `draw_widget`, because a nested container on the page borrows the same
// registry to draw its own children and would panic as an aliased `RefCell`
// borrow. Each acquisition is O(1), so there is nothing to merge here (VIEW-N04).
reg.borrow_mut().set_widget_geometry(widget_id, page_rect);
context.push_clip(page_rect.x, page_rect.y, page_rect.width, page_rect.height);
reg.borrow_mut().draw_widget(widget_id, context);
context.pop_clip();
}
}
}
// Draw items
for i in 0..self.items.len() {
if let Some(item_rect) = self.item_rect(i) {
let item = &self.items[i];
let is_current = i == self.current_index;
let is_enabled = item.enabled;
// Draw item background
//
// The tab strip is this widget's own surface, so it uses the style;
// the *selected* tab is a state highlight, not a colour, and is kept
// as the literal it always was (swapping it for `border_color` would
// have turned the selection into a plain border-coloured box).
let bg_color = if !is_enabled {
style.background_color.unwrap_or(Color::rgb(240, 240, 240))
} else if is_current {
selected_background
} else {
style.background_color.unwrap_or(Color::rgb(240, 240, 240))
};
context.fill_rect(
Rect::new(item_rect.x, item_rect.y, item_rect.width, item_rect.height),
bg_color,
);
// Draw item border
let border_color = if !is_enabled {
style.border_color.unwrap_or(Color::rgb(200, 200, 200))
} else if is_current {
selected_border
} else {
style.border_color.unwrap_or(Color::rgb(200, 200, 200))
};
context.draw_rect(
Rect::new(item_rect.x, item_rect.y, item_rect.width, item_rect.height),
border_color,
);
let padding = 5i32;
let icon_size = 16u32;
let has_icon = item.has_icon();
let text_x = if has_icon {
item_rect.x + icon_size as i32 + padding + 4
} else {
item_rect.x + padding
};
if has_icon {
let icon_x = item_rect.x + padding;
let icon_y = item_rect.y + (item_rect.height as i32 - icon_size as i32) / 2;
let icon_rect = Rect::new(icon_x, icon_y, icon_size, icon_size);
// Draw a small rounded square as the icon tile.
//
// The tile colour is **derived from the resolved style**, not a literal: the
// selected tile uses the control's own `border_color` (the same token the
// selected item's frame already uses) and an unselected tile is that colour
// blended toward the surface. Two hardcoded RGB literals here meant the embedded
// icon ignored the theme entirely \u2014 the colour-source gap the icon audit
// (issue \u00a79 item #7) flagged. `text_color` remains the fallback so a build
// with no active theme still paints a visible tile.
let tile_ink = style.border_color.unwrap_or(Color::rgb(100, 100, 200));
let surface = style.background_color.unwrap_or(Color::rgb(240, 240, 240));
let icon_bg_color = Self::icon_tile_color(tile_ink, surface, is_current);
context.fill_rounded_rect(icon_rect, 3, icon_bg_color);
let glyph_rect = Rect::new(icon_x + 2, icon_y + 2, 12, 12);
if let Some(source) = item.icon_source {
if let Err(error) = crate::widget::paint_icon(
context,
source.into(),
glyph_rect,
style.text_color.unwrap_or(Color::rgb(255, 255, 255)),
) {
log::warn!("toolbox item {i} icon failed: {error:?}");
}
}
// The legacy `Image` slot reserved this tile and then painted a stand-in dot
// instead of the image the caller set. It now draws the image itself, through
// the same seam every other legacy image slot in the crate uses.
#[cfg(feature = "image")]
if item.icon_source.is_none() {
if let Some(image) = item.icon.as_ref() {
if !crate::widget::draw_icon_image(context, image, glyph_rect) {
log::warn!("toolbox item {i} icon image could not be drawn");
}
}
}
}
// Draw item text
//
// A disabled item keeps its dimmed literal: that muted grey *is* how
// the state is communicated, and `text_color` would repaint it at
// full strength and erase the distinction.
let item_text_color = style.text_color.unwrap_or(Color::rgb(0, 0, 0));
let text_color =
if !is_enabled { Color::rgb(150, 150, 150) } else { item_text_color };
// The label is centred on the item's own line box. `item_rect.y +
// item_rect.height / 2` placed the glyph box's top edge on the item's middle
// line instead, which drew the text half a line low.
// The **effective font** — the resolved theme/caller font — not a hardcoded default,
// so the item label honours the theme body font and the user's text scale
// (D09-STYLE-01).
let text_font = effective_font(style);
let item_line = context.text_line(item_rect, text_font);
// Fitted inside the item: horizontal tabs are a fixed width, so a long item name
// used to paint over the next tab (and, vertically, outside the control).
let text_width = (item_rect.x + item_rect.width as i32 - text_x).max(0) as u32;
context.draw_text_fitted_cow(
Rect::new(text_x, item_line.y, text_width, item_line.height.max(1)),
&item.text,
text_font,
text_color,
HorizontalAlignment::Left,
);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::{Point, Rect};
use crate::event::Event;
use crate::widget::svg::render_to_svg;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
// ── Helper constants ──────────────────────────────────────────────────
fn widget_id_1() -> ObjectId {
9101
}
fn widget_id_2() -> ObjectId {
9102
}
fn widget_id_3() -> ObjectId {
9103
}
// ── 1. Creation defaults ──────────────────────────────────────────────
#[test]
fn toolbox_creation_defaults() {
let tb = ToolBox::new(Rect::new(0, 0, 200, 160));
assert_eq!(tb.count(), 0, "should have no items");
assert_eq!(tb.selected_index().unwrap_or(0), 0, "current index should be 0");
assert_eq!(tb.current_widget(), None, "no current widget");
assert_eq!(tb.orientation(), Orientation::Vertical, "default orientation");
assert!(tb.is_visible(), "should be visible");
assert!(tb.is_enabled(), "should be enabled");
assert_eq!(tb.geometry(), Rect::new(0, 0, 200, 160));
assert_eq!(tb.kind(), WidgetKind::Toolbox);
assert!(tb.registry().is_none(), "registry should be None by default");
}
// ── 2. Adding items ───────────────────────────────────────────────────
#[test]
fn toolbox_add_item() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
let idx = tb.add_item("First".to_string(), None);
assert_eq!(idx, 0, "first item index should be 0");
assert_eq!(tb.count(), 1);
let idx2 = tb.add_item("Second".to_string(), Some(widget_id_1()));
assert_eq!(idx2, 1, "second item index should be 1");
assert_eq!(tb.count(), 2);
// The widget associated with the second item becomes a child
let children = tb.children();
assert!(children.contains(&widget_id_1()), "child widget should be tracked");
}
#[test]
fn toolbox_add_item_without_widget_does_not_add_child() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("NoWidget".to_string(), None);
assert!(tb.children().is_empty(), "no children when no widget is given");
}
#[test]
fn toolbox_typed_item_icon_is_checked_rendered_and_cleared_with_items() {
static RING: crate::widget::IconData = crate::widget::IconData {
name: "toolbox-ring",
grid: 24,
paths: &["M2-2H22V-22H2Z M7-7V-17H17V-7Z"],
};
let mut toolbox = ToolBox::new(Rect::new(0, 0, 200, 160));
toolbox.add_item("Page".to_string(), None);
assert_eq!(
toolbox.set_item_icon_source(1, Some(IconSource::static_geometry(&RING))),
Err(IconItemError::IndexOutOfBounds { index: 1, len: 1 })
);
toolbox
.set_item_icon_source(0, Some(IconSource::static_geometry(&RING)))
.expect("existing toolbox item accepts typed source");
assert!(render_to_svg(&mut toolbox).contains("fill-rule=\"nonzero\""));
toolbox.clear();
assert_eq!(
toolbox.set_item_icon_source(0, None),
Err(IconItemError::IndexOutOfBounds { index: 0, len: 0 })
);
}
/// A legacy item `Image` used to reserve the tile and then paint a stand-in dot instead of the
/// image the caller set -- the API promised an icon and drew a decoration.
#[cfg(feature = "image")]
#[test]
fn toolbox_legacy_item_image_is_drawn_and_cleared() {
let mut toolbox = ToolBox::new(Rect::new(0, 0, 200, 160));
toolbox.add_item("Page".to_string(), None);
let plain = render_to_svg(&mut toolbox);
assert!(!plain.contains("<image"), "an item with no icon draws no image");
toolbox.item_mut(0).expect("the item exists").set_icon(Some(
Image::try_from_rgba(vec![200; 4 * 4 * 4], 4, 4)
.expect("the buffer matches its declared size"),
));
assert!(toolbox.item(0).expect("the item exists").has_icon());
assert!(render_to_svg(&mut toolbox).contains("<image"), "the image must reach the backend");
// An explicit typed clear clears the legacy image too, so the tile cannot keep painting an
// icon the caller just cleared.
toolbox.set_item_icon_source(0, None).expect("the item exists");
assert!(!toolbox.item(0).expect("the item exists").has_icon());
assert!(
!render_to_svg(&mut toolbox).contains("<image"),
"clear must release the image slot"
);
}
// ── 3. Setting/getting current index ──────────────────────────────────
#[test]
fn toolbox_get_set_current_index() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
assert_eq!(tb.selected_index().unwrap_or(0), 0, "starts at 0");
tb.set_current_index(1);
assert_eq!(tb.selected_index().unwrap_or(0), 1);
tb.set_current_index(0);
assert_eq!(tb.selected_index().unwrap_or(0), 0);
}
#[test]
fn toolbox_set_current_index_out_of_bounds_is_noop() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("Only".to_string(), None);
tb.set_current_index(5); // out of bounds
assert_eq!(tb.selected_index().unwrap_or(0), 0, "should remain 0");
}
#[test]
fn toolbox_set_current_index_same_value_is_noop() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.set_current_index(0); // already 0, should not emit signal
assert_eq!(tb.selected_index().unwrap_or(0), 0);
}
// ── 4. Item count ─────────────────────────────────────────────────────
#[test]
fn toolbox_item_count() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
assert_eq!(tb.count(), 0);
tb.add_item("A".to_string(), None);
assert_eq!(tb.count(), 1);
tb.add_item("B".to_string(), None);
tb.add_item("C".to_string(), None);
assert_eq!(tb.count(), 3);
}
// ── 5. Removing items ─────────────────────────────────────────────────
#[test]
fn toolbox_remove_item() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), Some(widget_id_1()));
tb.add_item("C".to_string(), None);
tb.remove_item(1);
assert_eq!(tb.count(), 2);
// The widget should be removed from children
assert!(!tb.children().contains(&widget_id_1()), "child widget should be removed");
}
#[test]
fn toolbox_remove_item_out_of_bounds_is_noop() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.remove_item(5); // out of bounds
assert_eq!(tb.count(), 1);
}
#[test]
fn toolbox_remove_item_adjusts_current_index() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
tb.add_item("C".to_string(), None);
tb.set_current_index(2);
// Remove item at index 0 (< current_index), current decrements to 1
tb.remove_item(0);
assert_eq!(tb.selected_index().unwrap_or(0), 1);
// Removing last item leaves empty -> current_index resets to 0
tb.remove_item(1);
tb.remove_item(0);
assert_eq!(tb.count(), 0);
assert_eq!(tb.selected_index().unwrap_or(0), 0);
}
#[test]
fn toolbox_remove_item_above_current_index_does_not_change_it() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
tb.set_current_index(0);
// Remove item at index 1 (> current_index 0), current stays 0
tb.remove_item(1);
assert_eq!(tb.selected_index().unwrap_or(0), 0);
}
// ── 6. Clear all items ────────────────────────────────────────────────
#[test]
fn toolbox_clear_all_items() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), Some(widget_id_1()));
tb.add_item("B".to_string(), Some(widget_id_2()));
tb.add_item("C".to_string(), Some(widget_id_3()));
// Remove all items one by one (no dedicated clear() method)
tb.remove_item(0);
tb.remove_item(0);
tb.remove_item(0);
assert_eq!(tb.count(), 0);
assert_eq!(tb.selected_index().unwrap_or(0), 0);
assert!(tb.children().is_empty(), "all child widgets should be removed");
assert_eq!(tb.current_widget(), None);
}
// ── 7. Setting/getting item text/tooltip ──────────────────────────────
#[test]
fn toolbox_item_text_and_tooltip() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("Hello".to_string(), None);
tb.add_item("World".to_string(), None);
if let Some(item) = tb.item(0) {
assert_eq!(item.text(), "Hello");
assert_eq!(item.tooltip(), "");
}
// Modify item via item_mut
if let Some(item) = tb.item_mut(1) {
item.set_text("Modified".to_string());
item.set_tooltip("Tooltip text".to_string());
}
if let Some(item) = tb.item(1) {
assert_eq!(item.text(), "Modified");
assert_eq!(item.tooltip(), "Tooltip text");
}
// Getting item at out-of-bounds index
assert!(tb.item(5).is_none());
assert!(tb.item_mut(5).is_none());
}
// ── 8. Geometry delegation ────────────────────────────────────────────
#[test]
fn toolbox_geometry_delegation() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
assert_eq!(tb.geometry(), Rect::new(0, 0, 200, 160));
tb.set_geometry(Rect::new(10, 20, 300, 200));
assert_eq!(tb.geometry(), Rect::new(10, 20, 300, 200));
assert_eq!(tb.base().geometry(), tb.geometry());
}
#[test]
fn toolbox_position_and_size() {
let mut tb = ToolBox::new(Rect::new(5, 10, 200, 150));
assert_eq!(tb.position(), Point::new(5, 10));
assert_eq!(tb.size(), crate::core::Size::new(200, 150));
tb.set_position(Point::new(20, 30));
assert_eq!(tb.geometry(), Rect::new(20, 30, 200, 150));
tb.set_size(crate::core::Size::new(300, 200));
assert_eq!(tb.geometry(), Rect::new(20, 30, 300, 200));
}
// ── 9. Visibility ─────────────────────────────────────────────────────
#[test]
fn toolbox_visibility() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
assert!(tb.is_visible());
tb.hide();
assert!(!tb.is_visible());
tb.show();
assert!(tb.is_visible());
tb.set_visible(false);
assert!(!tb.is_visible());
tb.set_visible(true);
assert!(tb.is_visible());
}
#[test]
fn toolbox_enabled_state() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
assert!(tb.is_enabled());
tb.set_enabled(false);
assert!(!tb.is_enabled());
tb.set_enabled(true);
assert!(tb.is_enabled());
}
// ── 10. ID / Kind ─────────────────────────────────────────────────────
#[test]
fn toolbox_kind() {
let tb = ToolBox::new(Rect::new(0, 0, 200, 160));
assert_eq!(tb.kind(), WidgetKind::Toolbox);
}
#[test]
fn toolbox_id_is_unique() {
let tb1 = ToolBox::new(Rect::new(0, 0, 100, 100));
let tb2 = ToolBox::new(Rect::new(0, 0, 100, 100));
assert_ne!(tb1.id(), tb2.id(), "each ToolBox must have a unique id");
}
// ── 11. SVG draw output ───────────────────────────────────────────────
#[test]
fn toolbox_draw_produces_svg_output() {
let mut tb = ToolBox::new(Rect::new(0, 0, 300, 160));
tb.add_item("Item1".to_string(), None);
tb.add_item("Item2".to_string(), None);
let svg = render_to_svg(&mut tb);
assert!(svg.starts_with("<svg"), "SVG must start with <svg");
assert!(svg.ends_with("</svg>"), "SVG must end with </svg>");
assert!(svg.contains("width=\"300\""), "SVG must contain correct width");
assert!(svg.contains("height=\"160\""), "SVG must contain correct height");
assert!(svg.contains("fill="), "SVG should contain fill attributes");
assert!(svg.len() > 100, "SVG output should be substantial");
}
/// An embedded icon's tile colour is derived from the style, not a hardcoded literal.
///
/// # The defect this pins (issue §9 item #7)
///
/// `ToolBox`'s icon tile used two fixed RGB literals, so an embedded icon ignored the active
/// theme while every other pixel of the item honoured it. `icon_tile_color` now takes the ink and
/// surface from the resolved style; this asserts the two observable properties: the selected tile
/// is exactly the ink, and an unselected tile is a *different*, surface-blended colour (so it is
/// not the same literal in disguise).
///
#[test]
fn an_embedded_icon_tile_is_derived_from_the_style() {
let ink = Color::rgb(10, 20, 30);
let surface = Color::rgb(250, 250, 250);
assert_eq!(
ToolBox::icon_tile_color(ink, surface, true),
ink,
"the selected tile must use the style ink directly"
);
let unselected = ToolBox::icon_tile_color(ink, surface, false);
assert_ne!(unselected, ink, "an unselected tile must be a distinct, blended colour");
assert_ne!(
unselected,
Color::rgb(180, 180, 200),
"the tile must not be the old hardcoded literal"
);
// Blending toward a light surface must lighten the ink, not darken it.
assert!(
unselected.r > ink.r && unselected.g > ink.g && unselected.b > ink.b,
"blending toward a light surface must lighten the tile: ink={ink:?} tile={unselected:?}"
);
}
#[test]
fn toolbox_draw_empty_produces_svg() {
let mut tb = ToolBox::new(Rect::new(0, 0, 100, 50));
let svg = render_to_svg(&mut tb);
assert!(svg.starts_with("<svg"));
assert!(svg.len() > 50);
}
// ── 12. Signal accessors ──────────────────────────────────────────────
#[test]
fn toolbox_current_changed_signal_emits_on_set() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
tb.add_item("C".to_string(), None);
let emitted = Arc::new(AtomicUsize::new(0));
let e = emitted.clone();
tb.current_changed.connect(move |idx: Arc<usize>| {
e.store(*idx, Ordering::SeqCst);
});
tb.set_current_index(1);
assert_eq!(emitted.load(Ordering::SeqCst), 1);
tb.set_current_index(2);
assert_eq!(emitted.load(Ordering::SeqCst), 2);
}
#[test]
fn toolbox_current_changed_does_not_emit_for_same_value() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
let hits = Arc::new(AtomicUsize::new(0));
let h = hits.clone();
tb.current_changed.connect(move |_: Arc<usize>| {
h.fetch_add(1, Ordering::SeqCst);
});
// Set to same index (0) — should not emit
tb.set_current_index(0);
assert_eq!(hits.load(Ordering::SeqCst), 0);
}
// ── 13. Mouse click selects item ──────────────────────────────────────
#[test]
fn toolbox_mouse_click_selects_item() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("First".to_string(), None);
tb.add_item("Second".to_string(), None);
assert_eq!(tb.selected_index().unwrap_or(0), 0);
// Default orientation is Vertical; item_rect(1) has y = 0 + 32*1 = 32
// Click on item 1 at (10, 40) which is within vertical item 1: (0, 32, 200, 32)
tb.handle_event(&Event::MousePress { pos: Point::new(10, 40), button: 1, modifiers: 0 });
assert_eq!(tb.selected_index().unwrap_or(0), 1, "clicking item should select it");
}
#[test]
fn toolbox_mouse_click_on_disabled_item_does_not_select() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
// Disable item 1
if let Some(item) = tb.item_mut(1) {
item.set_enabled(false);
}
assert_eq!(tb.selected_index().unwrap_or(0), 0);
// Click on item 1 (y=32), should not select because item is disabled
tb.handle_event(&Event::MousePress { pos: Point::new(10, 40), button: 1, modifiers: 0 });
assert_eq!(
tb.selected_index().unwrap_or(0),
0,
"clicking disabled item should not change current"
);
}
#[test]
fn toolbox_mouse_click_on_empty_region_is_noop() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
assert_eq!(tb.selected_index().unwrap_or(0), 0);
// Click far below items (y=200, but geometry only goes to 160)
// Since item_at_position returns None for out-of-bounds, no change
tb.handle_event(&Event::MousePress { pos: Point::new(10, 200), button: 1, modifiers: 0 });
assert_eq!(tb.selected_index().unwrap_or(0), 0);
}
// ── 14. Disabled state blocks events ──────────────────────────────────
#[test]
fn toolbox_disabled_state_blocks_mouse_events() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
tb.set_current_index(0);
// Disable the entire toolbox
tb.set_enabled(false);
// Try clicking on item 1
tb.handle_event(&Event::MousePress { pos: Point::new(10, 40), button: 1, modifiers: 0 });
// current_index should remain 0 because event handling returns early when disabled
assert_eq!(
tb.selected_index().unwrap_or(0),
0,
"disabled toolbox should not process mouse events"
);
}
#[test]
fn toolbox_disabled_re_enable_processes_events() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
tb.set_enabled(false);
tb.handle_event(&Event::MousePress { pos: Point::new(10, 40), button: 1, modifiers: 0 });
assert_eq!(tb.selected_index().unwrap_or(0), 0);
// Re-enable and click again
tb.set_enabled(true);
tb.handle_event(&Event::MousePress { pos: Point::new(10, 40), button: 1, modifiers: 0 });
assert_eq!(
tb.selected_index().unwrap_or(0),
1,
"after re-enable, mouse clicks should work"
);
}
// ── 16. Keyboard navigation (vertical) ────────────────────────────────
#[test]
fn toolbox_key_down_selects_next_item_vertical() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
tb.add_item("C".to_string(), None);
assert_eq!(tb.selected_index().unwrap_or(0), 0);
tb.handle_event(&Event::KeyPress { key: 40, modifiers: 0 });
assert_eq!(tb.selected_index().unwrap_or(0), 1);
tb.handle_event(&Event::KeyPress { key: 40, modifiers: 0 });
assert_eq!(tb.selected_index().unwrap_or(0), 2);
}
#[test]
fn toolbox_key_up_selects_prev_item_vertical() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
tb.set_current_index(1);
tb.handle_event(&Event::KeyPress { key: 38, modifiers: 0 });
assert_eq!(tb.selected_index().unwrap_or(0), 0);
}
#[test]
fn toolbox_key_navigation_stops_at_boundaries() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
// Already at first item, Up should stay
tb.handle_event(&Event::KeyPress { key: 38, modifiers: 0 });
assert_eq!(tb.selected_index().unwrap_or(0), 0);
// Already at last item, Down should stay
tb.handle_event(&Event::KeyPress { key: 40, modifiers: 0 });
assert_eq!(tb.selected_index().unwrap_or(0), 0);
}
#[test]
fn toolbox_key_navigation_horizontal() {
let mut tb = ToolBox::new(Rect::new(0, 0, 400, 100));
tb.set_orientation(Orientation::Horizontal);
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
tb.add_item("C".to_string(), None);
assert_eq!(tb.selected_index().unwrap_or(0), 0);
// Right arrow advances
tb.handle_event(&Event::KeyPress { key: 39, modifiers: 0 });
assert_eq!(tb.selected_index().unwrap_or(0), 1);
// Left arrow goes back
tb.handle_event(&Event::KeyPress { key: 37, modifiers: 0 });
assert_eq!(tb.selected_index().unwrap_or(0), 0);
}
// ── 17. Clear method ──────────────────────────────────────────────────
#[test]
fn toolbox_clear_removes_all_items() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 160));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), Some(widget_id_1()));
tb.add_item("C".to_string(), None);
tb.set_current_index(2);
tb.clear();
assert_eq!(tb.count(), 0);
assert_eq!(tb.selected_index().unwrap_or(0), 0);
assert!(tb.children().is_empty(), "child widgets should be removed");
}
// ── 18. Overflow: the page's minimum and the tab strip's containment ──────
/// The defect: `content_rect` was `rect.height - item_height * items.len()`, floored at zero,
/// so four 32 px tabs in the 120 px census control left the page **0** px tall.
///
/// The page is observed through the drawing, because that is what the defect was visible as:
/// the page rectangle the control emits must have a positive height, and it must be
/// *distinct* from the tab strip rather than collapsed onto it.
#[test]
fn toolbox_page_keeps_an_extent_when_the_tabs_would_consume_it() {
// Enough tabs to swallow the whole control at the fixed tab height.
for count in [4usize, 6, 20] {
let mut tb = ToolBox::new(Rect::new(0, 0, 240, 120));
for i in 0..count {
tb.add_item(format!("Item {i}"), None);
}
let svg = render_to_svg(&mut tb);
// The page is the full-width rectangle below the tab strip; the strip's own rects are
// only as tall as one tab. The tallest full-width rectangle is the page.
let page_height = svg
.lines()
.filter(|l| l.contains("<rect"))
.filter_map(|l| {
let n: Vec<i32> = l
.split(|c: char| !(c.is_ascii_digit() || c == '-'))
.filter(|s| !s.is_empty())
.filter_map(|s| s.parse().ok())
.collect();
(n.len() >= 4).then(|| (n[0], n[1], n[2], n[3]))
})
.filter(|(_, _, w, _)| *w == 240)
.map(|(_, _, _, h)| h)
.max()
.unwrap_or(0);
assert!(
page_height > 0,
"{count} tabs on a 120px box left the page {page_height}px tall"
);
}
}
/// The defect: `item_rect` was unclamped, so item `n` was placed at `item_height * n` with no
/// reference to the control's extent and later items were painted outside it.
#[test]
fn toolbox_no_item_rect_escapes_the_control() {
let geometry = Rect::new(0, 0, 240, 120);
for count in [1usize, 3, 4, 6, 20] {
let mut tb = ToolBox::new(geometry);
for i in 0..count {
tb.add_item(format!("Item {i}"), None);
}
for i in 0..count {
if let Some(r) = tb.item_rect(i) {
assert!(
r.y >= geometry.y && r.bottom() <= geometry.bottom(),
"item {i} of {count} at {r:?} escapes {geometry:?}"
);
assert!(r.width > 0 && r.height > 0, "item {i} of {count} is empty: {r:?}");
}
}
}
}
/// The overflow rule must be *driven*, not just computed: with more tabs than fit, the strip
/// can scroll, and selecting an off-screen tab brings its tab into view.
#[test]
fn toolbox_overflow_scrolls_and_the_page_follows_the_middle_tab() {
let mut tb = ToolBox::new(Rect::new(0, 0, 240, 120));
for i in 0..10 {
tb.add_item(format!("Item {i}"), None);
}
assert!(tb.max_scroll() > 0, "ten 32px tabs cannot fit a 24px page in 120px");
tb.set_current_index(9);
assert!(tb.scroll_offset() > 0, "selecting a scrolled-off tab must bring it into view");
assert!(tb.item_rect(9).is_some(), "the selected tab must be on screen after scrolling");
// An out-of-range scroll request is clamped rather than leaving the strip blank.
tb.set_scroll_offset(usize::MAX);
assert_eq!(tb.scroll_offset(), tb.max_scroll());
tb.set_scroll_offset(0);
assert_eq!(tb.scroll_offset(), 0);
}
/// The rendered SVG must contain nothing outside the control, at any tab count.
#[test]
fn toolbox_draw_never_paints_outside_the_control() {
for count in [1usize, 4, 6, 20] {
let mut tb = ToolBox::new(Rect::new(0, 0, 240, 120));
for i in 0..count {
tb.add_item(format!("Item {i}"), None);
}
let svg = render_to_svg(&mut tb);
for line in svg.lines().filter(|l| l.contains("<rect") || l.contains("<line")) {
let nums: Vec<i32> = line
.split(|c: char| !(c.is_ascii_digit() || c == '-'))
.filter(|s| !s.is_empty())
.filter_map(|s| s.parse().ok())
.collect();
assert!(
nums.iter().all(|v| *v >= 0),
"{count} items painted outside the control: {line}"
);
}
}
}
/// An index past the end appends instead of panicking.
///
/// `insert_item` passed the caller's index straight to `Vec::insert`, which panics
/// when it exceeds the length. `tab_bar` and `pie_menu` clamp, so a caller that
/// asked to insert past the end crashed here and appended everywhere else.
#[test]
fn toolbox_insert_past_the_end_appends() {
let mut tb = ToolBox::new(Rect::new(0, 0, 240, 120));
tb.add_item("A".to_string(), None);
tb.add_item("B".to_string(), None);
tb.insert_item(9, "C".to_string(), None);
assert_eq!(tb.count(), 3);
assert_eq!(tb.item(2).map(|i| i.text()), Some("C"), "past the end lands last");
}
/// The row height is a settable metric, and a smooth scroll eases toward the target.
#[test]
fn item_height_and_smooth_scroll_are_wired() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 100));
for index in 0..6 {
tb.add_item(format!("Item {index}"), None);
}
tb.set_item_height(48);
assert_eq!(tb.item_height(), 48);
assert_eq!(tb.item_rect(1).map(|rect| rect.height), Some(48));
tb.set_smooth_scroll(true);
tb.set_scroll_offset(1);
assert!(tb.is_animating());
assert!(tb.tick(60));
assert!(!tb.tick(1000));
assert!(!tb.is_animating());
}
/// C-28: the pointer path records the hovered item and exposes its tooltip.
#[test]
fn hovering_an_item_exposes_its_tooltip() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 300));
tb.add_item("Group".to_string(), None);
tb.item_mut(0).expect("item").tooltip = "First group".to_string();
assert_eq!(tb.hovered_tooltip(), None);
tb.handle_event(&crate::event::Event::MouseMove { pos: crate::core::Point::new(10, 10) });
assert_eq!(tb.hovered_item(), Some(0));
assert_eq!(tb.hovered_tooltip(), Some("First group"));
tb.handle_event(&crate::event::Event::MouseLeave { pos: crate::core::Point::new(10, 10) });
assert_eq!(tb.hovered_item(), None);
}
/// C-29: popup mode presents the page in a floating rectangle, not the page band.
#[test]
fn popup_mode_uses_a_floating_rect() {
let mut tb = ToolBox::new(Rect::new(0, 0, 200, 300));
tb.add_item("Group".to_string(), None);
tb.add_item("Other".to_string(), None);
tb.set_mode(ToolBoxMode::Popup);
assert!(tb.set_item_expanded(0, true));
let content = tb.content_rect();
let popup = tb.popup_rect(content).expect("an open popup has a rect");
assert_eq!(popup.height, 180, "the floating layer has its own extent");
assert!(popup.y >= content.y, "the layer stays inside the content box");
assert_ne!(popup, content, "it is a presentation of its own, not the page band");
assert_eq!(tb.page_rects(content), vec![(0, popup)]);
}
}