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
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
//! MDI area widget.
use crate::core::{Color, HorizontalAlignment, ObjectId, Point, Rect};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::Signal1;
use crate::widget::capability::properties_trait::{base_property_get, base_property_set};
use crate::widget::capability::types::{CapabilityAccessError, CapabilityValue};
use crate::widget::capability::WidgetProperties;
use crate::widget::metrics::effective_font;
use crate::widget::IconSource;
#[cfg(feature = "image")]
use crate::widget::Image;
use crate::widget::{BaseWidget, Draw, SimpleRegistry, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use super::theme_snapshot::ThemeSnapshot;
/// MDI area widget.
pub struct MdiArea {
base: BaseWidget,
subwindows: Vec<MdiSubWindow>,
active_subwindow: Option<usize>,
view_mode: ViewMode,
background: Background,
activation_order: ActivationOrder,
/// Emitted with the child `ObjectId` whenever a sub-window becomes active,
/// including the automatic activation of the first sub-window on add and of
/// a replacement sub-window after the active one is removed.
pub subwindow_activated: Signal1<ObjectId>,
/// Emitted with `(child_id, new_title)` whenever a sub-window's title is changed through
/// [`MdiArea::set_sub_window_title`].
///
/// # Why the change is announced by the container
///
/// A sub-window is a record (an id, a rectangle and some flags), not a control: it owns no
/// signal and cannot repaint the area it sits in. Announcing a title change from the container
/// entry is what keeps the signal reachable — `MdiSubWindow::set_title` stays a value-level
/// setter, and this is the entry a host should use.
pub subwindow_title_changed: Signal1<(ObjectId, String)>,
/// Child ids in the order they were activated, most recent last.
///
/// Only [`ActivationOrder::HistoryOrder`] reads this, but every activation records it so a
/// later switch of order has a real history to traverse rather than starting empty.
activation_history: Vec<ObjectId>,
/// The live edge-resize gesture, if one is in progress.
resize_drag: Option<SubwindowResize>,
/// The live title-bar move gesture, if one is in progress (C-14).
move_drag: Option<SubwindowMove>,
/// The live tab-reorder gesture, if one is in progress (C-17).
tab_drag: Option<(usize, Point)>,
/// The geometry a maximized sub-window will return to, with its index.
maximized_restore: Option<(usize, Rect)>,
/// Viewport offset applied to every sub-window frame in `SubWindowView`.
///
/// Zero is the default, so every existing placement, hit test and snapshot is unchanged.
/// A non-zero offset pans the work area without moving the stored geometries, which is
/// what lets a host scroll a desktop larger than the visible area.
scroll_offset: Point,
/// Optional shared registry for child forwarding.
registry: Option<Rc<RefCell<SimpleRegistry>>>,
/// Set by a sub-window title write; the next paint consumes it and schedules the frame that
/// makes the new title visible.
title_dirty: Rc<Cell<bool>>,
/// Emitted with `(child_id, minimized)` when a sub-window is minimized or restored (C-15).
pub subwindow_minimized_changed: Signal1<(ObjectId, bool)>,
/// Emitted with `(old_index, new_index)` when a tab is dragged to a new slot (C-17).
pub subwindow_tab_moved: Signal1<(usize, usize)>,
/// Cached z-order index and the key it was produced under (C-P08).
///
/// The order changes only when a sub-window is added, removed or raised, but the hit chain
/// and the painter used to collect and sort it four times per frame/event.
z_cache: RefCell<Option<(u64, Rc<Vec<usize>>)>>,
}
/// MDI sub-window.
pub struct MdiSubWindow {
widget: ObjectId,
geometry: Rect,
title: String,
#[cfg(feature = "image")]
icon: Option<Image>,
/// Typed icon content for this sub-window.
///
/// Shares one active state with the legacy [`MdiSubWindow::icon`] image: whichever was assigned
/// most recently wins, so a getter never reports content the title bar is not drawing.
icon_source: Option<IconSource>,
minimized: bool,
maximized: bool,
closable: bool,
movable: bool,
resizable: bool,
z_order: i32,
/// The owning area's title signal, installed when the record is attached.
///
/// A sub-window is a record, not a control, so it has no signal of its own. Holding a clone of
/// the area's signal lets [`Self::set_title`] announce the same event as the container entry;
/// the two write paths then cannot disagree (C-09).
title_sink: Option<Signal1<(ObjectId, String)>>,
/// Set when a title write changed the text; the owning area turns it into a repaint.
title_dirty: Option<Rc<Cell<bool>>>,
}
/// How an MDI area arranges its sub-windows.
///
/// # Not the same as [`crate::widget::view_widgets::list_view::ViewMode`]
///
/// Both types are called `ViewMode`, but they describe different axes and are
/// **not** interchangeable:
///
/// * this one — how an `MdiArea` lays its child windows out (free-floating
/// sub-windows vs. tabs inside the area);
/// * [`list_view::ViewMode`][lv] — how one `ListView` presents its *own items*
/// (list, icons, details, thumbnails).
///
/// They are deliberately left as two enums: merging them would create one type
/// whose variants are meaningless in the other's context, so a `match` could not
/// be exhaustive in either widget. See principle #49.
///
/// [lv]: crate::widget::view_widgets::list_view::ViewMode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ViewMode {
/// Sub-window mode
#[default]
SubWindowView,
/// Tabbed view
TabbedView,
}
/// MDI background.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Background {
/// No background
NoBackground,
/// Plain color background
#[default]
Plain,
/// Gradient background
Gradient,
/// Pattern background
Pattern,
}
/// MDI activation order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ActivationOrder {
/// Activation follows creation order
CreationOrder,
/// Activation follows stacking order
#[default]
StackingOrder,
/// Activation follows history order
HistoryOrder,
}
impl MdiSubWindow {
/// Creates a new MDI sub-window.
pub fn new(widget: ObjectId, geometry: Rect) -> Self {
Self {
widget,
geometry,
title: String::new(),
#[cfg(feature = "image")]
icon: None,
icon_source: None,
minimized: false,
maximized: false,
closable: true,
movable: true,
resizable: true,
z_order: 0,
title_sink: None,
title_dirty: None,
}
}
/// Returns widget.
pub fn widget(&self) -> ObjectId {
self.widget
}
/// Returns geometry.
pub fn geometry(&self) -> Rect {
self.geometry
}
/// Sets geometry.
pub fn set_geometry(&mut self, geometry: Rect) {
self.geometry = geometry;
}
/// Returns title.
pub fn title(&self) -> &str {
&self.title
}
/// Replaces the title.
///
/// A real change is announced through the owning area's `subwindow_title_changed` signal and
/// marks the area for repaint; a write of the same text is a no-op. The announcement lives here
/// as well as on the container setter so a host that reaches a record through
/// [`MdiArea::sub_window_mut`] cannot silently bypass the event (C-09).
pub fn set_title(&mut self, title: String) {
if self.title == title {
return;
}
self.title = title;
if let Some(sink) = &self.title_sink {
sink.emit((self.widget, self.title.clone()));
}
if let Some(dirty) = &self.title_dirty {
dirty.set(true);
}
}
#[cfg(feature = "image")]
/// Returns icon.
pub fn icon(&self) -> Option<&Image> {
self.icon.as_ref()
}
#[cfg(feature = "image")]
/// Sets icon.
///
/// Replaces any typed [`MdiSubWindow::icon_source`] previously assigned. A non-RGBA8 image is
/// normalised once here so the title-bar draw can borrow its bytes 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;
}
/// Assigns typed, already-resolved icon content to this sub-window.
///
/// `None` clears the typed source and the legacy image together, so an explicit clear cannot be
/// followed by the previous icon reappearing.
pub fn set_icon_source(&mut self, source: Option<IconSource>) {
self.icon_source = source;
#[cfg(feature = "image")]
{
self.icon = None;
}
}
/// Returns the typed icon source, if one is active.
pub fn icon_source(&self) -> Option<IconSource> {
self.icon_source
}
/// Clears the sub-window's icon, whatever form it was stored in.
pub fn clear_icon_source(&mut self) {
self.set_icon_source(None);
}
/// Whether the title bar of this sub-window reserves a leading icon slot.
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 whether sub-window is minimized.
pub fn is_minimized(&self) -> bool {
self.minimized
}
/// Sets minimized state.
pub fn set_minimized(&mut self, minimized: bool) {
self.minimized = minimized;
}
/// Returns whether sub-window is maximized.
pub fn is_maximized(&self) -> bool {
self.maximized
}
/// Sets maximized state.
pub fn set_maximized(&mut self, maximized: bool) {
self.maximized = maximized;
}
/// Returns whether sub-window is closable.
pub fn is_closable(&self) -> bool {
self.closable
}
/// Sets closable state.
pub fn set_closable(&mut self, closable: bool) {
self.closable = closable;
}
/// Returns whether sub-window is movable.
pub fn is_movable(&self) -> bool {
self.movable
}
/// Sets movable state.
pub fn set_movable(&mut self, movable: bool) {
self.movable = movable;
}
/// Returns whether sub-window is resizable.
pub fn is_resizable(&self) -> bool {
self.resizable
}
/// Sets resizable state.
pub fn set_resizable(&mut self, resizable: bool) {
self.resizable = resizable;
}
/// Returns z-order.
pub fn z_order(&self) -> i32 {
self.z_order
}
/// Sets z-order.
pub fn set_z_order(&mut self, z_order: i32) {
self.z_order = z_order;
}
}
impl MdiArea {
/// Creates an MDI area.
pub fn new(geometry: Rect) -> Self {
Self {
base: BaseWidget::new(WidgetKind::MdiArea, geometry, "MdiArea"),
subwindows: Vec::new(),
active_subwindow: None,
view_mode: ViewMode::SubWindowView,
background: Background::Plain,
activation_order: ActivationOrder::StackingOrder,
subwindow_activated: Signal1::new(),
subwindow_title_changed: Signal1::new(),
activation_history: Vec::new(),
resize_drag: None,
move_drag: None,
tab_drag: None,
maximized_restore: None,
scroll_offset: Point::new(0, 0),
registry: None,
title_dirty: Rc::new(Cell::new(false)),
subwindow_minimized_changed: Signal1::new(),
subwindow_tab_moved: Signal1::new(),
z_cache: RefCell::new(None),
}
}
/// 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()
}
/// Adds a sub-window.
///
/// Repaints: the sub-window frame is painted from the list this grows.
pub fn add_sub_window(&mut self, widget: ObjectId, geometry: Rect) -> usize {
let mut subwindow = MdiSubWindow::new(widget, geometry);
subwindow.z_order = self.subwindows.len() as i32;
subwindow.title_sink = Some(self.subwindow_title_changed.clone());
subwindow.title_dirty = Some(Rc::clone(&self.title_dirty));
self.base.add_child_linked(widget);
self.subwindows.push(subwindow);
let index = self.subwindows.len().saturating_sub(1);
if self.active_subwindow.is_none() {
self.active_subwindow = Some(index);
self.record_activation(widget);
self.subwindow_activated.emit(widget);
}
self.base.request_redraw();
index
}
/// Removes a sub-window.
///
/// Repaints: removing a frame changes the painted picture. The active index is
/// repaired in both directions — decremented when an earlier sub-window was removed
/// (so activation stays on the same window), and re-pointed when the active one
/// itself was removed.
pub fn remove_sub_window(&mut self, widget: ObjectId) {
if let Some(index) = self.subwindows.iter().position(|sw| sw.widget == widget) {
self.base.remove_child_linked(widget);
// A closed sub-window is no longer part of the area; clear its visibility so it
// cannot paint over the remaining frames.
crate::set_widget_visible(widget, false);
self.subwindows.remove(index);
self.activation_history.retain(|id| *id != widget);
if self.resize_drag.is_some_and(|drag| drag.index == index) {
self.resize_drag = None;
}
if self.maximized_restore.is_some_and(|(maximized, _)| maximized == index) {
self.maximized_restore = None;
} else if let Some((maximized, _)) = self.maximized_restore {
if maximized > index {
self.maximized_restore =
self.maximized_restore.map(|(maximized, rect)| (maximized - 1, rect));
}
}
if self.active_subwindow == Some(index) {
self.active_subwindow = None;
// Try to activate another sub-window
if !self.subwindows.is_empty() {
let new_index = index.min(self.subwindows.len() - 1);
self.active_subwindow = Some(new_index);
self.subwindow_activated.emit(self.subwindows[new_index].widget);
}
} else if let Some(active_index) = self.active_subwindow {
if active_index > index {
self.active_subwindow = Some(active_index - 1);
}
}
self.base.request_redraw();
}
}
/// Returns number of sub-windows.
pub fn sub_window_count(&self) -> usize {
self.subwindows.len()
}
/// Closes the active sub-window, returning whether one was closed.
///
/// # What "close" means here
///
/// The area owns the *frame*, not the host's document: closing removes the sub-window from
/// this collection, unlinks its child (both directions) and hides it, exactly as
/// [`Self::remove_sub_window`] does. The activation the removal implies is announced through
/// [`Self::subwindow_activated`], so a host that mirrors the selection is told which window is
/// now in front.
pub fn close_active_sub_window(&mut self) -> bool {
let Some(active) = self.active_sub_window() else {
return false;
};
self.remove_sub_window(active);
true
}
/// Closes every sub-window, returning how many were closed.
///
/// The ids are collected before anything is removed, so the walk is not mutating the list it
/// iterates and the activation repairs `remove_sub_window` performs still run once per
/// window.
pub fn close_all_sub_windows(&mut self) -> usize {
let ids: Vec<ObjectId> = self.subwindows.iter().map(|subwindow| subwindow.widget).collect();
let count = ids.len();
for widget in ids {
self.remove_sub_window(widget);
}
count
}
/// Replaces the title of the sub-window hosting `widget`.
///
/// Returns `false` when no sub-window hosts `widget`, so a caller cannot mistake a dropped
/// write for a successful one. On a real change the area repaints and emits
/// [`Self::subwindow_title_changed`] with the new text — a title is painted in the title bar,
/// so a silently assigned field would be invisible until some unrelated repaint happened.
pub fn set_sub_window_title(&mut self, widget: ObjectId, title: String) -> bool {
let Some(subwindow) = self.subwindows.iter_mut().find(|sw| sw.widget == widget) else {
return false;
};
if subwindow.title == title {
return true;
}
// The record's setter announces a change through the sink it was given. Here the area is
// the caller, so it suppresses that path and emits once itself, keeping one announcement
// per change (and one visible emit for the capability gate).
let sink = subwindow.title_sink.take();
subwindow.set_title(title);
subwindow.title_sink = sink;
self.title_dirty.set(false);
self.base.request_redraw();
self.subwindow_title_changed.emit((widget, subwindow.title.clone()));
true
}
/// Returns active sub-window.
pub fn active_sub_window(&self) -> Option<ObjectId> {
self.active_subwindow.and_then(|index| self.subwindows.get(index).map(|sw| sw.widget))
}
/// Sets active sub-window.
pub fn set_active_sub_window(&mut self, widget: ObjectId) {
if let Some(index) = self.subwindows.iter().position(|sw| sw.widget == widget) {
if self.active_subwindow != Some(index) {
self.active_subwindow = Some(index);
self.record_activation(widget);
self.subwindow_activated.emit(widget);
self.base.request_redraw();
}
}
}
/// Records `widget` as the most recently activated sub-window.
///
/// A repeated activation moves the id to the end rather than adding a duplicate, so the
/// history reads as an ordering of windows rather than as a click log.
fn record_activation(&mut self, widget: ObjectId) {
self.activation_history.retain(|id| *id != widget);
self.activation_history.push(widget);
}
/// Returns sub-window at index.
pub fn sub_window(&self, index: usize) -> Option<&MdiSubWindow> {
self.subwindows.get(index)
}
/// Returns mutable sub-window at index.
pub fn sub_window_mut(&mut self, index: usize) -> Option<&mut MdiSubWindow> {
self.subwindows.get_mut(index)
}
/// Assigns typed icon content to the sub-window owning `widget`.
///
/// A sub-window is identified by the child `ObjectId` it hosts, which is the stable identity this
/// collection already has, so no second selection model is introduced. A missing or duplicated
/// child is reported instead of silently targeting the wrong window.
pub fn set_item_icon_source(
&mut self,
widget: ObjectId,
source: Option<IconSource>,
) -> Result<(), crate::widget::IconItemError> {
let mut found: Option<usize> = None;
for (index, subwindow) in self.subwindows.iter().enumerate() {
if subwindow.widget == widget {
if found.is_some() {
return Err(crate::widget::IconItemError::AmbiguousItemId { id: widget });
}
found = Some(index);
}
}
match found {
Some(index) => {
self.subwindows[index].set_icon_source(source);
self.base.request_redraw();
Ok(())
}
None => Err(crate::widget::IconItemError::ItemNotFound { id: widget }),
}
}
/// Returns view mode.
pub fn view_mode(&self) -> ViewMode {
self.view_mode
}
/// Sets view mode.
pub fn set_view_mode(&mut self, mode: ViewMode) {
if self.view_mode == mode {
return;
}
self.view_mode = mode;
self.base.request_redraw();
}
/// The box the tab strip occupies in [`ViewMode::TabbedView`].
///
/// One derivation, read by the paint *and* by [`Self::tab_at_point`], because a strip whose
/// clickable tabs are not the tabs it drew is the same class of defect the row height had in
/// `list_view` -- two independent derivations that drift.
fn tab_strip_rect(&self) -> Rect {
let rect = self.geometry();
Rect::new(
rect.x,
rect.y,
rect.width,
TAB_STRIP_HEIGHT.min(rect.height as i32).max(0) as u32,
)
}
/// Tab `index`'s own box, or `None` when it is past the strip's trailing edge.
///
/// Tabs are laid out end to end from the strip's leading edge at an equal width, capped at
/// [`TAB_MAX_WIDTH`]: a strip holding two tabs must not stretch each across half the work area,
/// because tabs and a toolbar are read differently. A tab that would cross the trailing edge is
/// not returned at all rather than returned truncated -- a half-drawn tab reads as a rendering
/// error, and the caller can draw an overflow affordance instead.
fn tab_rect(&self, index: usize) -> Option<Rect> {
let count = self.subwindows.len();
if count == 0 || index >= count {
return None;
}
let strip = self.tab_strip_rect();
let width = (strip.width / count as u32).clamp(1, TAB_MAX_WIDTH);
let x = strip.x + (width as usize * index) as i32;
if x + width as i32 > strip.x + strip.width as i32 {
return None;
}
Some(Rect::new(x, strip.y, width, strip.height))
}
/// The tab index a point falls on, if it falls on a drawn tab.
///
/// The inverse of [`Self::tab_rect`], and deliberately built on it for the same reason
/// `list_view` builds its own inverse on its forward geometry.
fn tab_at_point(&self, point: Point) -> Option<usize> {
let strip = self.tab_strip_rect();
if !strip.contains_point(point) {
return None;
}
(0..self.subwindows.len())
.find(|index| self.tab_rect(*index).is_some_and(|tab| tab.contains_point(point)))
}
/// Where sub-window `index` is placed under the current [`ViewMode`].
///
/// # Why one struct rather than a branch in the paint
///
/// The two modes differ in **two** things: the frame's box and whether the frame draws its own
/// title bar. A paint that read `self.view_mode` at each of those places would be two chances to
/// update one of them, and the symptom would be a tab strip naming a window whose title bar is
/// also drawn -- or, worse, a click that activates a tab whose body is not the one on screen.
/// Resolving the mode once and reading the result everywhere is what makes that impossible.
///
/// # The modes
///
/// | mode | frame | title bar |
/// |---|---|---|
/// | `SubWindowView` | the sub-window's own stored geometry | drawn |
/// | `TabbedView` | the work area below the tab strip | not drawn (the tab names it) |
///
/// # Why a non-active sub-window has no placement in `TabbedView`
///
/// The mode shows exactly one window, and which one is [`Self::active_sub_window`]'s answer.
/// Handing every sub-window the whole body would paint them all on top of each other, and the
/// last one drawn -- not the active one -- would be the one the user sees.
fn subwindow_placement(&self, index: usize) -> Option<SubwindowPlacement> {
let subwindow = self.subwindows.get(index)?;
match self.view_mode {
ViewMode::SubWindowView => {
// A maximized window owns the whole area, so the viewport offset does not
// move it; every other frame is drawn at its stored position minus the pan.
let frame = if subwindow.maximized {
subwindow.geometry
} else {
self.view_frame(subwindow.geometry)
};
Some(SubwindowPlacement { frame, title_bar: true })
}
ViewMode::TabbedView => {
if self.active_subwindow != Some(index) {
return None;
}
let rect = self.geometry();
let strip = self.tab_strip_rect();
// The body is the work area below the strip. Both edges are taken from the same
// `geometry()` the strip was, so a strip clamped to a short area cannot leave the
// body starting *above* the strip's bottom edge.
let top = strip.y + strip.height as i32;
Some(SubwindowPlacement {
frame: Rect::new(
rect.x,
top,
rect.width,
(rect.y + rect.height as i32 - top).max(0) as u32,
),
title_bar: false,
})
}
}
}
/// Returns background.
pub fn background(&self) -> Background {
self.background
}
/// The viewport pan applied to sub-window frames in `SubWindowView`.
pub fn scroll_offset(&self) -> Point {
self.scroll_offset
}
/// Sets the viewport pan.
///
/// The stored geometries do not move: only the frames the painter and the hit test see
/// are translated, so a pan is reversible and a window dragged while panned still lands
/// where the pointer puts it.
pub fn set_scroll_offset(&mut self, offset: Point) {
if self.scroll_offset == offset {
return;
}
self.scroll_offset = offset;
self.base.request_redraw();
}
/// Pans the work area by `(dx, dy)`.
pub fn scroll_by(&mut self, dx: i32, dy: i32) {
self.set_scroll_offset(Point::new(
self.scroll_offset.x.saturating_add(dx),
self.scroll_offset.y.saturating_add(dy),
));
}
/// Translates a stored frame into the panned viewport.
fn view_frame(&self, frame: Rect) -> Rect {
Rect::new(
frame.x.saturating_sub(self.scroll_offset.x),
frame.y.saturating_sub(self.scroll_offset.y),
frame.width,
frame.height,
)
}
/// Sets background.
pub fn set_background(&mut self, background: Background) {
self.background = background;
self.base.request_redraw();
}
/// Returns activation order.
pub fn activation_order(&self) -> ActivationOrder {
self.activation_order
}
/// Sets activation order.
pub fn set_activation_order(&mut self, order: ActivationOrder) {
if self.activation_order == order {
return;
}
self.activation_order = order;
self.base.request_redraw();
}
/// Cascade sub-windows.
pub fn cascade_sub_windows(&mut self) {
let area_rect = self.geometry();
let count = self.subwindows.len();
if count == 0 {
return;
}
let offset = 30;
let max_width =
(area_rect.width as f32 - offset as f32 * (count as f32 - 1.0)).max(0.0) as u32;
let max_height =
(area_rect.height as f32 - offset as f32 * (count as f32 - 1.0)).max(0.0) as u32;
for (i, subwindow) in self.subwindows.iter_mut().enumerate() {
let x = area_rect.x as f32 + offset as f32 * i as f32;
let y = area_rect.y as f32 + offset as f32 * i as f32;
subwindow.geometry = Rect::new(x as i32, y as i32, max_width, max_height);
}
}
/// Tile sub-windows.
pub fn tile_sub_windows(&mut self) {
let area_rect = self.geometry();
let count = self.subwindows.len();
if count == 0 {
return;
}
let cols = (count as f32).sqrt().ceil() as usize;
let rows = (count as f32 / cols as f32).ceil() as usize;
let cell_width = (area_rect.width as f32 / cols as f32).max(0.0) as u32;
let cell_height = (area_rect.height as f32 / rows as f32).max(0.0) as u32;
for (i, subwindow) in self.subwindows.iter_mut().enumerate() {
let col = i % cols;
let row = i / cols;
let x = area_rect.x as f32 + cell_width as f32 * col as f32;
let y = area_rect.y as f32 + cell_height as f32 * row as f32;
subwindow.geometry = Rect::new(x as i32, y as i32, cell_width, cell_height);
}
}
/// Arranges minimized sub-windows.
pub fn arrange_icons(&mut self) {
let area_rect = self.geometry();
let mut minimized: Vec<_> = self.subwindows.iter_mut().filter(|sw| sw.minimized).collect();
let count = minimized.len();
if count == 0 {
return;
}
let icon_width = 100;
let icon_height = 80;
let spacing = 10;
let cols = ((area_rect.width as f32 - spacing as f32)
/ (icon_width as f32 + spacing as f32))
.floor()
.max(1.0) as usize;
let _rows = count.div_ceil(cols);
for (i, subwindow) in minimized.iter_mut().enumerate() {
let col = i % cols;
let row = i / cols;
let x = area_rect.x + spacing + (icon_width as i32 + spacing) * col as i32;
let y = area_rect.y + area_rect.height as i32
- icon_height
- spacing
- (icon_height + spacing) * row as i32;
subwindow.geometry = Rect::new(x, y, icon_width, icon_height as u32);
}
}
/// Activates next sub-window.
///
/// The order traversed is [`Self::activation_order`], not the storage order: `CreationOrder`
/// walks the windows in the order they were added, `StackingOrder` from bottom to top by
/// `z_order`, and `HistoryOrder` from the most recently activated window backwards through the
/// activation history. Before this the field was stored and reported but every traversal used
/// the array index, so the enum named a behaviour nothing performed.
pub fn activate_next_sub_window(&mut self) {
let cycle = self.activation_cycle();
if cycle.is_empty() {
return;
}
let current = self.active_subwindow.unwrap_or(cycle[0]);
let position = cycle.iter().position(|index| *index == current).unwrap_or(0);
let next = cycle[(position + 1) % cycle.len()];
self.set_active_sub_window(self.subwindows[next].widget);
}
/// Activates previous sub-window.
///
/// Walks the same [`Self::activation_order`] cycle as [`Self::activate_next_sub_window`] in the
/// opposite direction. Under `HistoryOrder` activating a window moves it to the head of the
/// cycle, so the two are not strict inverses of each other: previous from the most recently
/// activated window wraps to the oldest entry.
pub fn activate_previous_sub_window(&mut self) {
let cycle = self.activation_cycle();
if cycle.is_empty() {
return;
}
let current = self.active_subwindow.unwrap_or(cycle[0]);
let position = cycle.iter().position(|index| *index == current).unwrap_or(0);
let previous = cycle[(position + cycle.len() - 1) % cycle.len()];
self.set_active_sub_window(self.subwindows[previous].widget);
}
/// The sub-window indices in the order activation traverses them.
fn activation_cycle(&self) -> Vec<usize> {
match self.activation_order {
ActivationOrder::CreationOrder => (0..self.subwindows.len()).collect(),
ActivationOrder::StackingOrder => {
let mut cycle: Vec<usize> = (0..self.subwindows.len()).collect();
cycle.sort_by_key(|index| (self.subwindows[*index].z_order, *index));
cycle
}
ActivationOrder::HistoryOrder => {
// Most recent first: the head is the window the user just left, which is what
// "next" should return -- an activation history that walked oldest-first would
// answer the *least* recently used window, the opposite of the name.
self.activation_history
.iter()
.rev()
.filter_map(|id| self.subwindows.iter().position(|sw| sw.widget == *id))
.collect()
}
}
}
/// Maximises or restores a sub-window, returning whether anything changed.
///
/// A maximized sub-window fills the area and remembers the rectangle it came from, so a
/// second toggle puts it back where it was. This is what a title-bar double-click calls; making
/// it public gives a host the same transition without synthesising pointer events.
pub fn toggle_maximized_sub_window(&mut self, widget: ObjectId) -> bool {
let Some(index) = self.subwindows.iter().position(|sw| sw.widget == widget) else {
return false;
};
if self.subwindows[index].maximized {
self.subwindows[index].maximized = false;
if let Some((maximized, rect)) = self.maximized_restore.take() {
if maximized == index {
self.subwindows[index].geometry = rect;
} else {
self.maximized_restore = Some((maximized, rect));
}
}
} else {
let previous = self.subwindows[index].geometry;
self.maximized_restore = Some((index, previous));
self.subwindows[index].maximized = true;
self.subwindows[index].geometry = self.geometry();
}
self.set_active_sub_window(widget);
self.base.request_redraw();
true
}
/// Minimizes or restores sub-window `widget` (C-15).
///
/// Returns `false` for a widget this area does not hold. Minimizing clears the maximized flag
/// (a window cannot be both) and re-arranges the minimized icons, so the window takes its icon
/// slot immediately. Restoring raises the window again. The change is announced through
/// [`Self::subwindow_minimized_changed`].
pub fn set_sub_window_minimized(&mut self, widget: ObjectId, minimized: bool) -> bool {
let Some(index) = self.subwindows.iter().position(|sw| sw.widget == widget) else {
return false;
};
if self.subwindows[index].minimized == minimized {
return true;
}
self.subwindows[index].minimized = minimized;
if minimized {
self.subwindows[index].maximized = false;
// A minimized window that owned the maximized restore slot must not hand its stored
// rectangle back when another window is restored.
if matches!(self.maximized_restore, Some((maximized, _)) if maximized == index) {
self.maximized_restore = None;
}
self.arrange_icons();
} else {
self.set_active_sub_window(widget);
}
self.subwindow_minimized_changed.emit((widget, minimized));
self.base.request_redraw();
true
}
/// Whether sub-window `widget` is minimized.
pub fn is_sub_window_minimized(&self, widget: ObjectId) -> bool {
self.subwindows.iter().find(|sw| sw.widget == widget).is_some_and(|sw| sw.minimized)
}
/// The sub-window whose title bar contains `pos`, topmost first.
///
/// Only the sub-window presentation draws title bars; in `TabbedView` the strip names the
/// windows, so there is no bar to double-click.
fn title_bar_at(&self, pos: Point) -> Option<usize> {
if self.view_mode != ViewMode::SubWindowView {
return None;
}
self.z_order_indices().iter().rev().copied().find(|index| {
let frame = self.view_frame(self.subwindows[*index].geometry);
let bar = Rect::new(
frame.x,
frame.y,
frame.width,
TITLE_BAR_HEIGHT.min(frame.height as i32).max(0) as u32,
);
bar.contains_point(pos)
})
}
/// The sub-window indices in paint order — lowest z-order first — cached (C-P08).
///
/// The z-order changes only when a window is added, removed or raised, yet the hit chain
/// (`close_button_at`, `resize_edges_at`, `sub_window_at_position`) and the painter each built
/// and sorted the index list on every pointer event and every frame. One cached list feeds all
/// four, so the cost is paid on a real order change only.
fn z_order_indices(&self) -> Rc<Vec<usize>> {
use std::hash::{Hash, Hasher};
let mut hasher = std::collections::hash_map::DefaultHasher::new();
for subwindow in &self.subwindows {
subwindow.z_order.hash(&mut hasher);
subwindow.widget.hash(&mut hasher);
}
let fingerprint = hasher.finish();
if let Some((cached, indices)) = self.z_cache.borrow().as_ref() {
if *cached == fingerprint {
return Rc::clone(indices);
}
}
let mut indices: Vec<usize> = (0..self.subwindows.len()).collect();
indices.sort_by_key(|&index| self.subwindows[index].z_order);
let indices = Rc::new(indices);
*self.z_cache.borrow_mut() = Some((fingerprint, Rc::clone(&indices)));
indices
}
/// The topmost resizable sub-window edge band under `pos`, if any.
fn resize_edges_at(&self, pos: Point) -> Option<(usize, ResizeEdges)> {
if self.view_mode != ViewMode::SubWindowView {
return None;
}
for index in self.z_order_indices().iter().rev().copied() {
let subwindow = &self.subwindows[index];
if !subwindow.resizable || subwindow.maximized || subwindow.minimized {
continue;
}
let frame = self.view_frame(subwindow.geometry);
if !frame.contains_point(pos) {
continue;
}
let right = frame.x + frame.width as i32;
let bottom = frame.y + frame.height as i32;
let edges = ResizeEdges {
left: pos.x - frame.x < RESIZE_BAND,
right: right - pos.x <= RESIZE_BAND,
top: pos.y - frame.y < RESIZE_BAND,
bottom: bottom - pos.y <= RESIZE_BAND,
};
// The topmost window owns the point, so a press that is not on its edge is not a
// resize of a window *below* it either.
return edges.any().then_some((index, edges));
}
None
}
/// Moves the edges of the sub-window being resized to follow `pos`.
fn drag_resize(&mut self, pos: Point) {
let Some(drag) = self.resize_drag else {
return;
};
let Some(subwindow) = self.subwindows.get_mut(drag.index) else {
self.resize_drag = None;
return;
};
let dx = pos.x - drag.origin.x;
let dy = pos.y - drag.origin.y;
let mut x = drag.geometry.x;
let mut y = drag.geometry.y;
let mut width = drag.geometry.width as i32;
let mut height = drag.geometry.height as i32;
if drag.edges.left {
let candidate = width - dx;
if candidate >= MIN_SUBWINDOW_WIDTH as i32 {
x = drag.geometry.x + dx;
width = candidate;
}
}
if drag.edges.right {
width = (width + dx).max(MIN_SUBWINDOW_WIDTH as i32);
}
if drag.edges.top {
let candidate = height - dy;
if candidate >= MIN_SUBWINDOW_HEIGHT as i32 {
y = drag.geometry.y + dy;
height = candidate;
}
}
if drag.edges.bottom {
height = (height + dy).max(MIN_SUBWINDOW_HEIGHT as i32);
}
subwindow.geometry = Rect::new(x, y, width as u32, height as u32);
self.base.request_redraw();
}
/// Moves the sub-window whose title bar is being dragged (C-14).
///
/// The delta is measured from the press position against the geometry snapshot, so the move is
/// absolute and a re-delivered pointer sample lands in the same place. The window is clamped so
/// at least a title bar's worth stays inside the area: a window dragged fully off the desktop
/// would be unreachable with the pointer.
fn drag_move(&mut self, pos: Point) {
let Some(drag) = self.move_drag else {
return;
};
let area = self.geometry();
let Some(subwindow) = self.subwindows.get_mut(drag.index) else {
self.move_drag = None;
return;
};
if subwindow.maximized {
return;
}
let dx = pos.x - drag.origin.x;
let dy = pos.y - drag.origin.y;
let keep = TITLE_BAR_HEIGHT;
let min_x = area.x - drag.geometry.width as i32 + keep;
let max_x = area.x + area.width as i32 - keep;
let min_y = area.y;
let max_y = area.y + area.height as i32 - keep;
let raw_x = drag.geometry.x.saturating_add(dx);
let raw_y = drag.geometry.y.saturating_add(dy);
let x = if min_x <= max_x { raw_x.clamp(min_x, max_x) } else { min_x };
let y = if min_y <= max_y { raw_y.clamp(min_y, max_y) } else { min_y };
subwindow.geometry = Rect::new(x, y, drag.geometry.width, drag.geometry.height);
self.base.request_redraw();
}
/// The sub-window whose minimize/restore button is under `pos` (C-15), topmost first.
fn minimize_button_at(&self, pos: Point) -> Option<usize> {
for index in self.z_order_indices().iter().rev().copied() {
let Some(placement) = self.subwindow_placement(index) else {
continue;
};
if !placement.title_bar {
continue;
}
if minimize_button_rect(placement.frame).contains_point(pos) {
return Some(index);
}
}
None
}
/// Reorders the tab strip when the carried tab is dragged onto another slot (C-17).
///
/// The sub-window records move within [`Self::subwindows`]; their `z_order` values travel with
/// them, so stacking and activation relations are preserved. The selection is re-resolved by
/// widget id, and the index-keyed maximized-restore slot is re-resolved the same way.
fn drag_tab_to(&mut self, pos: Point) {
let Some((from, origin)) = self.tab_drag else {
return;
};
let Some(to) = self.tab_at_point(pos) else {
return;
};
if from == to || from >= self.subwindows.len() || to >= self.subwindows.len() {
return;
}
let active_widget = self
.active_subwindow
.and_then(|index| self.subwindows.get(index))
.map(|subwindow| subwindow.widget);
let maximized_widget = self
.maximized_restore
.and_then(|(index, _)| self.subwindows.get(index))
.map(|subwindow| subwindow.widget);
let maximized_rect = self.maximized_restore.map(|(_, rect)| rect);
let moved = self.subwindows.remove(from);
self.subwindows.insert(to, moved);
self.active_subwindow = active_widget
.and_then(|widget| self.subwindows.iter().position(|sw| sw.widget == widget));
self.maximized_restore = maximized_widget.and_then(|widget| {
let index = self.subwindows.iter().position(|sw| sw.widget == widget)?;
Some((index, maximized_rect?))
});
self.tab_drag = Some((to, origin));
self.subwindow_tab_moved.emit((from, to));
self.base.request_redraw();
}
/// The index of the sub-window whose close button is under `pos`, topmost first.
///
/// The button is chrome of a *frame*, so the test walks the frames in paint order (highest
/// z-order first) exactly as [`Self::sub_window_at_position`] does, and skips a sub-window
/// whose frame draws no title bar (a `TabbedView` frame names itself through the tab strip).
fn close_button_at(&self, pos: Point) -> Option<usize> {
for index in self.z_order_indices().iter().rev().copied() {
if !self.subwindows[index].closable {
continue;
}
let Some(placement) = self.subwindow_placement(index) else {
continue;
};
if !placement.title_bar {
continue;
}
if close_button_rect(placement.frame).contains_point(pos) {
return Some(index);
}
}
None
}
/// Returns sub-window at position.
fn sub_window_at_position(&self, pos: Point) -> Option<usize> {
// Check from top (highest z-order) to bottom
for index in self.z_order_indices().iter().rev().copied() {
let subwindow = &self.subwindows[index];
if self.view_frame(subwindow.geometry).contains(pos) {
return Some(index);
}
}
None
}
}
// Implement Widget trait
impl Widget for MdiArea {
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(
crate::widget::metrics::dimensions::PANEL_DEFAULT_WIDTH,
crate::widget::metrics::dimensions::PANEL_DEFAULT_HEIGHT,
)
}
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.
///
/// # Payload mapping
///
/// A hub name carries no value, so a `Signal1<T>` needs a conversion. The one used here is the
/// same spelling the property side uses, so a designer reads one representation per kind: a
/// number as `Int`, a float as `Float`, a flag as `Bool`, text as `String`, and a structured
/// payload as its debug spelling. A `None` maps to `Null`, which is the same value an absent
/// optional property uses — so "no selection" is distinguishable from "selection 0".
fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
use crate::signal::EventSignalRef;
match name {
"subwindow_activated" => Some(EventSignalRef::mapped(
"subwindow_activated",
&self.subwindow_activated,
|v| CapabilityValue::UInt(*v),
)),
"subwindow_title_changed" => Some(EventSignalRef::mapped(
"subwindow_title_changed",
&self.subwindow_title_changed,
|v| {
CapabilityValue::Tuple(crate::compat::Vec::from([
CapabilityValue::UInt(v.0),
CapabilityValue::String(v.1.clone()),
]))
},
)),
"subwindow_minimized_changed" => Some(EventSignalRef::mapped(
"subwindow_minimized_changed",
&self.subwindow_minimized_changed,
// `(id, minimized)` — an object id travels as `UInt`, the flag as `Bool`.
|v| {
CapabilityValue::Tuple(crate::compat::Vec::from([
CapabilityValue::UInt(v.0),
CapabilityValue::Bool(v.1),
]))
},
)),
"subwindow_tab_moved" => Some(EventSignalRef::mapped(
"subwindow_tab_moved",
&self.subwindow_tab_moved,
|v| {
CapabilityValue::Tuple(crate::compat::Vec::from([
CapabilityValue::UInt(v.0 as u64),
CapabilityValue::UInt(v.1 as u64),
]))
},
)),
_ => None,
}
}
}
/// `MdiArea`'s property contract.
///
/// `view_mode` uses this module's own [`ViewMode`] (sub-windows vs. tabs), not
/// `list_view::ViewMode`; the published tokens are the two this widget's reader
/// produced and nothing else.
impl WidgetProperties for MdiArea {
fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
match name {
"subwindow_count" => Ok(CapabilityValue::UInt(self.sub_window_count() as u64)),
"active_subwindow" => match self.active_sub_window() {
Some(id) => Ok(CapabilityValue::UInt(id)),
None => Ok(CapabilityValue::Null),
},
"view_mode" => {
let token = match self.view_mode() {
ViewMode::SubWindowView => "sub_window_view",
ViewMode::TabbedView => "tabbed",
};
Ok(CapabilityValue::String(token.to_string()))
}
_ => base_property_get(self, name),
}
}
fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
match name {
// All three are read-only in the old dispatch: the reader serves them
// and the writer has no arm, so the contract says so explicitly.
"subwindow_count" | "active_subwindow" | "view_mode" => {
Err(CapabilityAccessError::ReadOnlyProperty)
}
_ => base_property_set(self, name, value),
}
}
/// Returns the names this control serves through the property contract.
///
/// Only what `get` answers: `MDI_AREA_PROPERTIES` marks all three
/// non-readable, and the old reader nevertheless served all three, so the
/// readable set — not the schema flag — is what callers can actually use.
fn property_names(&self) -> &'static [&'static str] {
property_names_of!["subwindow_count", "active_subwindow", "view_mode", BASE_PROPERTY_NAMES]
}
/// Runs one of the commands `mdi_area` publishes.
///
/// All three address a sub-window by its `ObjectId` (or, for `add_subwindow`, a
/// `Rect` alongside it), so none can run on the bare name: there is no sub-window
/// the control could pick that the caller did not name, and activating an
/// arbitrary one would move focus the caller never asked to move. They are all
/// [`CapabilityAccessError::OutOfRange`] — valid names whose arguments are what
/// is missing. `set_view_mode` is a payload-carrying write published by the same
/// capability and is refused the same way; an override that named only the
/// non-`set_` commands dropped the trait default's `set_` acceptance, which is
/// what the final arm restores.
fn command(&mut self, name: &str) -> Result<(), CapabilityAccessError> {
match name {
"add_subwindow" | "remove_subwindow" | "activate_subwindow" => {
Err(CapabilityAccessError::OutOfRange)
}
_ if name.starts_with("set_") => Err(CapabilityAccessError::OutOfRange),
_ => Err(CapabilityAccessError::UnknownCommand),
}
}
}
impl EventHandler for MdiArea {
/// Sub-window activation and event forwarding stop while the area is disabled.
///
/// Without the guard a disabled MDI area still raised whichever sub-window was clicked and
/// forwarded the event into it, so `set_enabled(false)` did not take the workspace out of play.
fn handle_event(&mut self, event: &Event) {
self.base.handle_event(event);
if !self.base.is_enabled() {
return;
}
let mut hit_subwindow = false;
let mut gesture_consumed = false;
if let Event::MousePress { pos, button, .. } = event {
if *button == 1 {
// In `TabbedView` the tabs are the only clickable chrome, because the windows they
// name carry no title bar of their own and all sit in the same body. Resolving the
// tab first is what makes the strip interactive rather than decorative -- a mode
// whose tabs cannot be clicked is the dead state this whole change closes.
if self.view_mode == ViewMode::TabbedView {
if let Some(index) = self.tab_at_point(*pos) {
hit_subwindow = true;
self.set_active_sub_window(self.subwindows[index].widget);
// C-17: a press on a tab also arms a reorder. The reorder only begins once
// the pointer crosses the drag threshold, so selecting a tab stays a click.
self.tab_drag = (self.subwindows.len() > 1).then_some((index, *pos));
}
} else if let Some(index) = self.close_button_at(*pos) {
// The ✕ closes the window it belongs to — which is what the glyph says and
// what `remove_sub_window` (the area's own public close path) does. The press
// is *consumed* here: `hit_subwindow` stays false so it is neither
// re-targeted at the window it just closed nor at whichever window the
// removal activated.
let widget = self.subwindows[index].widget;
self.remove_sub_window(widget);
} else if let Some(index) = self.minimize_button_at(*pos) {
// C-15: the title-bar button next to ✕ toggles minimize/restore.
let widget = self.subwindows[index].widget;
let minimized = !self.subwindows[index].minimized;
self.set_sub_window_minimized(widget, minimized);
} else if let Some((index, edges)) = self.resize_edges_at(*pos) {
// Grabbing a frame edge is the area's gesture, not the child's: it is the
// frame's geometry being changed, so the press must not reach the content.
let widget = self.subwindows[index].widget;
self.set_active_sub_window(widget);
self.resize_drag = Some(SubwindowResize {
index,
edges,
origin: *pos,
geometry: self.subwindows[index].geometry,
});
self.base.set_mouse_pressed(true);
gesture_consumed = true;
} else if let Some(index) = self.sub_window_at_position(*pos) {
hit_subwindow = true;
self.set_active_sub_window(self.subwindows[index].widget);
// C-14: a press on the title bar (and not on either button, which are tested
// above) starts a move. A maximized window has no meaningful frame to move —
// it fills the area — so it is left out.
let frame = self.view_frame(self.subwindows[index].geometry);
let bar = Rect::new(
frame.x,
frame.y,
frame.width,
TITLE_BAR_HEIGHT.min(frame.height as i32).max(0) as u32,
);
if self.subwindows[index].movable
&& !self.subwindows[index].maximized
&& bar.contains_point(*pos)
{
self.move_drag = Some(SubwindowMove {
index,
origin: *pos,
geometry: self.subwindows[index].geometry,
});
self.base.set_mouse_pressed(true);
}
}
}
} else if let Event::MouseDoubleClick { pos, button } = event {
if *button == 1 {
if let Some(index) = self.title_bar_at(*pos) {
let widget = self.subwindows[index].widget;
self.toggle_maximized_sub_window(widget);
gesture_consumed = true;
}
}
} else if let Event::MouseMove { pos } = event {
if self.resize_drag.is_some() {
self.drag_resize(*pos);
gesture_consumed = true;
} else if self.move_drag.is_some() {
self.drag_move(*pos);
gesture_consumed = true;
} else if let Some((_, origin)) = self.tab_drag {
// C-17: cross the drag threshold before reordering, so a click stays a click.
if (pos.x - origin.x).abs() > 4 || (pos.y - origin.y).abs() > 4 {
self.drag_tab_to(*pos);
gesture_consumed = true;
}
}
} else if let Event::MouseRelease { button, .. } = event {
if *button == 1 {
if self.resize_drag.take().is_some() {
self.base.set_mouse_pressed(false);
gesture_consumed = true;
}
if self.move_drag.take().is_some() {
self.base.set_mouse_pressed(false);
gesture_consumed = true;
}
if self.tab_drag.take().is_some() {
gesture_consumed = true;
}
}
} else if let Event::Wheel { delta, .. } = event {
// C-16: the wheel pans the work area, the same viewport offset the public `scroll_by`
// drives. `TabbedView` shows one page with no desktop to pan, so it is ignored there.
if self.view_mode == ViewMode::SubWindowView {
self.scroll_by(
-delta.x.saturating_mul(WHEEL_PAN_STEP),
-delta.y.saturating_mul(WHEEL_PAN_STEP),
);
gesture_consumed = true;
}
}
let should_forward = if gesture_consumed {
false
} else {
!matches!(event, Event::MousePress { .. }) || hit_subwindow
};
// Forward background-independent events, or pointer events that hit a subwindow.
if should_forward {
if let Some(widget_id) = self.active_sub_window() {
if let Some(ref reg) = self.registry {
reg.borrow_mut().forward_event(widget_id, event);
}
}
}
}
}
/// Side of a sub-window's close button, in logical pixels.
///
/// A constant so the title's reserved width and the button's own geometry are derived from
/// one number rather than two literals that have to be kept in step.
const CLOSE_SIZE: i32 = 12;
/// Width reserved at the trailing edge of a sub-window's title bar for the close button
/// and its margins. Used to bound the title so the two cannot overlap.
const CLOSE_SIZE_SUM: u32 = 22;
/// Inset of the close button from the frame's trailing edge, in logical pixels.
///
/// `CLOSE_SIZE_SUM` is `CLOSE_SIZE + 2 * CLOSE_BUTTON_INSET`, so the title's reserved band and
/// the button's own box are derived from the same layout instead of two literals.
const CLOSE_BUTTON_INSET: i32 = 5;
/// The close button's box inside a sub-window's title bar.
///
/// Shared by the painter and the hit test. The ✕ was painted for every closable sub-window but
/// nothing hit-tested it, so a click on it activated the window and changed nothing — a drawn
/// affordance with no handler. Both sides now read this one derivation, so the glyph and the
/// click area cannot drift.
fn close_button_rect(frame: Rect) -> Rect {
let y = frame.y + (TITLE_BAR_HEIGHT - CLOSE_SIZE) / 2;
Rect::new(
frame.x + frame.width as i32 - CLOSE_SIZE - CLOSE_BUTTON_INSET,
y,
CLOSE_SIZE as u32,
CLOSE_SIZE as u32,
)
}
/// Side of the minimize/restore button, in logical pixels (C-15).
const MINIMIZE_SIZE: i32 = 12;
/// Pixels the work area pans per wheel notch (C-16).
const WHEEL_PAN_STEP: i32 = 24;
/// Gap between the minimize/restore button and the close button.
const MINIMIZE_GAP: i32 = 4;
/// The minimize/restore button's box inside a sub-window's title bar (C-15).
///
/// Shared by the painter and the hit test, exactly as [`close_button_rect`] is, so the glyph and
/// the click area cannot drift.
fn minimize_button_rect(frame: Rect) -> Rect {
let close = close_button_rect(frame);
Rect::new(
close.x - MINIMIZE_SIZE - MINIMIZE_GAP,
frame.y + (TITLE_BAR_HEIGHT - MINIMIZE_SIZE) / 2,
MINIMIZE_SIZE as u32,
MINIMIZE_SIZE as u32,
)
}
/// Height of a sub-window's title bar, in logical pixels.
const TITLE_BAR_HEIGHT: i32 = 24;
/// How close to an edge a press must land to begin an edge-resize drag, in logical pixels.
const RESIZE_BAND: i32 = 4;
/// The narrowest a sub-window may become through an edge drag, in logical pixels.
const MIN_SUBWINDOW_WIDTH: u32 = 60;
/// The shortest a sub-window may become through an edge drag, in logical pixels.
const MIN_SUBWINDOW_HEIGHT: u32 = 40;
/// Which edges of a sub-window an in-progress drag is moving.
#[derive(Debug, Clone, Copy, Default)]
struct ResizeEdges {
left: bool,
right: bool,
top: bool,
bottom: bool,
}
impl ResizeEdges {
/// Whether the gesture moves any edge at all.
fn any(self) -> bool {
self.left || self.right || self.top || self.bottom
}
}
/// The live edge-resize gesture.
#[derive(Debug, Clone, Copy)]
struct SubwindowResize {
index: usize,
edges: ResizeEdges,
origin: Point,
geometry: Rect,
}
/// The live title-bar move gesture (C-14).
///
/// The snapshot is the window's geometry when the press happened, so the move is absolute — a
/// replayed or coalesced pointer sample lands in the same place rather than doubling the delta.
#[derive(Clone, Copy)]
struct SubwindowMove {
index: usize,
/// The pointer position the gesture started at, in area coordinates.
origin: Point,
/// The window's stored geometry at that moment (not the panned frame).
geometry: Rect,
}
/// Square side of the leading icon slot a sub-window's title bar reserves, in logical pixels.
const SUBWINDOW_ICON_SIDE: u32 = 12;
/// Gap between that icon slot and the title text.
const SUBWINDOW_ICON_GAP: u32 = 4;
/// Inset of the title bar's content from the frame's leading edge.
const TITLE_BAR_INSET: i32 = 5;
/// The tab strip's height in `ViewMode::TabbedView`, in logical pixels.
///
/// Two pixels taller than a sub-window's own title bar: a tab carries the title *and* sits above
/// the body it names, so it is chrome twice over and reads as a strip rather than as a detached
/// title bar.
const TAB_STRIP_HEIGHT: i32 = TITLE_BAR_HEIGHT + 2;
/// The widest a single tab may grow in `ViewMode::TabbedView`, in logical pixels.
///
/// A strip of one or two tabs would otherwise stretch each to half the work area, which reads as
/// a toolbar rather than as tabs. Past this a tab stops growing and the free space is left at the
/// strip's trailing edge.
const TAB_MAX_WIDTH: u32 = 160;
/// How far a tab's label is inset from the tab's own edges, in logical pixels.
const TAB_TEXT_INSET: i32 = 6;
/// How a sub-window is *placed* under the current [`ViewMode`], independent of what it contains.
///
/// Produced by `MdiArea::subwindow_placement` and read by the paint, so the two modes cannot be
/// half-applied -- the defect `list_view`'s own `ViewMode` had, where four modes were stored,
/// published and read by nothing. See that method for what each mode does.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct SubwindowPlacement {
/// The sub-window's frame, in the work area's coordinates.
frame: Rect,
/// Whether the frame's own title bar is drawn.
///
/// False in `TabbedView`, where the tab strip names the sub-window: drawing both would name
/// the same window twice, one above the other.
title_bar: bool,
}
/// Paints a sub-window's icon into `icon_rect`, in `color`.
///
/// One function for both presentations of a sub-window — the sub-window view's title bar and the
/// tabbed view's tab — so the two cannot disagree about which active form is drawn.
fn draw_subwindow_icon(
context: &mut RenderContext,
subwindow: &MdiSubWindow,
icon_rect: Rect,
color: Color,
) {
if let Some(source) = subwindow.icon_source {
if let Err(error) = crate::widget::paint_icon(context, source.into(), icon_rect, color) {
log::warn!("sub-window icon could not be painted: {error:?}");
}
}
#[cfg(feature = "image")]
if subwindow.icon_source.is_none() {
if let Some(image) = subwindow.icon.as_ref() {
// `set_icon` normalises to RGBA8, so the whole image is borrowed here.
if let crate::image::ImageData::Rgba8(pixels) = &image.inner.data {
// The shared fit rule, as `SharedIconPixels::fit_into` uses, so every image this
// crate draws into an icon slot is fitted the same way.
let fitted = crate::widget::icon_source::fit_image_into(
image.width(),
image.height(),
icon_rect,
);
context.draw_image(fitted.x, fitted.y, fitted.width, fitted.height, pixels);
}
}
}
}
impl Draw for MdiArea {
fn draw(&mut self, context: &mut RenderContext) {
// A title write performed through a sub-window record marks this flag instead of borrowing
// the area; consuming it here schedules the frame that shows the new text.
if self.title_dirty.replace(false) {
self.base.request_redraw();
}
// Chrome colours resolve the explicit style first, then the theme's resolved style for
// this control, and only then a literal. Every colour below used to be a literal, and the
// plain background in particular was `rgb(240,240,240)` — byte-identical to the light
// frame fill, so the area painted plenty of pixels and showed none of them. The census
// reported it as both `ink = 0` and theme-blind.
//
// The theme read is a separate manager lock, taken and released inside
// `resolved_theme_style`, so it is not held across the draw — the global manager's mutex
// is not re-entrant. `ThemeSnapshot` folds the resolved style and the palette roles into
// one visit for the frame (C-P01).
let theme = ThemeSnapshot::capture("mdi_area");
let style = self.base.style();
// Read as its own lock acquisition and copied out as values, so the guard is dropped
// before anything else touches the theme.
let (window_fill, foreground, secondary, primary) = match theme.palette {
Some(active) => {
(active.background, active.foreground, active.secondary, active.primary)
}
None => (
Color::rgb(240, 240, 240),
Color::BLACK,
Color::rgb(158, 158, 158),
Color::rgb(33, 150, 243),
),
};
let ink = style
.text_color
.or_else(|| theme.style.as_ref().and_then(|t| t.text_color))
.unwrap_or(foreground);
// `mdi_area` is absent from `WidgetRole::for_kind_name`'s table, so it classifies as
// `Surface` and the active theme writes the window fill into `style.background_color`. A
// work area painted in that colour would be byte-identical to the frame behind it, so a
// resolved surface equal to the window fill is re-derived a visible step away from it,
// while a colour the caller set still wins.
let area = match style.background_color {
Some(resolved) if resolved != window_fill => resolved,
_ => window_fill.blend(&ink, 0.08),
};
let border = style
.border_color
.or_else(|| theme.style.as_ref().and_then(|t| t.border_color))
.filter(|resolved| *resolved != area)
.unwrap_or_else(|| area.blend(&secondary, 0.45));
// Draw base widget
let rect = self.geometry();
// Draw background
match self.background {
Background::NoBackground => {
// No background
}
Background::Plain => {
context.face_with_gradient(
rect,
area,
self.style().background_gradient.as_ref(),
self.style().surface.unwrap_or_default(),
self.style().border_radius.unwrap_or(0),
Color::BLACK,
);
}
Background::Gradient => {
// C-P09: one gradient face instead of one `draw_line` per pixel row. The ramp is
// the same themed pair the row loop used, described once to the renderer.
let top = area;
let bottom = area.blend(&border, 0.55);
let ramp = crate::style::Gradient::linear(
Point::new(rect.x, rect.y),
Point::new(rect.x, rect.y + rect.height as i32),
)
.with_stops(crate::compat::vec![
crate::style::GradientStop::new(0.0, top),
crate::style::GradientStop::new(1.0, bottom),
]);
context.face_with_gradient(
rect,
top,
Some(&ramp),
self.style().surface.unwrap_or_default(),
self.style().border_radius.unwrap_or(0),
Color::BLACK,
);
}
Background::Pattern => {
// C-P09: a base fill plus one line per pattern step. The old form emitted a
// `fill_rect` per 20 px cell, i.e. hundreds to thousands of commands per frame
// for an unchanging result; a weave needs `rows + columns` lines instead.
//
// The base fill comes first. The lines below truncate to whole steps, so without
// it the right and bottom remainders kept whatever the parent had painted, and an
// area narrower or shorter than one step painted *nothing*.
let pattern_size = 20;
let alt = area.blend(&ink, 0.06);
context.fill_rect(rect, area);
let bottom = rect.y + rect.height as i32;
let right = rect.x + rect.width as i32;
// Every other row and every other column of the weave, offset by half a step so
// the grid reads as a pattern rather than as a border.
let mut y = rect.y + pattern_size / 2;
while y < bottom {
context.draw_line(Point::new(rect.x, y), Point::new(right, y), alt);
y += pattern_size;
}
let mut x = rect.x + pattern_size / 2;
while x < right {
context.draw_line(Point::new(x, rect.y), Point::new(x, bottom), alt);
x += pattern_size;
}
}
}
// In `TabbedView` the tab strip is the mode's whole visible difference at rest, so it is
// drawn before the body: the active tab is the one whose window is about to be painted
// below it, and painting the strip first keeps the body's own frame from covering it.
if self.view_mode == ViewMode::TabbedView {
self.draw_tab_strip(context, &area, &border, &ink, &primary);
}
// Draw sub-windows in paint order (lowest z-order first, so the highest draws last).
// The order is cached, so a frame that did not raise a window does not re-sort (C-P08).
for index in self.z_order_indices().iter().copied() {
let Some(placement) = self.subwindow_placement(index) else {
continue;
};
let subwindow = &self.subwindows[index];
let is_active = self.active_subwindow == Some(index);
// Draw sub-window frame
let frame_rect = placement.frame;
// Draw frame background
let bg_color = if is_active { area.blend(&ink, 0.22) } else { area.blend(&ink, 0.14) };
context.fill_rect(frame_rect, bg_color);
// Draw frame border
let border_color = if is_active { primary } else { border };
context.draw_rect(frame_rect, border_color);
// Draw title bar. `TabbedView` places the window under a tab that already names it, so
// the frame draws no title bar of its own -- and, crucially, its content gets the whole
// frame rather than the frame minus a bar nobody drew.
let title_bar_height = if placement.title_bar { TITLE_BAR_HEIGHT } else { 0 };
if placement.title_bar {
let title_bar_color =
if is_active { primary } else { area.blend(&secondary, 0.55) };
context.fill_rect(
Rect::new(
frame_rect.x,
frame_rect.y,
frame_rect.width,
title_bar_height as u32,
),
title_bar_color,
);
// Draw title text. Centred on the **title bar's own line box**, not on
// `title_bar_height / 2`: the latter put the glyph box's top edge on the bar's
// middle line, so a 14 px label occupied `12..26` inside a 24 px bar and the
// descenders crossed the bottom border. The close button below already used the
// correct `(bar - size) / 2` form — one bar, two rules — so both now share this
// line box.
let title_bar_rect = Rect::new(
frame_rect.x,
frame_rect.y,
frame_rect.width,
title_bar_height as u32,
);
// The **effective font** — the resolved theme/caller font — for the subwindow title
// (D09-STYLE-01).
let title_font = effective_font(style);
let title_line = context.text_line(title_bar_rect, title_font);
let text_color = if is_active { primary.contrast_color() } else { ink };
// ── Icon ──
//
// A sub-window with an icon reserves a leading square inside the title bar, and the
// title band starts after it. The title bar is a fixed height and the text is
// left-aligned, so the slot is the only thing that moves.
let mut title_x = frame_rect.x + TITLE_BAR_INSET;
let mut title_width =
title_bar_rect.width.saturating_sub(TITLE_BAR_INSET as u32 + CLOSE_SIZE_SUM);
if subwindow.has_icon() {
let side = SUBWINDOW_ICON_SIDE.min(title_line.height.max(1));
let icon_rect = Rect {
x: frame_rect.x + TITLE_BAR_INSET,
y: title_line.y + (title_line.height.saturating_sub(side) / 2) as i32,
width: side,
height: side,
};
draw_subwindow_icon(context, subwindow, icon_rect, text_color);
title_x += (SUBWINDOW_ICON_SIDE + SUBWINDOW_ICON_GAP) as i32;
title_width =
title_width.saturating_sub(SUBWINDOW_ICON_SIDE + SUBWINDOW_ICON_GAP);
}
context.draw_text_fitted_cow(
Rect {
x: title_x,
y: title_line.y,
width: title_width,
height: title_line.height,
},
&subwindow.title,
title_font,
text_color,
HorizontalAlignment::Left,
);
// Draw close button if closable
if subwindow.closable {
// One geometry for the ✕ and for the click that dismisses it: the button used
// to be positioned from the title's line box here and not hit-tested at all.
let close = close_button_rect(frame_rect);
let (close_x, close_y) = (close.x, close.y);
let close_size = CLOSE_SIZE;
let close_color = if is_active {
primary.contrast_color()
} else {
ink.blend(&title_bar_color, 0.35)
};
context.draw_line(
Point::new(close_x, close_y),
Point::new(close_x + close_size, close_y + close_size),
close_color,
);
context.draw_line(
Point::new(close_x + close_size, close_y),
Point::new(close_x, close_y + close_size),
close_color,
);
}
// C-15: minimize/restore button, left of the close button. A minimized window
// shows a restore square; an expanded or maximized one shows the minimize rule.
{
let button = minimize_button_rect(frame_rect);
let button_color = if is_active {
primary.contrast_color()
} else {
ink.blend(&title_bar_color, 0.35)
};
if subwindow.minimized {
context.draw_rect(
Rect::new(
button.x + 2,
button.y + 2,
button.width.saturating_sub(4),
button.height.saturating_sub(4),
),
button_color,
);
} else {
context.draw_line(
Point::new(button.x + 2, button.y + button.height as i32 - 3),
Point::new(
button.x + button.width as i32 - 2,
button.y + button.height as i32 - 3,
),
button_color,
);
}
}
}
// Draw widget content via registry
let content_rect = Rect::new(
frame_rect.x,
frame_rect.y + title_bar_height,
frame_rect.width,
frame_rect.height.saturating_sub(title_bar_height as u32),
);
if let Some(ref reg) = self.registry {
reg.borrow_mut().set_widget_geometry(subwindow.widget, content_rect);
context.push_clip(
content_rect.x,
content_rect.y,
content_rect.width,
content_rect.height,
);
reg.borrow_mut().draw_widget(subwindow.widget, context);
context.pop_clip();
}
}
}
}
impl MdiArea {
/// Paints the `TabbedView` tab strip: one tab per sub-window, the active one raised.
///
/// Every tab's box comes from [`Self::tab_rect`], which is also what [`Self::tab_at_point`]
/// resolves a click through, so the tab that is highlighted is the tab a click would activate.
/// A tab past the strip's trailing edge is skipped by `tab_rect` returning `None`, and the loop
/// stops rather than drawing a truncated tab.
fn draw_tab_strip(
&self,
context: &mut RenderContext,
area: &Color,
border: &Color,
ink: &Color,
primary: &Color,
) {
let strip = self.tab_strip_rect();
context.fill_rect(strip, area.blend(ink, 0.06));
context.draw_line(
Point::new(strip.x, strip.y + strip.height as i32 - 1),
Point::new(strip.x + strip.width as i32, strip.y + strip.height as i32 - 1),
*border,
);
// The **effective font** — the resolved theme/caller font — for the tab labels
// (D09-STYLE-01).
let font = effective_font(self.style());
for index in 0..self.subwindows.len() {
let Some(tab) = self.tab_rect(index) else { break };
let is_active = self.active_subwindow == Some(index);
// The active tab is drawn in the accent and the rest in the strip's own surface, which
// is what makes "which window am I looking at" answerable without reading the title.
let fill = if is_active { *primary } else { area.blend(ink, 0.12) };
context.fill_rect(tab, fill);
context.draw_rect(tab, *border);
let text_color = if is_active { primary.contrast_color() } else { *ink };
// The label's band is the tab minus its own padding, so a long title is ellipsised by
// `draw_text_fitted` rather than running into the next tab. A tab for a window with an
// icon gives up a leading square first, so the icon and the title share the tab the same
// way they share the title bar in the sub-window view.
let inset = TAB_TEXT_INSET.min(tab.width as i32 / 2);
let mut label = Rect::new(
tab.x + inset,
tab.y,
tab.width.saturating_sub(inset as u32 * 2),
tab.height,
);
let subwindow = &self.subwindows[index];
if subwindow.has_icon() {
let line = context.text_line(label, font);
let side = SUBWINDOW_ICON_SIDE.min(line.height.max(1));
let icon_rect = Rect {
x: label.x,
y: line.y + (line.height.saturating_sub(side) / 2) as i32,
width: side,
height: side,
};
draw_subwindow_icon(context, subwindow, icon_rect, text_color);
label.x += (SUBWINDOW_ICON_SIDE + SUBWINDOW_ICON_GAP) as i32;
label.width = label.width.saturating_sub(SUBWINDOW_ICON_SIDE + SUBWINDOW_ICON_GAP);
}
context.draw_text_fitted_cow(
context.text_line(label, font),
&subwindow.title,
font,
text_color,
HorizontalAlignment::Center,
);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::{Point, Rect};
use crate::event::Event;
use crate::widget::svg::render_to_svg;
use crate::widget::IconName;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::Arc;
// ── Helper constants ──────────────────────────────────────────────────
fn widget_id_1() -> ObjectId {
9201
}
fn widget_id_2() -> ObjectId {
9202
}
fn widget_id_3() -> ObjectId {
9203
}
// ── 1. Creation defaults ──────────────────────────────────────────────
#[test]
fn a_title_written_through_the_record_is_announced_and_marks_the_area_dirty() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
let index = area.add_sub_window(widget_id_1(), Rect::new(0, 0, 200, 150));
let seen = Arc::new(std::sync::Mutex::new(Vec::new()));
area.subwindow_title_changed.connect({
let seen = Arc::clone(&seen);
move |value: Arc<(ObjectId, String)>| seen.lock().unwrap().push(value.as_ref().clone())
});
area.sub_window_mut(index).unwrap().set_title("Report".to_string());
assert_eq!(area.sub_window(index).unwrap().title(), "Report");
assert_eq!(*seen.lock().unwrap(), vec![(widget_id_1(), "Report".to_string())]);
assert!(area.title_dirty.get(), "the area must schedule a repaint");
// Writing the same text is a no-op: no second event, no second dirty mark.
area.sub_window_mut(index).unwrap().set_title("Report".to_string());
assert_eq!(seen.lock().unwrap().len(), 1);
}
#[test]
fn mdiarea_creation_defaults() {
let area = MdiArea::new(Rect::new(0, 0, 600, 400));
assert_eq!(area.sub_window_count(), 0, "should have no sub-windows");
assert_eq!(area.active_sub_window(), None, "no active sub-window");
assert_eq!(area.view_mode(), ViewMode::SubWindowView, "default view mode");
assert_eq!(area.background(), Background::Plain, "default background");
assert_eq!(
area.activation_order(),
ActivationOrder::StackingOrder,
"default activation order"
);
assert!(area.is_visible(), "should be visible");
assert!(area.is_enabled(), "should be enabled");
assert_eq!(area.geometry(), Rect::new(0, 0, 600, 400));
assert_eq!(area.kind(), WidgetKind::MdiArea);
assert!(area.registry().is_none(), "registry should be None by default");
}
// ── 2. Adding sub-windows ─────────────────────────────────────────────
#[test]
fn mdiarea_add_sub_window() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
let idx = area.add_sub_window(widget_id_1(), Rect::new(10, 10, 200, 150));
assert_eq!(idx, 0, "first sub-window index should be 0");
assert_eq!(area.sub_window_count(), 1);
assert_eq!(
area.active_sub_window(),
Some(widget_id_1()),
"first sub-window becomes active"
);
let idx2 = area.add_sub_window(widget_id_2(), Rect::new(50, 50, 300, 200));
assert_eq!(idx2, 1);
assert_eq!(area.sub_window_count(), 2);
// Children should include both widget IDs
let children = area.children();
assert!(children.contains(&widget_id_1()));
assert!(children.contains(&widget_id_2()));
}
#[test]
fn mdiarea_add_sub_window_sets_z_order() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 100, 100));
if let Some(sw) = area.sub_window(0) {
assert_eq!(sw.z_order(), 0);
}
if let Some(sw) = area.sub_window(1) {
assert_eq!(sw.z_order(), 1);
}
}
// ── 3. Cascading / tiling ─────────────────────────────────────────────
#[test]
fn mdiarea_cascade_sub_windows() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 200, 150));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 200, 150));
area.add_sub_window(widget_id_3(), Rect::new(0, 0, 200, 150));
area.cascade_sub_windows();
// Each sub-window is offset by 30px
if let Some(sw) = area.sub_window(0) {
assert_eq!(sw.geometry().x, 0);
assert_eq!(sw.geometry().y, 0);
}
if let Some(sw) = area.sub_window(1) {
assert_eq!(sw.geometry().x, 30);
assert_eq!(sw.geometry().y, 30);
}
if let Some(sw) = area.sub_window(2) {
assert_eq!(sw.geometry().x, 60);
assert_eq!(sw.geometry().y, 60);
}
}
#[test]
fn mdiarea_cascade_empty_is_noop() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.cascade_sub_windows(); // should not panic
assert_eq!(area.sub_window_count(), 0);
}
#[test]
fn mdiarea_tile_sub_windows() {
let mut area = MdiArea::new(Rect::new(0, 0, 400, 300));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_3(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100)); // 4th
area.tile_sub_windows();
// 4 items -> 2 cols, 2 rows; cell width = 400/2 = 200, cell height = 300/2 = 150
if let Some(sw) = area.sub_window(1) {
assert_eq!(sw.geometry().x, 200, "item 1 should be in column 1");
assert_eq!(sw.geometry().y, 0);
}
if let Some(sw) = area.sub_window(2) {
assert_eq!(sw.geometry().x, 0, "item 2 should be in column 0");
assert_eq!(sw.geometry().y, 150, "item 2 should be in row 1");
}
if let Some(sw) = area.sub_window(3) {
assert_eq!(sw.geometry().x, 200);
assert_eq!(sw.geometry().y, 150);
}
}
#[test]
fn mdiarea_tile_empty_is_noop() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.tile_sub_windows(); // should not panic
assert_eq!(area.sub_window_count(), 0);
}
// ── 4. Removing sub-windows ───────────────────────────────────────────
#[test]
fn mdiarea_remove_sub_window() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(10, 10, 200, 150));
area.add_sub_window(widget_id_2(), Rect::new(50, 50, 300, 200));
area.remove_sub_window(widget_id_1());
assert_eq!(area.sub_window_count(), 1);
assert!(!area.children().contains(&widget_id_1()));
// The remaining one becomes active
assert_eq!(area.active_sub_window(), Some(widget_id_2()));
}
#[test]
fn mdiarea_remove_sub_window_nonexistent_is_noop() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.remove_sub_window(9999); // not present
assert_eq!(area.sub_window_count(), 1);
}
#[test]
fn mdiarea_remove_last_sub_window_clears_active() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.remove_sub_window(widget_id_1());
assert_eq!(area.sub_window_count(), 0);
assert_eq!(area.active_sub_window(), None);
}
#[test]
fn mdiarea_remove_sub_window_adjusts_active_index() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_3(), Rect::new(0, 0, 100, 100));
area.set_active_sub_window(widget_id_3()); // active index = 2
// Remove first sub-window (index 0 < active index 2) -> active shifts to 1
area.remove_sub_window(widget_id_1());
assert_eq!(area.active_sub_window(), Some(widget_id_3()), "active widget should stay same");
// Now remove the widget at the new active index (widget_id_3 is now at index 1)
area.remove_sub_window(widget_id_3());
// active becomes 0 (widget_id_2)
assert_eq!(area.active_sub_window(), Some(widget_id_2()));
}
// ── 5. Active sub-window management ───────────────────────────────────
#[test]
fn mdiarea_set_active_sub_window() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 100, 100));
assert_eq!(area.active_sub_window(), Some(widget_id_1()), "first added is active");
area.set_active_sub_window(widget_id_2());
assert_eq!(area.active_sub_window(), Some(widget_id_2()));
// Activating non-existent widget is no-op
area.set_active_sub_window(9999);
assert_eq!(area.active_sub_window(), Some(widget_id_2()), "should not change");
}
#[test]
fn mdiarea_activate_next_previous_sub_window() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_3(), Rect::new(0, 0, 100, 100));
assert_eq!(area.active_sub_window(), Some(widget_id_1()));
area.activate_next_sub_window();
assert_eq!(area.active_sub_window(), Some(widget_id_2()));
area.activate_next_sub_window();
assert_eq!(area.active_sub_window(), Some(widget_id_3()));
// Wrap around
area.activate_next_sub_window();
assert_eq!(area.active_sub_window(), Some(widget_id_1()));
// Previous
area.activate_previous_sub_window();
assert_eq!(area.active_sub_window(), Some(widget_id_3()));
area.activate_previous_sub_window();
assert_eq!(area.active_sub_window(), Some(widget_id_2()));
}
#[test]
fn mdiarea_activate_next_previous_empty_is_noop() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.activate_next_sub_window();
area.activate_previous_sub_window();
assert_eq!(area.active_sub_window(), None);
}
// ── 6. Geometry delegation ────────────────────────────────────────────
#[test]
fn mdiarea_geometry_delegation() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
assert_eq!(area.geometry(), Rect::new(0, 0, 600, 400));
area.set_geometry(Rect::new(10, 20, 800, 600));
assert_eq!(area.geometry(), Rect::new(10, 20, 800, 600));
assert_eq!(area.base().geometry(), area.geometry());
}
#[test]
fn mdiarea_position_and_size() {
let mut area = MdiArea::new(Rect::new(5, 10, 600, 400));
assert_eq!(area.position(), Point::new(5, 10));
assert_eq!(area.size(), crate::core::Size::new(600, 400));
area.set_position(Point::new(20, 30));
assert_eq!(area.geometry(), Rect::new(20, 30, 600, 400));
area.set_size(crate::core::Size::new(800, 600));
assert_eq!(area.geometry(), Rect::new(20, 30, 800, 600));
}
// ── 7. Visibility ─────────────────────────────────────────────────────
#[test]
fn mdiarea_visibility() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
assert!(area.is_visible());
area.hide();
assert!(!area.is_visible());
area.show();
assert!(area.is_visible());
area.set_visible(false);
assert!(!area.is_visible());
area.set_visible(true);
assert!(area.is_visible());
}
#[test]
fn mdiarea_enabled_state() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
assert!(area.is_enabled());
area.set_enabled(false);
assert!(!area.is_enabled());
area.set_enabled(true);
assert!(area.is_enabled());
}
// ── 8. ID / Kind ──────────────────────────────────────────────────────
#[test]
fn mdiarea_kind() {
let area = MdiArea::new(Rect::new(0, 0, 600, 400));
assert_eq!(area.kind(), WidgetKind::MdiArea);
}
#[test]
fn mdiarea_id_is_unique() {
let a1 = MdiArea::new(Rect::new(0, 0, 100, 100));
let a2 = MdiArea::new(Rect::new(0, 0, 100, 100));
assert_ne!(a1.id(), a2.id(), "each MdiArea must have a unique id");
}
// ── 9. SVG draw output ────────────────────────────────────────────────
#[test]
fn mdiarea_draw_produces_svg_output() {
let mut area = MdiArea::new(Rect::new(0, 0, 400, 300));
let svg = render_to_svg(&mut area);
assert!(svg.starts_with("<svg"), "SVG must start with <svg");
assert!(svg.ends_with("</svg>"), "SVG must end with </svg>");
assert!(svg.contains("width=\"400\""), "SVG must contain correct width");
assert!(svg.contains("height=\"300\""), "SVG must contain correct height");
assert!(svg.contains("fill="), "SVG should contain fill attributes");
assert!(svg.len() > 100, "SVG output should be substantial");
}
#[test]
fn mdiarea_draw_with_subwindow_produces_svg() {
let mut area = MdiArea::new(Rect::new(0, 0, 400, 300));
area.add_sub_window(widget_id_1(), Rect::new(20, 30, 200, 150));
area.set_background(Background::Gradient);
let svg = render_to_svg(&mut area);
assert!(svg.starts_with("<svg"));
assert!(svg.contains("fill="));
assert!(svg.len() > 200);
}
#[test]
fn mdiarea_draw_with_pattern_background_produces_svg() {
let mut area = MdiArea::new(Rect::new(0, 0, 60, 60));
area.set_background(Background::Pattern);
let svg = render_to_svg(&mut area);
assert!(svg.starts_with("<svg"));
assert!(svg.contains("fill="));
}
#[test]
fn mdiarea_draw_no_background_produces_svg() {
let mut area = MdiArea::new(Rect::new(0, 0, 100, 80));
area.set_background(Background::NoBackground);
let svg = render_to_svg(&mut area);
assert!(svg.starts_with("<svg"));
assert!(svg.len() > 50);
}
// ── 9b. Sub-window icons and their draw ──────────────────────────────
/// Builds one titled sub-window in sub-window view, ready to be drawn.
fn titled_area() -> MdiArea {
let mut area = MdiArea::new(Rect::new(0, 0, 240, 160));
area.add_sub_window(widget_id_1(), Rect::new(10, 10, 120, 90));
area.sub_window_mut(0).expect("exists").set_title("Doc".to_string());
area
}
#[test]
fn mdiarea_set_item_icon_source_reports_a_missing_child() {
let mut area = titled_area();
assert_eq!(
area.set_item_icon_source(widget_id_2(), Some(IconSource::Builtin(IconName::Info))),
Err(crate::widget::IconItemError::ItemNotFound { id: widget_id_2() })
);
assert!(area
.set_item_icon_source(widget_id_1(), Some(IconSource::Builtin(IconName::Info)))
.is_ok());
assert_eq!(
area.sub_window(0).unwrap().icon_source(),
Some(IconSource::Builtin(IconName::Info))
);
}
#[test]
fn mdiarea_sub_window_icon_adds_geometry_to_the_title_bar() {
let without = render_to_svg(&mut titled_area());
let mut with_icon = titled_area();
with_icon
.set_item_icon_source(widget_id_1(), Some(IconSource::Builtin(IconName::Info)))
.expect("the child exists");
let with = render_to_svg(&mut with_icon);
// One compound geometry becomes exactly one extra `<path>`; the left-aligned title is
// unchanged, so nothing else moves.
assert_eq!(
with.matches("<path").count(),
without.matches("<path").count() + 1,
"a typed sub-window icon must reach the drawing backend exactly once"
);
}
#[cfg(feature = "image")]
#[test]
fn mdiarea_legacy_image_icon_reaches_the_drawing_backend() {
let mut area = titled_area();
let without = render_to_svg(&mut area);
assert!(!without.contains("<image"), "an iconless sub-window draws no image");
area.sub_window_mut(0).expect("exists").set_icon(Some(
Image::try_from_rgba(vec![128; 4 * 4 * 4], 4, 4)
.expect("the buffer matches its declared size"),
));
let with = render_to_svg(&mut area);
assert!(with.contains("<image"), "a legacy sub-window image must reach the backend");
}
#[cfg(feature = "image")]
#[test]
fn mdiarea_typed_and_legacy_icon_share_one_active_state() {
let mut area = titled_area();
area.sub_window_mut(0).expect("exists").set_icon(Some(
Image::try_from_rgba(vec![3; 4], 1, 1).expect("the buffer matches its declared size"),
));
assert!(area.sub_window(0).unwrap().has_icon());
area.set_item_icon_source(widget_id_1(), Some(IconSource::Builtin(IconName::Save)))
.expect("the child exists");
assert!(area.sub_window(0).unwrap().icon().is_none());
assert_eq!(
area.sub_window(0).unwrap().icon_source(),
Some(IconSource::Builtin(IconName::Save))
);
area.sub_window_mut(0).unwrap().clear_icon_source();
assert!(!area.sub_window(0).unwrap().has_icon());
assert!(area.sub_window(0).unwrap().icon().is_none());
}
#[test]
fn mdiarea_tabbed_view_draws_the_sub_window_icon_too() {
// Both presentations of one sub-window must show its icon: the tabbed view has no title
// bar, so an icon wired only into the title bar would simply vanish there.
let build = |icon: bool| {
let mut area = MdiArea::new(Rect::new(0, 0, 240, 160));
area.add_sub_window(widget_id_1(), Rect::new(10, 10, 120, 90));
area.sub_window_mut(0).expect("exists").set_title("Doc".to_string());
area.set_view_mode(ViewMode::TabbedView);
if icon {
area.set_item_icon_source(widget_id_1(), Some(IconSource::Builtin(IconName::Info)))
.expect("the child exists");
}
area
};
let without = render_to_svg(&mut build(false));
let with = render_to_svg(&mut build(true));
assert_eq!(
with.matches("<path").count(),
without.matches("<path").count() + 1,
"the tabbed view must paint the sub-window icon exactly once"
);
}
// ── 10. Signal accessors ─────────────────────────────────────────────
#[test]
fn mdiarea_subwindow_activated_signal_emits_on_set() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 100, 100));
let emitted = Arc::new(AtomicUsize::new(0));
let last_id = Arc::new(AtomicU64::new(0));
{
let e = emitted.clone();
let lid = last_id.clone();
area.subwindow_activated.connect(move |id: Arc<ObjectId>| {
e.fetch_add(1, Ordering::SeqCst);
lid.store(*id, Ordering::SeqCst);
});
}
// Emitted once on add (first sub-window becomes active)
assert_eq!(emitted.load(Ordering::SeqCst), 0, "signal not connected during add");
// Connect first, then set active
let emitted2 = Arc::new(AtomicUsize::new(0));
let e2 = emitted2.clone();
area.subwindow_activated.connect(move |_: Arc<ObjectId>| {
e2.fetch_add(1, Ordering::SeqCst);
});
area.set_active_sub_window(widget_id_2());
assert_eq!(emitted2.load(Ordering::SeqCst), 1, "signal should emit on set_active");
// Setting same value does not emit
area.set_active_sub_window(widget_id_2());
assert_eq!(emitted2.load(Ordering::SeqCst), 1, "same value should not emit");
}
#[test]
fn mdiarea_subwindow_activated_on_remove_and_next() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 100, 100));
let hits = Arc::new(AtomicUsize::new(0));
let h = hits.clone();
area.subwindow_activated.connect(move |_: Arc<ObjectId>| {
h.fetch_add(1, Ordering::SeqCst);
});
// Removing the active sub-window triggers activation of another
let before = hits.load(Ordering::SeqCst);
area.remove_sub_window(widget_id_1());
assert!(
hits.load(Ordering::SeqCst) > before,
"removing active sub-window should emit activation signal"
);
}
// ── View mode / Background / Activation order setters ─────────────────
#[test]
fn mdiarea_view_mode_enum() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
assert_eq!(area.view_mode(), ViewMode::SubWindowView);
area.set_view_mode(ViewMode::TabbedView);
assert_eq!(area.view_mode(), ViewMode::TabbedView);
area.set_view_mode(ViewMode::SubWindowView);
assert_eq!(area.view_mode(), ViewMode::SubWindowView);
}
#[test]
fn mdiarea_background_enum() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
assert_eq!(area.background(), Background::Plain);
area.set_background(Background::NoBackground);
assert_eq!(area.background(), Background::NoBackground);
area.set_background(Background::Gradient);
assert_eq!(area.background(), Background::Gradient);
area.set_background(Background::Pattern);
assert_eq!(area.background(), Background::Pattern);
}
#[test]
fn mdiarea_activation_order_enum() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
assert_eq!(area.activation_order(), ActivationOrder::StackingOrder);
area.set_activation_order(ActivationOrder::CreationOrder);
assert_eq!(area.activation_order(), ActivationOrder::CreationOrder);
area.set_activation_order(ActivationOrder::HistoryOrder);
assert_eq!(area.activation_order(), ActivationOrder::HistoryOrder);
}
// ── Sub-window property access ────────────────────────────────────────
#[test]
fn mdiarea_sub_window_accessors() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(10, 20, 200, 150));
if let Some(sw) = area.sub_window(0) {
assert_eq!(sw.widget(), widget_id_1());
assert_eq!(sw.geometry(), Rect::new(10, 20, 200, 150));
assert_eq!(sw.title(), "");
assert!(sw.is_closable());
assert!(sw.is_movable());
assert!(sw.is_resizable());
assert!(!sw.is_minimized());
assert!(!sw.is_maximized());
assert_eq!(sw.z_order(), 0);
}
// Mutable access
if let Some(sw) = area.sub_window_mut(0) {
sw.set_title("Doc".to_string());
sw.set_minimized(true);
sw.set_closable(false);
sw.set_z_order(42);
}
if let Some(sw) = area.sub_window(0) {
assert_eq!(sw.title(), "Doc");
assert!(sw.is_minimized());
assert!(!sw.is_closable());
assert_eq!(sw.z_order(), 42);
}
// Out of bounds
assert!(area.sub_window(5).is_none());
assert!(area.sub_window_mut(5).is_none());
}
#[test]
fn mdiarea_sub_window_set_geometry() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
if let Some(sw) = area.sub_window_mut(0) {
sw.set_geometry(Rect::new(50, 60, 300, 200));
}
if let Some(sw) = area.sub_window(0) {
assert_eq!(sw.geometry(), Rect::new(50, 60, 300, 200));
}
}
// ── Mouse event activates sub-window ─────────────────────────────────
#[test]
fn mdiarea_mouse_click_activates_sub_window() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(10, 10, 200, 150));
area.add_sub_window(widget_id_2(), Rect::new(100, 100, 200, 150));
// First sub-window is active by default
assert_eq!(area.active_sub_window(), Some(widget_id_1()));
// Click on second sub-window area
area.handle_event(&Event::MousePress {
pos: Point::new(150, 150),
button: 1,
modifiers: 0,
});
assert_eq!(area.active_sub_window(), Some(widget_id_2()));
// Click back on first sub-window
area.handle_event(&Event::MousePress { pos: Point::new(20, 20), button: 1, modifiers: 0 });
assert_eq!(area.active_sub_window(), Some(widget_id_1()));
}
#[test]
fn mdiarea_mouse_click_outside_sub_window_is_noop() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(10, 10, 200, 150));
assert_eq!(area.active_sub_window(), Some(widget_id_1()));
// Click outside all sub-windows
area.handle_event(&Event::MousePress {
pos: Point::new(500, 300),
button: 1,
modifiers: 0,
});
assert_eq!(area.active_sub_window(), Some(widget_id_1()), "should not change");
}
// ── Arrange icons ─────────────────────────────────────────────────────
#[test]
fn mdiarea_arrange_icons() {
let mut area = MdiArea::new(Rect::new(0, 0, 400, 300));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 200, 150));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 200, 150));
// Mark both as minimized
if let Some(sw) = area.sub_window_mut(0) {
sw.set_minimized(true);
}
if let Some(sw) = area.sub_window_mut(1) {
sw.set_minimized(true);
}
area.arrange_icons();
// Both should now be placed at the bottom of the area
if let Some(sw) = area.sub_window(0) {
assert_eq!(sw.geometry().width, 100);
assert_eq!(sw.geometry().height, 80);
// Positioned at bottom
assert_eq!(sw.geometry().y, 300 - 80 - 10);
}
}
#[test]
fn mdiarea_arrange_icons_empty_is_noop() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.arrange_icons(); // should not panic
assert_eq!(area.sub_window_count(), 0);
}
#[test]
fn mdiarea_arrange_icons_handles_narrow_area() {
let mut area = MdiArea::new(Rect::new(0, 0, 50, 40));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 20, 20));
area.sub_window_mut(0).expect("subwindow exists").set_minimized(true);
area.arrange_icons();
assert_eq!(area.sub_window(0).map(|window| window.geometry().width), Some(100));
}
// ── 11. `view_mode` decides where a sub-window is placed ────────────────
/// A `TabbedView` area places its active window in the body below the tab strip, and only
/// that one.
///
/// # What was dead, and what proves it is alive now
///
/// `view_mode` was stored, published (getter, `view_mode` property token, round-trip test) and
/// read by nothing: `draw` painted the stored sub-window geometries and a title bar for each, so
/// `TabbedView` rendered byte-identically to `SubWindowView`. Merely *branching* on the mode in
/// `subwindow_placement` would leave the paint ignoring the result, so this asserts the numbers a
/// paint has to obey: the frame starts below the strip, spans the work area, and the inactive
/// windows have no placement at all rather than one under the active window.
#[test]
fn tabbed_view_stacks_every_window_in_one_body_below_the_strip() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(10, 10, 100, 80));
area.add_sub_window(widget_id_2(), Rect::new(200, 200, 100, 80));
area.set_active_sub_window(widget_id_2());
area.set_view_mode(ViewMode::TabbedView);
let strip = area.tab_strip_rect();
assert_eq!(strip.y, 0);
assert_eq!(strip.height, TAB_STRIP_HEIGHT as u32);
assert_eq!(strip.width, 600);
let active = area.subwindow_placement(1).expect("the active window is placed");
assert_eq!(active.frame.x, 0, "the body spans the work area");
assert_eq!(active.frame.width, 600);
assert_eq!(
active.frame.y,
strip.y + strip.height as i32,
"the body starts below the strip, not over it"
);
assert_eq!(
active.frame.y + active.frame.height as i32,
400,
"and runs to the work area's bottom edge"
);
assert!(!active.title_bar, "the tab already names this window");
// The window that is *not* active has no placement: two placements would paint both frames
// over the same body and the later one would win, which is the wrong window on screen.
assert!(area.subwindow_placement(0).is_none(), "only the active window occupies the body");
// And the stored geometry is left alone, so switching back to `SubWindowView` restores it.
area.set_view_mode(ViewMode::SubWindowView);
let restored = area.subwindow_placement(0).expect("sub-window view places every window");
assert_eq!(restored.frame, Rect::new(10, 10, 100, 80));
assert!(restored.title_bar, "sub-window view draws its own title bar");
}
/// A tab is where the click says it is: the tab box the paint reads is the box the hit test
/// resolves.
///
/// `tab_rect` and `tab_at_point` are one derivation for the same reason `list_view`'s row
/// geometry is: two independent calculations of "where is tab 3" drift, and the symptom is a
/// click activating the tab next to the one that was clicked.
#[test]
fn a_tab_click_activates_the_tab_it_is_painted_on() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_3(), Rect::new(0, 0, 100, 100));
area.set_view_mode(ViewMode::TabbedView);
for index in 0..3 {
let tab = area.tab_rect(index).expect("three tabs fit");
let centre = Point::new(tab.x + tab.width as i32 / 2, tab.y + tab.height as i32 / 2);
assert_eq!(
area.tab_at_point(centre),
Some(index),
"the centre of tab {index} at {tab:?} must hit-test back to it"
);
}
// Tabs are capped rather than stretched: three tabs of 600 px each would each be 200 wide,
// which is past the cap and reads as a toolbar.
assert_eq!(area.tab_rect(0).map(|tab| tab.width), Some(TAB_MAX_WIDTH));
// A click on the strip past the last tab is no tab, not a clamp onto the last one.
assert_eq!(area.tab_at_point(Point::new(599, 2)), None);
// And outside the strip entirely is no tab either.
assert_eq!(area.tab_at_point(Point::new(5, TAB_STRIP_HEIGHT + 40)), None);
}
/// The mode reaches the pixels: a `TabbedView` snapshot is not a `SubWindowView` one.
///
/// The geometry assertions above say where the frames go; this says the paint actually obeys
/// them. Without it a `subwindow_placement` that nothing called would still pass every number.
#[test]
fn the_two_view_modes_render_differently() {
let build = |mode: ViewMode| {
let mut area = MdiArea::new(Rect::new(0, 0, 240, 160));
area.add_sub_window(widget_id_1(), Rect::new(10, 10, 120, 90));
area.add_sub_window(widget_id_2(), Rect::new(40, 40, 120, 90));
area.set_active_sub_window(widget_id_2());
area.sub_window_mut(0).expect("exists").set_title("First".to_string());
area.sub_window_mut(1).expect("exists").set_title("Second".to_string());
area.set_view_mode(mode);
area
};
let windowed = render_to_svg(&mut build(ViewMode::SubWindowView));
let tabbed = render_to_svg(&mut build(ViewMode::TabbedView));
assert_ne!(
windowed, tabbed,
"the two view modes must not render identically -- that was the dead state"
);
// The tab strip sits on the top row and the sub-window view puts a frame there instead.
let tabbed_strip = build(ViewMode::TabbedView);
assert_eq!(tabbed_strip.tab_strip_rect().height, TAB_STRIP_HEIGHT as u32);
// The *body* is what the mode is actually about, and "the strings differ" alone does not
// pin it: a `TabbedView` that still painted every window at its stored geometry would differ
// from the windowed one by the tab strip alone. So the placement is asserted through the
// rendering's own numbers -- the active window's frame must start at the strip's bottom
// edge, and the inactive one must not be drawn at its stored geometry.
let tabbed_area = build(ViewMode::TabbedView);
let active_frame =
tabbed_area.subwindow_placement(1).expect("the active window is placed").frame;
assert_eq!(active_frame.y, TAB_STRIP_HEIGHT, "the tabbed body begins where the strip ends");
assert_eq!(active_frame.y + active_frame.height as i32, 160);
assert!(
tabbed_area.subwindow_placement(0).is_none(),
"and the inactive window is not painted at its stored geometry"
);
// The windowed mode, by contrast, uses the stored geometry for both.
let windowed_area = build(ViewMode::SubWindowView);
assert_eq!(
windowed_area.subwindow_placement(0).expect("placed").frame,
Rect::new(10, 10, 120, 90)
);
}
/// The ✕ closes the window it belongs to instead of activating it.
///
/// The button was painted for every closable sub-window but no code hit-tested it: a press on
/// it resolved to the frame and called `set_active_sub_window`, so a drawn "close" affordance
/// did nothing — and the press was then forwarded into the window it had just "activated".
#[test]
fn mdi_close_button_closes_the_sub_window() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(20, 20, 200, 150));
area.add_sub_window(widget_id_2(), Rect::new(240, 20, 200, 150));
assert_eq!(
area.active_sub_window(),
Some(widget_id_1()),
"the first window is active before any press"
);
// Close the **inactive** window, so the assertion below distinguishes "closed it" from
// "activated it".
let frame = area.subwindow_placement(1).expect("placed").frame;
let close = close_button_rect(frame);
let pos = Point::new(close.x + close.width as i32 / 2, close.y + close.height as i32 / 2);
area.handle_event(&Event::MousePress { pos, button: 1, modifiers: 0 });
assert_eq!(area.sub_window_count(), 1, "the ✕ must close its own window");
assert_eq!(
area.sub_window(0).map(|window| window.widget()),
Some(widget_id_1()),
"the surviving window is the untouched one"
);
assert_eq!(
area.active_sub_window(),
Some(widget_id_1()),
"closing an inactive window must not activate it"
);
}
/// The checker background reaches the work area's own edges.
///
/// The tile loops truncated to whole tiles and never filled the base, so the right and bottom
/// remainders (up to 19 px each) kept whatever the parent had painted — and an area smaller
/// than one tile painted nothing at all.
#[test]
fn mdi_pattern_background_covers_its_whole_rect() {
let mut area = MdiArea::new(Rect::new(0, 0, 250, 250));
area.set_background(Background::Pattern);
let svg = render_to_svg(&mut area);
assert!(
svg.contains(r#"<rect x="0" y="0" width="250" height="250" fill="rgba"#),
"the checker must start from a base fill covering the whole work area"
);
}
/// Every activation order actually drives `next`/`previous`.
#[test]
fn activation_order_decides_what_next_visits() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_2(), Rect::new(0, 0, 100, 100));
area.add_sub_window(widget_id_3(), Rect::new(0, 0, 100, 100));
// Reverse the stacking order so it differs from creation order.
area.sub_window_mut(0).expect("first").set_z_order(2);
area.sub_window_mut(1).expect("second").set_z_order(1);
area.sub_window_mut(2).expect("third").set_z_order(0);
area.set_active_sub_window(widget_id_1());
area.set_activation_order(ActivationOrder::CreationOrder);
area.activate_next_sub_window();
assert_eq!(area.active_sub_window(), Some(widget_id_2()), "creation order is add order");
area.set_active_sub_window(widget_id_1());
area.set_activation_order(ActivationOrder::StackingOrder);
area.activate_next_sub_window();
assert_eq!(
area.active_sub_window(),
Some(widget_id_3()),
"stacking order follows z_order, not the storage index"
);
area.set_activation_order(ActivationOrder::HistoryOrder);
area.set_active_sub_window(widget_id_1());
area.set_active_sub_window(widget_id_3());
area.set_active_sub_window(widget_id_2());
area.activate_next_sub_window();
assert_eq!(
area.active_sub_window(),
Some(widget_id_3()),
"history order returns the previously active window"
);
area.activate_previous_sub_window();
assert_eq!(
area.active_sub_window(),
Some(widget_id_1()),
"from the newest window, previous wraps through the history cycle"
);
}
/// Dragging a frame edge resizes it, and a title-bar double-click maximizes and restores.
#[test]
fn edge_drag_resizes_and_title_bar_double_click_maximizes() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(20, 20, 200, 150));
// Grab the right edge and pull it 30 px outward.
area.handle_event(&Event::MousePress {
pos: Point::new(219, 100),
button: 1,
modifiers: 0,
});
area.handle_event(&Event::MouseMove { pos: Point::new(249, 100) });
area.handle_event(&Event::MouseRelease { pos: Point::new(249, 100), button: 1 });
let resized = area.sub_window(0).expect("frame").geometry();
assert_eq!(resized, Rect::new(20, 20, 230, 150), "the right edge followed the drag");
// The drag cannot shrink the frame past its minimum.
area.handle_event(&Event::MousePress {
pos: Point::new(249, 100),
button: 1,
modifiers: 0,
});
area.handle_event(&Event::MouseMove { pos: Point::new(-400, 100) });
area.handle_event(&Event::MouseRelease { pos: Point::new(-400, 100), button: 1 });
assert_eq!(
area.sub_window(0).expect("frame").geometry().width,
MIN_SUBWINDOW_WIDTH,
"the drag stops at the minimum width"
);
// Double-click the title bar: maximize, then restore.
let before = area.sub_window(0).expect("frame").geometry();
area.handle_event(&Event::MouseDoubleClick { pos: Point::new(40, 22), button: 1 });
assert!(area.sub_window(0).expect("frame").is_maximized());
assert_eq!(area.sub_window(0).expect("frame").geometry(), area.geometry());
area.handle_event(&Event::MouseDoubleClick { pos: Point::new(40, 22), button: 1 });
assert!(!area.sub_window(0).expect("frame").is_maximized());
assert_eq!(area.sub_window(0).expect("frame").geometry(), before);
}
/// The viewport pan moves the painted frame and the hit test together, while the stored
/// geometry stays put; a maximized window ignores the pan.
#[test]
fn the_viewport_pan_translates_frames_and_hit_tests() {
let mut area = MdiArea::new(Rect::new(0, 0, 600, 400));
area.add_sub_window(widget_id_1(), Rect::new(100, 40, 120, 100));
let stored = area.sub_window(0).expect("frame").geometry();
assert!(area.sub_window_at_position(Point::new(110, 50)).is_some());
assert!(area.sub_window_at_position(Point::new(50, 35)).is_none());
area.set_scroll_offset(Point::new(60, 10));
assert_eq!(area.scroll_offset(), Point::new(60, 10));
let placed = area.subwindow_placement(0).expect("placed").frame;
assert_eq!(placed, Rect::new(40, 30, 120, 100), "the frame follows the pan");
assert!(area.sub_window_at_position(Point::new(50, 35)).is_some());
assert!(area.sub_window_at_position(Point::new(200, 50)).is_none());
assert_eq!(
area.sub_window(0).expect("frame").geometry(),
stored,
"stored frame is unchanged"
);
area.scroll_by(-10, -10);
assert_eq!(area.scroll_offset(), Point::new(50, 0));
area.toggle_maximized_sub_window(widget_id_1());
assert_eq!(
area.subwindow_placement(0).expect("placed").frame,
area.geometry(),
"a maximized window owns the area regardless of the pan"
);
}
/// C-15: the title-bar button minimizes and restores, and the state is observable.
#[test]
fn the_minimize_button_toggles_the_icon_state() {
let mut area = MdiArea::new(Rect::new(0, 0, 400, 300));
area.add_sub_window(widget_id_1(), Rect::new(10, 10, 120, 100));
let button = minimize_button_rect(area.subwindow_placement(0).expect("placed").frame);
area.handle_event(&crate::event::Event::MousePress {
pos: crate::core::Point::new(button.x + 1, button.y + 1),
button: 1,
modifiers: 0,
});
assert!(area.is_sub_window_minimized(widget_id_1()));
assert!(area.set_sub_window_minimized(widget_id_1(), false));
assert!(!area.is_sub_window_minimized(widget_id_1()));
}
/// C-14: dragging the title bar moves the window by the pointer delta.
#[test]
fn dragging_a_title_bar_moves_the_window() {
let mut area = MdiArea::new(Rect::new(0, 0, 400, 300));
area.add_sub_window(widget_id_1(), Rect::new(10, 10, 120, 100));
area.handle_event(&crate::event::Event::MousePress {
pos: crate::core::Point::new(20, 15),
button: 1,
modifiers: 0,
});
area.handle_event(&crate::event::Event::MouseMove { pos: crate::core::Point::new(60, 45) });
area.handle_event(&crate::event::Event::MouseRelease {
pos: crate::core::Point::new(60, 45),
button: 1,
});
let geometry = area.sub_window_mut(0).expect("window").geometry;
assert_eq!(geometry.x, 50, "moved by the pointer delta");
assert_eq!(geometry.y, 40);
}
/// C-16: the wheel pans the work area in sub-window mode.
#[test]
fn the_wheel_pans_the_work_area() {
let mut area = MdiArea::new(Rect::new(0, 0, 400, 300));
area.handle_event(&crate::event::Event::Wheel {
delta: crate::core::Point::new(0, 2),
modifiers: 0,
});
assert!(area.scroll_offset().y < 0, "a downward notch scrolls the desktop up");
}
}