bobview 1.8.5

A lean viewer for Phoebus Display Builder .bob and CS-Studio BOY .opi screens, with live EPICS PVs
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
//! Painting and interaction for each widget type.

use bob::{Action, Macros, Source, Widget};
use eframe::egui::{
    self, Align2, Color32, CornerRadius, FontId, Id, Pos2, Rect, Response, Sense, Shape, Stroke,
    StrokeKind, Ui, Vec2,
};
use pv::{PvManager, PvState, Value};

pub(crate) use crate::format::format_value;
use crate::rules::{self, Outcome};
use crate::screen::{Builder, Node, Screen, group_inset, tab_elements};
use crate::style::{self, View, color, named, severity_color};
use crate::view::WindowKey;

/// Requests that widgets pass up to the application.
pub enum Request {
    Open {
        source: Source,
        macros: Macros,
        target: String,
    },
    Webpage(String),
    /// Show the details of a widget's PV, from its context menu.
    Details(crate::pvmenu::Details),
    /// Open a trend window of these PVs.
    Trend(Vec<String>),
    /// Add these PVs to the open trend window with this id.
    TrendAdd(u64, Vec<String>),
    /// Close the display: go back to the one before, or close the window.
    Close,
    /// Bring every window to the front, the one asking last.
    RaiseAll,
    /// Close every display and trend window but the main one and the one
    /// asking.
    CloseOthers,
    /// Run a command action of the display at `base` (see
    /// [`crate::launch::command_target`]) and open the display it names.
    Command {
        base: Option<Source>,
        directory: Option<Source>,
        command: String,
    },
}

/// Per-frame drawing context.
pub struct Cx<'a> {
    pub view: View,
    pub clip: Rect,
    pub pvs: &'a PvManager,
    pub requests: &'a mut Vec<Request>,
    pub depth: u32,
    /// Touch mode: finger-sized rows in drop-down lists.
    pub touch: bool,
    /// The open trend windows, (id, title), that a PV's context menu can
    /// add it to.
    pub trends: &'a [(u64, String)],
    /// The window being drawn, which its requests act on.
    pub window: WindowKey,
}

impl Cx<'_> {
    fn sub<'b>(&'b mut self, view: View, clip: Rect) -> Cx<'b> {
        Cx {
            view,
            clip,
            pvs: self.pvs,
            requests: self.requests,
            depth: self.depth + 1,
            touch: self.touch,
            trends: self.trends,
            window: self.window,
        }
    }
}

/// The smallest target, in points, a finger can hit reliably.
pub const TOUCH_TARGET: f32 = 44.0;

/// egui's own hit radius outside touch mode.
const MOUSE_RADIUS: f32 = 5.0;

/// How far outside a control a touch still hits it: enough to make a 20
/// point control (a pvi slide button or checkbox at 100%) a full
/// [`TOUCH_TARGET`].
const TOUCH_RADIUS: f32 = (TOUCH_TARGET - 20.0) / 2.0;

/// Grow the hit area of every control in touch mode, without
/// changing how anything is drawn. egui hit-tests in two steps: a touch
/// directly on a control always goes to that control (the topmost one), and
/// only a touch on no control at all goes to the nearest control within
/// `interact_radius`. So a small control gets a finger-sized target in the
/// space around it, but never takes a touch from a neighbour that was hit
/// directly, and the gap between two controls is shared by distance.
pub fn touch_targets(ctx: &egui::Context, touch: bool) {
    let radius = if touch { TOUCH_RADIUS } else { MOUSE_RADIUS };
    if ctx.global_style().interaction.interact_radius != radius {
        ctx.all_styles_mut(|s| s.interaction.interact_radius = radius);
    }
}

pub fn draw_screen(ui: &mut Ui, screen: &mut Screen, cx: &mut Cx, id: Id) {
    draw_nodes(ui, &mut screen.nodes, cx, id);
}

pub fn draw_nodes(ui: &mut Ui, nodes: &mut [Node], cx: &mut Cx, id: Id) {
    for (i, node) in nodes.iter_mut().enumerate() {
        draw_node(ui, node, cx, id.with(i));
    }
}

/// Apply the outcomes of the node's rules to its properties, when they
/// change. A rule on `x`, `y`, `width` or `height` moves or sizes the
/// widget (and its children with it), and one on `pv_name` connects it to
/// the PV named.
pub(crate) fn update_rules(node: &mut Node, pvs: &pv::PvManager) {
    if node.rules.is_empty() {
        return;
    }
    // Rules depend only on their PVs: none changed, nothing to do.
    let versions: Vec<u64> = node
        .rules
        .iter()
        .flat_map(|r| r.pvs())
        .map(pv::Pv::version)
        .collect();
    if node.rule_versions.as_ref() == Some(&versions) {
        return;
    }
    node.rule_versions = Some(versions);
    let outcomes: Vec<(String, Outcome)> = node
        .rules
        .iter()
        .map(|r| (r.prop_id.clone(), r.evaluate()))
        .collect();
    if outcomes != node.rule_outcomes {
        let orig = node.orig_props.get_or_insert_with(|| node.w.props.clone());
        let mut props = orig.clone();
        rules::apply(&mut props, &outcomes);
        let before = crate::screen::geometry(&node.w);
        node.w.props = props;
        node.rule_outcomes = outcomes;
        let after = crate::screen::geometry(&node.w);
        if before != after {
            node.reshape(before, after);
        }
        let pv_name = node.prop("pv_name");
        if pv_name != node.pv_name {
            node.pv = (!pv_name.is_empty()).then(|| pvs.subscribe(&pv_name));
            node.pv_name = pv_name;
        }
    }
}

fn draw_node(ui: &mut Ui, node: &mut Node, cx: &mut Cx, id: Id) {
    crate::group_link::update(node, cx.pvs);
    update_rules(node, cx.pvs);
    if node.w.bool("visible") == Some(false) {
        return;
    }
    if node.w.kind == "label" && node.w.bool("auto_size") == Some(true) {
        auto_size(ui.painter(), node, cx.view.zoom);
    }
    let rect = cx.view.rect(&node.rect);
    if !rect.intersects(cx.clip) {
        return;
    }
    let painter = ui.painter().with_clip_rect(cx.clip);
    let z = cx.view.zoom;
    let state = node.state();

    let response = match node.w.kind.as_str() {
        "label" => {
            label(&painter, node, rect, z);
            None
        }
        "textupdate" => {
            text_update(&painter, node, state.as_ref(), rect, z);
            None
        }
        "textentry" => Some(text_entry(ui, node, state.as_ref(), rect, cx, id)),
        "led" => {
            led(&painter, &node.w, state.as_ref(), rect, z);
            None
        }
        "multi_state_led" => {
            multi_state_led(&painter, &node.w, state.as_ref(), rect, z);
            None
        }
        "byte_monitor" => {
            byte_monitor(&painter, &node.w, state.as_ref(), rect, z);
            None
        }
        "action_button" => Some(action_button(ui, node, rect, cx, id)),
        "bool_button" => Some(bool_button(ui, node, state.as_ref(), rect, cx, id)),
        "choice" | "radio" => Some(choice(ui, node, state.as_ref(), rect, cx, id)),
        "combo" => Some(combo(ui, node, state.as_ref(), rect, cx, id)),
        "checkbox" => Some(checkbox(ui, node, state.as_ref(), rect, cx, id)),
        "slide_button" => Some(slide_button(ui, node, state.as_ref(), rect, cx, id)),
        "rectangle" | "ellipse" | "arc" => {
            shape(&painter, &node.w, rect, z);
            None
        }
        "polyline" | "polygon" => {
            poly(&painter, &node.w, cx.view, &node.rect);
            None
        }
        "linearmeter" => {
            crate::meter::linear_meter(&painter, &node.w, state.as_ref(), rect, z);
            None
        }
        "xyplot" => {
            crate::plot::draw(&painter, node, rect, z);
            None
        }
        "stripchart" => {
            crate::trend::draw_stripchart(&painter, node, rect, z);
            None
        }
        "meter" => {
            crate::meter::dial(&painter, &node.w, state.as_ref(), rect, z);
            None
        }
        "progressbar" | "tank" | "thermometer" => {
            bar(&painter, &node.w, state.as_ref(), rect, z);
            None
        }
        "scaledslider" | "scrollbar" | "spinner" => {
            Some(slider(ui, node, state.as_ref(), rect, cx, id))
        }
        "group" => {
            group(ui, node, rect, cx, id);
            None
        }
        "tabs" => {
            tabs(ui, node, rect, cx, id);
            None
        }
        "embedded" => {
            embedded(ui, node, rect, cx, id);
            None
        }
        "navtabs" => {
            navtabs(ui, node, rect, cx, id);
            None
        }
        "symbol" => crate::symbol::draw_symbol(ui, node, state.as_ref(), rect, cx, id),
        "picture" => {
            crate::symbol::draw_picture(ui, node, rect, cx);
            None
        }
        "image" => Some(crate::image::draw(
            ui,
            node,
            state.as_ref(),
            rect,
            cx.clip,
            id,
            z,
        )),
        other => {
            let what = if node.pv_name.is_empty() {
                other.to_string()
            } else {
                format!("{other}\n{}", node.pv_name)
            };
            placeholder(&painter, rect, &what, z);
            None
        }
    };

    if matches!(
        node.w.kind.as_str(),
        "label" | "textupdate" | "textentry" | "embedded"
    ) {
        border(&painter, &node.w, rect, z);
    }
    if let Some(state) = &state {
        alarm_border(&painter, &node.w, state, rect, z);
    }
    // A shape or monitor (such as an EDM related-display hotspot from an
    // `.opi`) that runs its actions when clicked.
    let control = response.is_some();
    let response = match response {
        None if !is_container(&node.w.kind) => click_hook(ui, node, rect, cx, id),
        r => r,
    };

    // A control that is disabled or whose PV is read-only is greyed out and
    // shows a "not allowed" cursor; its writes are refused in `write`. The
    // image and symbol widgets respond to the pointer but are not controls,
    // nor is a monitor that only runs actions when clicked.
    let disabled = control
        && !matches!(node.w.kind.as_str(), "image" | "symbol")
        && is_disabled(&node.w, state.as_ref());
    // A slide button greys only its track: the rest of its box is see-through.
    if disabled && node.w.kind != "slide_button" {
        painter.rect_filled(rect, CornerRadius::ZERO, Color32::from_white_alpha(110));
    }

    // Hover shows the tooltip: the widget's own, or the PV name and value.
    // Right-click (long-press on a touch screen) opens a menu to copy them or
    // show the PV's details.
    if !cx.clip.contains_rect(rect) {
        return;
    }
    if !node.pv_name.is_empty() {
        let resp = response.unwrap_or_else(|| ui.interact(rect, id.with("hover"), Sense::click()));
        let resp = if disabled {
            resp.on_hover_cursor(egui::CursorIcon::NotAllowed)
        } else {
            resp
        };
        // The context menu has its own popup id, so it does not fight a
        // combo or action button's own menu (which uses the default id).
        egui::Popup::context_menu(&resp)
            .id(id.with("context-menu"))
            .show(|ui| crate::pvmenu::menu(ui, node, state.as_ref(), cx.trends, cx.requests));
        let text = tooltip(node, state.as_ref());
        if !text.is_empty() {
            resp.on_hover_text(text);
        }
    } else if node.w.get("tooltip").is_some() && !is_container(&node.w.kind) {
        let text = tooltip(node, None);
        if !text.is_empty() {
            let resp =
                response.unwrap_or_else(|| ui.interact(rect, id.with("hover"), Sense::hover()));
            resp.on_hover_text(text);
        }
    }
}

/// The widget's `border_width` and `border_color`, drawn around it.
fn border(painter: &egui::Painter, w: &Widget, rect: Rect, z: f32) {
    let width = w.f64("border_width").unwrap_or(0.0) as f32 * z;
    if width <= 0.0 {
        return;
    }
    let c = w
        .color("border_color")
        .map(|c| color(&c))
        .unwrap_or_else(|| named("Text"));
    painter.rect_stroke(
        rect,
        CornerRadius::ZERO,
        Stroke::new(width, c),
        StrokeKind::Outside,
    );
}

/// Run the actions of a widget with `run_actions_on_mouse_click` when it is
/// clicked; its response, if it has any such actions.
fn click_hook(ui: &mut Ui, node: &Node, rect: Rect, cx: &mut Cx, id: Id) -> Option<Response> {
    let w = &node.w;
    if w.bool("run_actions_on_mouse_click") != Some(true) || w.bool("enabled") == Some(false) {
        return None;
    }
    let actions = w.actions();
    if actions.is_empty() {
        return None;
    }
    let resp = ui
        .interact(rect.intersect(cx.clip), id.with("click"), Sense::click())
        .on_hover_cursor(egui::CursorIcon::PointingHand);
    if resp.clicked() {
        for a in &actions {
            run_action(node, a, cx);
        }
    }
    Some(resp)
}

/// Widgets that hold others; their tooltips would cover their children's.
fn is_container(kind: &str) -> bool {
    matches!(kind, "group" | "tabs" | "embedded" | "navtabs")
}

/// The hover text: the widget's `tooltip` with `$(pv_name)` and `$(pv_value)`
/// filled in, or, without one, the PV name and value as Phoebus's default
/// `$(pv_name)\n$(pv_value)` shows them (with the alarm severity).
fn tooltip(node: &Node, s: Option<&PvState>) -> String {
    let value = match s {
        Some(s) if s.connected => s.format(None, true),
        Some(s) => s.error.clone().unwrap_or_else(|| "disconnected".into()),
        None => String::new(),
    };
    match node.w.text("tooltip") {
        Some(t) => {
            // `$(actions)` lists what the widget's actions do, as in Phoebus.
            let actions = node.w.actions();
            let t = if t.contains("$(actions)") {
                let list: Vec<&str> = actions
                    .iter()
                    .map(|a| a.description())
                    .filter(|d| !d.trim().is_empty())
                    .collect();
                t.replace("$(actions)", &list.join("\n"))
            } else {
                t.to_string()
            };
            tooltip_text(&t, &node.pv_name, &value, &|t| node.expand(t))
        }
        None => {
            let name = plain_pv_name(&node.pv_name);
            match s {
                Some(s) if s.connected => format!("{name}\n{value} {:?}", s.severity),
                Some(_) => format!("{name}\n{value}"),
                None => name,
            }
        }
    }
}

fn tooltip_text(
    template: &str,
    pv_name: &str,
    value: &str,
    expand: &dyn Fn(&str) -> String,
) -> String {
    let t = template
        .replace("$(pv_name)", pv_name)
        .replace("$(pv_value)", value);
    expand(&t).trim().to_string()
}

// --- helpers -----------------------------------------------------------------

/// A PV name as a user reads it, without a local PV's type and initial value
/// (`loc://arr`, not `loc://arr<VDoubleArray>(1, 3, 2)`).
pub(crate) fn plain_pv_name(name: &str) -> String {
    crate::app::plain_name(&pv::PvName::parse(name))
}

pub(crate) fn fg(w: &Widget) -> Color32 {
    w.color("foreground_color")
        .map(|c| color(&c))
        .unwrap_or(Color32::BLACK)
}

pub(crate) fn bg(w: &Widget, default: &str) -> Color32 {
    w.color("background_color")
        .map(|c| color(&c))
        .unwrap_or_else(|| named(default))
}

pub(crate) fn font(w: &Widget, z: f32) -> FontId {
    style::font_id(&w.font("font").unwrap_or_default(), z)
}

pub(crate) fn fill(painter: &egui::Painter, rect: Rect, c: Color32) {
    painter.rect_filled(rect, CornerRadius::ZERO, c);
}

pub(crate) fn outline(painter: &egui::Painter, rect: Rect, width: f32, c: Color32) {
    painter.rect_stroke(
        rect,
        CornerRadius::ZERO,
        Stroke::new(width, c),
        StrokeKind::Inside,
    );
}

/// Paint `text` in `rect` the way a JavaFX `Label` does: each row aligned on
/// its own, only whole rows that fit the height, and overflow elided with `…`
/// so the start of the text always stays visible.
pub(crate) fn text_in(
    painter: &egui::Painter,
    rect: Rect,
    text: &str,
    font: FontId,
    c: Color32,
    align: Align2,
    wrap: bool,
) {
    text_turned(painter, rect, text, font, c, align, wrap, 0);
}

/// [`text_in`], turned by Phoebus `rotation_step`: 1 is 90° anticlockwise
/// (reading upwards), 2 is 180° and 3 is 90° clockwise.
#[allow(clippy::too_many_arguments)]
pub(crate) fn text_turned(
    painter: &egui::Painter,
    rect: Rect,
    text: &str,
    font: FontId,
    c: Color32,
    align: Align2,
    wrap: bool,
    step: i64,
) {
    use std::f32::consts::{FRAC_PI_2, PI};
    let angle = match step {
        1 => -FRAC_PI_2,
        2 => PI,
        3 => FRAC_PI_2,
        _ => 0.0,
    };
    // Laid out in the box the text has before turning.
    let size = if step == 1 || step == 3 {
        Vec2::new(rect.height(), rect.width())
    } else {
        rect.size()
    };
    let area = Rect::from_center_size(rect.center(), size);
    let row_height = painter.fonts_mut(|f| f.row_height(&font));
    let job = text_job(text, font, c, size, row_height, align.x(), wrap);
    let galley = painter.layout_job(job);
    let x = match align.x() {
        egui::Align::Min => area.left() + 1.0,
        egui::Align::Center => area.center().x,
        egui::Align::Max => area.right() - 1.0,
    };
    let height = galley.size().y;
    let y = match align.y() {
        egui::Align::Min => area.top(),
        egui::Align::Center => area.center().y - height / 2.0,
        egui::Align::Max => area.bottom() - height,
    };
    let painter = painter.with_clip_rect(rect.intersect(painter.clip_rect()));
    if angle == 0.0 {
        painter.galley(Pos2::new(x, y), galley, c);
    } else {
        let center = rect.center();
        let pos = center + egui::emath::Rot2::from_angle(angle) * (Pos2::new(x, y) - center);
        painter.add(egui::epaint::TextShape::new(pos, galley, c).with_angle(angle));
    }
}

/// The layout for [`text_in`]: rows aligned on their own and limited to what
/// fits in `size`, with the overflow elided.
fn text_job(
    text: &str,
    font: FontId,
    c: Color32,
    size: Vec2,
    row_height: f32,
    halign: egui::Align,
    wrap: bool,
) -> egui::text::LayoutJob {
    let mut job = egui::text::LayoutJob::simple(text.to_string(), font, c, f32::INFINITY);
    job.halign = halign;
    if !wrap && text.contains('\n') {
        // like a JavaFX Label without wrapping: keep every explicit line,
        // unwrapped, and let the widget's clip trim what doesn't fit
        return job;
    }
    let rows = max_rows(size.y, row_height, wrap);
    job.wrap = egui::text::TextWrapping {
        // the full width: the widget clip hides the 1 px the text may be
        // inset by, and any less elides text that fits at fractional zooms
        max_rows: rows,
        break_anywhere: rows == 1,
        ..elide_wrap(size.x)
    };
    job
}

/// One row `width` wide, with what does not fit elided with `…`.
fn elide_wrap(width: f32) -> egui::text::TextWrapping {
    egui::text::TextWrapping {
        max_width: width.max(1.0),
        max_rows: 1,
        break_anywhere: true,
        overflow_character: Some('…'),
    }
}

/// How many rows of text to lay out in a box `height` high: as many whole
/// rows as fit when wrapping (allowing a little slack for font metrics), and
/// always at least one.
fn max_rows(height: f32, row_height: f32, wrap: bool) -> usize {
    if !wrap || row_height <= 0.0 {
        return 1;
    }
    (((height + 2.0) / row_height).floor() as usize).max(1)
}

fn alignment(w: &Widget, v_default: i64) -> Align2 {
    style::align(
        w.i64("horizontal_alignment").unwrap_or(0),
        w.i64("vertical_alignment").unwrap_or(v_default),
    )
}

/// Button-like 3D box.
pub(crate) fn button_box(
    painter: &egui::Painter,
    rect: Rect,
    fill_color: Color32,
    pressed: bool,
    z: f32,
) {
    // Like a JavaFX Button: rounded, with a grey outline on all four sides so
    // it reads as a button on any background, a light top half for the
    // gradient, and a darker fill while pressed.
    let r = CornerRadius::same((3.0 * z).min(255.0) as u8);
    let w = (1.0 * z).max(1.0);
    let fill_color = if pressed {
        fill_color.lerp_to_gamma(Color32::BLACK, 0.12)
    } else {
        fill_color
    };
    painter.rect_filled(rect, r, fill_color);
    if !pressed {
        let top = Rect::from_min_max(rect.min, Pos2::new(rect.right(), rect.center().y)).shrink(w);
        let tr = CornerRadius {
            sw: 0,
            se: 0,
            ..CornerRadius::same(r.nw.saturating_sub(1))
        };
        painter.rect_filled(top, tr, Color32::from_white_alpha(40));
    }
    painter.rect_stroke(
        rect,
        r,
        Stroke::new(w, Color32::from_gray(150)),
        StrokeKind::Inside,
    );
}

pub(crate) fn placeholder(painter: &egui::Painter, rect: Rect, what: &str, z: f32) {
    fill(painter, rect, Color32::from_gray(235));
    outline(painter, rect, 1.0, Color32::from_gray(160));
    text_in(
        painter,
        rect,
        what,
        FontId::proportional(11.0 * z.max(0.6)),
        Color32::from_gray(90),
        Align2::CENTER_CENTER,
        true,
    );
}

/// The `format` that shows (and writes) an array of numbers as a long
/// string, as areaDetector's `FilePath` and other char waveforms are.
const STRING_FORMAT: i64 = 6;

/// Whether a control writes its text as a long string, as Phoebus does
/// when its `format` is string: see [`pv::Pv::write_long_string`].
fn writes_long_string(w: &Widget) -> bool {
    w.i64("format") == Some(STRING_FORMAT)
}

/// The `bit` property: Phoebus defaults it to 0 for checkbox, slide_button and
/// bool_button, and to -1 (the whole value) for LEDs.
fn bit(w: &Widget) -> i64 {
    let default = match w.kind.as_str() {
        "checkbox" | "slide_button" | "bool_button" => 0,
        _ => -1,
    };
    w.i64("bit").unwrap_or(default)
}

fn bit_on(v: i64, bit: i64) -> bool {
    if bit < 0 { v != 0 } else { (v >> bit) & 1 == 1 }
}

fn is_on(w: &Widget, s: &PvState) -> bool {
    bit_on(s.value.as_i64().unwrap_or(0), bit(w))
}

/// `value` with the widget's `bit` set to `on`, or 0/1 when `bit` is -1 (the
/// whole value), as Phoebus's checkbox, bool and slide buttons write it.
fn with_bit(bit: i64, value: i64, on: bool) -> i64 {
    if !(0..63).contains(&bit) {
        on as i64
    } else if on {
        value | (1 << bit)
    } else {
        value & !(1 << bit)
    }
}

/// The value that toggles the widget's bit (or the whole value).
fn toggled(w: &Widget, s: Option<&PvState>) -> i64 {
    let bit = bit(w);
    let on = s.is_some_and(|s| s.connected && is_on(w, s));
    let v = s.and_then(|s| s.value.as_i64()).unwrap_or(0);
    with_bit(bit, v, !on)
}

/// Phoebus disables a control whose `enabled` property is false or whose PV
/// is connected without write access.
fn is_disabled(w: &Widget, s: Option<&PvState>) -> bool {
    w.bool("enabled") == Some(false) || s.is_some_and(|s| s.connected && !s.writable)
}

/// Whether a control may write its PV: enabled, connected and writable.
fn can_write(w: &Widget, s: Option<&PvState>) -> bool {
    !is_disabled(w, s) && s.is_some_and(|s| s.connected)
}

fn write(node: &Node, value: impl Into<String>) {
    write_with(node, |pv| pv.write(value));
}

/// Write to the widget's PV with `put`, if the control may write it.
fn write_with(node: &Node, put: impl FnOnce(&pv::Pv)) {
    if let Some(pv) = &node.pv {
        if can_write(&node.w, Some(&pv.state())) {
            put(pv);
        } else {
            log::debug!("{}: not writing, the control is disabled", node.pv_name);
        }
    }
}

// --- static and monitor widgets --------------------------------------------

fn label(painter: &egui::Painter, node: &Node, rect: Rect, z: f32) {
    let w = &node.w;
    if w.bool("transparent") == Some(false) {
        fill(painter, rect, bg(w, "Background"));
    }
    let text = node.prop("text");
    let wrap = w.bool("wrap_words").unwrap_or(true);
    text_turned(
        painter,
        rect,
        &text,
        font(w, z),
        fg(w),
        alignment(w, 0),
        wrap,
        w.i64("rotation_step").unwrap_or(0),
    );
}

/// Size an `auto_size` label to its text, as Phoebus does: the width and
/// height become those of the expanded text on one row per line, with x and
/// y kept. Done each frame, so a rule that changes the text resizes it too.
fn auto_size(painter: &egui::Painter, node: &mut Node, z: f32) {
    let w = &node.w;
    let galley = painter.layout_no_wrap(node.prop("text"), font(w, z), fg(w));
    let size = galley.size() / z;
    let (width, height) = match w.i64("rotation_step") {
        Some(1 | 3) => (size.y, size.x),
        _ => (size.x, size.y),
    };
    node.rect[2] = width.ceil() as f64;
    node.rect[3] = height.ceil() as f64;
}

fn text_update(painter: &egui::Painter, node: &Node, state: Option<&PvState>, rect: Rect, z: f32) {
    let w = &node.w;
    if w.bool("transparent") != Some(true) {
        fill(painter, rect, bg(w, "Read_Background"));
    }
    let text = match state {
        Some(s) if s.connected => format_value(w, s),
        _ => format!("<{}>", node.pv_name),
    };
    let wrap = w.bool("wrap_words").unwrap_or(true);
    text_turned(
        painter,
        rect.shrink(1.0),
        &text,
        font(w, z),
        fg(w),
        alignment(w, 1),
        wrap,
        w.i64("rotation_step").unwrap_or(0),
    );
}

fn led_color(w: &Widget, s: Option<&PvState>) -> Color32 {
    match s {
        Some(s) if s.connected => {
            let (key, default) = if is_on(w, s) {
                ("on_color", "On")
            } else {
                ("off_color", "Off")
            };
            w.color(key)
                .map(|c| color(&c))
                .unwrap_or_else(|| named(default))
        }
        _ => named("DISCONNECTED"),
    }
}

fn draw_led(
    painter: &egui::Painter,
    w: &Widget,
    rect: Rect,
    fill_color: Color32,
    text: &str,
    z: f32,
) {
    let line = w
        .color("line_color")
        .map(|c| color(&c))
        .unwrap_or(Color32::from_rgba_unmultiplied(50, 50, 50, 178));
    if w.bool("square") == Some(true) {
        fill(painter, rect, fill_color);
        outline(painter, rect, z.max(1.0), line);
    } else {
        let r = rect.width().min(rect.height()) / 2.0;
        painter.circle(
            rect.center(),
            r - 0.5,
            fill_color,
            Stroke::new(z.max(1.0), line),
        );
        // A little highlight so it reads as a lamp.
        painter.circle_filled(
            rect.center() - Vec2::splat(r * 0.3),
            r * 0.25,
            Color32::from_white_alpha(90),
        );
    }
    if !text.is_empty() {
        text_in(
            painter,
            rect,
            text,
            font(w, z),
            fg(w),
            Align2::CENTER_CENTER,
            false,
        );
    }
}

fn led(painter: &egui::Painter, w: &Widget, s: Option<&PvState>, rect: Rect, z: f32) {
    let text = match s {
        Some(s) if s.connected && is_on(w, s) => w.text("on_label"),
        Some(s) if s.connected => w.text("off_label"),
        _ => None,
    };
    draw_led(painter, w, rect, led_color(w, s), text.unwrap_or(""), z);
}

fn multi_state_led(painter: &egui::Painter, w: &Widget, s: Option<&PvState>, rect: Rect, z: f32) {
    let (c, text) = match s {
        Some(s) if s.connected => {
            let v = s.value.as_i64().unwrap_or(i64::MIN);
            let states = w
                .get("states")
                .map(|e| e.children.clone())
                .unwrap_or_default();
            let hit = states.iter().find(|st| {
                st.child_text("value")
                    .and_then(|t| t.trim().parse::<f64>().ok())
                    == Some(v as f64)
            });
            match hit {
                Some(st) => (
                    st.child("color")
                        .and_then(|c| c.child("color"))
                        .map(|c| color(&bob::Color::from_element(c)))
                        .unwrap_or(Color32::GRAY),
                    st.child_text("label").unwrap_or("").to_string(),
                ),
                None => (
                    w.color("fallback_color")
                        .map(|c| color(&c))
                        .unwrap_or_else(|| named("INVALID")),
                    w.text("fallback_label").unwrap_or("Err").to_string(),
                ),
            }
        }
        _ => (named("DISCONNECTED"), String::new()),
    };
    draw_led(painter, w, rect, c, &text, z);
}

fn byte_monitor(painter: &egui::Painter, w: &Widget, s: Option<&PvState>, rect: Rect, z: f32) {
    let n = w.i64("numBits").unwrap_or(8).clamp(1, 64) as usize;
    let start = w.i64("startBit").unwrap_or(0).clamp(0, 63);
    let reverse = w.bool("bitReverse").unwrap_or(false);
    let horizontal = w.bool("horizontal").unwrap_or(true);
    let on = w
        .color("on_color")
        .map(|c| color(&c))
        .unwrap_or(Color32::from_rgb(60, 255, 60));
    let off = w
        .color("off_color")
        .map(|c| color(&c))
        .unwrap_or(Color32::from_rgb(60, 100, 60));
    let value = s.filter(|s| s.connected).and_then(|s| s.value.as_i64());
    let step = if horizontal {
        rect.width() / n as f32
    } else {
        rect.height() / n as f32
    };
    for i in 0..n {
        // Phoebus draws the highest bit first unless bitReverse is set.
        let bit = if reverse {
            start + i as i64
        } else {
            start + (n - 1 - i) as i64
        };
        let cell = if horizontal {
            Rect::from_min_size(
                rect.min + Vec2::new(step * i as f32, 0.0),
                Vec2::new(step, rect.height()),
            )
        } else {
            Rect::from_min_size(
                rect.min + Vec2::new(0.0, step * i as f32),
                Vec2::new(rect.width(), step),
            )
        };
        let c = match value {
            Some(v) if (v >> bit) & 1 == 1 => on,
            Some(_) => off,
            None => named("DISCONNECTED"),
        };
        draw_led(painter, w, cell.shrink(0.5 * z), c, "", z);
    }
}

fn shape(painter: &egui::Painter, w: &Widget, rect: Rect, z: f32) {
    let transparent = w.bool("transparent").unwrap_or(false);
    let fill_c = if transparent {
        Color32::TRANSPARENT
    } else {
        w.color("background_color")
            .map(|c| color(&c))
            .unwrap_or(Color32::from_rgb(30, 144, 255))
    };
    let lw = w.f64("line_width").unwrap_or(3.0) as f32 * z;
    let line = w
        .color("line_color")
        .map(|c| color(&c))
        .unwrap_or(Color32::from_rgb(0, 0, 255));
    let stroke = if lw > 0.0 {
        Stroke::new(lw, line)
    } else {
        Stroke::NONE
    };
    if w.kind == "arc" {
        let start = w.f64("start_angle").unwrap_or(0.0) as f32;
        let total = w.f64("total_angle").unwrap_or(90.0) as f32;
        let inner = rect.shrink(lw / 2.0);
        let points = arc_points(inner, start, total);
        if !transparent && fill_c.a() > 0 {
            // A pie wedge, as a fan from the centre (it may be concave).
            let mut mesh = egui::Mesh::default();
            mesh.colored_vertex(inner.center(), fill_c);
            for p in &points {
                mesh.colored_vertex(*p, fill_c);
            }
            for i in 1..points.len() as u32 {
                mesh.add_triangle(0, i, i + 1);
            }
            painter.add(mesh);
        }
        if lw > 0.0 {
            let mut line = points;
            if !transparent {
                line.insert(0, inner.center());
                line.push(inner.center());
            }
            painter.add(Shape::line(line, stroke));
        }
    } else if w.kind == "rectangle" {
        let cr = w
            .f64("corner_width")
            .unwrap_or(0.0)
            .max(w.f64("corner_height").unwrap_or(0.0)) as f32
            * z
            / 2.0;
        let r = CornerRadius::same(cr.min(255.0) as u8);
        let style = w.i64("line_style").unwrap_or(0);
        if style == 0 || cr > 0.0 || lw <= 0.0 {
            painter.rect(rect, r, fill_c, stroke, StrokeKind::Inside);
        } else {
            painter.rect_filled(rect, r, fill_c);
            let inner = rect.shrink(lw / 2.0);
            let corners = [
                inner.left_top(),
                inner.right_top(),
                inner.right_bottom(),
                inner.left_bottom(),
                inner.left_top(),
            ];
            crate::plot::line(painter, corners.to_vec(), stroke, style);
        }
    } else {
        let inner = rect.shrink(lw / 2.0);
        painter.add(Shape::ellipse_filled(
            inner.center(),
            inner.size() / 2.0,
            fill_c,
        ));
        painter.add(Shape::ellipse_stroke(
            inner.center(),
            inner.size() / 2.0,
            stroke,
        ));
    }
}

/// Points along the arc of the ellipse in `rect` from `start` degrees over
/// `total` degrees, anticlockwise from three o'clock, as Phoebus draws arcs.
fn arc_points(rect: Rect, start: f32, total: f32) -> Vec<Pos2> {
    let n = ((total.abs() / 5.0).ceil() as usize).clamp(2, 73);
    let (c, r) = (rect.center(), rect.size() / 2.0);
    (0..=n)
        .map(|i| {
            let a = (start + total * i as f32 / n as f32).to_radians();
            Pos2::new(c.x + r.x * a.cos(), c.y - r.y * a.sin())
        })
        .collect()
}

fn poly(painter: &egui::Painter, w: &Widget, view: View, r: &[f64; 4]) {
    let display = w.points();
    let points: Vec<Pos2> = display
        .iter()
        .map(|(x, y)| view.pos(r[0] + x, r[1] + y))
        .collect();
    if points.len() < 2 {
        return;
    }
    let lw = w.f64("line_width").unwrap_or(3.0) as f32 * view.zoom;
    let line = w
        .color("line_color")
        .map(|c| color(&c))
        .unwrap_or(Color32::from_rgb(0, 0, 255));
    if w.kind == "polygon" {
        let fill_c = w
            .color("background_color")
            .map(|c| color(&c))
            .unwrap_or(Color32::from_rgb(50, 50, 255));
        crate::polygon::fill(painter, &display, points.clone(), fill_c);
        let mut closed = points;
        closed.push(closed[0]);
        painter.add(Shape::line(closed, Stroke::new(lw, line)));
    } else {
        let style = w.i64("line_style").unwrap_or(0);
        crate::plot::line(painter, points, Stroke::new(lw, line), style);
    }
}

fn bar(painter: &egui::Painter, w: &Widget, s: Option<&PvState>, rect: Rect, z: f32) {
    let (min, max) = s
        .filter(|_| w.bool("limits_from_pv").unwrap_or(true))
        .and_then(|s| s.range())
        .unwrap_or((
            w.f64("minimum").unwrap_or(0.0),
            w.f64("maximum").unwrap_or(100.0),
        ));
    let vertical = match w.kind.as_str() {
        "tank" | "thermometer" => true,
        "progressbar" => w.bool("horizontal") == Some(false),
        _ => false,
    };
    let fill_c = w
        .color("fill_color")
        .map(|c| color(&c))
        .unwrap_or(match w.kind.as_str() {
            "tank" => Color32::from_rgb(0, 0, 255),
            "thermometer" => Color32::from_rgb(255, 0, 0),
            _ => Color32::from_rgb(60, 255, 60),
        });
    fill(painter, rect, bg(w, "Read_Background"));
    outline(painter, rect, 1.0, Color32::from_gray(150));
    let Some(s) = s.filter(|s| s.connected) else {
        return;
    };
    let v = s.value.as_f64().unwrap_or(min);
    let frac = if max > min {
        ((v - min) / (max - min)).clamp(0.0, 1.0) as f32
    } else {
        0.0
    };
    let inner = rect.shrink(z.max(1.0));
    let filled = if vertical {
        Rect::from_min_max(
            Pos2::new(inner.left(), inner.bottom() - inner.height() * frac),
            inner.max,
        )
    } else {
        Rect::from_min_max(
            inner.min,
            Pos2::new(inner.left() + inner.width() * frac, inner.bottom()),
        )
    };
    fill(painter, filled, fill_c);
}

// --- controls ----------------------------------------------------------------

fn text_entry(
    ui: &mut Ui,
    node: &mut Node,
    state: Option<&PvState>,
    rect: Rect,
    cx: &mut Cx,
    id: Id,
) -> Response {
    let w = node.w.clone();
    let shown = match state {
        Some(s) if s.connected => format_value(&w, s),
        _ => format!("<{}>", node.pv_name),
    };
    let align = alignment(&w, 1);
    let enabled = can_write(&w, state);
    let mut buf = node.edit.clone().unwrap_or(shown);
    // Text that does not fit is elided with `…` like a text update's, except
    // while it is being edited, when every character must be there for the
    // cursor. (A click that focuses it lands in the elided layout, which
    // matches the full text up to the `…`.)
    let editing = ui.memory(|m| m.has_focus(id));
    let fnt = font(&w, cx.view.zoom);
    let color = fg(&w);
    let margin = Vec2::new(2.0, 0.0);
    let text_width = rect.width() - 2.0 * margin.x;
    let mut elided = |ui: &Ui, text: &dyn egui::TextBuffer, _wrap_width: f32| {
        let job = entry_job(
            text.as_str(),
            fnt.clone(),
            color,
            align.x(),
            text_width,
            editing,
        );
        ui.fonts_mut(|f| f.layout_job(job))
    };
    let edit = egui::TextEdit::singleline(&mut buf)
        .layouter(&mut elided)
        .id(id)
        .font(font(&w, cx.view.zoom))
        .text_color(fg(&w))
        .background_color(bg(&w, "Write_Background"))
        .horizontal_align(align.x())
        .vertical_align(egui::Align::Center)
        .margin(margin)
        .interactive(enabled)
        .min_size(rect.size());
    let resp = put_clipped(ui, id, rect, cx.clip, edit);
    if resp.has_focus() {
        node.edit = Some(buf);
    } else if resp.lost_focus() {
        if ui.input(|i| i.key_pressed(egui::Key::Enter)) {
            // Phoebus keeps the spaces of text written as a string.
            if writes_long_string(&node.w) {
                write_with(node, |pv| pv.write_long_string(buf));
            } else {
                // The edit starts from the text shown, units and all.
                write(node, crate::format::entry_value(&w, state, &buf));
            }
        }
        node.edit = None;
    }
    resp
}

/// The layout of a text entry's text: one row, as egui's own single-line
/// layout, but elided with `…` to `width` unless it is being edited.
fn entry_job(
    text: &str,
    font: FontId,
    c: Color32,
    halign: egui::Align,
    width: f32,
    editing: bool,
) -> egui::text::LayoutJob {
    let mut job = egui::text::LayoutJob::simple_singleline(text.to_string(), font, c);
    job.halign = halign;
    job.keep_trailing_whitespace = true;
    if !editing {
        job.wrap = elide_wrap(width);
    }
    job
}

/// Put a widget at `rect`, clipped. The scope starts at `rect`, not at the
/// cursor: one there would take in the space after the last widget each
/// time, and grow the display's scrolled size below its bottom. Its ids
/// derive from `id`, so a widget keeps its edit state (a drag, typing)
/// however many widgets came before it.
fn put_clipped(ui: &mut Ui, id: Id, rect: Rect, clip: Rect, widget: impl egui::Widget) -> Response {
    ui.scope_builder(
        egui::UiBuilder::new().max_rect(rect).id(id.with("clipped")),
        |ui| {
            ui.set_clip_rect(clip.intersect(ui.clip_rect()));
            ui.put(rect, widget)
        },
    )
    .inner
}

fn action_button(ui: &mut Ui, node: &mut Node, rect: Rect, cx: &mut Cx, id: Id) -> Response {
    let w = &node.w;
    let actions = w.actions();
    let resp = ui.interact(rect.intersect(cx.clip), id, Sense::click());
    // A transparent button draws only its text, over what lies beneath.
    if w.bool("transparent") != Some(true) {
        button_box(
            ui.painter(),
            rect,
            bg(w, "Button_Background"),
            resp.is_pointer_button_down_on(),
            cx.view.zoom,
        );
    }
    let text = button_text(node, &actions);
    let painter = ui.painter().with_clip_rect(cx.clip);
    text_in(
        &painter,
        rect,
        &text,
        font(w, cx.view.zoom),
        fg(w),
        Align2::CENTER_CENTER,
        true,
    );

    // A disabled button, or one whose PV is read-only, runs no actions.
    let allowed = !is_disabled(&node.w, node.state().as_ref());
    if allowed && actions.len() == 1 && resp.clicked() {
        run_action(node, &actions[0], cx);
    } else if allowed && actions.len() > 1 {
        egui::Popup::menu(&resp).show(|ui| {
            for a in &actions {
                if ui.button(node.expand(a.label())).clicked() {
                    run_action(node, a, cx);
                }
            }
        });
    }
    resp
}

/// An action button's label, as Phoebus' `makeButtonText`: text that is
/// exactly `$(actions)` (the default when there is none) names the one
/// action, or says how many there are; other text, empty too, is shown.
fn button_text(node: &Node, actions: &[Action]) -> String {
    if node.w.text("text").is_some_and(|t| t != "$(actions)") {
        return node.prop("text");
    }
    let as_one = node.w.get("actions").and_then(|a| a.attr("execute_as_one")) == Some("true");
    match actions {
        [] => "EMPTY".into(),
        [one] => node.expand(one.label()),
        _ if as_one => format!("{} actions", actions.len()),
        _ => format!("Choose 1 of {}", actions.len()),
    }
}

pub(crate) fn run_action(node: &Node, action: &Action, cx: &mut Cx) {
    match action {
        Action::OpenDisplay {
            file,
            macros,
            target,
            ..
        } => {
            // The file name may use the action's own macros, as in
            // `<file>$(IOC)/$(P).bob</file>` with IOC and P set by the action.
            let expanded =
                Macros::from_pairs(macros.iter().map(|(k, v)| (k.to_string(), node.expand(v))));
            let macros = node.macros.with(&expanded);
            let file = node.expand_with(&macros, file);
            cx.requests.push(Request::Open {
                source: node.resolve(&file),
                macros,
                target: target.clone(),
            });
        }
        Action::WritePv { pv_name, value, .. } => {
            let pv = action_pv(node, cx.pvs, &node.expand(pv_name));
            // As in Phoebus, written at once, wherever the display came
            // from; in read-only mode the PV refuses, and says so.
            pv.write(node.expand(value));
        }
        Action::OpenWebpage { url, .. } => {
            let url = node.expand(url);
            if webpage_allowed(node.base.as_ref(), &url) {
                cx.requests.push(Request::Webpage(url));
            } else {
                log::warn!("not opening '{url}': a display from the web may only open web pages");
            }
        }
        Action::Execute {
            text, description, ..
        } => {
            if let Some(names) = bob::databrowser_pvs(text) {
                trend(node, &names, cx);
            } else if is_toggle_script(text) {
                if let Some(s) = node.state() {
                    // As `(PVUtil.getDouble(pv) == 0) ? 1 : 0`: a value
                    // that is not a number is NaN there, so it writes 0.
                    let zero = s.value.as_f64() == Some(0.0);
                    write(node, if zero { "1" } else { "0" });
                }
            } else {
                log::warn!("script '{description}' is not supported: bobview runs no scripts");
            }
        }
        Action::Command {
            command, directory, ..
        } => {
            let command = node.expand(command);
            // A StripTool launcher is shown as a trend, and not run.
            if let Some(names) = bob::strip_tool_pvs(&command) {
                cx.requests.push(Request::Trend(names));
                return;
            }
            let directory = node.expand(directory);
            let directory = (!directory.trim().is_empty()).then(|| node.resolve(&directory));
            // Only DLS launcher scripts of local displays, which open a
            // display; run on a thread, as a script may take seconds.
            cx.requests.push(Request::Command {
                base: node.base.clone(),
                directory,
                command,
            });
        }
        Action::CloseDisplay { .. } => cx.requests.push(Request::Close),
        Action::Trend { pv_names, .. } => trend(node, pv_names, cx),
        Action::Unsupported { kind, .. } => log::warn!("action '{kind}' is not supported"),
    }
}

/// Open a trend of PVs named by a widget's action, macros expanded.
fn trend(node: &Node, names: &[String], cx: &mut Cx) {
    let names = names.iter().map(|n| node.expand(n)).collect();
    cx.requests.push(Request::Trend(names));
}

/// Whether `script` is the toggle that techui-builder embeds in synoptic
/// symbols, which bobview runs natively since it runs no scripts:
///
/// ```js
/// pv = ScriptUtil.getPrimaryPV(widget)
/// var pv_value = PVUtil.getDouble(pv);
/// pv_value = (pv_value == 0) ? 1 : 0;
/// pv.setValue(pv_value);
/// ```
fn is_toggle_script(script: &str) -> bool {
    let code: String = script
        .lines()
        .filter(|l| !l.trim_start().starts_with("importClass") && !l.trim_start().starts_with("/*"))
        .flat_map(|l| l.chars())
        .filter(|c| !c.is_whitespace() && *c != ';')
        .collect();
    code == "pv=ScriptUtil.getPrimaryPV(widget)varpv_value=PVUtil.getDouble(pv)\
             pv_value=(pv_value==0)?1:0pv.setValue(pv_value)"
}

/// Whether a display at `base` may hand `url` to `xdg-open`. One fetched
/// over HTTP is not trusted with local files or other URL schemes (which
/// could open or run something on this machine), only with web pages; and
/// nothing may pass `xdg-open` an option.
fn webpage_allowed(base: Option<&Source>, url: &str) -> bool {
    let url = url.trim();
    if url.is_empty() || url.starts_with('-') {
        return false;
    }
    !base.is_some_and(Source::is_url) || Source::parse(url).is_url()
}

fn bool_button(
    ui: &mut Ui,
    node: &mut Node,
    s: Option<&PvState>,
    rect: Rect,
    cx: &mut Cx,
    id: Id,
) -> Response {
    let w = node.w.clone();
    let z = cx.view.zoom;
    let on = s.is_some_and(|s| s.connected && is_on(&w, s));
    let resp = ui.interact(rect.intersect(cx.clip), id, Sense::click());
    button_box(
        ui.painter(),
        rect,
        bg(&w, "Button_Background"),
        resp.is_pointer_button_down_on() || on,
        z,
    );
    let painter = ui.painter().with_clip_rect(cx.clip);
    let label = if on {
        w.text("on_label").unwrap_or("On")
    } else {
        w.text("off_label").unwrap_or("Off")
    };
    let mut text_rect = rect;
    if w.bool("show_led").unwrap_or(true) {
        let d = (rect.height() * 0.5).min(rect.width() * 0.3);
        let led_rect =
            Rect::from_center_size(Pos2::new(rect.left() + d, rect.center().y), Vec2::splat(d));
        draw_led(&painter, &w, led_rect, led_color(&w, s), "", z);
        text_rect.min.x += d * 1.6;
    }
    text_in(
        &painter,
        text_rect,
        label,
        font(&w, z),
        fg(&w),
        Align2::CENTER_CENTER,
        false,
    );
    // Mode 0 toggles; 1 and 2 push (2 inverted): the pressed value is written
    // when the button goes down and the released one when it comes up.
    let bit = bit(&w);
    match w.i64("mode").unwrap_or(0) {
        0 if resp.clicked() => write(node, toggled(&w, s).to_string()),
        m @ (1 | 2) => {
            let v = s.and_then(|s| s.value.as_i64()).unwrap_or(0);
            let value = |pressed: bool| with_bit(bit, v, pressed == (m == 1)).to_string();
            // Only the primary button pushes; a right-click opens the PV menu.
            let down = resp.is_pointer_button_down_on() && ui.input(|i| i.pointer.primary_down());
            let was_down = ui.data(|d| d.get_temp::<bool>(id).unwrap_or(false));
            if down != was_down {
                ui.data_mut(|d| d.insert_temp(id, down));
            }
            match push_edge(was_down, down, resp.clicked()) {
                Push::Press => write(node, value(true)),
                Push::Release => write(node, value(false)),
                Push::Both => {
                    write(node, value(true));
                    write(node, value(false));
                }
                Push::None => {}
            }
        }
        _ => {}
    }
    resp
}

#[derive(Debug, PartialEq)]
enum Push {
    None,
    Press,
    Release,
    /// Pressed and released within one frame.
    Both,
}

/// What a push button writes, from whether it was down last frame, is down
/// now, and was clicked this frame.
fn push_edge(was_down: bool, down: bool, clicked: bool) -> Push {
    match (was_down, down) {
        (false, true) => Push::Press,
        (true, false) => Push::Release,
        (false, false) if clicked => Push::Both,
        _ => Push::None,
    }
}

/// The choices for a choice/radio/combo widget and the index of the current one.
fn choices(w: &Widget, s: Option<&PvState>) -> (Vec<String>, Option<usize>) {
    let from_pv = w.bool("items_from_pv").unwrap_or(true);
    let items = match s {
        Some(s) if from_pv && !s.labels.is_empty() => s.labels.clone(),
        _ => w.items(),
    };
    let current = s.filter(|s| s.connected).and_then(|s| match &s.value {
        Value::Enum(i) => Some(*i as usize),
        Value::Str(t) => items.iter().position(|i| i == t),
        v => v.as_i64().map(|i| i as usize),
    });
    (items, current)
}

fn choice(
    ui: &mut Ui,
    node: &mut Node,
    s: Option<&PvState>,
    rect: Rect,
    cx: &mut Cx,
    id: Id,
) -> Response {
    let w = node.w.clone();
    let z = cx.view.zoom;
    let (items, current) = choices(&w, s);
    let horizontal = w.bool("horizontal").unwrap_or(true);
    let n = items.len().max(1);
    // Click sensing (here and merged from each choice below) lets the PV
    // context menu open from anywhere on the widget.
    let mut resp = ui.interact(rect.intersect(cx.clip), id, Sense::click());
    let painter = ui.painter().with_clip_rect(cx.clip);
    let selected = w
        .color("selected_color")
        .map(|c| color(&c))
        .unwrap_or(Color32::from_gray(200));
    let (dx, dy) = if horizontal {
        (rect.width() / n as f32, 0.0)
    } else {
        (0.0, rect.height() / n as f32)
    };
    let size = if horizontal {
        Vec2::new(dx, rect.height())
    } else {
        Vec2::new(rect.width(), dy)
    };
    for (i, item) in items.iter().enumerate() {
        let cell =
            Rect::from_min_size(rect.min + Vec2::new(dx * i as f32, dy * i as f32), size).shrink(z);
        let r = ui.interact(cell.intersect(cx.clip), id.with(i), Sense::click());
        let is_sel = current == Some(i);
        if w.kind == "radio" {
            let d = cell.height().min(14.0 * z);
            let c = Pos2::new(cell.left() + d / 2.0 + 2.0, cell.center().y);
            painter.circle(
                c,
                d / 2.0,
                Color32::WHITE,
                Stroke::new(1.0, Color32::DARK_GRAY),
            );
            if is_sel {
                painter.circle_filled(c, d / 4.0, Color32::BLACK);
            }
            let tr = Rect::from_min_max(Pos2::new(cell.left() + d + 6.0, cell.top()), cell.max);
            text_in(
                &painter,
                tr,
                item,
                font(&w, z),
                fg(&w),
                Align2::LEFT_CENTER,
                false,
            );
        } else {
            let fill_c = if is_sel {
                selected
            } else {
                bg(&w, "Button_Background")
            };
            button_box(
                &painter,
                cell,
                fill_c,
                is_sel || r.is_pointer_button_down_on(),
                z,
            );
            text_in(
                &painter,
                cell,
                item,
                font(&w, z),
                fg(&w),
                Align2::CENTER_CENTER,
                false,
            );
        }
        if r.clicked() {
            write(node, item.clone());
        }
        resp = resp.union(r);
    }
    resp
}

fn combo(
    ui: &mut Ui,
    node: &mut Node,
    s: Option<&PvState>,
    rect: Rect,
    cx: &mut Cx,
    id: Id,
) -> Response {
    let w = node.w.clone();
    let (items, current) = choices(&w, s);
    let mux = w.menu_mux();
    // Like Phoebus, show the PV's value whatever the items are; the items
    // only fill the dropdown. A menu mux shows the item the value is for.
    let text = match s {
        Some(s) if s.connected => mux
            .as_ref()
            .and_then(|m| m.current(current, &s.format(None, false)))
            .and_then(|i| items.get(i).cloned())
            .unwrap_or_else(|| s.format(None, false)),
        _ => format!("<{}>", node.pv_name),
    };
    let resp = ui.interact(rect.intersect(cx.clip), id, Sense::click());
    let z = cx.view.zoom;
    let painter = ui.painter().with_clip_rect(cx.clip);
    button_box(&painter, rect, bg(&w, "Button_Background"), false, z);
    let arrow = rect.height().min(rect.width() / 4.0);
    let text_rect = Rect::from_min_max(
        rect.min + Vec2::new(4.0, 0.0),
        Pos2::new(rect.right() - arrow, rect.bottom()),
    );
    text_in(
        &painter,
        text_rect,
        &text,
        font(&w, z),
        fg(&w),
        Align2::LEFT_CENTER,
        false,
    );
    let c = Pos2::new(rect.right() - arrow / 2.0, rect.center().y);
    let a = arrow * 0.2;
    painter.add(Shape::convex_polygon(
        vec![
            c + Vec2::new(-a, -a / 2.0),
            c + Vec2::new(a, -a / 2.0),
            c + Vec2::new(0.0, a),
        ],
        fg(&w),
        Stroke::NONE,
    ));
    if !can_write(&w, s) {
        return resp;
    }
    // Like Phoebus, the list is at least as wide as the combo and in its
    // font (but never smaller than the rest of the UI, so it stays readable
    // when the display is zoomed out); a finger-sized row in touch mode.
    let item_font = combo_item_font(
        font(&w, z),
        &ui.style().text_styles[&egui::TextStyle::Button],
    );
    let row_h = if cx.touch { TOUCH_TARGET } else { 0.0 };
    egui::Popup::menu(&resp).width(rect.width()).show(|ui| {
        ui.set_min_width(rect.width() - ui.spacing().menu_margin.sum().x);
        for (i, item) in items.iter().enumerate() {
            let label = egui::RichText::new(item).font(item_font.clone());
            let button = egui::Button::selectable(*item == text, label)
                .min_size(Vec2::new(ui.available_width(), row_h));
            if ui.add(button).clicked() {
                match &mux {
                    Some(mux) => mux_pick(node, cx.pvs, mux, i),
                    None => write(node, item.clone()),
                }
            }
        }
    });
    resp
}

/// Pick item `i` of a menu mux: write its value to each target, and its
/// index to the mux's own PV.
fn mux_pick(node: &Node, pvs: &pv::PvManager, mux: &bob::MenuMux, i: usize) {
    for (target, value) in mux.writes(i) {
        action_pv(node, pvs, &node.expand(target)).write(node.expand(value));
    }
    if mux.index {
        write(node, i.to_string());
    }
}

/// The PV `name` that an action of `node` writes: the one connected in
/// advance, or else a new one.
fn action_pv(node: &Node, pvs: &pv::PvManager, name: &str) -> pv::Pv {
    let parsed = pv::PvName::parse(name).to_string();
    match node
        .action_pvs
        .iter()
        .find(|p| p.name().to_string() == parsed)
    {
        Some(pv) => pv.clone(),
        None => pvs.subscribe(name),
    }
}

/// The font for a combo's dropdown rows: the widget's, unless that is
/// smaller than the UI's own button font.
fn combo_item_font(widget: FontId, ui: &FontId) -> FontId {
    if widget.size < ui.size {
        FontId::new(ui.size, widget.family)
    } else {
        widget
    }
}

/// Phoebus labels a checkbox or slide button "Label" unless the file says
/// otherwise; an explicit empty `<label>` stays empty.
fn raw_label(w: &Widget) -> &str {
    w.text("label").unwrap_or("Label")
}

fn checkbox(
    ui: &mut Ui,
    node: &mut Node,
    s: Option<&PvState>,
    rect: Rect,
    cx: &mut Cx,
    id: Id,
) -> Response {
    let w = node.w.clone();
    let z = cx.view.zoom;
    let on = s.is_some_and(|s| s.connected && is_on(&w, s));
    let resp = ui.interact(rect.intersect(cx.clip), id, Sense::click());
    let painter = ui.painter().with_clip_rect(cx.clip);
    let d = (14.0 * z).min(rect.height());
    let b = Rect::from_min_size(
        Pos2::new(rect.left() + 1.0, rect.center().y - d / 2.0),
        Vec2::splat(d),
    );
    painter.rect(
        b,
        CornerRadius::same(2),
        Color32::WHITE,
        Stroke::new(1.0, Color32::DARK_GRAY),
        StrokeKind::Inside,
    );
    if on {
        painter.line(
            vec![
                b.left_center() + Vec2::new(d * 0.2, 0.0),
                b.center_bottom() - Vec2::new(0.0, d * 0.2),
                b.right_top() + Vec2::new(-d * 0.2, d * 0.2),
            ],
            Stroke::new(2.0 * z, Color32::BLACK),
        );
    }
    let tr = Rect::from_min_max(Pos2::new(b.right() + 4.0, rect.top()), rect.max);
    text_in(
        &painter,
        tr,
        &node.expand(raw_label(&w)),
        font(&w, z),
        fg(&w),
        Align2::LEFT_CENTER,
        false,
    );
    if resp.clicked() {
        write(node, toggled(&w, s).to_string());
    }
    resp
}

/// Phoebus SlideButton (a JavaFX ToggleSwitch): a pill-shaped track in
/// off_color/on_color with a round thumb that slides to the right when the
/// selected bit is set, then the label. A click toggles the bit like a checkbox.
fn slide_button(
    ui: &mut Ui,
    node: &mut Node,
    s: Option<&PvState>,
    rect: Rect,
    cx: &mut Cx,
    id: Id,
) -> Response {
    let w = node.w.clone();
    let z = cx.view.zoom;
    let on = s.is_some_and(|s| s.connected && is_on(&w, s));
    let resp = ui.interact(rect.intersect(cx.clip), id, Sense::click());
    let painter = ui.painter().with_clip_rect(cx.clip);
    let label = node.expand(raw_label(&w));
    let (track, label_rect) = slide_layout(rect, z, !label.is_empty());
    let track_color = if on {
        w.color("on_color")
            .map(|c| color(&c))
            .unwrap_or_else(|| named("OK"))
    } else {
        w.color("off_color")
            .map(|c| color(&c))
            .unwrap_or_else(|| named("Button_Background"))
    };
    let r = CornerRadius::same((track.height() / 2.0).min(255.0) as u8);
    painter.rect(
        track,
        r,
        track_color,
        Stroke::new(1.0, Color32::from_gray(150)),
        StrokeKind::Inside,
    );
    let d = track.height() - 2.0 * (z.max(1.0));
    let cx_thumb = if on {
        track.right() - track.height() / 2.0
    } else {
        track.left() + track.height() / 2.0
    };
    let thumb = Pos2::new(cx_thumb, track.center().y);
    painter.circle(
        thumb,
        d / 2.0,
        Color32::WHITE,
        Stroke::new(1.0, Color32::from_gray(140)),
    );
    if is_disabled(&w, s) {
        painter.rect_filled(track, r, Color32::from_white_alpha(110));
    }
    if let Some(lr) = label_rect {
        text_in(
            &painter,
            lr,
            &label,
            font(&w, z),
            fg(&w),
            Align2::LEFT_CENTER,
            false,
        );
    }
    if resp.clicked() {
        write(node, toggled(&w, s).to_string());
    }
    resp
}

/// Track rect and optional label rect of a slide button. The track is a
/// pill 1.8 times as wide as it is tall, at most 16px tall at zoom 1,
/// fitted into the widget at its left, with the label to its right, as
/// Phoebus lays out its toggle switch and label.
fn slide_layout(rect: Rect, z: f32, has_label: bool) -> (Rect, Option<Rect>) {
    let h = (16.0 * z)
        .min(rect.height() - 2.0)
        .min((rect.width() - 2.0) / 1.8)
        .max(2.0);
    let tw = h * 1.8;
    let track = Rect::from_min_size(
        Pos2::new(rect.left() + 1.0, rect.center().y - h / 2.0),
        Vec2::new(tw, h),
    );
    let label = has_label
        .then(|| Rect::from_min_max(Pos2::new(track.right() + 6.0 * z, rect.top()), rect.max));
    (track, label)
}

/// Phoebus Spinner: the value, dragged or typed, kept within the range and
/// stepped by `increment`.
fn spinner(
    ui: &mut Ui,
    node: &mut Node,
    s: Option<&PvState>,
    rect: Rect,
    cx: &mut Cx,
    id: Id,
) -> Response {
    let w = node.w.clone();
    let (min, max) = crate::meter::limits(&w, s);
    let (min, max) = (min.min(max), max.max(min));
    let mut v = s.and_then(|s| s.value.as_f64()).unwrap_or(min);
    let step = w.f64("increment").unwrap_or(1.0);
    let drag = egui::DragValue::new(&mut v).range(min..=max).speed(step);
    let resp = put_clipped(ui, id, rect, cx.clip.intersect(rect), drag);
    if resp.drag_stopped() || (resp.changed() && !resp.dragged()) {
        write(node, v.to_string());
    }
    resp
}

/// Where a slider's parts go, along its length and across it.
#[derive(Debug)]
struct SliderLayout {
    /// The band the thumb slides along.
    band: Rect,
    /// The thumb's length along the slider.
    thumb: f32,
    /// The thumb's centre at the minimum and at the maximum.
    from: f32,
    to: f32,
    /// The band on the far side of the thumb's for the scale's ticks, and
    /// whether its labels fit in it too.
    scale: Option<Rect>,
    labels: bool,
    /// The band on the near side for the alarm level markers.
    markers: Option<Rect>,
}

/// Lay out a slider in `rect`: the thumb's band, the scale on its far side
/// (below a horizontal slider, right of a vertical one) and the level
/// markers on its near side, all centred across it, as a JavaFX Slider
/// centres its track and tick marks. `scale` is the room the scale's
/// labels take across the slider and half of their length along it, and
/// `markers` the room the markers' labels take across it; `None` leaves
/// either out. What the box has no room for is left out, the markers
/// first, then the scale's labels and then its ticks, before the thumb
/// gets thin.
fn slider_layout(
    rect: Rect,
    horizontal: bool,
    z: f32,
    scale: Option<(f32, f32)>,
    markers: Option<f32>,
) -> SliderLayout {
    let (along, across) = if horizontal {
        (rect.x_range(), rect.y_range())
    } else {
        (rect.y_range(), rect.x_range())
    };
    let pad = 2.0 * z;
    let tick = 5.0 * z;
    let room = (across.span() - 2.0 * pad).max(1.0);
    let (full, thin) = (16.0 * z, 8.0 * z);
    let ticks = scale.is_some();
    let label = scale.map_or(0.0, |(l, _)| l);
    let (mark, depth, labels) = [
        (markers.map(|m| m + tick), ticks, ticks),
        (None, ticks, ticks),
        (None, ticks, false),
        (None, false, false),
    ]
    .into_iter()
    .map(|(m, t, l)| {
        let depth = if t { tick } else { 0.0 } + if l { label } else { 0.0 };
        (m.unwrap_or(0.0), depth, l)
    })
    .find(|(m, depth, _)| room - m - depth >= thin)
    .unwrap_or((0.0, 0.0, false));
    let thick = (room - mark - depth).clamp(1.0, full);
    let start = across.center() - (mark + thick + depth) / 2.0;
    let thumb = (thick * 0.6).max(1.0);
    let half_label = scale.filter(|_| labels).map_or(0.0, |(_, half)| half);
    let end = (thumb / 2.0).max(half_label) + pad;
    let (lo, hi) = if along.span() > 2.0 * end {
        (along.min + end, along.max - end)
    } else {
        (along.center(), along.center())
    };
    let band = (start + mark, start + mark + thick);
    let part = |(a, b): (f32, f32)| {
        if horizontal {
            Rect::from_x_y_ranges(along, a..=b)
        } else {
            Rect::from_x_y_ranges(a..=b, along)
        }
    };
    // A vertical slider has its minimum at the bottom.
    let (from, to) = if horizontal { (lo, hi) } else { (hi, lo) };
    SliderLayout {
        band: part(band),
        thumb,
        from,
        to,
        scale: (depth > 0.0).then(|| part((band.1, band.1 + depth))),
        labels,
        markers: (mark > 0.0).then(|| part((start, band.0))),
    }
}

/// The range a slider runs over, from its `minimum` and `maximum` or its
/// PV's: as Phoebus, 0 to 100 for one that is empty or inverted. A BOY
/// slider (the bobview-only `<boy_range>` from the `.opi` import) keeps
/// an inverted range, running from the maximum, and widens an empty one
/// to the minimum plus one, as BOY's scale did.
fn slider_range(w: &Widget, (min, max): (f64, f64)) -> (f64, f64) {
    if w.bool("boy_range") == Some(true) {
        return if min == max {
            (min, min + 1.0)
        } else {
            (min, max)
        };
    }
    if min < max { (min, max) } else { (0.0, 100.0) }
}

/// A scaled slider's alarm level markers, as Phoebus': each of LOLO, LOW,
/// HIGH and HIHI there is a level for, unless its `show_*` is false. The
/// levels are the PV's with `limits_from_pv` while it is connected, else
/// the widget's `level_*` (10, 20, 80 and 90 unless given). Room is kept
/// for them all; those outside the range are not drawn.
fn slider_markers(w: &Widget, s: Option<&PvState>) -> Vec<(f64, &'static str)> {
    let from_pv = w.bool("limits_from_pv").unwrap_or(true) && s.is_some_and(|s| s.connected);
    let mut levels = if from_pv {
        crate::meter::levels(w, s)
    } else {
        ["level_lolo", "level_low", "level_high", "level_hihi"]
            .map(|k| w.f64(k).filter(|v| v.is_finite()))
    };
    if !from_pv {
        for (level, default) in levels.iter_mut().zip([10.0, 20.0, 80.0, 90.0]) {
            level.get_or_insert(default);
        }
    }
    levels
        .into_iter()
        .zip([
            ("LOLO", "show_lolo"),
            ("LOW", "show_low"),
            ("HIGH", "show_high"),
            ("HIHI", "show_hihi"),
        ])
        .filter(|(_, (_, show))| w.bool(show).unwrap_or(true))
        .filter_map(|(level, (name, _))| Some((level?, name)))
        .collect()
}

/// Phoebus ScaledSlider and ScrollBar: a groove along the widget with a
/// thumb at the value, and for a scaled slider with `show_scale` labelled
/// ticks on one side and its alarm level markers on the other, as
/// Phoebus' JavaFX Slider; no value box. A click or drag moves the thumb,
/// and its value is written when the button is let go, over the range
/// [`slider_range`] gives.
fn slider(
    ui: &mut Ui,
    node: &mut Node,
    s: Option<&PvState>,
    rect: Rect,
    cx: &mut Cx,
    id: Id,
) -> Response {
    if node.w.kind == "spinner" {
        return spinner(ui, node, s, rect, cx, id);
    }
    let w = node.w.clone();
    let z = cx.view.zoom;
    let (min, max) = slider_range(&w, crate::meter::limits(&w, s));
    // Only a range too large to widen is still empty.
    let live = min != max;
    let horizontal = w.bool("horizontal").unwrap_or(true);
    let show_scale = w.kind == "scaledslider" && w.bool("show_scale").unwrap_or(true) && live;
    let painter = ui.painter().with_clip_rect(cx.clip.intersect(rect));
    if w.kind == "scaledslider" && w.bool("transparent") == Some(false) {
        fill(&painter, rect, bg(&w, "Background"));
    }
    let fnt = font(&w, z);
    let text = fg(&w);
    let size_of = |s: &str| {
        painter
            .layout_no_wrap(s.to_string(), fnt.clone(), text)
            .size()
    };
    let guess = (max - min).abs() / 10.0;
    let size = |v: f64| size_of(&crate::axis::label(v, guess));
    let (lo_size, hi_size) = (size(min), size(max));
    let widest = lo_size.x.max(hi_size.x);
    let line_h = lo_size.y;
    let room = if horizontal {
        (line_h, widest / 2.0)
    } else {
        (widest + 2.0 * z, line_h / 2.0)
    };
    let markers = if w.kind == "scaledslider" {
        slider_markers(&w, s)
    } else {
        Vec::new()
    };
    let mark_room = (!markers.is_empty() && live).then(|| {
        if horizontal {
            line_h
        } else {
            size_of("HIHI").x + 2.0 * z
        }
    });
    let lay = slider_layout(rect, horizontal, z, show_scale.then_some(room), mark_room);
    let scale = crate::axis::Scale {
        lo: min,
        hi: max,
        log: false,
        from: lay.from,
        to: lay.to,
    };

    // Follow the pointer while it is down on an enabled slider, and write
    // where it was let go.
    let resp = ui.interact(rect.intersect(cx.clip), id, Sense::click_and_drag());
    let mut v = s
        .and_then(|s| s.value.as_f64())
        .filter(|v| v.is_finite())
        .unwrap_or(min);
    let enabled = live && !is_disabled(&w, s);
    if enabled && let Some(p) = resp.interact_pointer_pos() {
        let at = if horizontal { p.x } else { p.y };
        let t = if lay.to != lay.from {
            ((at - lay.from) / (lay.to - lay.from)).clamp(0.0, 1.0) as f64
        } else {
            0.0
        };
        v = min + (max - min) * t;
        ui.data_mut(|d| d.insert_temp(id, v));
    }
    if (resp.drag_stopped() || resp.clicked())
        && let Some(v) = ui.data_mut(|d| d.remove_temp::<f64>(id))
    {
        write(node, v.to_string());
    }

    // The groove, then the scale, then the thumb over both.
    let band = lay.band;
    let groove_w = (band.height().min(band.width()) / 4.0).max(2.0 * z);
    let groove = if horizontal {
        Rect::from_center_size(
            Pos2::new((lay.from + lay.to) / 2.0, band.center().y),
            Vec2::new((lay.to - lay.from).abs() + lay.thumb, groove_w),
        )
    } else {
        Rect::from_center_size(
            Pos2::new(band.center().x, (lay.from + lay.to) / 2.0),
            Vec2::new(groove_w, (lay.to - lay.from).abs() + lay.thumb),
        )
    };
    painter.rect(
        groove,
        CornerRadius::same((groove_w / 2.0).min(255.0) as u8),
        Color32::from_gray(225),
        Stroke::new(1.0, Color32::from_gray(150)),
        StrokeKind::Inside,
    );
    // A tick at `p` along the slider, out from `edge` across it (`out` is
    // 1 to the far side, -1 to the near one), with its label beyond.
    let stroke = Stroke::new(1.0, text);
    let tick_at = |edge: f32, p: f32, out: f32, label: Option<String>| {
        let len = 5.0 * z * out;
        let (a, b) = if horizontal {
            (Pos2::new(p, edge), Pos2::new(p, edge + len))
        } else {
            (Pos2::new(edge, p), Pos2::new(edge + len, p))
        };
        painter.line_segment([a, b], stroke);
        if let Some(label) = label {
            let (align, gap) = match (horizontal, out > 0.0) {
                (true, true) => (Align2::CENTER_TOP, 0.0),
                (true, false) => (Align2::CENTER_BOTTOM, 0.0),
                (false, true) => (Align2::LEFT_CENTER, 2.0 * z),
                (false, false) => (Align2::RIGHT_CENTER, -2.0 * z),
            };
            painter.text(b + Vec2::new(gap, 0.0), align, label, fnt.clone(), text);
        }
    };
    if let Some(area) = lay.scale {
        // Major ticks at least `major_tick_step_hint` apart, and far
        // enough apart for their labels.
        let hint = w.f64("major_tick_step_hint").unwrap_or(40.0) as f32 * z;
        let label_len = if horizontal {
            widest + 8.0 * z
        } else {
            line_h * 1.5
        };
        let gaps = ((lay.to - lay.from).abs() / hint.max(label_len)).floor();
        let (ticks, step) = crate::axis::ticks(min, max, gaps as usize + 1);
        let edge = if horizontal { area.top() } else { area.left() };
        for t in ticks {
            let label = lay.labels.then(|| crate::axis::label(t, step));
            tick_at(edge, scale.pos(t), 1.0, label);
        }
    }
    if let Some(area) = lay.markers {
        let edge = if horizontal {
            area.bottom()
        } else {
            area.right()
        };
        let range = min.min(max)..=max.max(min);
        for (level, name) in markers.iter().filter(|(l, _)| range.contains(l)) {
            tick_at(edge, scale.pos(*level), -1.0, Some(name.to_string()));
        }
    }
    let at = if live {
        scale.pos(v.clamp(min.min(max), max.max(min)))
    } else {
        lay.from
    };
    let thumb = if horizontal {
        Rect::from_center_size(
            Pos2::new(at, band.center().y),
            Vec2::new(lay.thumb, band.height()),
        )
    } else {
        Rect::from_center_size(
            Pos2::new(band.center().x, at),
            Vec2::new(band.width(), lay.thumb),
        )
    };
    button_box(
        &painter,
        thumb,
        named("Button_Background"),
        enabled && resp.is_pointer_button_down_on(),
        z,
    );
    resp
}

// --- containers ----------------------------------------------------------------

fn group(ui: &mut Ui, node: &mut Node, rect: Rect, cx: &mut Cx, id: Id) {
    let w = node.w.clone();
    let z = cx.view.zoom;
    let painter = ui.painter().with_clip_rect(cx.clip);
    let style_n = w.i64("style").unwrap_or(0);
    if w.bool("transparent") != Some(true) {
        fill(&painter, rect, bg(&w, "Background"));
    }
    let line = w
        .color("line_color")
        .map(|c| color(&c))
        .unwrap_or(Color32::BLACK);
    let (_, inset) = group_inset(&w);
    let inset = inset as f32 * z;
    let fnt = font(&w, z);
    match style_n {
        0 => {
            let border = rect.shrink(inset / 2.0);
            outline(&painter, border, 1.0, line);
            let galley = painter.layout_no_wrap(node.prop("name"), fnt, fg(&w));
            let at = Pos2::new(rect.left() + inset, rect.top());
            let bg_rect = Rect::from_min_size(at, galley.size()).expand2(Vec2::new(2.0, 0.0));
            let back = bg(&w, "Background");
            fill(&painter, bg_rect, back);
            painter.galley(at, galley, fg(&w));
        }
        1 => {
            let bar = Rect::from_min_size(rect.min, Vec2::new(rect.width(), inset));
            fill(&painter, bar, line);
            outline(&painter, rect, 1.0, line);
            let tr = bar.shrink2(Vec2::new(3.0, 0.0));
            text_in(
                &painter,
                tr,
                &node.prop("name"),
                fnt,
                bg(&w, "Background"),
                Align2::LEFT_CENTER,
                false,
            );
        }
        2 => outline(&painter, rect, 1.0, line),
        _ => {}
    }
    let clip = cx.clip.intersect(rect);
    let view = cx.view;
    draw_nodes(ui, &mut node.children, &mut cx.sub(view, clip), id);
}

fn tabs(ui: &mut Ui, node: &mut Node, rect: Rect, cx: &mut Cx, id: Id) {
    let w = node.w.clone();
    let z = cx.view.zoom;
    let names: Vec<String> = tab_elements(&w)
        .iter()
        .map(|t| node.expand(t.child_text("name").unwrap_or("")))
        .collect();
    let tab_h = w.f64("tab_height").unwrap_or(30.0) as f32 * z;
    let painter = ui.painter().with_clip_rect(cx.clip);
    // The content sits where the builder put the children: below the
    // tabs, or right of them when they run down the side, `tab_width` wide.
    let (ix, iy) = crate::screen::tabs_inset(&w);
    let vertical = ix > 0.0;
    let body = Rect::from_min_max(rect.min + Vec2::new(ix as f32, iy as f32) * z, rect.max);
    fill(&painter, body, bg(&w, "Background"));
    outline(&painter, body, 1.0, Color32::GRAY);
    let mut pos = rect.min;
    for (i, name) in names.iter().enumerate() {
        let size = if vertical {
            Vec2::new(ix as f32 * z, tab_h)
        } else {
            let tw = painter
                .layout_no_wrap(name.clone(), font(&w, z), Color32::BLACK)
                .size()
                .x;
            Vec2::new(tw + 20.0 * z, tab_h)
        };
        let tab = Rect::from_min_size(pos, size);
        let r = ui.interact(tab.intersect(cx.clip), id.with(("tab", i)), Sense::click());
        let active = i == node.active_tab;
        button_box(
            &painter,
            tab,
            if active {
                bg(&w, "Background")
            } else {
                named("Button_Background")
            },
            active,
            z,
        );
        text_in(
            &painter,
            tab,
            name,
            font(&w, z),
            fg(&w),
            Align2::CENTER_CENTER,
            false,
        );
        if r.clicked() {
            node.active_tab = i;
        }
        if vertical {
            pos.y += size.y;
        } else {
            pos.x += size.x;
        }
    }
    let active = node.active_tab;
    if let Some(children) = node.tabs.get_mut(active) {
        let view = cx.view;
        draw_nodes(
            ui,
            children,
            &mut cx.sub(view, cx.clip.intersect(body)),
            id.with(active),
        );
    }
}

/// Draw an embedded screen into `area`, scaled per the widget's resize mode.
fn draw_embedded(ui: &mut Ui, w: &Widget, screen: &mut Screen, area: Rect, cx: &mut Cx, id: Id) {
    let z = cx.view.zoom;
    let scale = embed_scale(w, screen, area.size() / z);
    if w.bool("transparent") != Some(true) {
        fill(
            &ui.painter().with_clip_rect(cx.clip),
            area,
            color(&screen.display.background),
        );
    }
    let view = View {
        origin: area.min,
        zoom: z * scale,
    };
    let clip = cx.clip.intersect(area);
    draw_screen(ui, screen, &mut cx.sub(view, clip), id);
}

/// Whether the display `node` embeds, fetched in the background (in a
/// browser), has arrived, so should be embedded again.
#[cfg(target_arch = "wasm32")]
fn fetched(ui: &Ui, node: &Node) -> bool {
    if node.loading && crate::fetch::pending() {
        ui.ctx()
            .request_repaint_after(std::time::Duration::from_millis(50));
    }
    node.loading && !crate::fetch::pending()
}

/// Show the display `node` embeds once its thread has built it, keeping
/// the window repainting until then. It is never embedded again, so false.
#[cfg(not(target_arch = "wasm32"))]
fn fetched(ui: &Ui, node: &mut Node) -> bool {
    if node.poll_building(std::time::Duration::ZERO) {
        ui.ctx()
            .request_repaint_after(std::time::Duration::from_millis(50));
    }
    false
}

/// How much an embedded display is scaled to fit a widget `size` (unzoomed):
/// resize mode 1 scales the content to fit; the others draw it 1:1, clipped
/// to the widget (4, Crop, included).
fn embed_scale(w: &Widget, screen: &Screen, size: Vec2) -> f32 {
    match w.i64("resize").unwrap_or(0) {
        1 if screen.width > 0.0 && screen.height > 0.0 => {
            (size.x / screen.width as f32).min(size.y / screen.height as f32)
        }
        _ => 1.0,
    }
}

fn embedded(ui: &mut Ui, node: &mut Node, rect: Rect, cx: &mut Cx, id: Id) {
    if fetched(ui, node) {
        let file = node.prop("file");
        let group = node.prop("group_name");
        let macros = node.macros.clone();
        Builder::ui(cx.pvs).embed(node, &file, &macros, &group, cx.depth);
    }
    let w = node.w.clone();
    match node.embedded.as_mut() {
        Some(screen) => draw_embedded(ui, &w, screen, rect, cx, id),
        None => {
            let painter = ui.painter().with_clip_rect(cx.clip);
            let msg = node.error.clone().unwrap_or_else(|| node.prop("file"));
            if !msg.is_empty() {
                placeholder(&painter, rect, &msg, cx.view.zoom);
            }
        }
    }
}

fn navtabs(ui: &mut Ui, node: &mut Node, rect: Rect, cx: &mut Cx, id: Id) {
    let w = node.w.clone();
    let z = cx.view.zoom;
    let names: Vec<String> = tab_elements(&w)
        .iter()
        .map(|t| node.expand(t.child_text("name").unwrap_or("")))
        .collect();
    let vertical = w.i64("direction").unwrap_or(1) == 1;
    let tw = w.f64("tab_width").unwrap_or(100.0) as f32 * z;
    let th = w.f64("tab_height").unwrap_or(30.0) as f32 * z;
    let gap = w.f64("tab_spacing").unwrap_or(2.0) as f32 * z;
    let sel = w
        .color("selected_color")
        .map(|c| color(&c))
        .unwrap_or(Color32::from_rgb(236, 236, 236));
    let desel = w
        .color("deselected_color")
        .map(|c| color(&c))
        .unwrap_or(Color32::from_rgb(200, 200, 200));
    let painter = ui.painter().with_clip_rect(cx.clip);
    let mut clicked = None;
    for (i, name) in names.iter().enumerate() {
        let off = i as f32 * if vertical { th + gap } else { tw + gap };
        let pos = if vertical {
            Vec2::new(0.0, off)
        } else {
            Vec2::new(off, 0.0)
        };
        let tab = Rect::from_min_size(rect.min + pos, Vec2::new(tw, th));
        let r = ui.interact(tab.intersect(cx.clip), id.with(("nav", i)), Sense::click());
        let active = i == node.active_tab;
        button_box(&painter, tab, if active { sel } else { desel }, active, z);
        text_in(
            &painter,
            tab,
            name,
            font(&w, z),
            fg(&w),
            Align2::CENTER_CENTER,
            true,
        );
        if r.clicked() && !active {
            clicked = Some(i);
        }
    }
    if let Some(i) = clicked {
        Builder::ui(cx.pvs).pick_navtab(node, i, cx.depth);
    }
    if fetched(ui, node) {
        Builder::ui(cx.pvs).select_navtab(node, node.active_tab, cx.depth);
    }
    let body = if vertical {
        Rect::from_min_max(rect.min + Vec2::new(tw + gap, 0.0), rect.max)
    } else {
        Rect::from_min_max(rect.min + Vec2::new(0.0, th + gap), rect.max)
    };
    match node.embedded.as_mut() {
        Some(screen) => draw_embedded(ui, &w, screen, body, cx, id.with("body")),
        None => {
            if let Some(e) = &node.error {
                placeholder(&painter, body, e, z);
            }
        }
    }
}

pub(crate) fn alarm_border(painter: &egui::Painter, w: &Widget, s: &PvState, rect: Rect, z: f32) {
    if w.bool("border_alarm_sensitive") == Some(false) {
        return;
    }
    // A BOY widget (from an `.opi` file) drew INVALID and disconnected
    // solid white, DLS's BOY INVALID colour.
    if w.bool("boy_alarm_border") == Some(true)
        && (!s.connected || s.severity == pv::Severity::Invalid)
    {
        let width = (2.0 * z).max(1.0);
        outline(painter, rect.expand(width), width, Color32::WHITE);
        return;
    }
    let c = if s.connected {
        severity_color(s.severity)
    } else {
        Some(named("DISCONNECTED"))
    };
    if let Some(c) = c {
        let width = (2.0 * z).max(1.0);
        if s.connected {
            outline(painter, rect.expand(width), width, c);
        } else {
            // Dashed, as Phoebus marks disconnected PVs.
            let r = rect.expand(width / 2.0);
            let pts = [
                r.left_top(),
                r.right_top(),
                r.right_bottom(),
                r.left_bottom(),
                r.left_top(),
            ];
            painter.extend(Shape::dashed_line(
                &pts,
                Stroke::new(width, c),
                4.0 * z,
                3.0 * z,
            ));
        }
    }
}

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

    #[test]
    fn only_resize_mode_1_scales_embedded_content() {
        let d = bob::Display::parse(
            "<display><width>400</width><height>100</height><widget type='label'/></display>",
        )
        .unwrap();
        let pvs = PvManager::new();
        let screen = crate::screen::Builder::new(&pvs).screen(d, &bob::Macros::default(), None, 0);
        let widget = |resize: i64| {
            bob::Display::parse(&format!(
                "<display><widget type='embedded'><resize>{resize}</resize></widget></display>"
            ))
            .unwrap()
            .widgets[0]
                .clone()
        };
        let size = Vec2::new(200.0, 100.0);
        assert_eq!(embed_scale(&widget(1), &screen, size), 0.5);
        assert_eq!(embed_scale(&widget(4), &screen, size), 1.0);
        assert_eq!(embed_scale(&widget(0), &screen, size), 1.0);
    }
    #[test]
    fn commands_of_a_display_from_the_web_do_nothing() {
        let actions = |dir: &str| {
            format!(
                "<display><widget type='action_button'><actions><action type='command'>
                <command>./launch 04</command><command_directory>{dir}</command_directory>
                </action></actions></widget></display>"
            )
        };
        let pvs = PvManager::new();
        for dir in ["", "/tmp", "file:///tmp"] {
            let mut d = bob::Display::parse(&actions(dir)).unwrap();
            d.source = Some(Source::Url("https://example.com/x.bob".into()));
            let screen = crate::screen::Builder::new(&pvs).screen(d, &Macros::default(), None, 0);
            let node = &screen.nodes[0];
            let mut requests = Vec::new();
            let mut cx = Cx {
                view: View {
                    origin: Pos2::ZERO,
                    zoom: 1.0,
                },
                clip: Rect::EVERYTHING,
                pvs: &pvs,
                requests: &mut requests,
                depth: 0,
                touch: false,
                trends: &[],
                window: WindowKey::Main,
            };
            let actions = node.w.actions();
            assert_eq!(actions.len(), 1);
            for a in actions {
                assert!(matches!(a, Action::Command { .. }));
                run_action(node, &a, &mut cx);
            }
            // The command is run on a thread, where it is refused.
            assert_eq!(requests.len(), 1, "{dir}");
            for r in requests {
                let Request::Command {
                    base,
                    directory,
                    command,
                } = r
                else {
                    panic!("not a command: {dir}");
                };
                let target = crate::launch::command_target(base.as_ref(), directory, &command);
                assert!(target.is_none(), "{dir}");
            }
        }
    }

    use std::sync::Arc;

    /// An action button with properties `props` and one action, "Go".
    fn button_screen(pvs: &PvManager, props: &str) -> crate::screen::Screen {
        let d = bob::Display::parse(&format!(
            "<display><widget type='action_button'>{props}<actions>
            <action type='open_display'><file>a.bob</file><description>Go</description>
            </action></actions></widget></display>"
        ))
        .unwrap();
        crate::screen::Builder::new(pvs).screen(d, &Macros::default(), None, 0)
    }

    #[test]
    fn only_action_button_text_naming_actions_shows_them() {
        let pvs = PvManager::new();
        for (props, shown) in [
            ("", "Go"),
            ("<text>$(actions)</text>", "Go"),
            ("<text></text>", ""),
            ("<text>Open</text>", "Open"),
        ] {
            let screen = button_screen(&pvs, props);
            let node = &screen.nodes[0];
            assert_eq!(button_text(node, &node.w.actions()), shown, "{props}");
        }
        // As Phoebus' messages: none, several, several run as one, and an
        // action without a description.
        let text = |actions: &str| {
            let d = bob::Display::parse(&format!(
                "<display><widget type='action_button'>{actions}</widget></display>"
            ))
            .unwrap();
            let screen = crate::screen::Builder::new(&pvs).screen(d, &Macros::default(), None, 0);
            let node = &screen.nodes[0];
            button_text(node, &node.w.actions())
        };
        let two = "<action type='write_pv'><pv_name>loc://a</pv_name></action>
            <action type='open_display'><file>a.bob</file></action>";
        assert_eq!(text(""), "EMPTY");
        assert_eq!(text(&format!("<actions>{two}</actions>")), "Choose 1 of 2");
        assert_eq!(
            text(&format!("<actions execute_as_one='true'>{two}</actions>")),
            "2 actions"
        );
        assert_eq!(
            text("<actions><action type='write_pv'><pv_name>loc://a</pv_name></action></actions>"),
            "Write PV"
        );
    }

    #[test]
    fn transparent_action_buttons_have_no_face() {
        let pvs = PvManager::new();
        let filled = |props: &str| {
            let mut screen = button_screen(&pvs, props);
            let ctx = egui::Context::default();
            let mut requests = Vec::new();
            let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
                let mut cx = Cx {
                    view: View {
                        origin: Pos2::ZERO,
                        zoom: 1.0,
                    },
                    clip: Rect::EVERYTHING,
                    pvs: &pvs,
                    requests: &mut requests,
                    depth: 0,
                    touch: false,
                    trends: &[],
                    window: WindowKey::Main,
                };
                let rect = Rect::from_min_size(Pos2::ZERO, Vec2::new(100.0, 30.0));
                action_button(ui, &mut screen.nodes[0], rect, &mut cx, Id::new("b"));
            });
            out.textures_delta.clear();
            out.shapes
                .iter()
                .any(|s| matches!(&s.shape, egui::Shape::Rect(r) if r.fill.a() > 0))
        };
        assert!(filled("<text></text>"));
        assert!(!filled("<text></text><transparent>true</transparent>"));
    }

    #[test]
    fn arcs_go_anticlockwise_from_three_o_clock() {
        let rect = Rect::from_center_size(Pos2::ZERO, Vec2::new(20.0, 10.0));
        let p = arc_points(rect, 0.0, 90.0);
        let near = |a: Pos2, b: Pos2| (a - b).length() < 1e-4;
        assert!(near(p[0], Pos2::new(10.0, 0.0)));
        assert!(near(*p.last().unwrap(), Pos2::new(0.0, -5.0)));
        let full = arc_points(rect, 90.0, -360.0);
        assert!(near(full[0], *full.last().unwrap()));
        assert!(full.len() > 60);
    }

    #[test]
    fn rows_that_fit() {
        // A 20px textupdate with a 14px font holds a single row.
        assert_eq!(max_rows(18.0, 17.0, true), 1);
        assert_eq!(max_rows(40.0, 17.0, true), 2);
        // Never fewer than one row, even in a box shorter than the font.
        assert_eq!(max_rows(5.0, 17.0, true), 1);
        // Without wrapping it's always one row.
        assert_eq!(max_rows(400.0, 17.0, false), 1);
    }

    /// Lay out `text` in a `w`x`h` box with Liberation Sans 14, as text_in does.
    fn layout(text: &str, w: f32, h: f32, halign: egui::Align, wrap: bool) -> Arc<egui::Galley> {
        let ctx = egui::Context::default();
        style::install_fonts(&ctx);
        let mut galley = None;
        let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
            let font = FontId::new(14.0, egui::FontFamily::Name(style::BOLD.into()));
            let row_height = ui.fonts_mut(|f| f.row_height(&font));
            let job = text_job(
                text,
                font,
                Color32::BLACK,
                Vec2::new(w, h),
                row_height,
                halign,
                wrap,
            );
            galley = Some(ui.painter().layout_job(job));
        });
        out.textures_delta.clear();
        galley.unwrap()
    }

    const PV: &str = "<BL01T-DI-CAM-01:DET:PortName_RBV>";

    #[test]
    fn auto_size_labels_fit_their_expanded_text() {
        let xml = r#"<display><macros><camera>LI-DI-CAMERA-01</camera></macros>
            <widget type="label"><x>10</x><y>20</y><width>66</width><height>20</height>
              <text>$(camera)</text><auto_size>true</auto_size></widget>
            <widget type="label"><x>10</x><y>50</y><width>66</width><height>20</height>
              <text>$(camera)</text></widget></display>"#;
        let pvs = PvManager::new();
        let display = bob::Display::parse(xml).unwrap();
        let mut screen = Builder::new(&pvs).screen(display, &Macros::default(), None, 0);
        let ctx = egui::Context::default();
        style::install_fonts(&ctx);
        let mut natural = 0.0;
        let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
            let mut requests = Vec::new();
            let mut cx = Cx {
                view: View {
                    origin: Pos2::ZERO,
                    zoom: 1.0,
                },
                clip: Rect::EVERYTHING,
                pvs: &pvs,
                requests: &mut requests,
                depth: 0,
                touch: false,
                trends: &[],
                window: WindowKey::Main,
            };
            draw_screen(ui, &mut screen, &mut cx, Id::new("auto_size"));
            let font = font(&screen.nodes[0].w, 1.0);
            natural = ui
                .painter()
                .layout_no_wrap("LI-DI-CAMERA-01".into(), font, Color32::BLACK)
                .size()
                .x;
        });
        out.textures_delta.clear();
        let [x, y, w, h] = screen.nodes[0].rect;
        assert_eq!((x, y), (10.0, 20.0));
        // Wide enough for the whole text, so nothing is elided.
        assert!(
            w > 66.0 && w >= natural as f64 && w < natural as f64 + 1.0,
            "{w} {natural}"
        );
        assert!(h > 0.0);
        // Without auto_size the label keeps its size.
        assert_eq!(screen.nodes[1].rect, [10.0, 50.0, 66.0, 20.0]);
    }

    /// Lay out a text entry's text `w` wide, as `text_entry` does.
    fn entry(text: &str, w: f32, editing: bool) -> Arc<egui::Galley> {
        let ctx = egui::Context::default();
        style::install_fonts(&ctx);
        let mut galley = None;
        let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
            let font = FontId::new(14.0, egui::FontFamily::Name(style::BOLD.into()));
            let job = entry_job(text, font, Color32::BLACK, egui::Align::Min, w, editing);
            galley = Some(ui.painter().layout_job(job));
        });
        out.textures_delta.clear();
        galley.unwrap()
    }

    #[test]
    fn text_entry_elides_like_a_text_update_except_while_editing() {
        let shown = entry(PV, 100.0, false);
        assert_eq!(shown.rows.len(), 1);
        assert!(shown.elided);
        assert!(shown.rows[0].text().starts_with("<BL01T"));
        assert!(shown.rows[0].text().ends_with('…'));
        assert!(shown.size().x <= 100.0);
        // Editing keeps every character for the cursor.
        let editing = entry(PV, 100.0, true);
        assert!(!editing.elided);
        assert_eq!(editing.rows[0].text(), PV);
        // Text that fits is untouched.
        assert!(!entry("1.5", 100.0, false).elided);
    }

    #[test]
    fn combo_items_never_shrink_below_the_ui_font() {
        let ui = FontId::proportional(14.0);
        assert_eq!(combo_item_font(FontId::proportional(8.0), &ui).size, 14.0);
        assert_eq!(combo_item_font(FontId::proportional(28.0), &ui).size, 28.0);
        let mono = combo_item_font(FontId::monospace(8.0), &ui);
        assert_eq!(mono.family, egui::FontFamily::Monospace);
    }

    #[test]
    fn touch_mode_widens_the_hit_radius_and_back() {
        let ctx = egui::Context::default();
        touch_targets(&ctx, true);
        let radius = ctx.global_style().interaction.interact_radius;
        assert_eq!(radius, TOUCH_RADIUS);
        // A 20 point control is a full touch target.
        assert_eq!(20.0 + 2.0 * radius, TOUCH_TARGET);
        touch_targets(&ctx, false);
        assert_eq!(ctx.global_style().interaction.interact_radius, MOUSE_RADIUS);
    }

    #[test]
    fn web_displays_only_open_web_pages() {
        let web = Source::parse("http://h/opi/a.bob");
        let file = Source::parse("/opi/a.bob");
        assert!(webpage_allowed(Some(&web), "https://example.com/x"));
        assert!(!webpage_allowed(Some(&web), "/home/me/run.sh"));
        assert!(!webpage_allowed(Some(&web), "file:///etc/passwd"));
        assert!(!webpage_allowed(Some(&web), "ssh://h"));
        assert!(webpage_allowed(Some(&file), "/opi/manual.pdf"));
        assert!(webpage_allowed(None, "https://example.com"));
        assert!(!webpage_allowed(Some(&file), "--help"));
        assert!(!webpage_allowed(None, " "));
    }

    #[test]
    fn pv_names_are_shown_without_local_initialisers() {
        assert_eq!(plain_pv_name("loc://arr<VDoubleArray>(1,3,2)"), "loc://arr");
        assert_eq!(plain_pv_name("BL01T:X"), "BL01T:X");
    }

    #[test]
    fn overflow_in_one_row_keeps_the_start() {
        // A 20px-high textupdate shows one whole row, elided, never a middle fragment.
        for halign in [egui::Align::Min, egui::Align::Center, egui::Align::Max] {
            for wrap in [true, false] {
                let g = layout(PV, 205.0, 18.0, halign, wrap);
                assert_eq!(g.rows.len(), 1);
                assert!(g.elided);
                assert!(g.rows[0].text().starts_with("<BL01T-DI-CAM-01"));
                assert!(g.rows[0].text().ends_with('…'));
                assert!(g.size().x <= 205.5);
            }
        }
    }

    #[test]
    fn wrapped_rows_fit_and_align_on_their_own() {
        let g = layout(PV, 205.0, 40.0, egui::Align::Center, true);
        assert_eq!(g.rows.len(), 2);
        assert!(!g.elided);
        // Each row centred on x = 0, so the shorter row starts further right.
        for row in &g.rows {
            let r = row.rect();
            assert!((r.center().x).abs() < 1.0, "{r:?}");
        }
        let g = layout(PV, 205.0, 40.0, egui::Align::Max, true);
        for row in &g.rows {
            assert!(row.rect().max.x.abs() < 1.0);
        }
    }

    #[test]
    fn text_that_just_fits_is_not_elided() {
        // At fractional zooms text can fit with under a pixel to spare.
        let natural = layout(PV, 10_000.0, 18.0, egui::Align::Center, true)
            .size()
            .x;
        let g = layout(PV, natural.ceil(), 18.0, egui::Align::Center, true);
        assert!(!g.elided);
        assert_eq!(g.text(), PV);
    }

    #[test]
    fn explicit_lines_are_kept_without_wrapping() {
        // wrap_words=false still shows every line, as a JavaFX Label does.
        let g = layout(
            "Line one\nLine two longer",
            200.0,
            50.0,
            egui::Align::Min,
            false,
        );
        assert_eq!(g.rows.len(), 2);
        assert!(!g.elided);
    }

    #[test]
    fn short_text_is_untouched() {
        let g = layout("12.5 mm", 100.0, 18.0, egui::Align::Center, true);
        assert_eq!(g.rows.len(), 1);
        assert!(!g.elided);
        assert_eq!(g.text(), "12.5 mm");
    }

    #[test]
    fn pvi_open_display_symbol_has_a_glyph() {
        let ctx = egui::Context::default();
        style::install_fonts(&ctx);
        let mut found = vec![];
        let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
            for family in [
                egui::FontFamily::Proportional,
                egui::FontFamily::Name(style::BOLD.into()),
                egui::FontFamily::Monospace,
            ] {
                let font = FontId::new(14.0, family);
                found.push(ui.fonts_mut(|f| f.has_glyph(&font, '\u{29C9}')));
            }
        });
        out.textures_delta.clear();
        assert_eq!(found, [true, true, true]);
    }

    fn widget(props: &str) -> Widget {
        let xml = format!(r#"<display><widget type="slide_button">{props}</widget></display>"#);
        bob::Display::parse(&xml).unwrap().widgets.remove(0)
    }

    fn state(value: i64, writable: bool) -> PvState {
        PvState {
            connected: true,
            value: Value::Long(value),
            writable,
            ..Default::default()
        }
    }

    #[test]
    fn string_format_shows_char_arrays_as_text() {
        let chars = |codes: &[i64]| PvState {
            connected: true,
            value: Value::LongArray(Arc::from(codes)),
            ..Default::default()
        };
        let path = chars(&[47, 116, 109, 112, 0, 120, 121]);
        let string = widget("<format>6</format>");
        assert!(writes_long_string(&string));
        assert_eq!(format_value(&string, &path), "/tmp");
        assert_eq!(format_value(&string, &chars(&[104, 105])), "hi");
        // Other formats, and values that are not arrays of numbers, are as before.
        let default = widget("");
        assert!(!writes_long_string(&default));
        assert_eq!(
            format_value(&default, &path),
            "[47, 116, 109, 112, 0, 120, 121]"
        );
        let text = PvState {
            connected: true,
            value: Value::Str("a b".into()),
            ..Default::default()
        };
        assert_eq!(format_value(&string, &text), "a b");
        // A number shows as the character of its low byte, as in Phoebus,
        // and NUL as nothing.
        assert_eq!(format_value(&string, &state(65, true)), "A");
        assert_eq!(format_value(&string, &state(0x141, true)), "A");
        assert_eq!(format_value(&string, &state(0, true)), "");
        let double = PvState {
            connected: true,
            value: Value::Double(104.7),
            units: "mm".into(),
            ..Default::default()
        };
        assert_eq!(format_value(&string, &double), "h mm");
    }

    #[test]
    fn label_defaults_to_label_only_when_absent() {
        assert_eq!(raw_label(&widget("")), "Label");
        assert_eq!(raw_label(&widget("<label></label>")), "");
        assert_eq!(raw_label(&widget("<label>Go</label>")), "Go");
    }

    #[test]
    fn toggles_the_whole_value_or_one_bit() {
        // A slide button's bit defaults to 0, as in Phoebus.
        let default = widget("");
        assert_eq!(toggled(&default, Some(&state(0, true))), 1);
        assert_eq!(toggled(&default, Some(&state(5, true))), 4);
        let whole = widget("<bit>-1</bit>");
        assert_eq!(toggled(&whole, Some(&state(0, true))), 1);
        assert_eq!(toggled(&whole, Some(&state(5, true))), 0);
        let bit1 = widget("<bit>1</bit>");
        assert_eq!(toggled(&bit1, Some(&state(1, true))), 3);
        assert_eq!(toggled(&bit1, Some(&state(3, true))), 1);
        let bit3 = widget("<bit>3</bit>");
        assert_eq!(toggled(&bit3, Some(&state(1, true))), 9);
    }

    #[test]
    fn push_values_set_and_clear_the_bit() {
        assert_eq!(with_bit(-1, 6, true), 1);
        assert_eq!(with_bit(-1, 6, false), 0);
        assert_eq!(with_bit(2, 1, true), 5);
        assert_eq!(with_bit(2, 5, false), 1);
    }

    #[test]
    fn push_writes_on_press_and_release() {
        assert_eq!(push_edge(false, true, false), Push::Press);
        assert_eq!(push_edge(true, true, false), Push::None);
        assert_eq!(push_edge(true, false, true), Push::Release);
        assert_eq!(push_edge(false, false, true), Push::Both);
        assert_eq!(push_edge(false, false, false), Push::None);
    }

    #[test]
    fn disabled_or_read_only_controls_do_not_write() {
        let w = widget("");
        assert!(can_write(&w, Some(&state(0, true))));
        assert!(!can_write(&w, Some(&state(0, false))));
        assert!(is_disabled(&w, Some(&state(0, false))));
        assert!(!can_write(&w, None));
        let disconnected = PvState::default();
        assert!(!can_write(&w, Some(&disconnected)));
        assert!(!is_disabled(&w, Some(&disconnected)));
        let off = widget("<enabled>false</enabled>");
        assert!(is_disabled(&off, None));
        assert!(!can_write(&off, Some(&state(0, true))));
    }

    #[test]
    fn tooltip_fills_in_pv_name_and_value() {
        let expand = |t: &str| t.replace("$(P)", "BL01T:");
        assert_eq!(
            tooltip_text("$(pv_name)\n$(pv_value)", "X:Y", "1.5 mm", &expand),
            "X:Y\n1.5 mm"
        );
        assert_eq!(
            tooltip_text("$(P)M1.STOP = 1", "", "", &expand),
            "BL01T:M1.STOP = 1"
        );
    }

    #[test]
    fn open_display_files_use_the_action_macros() {
        let xml = r#"<display><widget type="symbol"><name>S</name>
            <macros><D>sub</D></macros>
            <actions><action type="open_display"><file>$(IOC)/$(P).bob</file>
            <macros><P>BL01C-MO-01</P><IOC>../$(D)/ioc</IOC></macros>
            </action></actions></widget></display>"#;
        let pvs = PvManager::new();
        let display = bob::Display::parse(xml).unwrap();
        let screen = Builder::new(&pvs).screen(display, &Macros::default(), None, 0);
        let node = &screen.nodes[0];
        let mut requests = Vec::new();
        let mut cx = Cx {
            view: View {
                origin: Pos2::ZERO,
                zoom: 1.0,
            },
            clip: Rect::EVERYTHING,
            pvs: &pvs,
            requests: &mut requests,
            depth: 0,
            touch: false,
            trends: &[],
            window: WindowKey::Main,
        };
        run_action(node, &node.w.actions()[0], &mut cx);
        let Some(Request::Open { source, macros, .. }) = requests.first() else {
            panic!("no display opened")
        };
        assert_eq!(*source, Source::parse("../sub/ioc/BL01C-MO-01.bob"));
        assert_eq!(macros.get("P"), Some("BL01C-MO-01"));
    }

    #[test]
    fn text_entries_and_sliders_take_no_space_below_the_display() {
        // Each used to take the space from the cursor, below the display, so
        // a display with many of them could scroll past its bottom.
        let mut xml = String::from("<display><width>300</width><height>100</height>");
        for (i, kind) in ["textentry", "spinner", "scaledslider"].iter().enumerate() {
            for j in 0..4 {
                xml += &format!(
                    r#"<widget type="{kind}"><x>{}</x><y>{}</y><width>60</width><height>20</height></widget>"#,
                    70 * j,
                    30 * i
                );
            }
        }
        xml += "</display>";
        let pvs = PvManager::new();
        let display = bob::Display::parse(&xml).unwrap();
        let mut screen = Builder::new(&pvs).screen(display, &Macros::default(), None, 0);
        let ctx = egui::Context::default();
        let mut used = None;
        let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
            let size = Vec2::new(screen.width as f32, screen.height as f32);
            let (rect, _) = ui.allocate_exact_size(size, Sense::hover());
            let mut requests = Vec::new();
            let mut cx = Cx {
                view: View {
                    origin: rect.min,
                    zoom: 1.0,
                },
                clip: rect,
                pvs: &pvs,
                requests: &mut requests,
                depth: 0,
                touch: false,
                trends: &[],
                window: WindowKey::Main,
            };
            draw_screen(ui, &mut screen, &mut cx, Id::new("display"));
            used = Some((rect, ui.min_rect()));
        });
        out.textures_delta.clear();
        let (rect, used) = used.unwrap();
        assert_eq!(used, rect);
    }

    #[test]
    fn trend_actions_and_strip_tool_commands_open_trends() {
        let xml = r#"<display><macros><dom>BL01T</dom></macros>
            <widget type="action_button"><actions>
            <action type="trend"><pv_name>$(dom)-VA-GAUGE-01:PLOG</pv_name><pv_name>$(dom)-VA-IMG-01:PLOG</pv_name></action>
            <action type="command"><command>strip.py $(dom)-VA-GAUGE-02:PLOG log_mbar</command></action>
            <action type="execute"><script file="EmbeddedPy"><text>ScriptUtil.executeEclipseCommand("org.csstudio.trends.databrowser2.newWindow", ['pvnames', '$(dom):X'])</text></script></action>
            </actions></widget></display>"#;
        let pvs = PvManager::new();
        let display = bob::Display::parse(xml).unwrap();
        let screen = Builder::new(&pvs).screen(display, &Macros::default(), None, 0);
        let node = &screen.nodes[0];
        let mut requests = Vec::new();
        let mut cx = Cx {
            view: View {
                origin: Pos2::ZERO,
                zoom: 1.0,
            },
            clip: Rect::EVERYTHING,
            pvs: &pvs,
            requests: &mut requests,
            depth: 0,
            touch: false,
            trends: &[],
            window: WindowKey::Main,
        };
        for a in node.w.actions() {
            run_action(node, &a, &mut cx);
        }
        let trends: Vec<&Vec<String>> = requests
            .iter()
            .filter_map(|r| match r {
                Request::Trend(names) => Some(names),
                _ => None,
            })
            .collect();
        assert_eq!(
            trends,
            [
                &vec!["BL01T-VA-GAUGE-01:PLOG", "BL01T-VA-IMG-01:PLOG"],
                &vec!["BL01T-VA-GAUGE-02:PLOG"],
                &vec!["BL01T:X"]
            ]
        );
    }

    #[test]
    fn displays_from_the_web_and_local_files_both_write_at_once() {
        let xml = r#"<display><widget type="action_button"><pv_name>loc://write_t(0)</pv_name><actions>
            <action type="write_pv"><pv_name>loc://write_t(0)</pv_name><value>5</value></action>
            <action type="execute"><script file="EmbeddedPy"><text>pv = ScriptUtil.getPrimaryPV(widget)
var pv_value = PVUtil.getDouble(pv);
pv_value = (pv_value == 0) ? 1 : 0;
pv.setValue(pv_value);
</text></script></action>
            </actions></widget></display>"#;
        let run = |source: &str| {
            let pvs = PvManager::new();
            let mut display = bob::Display::parse(xml).unwrap();
            display.source = Some(Source::parse(source));
            let screen = Builder::new(&pvs).screen(display, &Macros::default(), None, 0);
            let node = &screen.nodes[0];
            let pv = node.pv.clone().unwrap();
            let mut requests = Vec::new();
            let mut cx = Cx {
                view: View {
                    origin: Pos2::ZERO,
                    zoom: 1.0,
                },
                clip: Rect::EVERYTHING,
                pvs: &pvs,
                requests: &mut requests,
                depth: 0,
                touch: false,
                trends: &[],
                window: WindowKey::Main,
            };
            let values: Vec<_> = node
                .w
                .actions()
                .iter()
                .map(|a| {
                    run_action(node, a, &mut cx);
                    pv.state().value.as_f64()
                })
                .collect();
            (values, requests)
        };
        // Wherever the display came from, both actions write at once, as
        // in Phoebus: write_pv sets 5, then the toggle flips 5 to 0.
        for source in ["https://host/a.bob", "/tmp/a.bob"] {
            let (values, requests) = run(source);
            assert!(requests.is_empty(), "{source}");
            assert_eq!(values, [Some(5.0), Some(0.0)], "{source}");
        }
    }

    #[test]
    fn recognises_the_techui_toggle_script() {
        let script = "/* Embedded javascript */
importClass(org.csstudio.display.builder.runtime.script.PVUtil);
importClass(org.csstudio.display.builder.runtime.script.ScriptUtil);
pv = ScriptUtil.getPrimaryPV(widget)
var pv_value = PVUtil.getDouble(pv);
pv_value = (pv_value == 0) ? 1 : 0;
pv.setValue(pv_value);
";
        assert!(is_toggle_script(script));
        assert!(is_toggle_script(&script.replace('\n', "\n        ")));
        assert!(!is_toggle_script(&script.replace("? 1 : 0", "? 2 : 0")));
        assert!(!is_toggle_script(""));
    }

    #[test]
    fn bit_selection() {
        assert!(bit_on(1, 0));
        assert!(!bit_on(2, 0));
        assert!(bit_on(2, 1));
        assert!(bit_on(2, -1));
        assert!(!bit_on(0, -1));
    }

    #[test]
    fn bit_defaults_follow_phoebus() {
        let kind = |k: &str| {
            let xml = format!(r#"<display><widget type="{k}"/></display>"#);
            bit(&bob::Display::parse(&xml).unwrap().widgets[0])
        };
        assert_eq!(kind("bool_button"), 0);
        assert_eq!(kind("checkbox"), 0);
        assert_eq!(kind("slide_button"), 0);
        assert_eq!(kind("led"), -1);
    }

    #[test]
    fn slide_track_fits_small_widget() {
        let rect = Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::splat(20.0));
        let (track, label) = slide_layout(rect, 1.0, false);
        assert!(label.is_none());
        assert!(rect.contains_rect(track));
        assert!(track.width() > track.height());
        assert_eq!(track.center().y, rect.center().y);
        assert_eq!(track.left(), rect.left() + 1.0);
    }

    #[test]
    fn slide_track_without_label_stays_at_the_left() {
        // Phoebus: HBox(button, label) with the label growing, so an empty
        // label still leaves the switch at the left.
        let rect = Rect::from_min_size(Pos2::ZERO, Vec2::new(80.0, 20.0));
        let (track, label) = slide_layout(rect, 1.0, false);
        assert!(label.is_none());
        assert_eq!(track.left(), rect.left() + 1.0);
    }

    #[test]
    fn slide_track_left_of_label() {
        let rect = Rect::from_min_size(Pos2::ZERO, Vec2::new(120.0, 30.0));
        let (track, label) = slide_layout(rect, 1.0, true);
        let label = label.unwrap();
        assert!(rect.contains_rect(track));
        assert!(track.right() < label.left());
        assert_eq!(label.right(), rect.right());
        assert_eq!(track.height(), 16.0);
    }

    #[test]
    fn slider_is_centred_in_its_box() {
        // sr/pinger.opi: a tall slider with no scale sits in the middle of
        // its box, not at the top.
        let rect = Rect::from_min_size(Pos2::new(64.0, 3.0), Vec2::new(384.0, 76.0));
        let lay = slider_layout(rect, true, 1.0, None, None);
        assert_eq!(lay.band.center().y, rect.center().y);
        assert!(lay.scale.is_none());
        assert!(rect.contains_rect(lay.band));
        assert!(lay.from - lay.thumb / 2.0 >= rect.left());
        assert!(lay.to + lay.thumb / 2.0 <= rect.right());
    }

    #[test]
    fn slider_scale_sits_beside_the_thumb() {
        let rect = Rect::from_min_size(Pos2::ZERO, Vec2::new(384.0, 76.0));
        let lay = slider_layout(rect, true, 1.0, Some((14.0, 20.0)), None);
        let scale = lay.scale.unwrap();
        assert!(lay.labels);
        assert_eq!(scale.top(), lay.band.bottom());
        assert!(rect.contains_rect(scale) && rect.contains_rect(lay.band));
        // Room at the ends for the first and last labels.
        assert!(lay.from >= 20.0 && lay.to <= 364.0);
        // The two together are centred.
        assert_eq!((lay.band.top() + scale.bottom()) / 2.0, rect.center().y);
        // Vertical: the minimum at the bottom, the scale to the right.
        let rect = Rect::from_min_size(Pos2::ZERO, Vec2::new(76.0, 384.0));
        let lay = slider_layout(rect, false, 1.0, Some((30.0, 7.0)), None);
        assert!(lay.from > lay.to);
        assert_eq!(lay.scale.unwrap().left(), lay.band.right());
    }

    #[test]
    fn small_slider_drops_labels_before_its_thumb() {
        // cameraControl-gige.opi: 168x24, no room for labels under the thumb.
        let rect = Rect::from_min_size(Pos2::ZERO, Vec2::new(168.0, 24.0));
        let lay = slider_layout(rect, true, 1.0, Some((14.0, 20.0)), None);
        assert!(!lay.labels);
        assert!(lay.scale.is_some());
        assert!(lay.band.height() >= 8.0);
        assert!(rect.contains_rect(lay.band) && rect.contains_rect(lay.scale.unwrap()));
        // Too narrow for any travel: the thumb stays in the middle.
        let rect = Rect::from_min_size(Pos2::ZERO, Vec2::new(6.0, 24.0));
        let lay = slider_layout(rect, true, 1.0, None, None);
        assert_eq!((lay.from, lay.to), (3.0, 3.0));
        // Markers go before the scale's labels.
        let rect = Rect::from_min_size(Pos2::ZERO, Vec2::new(168.0, 40.0));
        let lay = slider_layout(rect, true, 1.0, Some((14.0, 20.0)), Some(14.0));
        assert!(lay.markers.is_none() && lay.labels);
    }

    #[test]
    fn slider_markers_sit_on_the_near_side() {
        // sr/pinger.opi: markers above the thumb and the scale below it,
        // the three centred, so the thumb is clear of the title label that
        // covers the top 29px of the box.
        let rect = Rect::from_min_size(Pos2::new(64.0, 3.0), Vec2::new(384.0, 76.0));
        let lay = slider_layout(rect, true, 1.0, Some((14.0, 20.0)), Some(14.0));
        let (markers, scale) = (lay.markers.unwrap(), lay.scale.unwrap());
        assert_eq!(markers.bottom(), lay.band.top());
        assert_eq!(scale.top(), lay.band.bottom());
        assert!(rect.contains_rect(markers) && rect.contains_rect(scale));
        assert_eq!(lay.band.center().y, rect.center().y);
        assert!(lay.band.top() > rect.top() + 29.0);
        // Vertical: markers to the left.
        let rect = Rect::from_min_size(Pos2::ZERO, Vec2::new(120.0, 300.0));
        let lay = slider_layout(rect, false, 1.0, Some((40.0, 7.0)), Some(36.0));
        assert_eq!(lay.markers.unwrap().right(), lay.band.left());
    }

    #[test]
    fn scaled_slider_markers_follow_the_show_flags() {
        let slider = |props: &str| {
            let xml = format!(r#"<display><widget type="scaledslider">{props}</widget></display>"#);
            bob::Display::parse(&xml).unwrap().widgets.remove(0)
        };
        let names = |w: &Widget, s| -> Vec<&str> {
            slider_markers(w, s).into_iter().map(|(_, n)| n).collect()
        };
        // Phoebus' default levels, all shown, without `show_markers`.
        let own = slider("<limits_from_pv>false</limits_from_pv>");
        assert_eq!(
            slider_markers(&own, None),
            [
                (10.0, "LOLO"),
                (20.0, "LOW"),
                (80.0, "HIGH"),
                (90.0, "HIHI")
            ]
        );
        let some = slider(
            "<limits_from_pv>false</limits_from_pv><level_low>30</level_low>
            <show_lolo>false</show_lolo><show_hihi>false</show_hihi>",
        );
        assert_eq!(slider_markers(&some, None), [(30.0, "LOW"), (80.0, "HIGH")]);
        // From the PV: only the limits it has.
        let pv = PvState {
            connected: true,
            warning_range: Some((2.0, 8.0)),
            ..Default::default()
        };
        assert_eq!(names(&slider(""), Some(&pv)), ["LOW", "HIGH"]);
        // Without a connected PV, the widget's own levels.
        assert_eq!(names(&slider(""), None).len(), 4);
        let gone = PvState {
            connected: false,
            ..pv.clone()
        };
        assert_eq!(names(&slider(""), Some(&gone)).len(), 4);
        assert!(
            names(
                &slider("<show_low>false</show_low><show_high>false</show_high>"),
                Some(&pv)
            )
            .is_empty()
        );
    }

    #[test]
    fn slider_ranges_fall_back_as_phoebus_or_boy() {
        let slider = |props: &str| {
            let xml = format!(r#"<display><widget type="scaledslider">{props}</widget></display>"#);
            bob::Display::parse(&xml).unwrap().widgets.remove(0)
        };
        let bob = slider("");
        assert_eq!(slider_range(&bob, (-5.0, 5.0)), (-5.0, 5.0));
        assert_eq!(slider_range(&bob, (3.0, 3.0)), (0.0, 100.0));
        assert_eq!(slider_range(&bob, (10.0, 1.0)), (0.0, 100.0));
        let boy = slider("<boy_range>true</boy_range>");
        assert_eq!(slider_range(&boy, (-5.0, 5.0)), (-5.0, 5.0));
        assert_eq!(slider_range(&boy, (3.0, 3.0)), (3.0, 4.0));
        assert_eq!(slider_range(&boy, (10.0, 1.0)), (10.0, 1.0));
    }

    #[test]
    fn clipped_widgets_are_keyed_by_their_id() {
        // The same widget after one or two others has the same id.
        let ctx = egui::Context::default();
        let rect = Rect::from_min_size(Pos2::new(10.0, 10.0), Vec2::new(60.0, 20.0));
        let mut ids = Vec::new();
        for before in [1, 2] {
            let mut out = ctx.run_ui(egui::RawInput::default(), |ui| {
                for _ in 0..before {
                    ui.label("before");
                }
                let mut v = 1.0;
                let drag = egui::DragValue::new(&mut v);
                ids.push(put_clipped(ui, Id::new("spinner"), rect, Rect::EVERYTHING, drag).id);
            });
            out.textures_delta.clear();
        }
        assert_eq!(ids[0], ids[1]);
    }
}