rust_widgets 2.9.2

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! MarkdownEditor widget.

use crate::core::{Color, Font, HorizontalAlignment, Point, Rect};
use crate::event::wheel::WheelScroll;
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::shortcut::Key;
use crate::signal::Signal1;
use crate::style::animation::CursorBlink;
use crate::undo::{CommandDescription, CommandId, UndoCommand, UndoStack};
use crate::widget::capability::coercion::{
    expect_bool, expect_string, expect_string_list, expect_usize,
};
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::input_widgets::shortcut_editor::primary_shortcut_pressed;
use crate::widget::metrics::{effective_font, estimate_text_width};
use crate::widget::special_widgets::code_editor::{
    BuiltinHighlighter, LanguageId, LineState, SyntaxHighlighter, TokenKind,
};
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};
use crate::{impl_widget_property_hooks, property_names_of};

// The formatting strip is a companion control, declared here so the editor's action surface and
// its pointer surface live in one module tree (COMP-H04).
#[path = "markdown_toolbar.rs"]
pub mod markdown_toolbar;
pub use markdown_toolbar::{MarkdownToolbar, ToolbarIconSide, MARKDOWN_TOOLBAR_ACTIONS};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;

/// The top of the first text row below the header band.
const TEXT_TOP: i32 = 36;
/// The pitch between text rows.
const TEXT_ROW_HEIGHT: i32 = 16;
/// The bottom inset a row has to clear to be painted.
const TEXT_BOTTOM_INSET: i32 = 8;

/// An undo step that records only the bytes that differ between two document states.
///
/// A whole-document snapshot would store both versions on every keystroke. The common
/// prefix and suffix are found on character boundaries, so the command keeps only the
/// changed run and an offset, and an undo touches O(edit) bytes instead of O(document).
struct MarkdownTextDeltaCommand {
    id: CommandId,
    target: Rc<RefCell<String>>,
    offset: usize,
    removed: String,
    inserted: String,
}

impl MarkdownTextDeltaCommand {
    /// Builds a command for a mutation that has already been applied.
    fn new(target: Rc<RefCell<String>>, before: &str, after: &str) -> Self {
        // The common prefix and suffix are found by byte first, then trimmed back to character
        // boundaries. Byte comparison is the same answer as character comparison here (equal
        // bytes at equal offsets mean equal characters) and is an order of magnitude cheaper on
        // the ASCII that dominates source text.
        let before_bytes = before.as_bytes();
        let after_bytes = after.as_bytes();
        let max_prefix: usize = before_bytes.len().min(after_bytes.len());
        let mut prefix: usize = 0;
        while prefix < max_prefix && before_bytes[prefix] == after_bytes[prefix] {
            prefix += 1;
        }
        // Both strings share this prefix, so a boundary in `before` is one in `after` too.
        while prefix > 0 && !before.is_char_boundary(prefix) {
            prefix -= 1;
        }
        let max_suffix: usize = before_bytes.len().min(after_bytes.len()) - prefix;
        let mut suffix: usize = 0;
        while suffix < max_suffix
            && before_bytes[before_bytes.len() - 1 - suffix]
                == after_bytes[after_bytes.len() - 1 - suffix]
        {
            suffix += 1;
        }
        while suffix > 0 && !before.is_char_boundary(before.len() - suffix) {
            suffix -= 1;
        }
        Self {
            id: CommandId(
                NEXT_MARKDOWN_COMMAND_ID.fetch_add(1, core::sync::atomic::Ordering::Relaxed),
            ),
            target,
            offset: prefix,
            removed: before[prefix..before.len() - suffix].to_string(),
            inserted: after[prefix..after.len() - suffix].to_string(),
        }
    }

    /// Replaces the recorded range with `insert`.
    fn splice(&self, remove_len: usize, insert: &str) {
        let mut text = self.target.borrow_mut();
        let start = self.offset.min(text.len());
        let end = (start + remove_len).min(text.len());
        if start <= end {
            text.replace_range(start..end, insert);
        }
    }
}

/// Counter for [`MarkdownTextDeltaCommand`] ids.
static NEXT_MARKDOWN_COMMAND_ID: core::sync::atomic::AtomicU64 =
    core::sync::atomic::AtomicU64::new(1);

impl UndoCommand for MarkdownTextDeltaCommand {
    fn id(&self) -> CommandId {
        self.id
    }

    fn description(&self) -> CommandDescription {
        CommandDescription {
            text: "Edit text".to_string(),
            timestamp_ms: 0,
            command_type: "markdown_editor_text",
        }
    }

    fn execute(&mut self) -> Result<(), String> {
        self.splice(self.removed.len(), &self.inserted.clone());
        Ok(())
    }

    fn undo(&mut self) -> Result<(), String> {
        self.splice(self.inserted.len(), &self.removed.clone());
        Ok(())
    }
}

/// One heading in the document outline.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MarkdownOutlineEntry {
    /// Zero-based line index.
    pub line: usize,
    /// Heading level, `1..=6`.
    pub level: u8,
    /// Heading text without its `#` markers.
    pub title: String,
}

/// One run of preview text with the style the inline parser resolved for it.
struct PreviewRun {
    text: String,
    style: PreviewStyle,
    /// The measured advance, resolved when the line was parsed so a still frame measures nothing.
    width: u32,
}

/// The inline style of one preview run.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PreviewStyle {
    /// Ordinary body text.
    Text,
    /// `**strong**`: drawn with a one-pixel overprint, like every bold run in this crate.
    Bold,
    /// `*emphasis*`: the face has no italic, so emphasis is a slight ink shift.
    Italic,
    /// `~~struck~~`: drawn faint with a rule through it.
    Strike,
    /// `` `code` ``: tinted backing.
    Code,
    /// `[label](target)`: accent ink with an underline.
    Link,
    /// Syntax that was recognised and is not shown as body text (fences, list markers).
    Dim,
    /// A list bullet, task box or quote bar.
    Accent,
    /// One token of a highlighted fenced block.
    Token(TokenKind),
}

/// How a preview row is laid out.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum PreviewRow {
    /// Runs drawn from left to right.
    Inline,
    /// A heading, carrying its level.
    Heading(u8),
    /// A horizontal rule.
    Rule,
    /// An opening or closing code fence.
    Fence,
    /// The body of a fenced code block.
    Code,
    /// A table row (the runs already contain the cell separators).
    Table,
}

/// A parsed preview row, cached by line index and text hash.
struct ParsedLine {
    runs: Vec<PreviewRun>,
    row: PreviewRow,
}

/// Where a line sits relative to a fenced code block.
#[derive(Clone, Copy)]
enum BlockStart {
    /// Outside any fence.
    Normal,
    /// The opening or closing fence line itself.
    Fence,
    /// Inside a fence, with the lexer state the line starts in.
    Code(LanguageId, LineState),
}

/// An IME composition that has not been committed yet.
#[derive(Debug, Clone, Default)]
struct Preedit {
    text: String,
    /// Byte offset of the insertion point inside `text`.
    cursor: usize,
}

/// Document statistics, cached per revision.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct Metrics {
    lines: usize,
    words: usize,
    headings: usize,
}

/// Per-line offset tables derived from one revision of the document.
///
/// Every caret query used to rescan the document from its first byte, and each keystroke
/// rebuilt the whole document as a character vector. Both are O(document) per event. With the
/// tables below a line lookup is a binary search and a character offset is resolved inside one
/// line, so the work is bounded by the line's own length instead of the document's.
#[derive(Debug, Default)]
struct LineIndex {
    /// Byte offset of the first character of every line.
    starts: Vec<usize>,
    /// Character offset of the first character of every line.
    char_starts: Vec<usize>,
    /// Character length of every line, excluding its `\n`.
    char_lens: Vec<usize>,
}

impl LineIndex {
    /// Builds the tables from `text` in one pass.
    fn new(text: &str) -> Self {
        let mut starts = Vec::new();
        let mut char_starts = Vec::new();
        let mut char_lens = Vec::new();
        let mut byte = 0usize;
        let mut chars = 0usize;
        for line in text.split('\n') {
            starts.push(byte);
            char_starts.push(chars);
            let length = line.chars().count();
            char_lens.push(length);
            byte += line.len() + 1;
            chars += length + 1;
        }
        Self { starts, char_starts, char_lens }
    }

    /// The index of the line that contains `offset`, counted in characters.
    fn line_of_char(&self, offset: usize) -> usize {
        // `char_starts` is ascending, so the last entry that starts at or before `offset` is the
        // containing line. A `\n` position belongs to the line it terminates.
        self.char_starts.partition_point(|start| *start <= offset).saturating_sub(1)
    }

    /// The number of characters in the document, including the `\n` separators.
    fn char_len(&self) -> usize {
        match (self.char_starts.last(), self.char_lens.last()) {
            (Some(start), Some(length)) => start + length,
            _ => 0,
        }
    }
}

/// The find bar's state.
#[derive(Debug, Clone, Default)]
struct FindState {
    visible: bool,
    query: String,
    /// Byte offset of the current match, when one is highlighted.
    current: Option<usize>,
}

/// Lightweight markdown editor with preview toggle and metrics.
pub struct MarkdownEditor {
    base: BaseWidget,
    text: String,
    preview_mode: bool,
    cursor_line: usize,
    /// Character offset of the caret within [`Self::cursor_line`].
    cursor_col: usize,
    /// The first text line the viewport paints.
    ///
    /// The editor used to paint a fixed ten lines, so a longer document simply ended at row ten.
    /// This is the scroll state the wheel drives; the paint path starts here and takes only the
    /// rows that fit.
    first_visible_line: usize,
    /// Emitted when text changes.
    pub text_changed: Signal1<String>,
    /// Emitted when preview mode changes.
    pub preview_mode_changed: Signal1<bool>,
    /// Emitted when a selection appears (`true`) or disappears (`false`).
    pub selection_changed: Signal1<bool>,
    /// Emitted when a heading row is activated by a click, with its outline index.
    pub heading_activated: Signal1<usize>,
    undo_stack: UndoStack,
    history_target: Rc<RefCell<String>>,
    restoring_history: bool,
    /// The configured undo depth, so the property can be read back after a write.
    undo_stack_limit: usize,
    /// The selection as ordered document character offsets.
    selection: Option<(usize, usize)>,
    /// Where the selection started, as `(line, column)`, for shift-extension.
    selection_anchor: Option<(usize, usize)>,
    /// Whether a pointer drag is still extending the selection.
    selecting: bool,
    /// Uncommitted IME composition, drawn at the caret.
    preedit: Option<Preedit>,
    /// The caret blink. Unlike the other two text controls this one had none.
    cursor_blink: CursorBlink,
    /// Whether the editor paints a scrollbar beside the text.
    show_scrollbar: bool,
    /// Whether a drag on the scrollbar thumb is in progress.
    dragging_scrollbar: bool,
    /// The find bar.
    find: FindState,
    /// The mounted find/replace bar.
    ///
    /// It is a view over this editor's bytes, not the editor's own find state: the bar paints the
    /// query and the match counter while [`Self::find`] stays the navigation state the editor's
    /// keyboard path uses. [`Self::show_find_bar`] is what points the two at the same text.
    find_bar: FindBar,
    /// The mounted document outline.
    outline_panel: MarkdownOutlinePanel,
    /// Whether the outline panel is painted inside the editor's own rectangle.
    show_outline: bool,
    /// The revision the panel's entries were last rebuilt for.
    ///
    /// `outline()` walks every line, so it is rebuilt when the text changes rather than on every
    /// frame; the active row still follows the caret on each paint.
    outline_revision: u64,
    /// Whether GFM constructs (tables, tasks, strikethrough, quotes, fences, rules) are parsed.
    gfm_extensions: bool,
    /// Whether fenced code blocks are tokenized with the shared lexer.
    code_highlight: bool,
    /// Counts text mutations, so caches can tell a stale frame from a live one.
    revision: u64,
    /// `line_count` / `word_count` / `heading_count`, keyed by revision.
    metrics: RefCell<Option<(u64, Metrics)>>,
    /// Parsed preview rows, keyed by line index and text hash.
    preview_cache: HashMap<(usize, u64), Rc<ParsedLine>>,
    /// Per-line block classification, rebuilt when the revision changes.
    blocks: Vec<BlockStart>,
    /// Whether the line *after* each line is a table divider, so a row can be recognised as a
    /// table header without the paint path looking ahead (which would be quadratic).
    table_header: Vec<bool>,
    /// The revision `blocks` was built for.
    blocks_revision: u64,
    /// Measured glyph advances for the caret's line prefix, keyed by font size.
    char_widths: Vec<(char, f32, i32)>,
    /// Line offset tables for [`Self::revision`], rebuilt at most once per revision.
    line_index: RefCell<Option<(u64, LineIndex)>>,
}

impl MarkdownEditor {
    /// Creates editor.
    pub fn new(geometry: Rect) -> Self {
        Self::with_undo_limit(geometry, 100)
    }

    /// Creates an editor whose undo history keeps at most `undo_limit` steps.
    ///
    /// The stack used to be the default 100 with no way to reach it, so a long writing session
    /// silently lost everything past the hundredth edit.
    pub fn with_undo_limit(geometry: Rect, undo_limit: usize) -> Self {
        let undo_limit = undo_limit.max(1);
        let mut cursor_blink = CursorBlink::new();
        // The editor is an input surface by definition, so a freshly mounted one is already
        // waiting for typing; waiting for a focus event a host may not send would make this the
        // only text control in the crate whose caret never blinks.
        cursor_blink.start();
        Self {
            base: BaseWidget::new(WidgetKind::RichEdit, geometry, "MarkdownEditor"),
            text: String::new(),
            preview_mode: false,
            cursor_line: 0,
            cursor_col: 0,
            first_visible_line: 0,
            text_changed: Signal1::new(),
            preview_mode_changed: Signal1::new(),
            selection_changed: Signal1::new(),
            heading_activated: Signal1::new(),
            undo_stack: UndoStack::with_capacity(undo_limit),
            history_target: Rc::new(RefCell::new(String::new())),
            restoring_history: false,
            undo_stack_limit: undo_limit,
            selection: None,
            selection_anchor: None,
            selecting: false,
            preedit: None,
            cursor_blink,
            show_scrollbar: true,
            dragging_scrollbar: false,
            find: FindState::default(),
            find_bar: FindBar::new(Rect::new(0, 0, 0, 0)),
            outline_panel: MarkdownOutlinePanel::new(Rect::new(0, 0, 0, 0)),
            show_outline: false,
            outline_revision: u64::MAX,
            gfm_extensions: true,
            code_highlight: true,
            revision: 0,
            metrics: RefCell::new(None),
            preview_cache: HashMap::new(),
            blocks: Vec::new(),
            table_header: Vec::new(),
            blocks_revision: u64::MAX,
            char_widths: Vec::new(),
            line_index: RefCell::new(None),
        }
    }

    /// Returns markdown text.
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Sets markdown text.
    pub fn set_text(&mut self, text: impl Into<String>) {
        let next = text.into();
        self.commit_text(next);
    }

    /// Inserts `text` at the caret and leaves the caret after the inserted run.
    ///
    /// This is the entry point for platform text input (`Event::TextInput` and
    /// `Event::ImeCommit`). The write goes through `commit_text`, so it is one undo step
    /// and emits `text_changed` exactly like every other text mutation.
    pub fn insert_text_at_cursor(&mut self, text: &str) {
        // Control characters are protocol, not keystrokes: a `\n` delivered through
        // `TextInput` must not split a line, and an escape must not enter the document.
        // This is the same filter `Mention` applies to the same event.
        let filtered: String = text.chars().filter(|ch| !ch.is_control()).collect();
        if filtered.is_empty() {
            return;
        }
        // Typing over a selection replaces it in the same undo step.
        self.replace_selection_or_caret(&filtered);
    }

    /// Replaces the selection with `insert`, or inserts at the caret when nothing is selected.
    ///
    /// One commit, so typing over a selection is one undo step rather than a delete followed by
    /// an insert.
    fn replace_selection_or_caret(&mut self, insert: &str) {
        let caret = self.doc_char_offset_of_caret();
        let (start, end) = self.selection.unwrap_or((caret, caret));
        let total = self.total_char_count();
        let start = start.min(total);
        let end = end.min(total).max(start);
        // Only the affected byte range is spliced: the untouched head and tail are copied once
        // instead of the document being rebuilt character by character.
        let start_byte = self.char_offset_to_byte(start);
        let end_byte = self.char_offset_to_byte(end);
        let mut next =
            String::with_capacity(self.text.len() - (end_byte - start_byte) + insert.len());
        next.push_str(&self.text[..start_byte]);
        next.push_str(insert);
        next.push_str(&self.text[end_byte..]);
        let (line, col) = self.line_col_of(start);
        self.cursor_line = line;
        self.cursor_col = col + insert.chars().count();
        self.selection_anchor = None;
        self.apply_selection(None);
        self.commit_text(next);
    }

    /// The caret as a document character offset.
    fn doc_char_offset_of_caret(&self) -> usize {
        self.doc_char_offset(self.cursor_line, self.cursor_col)
    }

    /// The line offset tables, rebuilt at most once per revision.
    fn ensure_line_index(&self) {
        let fresh = self
            .line_index
            .borrow()
            .as_ref()
            .is_some_and(|(revision, _)| *revision == self.revision);
        if !fresh {
            let index = LineIndex::new(&self.text);
            *self.line_index.borrow_mut() = Some((self.revision, index));
        }
    }

    /// Runs `read` against the current line tables.
    fn with_line_index<R>(&self, read: impl FnOnce(&LineIndex) -> R) -> R {
        self.ensure_line_index();
        let index = self.line_index.borrow();
        let (_, table) = index.as_ref().expect("ensure_line_index() populates the table");
        read(table)
    }

    /// The document's character count, including its `\n` separators.
    fn total_char_count(&self) -> usize {
        self.with_line_index(LineIndex::char_len)
    }

    /// The byte offset of a document character offset.
    fn char_offset_to_byte(&self, offset: usize) -> usize {
        self.with_line_index(|index| {
            let line = index.line_of_char(offset);
            let start_byte = index.starts.get(line).copied().unwrap_or(self.text.len());
            let line_end = match index.starts.get(line + 1) {
                Some(next) => next.saturating_sub(1).min(self.text.len()),
                None => self.text.len(),
            };
            let line_length = index.char_lens.get(line).copied().unwrap_or(0);
            let column = offset.saturating_sub(index.char_starts.get(line).copied().unwrap_or(0));
            let column = column.min(line_length);
            start_byte
                + self.text[start_byte..line_end]
                    .char_indices()
                    .nth(column)
                    .map(|(byte, _)| byte)
                    .unwrap_or(line_end - start_byte)
        })
    }

    /// Appends one line to markdown.
    pub fn append_line(&mut self, line: impl AsRef<str>) {
        let line = line.as_ref();
        let needs_separator = !self.text.is_empty();
        let mut next =
            String::with_capacity(self.text.len() + line.len() + usize::from(needs_separator));
        next.push_str(&self.text);
        if needs_separator {
            next.push('\n');
        }
        next.push_str(line);
        self.set_text(next);
        self.cursor_line = self.edit_line_count().saturating_sub(1);
        self.cursor_col = self.current_line_char_len();
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Toggles preview mode.
    pub fn toggle_preview_mode(&mut self) {
        self.preview_mode = !self.preview_mode;
        self.preview_mode_changed.emit(self.preview_mode);
        self.base.request_redraw();
    }

    /// Sets preview mode.
    pub fn set_preview_mode(&mut self, preview_mode: bool) {
        if self.preview_mode == preview_mode {
            return;
        }
        self.preview_mode = preview_mode;
        self.preview_mode_changed.emit(preview_mode);
        self.base.request_redraw();
    }

    /// Returns whether preview mode is enabled.
    pub fn preview_mode(&self) -> bool {
        self.preview_mode
    }

    /// Returns line count.
    pub fn line_count(&self) -> usize {
        self.metrics_snapshot().lines
    }

    /// Returns word count.
    pub fn word_count(&self) -> usize {
        self.metrics_snapshot().words
    }

    /// Returns heading count.
    pub fn heading_count(&self) -> usize {
        self.metrics_snapshot().headings
    }

    /// The document statistics, computed once per revision.
    ///
    /// The three public counters each walked the whole document, and the paint path called all
    /// three per frame just to build its header line. One pass per revision answers all of them.
    fn metrics_snapshot(&self) -> Metrics {
        if let Some((revision, metrics)) = *self.metrics.borrow() {
            if revision == self.revision {
                return metrics;
            }
        }
        let metrics = self.text.lines().fold(Metrics::default(), |mut total, line| {
            total.lines += 1;
            total.words += line.split_whitespace().count();
            total.headings += usize::from(line.trim_start().starts_with('#'));
            total
        });
        *self.metrics.borrow_mut() = Some((self.revision, metrics));
        metrics
    }

    /// Returns headings in document order, as navigation targets.
    pub fn outline(&self) -> Vec<MarkdownOutlineEntry> {
        self.text
            .lines()
            .enumerate()
            .filter_map(|(line, text)| {
                let trimmed = text.trim_start();
                let level = trimmed.chars().take_while(|ch| *ch == '#').count();
                (level > 0).then_some(MarkdownOutlineEntry {
                    line,
                    level: level.min(6) as u8,
                    title: trimmed[level..].trim().to_string(),
                })
            })
            .collect()
    }

    /// Moves the caret to outline entry `index`.
    pub fn goto_outline(&mut self, index: usize) -> bool {
        let Some(entry) = self.outline().get(index).cloned() else {
            return false;
        };
        self.cursor_line = entry.line;
        self.cursor_col = 0;
        // Scroll the target into view rather than leaving it off screen.
        if entry.line < self.first_visible_line {
            self.first_visible_line = entry.line;
        } else if entry.line >= self.first_visible_line + self.visible_line_count() {
            self.first_visible_line = entry.line + 1 - self.visible_line_count();
        }
        self.base.request_redraw();
        true
    }

    /// Returns current line index.
    pub fn cursor_line(&self) -> usize {
        self.cursor_line
    }

    /// Returns the caret's character offset within the current line.
    pub fn cursor_col(&self) -> usize {
        self.cursor_col
    }

    /// The selection as ordered document character offsets.
    pub fn selection(&self) -> Option<(usize, usize)> {
        self.selection
    }

    /// The selected text, when a selection exists.
    pub fn selected_text(&self) -> Option<String> {
        let (start, end) = self.selection?;
        let chars: Vec<char> = self.text.chars().collect();
        let start = start.min(chars.len());
        let end = end.min(chars.len());
        Some(chars[start..end.max(start)].iter().collect())
    }

    /// Selects the whole document.
    pub fn select_all(&mut self) {
        let length = self.text.chars().count();
        self.selection_anchor = Some((0, 0));
        self.apply_selection(if length == 0 { None } else { Some((0, length)) });
        self.cursor_line = self.edit_line_count().saturating_sub(1);
        self.cursor_col = self.current_line_char_len();
        self.base.request_redraw();
    }

    /// Copies the selection to the clipboard, reporting whether anything was copied.
    pub fn copy(&mut self) -> bool {
        let Some(text) = self.selected_text() else {
            return false;
        };
        crate::clipboard::ClipboardManager::set_text(text)
    }

    /// Cuts the selection to the clipboard, reporting whether anything was cut.
    pub fn cut(&mut self) -> bool {
        let Some(text) = self.selected_text() else {
            return false;
        };
        let copied = crate::clipboard::ClipboardManager::set_text(text);
        self.delete_selection();
        copied
    }

    /// Pastes the clipboard at the caret, replacing any selection.
    pub fn paste(&mut self) -> bool {
        let text = crate::clipboard::ClipboardManager::text();
        if text.is_empty() {
            return false;
        }
        let filtered: String = text.chars().filter(|ch| !ch.is_control() || *ch == '\n').collect();
        if filtered.is_empty() {
            return false;
        }
        self.replace_selection_or_caret(&filtered);
        true
    }

    /// Moves the caret to `line` and scrolls it into view.
    pub fn goto_line(&mut self, line: usize) -> bool {
        if line >= self.edit_line_count() {
            return false;
        }
        self.cursor_line = line;
        self.cursor_col = 0;
        self.selection_anchor = None;
        self.apply_selection(None);
        self.reveal_caret();
        self.base.request_redraw();
        true
    }

    /// Finds the next occurrence of `needle` at or after the caret, wrapping to the start.
    ///
    /// The match becomes the selection and the caret moves to its end, which is what makes
    /// repeated calls step through the document instead of finding the same match again.
    pub fn find_next(&mut self, needle: &str) -> bool {
        if needle.is_empty() {
            return false;
        }
        let haystack = &self.text;
        let from = self.find_search_origin(true);
        let found = haystack
            .get(from..)
            .and_then(|tail| tail.find(needle).map(|offset| from + offset))
            .or_else(|| haystack.find(needle));
        let Some(byte) = found else {
            self.find.current = None;
            return false;
        };
        self.find.current = Some(byte);
        self.select_byte_range(byte, byte + needle.len());
        true
    }

    /// Finds the previous occurrence of `needle` before the caret, wrapping to the end.
    pub fn find_previous(&mut self, needle: &str) -> bool {
        if needle.is_empty() {
            return false;
        }
        let haystack = &self.text;
        let from = self.find_search_origin(false);
        let found = haystack
            .get(..from.min(haystack.len()))
            .and_then(|head| head.rfind(needle))
            .or_else(|| haystack.rfind(needle));
        let Some(byte) = found else {
            self.find.current = None;
            return false;
        };
        self.find.current = Some(byte);
        self.select_byte_range(byte, byte + needle.len());
        true
    }

    /// Whether the find bar is showing.
    pub fn find_bar_visible(&self) -> bool {
        self.find.visible
    }

    /// The find bar's current query.
    pub fn find_query(&self) -> &str {
        &self.find.query
    }

    /// Opens the find bar and focuses its query field.
    pub fn open_find(&mut self) {
        self.show_find_bar();
    }

    /// Opens the mounted [`FindBar`] over this editor's text.
    ///
    /// This is the host-facing form of [`Self::open_find`]: the bar is told the current document
    /// snapshot and the query the editor already holds, so the painted counter and the editor's
    /// Enter-to-next match refer to the same bytes.
    pub fn show_find_bar(&mut self) {
        self.find.visible = true;
        self.find_bar.open();
        let text = self.text.clone();
        self.find_bar.set_text(&text);
        if !self.find.query.is_empty() {
            self.find_bar.set_query(self.find.query.clone());
        }
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Closes the find bar, leaving the document and the current match untouched.
    pub fn close_find(&mut self) {
        if self.find.visible {
            self.find.visible = false;
            self.find_bar.close();
            self.base.request_layout();
            self.base.request_redraw();
        }
    }

    /// The mounted find/replace bar.
    pub fn find_bar(&self) -> &FindBar {
        &self.find_bar
    }

    /// The mounted find/replace bar, for a host that wants to set its replacement or policy.
    pub fn find_bar_mut(&mut self) -> &mut FindBar {
        &mut self.find_bar
    }

    /// The mounted document outline.
    pub fn outline_panel(&self) -> &MarkdownOutlinePanel {
        &self.outline_panel
    }

    /// The mounted document outline, for a host that wants to set its width or listen to it.
    pub fn outline_panel_mut(&mut self) -> &mut MarkdownOutlinePanel {
        &mut self.outline_panel
    }

    /// Whether the outline panel is painted.
    pub fn show_outline(&self) -> bool {
        self.show_outline
    }

    /// Shows or hides the outline panel inside the editor's rectangle.
    pub fn set_show_outline(&mut self, show: bool) {
        if self.show_outline == show {
            return;
        }
        self.show_outline = show;
        if show {
            self.sync_outline();
        }
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Rebuilds the mounted panel from this editor and marks the caret's heading.
    ///
    /// Entries are rebuilt only when the document revision changed; the active row is re-derived
    /// from the caret every call, so moving the caret is O(headings), not O(document).
    pub fn sync_outline(&mut self) {
        if self.outline_revision != self.revision {
            let entries = self.outline();
            self.outline_panel.set_entries(entries);
            self.outline_revision = self.revision;
        }
        let caret = self.cursor_line;
        let active = self
            .outline_panel
            .entries()
            .iter()
            .rposition(|entry| entry.line <= caret)
            .or_else(|| (!self.outline_panel.entries().is_empty()).then_some(0));
        self.outline_panel.set_active_index(active);
    }

    /// Points a [`FindBar`] at this editor's document.
    ///
    /// The bar keeps a snapshot of the text its byte offsets address, and it cannot observe an
    /// edit, so this is the call that keeps the two in step. The editor's own in-place find state
    /// is untouched: the bar is a second, reusable view over the same bytes.
    pub fn sync_find_bar(&self, bar: &mut FindBar) {
        bar.set_text(&self.text);
    }

    /// Applies a [`FindBar`] replacement request as one undo step.
    ///
    /// Returns how many ranges were replaced. A request whose ranges are out of order, overlap, or
    /// fall outside the document applies **nothing** and returns zero: a partial application would
    /// leave the text in a state neither the reader nor the bar asked for. Valid ranges are
    /// spliced from the end backwards so every earlier offset stays meaningful.
    pub fn apply_replace_request(&mut self, request: &ReplaceRequest) -> usize {
        let len = self.text.len();
        let mut last_start = len;
        for &(start, end) in request.ranges.iter().rev() {
            if start > end || end > len || end > last_start {
                return 0;
            }
            last_start = start;
        }
        if request.ranges.is_empty() {
            return 0;
        }
        let mut next = self.text.clone();
        for &(start, end) in request.ranges.iter().rev() {
            next.replace_range(start..end, &request.text);
        }
        let replaced = request.ranges.len();
        self.commit_text(next);
        // The bar holds a document snapshot, so it is re-pointed at the text the replacement just
        // produced; and it is told how many ranges actually changed, which is the feedback the
        // replacement row paints.
        self.find_bar.set_text(&self.text);
        self.find_bar.set_last_replaced(replaced);
        replaced
    }

    /// Advances the caret's blink by `delta_ms` and reports whether another frame is owed.
    pub fn tick(&mut self, delta_ms: u32) -> bool {
        let owes_frame = self.cursor_blink.tick(delta_ms);
        if owes_frame {
            self.base.request_redraw();
        }
        owes_frame
    }

    /// Whether the caret is drawn on the current frame.
    pub fn is_caret_visible(&self) -> bool {
        self.cursor_blink.is_visible()
    }

    /// Whether the editor paints a scrollbar.
    pub fn show_scrollbar(&self) -> bool {
        self.show_scrollbar
    }

    /// Shows or hides the scrollbar.
    pub fn set_show_scrollbar(&mut self, show: bool) {
        if self.show_scrollbar == show {
            return;
        }
        self.show_scrollbar = show;
        self.base.request_redraw();
    }

    /// Whether GFM extensions are parsed in preview.
    pub fn gfm_extensions(&self) -> bool {
        self.gfm_extensions
    }

    /// Enables or disables the GFM extension set.
    pub fn set_gfm_extensions(&mut self, enabled: bool) {
        if self.gfm_extensions == enabled {
            return;
        }
        self.gfm_extensions = enabled;
        self.preview_cache.clear();
        self.base.request_redraw();
    }

    /// Whether fenced code blocks are syntax-highlighted.
    pub fn code_highlight(&self) -> bool {
        self.code_highlight
    }

    /// Enables or disables fenced-code highlighting.
    pub fn set_code_highlight(&mut self, enabled: bool) {
        if self.code_highlight == enabled {
            return;
        }
        self.code_highlight = enabled;
        self.preview_cache.clear();
        self.base.request_redraw();
    }

    /// The configured undo depth.
    pub fn undo_stack_limit(&self) -> usize {
        self.undo_stack_limit
    }

    /// Sets the undo depth and trims the existing history to it.
    pub fn set_undo_limit(&mut self, limit: usize) {
        let limit = limit.max(1);
        if self.undo_stack_limit == limit {
            return;
        }
        self.undo_stack_limit = limit;
        self.undo_stack.set_max_capacity(limit);
    }

    /// The IME composition's screen rectangle, for a host placing its candidate window.
    ///
    /// The rectangle starts at the composition caret — the insertion point inside the preedit —
    /// not at the start of the composition, because that is where the candidate window belongs.
    /// Returns `None` when no composition is in progress: a rectangle for text that is not on
    /// screen would put the candidate window somewhere the user is not typing.
    pub fn preedit_screen_rect(&self) -> Option<Rect> {
        let preedit = self.preedit.as_ref()?;
        if preedit.text.is_empty() {
            return None;
        }
        let rect = self.geometry();
        let font = effective_font(self.base.style());
        let (line_start, line_end) = self.cursor_line_span();
        let caret_line = &self.text[line_start..line_end];
        let caret_byte = byte_offset_of_char(caret_line, self.cursor_col);
        let before_caret = &preedit.text[..char_boundary_at(&preedit.text, preedit.cursor)];
        let x = rect.x
            + 12
            + estimate_text_width(&caret_line[..caret_byte], font, 1.0) as i32
            + estimate_text_width(before_caret, font, 1.0) as i32;
        let row = self.cursor_line.saturating_sub(self.visible_start());
        let y = rect.y + TEXT_TOP + (row as i32) * TEXT_ROW_HEIGHT;
        let width = estimate_text_width(&preedit.text, font, 1.0).max(1);
        Some(Rect::new(x, y, width, TEXT_ROW_HEIGHT as u32))
    }

    /// Moves the caret to the start or the end of the current line.
    pub fn move_to_line_edge(&mut self, start: bool) {
        self.cursor_col = if start { 0 } else { self.current_line_char_len() };
        self.selection_anchor = None;
        self.apply_selection(None);
        self.base.request_redraw();
    }

    /// Scrolls the viewport by whole visible pages.
    ///
    /// A positive `direction` moves toward the end of the document. Returns whether the viewport
    /// moved.
    pub fn scroll_page(&mut self, direction: i32) -> bool {
        let page = self.visible_line_count().max(1) as i32;
        let moved = self.scroll_lines(direction.signum() * page);
        if moved {
            self.base.request_redraw();
        }
        moved
    }

    /// Stores a selection and reports the presence change.
    fn apply_selection(&mut self, next: Option<(usize, usize)>) {
        if self.selection == next {
            return;
        }
        self.selection = next;
        self.selection_changed.emit(next.is_some());
    }

    /// Selects the document characters spanned by the byte range `[start, end)`.
    fn select_byte_range(&mut self, start: usize, end: usize) {
        let start_char = self.text[..start.min(self.text.len())].chars().count();
        let end_char = start_char
            + self.text[start.min(self.text.len())..end.min(self.text.len())].chars().count();
        self.selection_anchor = Some(self.line_col_of(start_char));
        self.apply_selection(Some((start_char, end_char)));
        let (line, col) = self.line_col_of(end_char);
        self.cursor_line = line;
        self.cursor_col = col;
        self.reveal_caret();
        self.base.request_redraw();
    }

    /// Where the next forward/backward search starts.
    fn find_search_origin(&self, forward: bool) -> usize {
        let caret = self.cursor_byte_offset();
        match self.find.current {
            Some(current) if forward => current + 1,
            Some(current) => current,
            None if forward => caret,
            None => caret,
        }
    }

    /// Deletes the selected characters, if any, and reports whether anything was deleted.
    fn delete_selection(&mut self) -> bool {
        if self.selection.is_none() {
            return false;
        }
        self.replace_selection_or_caret("");
        true
    }

    /// Maps a document character offset to `(line, column)`.
    ///
    /// The lookup is a binary search over the cached line starts, so it costs O(log lines)
    /// rather than a walk from the top of the document on every caret move.
    fn line_col_of(&self, offset: usize) -> (usize, usize) {
        self.with_line_index(|index| {
            let line = index.line_of_char(offset);
            let start = index.char_starts.get(line).copied().unwrap_or(0);
            let length = index.char_lens.get(line).copied().unwrap_or(0);
            (line, offset.saturating_sub(start).min(length))
        })
    }

    /// Keeps the caret in view after a caret move or a search hit.
    fn reveal_caret(&mut self) {
        let visible = self.visible_line_count().max(1);
        if self.cursor_line < self.first_visible_line {
            self.first_visible_line = self.cursor_line;
        } else if self.cursor_line >= self.first_visible_line + visible {
            self.first_visible_line = self.cursor_line + 1 - visible;
        }
    }

    /// Reverts the most recent markdown text mutation.
    ///
    /// Returns `false` and changes nothing when there is nothing to undo;
    /// otherwise requests layout and redraw and clamps `cursor_line`.
    pub fn undo(&mut self) -> bool {
        // The undo commands splice into their shared scratch buffer, which is therefore synced
        // with the live document before the stack touches it. Syncing here (rather than after
        // every keystroke) keeps the edit path free of a whole-document copy.
        self.sync_history_target();
        if self.undo_stack.undo().is_err() {
            return false;
        }
        self.restore_history_text();
        true
    }

    /// Reapplies the most recently undone markdown text mutation.
    ///
    /// Returns `false` and changes nothing when there is nothing to redo;
    /// otherwise requests layout and redraw.
    pub fn redo(&mut self) -> bool {
        self.sync_history_target();
        if self.undo_stack.redo().is_err() {
            return false;
        }
        self.restore_history_text();
        true
    }

    /// Returns whether there is a markdown text mutation to undo.
    pub fn can_undo(&self) -> bool {
        self.undo_stack.can_undo()
    }

    /// Returns whether there is an undone markdown text mutation to reapply.
    pub fn can_redo(&self) -> bool {
        self.undo_stack.can_redo()
    }

    /// Copies the live document into the buffer the undo commands splice into.
    ///
    /// The buffer is only read by [`UndoStack`] while an undo/redo runs, so it can lag the live
    /// text by any number of edits without becoming wrong.
    fn sync_history_target(&mut self) {
        let mut target = self.history_target.borrow_mut();
        if target.as_str() != self.text {
            target.clear();
            target.push_str(&self.text);
        }
    }

    fn restore_history_text(&mut self) {
        self.restoring_history = true;
        self.text = self.history_target.borrow().clone();
        self.restoring_history = false;
        self.revision = self.revision.wrapping_add(1);
        self.clamp_cursor();
        self.selection_anchor = None;
        self.apply_selection(None);
        self.text_changed.emit(self.text.clone());
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Number of editable lines.
    ///
    /// This differs from [`Self::line_count`] only for a trailing newline: `str::lines`
    /// drops the final empty line, but an editor must be able to keep the caret on it,
    /// which is exactly where Enter at the end of the document leaves it.
    fn edit_line_count(&self) -> usize {
        self.with_line_index(|index| index.starts.len())
    }

    /// How many text rows fit below the header band.
    ///
    /// At least one, so a short editor still paints the line it is on instead of dividing to
    /// zero or scrolling everything away.
    fn visible_line_count(&self) -> usize {
        let usable = self.geometry().height as i32 - TEXT_TOP - TEXT_BOTTOM_INSET;
        if usable < 0 {
            return 1;
        }
        (usable / TEXT_ROW_HEIGHT + 1) as usize
    }

    /// The first line the viewport actually starts at, clamped to the last full window.
    fn visible_start(&self) -> usize {
        let max_first = self.line_count().saturating_sub(self.visible_line_count());
        self.first_visible_line.min(max_first)
    }

    /// Scrolls the viewport by `lines` rows toward the end of the document.
    ///
    /// Positive values reveal later lines, negative values reveal earlier ones. Returns whether
    /// the viewport moved, so the caller repaints only on a change.
    fn scroll_lines(&mut self, lines: i32) -> bool {
        if lines == 0 {
            return false;
        }
        let max_first = self.line_count().saturating_sub(self.visible_line_count());
        let next =
            (self.first_visible_line as i64 + lines as i64).clamp(0, max_first as i64) as usize;
        if next == self.first_visible_line {
            return false;
        }
        self.first_visible_line = next;
        true
    }

    /// Byte span of the caret's line in `text`.
    ///
    /// Returns an empty span at the end of the document when the caret is out of range, so
    /// callers cannot index past the string even before [`Self::clamp_cursor`] runs.
    fn cursor_line_span(&self) -> (usize, usize) {
        self.with_line_index(|index| {
            let Some(&start) = index.starts.get(self.cursor_line) else {
                return (self.text.len(), self.text.len());
            };
            let end = match index.starts.get(self.cursor_line + 1) {
                Some(next) => next.saturating_sub(1),
                None => self.text.len(),
            };
            (start, end.max(start))
        })
    }

    /// Byte offset of the caret in `text`.
    fn cursor_byte_offset(&self) -> usize {
        let (start, end) = self.cursor_line_span();
        let line = &self.text[start..end];
        start
            + line
                .char_indices()
                .nth(self.cursor_col)
                .map(|(offset, _)| offset)
                .unwrap_or(line.len())
    }

    /// Character length of the caret's line.
    fn current_line_char_len(&self) -> usize {
        self.with_line_index(|index| index.char_lens.get(self.cursor_line).copied().unwrap_or(0))
    }

    /// Keeps `cursor_line` / `cursor_col` inside the document.
    fn clamp_cursor(&mut self) {
        self.cursor_line = self.cursor_line.min(self.edit_line_count().saturating_sub(1));
        self.clamp_cursor_col();
    }

    /// Keeps `cursor_col` inside the caret's line.
    fn clamp_cursor_col(&mut self) {
        self.cursor_col = self.cursor_col.min(self.current_line_char_len());
    }

    /// Applies a new document state through the undo snapshot route.
    ///
    /// Every text mutation funnels through here so a single place owns the history
    /// snapshot, the `restoring_history` guard, the caret clamp and the signal.
    fn commit_text(&mut self, next: String) {
        if self.text == next {
            return;
        }
        let before = core::mem::take(&mut self.text);
        self.text = next;
        self.revision = self.revision.wrapping_add(1);
        if !self.restoring_history {
            // The delta command records only the changed run. The shared buffer is *not* refreshed
            // here: it is synced once before an undo/redo, which is the only moment it is read.
            self.undo_stack.push(Box::new(MarkdownTextDeltaCommand::new(
                self.history_target.clone(),
                &before,
                &self.text,
            )));
        }
        self.clamp_cursor();
        self.text_changed.emit(self.text.clone());
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Rebuilds the per-line block classification when the document changed.
    ///
    /// The preview needs to know which lines are inside a fenced code block before it can parse
    /// them, and that fact depends on every line above. Computing it once per revision keeps it
    /// out of the paint loop; the lexer state carried through the fence is what lets a block
    /// comment that spans lines continue to be highlighted.
    fn ensure_blocks(&mut self) {
        if self.blocks_revision == self.revision {
            return;
        }
        let mut blocks = Vec::new();
        let mut fence: Option<(LanguageId, LineState)> = None;
        // One lexer per fenced block, carried across its lines. A fresh highlighter per line reset
        // the interpreter state a multi-line construct depends on (block comments, template
        // literals) and re-created the lexer for every line of a long block.
        let mut highlighter: Option<(LanguageId, BuiltinHighlighter)> = None;
        for line in self.text.split('\n') {
            let trimmed = line.trim_start();
            match fence {
                Some((language, state)) => {
                    if fence_marker(trimmed).is_some() {
                        blocks.push(BlockStart::Fence);
                        fence = None;
                        highlighter = None;
                    } else {
                        blocks.push(BlockStart::Code(language, state));
                        if !highlighter.as_ref().is_some_and(|(active, _)| *active == language) {
                            highlighter = Some((language, BuiltinHighlighter::new(language)));
                        }
                        let next = match highlighter.as_ref() {
                            Some((_, lexer)) => lexer.highlight_line(line, state).1,
                            // Unreachable: the branch above always installs a lexer.
                            None => state,
                        };
                        fence = Some((language, next));
                    }
                }
                None => match fence_marker(trimmed) {
                    Some(info) => {
                        blocks.push(BlockStart::Fence);
                        let language = language_from_info(info);
                        highlighter = Some((language, BuiltinHighlighter::new(language)));
                        fence = Some((language, LineState::initial()));
                    }
                    None => blocks.push(BlockStart::Normal),
                },
            }
        }
        self.blocks = blocks;
        // GFM recognises a table by its *delimiter row*, so whether a line is a header depends on
        // the line after it. Resolving that once per revision keeps the paint path from looking
        // ahead per row.
        let lines: Vec<&str> = self.text.split('\n').collect();
        self.table_header = (0..lines.len())
            .map(|index| {
                lines.get(index + 1).is_some_and(|next| is_table_divider(next.trim_start()))
            })
            .collect();
        self.blocks_revision = self.revision;
    }

    /// Wraps the selection in `prefix`/`suffix`, or removes the pair when it is already there.
    ///
    /// This is the single formatting entry point: the keyboard chords below and the mounted
    /// [`MarkdownToolbar`] both call it, so a toolbar button and its shortcut cannot diverge. The
    /// write goes through `Self::commit_text`, so the wrap is one undo step.
    ///
    /// Returns whether the document changed.
    pub fn wrap_selection(&mut self, prefix: &str, suffix: &str) -> bool {
        if prefix.is_empty() && suffix.is_empty() {
            return false;
        }
        let caret = self.doc_char_offset_of_caret();
        let (start, end) = self.selection.unwrap_or((caret, caret));
        let start_byte = self.char_offset_to_byte(start);
        let end_byte = self.char_offset_to_byte(end);
        let wrapped =
            self.text[..start_byte].ends_with(prefix) && self.text[end_byte..].starts_with(suffix);
        let (next, caret_char, selection) = if wrapped {
            let mut next = String::with_capacity(self.text.len());
            next.push_str(&self.text[..start_byte - prefix.len()]);
            next.push_str(&self.text[start_byte..end_byte]);
            next.push_str(&self.text[end_byte + suffix.len()..]);
            let prefix_chars = prefix.chars().count();
            let selection =
                self.selection.map(|(first, last)| (first - prefix_chars, last - prefix_chars));
            (next, start - prefix_chars, selection)
        } else {
            let mut next = String::with_capacity(self.text.len() + prefix.len() + suffix.len());
            next.push_str(&self.text[..start_byte]);
            next.push_str(prefix);
            next.push_str(&self.text[start_byte..end_byte]);
            next.push_str(suffix);
            next.push_str(&self.text[end_byte..]);
            let prefix_chars = prefix.chars().count();
            let suffix_chars = suffix.chars().count();
            let selection =
                self.selection.map(|(first, last)| (first + prefix_chars, last + prefix_chars));
            // With no selection the caret lands between the two markers, which is where typing
            // the emphasised text begins.
            (next, end + prefix_chars + suffix_chars, selection)
        };
        self.commit_text(next);
        let (line, column) = self.line_col_of(caret_char);
        self.cursor_line = line;
        self.cursor_col = column;
        self.apply_selection(selection);
        self.base.request_redraw();
        true
    }

    /// Adds `prefix` to every selected line, or removes it when every selected line has it.
    ///
    /// A caret with no selection applies to its own line. The document is rewritten once and the
    /// change is one undo step, so a multi-line list toggle can be reverted in one action.
    pub fn toggle_line_prefix(&mut self, prefix: &str) -> bool {
        if prefix.is_empty() {
            return false;
        }
        let lines = self.selected_line_range();
        let all_prefixed = lines.iter().all(|line| {
            let text = self.line_slice(*line);
            text.is_empty() || text.starts_with(prefix)
        });
        self.apply_line_prefix(prefix, all_prefixed)
    }

    /// Adds one indent level to every selected line.
    fn indent_lines(&mut self) {
        const INDENT: &str = "  ";
        let _ = self.apply_line_prefix(INDENT, false);
    }

    /// Adds `prefix` to (or removes it from) every selected line, as one undo step.
    fn apply_line_prefix(&mut self, prefix: &str, remove: bool) -> bool {
        let had_selection = self.selection.is_some();
        let lines = self.selected_line_range();
        if lines.is_empty() {
            return false;
        }
        let starts = self.line_starts();
        let mut next = String::with_capacity(self.text.len() + lines.len() * prefix.len());
        let mut cursor = 0usize;
        for line in &lines {
            let Some(&start_byte) = starts.get(*line) else {
                continue;
            };
            let end_byte = match starts.get(line + 1) {
                Some(next) => next.saturating_sub(1),
                None => self.text.len(),
            };
            next.push_str(&self.text[cursor..start_byte]);
            let text = &self.text[start_byte..end_byte];
            if remove {
                next.push_str(text.strip_prefix(prefix).unwrap_or(text));
            } else {
                next.push_str(prefix);
                next.push_str(text);
            }
            cursor = end_byte;
        }
        next.push_str(&self.text[cursor..]);
        self.commit_text(next);
        // The selection follows the edited lines rather than being dropped: Tab then Shift+Tab is
        // one gesture on one block, and a reader would not expect the second key to apply to a
        // single line.
        let first = lines.first().copied().unwrap_or(0);
        let last = lines.last().copied().unwrap_or(first).min(self.edit_line_count() - 1);
        self.cursor_line = last;
        self.cursor_col = self.current_line_char_len();
        let start_char = self.doc_char_offset(first, 0);
        let end_char = self.doc_char_offset(last, self.current_line_char_len());
        self.selection_anchor = None;
        self.apply_selection(if had_selection {
            (start_char != end_char).then_some((start_char, end_char))
        } else {
            // A caret with no selection keeps a caret: indenting the line it is on must not
            // silently select the line.
            None
        });
        self.base.request_redraw();
        true
    }

    /// Every line the current selection touches, or the caret's line when nothing is selected.
    fn selected_line_range(&self) -> Vec<usize> {
        let (first, last) = match self.selection {
            Some((start, end)) => {
                let (first, _) = self.line_col_of(start);
                let (last, _) = self.line_col_of(end);
                (first, last)
            }
            None => (self.cursor_line, self.cursor_line),
        };
        let last = last.min(self.edit_line_count().saturating_sub(1));
        if first > last {
            return Vec::new();
        }
        (first..=last).collect()
    }

    /// The content of `line`, without its `\n`.
    fn line_slice(&self, line: usize) -> &str {
        self.with_line_index(|index| {
            let Some(&start_byte) = index.starts.get(line) else {
                return "";
            };
            let end_byte = match index.starts.get(line + 1) {
                Some(next) => next.saturating_sub(1),
                None => self.text.len(),
            };
            &self.text[start_byte..end_byte]
        })
    }

    /// Indents (`indent == true`) or outdents the selected lines by one level.
    fn shift_line_indent(&mut self, indent: bool) {
        if indent {
            self.indent_lines();
            return;
        }
        let had_selection = self.selection.is_some();
        const INDENT: &str = "  ";
        // Outdent removes one indent step, or a single tab, from each selected line.
        let lines = self.selected_line_range();
        let starts = self.line_starts();
        let mut removals: Vec<(usize, usize)> = Vec::new();
        for line in &lines {
            let Some(&start_byte) = starts.get(*line) else {
                continue;
            };
            let end_byte = match starts.get(line + 1) {
                Some(next) => next.saturating_sub(1),
                None => self.text.len(),
            };
            let text = &self.text[start_byte..end_byte];
            if let Some(rest) = text.strip_prefix(INDENT) {
                removals.push((start_byte, start_byte + (text.len() - rest.len())));
            } else if text.starts_with('\t') {
                removals.push((start_byte, start_byte + 1));
            }
        }
        if removals.is_empty() {
            return;
        }
        let mut next = String::with_capacity(self.text.len());
        let mut cursor = 0usize;
        for (start_byte, end_byte) in removals {
            next.push_str(&self.text[cursor..start_byte]);
            cursor = end_byte;
        }
        next.push_str(&self.text[cursor..]);
        self.commit_text(next);
        let first = lines.first().copied().unwrap_or(0);
        let last = lines.last().copied().unwrap_or(first).min(self.edit_line_count() - 1);
        self.cursor_line = last;
        self.clamp_cursor_col();
        let start_char = self.doc_char_offset(first, 0);
        let end_char = self.doc_char_offset(last, self.current_line_char_len());
        self.selection_anchor = None;
        self.apply_selection(if had_selection {
            (start_char != end_char).then_some((start_char, end_char))
        } else {
            None
        });
        self.base.request_redraw();
    }

    /// Byte offsets of every line's first character, from the cached tables.
    fn line_starts(&self) -> Vec<usize> {
        self.with_line_index(|index| index.starts.clone())
    }

    /// Deletes the character before the caret, merging lines at a line start.
    fn backspace_at_cursor(&mut self) {
        if self.delete_selection() {
            return;
        }
        self.clamp_cursor();
        let at = self.cursor_byte_offset();
        if self.cursor_col > 0 {
            let (start, _) = self.cursor_line_span();
            let removed = self.text[start..at].chars().next_back().map(char::len_utf8).unwrap_or(0);
            let mut next = String::with_capacity(self.text.len());
            next.push_str(&self.text[..at - removed]);
            next.push_str(&self.text[at..]);
            self.cursor_col -= 1;
            self.commit_text(next);
        } else if self.cursor_line > 0 {
            let (start, _) = self.cursor_line_span();
            let merged_col =
                self.text[..start - 1].chars().rev().take_while(|&ch| ch != '\n').count();
            let mut next = String::with_capacity(self.text.len());
            next.push_str(&self.text[..start - 1]);
            next.push_str(&self.text[start..]);
            self.cursor_line -= 1;
            self.cursor_col = merged_col;
            self.commit_text(next);
        }
    }

    /// Splits the caret's line at the caret.
    fn insert_newline_at_cursor(&mut self) {
        self.delete_selection();
        self.clamp_cursor();
        let at = self.cursor_byte_offset();
        let mut next = String::with_capacity(self.text.len() + 1);
        next.push_str(&self.text[..at]);
        next.push('\n');
        next.push_str(&self.text[at..]);
        self.cursor_line += 1;
        self.cursor_col = 0;
        self.commit_text(next);
    }

    /// Moves the caret one character left, wrapping to the previous line's end.
    fn move_cursor_left(&mut self) {
        self.clamp_cursor();
        if self.cursor_col > 0 {
            self.cursor_col -= 1;
            self.base.request_redraw();
        } else if self.cursor_line > 0 {
            self.cursor_line -= 1;
            self.cursor_col = self.current_line_char_len();
            self.base.request_redraw();
        }
    }

    /// Moves the caret one character right, wrapping to the next line's start.
    fn move_cursor_right(&mut self) {
        self.clamp_cursor();
        if self.cursor_col < self.current_line_char_len() {
            self.cursor_col += 1;
            self.base.request_redraw();
        } else if self.cursor_line + 1 < self.edit_line_count() {
            self.cursor_line += 1;
            self.cursor_col = 0;
            self.base.request_redraw();
        }
    }

    fn move_cursor(&mut self, delta: isize) {
        let lines = self.edit_line_count();
        let current = self.cursor_line as isize;
        let next = (current + delta).clamp(0, lines.saturating_sub(1) as isize) as usize;
        if next != self.cursor_line {
            self.cursor_line = next;
            // A column past the new line's end would put the caret outside the text, so
            // vertical movement keeps the column but clamps it to the line it landed on.
            self.clamp_cursor_col();
            self.base.request_redraw();
        }
    }
}

impl Widget for MarkdownEditor {
    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(500, 300)
    }

    /// One frame of the caret's blink. The frame bus calls this; nothing else does.
    fn tick(&mut self, delta_ms: u32) -> bool {
        MarkdownEditor::tick(self, delta_ms)
    }

    /// A blinking caret always owes frames while it runs.
    fn is_animating(&self) -> bool {
        self.cursor_blink.is_running()
    }

    impl_draw_bridge!();
    impl_widget_property_hooks!();
    /// Resolves a published event name to the control's own signal.
    ///
    /// # Why this is explicit per control
    ///
    /// `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 it a name
    /// is valid and inert, which is the silent failure `tools/check_event_signal_dyn.sh` exists to
    /// make impossible. The arm set is checked against the capability's published names, so this
    /// list cannot drift from what the control advertises.
    fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
        use crate::signal::EventSignalRef;
        #[allow(unused_imports)]
        use crate::widget::capability::CapabilityValue;
        match name {
            "text_changed" => {
                Some(EventSignalRef::mapped("text_changed", &self.text_changed, |v| {
                    CapabilityValue::String(v.clone())
                }))
            }
            "preview_mode_changed" => Some(EventSignalRef::mapped(
                "preview_mode_changed",
                &self.preview_mode_changed,
                |v| CapabilityValue::Bool(*v),
            )),
            "selection_changed" => {
                Some(EventSignalRef::mapped("selection_changed", &self.selection_changed, |v| {
                    CapabilityValue::Bool(*v)
                }))
            }
            "heading_activated" => {
                Some(EventSignalRef::mapped("heading_activated", &self.heading_activated, |v| {
                    CapabilityValue::UInt(*v as u64)
                }))
            }
            _ => None,
        }
    }
}

/// `MarkdownEditor`'s property contract.
///
/// `text` writes go through `set_text`, which keeps the undo stack and the
/// emitted `text_changed` signal in step with the stored document; writing the
/// field directly would make the editor's own history wrong. `cursor_line` is
/// read-only because the editor owns caret placement.
impl WidgetProperties for MarkdownEditor {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "text" => Ok(CapabilityValue::String(self.text().to_string())),
            "preview_mode" => Ok(CapabilityValue::Bool(self.preview_mode())),
            "line_count" => Ok(CapabilityValue::UInt(self.line_count() as u64)),
            "word_count" => Ok(CapabilityValue::UInt(self.word_count() as u64)),
            "heading_count" => Ok(CapabilityValue::UInt(self.heading_count() as u64)),
            "cursor_line" => Ok(CapabilityValue::UInt(self.cursor_line() as u64)),
            "show_scrollbar" => Ok(CapabilityValue::Bool(self.show_scrollbar)),
            "gfm_extensions" => Ok(CapabilityValue::Bool(self.gfm_extensions)),
            "code_highlight" => Ok(CapabilityValue::Bool(self.code_highlight)),
            "undo_stack_limit" => Ok(CapabilityValue::UInt(self.undo_stack_limit as u64)),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "text" => match value {
                CapabilityValue::String(text) => {
                    self.set_text(text);
                    Ok(())
                }
                _ => Err(CapabilityAccessError::TypeMismatch),
            },
            "preview_mode" => match value {
                CapabilityValue::Bool(enabled) => {
                    self.set_preview_mode(enabled);
                    Ok(())
                }
                _ => Err(CapabilityAccessError::TypeMismatch),
            },
            "line_count" | "word_count" | "heading_count" | "cursor_line" => {
                Err(CapabilityAccessError::ReadOnlyProperty)
            }
            "show_scrollbar" => {
                self.set_show_scrollbar(expect_bool(value)?);
                Ok(())
            }
            "gfm_extensions" => {
                self.set_gfm_extensions(expect_bool(value)?);
                Ok(())
            }
            "code_highlight" => {
                self.set_code_highlight(expect_bool(value)?);
                Ok(())
            }
            "undo_stack_limit" => {
                self.set_undo_limit(expect_usize(value)?);
                Ok(())
            }
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "text",
            "preview_mode",
            "line_count",
            "word_count",
            "heading_count",
            "cursor_line",
            "show_scrollbar",
            "gfm_extensions",
            "code_highlight",
            "undo_stack_limit",
            BASE_PROPERTY_NAMES
        ]
    }

    /// Runs one of the commands `markdown_editor` publishes.
    ///
    /// `toggle_preview_mode` and `undo` are the genuine zero-argument actions here;
    /// `undo` reports `false` when there is nothing to undo, which is the "could not
    /// handle it" case, so it is answered with
    /// [`CapabilityAccessError::OutOfRange`] rather than a success that did nothing.
    /// `append_line` takes the line to append and `set_text` / `set_preview_mode`
    /// assign state through the property route, so those are refused the same way —
    /// the names are right and the payload is what is missing.
    fn command(&mut self, name: &str) -> Result<(), CapabilityAccessError> {
        match name {
            "toggle_preview_mode" => {
                self.toggle_preview_mode();
                Ok(())
            }
            "undo" => {
                if self.undo() {
                    Ok(())
                } else {
                    Err(CapabilityAccessError::OutOfRange)
                }
            }
            "append_line" | "set_text" | "set_preview_mode" => {
                Err(CapabilityAccessError::OutOfRange)
            }
            // Any other `set_foo` name carries its value through the property route,
            // so the shared default reports that a payload is needed rather than
            // claiming the control has never heard of it.
            _ if name.starts_with("set_") => Err(CapabilityAccessError::OutOfRange),
            _ => Err(CapabilityAccessError::UnknownCommand),
        }
    }
}

impl EventHandler for MarkdownEditor {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if !self.base.is_enabled() {
            self.cursor_blink.stop();
            return;
        }
        if !self.cursor_blink.is_running() {
            self.cursor_blink.start();
        }
        // The side panel and find bar are hit-tested in this widget's coordinates, so their boxes
        // are refreshed before the press is resolved.
        self.layout_companions();

        match event {
            // The find bar owns text input while it is open: otherwise typing a query would edit
            // the document behind it. The mounted bar is kept in step through the same query, so
            // the painted counter always describes the text Enter will search.
            Event::TextInput { text } if self.find.visible => {
                self.find.query.push_str(text);
                self.find.current = None;
                self.find_bar.set_query(self.find.query.clone());
                self.base.request_redraw();
            }
            Event::ImeCommit { text } if self.find.visible => {
                self.find.query.push_str(text);
                self.find.current = None;
                self.find_bar.set_query(self.find.query.clone());
                self.base.request_redraw();
            }
            // Printable text and IME commits are what the platform delivers for typing; a
            // handler with only a `KeyPress` arm never sees them. Both insert at the caret
            // through the same undo-guarded path as `set_text` (the filter mirrors
            // `Mention`, which refuses control characters and empty runs).
            Event::TextInput { text }
                if text.chars().all(|ch| !ch.is_control()) && !text.is_empty() =>
            {
                self.preedit = None;
                self.insert_text_at_cursor(text);
            }
            Event::ImeCommit { text } => {
                self.preedit = None;
                self.insert_text_at_cursor(text);
            }
            Event::ImePreedit { text, cursor } => {
                // The composition has to be visible before it is committed, and the caret inside
                // it is what anchors the candidate window.
                self.preedit =
                    Some(Preedit { text: text.clone(), cursor: (*cursor).min(text.len()) });
                self.base.request_redraw();
            }
            Event::KeyPress { key, modifiers } => self.handle_key(*key, *modifiers),
            // Wheel: positive `delta.y` moves the content down, so the viewport moves toward the
            // start. Three lines per notch is the desktop default shared with the other text
            // controls.
            Event::MousePress { pos, button: 1, modifiers, .. } => {
                let rect = self.geometry();
                // The mounted outline is a real control: a press in its column selects the row and
                // navigates to its line, exactly as a standalone panel would.
                if let Some(panel_rect) = self.outline_rect() {
                    if panel_rect.contains_point(*pos) {
                        if let Some(index) = self.outline_panel.entry_at(*pos) {
                            if let Some(line) = self.outline_panel.goto(index) {
                                self.heading_activated.emit(index);
                                let _ = self.goto_line(line);
                            }
                        }
                        return;
                    }
                }
                // The mounted find bar owns presses inside its own band (the `Aa`/`W` toggles and
                // the query band); the document caret must not move for them.
                if self.find.visible && self.find_bar_rect().contains_point(*pos) {
                    self.find_bar.handle_event(event);
                    self.find.query = self.find_bar.query().to_string();
                    self.base.request_redraw();
                    return;
                }
                if self.show_scrollbar && self.scrollbar_track_contains(*pos) {
                    let on_thumb = self.scrollbar_thumb().is_some_and(|thumb| {
                        pos.x >= thumb.x
                            && pos.x < thumb.x + thumb.width as i32
                            && pos.y >= thumb.y
                            && pos.y < thumb.y + thumb.height as i32
                    });
                    if on_thumb {
                        self.dragging_scrollbar = true;
                    } else {
                        self.scroll_to_track(pos.y);
                    }
                    return;
                }
                let row = (pos.y - rect.y - TEXT_TOP) / TEXT_ROW_HEIGHT;
                if row < 0 {
                    return;
                }
                let line = self.visible_start() + row as usize;
                if line >= self.edit_line_count() {
                    return;
                }
                let font = effective_font(self.base.style());
                let x = pos.x - rect.x - 12;
                // The line is borrowed, not copied: a drag over the document copied one line per
                // pointer move. `text`, `char_widths` and the style are disjoint fields, so the
                // immutable line borrow and the mutable width cache coexist.
                let (column, line_chars) = {
                    let line_text = self.text.split('\n').nth(line).unwrap_or("");
                    let mut offset = 0i32;
                    let mut column = 0usize;
                    for ch in line_text.chars() {
                        let width = cached_char_width(&mut self.char_widths, ch, font, 1.0);
                        if x < offset + width / 2 {
                            break;
                        }
                        offset += width;
                        column += 1;
                    }
                    (column, line_text.chars().count())
                };
                let shift = crate::shortcut::Modifiers::from_event_bits(*modifiers)
                    .contains(crate::shortcut::Modifiers::SHIFT);
                self.cursor_line = line;
                self.cursor_col = column.min(line_chars);
                if !shift {
                    self.selection_anchor = Some((self.cursor_line, self.cursor_col));
                    self.apply_selection(None);
                }
                self.selecting = true;
                if self.preview_mode {
                    // A heading row in preview is a navigation target, so a click on it announces
                    // which outline entry it is.
                    let heading =
                        self.outline().iter().position(|entry| entry.line == self.cursor_line);
                    if let Some(index) = heading {
                        self.heading_activated.emit(index);
                        let _ = self.goto_outline(index);
                    }
                }
                self.base.request_redraw();
            }
            Event::MouseMove { pos } => {
                if self.dragging_scrollbar {
                    self.scroll_to_track(pos.y);
                    return;
                }
                if self.selecting {
                    let rect = self.geometry();
                    let row = (pos.y - rect.y - TEXT_TOP) / TEXT_ROW_HEIGHT;
                    if row >= 0 {
                        let line = (self.visible_start() + row as usize)
                            .min(self.edit_line_count().saturating_sub(1));
                        let font = effective_font(self.base.style());
                        let x = pos.x - rect.x - 12;
                        let (column, line_chars) = {
                            let line_text = self.text.split('\n').nth(line).unwrap_or("");
                            let mut offset = 0i32;
                            let mut column = 0usize;
                            for ch in line_text.chars() {
                                let width = cached_char_width(&mut self.char_widths, ch, font, 1.0);
                                if x < offset + width / 2 {
                                    break;
                                }
                                offset += width;
                                column += 1;
                            }
                            (column, line_text.chars().count())
                        };
                        self.cursor_line = line;
                        self.cursor_col = column.min(line_chars);
                        self.sync_selection_from_anchor();
                        self.base.request_redraw();
                    }
                }
            }
            Event::MouseRelease { .. } => {
                self.selecting = false;
                self.dragging_scrollbar = false;
            }
            Event::MouseLeave { .. } => {
                // A release outside the control is not delivered to it, so leaving is the last
                // signal that the drag ended.
                self.selecting = false;
                self.dragging_scrollbar = false;
            }
            Event::Resize { .. } => {
                self.cursor_line = self.cursor_line.min(self.edit_line_count().saturating_sub(1));
                self.clamp_cursor_col();
                self.base.request_layout();
                self.base.request_redraw();
            }
            Event::Wheel { delta, modifiers, .. } => {
                let scroll = WheelScroll::from_event(*delta, *modifiers, 3);
                if self.scroll_lines(-scroll.lines_y) {
                    self.base.request_redraw();
                }
            }
            _ => { /* Other events are not relevant */ }
        }
    }
}

impl MarkdownEditor {
    /// Routes one key press to the editor or to the open find bar.
    fn handle_key(&mut self, key: u32, modifiers: u32) {
        let shift = crate::shortcut::Modifiers::from_event_bits(modifiers)
            .contains(crate::shortcut::Modifiers::SHIFT);
        let primary = crate::shortcut::Modifiers::from_event_bits(modifiers)
            .contains(crate::shortcut::Modifiers::PRIMARY);

        if self.find.visible {
            match key {
                8 => {
                    self.find.query.pop();
                    self.find.current = None;
                    self.find_bar.set_query(self.find.query.clone());
                }
                13 => {
                    let query = self.find.query.clone();
                    if !self.find_next(&query) {
                        self.find.current = None;
                    }
                    let _ = self.find_bar.find_next();
                }
                27 => self.close_find(),
                _ if primary_shortcut_pressed(key, modifiers, Key::F) => self.close_find(),
                _ => { /* The find bar ignores editing chords it does not own. */ }
            }
            self.base.request_redraw();
            return;
        }

        if primary {
            if primary_shortcut_pressed(key, modifiers, Key::A) {
                self.select_all();
                return;
            }
            if primary_shortcut_pressed(key, modifiers, Key::C) {
                self.copy();
                return;
            }
            if primary_shortcut_pressed(key, modifiers, Key::X) {
                self.cut();
                return;
            }
            if primary_shortcut_pressed(key, modifiers, Key::V) {
                self.paste();
                return;
            }
            if primary_shortcut_pressed(key, modifiers, Key::F) {
                self.open_find();
                return;
            }
            // Inline formatting: the same actions the mounted toolbar calls, so a chord and its
            // button cannot produce different markdown.
            if primary_shortcut_pressed(key, modifiers, Key::B) {
                self.wrap_selection("**", "**");
                return;
            }
            if primary_shortcut_pressed(key, modifiers, Key::I) {
                self.wrap_selection("*", "*");
                return;
            }
            if primary_shortcut_pressed(key, modifiers, Key::K) {
                self.wrap_selection("[", "](url)");
                return;
            }
        }
        match key {
            _ if primary_shortcut_pressed(key, modifiers, Key::Z) => {
                let _ = self.undo();
            }
            _ if primary_shortcut_pressed(key, modifiers, Key::Y) => {
                let _ = self.redo();
            }
            // The second redo chord every desktop editor accepts; the handler used to answer
            // only primary+Y, so the habitual primary+Shift+Z did nothing.
            _ if primary_shift_shortcut(key, modifiers, Key::Z) => {
                let _ = self.redo();
            }
            9 => {
                // Tab indents; Shift+Tab outdents. With no selection the caret gains one indent
                // level, which is what markdown's nested lists and code blocks need.
                if shift {
                    self.shift_line_indent(false);
                } else if self.selection.is_some() {
                    self.shift_line_indent(true);
                } else {
                    self.replace_selection_or_caret("  ");
                }
            }
            8 => self.backspace_at_cursor(),
            13 => self.insert_newline_at_cursor(),
            46 | 127 => self.delete_forward(),
            36 => {
                if shift {
                    self.begin_shift_selection();
                    self.cursor_col = 0;
                    self.sync_selection_from_anchor();
                    self.base.request_redraw();
                } else {
                    self.move_to_line_edge(true);
                }
            }
            35 => {
                if shift {
                    self.begin_shift_selection();
                    self.cursor_col = self.current_line_char_len();
                    self.sync_selection_from_anchor();
                    self.base.request_redraw();
                } else {
                    self.move_to_line_edge(false);
                }
            }
            33 => {
                self.scroll_page(-1);
            }
            34 => {
                self.scroll_page(1);
            }
            37 => {
                if shift {
                    self.begin_shift_selection();
                } else {
                    self.selection_anchor = None;
                    self.apply_selection(None);
                }
                self.move_cursor_left();
                if shift {
                    self.sync_selection_from_anchor();
                }
            }
            39 => {
                if shift {
                    self.begin_shift_selection();
                } else {
                    self.selection_anchor = None;
                    self.apply_selection(None);
                }
                self.move_cursor_right();
                if shift {
                    self.sync_selection_from_anchor();
                }
            }
            38 => self.move_vertical(-1, shift),
            40 => self.move_vertical(1, shift),
            80 | 112 if primary_shortcut_pressed(key, modifiers, Key::P) => {
                self.toggle_preview_mode()
            }
            _ => { /* Other keys are not relevant */ }
        }
    }

    /// Deletes the character after the caret, or merges the next line at a line end.
    fn delete_forward(&mut self) {
        if self.delete_selection() {
            return;
        }
        self.clamp_cursor();
        let at = self.cursor_byte_offset();
        let Some(ch) = self.text[at..].chars().next() else {
            return;
        };
        let mut next = String::with_capacity(self.text.len());
        next.push_str(&self.text[..at]);
        next.push_str(&self.text[at + ch.len_utf8()..]);
        self.commit_text(next);
    }

    /// Moves the caret by whole lines, extending the selection when `shift` is held.
    fn move_vertical(&mut self, delta: isize, shift: bool) {
        if shift {
            self.begin_shift_selection();
        } else {
            self.selection_anchor = None;
            self.apply_selection(None);
        }
        self.move_cursor(delta);
        if shift {
            self.sync_selection_from_anchor();
        }
        self.reveal_caret();
    }

    /// Remembers the caret as the selection anchor if no selection is being extended yet.
    fn begin_shift_selection(&mut self) {
        if self.selection_anchor.is_none() {
            self.selection_anchor = Some((self.cursor_line, self.cursor_col));
        }
    }

    /// Recomputes the selection from the anchor and the current caret.
    fn sync_selection_from_anchor(&mut self) {
        let Some((anchor_line, anchor_col)) = self.selection_anchor else {
            self.apply_selection(None);
            return;
        };
        let anchor = self.doc_char_offset(anchor_line, anchor_col);
        let caret = self.doc_char_offset_of_caret();
        let (start, end) = if anchor <= caret { (anchor, caret) } else { (caret, anchor) };
        self.apply_selection((start != end).then_some((start, end)));
    }

    /// The document character offset of `(line, column)`.
    fn doc_char_offset(&self, line: usize, column: usize) -> usize {
        self.with_line_index(|index| {
            let start = index.char_starts.get(line).copied().unwrap_or_else(|| index.char_len());
            let length = index.char_lens.get(line).copied().unwrap_or(0);
            start + column.min(length)
        })
    }

    /// A glyph advance, measured at most once per size.
    /// The scrollbar track, or `None` when hidden or the document fits.
    fn scrollbar_track(&self) -> Option<Rect> {
        if !self.show_scrollbar {
            return None;
        }
        let total = self.line_count();
        if total <= self.visible_line_count().max(1) {
            return None;
        }
        let rect = self.geometry();
        let top = rect.y + TEXT_TOP;
        let bottom = rect.y + rect.height as i32 - TEXT_BOTTOM_INSET;
        if bottom <= top {
            return None;
        }
        // The track stops where the outline panel begins, so the two pieces of chrome never
        // paint over one another.
        let outline_width = self.outline_rect().map_or(0, |panel| panel.width as i32);
        Some(Rect::new(
            rect.x + rect.width as i32 - 10 - outline_width,
            top,
            6,
            (bottom - top) as u32,
        ))
    }

    /// The rectangle the mounted outline panel occupies, when it is shown and fits.
    fn outline_rect(&self) -> Option<Rect> {
        if !self.show_outline {
            return None;
        }
        let rect = self.geometry();
        // Half the editor is the most a side panel may take; below this the text column would be
        // too narrow to read, so the panel is simply not mounted.
        if rect.width < 240 || rect.height < TEXT_TOP as u32 + 48 {
            return None;
        }
        let width = self.outline_panel.width().min(rect.width / 2).max(1);
        Some(Rect::new(
            rect.x + rect.width as i32 - width as i32,
            rect.y + TEXT_TOP,
            width,
            rect.height - TEXT_TOP as u32 - TEXT_BOTTOM_INSET as u32,
        ))
    }

    /// The rectangle the mounted find bar occupies.
    fn find_bar_rect(&self) -> Rect {
        let rect = self.geometry();
        let height = self.find_bar.size_hint().height;
        Rect::new(rect.x + 8, rect.y + 4, rect.width.saturating_sub(16), height)
    }

    /// Places the mounted companions inside the editor's own rectangle.
    fn layout_companions(&mut self) {
        let find_bar = self.find_bar_rect();
        if self.find_bar.base().geometry() != find_bar {
            self.find_bar.base_mut().set_geometry(find_bar);
        }
        if let Some(outline) = self.outline_rect() {
            if self.outline_panel.base().geometry() != outline {
                self.outline_panel.base_mut().set_geometry(outline);
            }
        }
    }

    /// The scrollbar thumb, sized and placed from the scroll state.
    fn scrollbar_thumb(&self) -> Option<Rect> {
        let track = self.scrollbar_track()?;
        let total = self.line_count().max(1);
        let visible = self.visible_line_count().max(1).min(total);
        let height = ((track.height as i64 * visible as i64) / total as i64).max(12) as u32;
        let height = height.min(track.height);
        let range = total.saturating_sub(visible);
        let travel = track.height.saturating_sub(height) as i64;
        let top = self.visible_start().min(range);
        let y = track.y + if range > 0 { (travel * top as i64 / range as i64) as i32 } else { 0 };
        Some(Rect::new(track.x, y, track.width, height))
    }

    /// Whether `pos` falls on the painted scrollbar track.
    fn scrollbar_track_contains(&self, pos: Point) -> bool {
        self.scrollbar_track().is_some_and(|track| {
            pos.x >= track.x
                && pos.x < track.x + track.width as i32
                && pos.y >= track.y
                && pos.y < track.y + track.height as i32
        })
    }

    /// Scrolls so the first visible line is where `y` points on the track.
    fn scroll_to_track(&mut self, y: i32) {
        let Some(track) = self.scrollbar_track() else {
            return;
        };
        let ratio = ((y - track.y) as f32 / track.height.max(1) as f32).clamp(0.0, 1.0);
        let max_first = self.line_count().saturating_sub(self.visible_line_count());
        let next = (ratio * max_first as f32).round() as usize;
        if next != self.first_visible_line {
            self.first_visible_line = next.min(max_first);
            self.base.request_redraw();
        }
    }
}

impl Draw for MarkdownEditor {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        // The fence table depends on every line above it, so it is rebuilt once per revision —
        // before the style borrow is taken, because building it needs `&mut self`.
        self.ensure_blocks();
        if self.show_outline {
            // Reads the same heading list `outline()` reports, and marks the row the caret is in.
            self.sync_outline();
        }

        // The mounted companions follow the editor's own rectangle; doing it here (rather than in
        // `new`) means a resized editor moves its side panel and find bar with it.
        self.layout_companions();

        // Chrome colours resolve explicit style first, then the theme's resolved style
        // for this control, and only then fall back to a literal. Without the theme step
        // a light/dark switch changed nothing on screen, because the surface, the border
        // and every piece of text were hardcoded.
        //
        // 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.
        let style = self.base.style();
        let theme = crate::style::resolved_theme_style_shared("markdown_editor");
        let surface = style
            .background_color
            .or_else(|| theme.as_ref().and_then(|t| t.background_color))
            .unwrap_or(Color::rgb(252, 252, 253));
        let border = style
            .border_color
            .or_else(|| theme.as_ref().and_then(|t| t.border_color))
            .unwrap_or(Color::rgb(194, 201, 213));
        let ink = style
            .text_color
            .or_else(|| theme.as_ref().and_then(|t| t.text_color))
            .unwrap_or(Color::rgb(59, 72, 92));
        // The header is the dimmest ink that still reads, and the caret line is the
        // accent: both derived from the resolved ink and surface, so they move with
        // the appearance instead of being fixed literals.
        //
        // The caret line is the accent colour because it *means* "the line being edited",
        // so the hue is part of the information and must survive. It cannot be pushed
        // straight onto the surface unchecked, though: on the light appearance the editor
        // surface resolves to a mid grey and `colors.primary` is the saturated Material
        // blue, which measured 1.51:1 — the very line the caret is on became the least
        // readable line in the editor. `legible_on` keeps the hue and guarantees the ratio.
        let header_ink = ink.blend(&surface, 0.35);
        let cursor_ink = crate::style::theme_manager()
            .current_theme()
            .map(|active| active.colors.primary.legible_on(surface, 4.5))
            .unwrap_or_else(|| ink.contrast_color());

        context.face_with_gradient(
            rect,
            surface,
            style.background_gradient.as_ref(),
            style.surface.unwrap_or_default(),
            style.border_radius.unwrap_or(0),
            Color::BLACK,
        );
        crate::widget::draw::stroke_surface_border(
            context,
            rect,
            self.style().border_radius.unwrap_or(0),
            border,
            1,
        );

        // The counts come from the revision-keyed cache, so a still frame walks no document.
        let metrics = self.metrics_snapshot();
        let header = if self.find.visible {
            format!("Find: {}   (Enter next, Esc close)", self.find.query)
        } else if self.preview_mode {
            format!(
                "Markdown Preview  lines:{} words:{} headings:{}",
                metrics.lines, metrics.words, metrics.headings
            )
        } else {
            format!(
                "Markdown Edit  lines:{} words:{} headings:{}",
                metrics.lines, metrics.words, metrics.headings
            )
        };
        // The mounted find bar replaces the plain header text while it is open: it paints the
        // query, the match counter and its policy toggles, and a second header underneath would
        // be an unreadable overprint.
        if self.find.visible {
            self.find_bar.draw(context);
        } else {
            // Fitted to the control's width: the header is a long line of counts, and a narrow
            // editor previously drew it straight past its own right edge.
            let header_band = Rect::new(rect.x + 8, rect.y + 4, rect.width.saturating_sub(16), 14);
            context.draw_text_fitted_cow(
                header_band,
                &header,
                effective_font(style),
                header_ink,
                HorizontalAlignment::Left,
            );
        }

        let start = self.visible_start();
        let count = self.visible_line_count();
        // The line total comes from the cached offset tables, not from re-counting the document's
        // newlines on every frame.
        let line_total = self.edit_line_count();
        let font = effective_font(style);
        // Selection endpoints, resolved once. `line_col_of` is two document walks; doing it per
        // row would be quadratic, and the whole point of this path is that a frame is cheap.
        let selection =
            self.selection.map(|(first, last)| (self.line_col_of(first), self.line_col_of(last)));
        let scrollbar_width = if self.scrollbar_track().is_some() { 12 } else { 0 };
        let outline_width = self.outline_rect().map_or(0, |panel| panel.width as i32);
        let row_width = rect.width.saturating_sub(24 + scrollbar_width + outline_width as u32);

        {
            let text = &self.text;
            let cache = &mut self.preview_cache;
            let blocks = &self.blocks;
            let table_headers = &self.table_header;
            let gfm = self.gfm_extensions;
            let highlight = self.code_highlight;
            for (row, line) in text.split('\n').skip(start).take(count).enumerate() {
                let idx = start + row;
                let y = rect.y + TEXT_TOP + (row as i32) * TEXT_ROW_HEIGHT;
                // Rows below the control are not painted, which keeps a short editor from drawing
                // past its own bottom edge.
                if y > rect.y + rect.height as i32 - TEXT_BOTTOM_INSET {
                    break;
                }
                let color = if idx == self.cursor_line { cursor_ink } else { ink };
                let band = Rect::new(rect.x + 12, y, row_width, TEXT_ROW_HEIGHT as u32);

                // The selection band is drawn behind the text, trimmed to the exact characters
                // on the first and last rows.
                if let Some(((first_line, first_col), (last_line, last_col))) = selection {
                    if idx >= first_line && idx <= last_line {
                        let line_chars = line.chars().count();
                        let from = if idx == first_line { first_col } else { 0 };
                        let to = if idx == last_line { last_col } else { line_chars };
                        // Both edges are measured straight off the borrowed line: a prefix
                        // `String` per visible row was an allocation per row per frame.
                        let from_byte = byte_offset_of_char(line, from);
                        let to_byte = byte_offset_of_char(line, to).max(from_byte);
                        let x = band.x + estimate_text_width(&line[..from_byte], font, 1.0) as i32;
                        let width = estimate_text_width(&line[from_byte..to_byte], font, 1.0);
                        context.fill_rect(
                            Rect::new(x, y, width.max(1), TEXT_ROW_HEIGHT as u32),
                            cursor_ink.with_alpha(60),
                        );
                    }
                }

                if self.preview_mode {
                    let block = blocks.get(idx).copied().unwrap_or(BlockStart::Normal);
                    let key = (idx, text_hash(line));
                    let parsed = match cache.get(&key).cloned() {
                        Some(parsed) => parsed,
                        None => {
                            let parsed = Rc::new(parse_preview_line(
                                line,
                                block,
                                gfm,
                                highlight,
                                table_headers.get(idx).copied().unwrap_or(false),
                                font,
                                context,
                            ));
                            cache.insert(key, parsed.clone());
                            parsed
                        }
                    };
                    draw_parsed_line(context, band, &parsed, font, color, cursor_ink, surface);
                } else {
                    // Fitted to the pane, so a long line ends in an ellipsis inside the control
                    // instead of painting over the widget beside it.
                    context.draw_text_fitted_cow(
                        band,
                        line,
                        font,
                        color,
                        HorizontalAlignment::Left,
                    );
                }
            }
            // The cache is keyed by line index and text hash, so entries for lines that moved or
            // changed are simply unused. When the table outgrows its bound only the entries that
            // no longer describe a live line are dropped: clearing it rebuilt every visible line
            // on the next frame.
            let bound = line_total.saturating_mul(4).saturating_add(64);
            if cache.len() > bound {
                let live: std::collections::HashSet<(usize, u64)> = text
                    .split('\n')
                    .enumerate()
                    .map(|(index, line)| (index, text_hash(line)))
                    .collect();
                cache.retain(|key, _| live.contains(key));
            }
        }

        if let Some(track) = self.scrollbar_track() {
            context.fill_rect(track, ink.blend(&surface, 0.10));
            if let Some(thumb) = self.scrollbar_thumb() {
                context.fill_rect(thumb, ink.with_alpha(110));
            }
        }

        // The caret, the composition and the selection are edit-mode furniture: preview has no
        // caret because there is nothing to edit there.
        if !self.preview_mode {
            let start = self.visible_start();
            if let Some(row) =
                self.cursor_line.checked_sub(start).filter(|row| *row < self.visible_line_count())
            {
                let (line_start, line_end) = self.cursor_line_span();
                let caret_line = &self.text[line_start..line_end];
                let caret_byte = byte_offset_of_char(caret_line, self.cursor_col);
                let x =
                    rect.x + 12 + estimate_text_width(&caret_line[..caret_byte], font, 1.0) as i32;
                let y = rect.y + TEXT_TOP + (row as i32) * TEXT_ROW_HEIGHT;
                if y + 14 <= rect.y + rect.height as i32 && x <= rect.x + rect.width as i32 - 12 {
                    if let Some(preedit) = &self.preedit {
                        // The composition is drawn where the caret is and semi-transparent, so a
                        // user can see what has not been committed yet — the previous build drew
                        // nothing at all and the text appeared only after a candidate landed.
                        context.draw_text(
                            Point::new(x, y),
                            &preedit.text,
                            font,
                            cursor_ink.with_alpha(160),
                            HorizontalAlignment::Left,
                        );
                    }
                    if self.is_caret_visible() || self.preedit.is_some() {
                        context.fill_rect(Rect::new(x, y - 2, 1, 16), cursor_ink);
                    }
                }
            }
        }

        // The mounted outline is painted last, over the text column it reserved space for, so a
        // selected row is fully visible even when the text underneath is long.
        if self.outline_rect().is_some() {
            self.outline_panel.draw(context);
        }
    }
}

/// Whether the host's primary accelerator **plus Shift** is held for `expected`.
///
/// `primary_shortcut_pressed` compares the modifier set exactly, so the Shift bit is removed
/// before asking it: the remaining modifiers are what a plain primary chord carries.
fn primary_shift_shortcut(key: u32, modifiers: u32, expected: Key) -> bool {
    let shift = crate::shortcut::Modifiers::SHIFT;
    let pressed = crate::shortcut::Modifiers::from_event_bits(modifiers);
    pressed.contains(shift)
        && primary_shortcut_pressed(key, modifiers & !(shift.bits() as u32), expected)
}

/// A glyph advance, measured at most once per font size.
///
/// Free rather than a method because the callers hold a borrow of the widget's style (and thus of
/// `self.base`) while they need the cache mutably; taking the cache as its own parameter keeps
/// those two borrows disjoint.
fn cached_char_width(cache: &mut Vec<(char, f32, i32)>, ch: char, font: &Font, scale: f32) -> i32 {
    let size = font.size();
    if let Some((_, _, width)) = cache
        .iter()
        .find(|(candidate, cached, _)| *candidate == ch && (*cached - size).abs() < f32::EPSILON)
    {
        return *width;
    }
    let mut buffer = [0u8; 4];
    let glyph = ch.encode_utf8(&mut buffer);
    let width = estimate_text_width(glyph, font, scale) as i32;
    cache.push((ch, size, width));
    width
}

/// A stable hash of one preview line, used as the cache key beside its index.
fn text_hash(text: &str) -> u64 {
    let mut hash = 0xcbf2_9ce4_8422_2325u64;
    for byte in text.as_bytes() {
        hash ^= *byte as u64;
        hash = hash.wrapping_mul(0x0000_0100_0000_01B3);
    }
    hash
}

/// The largest character boundary at or below `offset`.
///
/// A platform-supplied byte offset is usually a boundary, but slicing on one that is not would
/// panic inside a draw/hit-test path; clamping is the only behaviour that cannot crash.
fn char_boundary_at(text: &str, offset: usize) -> usize {
    let offset = offset.min(text.len());
    (0..=offset).rev().find(|index| text.is_char_boundary(*index)).unwrap_or(0)
}

/// The byte offset of character `index` in `text`, clamped to its end.
///
/// Used by the paint path to measure a prefix without materialising it as a `String`.
fn byte_offset_of_char(text: &str, index: usize) -> usize {
    text.char_indices().nth(index).map(|(byte, _)| byte).unwrap_or(text.len())
}

/// The info string of a fenced code block opener, if `trimmed` opens one.
fn fence_marker(trimmed: &str) -> Option<&str> {
    let rest = trimmed.strip_prefix("```").or_else(|| trimmed.strip_prefix("~~~"))?;
    Some(rest.trim())
}

/// Maps a fence's info string onto one of the shared lexer's languages.
fn language_from_info(info: &str) -> LanguageId {
    let token = info.split_whitespace().next().unwrap_or("").to_ascii_lowercase();
    match token.as_str() {
        "rust" | "rs" => LanguageId::Rust,
        "python" | "py" => LanguageId::Python,
        "javascript" | "js" | "typescript" | "ts" | "tsx" | "jsx" => LanguageId::JavaScript,
        "go" | "golang" => LanguageId::Go,
        "c" | "h" | "cpp" | "c++" | "cc" | "hpp" | "java" | "cs" | "csharp" | "kt" => {
            LanguageId::CLike
        }
        "markdown" | "md" => LanguageId::Markdown,
        _ => LanguageId::PlainText,
    }
}

/// Builds one measured run.
fn push_run(
    runs: &mut Vec<PreviewRun>,
    text: &str,
    style: PreviewStyle,
    font: &Font,
    context: &RenderContext,
) {
    if text.is_empty() {
        return;
    }
    runs.push(PreviewRun {
        text: text.to_string(),
        style,
        width: context.measure_text(text, font).width,
    });
}

/// Parses one preview row.
///
/// This is where the GFM block constructs are recognised: fenced code (through the per-revision
/// fence table), headings, bullets and ordered items, task boxes, quotes, tables, rules, images
/// and the inline emphasis/code/link/delete run styles. Everything the previous preview did not
/// know about arrived as literal source text.
fn parse_preview_line(
    line: &str,
    block: BlockStart,
    gfm: bool,
    highlight: bool,
    table_follows: bool,
    font: &Font,
    context: &RenderContext,
) -> ParsedLine {
    let mut runs = Vec::new();
    match block {
        BlockStart::Fence => {
            push_run(&mut runs, line.trim(), PreviewStyle::Dim, font, context);
            return ParsedLine { runs, row: PreviewRow::Fence };
        }
        BlockStart::Code(language, state) => {
            if highlight {
                let highlighter = BuiltinHighlighter::new(language);
                let (spans, _) = highlighter.highlight_line(line, state);
                if spans.is_empty() {
                    push_run(&mut runs, line, PreviewStyle::Token(TokenKind::Plain), font, context);
                } else {
                    let mut cursor = 0usize;
                    for span in spans {
                        let start = span.start.min(line.len()).max(cursor);
                        let end = span.end.min(line.len()).max(start);
                        if start > cursor {
                            push_run(
                                &mut runs,
                                &line[cursor..start],
                                PreviewStyle::Token(TokenKind::Plain),
                                font,
                                context,
                            );
                        }
                        if end > start {
                            push_run(
                                &mut runs,
                                &line[start..end],
                                PreviewStyle::Token(span.kind),
                                font,
                                context,
                            );
                        }
                        cursor = end;
                    }
                    if cursor < line.len() {
                        push_run(
                            &mut runs,
                            &line[cursor..],
                            PreviewStyle::Token(TokenKind::Plain),
                            font,
                            context,
                        );
                    }
                }
            } else {
                push_run(&mut runs, line, PreviewStyle::Text, font, context);
            }
            return ParsedLine { runs, row: PreviewRow::Code };
        }
        BlockStart::Normal => {}
    }

    let trimmed = line.trim_start();
    let indent = line.len() - trimmed.len();

    // Heading: the leading `#` markers are syntax, so they are consumed rather than rendered.
    let hashes = trimmed.chars().take_while(|ch| *ch == '#').count();
    if hashes > 0 && hashes <= 6 && trimmed[hashes..].starts_with(' ') {
        let level = hashes as u8;
        let heading_font = font.with_size(font.size() + (7 - hashes) as f32);
        let body = trimmed[hashes..].trim_start();
        parse_inline(body, gfm, &heading_font, context, &mut runs);
        return ParsedLine { runs, row: PreviewRow::Heading(level) };
    }

    if gfm && is_rule(trimmed) {
        return ParsedLine { runs, row: PreviewRow::Rule };
    }
    if gfm && is_table_divider(trimmed) {
        return ParsedLine { runs, row: PreviewRow::Rule };
    }

    let mut body = trimmed;
    let mut prefix_width = 0u32;
    if gfm && body.starts_with('>') {
        push_run(&mut runs, "▏", PreviewStyle::Accent, font, context);
        prefix_width = context.measure_text("▏ ", font).width;
        body = body.trim_start_matches('>').trim_start();
    } else if body.starts_with("- [ ] ") || body.starts_with("- [x] ") || body.starts_with("- [X] ")
    {
        let checked = !body.starts_with("- [ ]");
        let marker = if checked { "☑ " } else { "☐ " };
        push_run(&mut runs, marker, PreviewStyle::Accent, font, context);
        prefix_width = context.measure_text(marker, font).width;
        body = &body[6..];
    } else if let Some(rest) = strip_bullet(body) {
        push_run(&mut runs, "• ", PreviewStyle::Accent, font, context);
        prefix_width = context.measure_text("• ", font).width;
        body = rest;
    } else if let Some((marker, rest)) = strip_ordered(body) {
        let marker = format!("{marker} ");
        push_run(&mut runs, &marker, PreviewStyle::Accent, font, context);
        prefix_width = context.measure_text(&marker, font).width;
        body = rest;
    }

    // A table needs its delimiter row (or pipes on both outer edges), exactly as GFM does; one
    // `|` in prose is not a table.
    if gfm && body.contains('|') && (table_follows || body.trim().starts_with('|')) {
        let row = render_table_row(body, font, context, &mut runs);
        return ParsedLine { runs, row };
    }

    // The indent is kept so a nested list item still lines up under its parent.
    if indent > 0 && prefix_width == 0 {
        let pad = " ".repeat(indent);
        push_run(&mut runs, &pad, PreviewStyle::Text, font, context);
    }
    parse_inline(body, gfm, font, context, &mut runs);
    ParsedLine { runs, row: PreviewRow::Inline }
}

/// Strips a bullet marker, returning the item body.
fn strip_bullet(text: &str) -> Option<&str> {
    for marker in ["- ", "* ", "+ "] {
        if let Some(rest) = text.strip_prefix(marker) {
            return Some(rest);
        }
    }
    None
}

/// Strips an ordered-list marker, returning the number and the item body.
fn strip_ordered(text: &str) -> Option<(&str, &str)> {
    let digits = text.chars().take_while(|ch| ch.is_ascii_digit()).count();
    if digits == 0 {
        return None;
    }
    let rest = &text[digits..];
    let rest = rest.strip_prefix(". ").or_else(|| rest.strip_prefix(") "))?;
    Some((&text[..digits], rest))
}

/// Whether a line is a horizontal rule (`---`, `***`, `___`).
fn is_rule(trimmed: &str) -> bool {
    let mut marker = None;
    let mut count = 0usize;
    for ch in trimmed.chars() {
        if ch == ' ' {
            continue;
        }
        match marker {
            None if ch == '-' || ch == '*' || ch == '_' => marker = Some(ch),
            Some(existing) if existing == ch => {}
            _ => return false,
        }
        count += 1;
    }
    count >= 3
}

/// Whether a table row is the `|---|:--:|` divider.
fn is_table_divider(trimmed: &str) -> bool {
    if !trimmed.contains('-') || !trimmed.contains('|') {
        return false;
    }
    trimmed.chars().all(|ch| matches!(ch, '|' | '-' | ':' | ' ' | '\t'))
}

/// Splits a table row into measured cells with separators between them.
///
/// Columns are separated but not padded to a shared width: padding needs the widest cell in the
/// whole table, which is a table-wide layout pass rather than a per-row one, and this preview
/// renders rows independently. The cells themselves keep their inline markup.
fn render_table_row(
    body: &str,
    font: &Font,
    context: &RenderContext,
    runs: &mut Vec<PreviewRun>,
) -> PreviewRow {
    let trimmed = body.trim().trim_start_matches('|').trim_end_matches('|');
    for (index, cell) in trimmed.split('|').enumerate() {
        if index > 0 {
            push_run(runs, " │ ", PreviewStyle::Dim, font, context);
        }
        let cell = cell.trim();
        // The cell's own inline markup still applies, so a bold cell is bold.
        parse_inline(cell, true, font, context, runs);
    }
    PreviewRow::Table
}

/// Splits inline markdown into styled runs.
// The parser peeks ahead while consuming (`**`/`__` need a second character), so a plain
// `while let Some(ch) = chars.next()` is the honest shape; clippy's iterator-lint suggestion
// would borrow the iterator twice.
#[allow(clippy::while_let_on_iterator)]
#[allow(clippy::while_let_loop)]
fn parse_inline(
    text: &str,
    gfm: bool,
    font: &Font,
    context: &RenderContext,
    runs: &mut Vec<PreviewRun>,
) {
    let mut plain = String::new();
    let mut chars = text.chars().peekable();
    while let Some(ch) = chars.next() {
        match ch {
            '*' if chars.peek() == Some(&'*') => {
                chars.next();
                push_run(runs, &core::mem::take(&mut plain), PreviewStyle::Text, font, context);
                let mut inner = String::new();
                let mut closed = false;
                loop {
                    let Some(next) = chars.next() else {
                        break;
                    };
                    if next == '*' && chars.peek() == Some(&'*') {
                        chars.next();
                        closed = true;
                        break;
                    }
                    inner.push(next);
                }
                push_run(
                    runs,
                    &inner,
                    if closed { PreviewStyle::Bold } else { PreviewStyle::Text },
                    font,
                    context,
                );
            }
            '_' if chars.peek() == Some(&'_') => {
                chars.next();
                push_run(runs, &core::mem::take(&mut plain), PreviewStyle::Text, font, context);
                let mut inner = String::new();
                let mut closed = false;
                while let Some(next) = chars.next() {
                    if next == '_' && chars.peek() == Some(&'_') {
                        chars.next();
                        closed = true;
                        break;
                    }
                    inner.push(next);
                }
                push_run(
                    runs,
                    &inner,
                    if closed { PreviewStyle::Bold } else { PreviewStyle::Text },
                    font,
                    context,
                );
            }
            '*' | '_' => {
                push_run(runs, &core::mem::take(&mut plain), PreviewStyle::Text, font, context);
                let marker = ch;
                let mut inner = String::new();
                let mut closed = false;
                while let Some(next) = chars.next() {
                    if next == marker {
                        closed = true;
                        break;
                    }
                    inner.push(next);
                }
                push_run(
                    runs,
                    &inner,
                    if closed { PreviewStyle::Italic } else { PreviewStyle::Text },
                    font,
                    context,
                );
            }
            '~' if gfm && chars.peek() == Some(&'~') => {
                chars.next();
                push_run(runs, &core::mem::take(&mut plain), PreviewStyle::Text, font, context);
                let mut inner = String::new();
                let mut closed = false;
                while let Some(next) = chars.next() {
                    if next == '~' && chars.peek() == Some(&'~') {
                        chars.next();
                        closed = true;
                        break;
                    }
                    inner.push(next);
                }
                push_run(
                    runs,
                    &inner,
                    if closed { PreviewStyle::Strike } else { PreviewStyle::Text },
                    font,
                    context,
                );
            }
            '`' => {
                push_run(runs, &core::mem::take(&mut plain), PreviewStyle::Text, font, context);
                let mut inner = String::new();
                for next in chars.by_ref() {
                    if next == '`' {
                        break;
                    }
                    inner.push(next);
                }
                push_run(runs, &inner, PreviewStyle::Code, font, context);
            }
            '!' if chars.peek() == Some(&'[') => {
                chars.next();
                push_run(runs, &core::mem::take(&mut plain), PreviewStyle::Text, font, context);
                let mut alt = String::new();
                let mut target = String::new();
                while let Some(next) = chars.next() {
                    if next == ']' {
                        if chars.peek() == Some(&'(') {
                            chars.next();
                            for part in chars.by_ref() {
                                if part == ')' {
                                    break;
                                }
                                target.push(part);
                            }
                        }
                        break;
                    }
                    alt.push(next);
                }
                let label = if alt.is_empty() { target } else { alt };
                push_run(runs, "▣ ", PreviewStyle::Accent, font, context);
                push_run(runs, &label, PreviewStyle::Link, font, context);
            }
            '[' => {
                push_run(runs, &core::mem::take(&mut plain), PreviewStyle::Text, font, context);
                let mut label = String::new();
                let mut is_link = false;
                while let Some(next) = chars.next() {
                    if next == ']' {
                        if chars.peek() == Some(&'(') {
                            chars.next();
                            for target in chars.by_ref() {
                                if target == ')' {
                                    break;
                                }
                            }
                            is_link = true;
                        }
                        break;
                    }
                    label.push(next);
                }
                push_run(
                    runs,
                    &label,
                    if is_link { PreviewStyle::Link } else { PreviewStyle::Text },
                    font,
                    context,
                );
            }
            _ => plain.push(ch),
        }
    }
    push_run(runs, &plain, PreviewStyle::Text, font, context);
}

/// The ink a fenced-code token is drawn in.
fn token_color(kind: TokenKind, ink: Color, accent: Color, surface: Color) -> Color {
    match kind {
        TokenKind::Keyword | TokenKind::Preprocessor => accent,
        TokenKind::String | TokenKind::Character => {
            crate::style::semantic_color(crate::style::SemanticColor::Success)
                .map(|token| token.legible_on(surface, 3.0))
                .unwrap_or(ink)
        }
        TokenKind::Number => crate::style::semantic_color(crate::style::SemanticColor::Warning)
            .map(|token| token.legible_on(surface, 3.0))
            .unwrap_or(ink),
        TokenKind::Comment | TokenKind::BlockComment => ink.blend(&surface, 0.45),
        TokenKind::Type | TokenKind::Function => {
            crate::style::semantic_color(crate::style::SemanticColor::Info)
                .map(|token| token.legible_on(surface, 3.0))
                .unwrap_or(ink)
        }
        _ => ink,
    }
}

/// Draws one parsed preview row.
fn draw_parsed_line(
    context: &mut RenderContext,
    band: Rect,
    parsed: &ParsedLine,
    base_font: &Font,
    ink: Color,
    accent: Color,
    surface: Color,
) {
    match parsed.row {
        PreviewRow::Rule => {
            context.fill_rect(
                Rect::new(band.x, band.y + (band.height as i32 / 2), band.width, 1),
                ink.blend(&surface, 0.5),
            );
            return;
        }
        PreviewRow::Code => {
            context.fill_rect(band, ink.with_alpha(12));
        }
        PreviewRow::Fence => {}
        // The heading's runs were measured with the level's own size; the drawing font below
        // re-derives the same size.
        PreviewRow::Heading(_) => {}
        PreviewRow::Inline | PreviewRow::Table => {}
    }
    let font = match parsed.row {
        PreviewRow::Heading(level) => {
            base_font.with_size(base_font.size() + (7 - level.min(6)) as f32)
        }
        _ => base_font.clone(),
    };
    let mut x = band.x;
    for run in &parsed.runs {
        draw_preview_run(context, run, &font, x, band.y, ink, accent, surface);
        x += run.width as i32;
    }
}

/// Draws one styled run at `x`.
fn draw_preview_run(
    context: &mut RenderContext,
    run: &PreviewRun,
    font: &Font,
    x: i32,
    y: i32,
    ink: Color,
    accent: Color,
    surface: Color,
) {
    let color = match run.style {
        PreviewStyle::Text => ink,
        PreviewStyle::Bold => ink,
        PreviewStyle::Italic => ink.blend(&surface, 0.15),
        PreviewStyle::Strike => ink.blend(&surface, 0.45),
        PreviewStyle::Code => ink,
        PreviewStyle::Link => accent,
        PreviewStyle::Dim => ink.blend(&surface, 0.5),
        PreviewStyle::Accent => accent,
        PreviewStyle::Token(kind) => token_color(kind, ink, accent, surface),
    };
    if matches!(run.style, PreviewStyle::Code) {
        context.fill_rect(
            Rect::new(x - 2, y, run.width + 4, TEXT_ROW_HEIGHT as u32),
            ink.with_alpha(24),
        );
    }
    context.draw_text(Point::new(x, y), &run.text, font, color, HorizontalAlignment::Left);
    match run.style {
        PreviewStyle::Bold => {
            // The face has one weight, so bold is the crate's one-pixel overprint.
            context.draw_text(
                Point::new(x + 1, y),
                &run.text,
                font,
                color,
                HorizontalAlignment::Left,
            );
        }
        PreviewStyle::Link => {
            context.fill_rect(Rect::new(x, y + TEXT_ROW_HEIGHT - 3, run.width.max(1), 1), color);
        }
        PreviewStyle::Strike => {
            context.fill_rect(Rect::new(x, y + TEXT_ROW_HEIGHT / 2, run.width.max(1), 1), color);
        }
        _ => {}
    }
}

/// Row height of one outline entry.
const OUTLINE_ROW_HEIGHT: i32 = 20;
/// How far each heading level indents its title.
const OUTLINE_INDENT: i32 = 12;

/// A document-outline side panel for a [`MarkdownEditor`].
///
/// The editor already computes its headings ([`MarkdownEditor::outline`]) and can navigate to
/// one ([`MarkdownEditor::goto_outline`]); what it never had was a control that *shows* them. This
/// panel is that view: it holds a copy of the heading list (refreshed by
/// [`Self::sync_with_editor`]), paints it as an indented list, and reports the row the reader
/// activated.
///
/// Selection and navigation are deliberately separate. `goto` selects a row and reports the line
/// it addresses; moving the editor's caret is the host's call, made through the editor method it
/// already has. That keeps the panel usable beside a document it did not open.
pub struct MarkdownOutlinePanel {
    base: BaseWidget,
    entries: Vec<MarkdownOutlineEntry>,
    /// The row painted as current, as an index into `entries`.
    active_index: Option<usize>,
    /// The width this panel prefers, reported through `size_hint`.
    width: u32,
    /// The first row the viewport paints.
    first_visible: usize,
    /// Emitted with the outline index when a row is activated.
    pub entry_selected: Signal1<usize>,
}

impl MarkdownOutlinePanel {
    /// Creates an empty panel 180 px wide.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(
                WidgetKind::MarkdownOutlinePanel,
                geometry,
                "MarkdownOutlinePanel",
            ),
            entries: Vec::new(),
            active_index: None,
            width: 180,
            first_visible: 0,
            entry_selected: Signal1::new(),
        }
    }

    /// The headings this panel shows.
    pub fn entries(&self) -> &[MarkdownOutlineEntry] {
        &self.entries
    }

    /// Replaces the entries, dropping an active index that no longer exists.
    pub fn set_entries(&mut self, entries: Vec<MarkdownOutlineEntry>) {
        self.entries = entries;
        if self.active_index.is_some_and(|index| index >= self.entries.len()) {
            self.active_index = None;
        }
        self.clamp_scroll();
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Copies `editor`'s outline into this panel.
    ///
    /// The active row follows the editor's caret: the last heading at or before the caret's line,
    /// which is the heading the reader is currently "inside". Without a preceding heading the
    /// first row is used so a document that starts with body text still has a current row.
    pub fn sync_with_editor(&mut self, editor: &MarkdownEditor) {
        let entries = editor.outline();
        let caret = editor.cursor_line();
        let active = entries
            .iter()
            .rposition(|entry| entry.line <= caret)
            .or_else(|| (!entries.is_empty()).then_some(0));
        self.entries = entries;
        self.active_index = active;
        self.clamp_scroll();
        self.base.request_redraw();
    }

    /// The row painted as current.
    pub fn active_index(&self) -> Option<usize> {
        self.active_index
    }

    /// Marks `index` as current without emitting `entry_selected`.
    ///
    /// This is the editor-to-panel direction (a caret move), which is not a reader action and must
    /// not be reported as one.
    pub fn set_active_index(&mut self, index: Option<usize>) {
        let next = index.filter(|index| *index < self.entries.len());
        if self.active_index == next {
            return;
        }
        self.active_index = next;
        if let Some(index) = next {
            self.reveal(index);
        }
        self.base.request_redraw();
    }

    /// Activates `index`, returning the document line it addresses.
    ///
    /// Returns `None` for a row that does not exist: a caller told about a line it cannot address
    /// would ask the editor to jump somewhere arbitrary.
    pub fn goto(&mut self, index: usize) -> Option<usize> {
        let line = self.entries.get(index)?.line;
        if self.active_index != Some(index) {
            self.active_index = Some(index);
            self.reveal(index);
            self.base.request_redraw();
        }
        self.entry_selected.emit(index);
        Some(line)
    }

    /// The preferred width reported through `size_hint`.
    pub fn width(&self) -> u32 {
        self.width
    }

    /// Sets the preferred width.
    pub fn set_width(&mut self, width: u32) {
        let width = width.max(40);
        if self.width == width {
            return;
        }
        self.width = width;
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Rows the viewport can paint.
    fn visible_rows(&self) -> usize {
        ((self.geometry().height as i32 / OUTLINE_ROW_HEIGHT).max(1)) as usize
    }

    /// The painted box of row `index`, when it is visible.
    pub fn row_rect(&self, index: usize) -> Option<Rect> {
        if index >= self.entries.len() {
            return None;
        }
        let row = index.checked_sub(self.first_visible)?;
        if row >= self.visible_rows() {
            return None;
        }
        let rect = self.geometry();
        Some(Rect::new(
            rect.x,
            rect.y + row as i32 * OUTLINE_ROW_HEIGHT,
            rect.width,
            OUTLINE_ROW_HEIGHT as u32,
        ))
    }

    /// The deepest indent the panel may draw, so a narrow panel keeps its titles readable.
    fn max_level_indent(&self) -> i32 {
        ((self.geometry().width as i32 - 40) / OUTLINE_INDENT).max(0)
    }

    /// Scrolls so `index` is visible.
    fn reveal(&mut self, index: usize) {
        let rows = self.visible_rows();
        if index < self.first_visible {
            self.first_visible = index;
        } else if index >= self.first_visible + rows {
            self.first_visible = index + 1 - rows;
        }
        self.clamp_scroll();
    }

    /// Keeps the scroll offset inside the entry list.
    fn clamp_scroll(&mut self) {
        let max_first = self.entries.len().saturating_sub(self.visible_rows());
        self.first_visible = self.first_visible.min(max_first);
    }

    /// The entry index under `pos`.
    ///
    /// Public because a host that mounts the panel in a container of its own resolves the press
    /// itself; the editor's mounted panel uses it for the same reason.
    pub fn entry_at(&self, pos: Point) -> Option<usize> {
        let rect = self.geometry();
        if !rect.contains_point(pos) {
            return None;
        }
        let row = (pos.y - rect.y) / OUTLINE_ROW_HEIGHT;
        if row < 0 {
            return None;
        }
        let index = self.first_visible + row as usize;
        (index < self.entries.len()).then_some(index)
    }
}

impl Widget for MarkdownOutlinePanel {
    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(self.width, 240)
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
    /// Resolves `entry_selected`; see `DiffViewer`'s note for why the arms are explicit.
    fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
        use crate::signal::EventSignalRef;
        match name {
            "entry_selected" => {
                Some(EventSignalRef::mapped("entry_selected", &self.entry_selected, |v| {
                    CapabilityValue::UInt(*v as u64)
                }))
            }
            _ => None,
        }
    }
}

impl WidgetProperties for MarkdownOutlinePanel {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "entries" => Ok(CapabilityValue::Tuple(
                self.entries
                    .iter()
                    .map(|entry| {
                        CapabilityValue::String(format!(
                            "{}|{}|{}",
                            entry.level, entry.title, entry.line
                        ))
                    })
                    .collect(),
            )),
            "active_index" => Ok(match self.active_index {
                Some(index) => CapabilityValue::UInt(index as u64),
                None => CapabilityValue::Null,
            }),
            "width" => Ok(CapabilityValue::UInt(self.width as u64)),
            "entry_count" => Ok(CapabilityValue::UInt(self.entries.len() as u64)),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "entries" => {
                let items = expect_string_list(value)?;
                let mut entries = Vec::with_capacity(items.len());
                for item in items {
                    let mut parts = item.splitn(3, '|');
                    let level = parts
                        .next()
                        .and_then(|text| text.trim().parse::<u8>().ok())
                        .ok_or(CapabilityAccessError::TypeMismatch)?;
                    let title = parts.next().unwrap_or_default().to_string();
                    let line = parts
                        .next()
                        .and_then(|text| text.trim().parse::<usize>().ok())
                        .ok_or(CapabilityAccessError::TypeMismatch)?;
                    entries.push(MarkdownOutlineEntry { line, level: level.clamp(1, 6), title });
                }
                self.set_entries(entries);
                Ok(())
            }
            "active_index" => match value {
                CapabilityValue::Null => {
                    self.set_active_index(None);
                    Ok(())
                }
                CapabilityValue::UInt(index) => {
                    let index =
                        usize::try_from(index).map_err(|_| CapabilityAccessError::TypeMismatch)?;
                    if index >= self.entries.len() {
                        return Err(CapabilityAccessError::OutOfRange);
                    }
                    self.set_active_index(Some(index));
                    Ok(())
                }
                _ => Err(CapabilityAccessError::TypeMismatch),
            },
            "width" => {
                let width = u32::try_from(expect_usize(value)?)
                    .map_err(|_| CapabilityAccessError::TypeMismatch)?;
                self.set_width(width);
                Ok(())
            }
            "entry_count" => Err(CapabilityAccessError::ReadOnlyProperty),
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of!["entries", "active_index", "width", "entry_count", BASE_PROPERTY_NAMES]
    }
}

impl EventHandler for MarkdownOutlinePanel {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if !self.base.is_enabled() {
            return;
        }
        match event {
            Event::MousePress { pos, button: 1, .. } => {
                if let Some(index) = self.entry_at(*pos) {
                    let _ = self.goto(index);
                }
            }
            Event::KeyPress { key, .. } => {
                let current = self.active_index.unwrap_or(0);
                match *key {
                    38 => {
                        if !self.entries.is_empty() {
                            let _ = self.goto(current.saturating_sub(1));
                        }
                    }
                    40 => {
                        if !self.entries.is_empty() {
                            let _ = self.goto((current + 1).min(self.entries.len() - 1));
                        }
                    }
                    13 if !self.entries.is_empty() => {
                        let _ = self.goto(current.min(self.entries.len() - 1));
                    }
                    _ => { /* Other keys are not relevant */ }
                }
            }
            _ => {}
        }
    }
}

impl Draw for MarkdownOutlinePanel {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        let style = self.base.style();
        let theme = crate::style::resolved_theme_style_shared("markdown_editor");
        let surface = style
            .background_color
            .or_else(|| theme.as_ref().and_then(|t| t.background_color))
            .unwrap_or(Color::WHITE);
        let ink = style
            .text_color
            .or_else(|| theme.as_ref().and_then(|t| t.text_color))
            .unwrap_or(Color::BLACK);
        let border = style
            .border_color
            .or_else(|| theme.as_ref().and_then(|t| t.border_color))
            .unwrap_or_else(|| crate::core::Color::border_of(surface));
        let accent =
            crate::style::semantic_color(crate::style::SemanticColor::Info).unwrap_or(Color::BLUE);
        let font = effective_font(style);

        context.face_with_gradient(
            rect,
            surface,
            self.style().background_gradient.as_ref(),
            self.style().surface.unwrap_or_default(),
            self.style().border_radius.unwrap_or(0),
            Color::BLACK,
        );
        crate::widget::draw::stroke_surface_border(
            context,
            rect,
            self.style().border_radius.unwrap_or(0),
            border,
            1,
        );

        let rows = self.visible_rows();
        for offset in 0..rows {
            let index = self.first_visible + offset;
            let Some(entry) = self.entries.get(index) else {
                break;
            };
            let Some(row) = self.row_rect(index) else {
                break;
            };
            if self.active_index == Some(index) {
                context.fill_rect(row, surface.blend(&accent, 0.20));
            }
            let indent = (entry.level.saturating_sub(1) as i32 * OUTLINE_INDENT)
                .min(self.max_level_indent());
            let title = crate::widget::text_utils::elide_end(
                &entry.title,
                ((row.width as i32 - indent - 8) / 7).max(1) as usize,
            );
            context.draw_text(
                Point::new(row.x + 6 + indent, row.y + 4),
                &title,
                font,
                ink,
                HorizontalAlignment::Left,
            );
        }
    }
}

/// One replacement the host is asked to apply.
///
/// Ranges are byte offsets into the document the bar was told about through
/// [`FindBar::set_text`], in document order. A host applies them back-to-front so an earlier
/// replacement does not shift a later range; `all` highlights are carried in one request rather
/// than one signal per match, because a signal has no way to say "and three more".
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReplaceRequest {
    /// Byte ranges to replace, in ascending document order.
    pub ranges: Vec<(usize, usize)>,
    /// Replacement text for every range.
    pub text: String,
}

/// Which field of a [`FindBar`] has focus.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
enum FindFocus {
    /// The query field; the default.
    #[default]
    Query,
    /// The replacement field.
    Replace,
}

/// Height of one field row in the find bar.
const FIND_ROW_HEIGHT: i32 = 26;
/// Side of a toggle cell (`Aa`, `W`).
const FIND_TOGGLE_SIDE: i32 = 18;

/// A reusable find/replace bar.
///
/// It is a control, not a dialog: it owns the query, the matching policy and the match list for a
/// document **snapshot** the host hands it, and reports navigation and replacement requests. It
/// does not own the document. That split is why [`Self::replace`] emits a
/// [`ReplaceRequest`] instead of mutating text it never received: a bar that edited a copy would
/// report success for an edit the document never saw.
pub struct FindBar {
    base: BaseWidget,
    visible: bool,
    query: String,
    replacement: String,
    case_sensitive: bool,
    whole_word: bool,
    /// Whether the replacement row is painted.
    replace_visible: bool,
    /// The document snapshot the matches address.
    haystack: String,
    /// `haystack` as `(byte offset, char)` pairs, rebuilt only when the snapshot changes.
    ///
    /// The matcher used to rebuild this vector on every keystroke of the query; the snapshot is
    /// what changes it, so it is built once in [`Self::set_text`].
    char_index: Vec<(usize, char)>,
    /// How many ranges the last replacement actually changed, as reported by the host.
    last_replaced: usize,
    /// Every match, in document order.
    matches: Vec<(usize, usize)>,
    /// The active match, as an index into `matches`.
    current: Option<usize>,
    /// Which field typing goes to.
    focus: FindFocus,
    /// Emitted with the active match's byte range, or `(0, 0)` when there is no match.
    pub search_changed: Signal1<(usize, usize)>,
    /// Emitted when the reader asks for a replacement.
    pub replace_requested: Signal1<ReplaceRequest>,
}

impl FindBar {
    /// Creates a closed find bar.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::FindBar, geometry, "FindBar"),
            visible: false,
            query: String::new(),
            replacement: String::new(),
            case_sensitive: false,
            whole_word: false,
            replace_visible: false,
            haystack: String::new(),
            char_index: Vec::new(),
            last_replaced: 0,
            matches: Vec::new(),
            current: None,
            focus: FindFocus::Query,
            search_changed: Signal1::new(),
            replace_requested: Signal1::new(),
        }
    }

    /// Whether the bar is showing.
    pub fn is_visible(&self) -> bool {
        self.visible
    }

    /// Shows the bar and focuses the query field.
    pub fn open(&mut self) {
        self.visible = true;
        self.focus = FindFocus::Query;
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Hides the bar without touching the document or the active match.
    pub fn close(&mut self) {
        if !self.visible {
            return;
        }
        self.visible = false;
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// Points the bar at `text` and recomputes the match list.
    ///
    /// The bar keeps the snapshot rather than a live handle because it has no way to observe a
    /// document edit; the host calls this after every change, which is also the moment the
    /// replacement it applied becomes visible to the next search.
    pub fn set_text(&mut self, text: &str) {
        self.haystack.clear();
        self.haystack.push_str(text);
        self.char_index = self.haystack.char_indices().collect();
        self.recompute_matches();
    }

    /// The document snapshot the matches address.
    pub fn text(&self) -> &str {
        &self.haystack
    }

    /// The current query.
    pub fn query(&self) -> &str {
        &self.query
    }

    /// Replaces the query and recomputes the matches.
    pub fn set_query(&mut self, query: impl Into<String>) {
        self.query = query.into();
        self.recompute_matches();
        self.base.request_redraw();
    }

    /// Whether matching is case-sensitive.
    pub fn case_sensitive(&self) -> bool {
        self.case_sensitive
    }

    /// Sets the case-sensitivity policy.
    pub fn set_case_sensitive(&mut self, sensitive: bool) {
        if self.case_sensitive == sensitive {
            return;
        }
        self.case_sensitive = sensitive;
        self.recompute_matches();
        self.base.request_redraw();
    }

    /// Whether matches must be whole words.
    pub fn whole_word(&self) -> bool {
        self.whole_word
    }

    /// Sets the whole-word policy.
    pub fn set_whole_word(&mut self, whole_word: bool) {
        if self.whole_word == whole_word {
            return;
        }
        self.whole_word = whole_word;
        self.recompute_matches();
        self.base.request_redraw();
    }

    /// The replacement text.
    pub fn replace_text(&self) -> &str {
        &self.replacement
    }

    /// Sets the replacement text.
    pub fn set_replace_text(&mut self, text: impl Into<String>) {
        self.replacement = text.into();
        self.base.request_redraw();
    }

    /// Whether the replacement row is painted.
    pub fn replace_visible(&self) -> bool {
        self.replace_visible
    }

    /// Shows or hides the replacement row.
    pub fn set_replace_visible(&mut self, visible: bool) {
        if self.replace_visible == visible {
            return;
        }
        self.replace_visible = visible;
        if !visible {
            self.focus = FindFocus::Query;
        }
        self.base.request_layout();
        self.base.request_redraw();
    }

    /// How many matches the current query has.
    pub fn match_count(&self) -> usize {
        self.matches.len()
    }

    /// The active match's byte range, when one is selected.
    pub fn current_range(&self) -> Option<(usize, usize)> {
        self.current.and_then(|index| self.matches.get(index).copied())
    }

    /// Selects the next match, wrapping at the end.
    ///
    /// Returns whether a match was selected; an empty match list reports `false` and emits the
    /// "no match" range, so a subscriber is told the search came up empty rather than left with
    /// the previous result.
    pub fn find_next(&mut self) -> bool {
        if self.matches.is_empty() {
            self.current = None;
            self.search_changed.emit((0, 0));
            return false;
        }
        let next = match self.current {
            Some(index) => (index + 1) % self.matches.len(),
            None => 0,
        };
        self.select_match(next);
        true
    }

    /// Selects the previous match, wrapping at the start.
    pub fn find_previous(&mut self) -> bool {
        if self.matches.is_empty() {
            self.current = None;
            self.search_changed.emit((0, 0));
            return false;
        }
        let next = match self.current {
            Some(0) => self.matches.len() - 1,
            Some(index) => index - 1,
            None => self.matches.len() - 1,
        };
        self.select_match(next);
        true
    }

    /// Selects match `index` and reports its range.
    fn select_match(&mut self, index: usize) {
        self.current = Some(index);
        if let Some(range) = self.current_range() {
            self.search_changed.emit(range);
        }
        self.base.request_redraw();
    }

    /// Recomputes `matches` and keeps the active match when it still exists.
    fn recompute_matches(&mut self) {
        let previous = self.current.and_then(|index| self.matches.get(index).copied());
        self.matches = find_matches(
            &self.haystack,
            &self.char_index,
            &self.query,
            self.case_sensitive,
            self.whole_word,
        );
        self.current = previous.and_then(|range| self.matches.iter().position(|hit| *hit == range));
        // Nothing is selected automatically: a query the reader is still typing is not yet a
        // navigation, and starting a match early would make the first `find_next` skip it.
    }

    /// How many ranges the host last reported as replaced.
    pub fn last_replaced_count(&self) -> usize {
        self.last_replaced
    }

    /// Records how many ranges a host-side replacement changed.
    ///
    /// The bar cannot observe the document, so the count is reported back to it by whoever applied
    /// the [`ReplaceRequest`]. Keeping it here is what makes the result visible in the bar instead
    /// of only in the host's state.
    pub fn set_last_replaced(&mut self, count: usize) {
        if self.last_replaced == count {
            return;
        }
        self.last_replaced = count;
        self.base.request_redraw();
    }

    /// Asks the host to replace the active match.
    ///
    /// Returns whether a request was emitted. The match list is left alone: the text it addresses
    /// has not changed yet, and a bar that renumbered its matches before the document did would
    /// point the next request at the wrong bytes.
    pub fn replace(&mut self) -> bool {
        let Some(range) = self.current_range() else {
            return false;
        };
        self.replace_requested
            .emit(ReplaceRequest { ranges: vec![range], text: self.replacement.clone() });
        true
    }

    /// Asks the host to replace every match in one request.
    pub fn replace_all(&mut self) -> bool {
        if self.matches.is_empty() {
            return false;
        }
        self.replace_requested
            .emit(ReplaceRequest { ranges: self.matches.clone(), text: self.replacement.clone() });
        true
    }

    /// The first row's box.
    fn query_band(&self) -> Rect {
        let rect = self.geometry();
        Rect::new(rect.x, rect.y, rect.width, FIND_ROW_HEIGHT.min(rect.height as i32) as u32)
    }

    /// The replacement row's box, when it is shown.
    fn replace_band(&self) -> Option<Rect> {
        if !self.replace_visible {
            return None;
        }
        let rect = self.geometry();
        let top = rect.y + FIND_ROW_HEIGHT;
        let height = rect.height as i32 - FIND_ROW_HEIGHT;
        (height > 0).then(|| Rect::new(rect.x, top, rect.width, height as u32))
    }

    /// The `Aa` toggle box.
    fn case_toggle_rect(&self) -> Rect {
        let band = self.query_band();
        Rect::new(
            band.x + band.width as i32 - 2 * FIND_TOGGLE_SIDE - 6,
            band.y + (band.height as i32 - FIND_TOGGLE_SIDE) / 2,
            FIND_TOGGLE_SIDE as u32,
            FIND_TOGGLE_SIDE as u32,
        )
    }

    /// The whole-word toggle box.
    fn word_toggle_rect(&self) -> Rect {
        let band = self.query_band();
        Rect::new(
            band.x + band.width as i32 - FIND_TOGGLE_SIDE - 3,
            band.y + (band.height as i32 - FIND_TOGGLE_SIDE) / 2,
            FIND_TOGGLE_SIDE as u32,
            FIND_TOGGLE_SIDE as u32,
        )
    }

    /// The box the query text occupies, between the left inset and the toggles.
    fn query_text_rect(&self) -> Rect {
        let band = self.query_band();
        let right = band.width as i32 - 2 * FIND_TOGGLE_SIDE - 12;
        Rect::new(band.x + 6, band.y, right.max(8) as u32, band.height)
    }

    /// The "replace" action cell on the replacement row.
    fn replace_button_rect(&self) -> Option<Rect> {
        let band = self.replace_band()?;
        Some(Rect::new(
            band.x + band.width as i32 - 74,
            band.y + 3,
            42,
            (band.height as i32 - 6).max(1) as u32,
        ))
    }

    /// The "replace all" action cell on the replacement row.
    fn replace_all_button_rect(&self) -> Option<Rect> {
        let band = self.replace_band()?;
        Some(Rect::new(
            band.x + band.width as i32 - 30,
            band.y + 3,
            28,
            (band.height as i32 - 6).max(1) as u32,
        ))
    }

    /// The box the replacement text occupies, left of the two action cells.
    fn replace_text_rect(&self) -> Option<Rect> {
        let band = self.replace_band()?;
        let width = (band.width as i32 - 86).max(8);
        Some(Rect::new(band.x + 6, band.y, width as u32, band.height))
    }
}

/// Case- and word-aware, non-overlapping literal search.
///
/// A literal search is what a find bar offers: the regex-capable editor has its own matcher, and
/// a second engine here would be a second set of semantics to keep in step. Offsets are byte
/// offsets into `hay`, so a host can splice the ranges directly.
fn find_matches(
    hay: &str,
    hay_chars: &[(usize, char)],
    needle: &str,
    case_sensitive: bool,
    whole_word: bool,
) -> Vec<(usize, usize)> {
    if needle.is_empty() || hay.is_empty() {
        return Vec::new();
    }
    let needle_chars: Vec<char> = needle.chars().collect();
    let len = needle_chars.len();
    if len == 0 || hay_chars.len() < len {
        return Vec::new();
    }
    let same = |a: char, b: char| {
        if case_sensitive {
            a == b
        } else {
            a.to_lowercase().eq(b.to_lowercase())
        }
    };
    // Boyer-Moore-Horspool's bad-character table, keyed by the case-folded last character. A
    // colliding fold only makes the shift shorter than the true one, which is safe: the matcher
    // still verifies the whole candidate before reporting it.
    let fold = |ch: char| {
        if case_sensitive {
            ch
        } else {
            ch.to_lowercase().next().unwrap_or(ch)
        }
    };
    let mut skip: HashMap<char, usize> = HashMap::new();
    for (index, ch) in needle_chars[..len - 1].iter().enumerate() {
        skip.insert(fold(*ch), len - 1 - index);
    }
    let is_word = |ch: char| ch.is_alphanumeric() || ch == '_';
    let mut matches = Vec::new();
    let mut index = 0usize;
    while index + len <= hay_chars.len() {
        let last = hay_chars[index + len - 1].1;
        if same(last, needle_chars[len - 1])
            && (0..len - 1).all(|offset| same(hay_chars[index + offset].1, needle_chars[offset]))
        {
            let starts_word = !whole_word || index == 0 || !is_word(hay_chars[index - 1].1);
            let ends_word =
                !whole_word || index + len == hay_chars.len() || !is_word(hay_chars[index + len].1);
            if starts_word && ends_word {
                let start = hay_chars[index].0;
                let end = hay_chars.get(index + len).map_or(hay.len(), |(offset, _)| *offset);
                matches.push((start, end));
                index += len;
                continue;
            }
        }
        let step = skip.get(&fold(last)).copied().unwrap_or(len);
        index += step.max(1);
    }
    matches
}

impl Widget for FindBar {
    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 {
        let height = if self.replace_visible {
            (FIND_ROW_HEIGHT * 2) as u32
        } else {
            FIND_ROW_HEIGHT as u32
        };
        crate::core::Size::new(360, height)
    }
    impl_draw_bridge!();
    impl_widget_property_hooks!();
    /// Resolves the bar's two events; see `DiffViewer`'s note for why the arms are explicit.
    fn event_signal_dyn(&self, name: &str) -> Option<crate::signal::EventSignalRef> {
        use crate::signal::EventSignalRef;
        match name {
            "search_changed" => {
                Some(EventSignalRef::mapped("search_changed", &self.search_changed, |v| {
                    CapabilityValue::Tuple(crate::compat::Vec::from([
                        CapabilityValue::UInt(v.0 as u64),
                        CapabilityValue::UInt(v.1 as u64),
                    ]))
                }))
            }
            "replace_requested" => {
                Some(EventSignalRef::mapped("replace_requested", &self.replace_requested, |v| {
                    CapabilityValue::Tuple(crate::compat::Vec::from([
                        CapabilityValue::Tuple(
                            v.ranges
                                .iter()
                                .map(|range| {
                                    CapabilityValue::Tuple(crate::compat::Vec::from([
                                        CapabilityValue::UInt(range.0 as u64),
                                        CapabilityValue::UInt(range.1 as u64),
                                    ]))
                                })
                                .collect(),
                        ),
                        CapabilityValue::String(v.text.clone()),
                    ]))
                }))
            }
            _ => None,
        }
    }
}

impl WidgetProperties for FindBar {
    fn get(&self, name: &str) -> Result<CapabilityValue, CapabilityAccessError> {
        match name {
            "query" => Ok(CapabilityValue::String(self.query.clone())),
            "case_sensitive" => Ok(CapabilityValue::Bool(self.case_sensitive)),
            "whole_word" => Ok(CapabilityValue::Bool(self.whole_word)),
            "replace_text" => Ok(CapabilityValue::String(self.replacement.clone())),
            "visible" => Ok(CapabilityValue::Bool(self.visible)),
            "replace_visible" => Ok(CapabilityValue::Bool(self.replace_visible)),
            "match_count" => Ok(CapabilityValue::UInt(self.matches.len() as u64)),
            "current_range" => Ok(match self.current_range() {
                Some((start, end)) => CapabilityValue::String(format!("{start},{end}")),
                None => CapabilityValue::Null,
            }),
            _ => base_property_get(self, name),
        }
    }

    fn set(&mut self, name: &str, value: CapabilityValue) -> Result<(), CapabilityAccessError> {
        match name {
            "query" => {
                self.set_query(expect_string(value)?);
                Ok(())
            }
            "case_sensitive" => {
                self.set_case_sensitive(expect_bool(value)?);
                Ok(())
            }
            "whole_word" => {
                self.set_whole_word(expect_bool(value)?);
                Ok(())
            }
            "replace_text" => {
                self.set_replace_text(expect_string(value)?);
                Ok(())
            }
            "visible" => {
                if expect_bool(value)? {
                    self.open();
                } else {
                    self.close();
                }
                Ok(())
            }
            "replace_visible" => {
                self.set_replace_visible(expect_bool(value)?);
                Ok(())
            }
            "match_count" | "current_range" => Err(CapabilityAccessError::ReadOnlyProperty),
            _ => base_property_set(self, name, value),
        }
    }

    fn property_names(&self) -> &'static [&'static str] {
        property_names_of![
            "query",
            "case_sensitive",
            "whole_word",
            "replace_text",
            "visible",
            "replace_visible",
            "match_count",
            "current_range",
            BASE_PROPERTY_NAMES
        ]
    }
}

impl EventHandler for FindBar {
    fn handle_event(&mut self, event: &Event) {
        self.base.handle_event(event);
        if !self.base.is_enabled() || !self.visible {
            return;
        }
        match event {
            Event::MousePress { pos, button: 1, .. } => {
                if self.case_toggle_rect().contains_point(*pos) {
                    self.set_case_sensitive(!self.case_sensitive);
                    return;
                }
                if self.word_toggle_rect().contains_point(*pos) {
                    self.set_whole_word(!self.whole_word);
                    return;
                }
                if self.replace_button_rect().is_some_and(|cell| cell.contains_point(*pos)) {
                    self.replace();
                    return;
                }
                if self.replace_all_button_rect().is_some_and(|cell| cell.contains_point(*pos)) {
                    self.replace_all();
                    return;
                }
                if self.replace_band().is_some_and(|band| band.contains_point(*pos)) {
                    self.focus = FindFocus::Replace;
                    self.base.request_redraw();
                    return;
                }
                if self.query_band().contains_point(*pos) {
                    self.focus = FindFocus::Query;
                    self.base.request_redraw();
                }
            }
            Event::TextInput { text } | Event::ImeCommit { text } => {
                let target = match self.focus {
                    FindFocus::Query => &mut self.query,
                    FindFocus::Replace => &mut self.replacement,
                };
                target.extend(text.chars().filter(|character| !character.is_control()));
                if matches!(self.focus, FindFocus::Query) {
                    self.recompute_matches();
                }
                self.base.request_redraw();
            }
            Event::KeyPress { key, modifiers } => {
                let shift = crate::shortcut::Modifiers::from_event_bits(*modifiers)
                    .contains(crate::shortcut::Modifiers::SHIFT);
                match *key {
                    crate::event::key_codes::ESCAPE => self.close(),
                    crate::event::key_codes::TAB => {
                        if self.replace_visible {
                            self.focus = match self.focus {
                                FindFocus::Query => FindFocus::Replace,
                                FindFocus::Replace => FindFocus::Query,
                            };
                            self.base.request_redraw();
                        }
                    }
                    crate::event::key_codes::ENTER => {
                        if shift {
                            self.find_previous();
                        } else {
                            self.find_next();
                        }
                    }
                    crate::event::key_codes::BACKSPACE => {
                        let target = match self.focus {
                            FindFocus::Query => &mut self.query,
                            FindFocus::Replace => &mut self.replacement,
                        };
                        target.pop();
                        if matches!(self.focus, FindFocus::Query) {
                            self.recompute_matches();
                        }
                        self.base.request_redraw();
                    }
                    _ => { /* Other keys are not relevant */ }
                }
            }
            _ => {}
        }
    }
}

impl Draw for FindBar {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        if rect.width == 0 || rect.height == 0 {
            return;
        }
        let style = self.base.style();
        let theme = crate::style::resolved_theme_style_shared("markdown_editor");
        let surface = style
            .background_color
            .or_else(|| theme.as_ref().and_then(|t| t.background_color))
            .unwrap_or(Color::WHITE);
        let ink = style
            .text_color
            .or_else(|| theme.as_ref().and_then(|t| t.text_color))
            .unwrap_or(Color::BLACK);
        let border = style
            .border_color
            .or_else(|| theme.as_ref().and_then(|t| t.border_color))
            .unwrap_or_else(|| crate::core::Color::border_of(surface));
        let accent =
            crate::style::semantic_color(crate::style::SemanticColor::Info).unwrap_or(Color::BLUE);
        let font = effective_font(style);

        context.face_with_gradient(
            rect,
            surface,
            self.style().background_gradient.as_ref(),
            self.style().surface.unwrap_or_default(),
            self.style().border_radius.unwrap_or(0),
            Color::BLACK,
        );
        crate::widget::draw::stroke_surface_border(
            context,
            rect,
            self.style().border_radius.unwrap_or(0),
            border,
            1,
        );

        let query_band = self.query_band();
        let text_rect = self.query_text_rect();
        let counter = if self.matches.is_empty() {
            "0/0".to_string()
        } else {
            format!("{}/{}", self.current.map_or(0, |index| index + 1), self.matches.len())
        };
        let counter_width = context.measure_text(&counter, font).width as i32;
        context.draw_text(
            Point::new(text_rect.x, text_rect.y + 7),
            &crate::widget::text_utils::elide_end(
                &self.query,
                ((text_rect.width as i32 - counter_width - 8) / 7).max(1) as usize,
            ),
            font,
            if self.query.is_empty() { ink.with_alpha(120) } else { ink },
            HorizontalAlignment::Left,
        );
        context.draw_text(
            Point::new(
                query_band.x + query_band.width as i32 - 2 * FIND_TOGGLE_SIDE - 12 - counter_width,
                query_band.y + 7,
            ),
            &counter,
            font,
            ink.blend(&surface, 0.35),
            HorizontalAlignment::Left,
        );

        for (box_rect, label, active) in [
            (self.case_toggle_rect(), "Aa", self.case_sensitive),
            (self.word_toggle_rect(), "W", self.whole_word),
        ] {
            if active {
                context.fill_rounded_rect(box_rect, 3, surface.blend(&accent, 0.30));
                crate::widget::draw::stroke_surface_border(context, box_rect, 3, accent, 1);
            }
            context.draw_text(
                Point::new(box_rect.x + 4, box_rect.y + 3),
                label,
                font,
                ink,
                HorizontalAlignment::Left,
            );
        }

        if let Some(band) = self.replace_band() {
            context.draw_line(
                Point::new(band.x, band.y),
                Point::new(band.x + band.width as i32, band.y),
                border,
            );
            if let Some(text_rect) = self.replace_text_rect() {
                context.draw_text(
                    Point::new(text_rect.x, text_rect.y + 7),
                    &crate::widget::text_utils::elide_end(
                        &self.replacement,
                        (text_rect.width as i32 / 7).max(1) as usize,
                    ),
                    font,
                    if self.replacement.is_empty() { ink.with_alpha(120) } else { ink },
                    HorizontalAlignment::Left,
                );
            }
            // The two replacement actions are controls on the bar, so a reader never has to know
            // that the host applies their request; the result count is the feedback that the
            // request reached the document.
            for (cell, label) in
                [(self.replace_button_rect(), "Repl"), (self.replace_all_button_rect(), "All")]
            {
                let Some(cell) = cell else {
                    continue;
                };
                let enabled = !self.matches.is_empty();
                context.fill_rounded_rect(
                    cell,
                    3,
                    if enabled { surface.blend(&accent, 0.25) } else { surface },
                );
                context.draw_rect(cell, if enabled { accent } else { border });
                context.draw_text_fitted_cow(
                    cell,
                    label,
                    font,
                    if enabled { ink } else { ink.with_alpha(120) },
                    HorizontalAlignment::Center,
                );
            }
            if self.last_replaced > 0 {
                let label = format!("{} replaced", self.last_replaced);
                context.draw_text(
                    Point::new(
                        band.x + band.width as i32
                            - 84
                            - context.measure_text(&label, font).width as i32,
                        band.y + 7,
                    ),
                    &label,
                    font,
                    ink.blend(&surface, 0.35),
                    HorizontalAlignment::Left,
                );
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::widget::input_widgets::shortcut_editor::{
        non_primary_modifier_event_bits, primary_modifier_event_bits,
    };
    use std::sync::{Arc, Mutex};

    #[test]
    fn metrics_follow_text_changes() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("# Title\nhello world\n## Details");

        assert_eq!(editor.line_count(), 3);
        assert_eq!(editor.word_count(), 6);
        assert_eq!(editor.heading_count(), 2);
    }

    #[test]
    fn preview_toggle_emits_signal() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        let states = Arc::new(Mutex::new(Vec::<bool>::new()));
        let sink = states.clone();
        editor.preview_mode_changed.connect(move |value| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(*value);
            }
        });

        editor.toggle_preview_mode();
        editor.toggle_preview_mode();

        let got = states.lock().ok().map(|guard| guard.clone()).unwrap_or_default();
        assert_eq!(got, vec![true, false]);
    }

    #[test]
    fn arrow_keys_move_cursor() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("a\nb\nc");

        editor.handle_event(&Event::key_press(40, 0));
        editor.handle_event(&Event::key_press(40, 0));
        assert_eq!(editor.cursor_line(), 2);

        editor.handle_event(&Event::key_press(38, 0));
        assert_eq!(editor.cursor_line(), 1);
    }

    #[test]
    fn undo_redo_restores_markdown_text() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("# One");
        editor.append_line("body");

        assert!(editor.can_undo());
        assert!(editor.undo());
        assert_eq!(editor.text(), "# One");
        assert!(editor.can_redo());
        assert!(editor.redo());
        assert_eq!(editor.text(), "# One\nbody");
    }

    #[test]
    fn keyboard_shortcuts_drive_markdown_history() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("draft");
        editor.set_text("final");

        editor.handle_event(&Event::KeyPress { key: 90, modifiers: primary_modifier_event_bits() });
        assert_eq!(editor.text(), "draft");
        editor.handle_event(&Event::KeyPress { key: 89, modifiers: primary_modifier_event_bits() });
        assert_eq!(editor.text(), "final");
    }

    #[test]
    fn keyboard_shortcuts_reject_shifted_and_non_primary_chords() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("draft");
        editor.set_text("final");

        let primary = primary_modifier_event_bits();
        // Shift+Primary+Z is a different chord, so it is not plain undo.
        editor.handle_event(&Event::key_press(90, primary | 0b0001));
        assert_eq!(editor.text(), "final");

        // The accelerator that is not primary on this host does not undo.
        editor.handle_event(&Event::key_press(90, non_primary_modifier_event_bits()));
        assert_eq!(editor.text(), "final");

        editor.handle_event(&Event::key_press(90, primary));
        assert_eq!(editor.text(), "draft");
        editor.handle_event(&Event::key_press(89, primary));
        assert_eq!(editor.text(), "final");
    }

    #[test]
    fn text_input_inserts_text_and_emits_signal() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        let seen = Arc::new(Mutex::new(Vec::<String>::new()));
        let sink = seen.clone();
        editor.text_changed.connect(move |value| {
            if let Ok(mut guard) = sink.lock() {
                guard.push((*value).clone());
            }
        });

        editor.handle_event(&Event::text_input("# Hello"));

        assert_eq!(editor.text(), "# Hello");
        let got = seen.lock().ok().map(|guard| guard.clone()).unwrap_or_default();
        assert_eq!(got, vec!["# Hello".to_string()]);
    }

    #[test]
    fn ime_commit_inserts_cjk_text() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.handle_event(&Event::ime_commit("你好"));

        assert_eq!(editor.text(), "你好");
        assert_eq!(editor.cursor_col(), 2);
    }

    #[test]
    fn typed_text_can_be_undone_and_redone() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.handle_event(&Event::text_input("first"));
        editor.handle_event(&Event::ime_commit(" second"));
        assert_eq!(editor.text(), "first second");

        assert!(editor.undo());
        assert_eq!(editor.text(), "first");
        assert!(editor.undo());
        assert_eq!(editor.text(), "");

        assert!(editor.redo());
        assert!(editor.redo());
        assert_eq!(editor.text(), "first second");
    }

    #[test]
    fn enter_splits_and_backspace_merges_lines() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("ab");

        editor.handle_event(&Event::key_press(39, 0));
        editor.handle_event(&Event::key_press(39, 0));
        editor.handle_event(&Event::key_press(13, 0));
        assert_eq!(editor.text(), "ab\n");
        assert_eq!(editor.cursor_line(), 1);
        assert_eq!(editor.cursor_col(), 0);

        editor.handle_event(&Event::key_press(8, 0));
        assert_eq!(editor.text(), "ab");
        assert_eq!(editor.cursor_line(), 0);
        assert_eq!(editor.cursor_col(), 2);

        editor.handle_event(&Event::key_press(37, 0));
        editor.handle_event(&Event::key_press(8, 0));
        assert_eq!(editor.text(), "b");
        assert_eq!(editor.cursor_col(), 0);
    }

    #[test]
    fn arrow_keys_clamp_cursor_column_to_line_length() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("long line\nx");

        editor.handle_event(&Event::key_press(39, 0));
        editor.handle_event(&Event::key_press(39, 0));
        assert_eq!(editor.cursor_col(), 2);

        editor.handle_event(&Event::key_press(40, 0));
        assert_eq!(editor.cursor_line(), 1);
        assert_eq!(editor.cursor_col(), 1);
    }

    /// The wheel scrolls the visible line window and clamps at both ends.
    ///
    /// # The defect this pins
    ///
    /// The paint path stopped at a fixed `take(10)`, so a longer document could not be reached at
    /// all. This drives the same window the paint path now starts from.
    #[test]
    fn markdown_editor_wheel_scrolls_and_clamps() {
        let text: Vec<String> = (0..20).map(|n| format!("line{n}")).collect();
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 100));
        editor.set_text(text.join("\n"));
        assert_eq!(editor.first_visible_line, 0);

        // One notch toward the end reveals three lines.
        editor.handle_event(&Event::Wheel { delta: crate::core::Point::new(0, -1), modifiers: 0 });
        assert_eq!(editor.first_visible_line, 3, "a downward notch moves the viewport down");

        // The other direction returns to the top and clamps there.
        editor.handle_event(&Event::Wheel { delta: crate::core::Point::new(0, 1), modifiers: 0 });
        assert_eq!(editor.first_visible_line, 0);
        editor.handle_event(&Event::Wheel { delta: crate::core::Point::new(0, 1), modifiers: 0 });
        assert_eq!(editor.first_visible_line, 0, "the viewport clamps at the first line");

        // Scrolling on stops at the last full window.
        let max_first = editor.line_count() - editor.visible_line_count();
        for _ in 0..20 {
            editor.handle_event(&Event::Wheel {
                delta: crate::core::Point::new(0, -1),
                modifiers: 0,
            });
        }
        assert_eq!(editor.first_visible_line, max_first, "the viewport clamps at the last window");
    }

    /// A whole-text replacement still records only the changed middle for undo.
    #[test]
    fn set_text_undoes_a_paragraph_change_without_losing_the_rest() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("alpha\nbeta\ngamma");
        editor.set_text("alpha\nBETA 你好\ngamma");

        assert!(editor.undo());
        assert_eq!(editor.text(), "alpha\nbeta\ngamma");
        assert!(editor.redo());
        assert_eq!(editor.text(), "alpha\nBETA 你好\ngamma");
    }

    /// Runs `f` with a real paint context, for the paths that need measurements.
    fn with_context<T>(f: impl FnOnce(&RenderContext) -> T) -> T {
        use crate::render::{PaintBackend, SoftwarePaintBackend};
        let mut backend = SoftwarePaintBackend::new(crate::core::Size::new(400, 300), 1.0);
        backend.begin_frame(Color::WHITE);
        let result = {
            let context = RenderContext::new(&mut backend);
            f(&context)
        };
        backend.end_frame();
        result
    }

    /// The counts are cached against the revision, so a still frame walks no document.
    ///
    /// `line_count`/`word_count`/`heading_count` each scanned the whole document, and the paint
    /// path called all three per frame.
    #[test]
    fn metrics_are_cached_until_the_revision_changes() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("# One\ntwo words here");
        assert_eq!((editor.line_count(), editor.word_count(), editor.heading_count()), (2, 5, 1));
        let cached = *editor.metrics.borrow();
        assert_eq!(cached.map(|(revision, _)| revision), Some(editor.revision));

        editor.set_text("# One\ntwo words here\nthree");
        assert_eq!(editor.word_count(), 6, "an edit invalidates the cache");
        assert_eq!(
            editor.metrics.borrow().map(|(revision, _)| revision),
            Some(editor.revision),
            "and the next read refreshes it"
        );
    }

    /// An IME composition is visible before it is committed, anchored at the caret.
    #[test]
    fn ime_preedit_is_visible_at_the_caret() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("ab");
        // `set_text` leaves the caret where it was, so it is moved to the end explicitly.
        editor.handle_event(&Event::key_press(35, 0));
        assert!(editor.preedit_screen_rect().is_none(), "nothing is composing yet");

        editor.handle_event(&Event::ime_preedit("你好", 3));
        let at_end = editor.preedit_screen_rect().expect("a composition has a rectangle");
        assert_eq!(editor.preedit.as_ref().map(|p| p.cursor), Some(3));

        editor.handle_event(&Event::key_press(37, 0));
        let mid_line = editor.preedit_screen_rect().expect("still composing");
        assert!(mid_line.x < at_end.x, "the composition moved back with the caret");

        editor.handle_event(&Event::ime_commit("你好"));
        assert!(editor.preedit_screen_rect().is_none(), "a commit ends the composition");
    }

    /// The preview recognises the GFM blocks and inline styles it used to print as source.
    #[test]
    fn preview_recognises_gfm_constructs() {
        with_context(|context| {
            let style = crate::style::WidgetStyle::default();
            let font = effective_font(&style);
            let heading =
                parse_preview_line("# Title", BlockStart::Normal, true, true, false, font, context);
            assert!(matches!(heading.row, PreviewRow::Heading(1)));
            assert!(heading.runs.iter().all(|run| !run.text.contains('#')));

            let task = parse_preview_line(
                "- [x] done",
                BlockStart::Normal,
                true,
                true,
                false,
                font,
                context,
            );
            assert_eq!(task.runs.first().map(|run| run.text.as_str()), Some("☑ "));

            let strike = parse_preview_line(
                "~~gone~~",
                BlockStart::Normal,
                true,
                true,
                false,
                font,
                context,
            );
            assert!(strike.runs.iter().any(|run| run.style == PreviewStyle::Strike));

            let quote = parse_preview_line(
                "> quoted",
                BlockStart::Normal,
                true,
                true,
                false,
                font,
                context,
            );
            assert_eq!(quote.runs.first().map(|run| run.style), Some(PreviewStyle::Accent));

            let rule =
                parse_preview_line("---", BlockStart::Normal, true, true, false, font, context);
            assert!(matches!(rule.row, PreviewRow::Rule));

            let fence =
                parse_preview_line("```rust", BlockStart::Fence, true, true, false, font, context);
            assert!(matches!(fence.row, PreviewRow::Fence));

            let table = parse_preview_line(
                "| a | b |",
                BlockStart::Normal,
                true,
                true,
                false,
                font,
                context,
            );
            assert!(matches!(table.row, PreviewRow::Table));
            assert!(table.runs.iter().any(|run| run.text.contains('│')));

            // GFM recognises a table by the delimiter row after it; a pipe in prose is not one.
            let header =
                parse_preview_line("a | b", BlockStart::Normal, true, true, true, font, context);
            assert!(matches!(header.row, PreviewRow::Table));
            let prose =
                parse_preview_line("a | b", BlockStart::Normal, true, true, false, font, context);
            assert!(!matches!(prose.row, PreviewRow::Table));

            let image = parse_preview_line(
                "![alt](x.png)",
                BlockStart::Normal,
                true,
                true,
                false,
                font,
                context,
            );
            assert!(image.runs.iter().any(|run| run.style == PreviewStyle::Link));

            // With the extensions off, the same text stays literal.
            let plain = parse_preview_line(
                "~~gone~~",
                BlockStart::Normal,
                false,
                true,
                false,
                font,
                context,
            );
            assert!(plain.runs.iter().all(|run| run.style != PreviewStyle::Strike));
            assert!(plain.runs.iter().any(|run| run.text.contains("~~")));
        });
    }

    /// Fenced code is tokenized with the shared lexer, so keywords are not body text.
    #[test]
    fn fenced_code_is_highlighted_with_the_shared_lexer() {
        with_context(|context| {
            let style = crate::style::WidgetStyle::default();
            let font = effective_font(&style);
            let parsed = parse_preview_line(
                "let x = 1;",
                BlockStart::Code(LanguageId::Rust, LineState::initial()),
                true,
                true,
                false,
                font,
                context,
            );
            assert!(matches!(parsed.row, PreviewRow::Code));
            assert!(
                parsed.runs.iter().any(|run| run.style == PreviewStyle::Token(TokenKind::Keyword)),
                "`let` must be a keyword token"
            );
        });
    }

    /// Selection, typing over it and cutting all work on the real document.
    #[test]
    fn selection_replaces_text_and_cut_deletes_it() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("alpha beta");
        editor.select_all();
        assert_eq!(editor.selected_text().as_deref(), Some("alpha beta"));

        editor.insert_text_at_cursor("X");
        assert_eq!(editor.text(), "X", "typing replaces the selection in one step");
        assert!(editor.selection().is_none());

        editor.set_text("abc");
        editor.select_all();
        editor.cut();
        assert_eq!(editor.text(), "", "cut removes the selected run");
    }

    /// The selection signal reports presence, and Ctrl+A drives it from the keyboard.
    #[test]
    fn select_all_emits_selection_changed() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("text");
        let seen = Arc::new(Mutex::new(Vec::<bool>::new()));
        let sink = seen.clone();
        editor.selection_changed.connect(move |present| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(*present);
            }
        });

        editor.handle_event(&Event::key_press(65, primary_modifier_event_bits()));
        assert_eq!(editor.selected_text().as_deref(), Some("text"));

        editor.handle_event(&Event::key_press(37, 0));
        let seen = seen.lock().ok().map(|guard| guard.clone()).unwrap_or_default();
        assert_eq!(seen, vec![true, false], "a plain arrow clears the selection");
    }

    /// Home/End/Delete/PageDown and the scrollbar reach the document.
    #[test]
    fn keyboard_covers_line_edges_delete_and_paging() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 100));
        let text: Vec<String> = (0..30).map(|n| format!("line{n}")).collect();
        editor.set_text(text.join("\n"));

        editor.handle_event(&Event::key_press(35, 0));
        assert_eq!(editor.cursor_col(), "line0".chars().count(), "End reaches the line's end");
        editor.handle_event(&Event::key_press(36, 0));
        assert_eq!(editor.cursor_col(), 0, "Home returns to its start");

        editor.handle_event(&Event::key_press(46, 0));
        assert_eq!(editor.text().split('\n').next(), Some("ine0"), "Delete removes forward");

        assert!(editor.scroll_page(1));
        assert!(editor.first_visible_line > 0, "PageDown moves the viewport");
        editor.handle_event(&Event::key_press(33, 0));
        assert_eq!(editor.first_visible_line, 0, "PageUp returns to the top");
    }

    /// The caret blinks on the ticks it is given — this control was the only text field in the
    /// crate whose caret never moved.
    #[test]
    fn the_caret_blinks_on_the_ticks_it_is_given() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        assert!(editor.is_animating());
        assert!(editor.is_caret_visible());
        editor.tick(crate::style::animation::CURSOR_BLINK_HALF_PERIOD_MS);
        assert!(!editor.is_caret_visible(), "one half-period hides the caret");
        editor.tick(crate::style::animation::CURSOR_BLINK_HALF_PERIOD_MS);
        assert!(editor.is_caret_visible());

        editor.set_enabled(false);
        editor.handle_event(&Event::key_press(65, 0));
        assert!(!editor.is_animating(), "a disabled editor holds a steady caret");
    }

    /// The scrollbar track jumps the viewport, and clicking a preview heading announces it.
    #[test]
    fn scrollbar_and_heading_clicks_navigate() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 200));
        let text: Vec<String> = (0..40).map(|n| format!("line{n}")).collect();
        editor.set_text(text.join("\n"));

        editor.handle_event(&Event::mouse_press(414, 190, 1));
        assert!(editor.first_visible_line > 0, "a click near the track's bottom jumps forward");

        let mut headings = MarkdownEditor::new(Rect::new(0, 0, 420, 200));
        headings.set_text("# First\n## Second");
        headings.set_preview_mode(true);
        let seen = Arc::new(Mutex::new(Vec::<usize>::new()));
        let sink = seen.clone();
        headings.heading_activated.connect(move |index| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(*index);
            }
        });
        headings.handle_event(&Event::mouse_press(40, TEXT_TOP + 2, 1));
        let seen = seen.lock().ok().map(|guard| guard.clone()).unwrap_or_default();
        assert_eq!(seen, vec![0], "the heading row announces its outline index");
    }

    /// The find bar takes typed text and Enter selects the next match.
    #[test]
    fn find_bar_selects_matches() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("alpha beta alpha");
        assert!(editor.find_next("alpha"));
        assert_eq!(editor.selection(), Some((0, 5)));
        assert!(editor.find_next("alpha"));
        assert_eq!(editor.selection(), Some((11, 16)), "the next call moves past the match");
        assert!(editor.find_previous("alpha"));
        assert_eq!(editor.selection(), Some((0, 5)));

        editor.open_find();
        assert!(editor.find_bar_visible());
        editor.handle_event(&Event::text_input("beta"));
        assert_eq!(editor.find_query(), "beta", "typing goes to the query, not the document");
        editor.handle_event(&Event::key_press(13, 0));
        assert_eq!(editor.selected_text().as_deref(), Some("beta"));
        editor.handle_event(&Event::key_press(27, 0));
        assert!(!editor.find_bar_visible());
    }

    #[test]
    fn outline_panel_follows_the_editor_caret() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("# One\ntext\n## Two\nmore");
        let mut panel = MarkdownOutlinePanel::new(Rect::new(0, 0, 180, 200));
        panel.sync_with_editor(&editor);
        assert_eq!(panel.entries().len(), 2);
        assert_eq!(panel.entries()[0].title, "One");
        assert_eq!(panel.active_index(), Some(0));

        assert!(editor.goto_line(2));
        panel.sync_with_editor(&editor);
        assert_eq!(panel.active_index(), Some(1), "the caret is inside the second heading");

        let selected = std::sync::Arc::new(std::sync::Mutex::new(Vec::<usize>::new()));
        let sink = selected.clone();
        panel.entry_selected.connect(move |index| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(*index);
            }
        });
        assert_eq!(panel.goto(0), Some(0), "goto reports the line it addresses");
        assert_eq!(*selected.lock().expect("selected lock"), vec![0]);
        assert_eq!(panel.goto(9), None);
    }

    #[test]
    fn find_bar_navigates_case_and_whole_word_matches() {
        let mut bar = FindBar::new(Rect::new(0, 0, 360, 26));
        bar.set_text("Alpha alpha alphabet alpha");
        bar.set_query("alpha");
        assert_eq!(bar.match_count(), 4, "case-insensitive by default, substrings included");
        assert!(bar.find_next());
        assert_eq!(bar.current_range(), Some((0, 5)));
        assert!(bar.find_next());
        assert_eq!(bar.current_range(), Some((6, 11)));

        bar.set_whole_word(true);
        assert_eq!(bar.match_count(), 3);
        bar.set_case_sensitive(true);
        assert_eq!(bar.match_count(), 2, "only the lowercase, whole-word hits remain");
        bar.set_query("zzz");
        assert!(!bar.find_next(), "an empty match list reports no match");
    }

    #[test]
    fn find_bar_replace_request_reaches_the_editor_and_is_undoable() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("cat dog cat");
        let mut bar = FindBar::new(Rect::new(0, 0, 360, 52));
        bar.set_replace_visible(true);
        bar.set_replace_text("fox");
        editor.sync_find_bar(&mut bar);
        bar.set_query("cat");
        assert_eq!(bar.match_count(), 2);

        let requests = std::sync::Arc::new(std::sync::Mutex::new(Vec::<ReplaceRequest>::new()));
        let sink = requests.clone();
        bar.replace_requested.connect(move |request| {
            if let Ok(mut guard) = sink.lock() {
                guard.push((*request).clone());
            }
        });
        assert!(bar.replace_all());
        let request = requests.lock().expect("requests lock")[0].clone();
        assert_eq!(request.ranges, vec![(0, 3), (8, 11)]);

        assert_eq!(editor.apply_replace_request(&request), 2);
        assert_eq!(editor.text(), "fox dog fox");
        assert!(editor.undo());
        assert_eq!(editor.text(), "cat dog cat");
    }

    #[test]
    fn find_bar_properties_round_trip() {
        let mut bar = FindBar::new(Rect::new(0, 0, 360, 26));
        bar.set("query", CapabilityValue::String("a".to_string())).expect("query writable");
        bar.set("case_sensitive", CapabilityValue::Bool(true)).expect("case writable");
        bar.set("whole_word", CapabilityValue::Bool(true)).expect("word writable");
        bar.set("replace_text", CapabilityValue::String("b".to_string()))
            .expect("replace writable");
        bar.set("visible", CapabilityValue::Bool(true)).expect("visible writable");
        assert!(bar.is_visible());
        assert_eq!(bar.get("query"), Ok(CapabilityValue::String("a".to_string())));
        assert_eq!(bar.get("case_sensitive"), Ok(CapabilityValue::Bool(true)));
        assert_eq!(bar.get("whole_word"), Ok(CapabilityValue::Bool(true)));
        assert_eq!(bar.get("replace_text"), Ok(CapabilityValue::String("b".to_string())));
        assert_eq!(
            bar.set("match_count", CapabilityValue::UInt(0)),
            Err(CapabilityAccessError::ReadOnlyProperty)
        );
    }

    /// The undo depth is a constructor argument and a property.
    #[test]
    fn undo_depth_is_configurable() {
        let mut editor = MarkdownEditor::with_undo_limit(Rect::new(0, 0, 420, 240), 2);
        assert_eq!(editor.undo_stack_limit(), 2);
        editor.set_text("a");
        editor.insert_text_at_cursor("b");
        editor.insert_text_at_cursor("c");

        assert!(editor.undo());
        assert!(editor.undo());
        assert!(!editor.undo(), "only the configured depth is retained");
        assert_eq!(editor.text(), "a");

        editor.set_undo_limit(8);
        assert_eq!(editor.undo_stack_limit(), 8);
    }

    /// The new properties round-trip through the property contract.
    #[test]
    fn new_properties_round_trip() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        assert_eq!(editor.get("gfm_extensions").ok(), Some(CapabilityValue::Bool(true)));
        editor.set("gfm_extensions", CapabilityValue::Bool(false)).expect("writable");
        assert!(!editor.gfm_extensions());
        editor.set("code_highlight", CapabilityValue::Bool(false)).expect("writable");
        assert!(!editor.code_highlight());
        editor.set("show_scrollbar", CapabilityValue::Bool(false)).expect("writable");
        assert!(!editor.show_scrollbar());
        editor.set("undo_stack_limit", CapabilityValue::UInt(7)).expect("writable");
        assert_eq!(editor.undo_stack_limit(), 7);
    }

    /// The editor mounts a real outline panel: a press in its side column navigates, and the
    /// painted rows come from the same heading list `outline()` reports.
    #[test]
    fn mounted_outline_panel_navigates_and_paints() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("# One\ntext\n## Two\nmore");
        editor.set_show_outline(true);
        assert_eq!(editor.outline_panel().entries().len(), 2);
        assert_eq!(editor.outline_panel().active_index(), Some(0));

        // A first paint places the companion and proves the path draws without panicking.
        use crate::render::{PaintBackend, SoftwarePaintBackend};
        let mut backend = SoftwarePaintBackend::new(crate::core::Size::new(420, 240), 1.0);
        backend.begin_frame(Color::WHITE);
        {
            let mut context = RenderContext::new(&mut backend);
            editor.draw(&mut context);
        }
        backend.end_frame();

        // The panel is 180 px wide, so it starts at x = 240; the second row sits one row down.
        editor.handle_event(&Event::mouse_press(250, TEXT_TOP + OUTLINE_ROW_HEIGHT + 2, 1));
        assert_eq!(editor.cursor_line(), 2, "the second outline row navigates to line 2");
        assert_eq!(editor.outline_panel().active_index(), Some(1));
    }

    /// The mounted find bar is the painted form of the editor's own find state: typing goes to the
    /// same query the editor's Enter handler searches, and closing mirrors both.
    #[test]
    fn mounted_find_bar_reflects_the_editor_query() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 240));
        editor.set_text("alpha beta alpha");
        editor.show_find_bar();
        assert!(editor.find_bar().is_visible());
        assert_eq!(editor.find_bar().text(), "alpha beta alpha");

        editor.handle_event(&Event::text_input("beta"));
        assert_eq!(editor.find_query(), "beta");
        assert_eq!(editor.find_bar().query(), "beta");
        assert_eq!(editor.find_bar().match_count(), 1);

        editor.handle_event(&Event::key_press(27, 0));
        assert!(!editor.find_bar_visible());
        assert!(!editor.find_bar().is_visible());
    }
}

#[cfg(test)]
mod issue_2_8_tests {
    use super::*;
    use crate::widget::input_widgets::shortcut_editor::primary_modifier_event_bits;
    use std::sync::{Arc, Mutex};

    fn editor(text: &str) -> MarkdownEditor {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 300));
        editor.set_text(text);
        editor
    }

    fn draw_once(editor: &mut MarkdownEditor) {
        use crate::render::{PaintBackend, SoftwarePaintBackend};
        let mut backend = SoftwarePaintBackend::new(crate::core::Size::new(400, 300), 1.0);
        backend.begin_frame(Color::WHITE);
        {
            let mut context = RenderContext::new(&mut backend);
            editor.draw(&mut context);
        }
        backend.end_frame();
    }

    /// SP-02: Tab indents the selected lines and Shift+Tab reverses it.
    #[test]
    fn tab_indents_and_shift_tab_outdents_selected_lines() {
        let mut editor = editor("alpha\nbeta");
        editor.select_all();
        editor.handle_key(9, 0);
        assert_eq!(editor.text(), "  alpha\n  beta");
        editor.handle_key(9, crate::shortcut::Modifiers::SHIFT.bits() as u32);
        assert_eq!(editor.text(), "alpha\nbeta");
    }

    /// SP-03: the formatting action wraps a selection and a second call unwraps it.
    #[test]
    fn format_actions_wrap_and_unwrap_the_selection() {
        let mut editor = editor("hello");
        editor.select_all();
        assert!(editor.wrap_selection("**", "**"));
        assert_eq!(editor.text(), "**hello**");
        assert!(editor.wrap_selection("**", "**"));
        assert_eq!(editor.text(), "hello");
    }

    /// SP-03: the keyboard chord drives the same action.
    #[test]
    fn primary_b_wraps_the_selection() {
        let mut editor = editor("hello");
        editor.select_all();
        editor.handle_key(66, primary_modifier_event_bits());
        assert_eq!(editor.text(), "**hello**");
    }

    /// SP-04: primary+Shift+Z redoes, the chord the handler used to ignore.
    #[test]
    fn primary_shift_z_redoes() {
        let mut editor = editor("ab");
        assert!(editor.undo());
        assert_eq!(editor.text(), "");
        let modifiers =
            primary_modifier_event_bits() | crate::shortcut::Modifiers::SHIFT.bits() as u32;
        editor.handle_key(90, modifiers);
        assert_eq!(editor.text(), "ab");
    }

    /// SP-03: a line prefix toggle is one undo step for a multi-line selection.
    #[test]
    fn line_prefix_toggles_every_selected_line_as_one_undo_step() {
        let mut editor = editor("one\ntwo");
        editor.select_all();
        assert!(editor.toggle_line_prefix("- "));
        assert_eq!(editor.text(), "- one\n- two");
        assert!(editor.undo());
        assert_eq!(editor.text(), "one\ntwo");
    }

    /// COMP-H04: the toolbar emits only actions it actually offers.
    #[test]
    fn toolbar_executes_only_enabled_actions() {
        let mut toolbar = MarkdownToolbar::new(Rect::new(0, 0, 300, 28));
        let hits = Arc::new(Mutex::new(Vec::<String>::new()));
        let sink = hits.clone();
        toolbar.action_triggered.connect_ref(move |token| {
            if let Ok(mut guard) = sink.lock() {
                guard.push(token.clone());
            }
        });
        assert!(toolbar.execute("bold"));
        assert!(!toolbar.execute("nope"));
        assert_eq!(hits.lock().ok().map(|guard| guard.clone()).unwrap_or_default(), vec!["bold"]);
        assert!(!toolbar.set_actions(vec!["nope".to_string()]));
        assert_eq!(toolbar.actions().len(), 7);
    }

    /// SP-P03: the preview cache keeps live entries when it is bounded.
    #[test]
    fn the_preview_cache_keeps_live_lines_when_it_is_trimmed() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 300));
        editor.set_preview_mode(true);
        let text: String = (0..200).map(|line| format!("line {line}\n")).collect();
        editor.set_text(text);
        draw_once(&mut editor);
        // The buffer is longer than the bound only after several revisions; the assertion that
        // matters is that the live entries survive, so the following frame still draws them.
        editor.set_text(editor.text().to_string() + "tail");
        draw_once(&mut editor);
        assert!(editor.line_count() >= 200);
    }

    /// SP-P04: caret geometry follows edits instead of rescanning the document.
    #[test]
    fn caret_geometry_tracks_edits_with_the_line_index() {
        let mut editor = MarkdownEditor::new(Rect::new(0, 0, 420, 300));
        editor.set_text("one\ntwo\nthree");
        editor.goto_line(2);
        editor.move_to_line_edge(false);
        assert_eq!(editor.cursor_line(), 2);
        assert_eq!(editor.cursor_col(), 5);
        editor.insert_text_at_cursor("!");
        assert_eq!(editor.text(), "one\ntwo\nthree!");
        assert_eq!(editor.cursor_line(), 2);
        assert_eq!(editor.cursor_col(), 6);
    }
}