1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
//! `BrepApp` — the THIN shell of the engine-native UI.
//!
//! One eframe [`App`](eframe::App) that hosts the EXISTING `brep-render` engine and lays out
//! the panels. The heavy lifting lives in focused modules; this file only owns
//! the shell:
//!
//! * [`Documents`] (`crate::document`) holds the OPEN MODELS — one
//! [`EngineState`] per document plus its identity, exactly one active. Panels
//! borrow the active engine through `self.docs.engine_mut()`, which borrows
//! only that FIELD and so still composes with the disjoint panel borrows
//! beside it. [`EngineState`] (`brep-render`) is still the single
//! windowing-agnostic BRAIN per document (scene / camera / controls / settings
//! / widgets + pointer/wheel/viewcube/pick); we do NOT fork it.
//! * [`crate::viewport::Viewport`] draws + drives the central 3D viewport (the
//! offscreen texture, the `egui_wgpu` blit callback, input routing).
//! * [`crate::panels`] — one module per left-panel section, each a small state
//! struct + a `show(&mut self, ui, state, …)` method. Adding a panel = add
//! `panels/<name>.rs`, one field here, one `self.<name>.show(…)` call in
//! [`eframe::App::ui`] below (see `README.md` → "Adding a panel").
//!
//! Native (`run_native`) and wasm (`WebRunner`) run this SAME code.
use crate::document::{Documents, EngineFactory};
use crate::panels::assembly_constraints::AssemblyConstraintsPanel;
use crate::panels::bug_report::BugReportPanel;
use crate::panels::wire_harness::WireHarnessPanel;
use crate::panels::component_actions::ComponentActionRequest;
use crate::panels::context_bar::ContextBarPanel;
use crate::panels::mode_bar::ModeBar;
use crate::panels::expressions::ExpressionsPanel;
use crate::panels::file::{FileAction, FileDialog};
use crate::panels::history::HistoryPanel;
use crate::panels::info_windows::InfoWindows;
use crate::panels::scene::ScenePanel;
use crate::panels::selection::SelectionPanel;
use crate::panels::sketch::SketchPanel;
use crate::panels::sketch3d::Sketch3dPanel;
use crate::panels::part_properties::PartPropertiesPanel;
use crate::panels::settings::SettingsPanel;
use crate::panels::toasts::Toasts;
use crate::panels::toolbar::ToolbarPanel;
use crate::panels::workbench_toolbar::WorkbenchToolbarPanel;
use crate::panels::update_components::UpdateComponents;
use crate::panels::bom::BomPanel;
use crate::panels::dock::{DockContext, DockState, PaneKind};
use crate::panels::document_tabs;
use crate::store::{default_model_store, ModelStore, SETTINGS_KEY};
use crate::viewport::Viewport;
use brep_render::engine_state::EngineState;
use brep_render::style::ThemeMode;
use eframe::egui;
/// A measured camera spin: an orbit drag the app steps ITSELF, one pointer move
/// per frame, from `perf_spin`. See `BrepApp::spin` for why it is not driven
/// from outside.
///
/// The steps run through the same `EngineState::pointer_down/move/up` the
/// viewport's real drag uses (see `viewport::interaction`), so a spin costs
/// exactly what dragging the mouse costs — no shortcut past the controls.
#[cfg(feature = "automation")]
pub struct Spin {
/// Pointer steps still to make; the drag ends (pointer_up) on the last one.
pub left: u32,
/// Where the drag started, in viewport-local logical px.
pub origin: (f64, f64),
/// Pointer step, in viewport-local logical px per frame.
pub step: (f64, f64),
/// Steps already made — the drag is a straight sweep from `origin`, and
/// reversing at the halfway mark keeps it inside the viewport however many
/// frames are asked for.
pub made: u32,
/// False until the first frame has pressed the button.
pub pressed: bool,
}
pub struct BrepApp {
/// Every OPEN MODEL and which one is active. Each document owns a full
/// `EngineState` (the shared viewer brain `desktop.rs` / the wasm shell
/// wrap) plus its file identity; panels borrow the active one.
pub(crate) docs: Documents,
/// The central 3D viewport: engine render core + offscreen texture + blit +
/// input routing.
pub(crate) viewport: Viewport,
/// The single persistence seam for settings, layout, and model documents
/// (native filesystem / wasm IndexedDB + download/upload).
pub(crate) model_store: Box<dyn ModelStore>,
// --- one small state value per panel --------------------------------------
/// Top toolbar: undo/redo, wireframe toggle, zoom-to-fit + standard views,
/// and the File-actions seam (owned by the concurrent file panel).
pub(crate) toolbar: ToolbarPanel,
/// New / Open / Save / Save As of the model document (the `.nbrep`
/// recipe) — a reusable modal file dialog opened from the toolbar.
file: FileDialog,
stl_import: Option<crate::panels::stl_import::StlImportPreview>,
/// The "Submit Bug" flow: on the toolbar bug button it screenshots the app
/// (UI + 3D model) BEFORE its own dialog opens, then collects a description
/// (+ optional email) and POSTs the model + screenshot to the public reports
/// endpoint. Native + wasm, one path.
bug_report: BugReportPanel,
/// The Info window: the licences and this session's diagnostics, toggled
/// from the toolbar's info button.
info: crate::panels::info::InfoPanel,
/// What this session is running on — captured ONCE, from the adapter eframe
/// actually gave us (never re-probed), and read by BOTH the Info window and
/// the problem report so the two cannot disagree. See
/// [`crate::diagnostics`].
diagnostics: crate::diagnostics::Diagnostics,
/// Workbench actions toolbar: the second top strip under the primary
/// toolbar — one button per feature the active workbench offers, plus the
/// constraint types where the Constraints panel is shown. Gated by the
/// `showWorkbenchToolbar` setting and hidden in the special modes. A click
/// flows out as the type to add; the shell adds it through the SAME paths
/// the palette / context bar use.
workbench_toolbar: WorkbenchToolbarPanel,
/// Display-settings + per-solid color panel (Phase 1): a FLOATING window
/// (movable + resizable, toggled from the toolbar gear button), no longer a
/// left-panel section.
settings: SettingsPanel,
pub(crate) plugins: crate::plugins::PluginsPanel,
pub(crate) javascript: crate::javascript::JavaScriptEditor,
/// The active document's OWN BOM attributes (Part Number, Material, Mass,
/// …) as a floating window, toggled from the toolbar's Properties button.
/// Document-level, not selection-level: entity inspection is the context
/// bar's `info_windows`.
part_properties: PartPropertiesPanel,
/// The Replace component chooser + result summary.
replace_dialog: crate::panels::replace_component::ReplaceDialog,
/// History feature-tree + schema-driven feature dialog panel (Phase 2).
/// NOTE: the editable history is NOT owned here — it lives in the engine core
/// (`EngineState.history`), the single source of truth; this panel only reads
/// it back to draw and calls the engine's `history_*` methods to mutate.
history: HistoryPanel,
/// Scene tree ("Scene Manager"): the display scene as a file-tree — per-solid
/// visibility + Faces/Edges/Vertices with two-way selection sync. Reads the
/// engine scene/emphasis; owns only transient expand + hit state.
scene: ScenePanel,
/// Assembly Structure tree (claimed by the Assembly workbench): a VIEW over
/// the scene's component records — per-instance fixed/visibility/status
/// adornments, actions routed to the owning ACOMP feature.
/// The BOM panel (claimed by the Assembly workbench): the parts list on
/// the shared column-tree widget, with the editable part/occurrence
/// attribute columns the Settings "Assemblies" section configures.
bom: BomPanel,
/// Assembly Constraints panel (claimed by the Assembly workbench): the
/// schema-driven constraint collection widget + Solve/auto-solve/DOF header.
assembly_constraints: AssemblyConstraintsPanel,
/// The wire-harness connection list (a Wire harness workbench pane): add /
/// edit / remove wires, read their routed length + status, hover to
/// highlight. Document data lives in the engine; this holds the widget's
/// transient state.
wire_harness: WireHarnessPanel,
/// The PMI view tree + annotation forms (the PMI workbench's pane).
pmi: crate::panels::pmi::PmiPanel,
/// The CAM browser tree (the CAM workbench's pane): setups and their
/// operations, NC programs and the tool library over the document's
/// `cam` block.
pub(crate) cam: crate::panels::cam::CamPanel,
pub(crate) instructions: crate::panels::instructions::InstructionsPanel,
pub(crate) instructions_player: crate::panels::instructions_player::InstructionsPlayerPanel,
/// The Simulation workbench's pane: the studies and the active timeline.
pub(crate) simulation: crate::panels::simulation::SimulationPanel,
/// The drawing sheets + their forms (the Drawing workbench's pane).
sheets: crate::panels::sheets::SheetsPanel,
/// Update-components checker: compares each parts-library entry's
/// `sourceSignature` against the model store's current content. Kept
/// current once per frame (cheap generation key: applied run + store
/// save); the constraints header reads the count + runs the batch refresh,
/// the structure tree reads per-part badges.
update_components: UpdateComponents,
/// Expressions / parameters panel: the variable sheet (engine-owned history
/// `expressions`) feature params reference. Owns only its editor buffer.
expressions: ExpressionsPanel,
/// Qualify: the part's declared CONNECTION POINTS, on whichever surface is
/// on screen (symbol, pads or 3D). Holds only the selected address; the
/// block and both consistency reports come from the engine.
qualify: crate::panels::qualify::QualifyPanel,
/// The family table editor: a `.fbrep` family seed's member table. Holds
/// the selection, the cell being typed and the last Generate report; the
/// table itself is the document's `familyTable` block.
family_table: crate::panels::family_table_editor::FamilyTableEditor,
/// Every PLM panel (`panels::plm_host`): empty in a file-based session.
pub(crate) plm: crate::panels::plm_host::PlmHost,
/// Info windows: MULTIPLE pinned per-entity inspector windows opened from the
/// selection-driven context bar's Info action. Each floating (movable +
/// resizable) window is PINNED to one object name at open time — a Metadata
/// (editable attribute) tab + a read-only Info (measurements + provenance) tab —
/// and keeps showing that entity regardless of later selection changes. Replaces
/// the old single Properties window.
info_windows: InfoWindows,
/// Interference results window: opened by the Assembly workbench's `∩`
/// toolbar button, which runs the engine's pairwise-intersect check; a
/// floating window like the Info windows with a row per interfering pair
/// (click = select both components), a green all-clear pass line, and a
/// Re-run button.
interference: crate::panels::interference::InterferenceWindow,
/// Auto Constraints window (Assembly workbench): opened by the `⚿` toolbar
/// button, it lists the constraint types the kernel's inference lane can
/// read out of the components' current placement, with what a scan found
/// for each, and creates the whole accepted set as one undo step.
auto_constraints: crate::panels::auto_constraints::AutoConstraintsWindow,
/// step.parts online model library browser (Assembly workbench): a ctx-level
/// window (opened by the library toolbar button) that searches the public
/// step.parts v1 API, shows results with thumbnails, and imports a chosen
/// STEP model as a new part document + adds it to the assembly as an ACOMP.
step_parts: crate::panels::step_parts::StepPartsPanel,
/// Selection panel: the pickable-kinds filter (which entity kinds a viewport
/// click may select — honored by the engine's `select_top_at`). The filter +
/// selection live in `EngineState`; this panel only reads/writes them.
selection: SelectionPanel,
/// Context action toolbar: the selection-driven action bar (Clear / Hide /
/// Edit-owning-feature + the feature-from-selection actions whose primary
/// reference accepts the selected kind). Shown only while something is
/// selected; drives the engine directly and returns a feature id for the shell
/// to expand in the history tree.
context_bar: ContextBarPanel,
/// Sketch (S0): a seeded, read-only sketch preview — pushes a solved rectangle
/// + circle to the `set_overlay` channel colored by solver mobility, and shows
/// the DOF status readout. The engine-native sketcher's foundation surface.
sketch: SketchPanel,
sketch3d: Sketch3dPanel,
/// Special-mode EXIT controls (Finish/Cancel), always pinned to the top-right
/// corner — a card-based reference pick, sketch mode, and any future
/// special mode. A form field's pick draws no card: its field is the UI.
mode_bar: ModeBar,
pick_bar: crate::panels::pick_bar::PickBar,
/// Transient toast overlay: drains the engine's queued notices each frame
/// (e.g. a sketch solve that failed after an edit) and shows each briefly.
pub(crate) toasts: Toasts,
/// The status bar's app-wide working indicator (`panels::busy`): what is
/// in flight, collected from every source each frame.
busy: crate::panels::busy::BusyIndicator,
/// Dockable / tabbed side-panel layout (egui_tiles): the shared, persisted
/// tree that hosts every side-panel section AND the 3D viewport as tiles the
/// user can split, tab, resize, and drag-rearrange. Owns the layout; borrows
/// each panel + the engine per frame through [`DockContext`]. Drawn in normal
/// modeling mode; sketch / ref-select mode bypasses it (viewport drawn direct).
dock: DockState,
/// Whether the ONE-SHOT first-model framing has fired. The seed run is async
/// under a background runner (native thread / wasm worker), so the boot
/// `zoom_to_fit` can run before the first solids exist → an unframed first
/// model. Once the seed run has landed (`has_solids() && !run_pending()`), the
/// `ui` loop frames it once and sets this. Under the synchronous Inline runner
/// (tests) the scene is already populated, so this fires on the very first frame.
first_run_framed: bool,
/// A model fetch kicked off at boot from a `?loadModel=<url>` query param
/// (wasm only). When the fetch lands it REPLACES the seed model. `None` on
/// native and once applied.
pending_boot_load: Option<std::sync::mpsc::Receiver<Result<String, String>>>,
/// The PLM tab's Connect now, on its way: the sign-in and index load run on
/// the frame loop's executor and the frame switches stores when they end.
#[cfg(not(target_arch = "wasm32"))]
reconnecting: Option<crate::store::PendingBoot>,
/// "Open in CAD" from the PLM's page: the `?open=part/…/rev/…` this page
/// was started with (wasm only), driven once a frame until it settles.
plm_launch: Option<crate::panels::plm_launch::Launch>,
/// The document handle the shared panels were last reset for. Compared to
/// `docs.view_id()` at the top of every frame: ONE check catches a switch
/// from any source (a tab click, a close, New, Open, Edit Part, a Work
/// part entered or left in place) instead of a hook per call site, and it
/// runs BEFORE any panel draws this frame.
active_document: u64,
/// The TAB the panels were last drawn for. A Work part entered or left in
/// place changes the view but not the tab, and the BOM — the assembly's
/// product structure in both — keeps its packed/expanded state.
active_tab: u64,
/// Whether the active workbench showed the Sheets pane LAST frame — the
/// edge the paper is closed on. Closing is a transition, not a condition:
/// a sheet opened while another workbench is active (a command, a script)
/// stays open until the Drawing workbench is LEFT, exactly as PMI's view
/// deactivates on leaving rather than on every frame outside it.
sheets_pane_shown: bool,
/// The DOCUMENT TAB STRIP's per-tab hit-rects from the last dock frame,
/// published for the headed verifier. The strip is drawn inside the dock's
/// viewport pane, so its rects have to ride back out through the outcome.
document_tab_hits: Vec<(String, egui::Rect)>,
/// The `__brepParams` blob and the document state it was built from:
/// `(document, rolled-to step, history revision, json)`.
///
/// The blob is the rolled-to feature's whole `inputParams`, and for an
/// `IMPORT3D` those params carry the embedded model text — 681 KB on the
/// heaviest corpus model. It was rebuilt EVERY frame, which on the browser
/// build (where the state registry is always on) also transcoded it into a
/// fresh JS string every frame, on the one thread that also has to answer
/// the pointer. Nothing reads it per frame; it only has to be correct when
/// someone asks. The revision is a mutation counter the document hands out
/// (`EngineState::history_revision`), so this cannot go stale without the
/// borrow checker having let a mutation past the door that bumps it.
params_blob: (u64, usize, u64, String),
/// Plain-state blobs rebuilt only when their whole input changed (see
/// [`StateBlobCache`]).
state_blob_cache: StateBlobCache,
/// A measured camera spin in flight (`perf_spin`): the orbit drag the app
/// drives ITSELF, one pointer step per frame, so the frames run back to back.
/// Driving it from a host instead would put a round trip between every pair
/// of frames, and `dt` — the frame period, the number a user feels as a
/// freeze — would measure the host's latency rather than the app's.
#[cfg(feature = "automation")]
spin: Option<Spin>,
/// The debounced autosave of every dirty document (`crate::recovery`), and
/// the boot-time **Recover unsaved work?** prompt its blob feeds. The
/// autosave is held while the prompt is open: the clean seed tab would
/// otherwise remove the very blob being offered.
autosave: crate::recovery::Autosave,
/// Closing (the window's close button, or `app_close`): when it was asked
/// (egui clock). The close waits until the store's writes have landed.
closing: Option<f64>,
/// A close the automation layer asked for, taken by the next frame.
pub(crate) close_asked: bool,
/// The app let the window close: nothing was left on its way.
closed: bool,
/// Where a close stands once the store's writes have settled: `0` not
/// yet, `1` one frame given to the failure drain (a write that failed
/// makes its tab unsaved), `2` the recovery blob written again.
close_stage: u8,
recovery: crate::recovery::RecoveryPanel,
/// The desktop update check and its notice. Inert until the desktop
/// binary's `main` starts it, so a harness or a test never checks.
#[cfg(not(target_arch = "wasm32"))]
updater: crate::update::Updater,
/// The reports this user submitted, checked for resolution
/// (`crate::report_followup`). Inert until the entry point starts it.
report_followup: crate::report_followup::ReportFollowup,
/// The UI zoom scale CURRENTLY applied to the egui context. Tracks
/// `settings.ui_scale` but is only synced to it while the pointer is up, so
/// dragging the Settings "UI scale" slider doesn't rescale the whole UI under
/// the cursor mid-drag — the settled value is committed on release. See the
/// zoom-apply block in `ui`.
applied_ui_scale: f32,
/// The automation channel (hosts submit commands; the frame drains them at
/// three fixed points — see `automation::queue`).
#[cfg(feature = "automation")]
pub(crate) automation: std::sync::Arc<crate::automation::queue::AutomationQueue>,
}
/// The caption text size, points (egui's own `Small` is 9). See `app_style`.
pub const SMALL_TEXT_SIZE: f32 = 10.5;
/// The app's tweaks over egui's style, applied to both themes at startup.
pub(crate) fn app_style(style: &mut egui::Style) {
// Keep overflow discoverable without hovering or scrolling first, in
// both themes and every child UI (including eCAD and plugin panels).
// ScrollArea's default VisibleWhenNeeded still hides bars that aren't needed.
style.spacing.scroll = egui::style::ScrollStyle::solid();
// Captions (`ui.small`, `RichText::small`): egui's 9 pt was too small to
// read beside 12.5 pt body text in the Info window, the eCAD inspectors and
// the Selection actions summary (lane U audit item 9). 10.5 keeps them
// visibly secondary. The toolbar/Ribbon group captions stay at 9
// (`ribbon::CHROME_CAPTION_SIZE`) so the chrome keeps its height.
style.text_styles.insert(egui::TextStyle::Small, egui::FontId::new(SMALL_TEXT_SIZE, egui::FontFamily::Proportional));
}
/// Land a `?loadModel=<url>` fetch (`result`: the fetched text, or why the
/// fetch failed) on the active document.
///
/// The model REPLACES the active document in place rather than adding a tab.
///
/// A fetch that fails, or a document the engine rejects (it does not parse,
/// or the history is locked), is shown to the user as an error toast with
/// the reason; it used to be dropped silently, leaving the seed cube on
/// screen as if it were the linked model. The untouched boot seed (clean
/// and unnamed) is then emptied, so nothing on screen passes for the model
/// that did not open. A named document (a file the user opened while the
/// fetch was pending) or one with unsaved edits is left as it is: emptying
/// a named one would let its next Save overwrite the user's file.
/// A document that loads with some features in error is NOT a failure: it
/// opens with those features marked in the tree.
pub(crate) fn apply_linked_model(docs: &mut crate::document::Documents, result: Result<String, String>) {
// Read before the load: a rejected document can already have reset parts
// of the engine (its metadata, its workbench) on the way to the refusal.
let untouched_seed = !docs.active().is_dirty() && docs.active().name().is_none();
let failure = match result {
// Text that is not JSON at all (an HTML page served 200, a truncated
// body) is refused before the engine sees it: `set_history_json`
// resets per-document state before its own parse fails.
Ok(json) => match serde_json::from_str::<serde::de::IgnoredAny>(&json) {
Err(error) => format!("the document could not be opened: history parse: {error}"),
Ok(_) => match docs.engine_mut().load_model_and_fit(&json) {
Ok(_) => {
docs.active_mut().mark_clean();
return;
}
Err(error) => format!("the document could not be opened: {error}"),
},
},
Err(error) => format!("the download failed: {error}"),
};
docs.engine_mut().push_notice(format!("Could not open the linked model: {failure}"));
if untouched_seed {
if let Err(error) = docs.engine_mut().set_history_json(crate::document::EMPTY_DOCUMENT) {
log::error!("Clearing the seed after a failed linked model: {error}");
}
docs.active_mut().mark_clean();
}
}
/// How long a close waits for writes still on their way to the store (a PLM
/// server that stopped answering must not keep the app open forever).
const CLOSE_WAIT_SECS: f64 = 30.0;
impl BrepApp {
/// Closing the window. What is unsaved is written for recovery at once
/// (the next start offers it, as after a crash), and the window stays open
/// until every write the store issued (a Save, that recovery blob) has
/// landed: a write-behind store would otherwise lose a Save made just
/// before. After [`CLOSE_WAIT_SECS`] it closes anyway.
fn close_when_written(&mut self, ctx: &egui::Context) {
let asked = ctx.input(|i| i.viewport().close_requested()) || std::mem::take(&mut self.close_asked);
if self.closed {
return;
}
let now = ctx.input(|i| i.time);
if asked && self.closing.is_none() {
if !self.recovery.is_open() {
self.autosave.flush(&self.docs, self.model_store.as_ref());
}
self.closing = Some(now);
}
let Some(since) = self.closing else { return };
let pending = self.model_store.pending_writes();
let timed_out = now - since > CLOSE_WAIT_SECS;
if pending == 0 || timed_out {
match self.close_stage {
// The writes settled. A failed one (the server down) makes its
// tab unsaved when the failures are drained, later in this
// frame: give that a frame…
0 => self.close_stage = 1,
// …then write the recovery blob again, so a Save that never
// reached the server is offered at the next start.
1 => {
if !self.recovery.is_open() {
self.autosave.flush(&self.docs, self.model_store.as_ref());
}
self.close_stage = 2;
}
_ if self.model_store.pending_writes() == 0 || timed_out => {
self.closed = true;
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
return;
}
_ => {}
}
ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
ctx.request_repaint();
return;
}
ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
egui::Area::new(egui::Id::new("brep-closing"))
.anchor(egui::Align2::CENTER_CENTER, egui::Vec2::ZERO)
.order(egui::Order::Foreground)
.show(ctx, |ui| {
egui::Frame::popup(ui.style()).show(ui, |ui| {
ui.horizontal(|ui| {
ui.spinner();
ui.label(format!("Finishing {pending} save(s) before closing…"));
});
});
});
ctx.request_repaint_after(std::time::Duration::from_millis(50));
}
/// The close state, for automation (`__brepClosing`).
pub(crate) fn closing_json(&self) -> String {
serde_json::json!({
"closing": self.closing.is_some(),
"closed": self.closed,
"pending": self.model_store.pending_writes(),
})
.to_string()
}
/// The automation queue a host submits commands to.
#[cfg(feature = "automation")]
pub fn automation(&self) -> &std::sync::Arc<crate::automation::queue::AutomationQueue> {
&self.automation
}
/// Open a file named on the command line — `brep-app model.nbrep`, which
/// is also what a desktop file association hands us (`Exec=… %f`).
///
/// Routed by extension (any case):
/// * a model document (`.nbrep`, `.fbrep`, `.tbrep`, or a name with no
/// extension) goes through the same `FileDialog::open_document` door as
/// File>Open, so the document keeps the full path as its identity and a
/// later plain Save writes back to the file the user double-clicked,
/// rather than to a copy in the app's own models directory;
/// * a file File > Import takes (STEP, IGES, STL, 3MF, OBJ, KiCad — the
/// import table in `panels/file.rs`) is imported by that same importer
/// into a NEW untitled tab named after the file's stem, so a plain Save
/// asks for a name instead of writing a document over the STEP file;
/// * anything else is refused by name.
///
/// A path that does not open or import leaves the status line and a
/// notice saying so, and the seed document in place — a bad argument must
/// not cost the user a window.
///
/// Native only: called once, before the first frame.
#[cfg(not(target_arch = "wasm32"))]
pub fn open_path_from_command_line(&mut self, path: &std::path::Path) {
// Absolute, so the store resolves it as a real file rather than as a
// name inside the models directory (`store::resolve` routes anything
// with a separator straight to the filesystem).
let path = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
let Some(name) = path.to_str() else {
log::warn!("brep-app: {} is not valid UTF-8; not opening it", path.display());
return;
};
match command_line_lane(name) {
CommandLineLane::Open => {
// Field-by-field, because `open_document` borrows the documents
// and the store while `self.file` is borrowed mutably.
let Self { file, docs, model_store, .. } = self;
file.open_document(docs, model_store.as_ref(), name);
}
CommandLineLane::Import => match std::fs::read(&path) {
Ok(bytes) => {
if let Err(e) = self.import_file_into_new_tab(name, bytes) {
log::warn!("brep-app: {e}");
}
}
Err(e) => self.refuse_command_line_path(format!("import failed: cannot read {name}: {e}")),
},
CommandLineLane::Refuse => self.refuse_command_line_path(format!(
"cannot open {name}: not a BREP document (.nbrep, .fbrep, .tbrep) or a file BREP imports (STEP, IGES, STL, 3MF, OBJ, KiCad)"
)),
}
}
/// Import a file (its path or name, and its bytes) as File > Import does,
/// into a new untitled tab named after it: the door a STEP or mesh file
/// opened from the OS takes, and the automation's `doc_open_with`.
pub(crate) fn import_file_into_new_tab(&mut self, name: &str, bytes: Vec<u8>) -> Result<(), String> {
let Self { file, docs, model_store, .. } = self;
file.import_into_new_tab(docs, model_store.as_ref(), name, bytes)
}
/// Say why a command-line path was not opened: the status line and a
/// notice on the seed document, which stays as it was.
#[cfg(not(target_arch = "wasm32"))]
fn refuse_command_line_path(&mut self, message: String) {
log::warn!("brep-app: {message}");
self.docs.engine_mut().push_notice(message.clone());
self.file.status = message;
}
/// The 3D viewport rect of the last frame, in egui points.
pub fn view_rect(&self) -> Option<egui::Rect> {
self.viewport.last_rect()
}
/// The session's store, read-only (the automation layer's `frame_info`
/// counts its writes still on their way).
pub(crate) fn model_store(&self) -> &dyn ModelStore {
self.model_store.as_ref()
}
/// Why this session cannot switch stores live, or `None` when it can: every
/// open document untitled and clean, the state a fresh start has. A named
/// document's identity belongs to the store it came from, and a dirty one's
/// work has nowhere to go in a new store.
pub(crate) fn reconnect_blocker(&self) -> Option<String> {
self.reconnect_blocker_with(crate::document::Document::is_dirty)
}
fn reconnect_blocker_with(&self, is_dirty: impl Fn(&crate::document::Document) -> bool) -> Option<String> {
let named = self.docs.iter().filter(|d| d.name().is_some()).count();
let dirty = self.docs.iter().filter(|d| is_dirty(d)).count();
match (named, dirty) {
(0, 0) => None,
(_, 0) => Some(format!("{named} document(s) from this store are open: close them, then Connect now (or restart)")),
_ => Some(format!("{dirty} document(s) have unsaved work: save or close them, then Connect now (or restart)")),
}
}
/// Start switching this session to the PLM configured at `root`, live
/// (S1's named limit): the native boot S2 runs at start, without blocking
/// the frame; [`Self::finish_reconnect`] takes the store when it ends.
/// Only while [`Self::reconnect_blocker`] is `None`.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn start_reconnect(&mut self, root: std::path::PathBuf) -> Result<(), String> {
if self.reconnecting.is_some() {
return Err("already connecting".into());
}
if let Some(why) = self.reconnect_blocker() {
return Err(why);
}
let config = crate::plm::config::resolve(&root, &|_| None, &Default::default())?
.ok_or_else(|| format!("no PLM is configured in {}", root.display()))?;
self.reconnecting = Some(crate::store::start_native_boot(root, config));
Ok(())
}
/// A Connect now is on its way (settle waits for it).
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn reconnecting(&self) -> bool {
self.reconnecting.is_some()
}
/// The Connect now under way, once its boot has ended: switch stores (or
/// say why not) and tell the PLM tab.
#[cfg(not(target_arch = "wasm32"))]
fn poll_reconnect(&mut self) {
let Some(store) = self.reconnecting.as_mut().and_then(crate::store::PendingBoot::poll) else { return };
self.reconnecting = None;
let outcome = self.finish_reconnect(store);
self.settings.reconnected(outcome);
}
/// Switch this session to `store`, the boot a Connect now started: then
/// everything the app read from the store when it was built is read again
/// from the new one. A refused boot, or a session that took a named or
/// dirty document meanwhile, changes nothing and says why.
#[cfg(not(target_arch = "wasm32"))]
fn finish_reconnect(&mut self, store: Box<dyn ModelStore>) -> Result<String, String> {
use crate::plm::connection::Status;
if let Some(why) = self.reconnect_blocker() {
return Err(why);
}
let Some(Status::Connected { url, username }) = store.plm_session().map(|s| s.status) else {
let why = store.take_persistence_errors().join("; ");
return Err(if why.is_empty() { "the PLM did not take this session".into() } else { why });
};
self.model_store = store;
self.docs.set_canonicalizer(self.model_store.canonicalizer());
// Settings: every open engine, and every later tab, starts from the
// defaults with the new store's settings over them, as a fresh boot
// would; nothing of the old store's carries over.
let saved = self.model_store.read(SETTINGS_KEY);
let fresh = move |mut state: EngineState| {
state.settings = brep_render::style::RenderSettings::default();
let defaults = state.settings.to_json();
let _ = state.apply_settings_json(&defaults);
if let Some(saved) = &saved {
let _ = state.apply_settings_json(saved);
}
state
};
for doc in self.docs.iter_mut() {
let engine = std::mem::replace(&mut doc.engine, EngineState::new());
doc.engine = fresh(engine);
}
self.docs.wrap_engine_factory(fresh);
self.dock = DockState::new(self.model_store.as_ref());
self.recovery.arm_from_store(
self.model_store.as_ref(),
self.docs.engine().settings.disable_recovery_prompt,
);
self.file = FileDialog::new();
self.update_components = UpdateComponents::new();
Ok(format!("connected to {url} as {username}"))
}
/// Keep automation waits active until the File dialog has delivered the PDF.
pub(crate) fn instructions_pdf_pending(&self) -> bool {
self.file.instructions_pdf_pending()
}
/// Keep automation waits active until a PLM Save As has landed or failed.
pub(crate) fn plm_save_as_pending(&self) -> bool {
self.file.plm_save_as_pending()
}
/// The file dialog, the documents and the store together — for the
/// automation commands that drive a document-class flow (Generate, a
/// store read-back) exactly as the panels do.
pub(crate) fn file_docs_store(&mut self) -> (&mut FileDialog, &mut Documents, &dyn ModelStore) {
(&mut self.file, &mut self.docs, self.model_store.as_ref())
}
/// The active document's engine.
pub fn docs_engine(&self) -> &EngineState {
self.docs.engine()
}
/// Open the mesh import preview for the file `name` (`.stl` or `.3mf`)
/// against the active document — what activating it in the Import explorer
/// does. The preview replaces the shell until it is accepted or cancelled.
pub fn open_mesh_preview(&mut self, name: String, bytes: Vec<u8>) -> Result<(), String> {
use brep_render::runner::MeshImportFormat;
let format = match name.rsplit('.').next().map(str::to_ascii_lowercase).as_deref() {
Some("stl") => MeshImportFormat::Stl,
Some("3mf") => MeshImportFormat::ThreeMf,
_ => return Err(format!("{name} is not an STL or 3MF file")),
};
self.start_mesh_preview(format, name, bytes);
Ok(())
}
fn start_mesh_preview(&mut self, format: brep_render::runner::MeshImportFormat, name: String, bytes: Vec<u8>) {
self.stl_import = Some(crate::panels::stl_import::StlImportPreview::new(
self.docs.active_id(), format, name, bytes, self.docs.spawn_engine(),
));
self.viewport.forget_document();
}
/// Whether the mesh import preview is open.
pub fn mesh_preview_open(&self) -> bool {
self.stl_import.is_some()
}
/// The engine the 3D viewport is drawing: the mesh import preview's own
/// while it is open (it replaces the shell), else the active document's.
/// What a pointer script's `locate` must project through.
pub fn viewport_engine(&self) -> &EngineState {
match &self.stl_import {
Some(preview) => &preview.engine,
None => self.docs.engine(),
}
}
pub fn new(cc: &eframe::CreationContext<'_>) -> Result<Self, String> {
Self::new_with(cc, crate::automation::AppOptions::default())
}
/// Build the app with host-supplied options: an isolated store (a host
/// MUST pass one) and whether to start on the seed model.
pub fn new_with(cc: &eframe::CreationContext<'_>, opts: crate::automation::AppOptions) -> Result<Self, String> {
// Solid scroll bars and readable captions, in both themes.
cc.egui_ctx.all_styles_mut(app_style);
// The action registry's view of each pass (`activate_widget`).
#[cfg(feature = "automation")]
crate::automation::actions::install(&cc.egui_ctx);
let crate::automation::AppOptions { store: opt_store, seed } = opts;
let render_state = cc
.wgpu_render_state
.as_ref()
.ok_or_else(|| "eframe was not created with a wgpu render state".to_string())?;
// The viewport owns the render core + blit pipeline, built from eframe's
// SHARED device/queue/format.
let viewport = Viewport::new(render_state);
// The session's diagnostics, taken from that same render state: the
// adapter about to draw every frame is the one a report must name. The
// WebGPU-vs-WebGL2 decision has already been made by the time we get
// here (eframe drops `BROWSER_WEBGPU` from the backend set when the
// browser offers no WebGPU adapter), so this READS the outcome — asking
// again later would be a second question with its own answer.
let diagnostics = crate::diagnostics::Diagnostics::from_render_state(render_state);
// --- storage seam: load the persisted settings ------------------------
let model_store = opt_store.unwrap_or_else(default_model_store);
// wasm: hand the store the egui context so an async file-upload load
// callback can wake the reactive frame loop (see `store::set_repaint_ctx`).
#[cfg(target_arch = "wasm32")]
crate::store::set_repaint_ctx(cc.egui_ctx.clone());
let saved_settings = model_store.read(SETTINGS_KEY);
// --- how a document's engine is built --------------------------------
// Every tab gets its OWN engine, and therefore its own history runner —
// a runner owns the resident kernel state of the document it executes,
// so one shared between documents would apply a background run against
// the wrong registry. See `crate::document`.
let plugins = crate::plugins::PluginsPanel::new(model_store.as_ref());
let javascript = crate::javascript::JavaScriptEditor::new(model_store.as_ref());
let plugin_packages = plugins.packages.clone();
let engine_factory: EngineFactory = Box::new(move || {
let mut state = EngineState::new();
// Native: run the whole history — and per-object measurement queries — on a
// persistent background thread so the UI never freezes during a run or a
// selection (M2b). Installed BEFORE anything loads so it builds through it.
#[cfg(not(target_arch = "wasm32"))]
state.set_runner(Box::new(brep_render::runner::ThreadRunner::new()));
// wasm: the browser-thread analogue — a dedicated web worker (M3b) so the
// single-threaded wasm UI stays responsive during a run. Same seam. Tests
// (which never hit this wasm path) keep the default synchronous InlineRunner.
#[cfg(target_arch = "wasm32")]
state.set_runner(Box::new(crate::worker::WorkerRunner::new()));
if !plugin_packages.borrow().is_empty() {
if let Err(error) = crate::plugins::restore(&mut state, &plugin_packages.borrow()) {
log::error!("Plugin restore failed: {error}");
}
}
state.set_viewcube_enabled(true);
// Partial-override apply: unknown/absent keys keep their defaults.
if let Some(saved) = &saved_settings {
let _ = state.apply_settings_json(saved);
}
state
});
// Start with the seed model on every launch (7626103c2). Reopening the
// previous session can immediately rerun a problematic document and
// prevent the user from recovering by restarting the app, so which
// documents were open is not persisted at all: saved models come back
// through the file dialog, and work that was never saved comes back as
// the crash-recovery OFFER below — a prompt, never an automatic load.
let mut docs = Documents::new(engine_factory);
docs.set_canonicalizer(model_store.canonicalizer());
if starts_on_seed_model(seed, &docs.engine().settings) {
let _ = docs.engine_mut().set_history_json(&seed_history_json());
}
docs.engine_mut().zoom_to_fit();
docs.active_mut().mark_clean();
// The autosave blob from a session that ended with unsaved work: offer
// it back (a prompt, never an automatic restore — see `crate::recovery`).
let mut recovery = crate::recovery::RecoveryPanel::new();
recovery.arm_from_store(model_store.as_ref(), docs.engine().settings.disable_recovery_prompt);
// The settings panel seeds its working JSON from the (post-load) engine
// settings so the widgets reflect the persisted state on first paint.
let settings = SettingsPanel::new();
let part_properties = PartPropertiesPanel::new();
// New / Open / Save / Save As. Holds no document identity — that lives
// on each `Document`.
let file = FileDialog::new();
// Boot at the saved UI scale.
let applied_ui_scale = docs.engine().settings.ui_scale;
let active_document = docs.active_id();
// The dock layout (loads the persisted tree, or the default). Built before
// `model_store` is moved into `Self`.
let dock = DockState::new(model_store.as_ref());
// Boot-load: if the page URL carries `?loadModel=<url>` (wasm only), start
// fetching that model NOW; the seed still loads this frame and the fetched
// model REPLACES it when it lands (drained in `ui`). See the drain block.
crate::offsite::prefetch();
#[cfg(target_arch = "wasm32")]
let pending_boot_load = web_sys::window()
.and_then(|w| w.location().search().ok())
.and_then(|search| web_sys::UrlSearchParams::new_with_str(&search).ok())
.and_then(|params| params.get("loadModel"))
.filter(|url| !url.is_empty())
.map(|url| crate::offsite::fetch_text(&cc.egui_ctx, "Opening a model from a link", url));
#[cfg(not(target_arch = "wasm32"))]
let pending_boot_load: Option<std::sync::mpsc::Receiver<Result<String, String>>> = None;
#[cfg(target_arch = "wasm32")]
let plm_launch = crate::panels::plm_launch::Launch::from_page();
#[cfg(not(target_arch = "wasm32"))]
let plm_launch = None;
Ok(Self {
docs,
viewport,
toolbar: ToolbarPanel::new(),
workbench_toolbar: WorkbenchToolbarPanel::new(),
model_store,
file,
stl_import: None,
bug_report: BugReportPanel::new(),
info: crate::panels::info::InfoPanel::new(),
diagnostics,
settings,
plugins,
javascript,
part_properties,
replace_dialog: Default::default(),
history: HistoryPanel::new(),
scene: ScenePanel::new(),
bom: BomPanel::new(),
assembly_constraints: AssemblyConstraintsPanel::new(),
wire_harness: WireHarnessPanel::new(),
pmi: crate::panels::pmi::PmiPanel::new(),
cam: crate::panels::cam::CamPanel::new(),
instructions: crate::panels::instructions::InstructionsPanel::new(),
instructions_player: crate::panels::instructions_player::InstructionsPlayerPanel::new(),
simulation: crate::panels::simulation::SimulationPanel::new(),
sheets: crate::panels::sheets::SheetsPanel::new(),
update_components: UpdateComponents::new(),
expressions: ExpressionsPanel::new(),
qualify: crate::panels::qualify::QualifyPanel::new(),
family_table: crate::panels::family_table_editor::FamilyTableEditor::new(),
plm: crate::panels::plm_host::PlmHost::new(),
info_windows: InfoWindows::new(),
interference: crate::panels::interference::InterferenceWindow::new(),
auto_constraints: crate::panels::auto_constraints::AutoConstraintsWindow::new(),
step_parts: crate::panels::step_parts::StepPartsPanel::new(),
selection: SelectionPanel::new(),
context_bar: ContextBarPanel::new(),
sketch: SketchPanel::new(),
sketch3d: Sketch3dPanel::new(),
mode_bar: ModeBar::new(),
pick_bar: crate::panels::pick_bar::PickBar::new(),
toasts: Toasts::new(),
busy: crate::panels::busy::BusyIndicator::new(),
dock,
first_run_framed: false,
pending_boot_load,
#[cfg(not(target_arch = "wasm32"))]
reconnecting: None,
plm_launch,
active_document,
active_tab: active_document,
sheets_pane_shown: false,
document_tab_hits: Vec::new(),
params_blob: (u64::MAX, usize::MAX, u64::MAX, String::new()),
state_blob_cache: StateBlobCache::default(),
#[cfg(feature = "automation")]
spin: None,
autosave: crate::recovery::Autosave::new(),
closing: None,
close_asked: false,
closed: false,
close_stage: 0,
recovery,
#[cfg(not(target_arch = "wasm32"))]
updater: crate::update::Updater::inert(),
report_followup: crate::report_followup::ReportFollowup::new(),
applied_ui_scale,
#[cfg(feature = "automation")]
automation: {
let q = crate::automation::queue::AutomationQueue::new();
q.attach(&cc.egui_ctx);
q
},
})
}
/// Start the desktop update check (`crate::update`). Called by the
/// desktop binary's `main` only.
#[cfg(not(target_arch = "wasm32"))]
pub fn start_update_check(&mut self, ctx: &egui::Context) {
self.updater = crate::update::Updater::start(ctx);
}
/// Remember submitted problem reports and check them for resolution
/// (`crate::report_followup`). Called by the desktop `main` and the web
/// entry point only.
pub fn start_report_followup(&mut self, ctx: &egui::Context) {
self.report_followup.start(ctx);
}
/// Arm a measured camera spin: `frames` pointer steps of `step` logical px
/// each, starting at the centre of the 3D viewport. Replaces any spin in
/// flight. See [`Spin`].
#[cfg(feature = "automation")]
pub fn start_spin(&mut self, frames: u32, step: (f64, f64)) {
let rect = self.viewport.last_rect();
let origin = rect
.map(|r| (r.width() as f64 * 0.5, r.height() as f64 * 0.5))
.unwrap_or((320.0, 240.0));
self.spin = Some(Spin { left: frames.max(1), origin, step, made: 0, pressed: false });
}
/// Is a measured spin still running?
#[cfg(feature = "automation")]
pub fn spinning(&self) -> bool {
self.spin.is_some()
}
/// Advance an in-flight spin by ONE pointer step and keep the frame loop
/// awake. The drag sweeps out and back over a fixed span so it never leaves
/// the viewport however many frames are asked for; the camera keeps turning
/// either way, which is all the measurement needs.
#[cfg(feature = "automation")]
fn step_spin(&mut self, ctx: &egui::Context) {
let Some(mut spin) = self.spin.take() else { return };
{
let state = self.docs.engine_mut();
if !spin.pressed {
state.pointer_down(spin.origin.0, spin.origin.1, brep_render::controls::BUTTON_LEFT);
spin.pressed = true;
}
const SPAN: f64 = 60.0;
let phase = f64::from(spin.made % (2 * SPAN as u32));
let t = if phase <= SPAN { phase } else { 2.0 * SPAN - phase };
state.pointer_move(spin.origin.0 + spin.step.0 * t, spin.origin.1 + spin.step.1 * t);
spin.made += 1;
spin.left -= 1;
if spin.left == 0 {
state.pointer_up();
}
}
if spin.left > 0 {
self.spin = Some(spin);
}
ctx.request_repaint();
}
/// The `__brepParams` JSON, rebuilt only when the document it describes can
/// have changed. The `params_blob` FIELD carries the reason this is not
/// simply built afresh each frame.
fn params_blob(&mut self) -> &str {
let key = (
self.docs.active_id(),
self.docs.engine().history_rollback(),
self.docs.engine().history_revision(),
);
if (self.params_blob.0, self.params_blob.1, self.params_blob.2) != key {
self.params_blob = (
key.0,
key.1,
key.2,
self.docs.engine().feature_params_json(key.1),
);
}
&self.params_blob.3
}
/// Global keyboard shortcuts (egui input): **Ctrl/Cmd+Z** undo,
/// **Ctrl/Cmd+Shift+Z** or **Ctrl/Cmd+Y** redo, **Esc** clears the selection
/// — unless a popup or menu is up, which the Escape closes instead.
///
/// `Modifiers::COMMAND` is Ctrl on Windows/Linux and ⌘ on macOS, so one map
/// covers both. Skipped entirely while an egui TEXT edit is focused so typing
/// (and text-field Ctrl+Z / Esc-to-defocus) is never hijacked. Redo is
/// consumed BEFORE undo because egui's `consume_key` matches modifiers
/// logically (a plain `COMMAND+Z` pattern would also swallow `COMMAND+Shift+Z`).
fn handle_shortcuts(&mut self, ctx: &egui::Context) {
// A modal owns every key: its Escape closes it, and nothing under it —
// undo, a Delete, the selection — is the key's to change.
if ctx.text_edit_focused() || crate::viewport::modal_open_last_pass(ctx) {
return;
}
use egui::{Key, Modifiers};
// A popup or menu that was up last frame owns this Escape: it closes on
// `key_pressed(Escape)`, which a consumed key never is, so the key is
// left in the input for it and nothing below acts on it — not the
// sketch tool, not a pick, and not the selection its menu is about.
let popup_open = crate::viewport::popup_open_last_pass(ctx);
// On an eCAD workbench, Delete and Escape are the editor's keys, and
// whether they reach it is the editors' ONE rule, `keys_reach_editor`
// (a text field, a menu or combo box, a modal), not a copy of it here.
// The watch above is the host's own addition: egui's popup memory does
// not see the host's `open_bool` menus (a tree row's), and those own
// their Escape on every workbench.
let ecad_target = crate::workbench::ecad::Target::of_workbench(&self.docs.engine().settings.workbench);
let keys_free = match ecad_target {
Some(_) => !popup_open && brep_ecad_egui::keys_reach_editor(ctx),
None => !popup_open,
};
if crate::javascript::JavaScriptEditor::editing_source(ctx) {
return;
}
let sketching_3d = self.docs.engine().sketch3d_mode();
let (redo, undo, esc, tab) = ctx.input_mut(|i| {
let redo = i.consume_key(Modifiers::COMMAND | Modifiers::SHIFT, Key::Z)
|| i.consume_key(Modifiers::COMMAND, Key::Y);
let undo = i.consume_key(Modifiers::COMMAND, Key::Z);
let esc = keys_free && i.consume_key(Modifiers::NONE, Key::Escape);
// Tab cycles the 3D sketch's working plane; outside that mode it
// stays egui's focus key.
let tab = sketching_3d && keys_free && i.consume_key(Modifiers::NONE, Key::Tab);
(redo, undo, esc, tab)
});
// Delete / Backspace: THE delete-selection command
// (`crate::delete_selection`), whatever the selection is — sketch,
// 3D sketch, eCAD, sheet object, PMI annotation, constraint, component
// or feature — before the sketch modes return below.
if keys_free {
self.delete_selection_key(ctx);
}
// The 3D sketch editor: the same per-session undo routing as the
// planar one; Esc drops the armed tool's pending clicks first and the
// tool itself second.
if sketching_3d {
if redo {
self.docs.engine_mut().sketch3d_redo();
}
if undo {
self.docs.engine_mut().sketch3d_undo();
}
if esc {
let engine = self.docs.engine_mut();
if engine.sketch3d_pending_len() > 0 {
engine.sketch3d_tool_cancel();
} else {
let _ = engine.sketch3d_set_tool(Some("select"));
}
}
if tab {
self.docs.engine_mut().sketch3d_cycle_work_plane();
}
return;
}
// While editing a sketch, Ctrl+Z / Ctrl+Shift+Z drive the PER-SESSION sketch
// history (S6a), not the model-level undo — this global router consumes the
// keys first (before the viewport), so it must intercept here. Esc drops the
// active draw/trim/pick tool back to Select/drag (clearing any in-progress
// placement): this is the ONLY reliable capture point, since `consume_key`
// above already swallowed the Escape before the viewport can see it.
if self.docs.engine().sketch_mode() {
if redo {
self.docs.engine_mut().sketch_redo();
}
if undo {
self.docs.engine_mut().sketch_undo();
}
if esc {
self.docs.engine_mut().sketch_set_tool(Some("select"));
}
return;
}
if redo {
self.docs.engine_mut().redo();
}
if undo {
self.docs.engine_mut().undo();
}
if esc {
// An open pick-list popup owns the first Escape: close it WITHOUT
// clearing the selection (a popup-built multi-selection must survive
// dismissing the list); the next Escape clears as before.
if self.viewport.close_candidate_popup() {
return;
}
// An open Replace component chooser closes on Escape (its own
// keys never see it: this handler takes Escape first). A repair
// pick in its result window is cancelled the same way.
if self.replace_dialog.escape() {
return;
}
// A REFERENCE PICK owns the next one — a 3D pick or anchors on a
// drawing sheet alike — and Escape has to CANCEL it
// rather than merely clear the selection. Beginning a pick ROLLS THE
// MODEL BACK to the step before the edited feature so the picker sees
// the geometry that feature is about to consume; Finish and Cancel
// both restore that roll through `end_ref_select`. Escape used to do
// neither: it cleared the selection, left the picker open and left the
// history PARKED before the feature, so the feature stopped running
// and the viewport quietly showed the model without it. Measured on
// the retired `rotate-face.json`, which read the seed volume back at its
// closed-form step with the feature never re-run.
if self.docs.engine().ref_select_active() {
self.docs.engine_mut().cancel_ref_select();
return;
}
// A family-row preview owns the next one: Escape leaves the
// preview (the family's own values again) and keeps the selection.
if self.family_table.escape_preview(self.docs.engine_mut()) {
return;
}
// The global router owns Escape before the viewport runs its keys.
if let Some(target) = ecad_target {
self.docs.active_mut().ecad.selection_key(target, false);
return;
}
self.docs.engine_mut().clear_selection();
}
}
/// A FORM FIELD's reference pick, settled at the end of the frame (after
/// the dock has drawn every pane, the viewport's click included):
///
/// * a SINGLE-valued field finishes on its first pick — there is nothing
/// left to wait for, and the feature's result appears at once;
/// * a form that asked for the pick to end (`ref_select_request_finish`:
/// an edit elsewhere on a feature's form) gets it now, after the frame's
/// viewport click has landed in the pick;
/// * a pick NO form claimed this frame (`ref_select_claim`) is finished:
/// its pane was tabbed away, closed, or its subject is gone, and a pick
/// with no field on screen has nothing to end it.
///
/// Card-based picks are not this method's: their card is always on screen.
fn settle_form_pick(&mut self) {
let engine = self.docs.engine_mut();
let Some(field) = engine.ref_select_field() else {
return;
};
if !engine.ref_select_in_form() {
return;
}
let claimed = engine.ref_select_take_claim();
let single_done = !field.multiple && field.picks > 0;
if single_done || engine.ref_select_finish_requested() || !claimed {
engine.finish_ref_select();
}
}
/// DELETE (or Backspace) runs THE delete-selection command
/// ([`crate::delete_selection`]) on whatever is selected: the same call as
/// that object's row ✕ / menu Delete, so the key and the menu do the same
/// thing (one undo step, no extra confirmation). The key is consumed only
/// when there IS something to delete, and left to the family-table grid
/// when it owns the keyboard. Never a SHEET, PMI VIEW or study: each takes
/// everything placed on it with it and stays behind its menu's Delete.
/// Called after the text-edit / modal / popup guards, so a Backspace typed
/// into a field never gets here.
fn delete_selection_key(&mut self, ctx: &egui::Context) {
use egui::{Key, Modifiers};
if self.family_table.owns_delete_key(ctx) {
return;
}
let Some(target) = crate::delete_selection::target(&self.docs) else {
return;
};
let pressed = ctx.input_mut(|i| {
i.consume_key(Modifiers::NONE, Key::Delete) || i.consume_key(Modifiers::NONE, Key::Backspace)
});
if !pressed {
return;
}
if let Err(error) = crate::delete_selection::apply(&mut self.docs, &target) {
self.docs.engine_mut().push_notice(format!("Delete: {error}"));
}
}
/// Open the component source, offering to recover a missing file from the
/// assembly's embedded part definition.
/// Open the Replace component chooser on `component_id` (the component
/// menus' Replace…).
pub(crate) fn open_replace(&mut self, component_id: &str) {
if let Err(error) = crate::panels::replace_component::finish_work_part_for_replace(&mut self.docs) {
self.docs.engine_mut().push_notice(format!("Replace: {error}"));
return;
}
let document = self.docs.active().name().map(str::to_string);
self.replace_dialog.open(self.docs.engine(), self.model_store.as_ref(), component_id, document.as_deref());
}
/// Open the Replace summary alone, for mates an earlier replace holds.
pub(crate) fn review_replace(&mut self) {
self.replace_dialog.review();
}
/// A component menu's document-level flow (`component_actions`): every
/// menu — the context bar, the viewport's right-click, the BOM row — hands
/// its request here, so each flow has one implementation.
fn run_component_request(&mut self, request: ComponentActionRequest, compact: bool) {
match request {
ComponentActionRequest::SavePartAs { component_id } => self.edit_part(&component_id),
ComponentActionRequest::Replace { component_id } => self.open_replace(&component_id),
ComponentActionRequest::WorkPart { component_path, in_place } => {
let mode = if in_place {
crate::document::work_part::WorkPartMode::InPlace
} else {
crate::document::work_part::WorkPartMode::NewTab
};
if let Err(error) = self.docs.begin_work_part_as(&component_path, mode) {
self.docs.engine_mut().push_notice(format!("Work part: {error}"));
}
}
ComponentActionRequest::Info { targets } => {
self.info_windows.open_for(&targets, self.viewport.last_rect());
}
ComponentActionRequest::EditFeature { feature_id } => {
self.history.focus_feature(feature_id);
if compact {
self.dock.show_pane(PaneKind::History);
}
}
}
}
fn edit_part(&mut self, component_id: &str) {
self.file.edit_component_part(
&mut self.docs,
self.model_store.as_ref(),
component_id,
);
}
/// Draw an isolated preview while the destination document remains untouched.
/// Hand the working indicator this frame's work, most important first:
/// the active document's runner queues, a boot load, the STEP parts
/// library's downloads, then every background document's queues.
fn collect_busy(&mut self, ctx: &egui::Context) {
use crate::panels::busy::{engine_activities, Activity};
let active = self.docs.active_index();
let mut activities = engine_activities(self.docs.engine(), self.docs.active_id(), None);
if self.pending_boot_load.is_some() {
activities.push(Activity::new("boot", "boot", "Loading document"));
}
#[cfg(not(target_arch = "wasm32"))]
if self.reconnecting.is_some() {
activities.push(Activity::new("plmConnect", "plmConnect", "Connecting to the PLM"));
}
activities.extend(self.step_parts.busy_activities());
activities.extend(self.file.busy_activities());
// A write-behind store still settling saves: the browser's IndexedDB
// or a PLM server (not the desktop's own settings/layout files).
let saving = self.model_store.pending_saves();
if saving > 0 {
#[cfg(target_arch = "wasm32")]
let label = match saving {
1 => "Saving to browser storage".to_string(),
n => format!("Saving to browser storage ({n} writes)"),
};
#[cfg(not(target_arch = "wasm32"))]
let label = match saving {
1 => "Saving".to_string(),
n => format!("Saving ({n} writes)"),
};
activities.push(Activity::new("storeWrite", "storeWrite", label));
}
for (index, doc) in self.docs.iter().enumerate() {
if index != active {
activities.extend(engine_activities(&doc.engine, doc.id(), Some(&doc.title())));
}
}
self.busy.update(activities, ctx.input(|i| i.time));
self.busy.request_repaint(ctx);
}
fn show_stl_preview(&mut self, ui: &mut egui::Ui) -> bool {
use crate::panels::stl_import::PreviewAction;
let Some(preview) = self.stl_import.as_mut() else {
return false;
};
let action = if preview.destination != self.docs.active_id() {
PreviewAction::Cancel
} else {
preview.show(ui, &mut self.viewport)
};
if crate::automation::registry::enabled() {
let _publish_span = crate::perf::span(crate::perf::Phase::Publish);
crate::automation::registry::publish("__brepImportPreview", "STL/OBJ import preview state (tolerances, counts, accept readiness); null when no preview is open", &preview.state_json());
crate::automation::registry::publish("__brepImportRegions", "interactive mesh import: phase, region list (proposed kind, status, residual, refits), selected region and its form; null when no preview is open", &preview.regions_json());
crate::automation::registry::publish("__brepImportPreviewHit", "import preview dialog widget rects", &preview.hits_json());
crate::automation::registry::publish("__brepCamera", "camera state: kind, eye, target, up, near/far, projection block, worldPerPixel", &preview.engine.camera_state_json());
crate::automation::registry::publish("__brepPpp", "pixels per point of the surface", &format!("{}", ui.ctx().pixels_per_point()));
crate::automation::registry::publish("__brepHistory", "history listing {step, features:[{index,type,id}]}", &self.docs.engine().history_listing_json());
crate::automation::registry::publish("__brepDocuments", "open document tabs {active, tabs:[{title,name,dirty}]}", &document_tabs::state_json(&self.docs));
}
let close = match action {
PreviewAction::Accept => {
match preview.accept_into(self.docs.active_id(), self.docs.engine_mut()) {
Ok(()) => true,
Err(error) => {
self.docs.engine_mut().push_notice(error);
false
}
}
}
PreviewAction::Cancel => true,
PreviewAction::None => false,
};
if close {
self.stl_import = None;
self.viewport.forget_document();
ui.ctx().request_repaint();
}
true
}
/// Phase 3 of the automation frame (§4.4): read-only commands answer from
/// THIS frame's registry and layout. A method rather than a block at the
/// end of `ui` because the import preview returns from that function early
/// and still owes the frame its reads.
#[cfg(feature = "automation")]
fn drain_reads(&mut self, ctx: &egui::Context) {
let _t = crate::perf::trace("drain reads + publish_view");
let queue = self.automation.clone();
let frame = ctx.cumulative_frame_nr();
queue.drain_app(
crate::automation::command::Phase::Read,
&mut crate::automation::command::Ctx { app: self, egui: ctx },
frame,
);
// The frame drawn now shows this document: a capture landing next
// frame is of it.
let view = self.docs.view();
queue.publish_view(view.handle(), view.committed_revision());
// This pass's layout is the current one for a layout read that comes
// before anything changes it (`automation::queue::Need::Layout`).
queue.note_pass();
}
/// Every notice raised since the last gather — the engine's, storage
/// failures found after a write returned, refused edits — mirrored to the
/// automation queue's `notices` drain. Called by the frame (which then
/// shows them as toasts) and by the frame-free service step, so a hidden
/// window still reports what the user would have seen.
fn gather_notices(&mut self) -> Vec<(brep_render::engine_state::NoticeSeverity, String)> {
use brep_render::engine_state::NoticeSeverity;
let mut raised = self.docs.engine_mut().take_graded_notices();
// Same lane for STORAGE failures the store could only discover after its
// synchronous `write` returned `Ok` (the browser backend writes behind an
// in-memory mirror). A save that did not persist must never be silent.
let errors = self.model_store.take_persistence_errors().into_iter().chain(self.autosave.take_errors());
let failures: Vec<String> = errors.filter_map(|text| crate::panels::plm_host::plain_write_failure(&text)).collect();
if let Some(last) = failures.last() {
// The status line said "saved …" when the write was issued.
self.file.status = last.clone();
}
if failures.iter().any(|text| text.contains("the PLM server is not reachable")) {
self.plm.note_unreachable();
}
raised.extend(failures.into_iter().map(|text| (NoticeSeverity::Error, text)));
// ...and the tab whose Save that was is unsaved again: it was marked
// clean when the write was issued, and a clean tab closes without a
// question.
let failed = self.model_store.take_failed_writes();
for name in &failed {
self.docs.mark_write_failed(self.model_store.as_ref(), name);
}
// A part an open assembly holds a copy of (saved with it): the copy
// keeps the change, marked, so the next Save writes it again.
for name in &failed {
self.docs.mark_failed_copies(self.model_store.as_ref(), name);
}
// An edit a read-only document refused (a PLM revision not checked out,
// or released): its history kept the saved bytes, and this says why.
for doc in self.docs.iter_mut() {
if let Some(text) = doc.take_refused() {
raised.push((NoticeSeverity::Warning, text));
}
}
// The automation channel's `notices` drain: what the user would have SEEN.
// Without this the drain has no producers at all and every script's
// `{"notices": []}` asserts nothing.
#[cfg(feature = "automation")]
for (_, text) in &raised {
self.automation.push_notice(
crate::automation::command::NoticeKind::Toast,
text.clone(),
);
}
raised
}
/// Application-command dispatch without a frame (the desktop loop's
/// service step; see `automation::queue`): the PLM/store tasks that
/// answered, the queue's frame-free head, the background runs it started,
/// and the notices they raised. Draws nothing. Returns whether work it
/// must keep servicing is still in flight.
#[cfg(feature = "automation")]
pub fn service_automation(&mut self, ctx: &egui::Context) -> bool {
#[cfg(not(target_arch = "wasm32"))]
crate::plm::native::run_pending();
let queue = self.automation.clone();
let polled = queue.drain_frameless(&mut crate::automation::command::Ctx { app: self, egui: ctx });
let running = self.docs.pump_all();
let raised = self.gather_notices();
if polled || running || !raised.is_empty() {
// Work in flight or landing moves what a pass would show.
queue.mark_layout_dirty();
}
if !raised.is_empty() {
let now = ctx.input(|i| i.time);
self.toasts.extend(raised, now);
}
// A window that IS drawn shows the effect on its next frame.
ctx.request_repaint();
polled || running || queue.has_frameless_work()
}
/// Reset everything the shared panels and the viewport hold ABOUT ONE
/// DOCUMENT, run at the top of the first frame that sees a different active
/// document.
///
/// Panel state is deliberately NOT per-document (one History panel, one
/// Scene tree, …): a second copy per tab would double every panel's state
/// for a benefit — remembering which feature form was open in a background
/// tab — nobody asked for. The price is that the transient state has to be
/// dropped on a switch, because every bit of it (expansion sets, hit maps,
/// an open feature form, a pinned Info window's object name) refers to the
/// document that just went away.
fn reset_document_scoped_state(&mut self) {
self.history = HistoryPanel::new();
self.scene = ScenePanel::new();
self.bom = BomPanel::new();
self.assembly_constraints = AssemblyConstraintsPanel::new();
self.wire_harness = WireHarnessPanel::new();
self.pmi = crate::panels::pmi::PmiPanel::new();
self.cam = crate::panels::cam::CamPanel::new();
self.sheets = crate::panels::sheets::SheetsPanel::new();
self.expressions = ExpressionsPanel::new();
self.qualify = crate::panels::qualify::QualifyPanel::new();
self.family_table = crate::panels::family_table_editor::FamilyTableEditor::new();
// A family-row preview left on in a document the user comes back to
// would have no pane state to show or end it: switching ends it.
self.docs.engine_mut().set_expression_preview(None);
// Pinned to object NAMES of the old document ("Box" exists in most of
// them), so these would silently retarget rather than go blank.
self.info_windows = InfoWindows::new();
self.interference = crate::panels::interference::InterferenceWindow::new();
self.auto_constraints = crate::panels::auto_constraints::AutoConstraintsWindow::new();
// The outdated-parts cache keys on `(applied_generation, save_generation)`,
// and two documents' generations are unrelated — a switch can land on the
// same key with an entirely different parts library.
self.update_components.invalidate();
self.viewport.forget_document();
}
/// Publish every plain-state blob ([`state_blobs::STATE_BLOBS`]) from its
/// producer. The frame calls this; `state_get` asks
/// [`state_blob_json`](Self::state_blob_json) itself, so a read of one of
/// these never waits for a frame.
///
/// [`state_blobs::STATE_BLOBS`]: crate::automation::state_blobs::STATE_BLOBS
fn publish_state_blobs(&mut self, ctx: &egui::Context) {
for (name, doc) in crate::automation::state_blobs::STATE_BLOBS {
if let Some(json) = self.state_blob_json(name, ctx) {
crate::automation::registry::publish(name, doc, &json);
}
}
}
/// One plain-state blob by name, built from the app's data now. `None`
/// for a name that is not one of [`STATE_BLOBS`] (a layout blob, which
/// only a drawn pass publishes).
///
/// [`STATE_BLOBS`]: crate::automation::state_blobs::STATE_BLOBS
pub fn state_blob_json(&mut self, name: &str, ctx: &egui::Context) -> Option<String> {
if name == "__brepSelectionInspection" {
let document = self.docs.active_id();
return Some(self.selection.inspection_json(self.docs.engine_mut(), document));
}
let engine = self.docs.engine();
Some(match name {
// The import preview draws its own engine while it is open.
"__brepCamera" => match &self.stl_import {
Some(preview) => preview.engine.camera_state_json(),
None => engine.camera_state_json(),
},
"__brepImportPreview" => self.stl_import.as_ref().map_or_else(|| "null".to_string(), |p| p.state_json()),
"__brepImportRegions" => self.stl_import.as_ref().map_or_else(|| "null".to_string(), |p| p.regions_json()),
"__brepPpp" => format!("{}", ctx.pixels_per_point()),
// Keyed on the settings themselves and the projection, the only
// inputs `settings_json` reads.
"__brepSettings" => {
let orthographic = matches!(engine.camera.projection, brep_render::view::Projection::Orthographic { .. });
let key = (engine.settings.clone(), orthographic);
StateBlobCache::keyed(&mut self.state_blob_cache.settings, key, || engine.settings_json())
}
"__brepSolidColors" => engine.solid_color_overrides_json(),
"__brepHistory" => engine.history_listing_json(),
"__brepGizmo" => engine.gizmo_state_json(),
"__brepFile" => self.file.file_state_json(&self.docs, self.model_store.as_ref()),
// Keyed on exactly what the signature reports (rollback step, each
// solid's name, triangle count and bounds), so building the key
// skips only the JSON, never a change.
"__brepModel" => {
let key = model_signature_key(engine);
match StateBlobCache::hit(&self.state_blob_cache.model, &key) {
Some(json) => json,
None => {
let json = self.model_signature_json();
self.state_blob_cache.model = Some((key, json.clone()));
json
}
}
}
"__brepReport" => engine.history_report_json(),
// The in-flight run: whether one is pending, the feature the runner
// says it is executing, and the feature a cancelled run was stuck on.
"__brepRun" => serde_json::json!({
"pending": engine.run_pending(),
"progress": engine.run_progress().map(|p| serde_json::json!({
"generation": p.generation,
"index": p.index,
"total": p.total,
"featureId": p.feature_id,
"featureType": p.feature_type,
})),
"cancelled": engine.cancelled_run(),
})
.to_string(),
"__brepBusy" => self.busy.state_json(),
"__brepExpr" => serde_json::json!({
"expressions": engine.expressions_json(),
"variables": serde_json::from_str::<serde_json::Value>(&engine.expression_variables_json())
.unwrap_or(serde_json::Value::Null),
"configurator": serde_json::from_str::<serde_json::Value>(&engine.configurator_json())
.unwrap_or(serde_json::Value::Null),
})
.to_string(),
"__brepPlmDoc" => self.plm.state_json(&self.docs),
// The queued toast texts — the only trace of a refusal the app shows
// as a transient card (e.g. a constraint the strip could not add).
"__brepNotices" => self.toasts.texts_json(),
"__brepToasts" => self.toasts.state_json(),
"__brepBug" => self.bug_report.state_json(),
"__brepDiagnostics" => self.diagnostics.json().to_string(),
// Where the last ~120 frames' milliseconds went. `__brepDiagnostics`
// says WHICH renderer is drawing; this says what it costs, in the
// same split the Info window's Performance rows show.
"__brepPerf" => crate::perf::json(),
"__brepWireHarness" => engine.wire_harness_state_json(),
"__brepCam" => {
self.cam.refresh_geometry(self.docs.engine());
crate::panels::cam::cam_state_json(self.docs.engine(), &self.cam)
}
"__brepInstructions" => self.instructions.state_json(),
"__brepInstructionsPlayer" => self.instructions_player.state_json(engine),
"__brepSimulation" => crate::panels::simulation::state_json(engine, &self.simulation),
"__brepCamSim" => self.cam.sim.state_json().to_string(),
"__brepPorts" => engine.ports_state_json(),
"__brepQualify" => self.qualify.state_json(),
"__brepFamily" => self.family_table.state_json(),
"__brepPmi" => engine.pmi_state_json(),
"__brepSheets" => self.docs.engine_mut().sheet_state_json(),
// The workbench logical state (resolved current id + available
// ids); the dropdown's rects ride in `__brepToolbar`.
"__brepWorkbench" => {
// Its inputs: the stored workbench and the plugins' workbenches.
let key = (engine.settings.workbench.clone(), engine.plugin_workbenches());
StateBlobCache::keyed(&mut self.state_blob_cache.workbench, key, || {
crate::workbench::workbench_state_scoped(engine).to_string()
})
}
"__brepSelection" => engine.selection_json(),
"__brepInfoWindows" => self.info_windows.published_json(self.docs.engine_mut()),
"__brepInterference" => self.interference.state_json_in(engine),
"__brepAutoConstraints" => self.auto_constraints.state_json(),
"__brepStepParts" => self.step_parts.state_json(),
"__brepSelectionFilter" => engine.selection_filter_json(),
"__brepFilterButton" => self.selection.button_json(),
"__brepContext" => self.context_bar.state_json(),
"__brepPickBar" => self.pick_bar.state_json(),
"__brepDocuments" => document_tabs::state_json(&self.docs),
"__brepRecovery" => self.recovery.state_json(),
"__brepClosing" => self.closing_json(),
"__brepComponentMove" => engine.component_move_json(),
"__brepSketch" => self.sketch.published_json(engine),
"__brepSketch3d" => self.sketch3d.published_json(engine),
"__brepWireframe" => format!("{}", engine.settings.wireframe),
// The pick's `subject` and `path` name the FORM FIELD it runs in
// (`<panel>/field:<path>#activate` is that field's button, Done
// while the pick runs); `inForm` is false for the card-based
// picks, whose controls are `modebar/refsel:*`.
"__brepRefSelect" => {
let field = engine.ref_select_field();
serde_json::json!({
"active": engine.ref_select_active(),
"prompt": engine.ref_select_prompt(),
"names": engine.ref_select_names(),
"subject": field.as_ref().map(|f| f.subject.clone()),
"path": field.as_ref().map(|f| f.path.join(".")),
"multiple": field.as_ref().map(|f| f.multiple),
"picks": field.as_ref().map(|f| f.picks),
"inForm": engine.ref_select_in_form(),
"ghostsLit": engine.ref_select_ghosts_lit(),
})
.to_string()
}
"__brepStep" => format!("{}", engine.history_rollback()),
"__brepParams" => self.params_blob().to_string(),
_ => return None,
})
}
/// A signature of the CURRENT rendered model (rolled-to step) — solid count,
/// per-solid triangle count + bbox, and total triangles. Published to JS so
/// the headed verifier can prove each roll / edit produced different geometry
/// (names alone don't: a SUBTRACT reuses the target's name).
#[cfg_attr(not(target_arch = "wasm32"), allow(dead_code))]
pub(crate) fn model_signature_json(&self) -> String {
let solids: Vec<serde_json::Value> = self
.docs
.engine()
.scene
.solids()
.iter()
.map(|s| {
serde_json::json!({
"name": s.name,
"tris": s.mesh.indices.len() / 3,
"min": s.bbox.min,
"max": s.bbox.max,
})
})
.collect();
let total_tris: usize = self
.docs
.engine()
.scene
.solids()
.iter()
.map(|s| s.mesh.indices.len() / 3)
.sum();
serde_json::json!({
"step": self.docs.engine().history_rollback(),
"solidCount": solids.len(),
"totalTris": total_tris,
"solids": solids,
})
.to_string()
}
/// Open or close the floating Settings window — the toolbar gear's flag,
/// reachable from the automation layer (`settings_window`).
pub fn set_settings_window_open(&mut self, open: bool) {
self.settings.open = open;
}
/// Open or close the floating Part Properties window — the toolbar tag
/// button's flag, reachable from the automation layer
/// (`part_properties_window`).
pub fn set_part_properties_window_open(&mut self, open: bool) {
self.part_properties.open = open;
}
/// Open or close the floating Info window — the toolbar info button's flag,
/// reachable from the automation layer (`info_window`).
pub fn set_info_window_open(&mut self, open: bool) {
self.info.open = open;
}
/// What this session is running on. The app's ONE record of it: the Info
/// window draws it, a problem report carries it, the `diagnostics` command
/// returns it and the MCP banner names its adapter — all from here.
/// Where the JavaScript editor's Run sends a program (a host's own
/// script runner; the desktop app installs its server's on its own).
pub fn set_script_host(&mut self, host: Box<dyn crate::javascript::ScriptHost>) {
self.javascript.host = Some(host);
}
pub fn diagnostics(&self) -> &crate::diagnostics::Diagnostics {
&self.diagnostics
}
/// Begin the in-app problem report: the same call the Submit Bug button
/// makes, and for the same reason it makes it THIS frame — `request`
/// captures the current frame before its own dialog exists.
pub fn begin_bug_report(&mut self, ctx: &egui::Context) {
self.bug_report.request(ctx, self.docs.engine(), &self.diagnostics);
}
/// Activate an available declarative panel using its stable package ID.
pub(crate) fn show_plugin_panel(&mut self, id: &str) -> Result<(), String> {
self.dock.show_plugin_panel(self.docs.engine(), id)
}
/// Bring a built-in dock pane to the front (`show_pane`).
pub fn show_pane(&mut self, kind: PaneKind) {
self.dock.show_pane(kind);
}
/// Shared document activation / unsaved-close handling for both presentations.
fn apply_document_actions(&mut self, activate: Option<usize>, close: Option<usize>) {
if (activate.is_some() || close.is_some()) && self.docs.engine().ref_select_in_form() {
self.docs.engine_mut().finish_ref_select();
}
if let Some(index) = activate { self.docs.activate(index); }
if let Some(index) = close { self.file.request_close(&mut self.docs, index); }
}
/// The parts-library staleness check and its refresh — the Constraints
/// header's "Update components (N)" button, reachable as the
/// `component_update` command. Returns `(outdated, missing, refreshed)`;
/// `run` is skipped when nothing is outdated.
pub fn update_components(&mut self, run: bool) -> Result<(usize, Vec<String>, usize), String> {
let store = self.model_store.as_ref();
let synced = self.docs.synced_signatures();
self.update_components.ensure_current(self.docs.engine_mut(), store, self.file.save_generation(), &synced);
let outdated = self.update_components.outdated_count();
let missing = self.update_components.missing().to_vec();
let refreshed = if run { self.update_components.run(self.docs.engine_mut(), store)? } else { 0 };
Ok((outdated, missing, refreshed))
}
/// Frame what the central tile is actually DRAWING: the OPEN SHEET's paper
/// when a sheet is open, the 3D scene otherwise.
///
/// This is where the fit branch lives, and it is here rather than in the
/// toolbar because the two halves sit in different places — the 3D fit is an
/// engine camera op (`EngineState::zoom_to_fit`) while the sheet's pan/zoom
/// is viewport-transient state, refitted by clearing the marker the sheet
/// paint compares against. One method, so the toolbar's Zoom-to-fit button
/// and the `zoom_to_fit` command can never mean different things — and so
/// the paper needs no Fit button of its own.
///
/// The toolbar draws BEFORE the dock in `ui`, so a fit requested from the
/// button lands on this very frame.
pub fn zoom_to_fit(&mut self) {
if self.docs.engine().sheet_open().is_some() {
self.viewport.request_sheet_fit();
} else {
self.docs.engine_mut().zoom_to_fit();
}
}
/// Run what a WORKBENCH TOOLBAR button does, by its `WorkbenchButton::id`.
///
/// Split out of `ui` so a toolbar click and the `workbench_button` command
/// run the SAME arms: the workbench registry declares a button, this
/// dispatches it, and the automation surface owns no second copy of the
/// list. Returns whether the id was known.
pub fn dispatch_workbench_button(&mut self, id: &str) -> bool {
// The eCAD workbenches' buttons (Diagram, PCB, Symbol, Pads) are
// eCAD's own actions: run on the active document's editors with
// eCAD's `run_action`, so a press does exactly what eCAD's toolbar
// button does. `None` is any other id, which falls through to the
// arms below.
if crate::workbench::ecad::dispatch(&mut self.docs.active_mut().ecad, id).is_some() {
return true;
}
// The SHEET's constructions are the Drawing workbench's two tables plus
// the section and the detail, and each press is the PMI annotation
// buttons' workflow: the object is created on the open sheet with
// nothing picked yet, and its dialog opens (through the dialog door,
// which surfaces the Sheets pane) — where its reference rows are picked
// on the paper with the reference picker.
if let Some((kind, alignment)) = crate::workbench::drawing::dim_tool(id) {
let engine = self.docs.engine_mut();
if let Err(error) = engine.sheet_add_dimension(None, kind, Some(alignment), Vec::new()) {
engine.push_notice(format!("Sheet dimension: {error}"));
}
return true;
}
if let Some(axis) = crate::workbench::drawing::ord_tool(id) {
let engine = self.docs.engine_mut();
if let Err(error) = engine.sheet_add_ordinate(None, axis, None, Vec::new()) {
engine.push_notice(format!("Ordinate set: {error}"));
}
return true;
}
if id == crate::workbench::drawing::BOM_BUTTON_ID {
let engine = self.docs.engine_mut();
if let Err(error) = engine.sheet_insert_bom() { engine.push_notice(format!("BOM table: {error}")); }
return true;
}
if id == crate::workbench::drawing::SECTION_BUTTON_ID {
let engine = self.docs.engine_mut();
if let Err(error) = engine.sheet_new_section(None) {
engine.push_notice(format!("Section view: {error}"));
}
return true;
}
if id == crate::workbench::drawing::DETAIL_BUTTON_ID {
let engine = self.docs.engine_mut();
if let Err(error) = engine.sheet_new_detail(None) {
engine.push_notice(format!("Detail view: {error}"));
}
return true;
}
// The SKETCH mode's draw tools are one table too, and they are SHARED —
// every workbench's row carries them while a sketch is being edited, so
// this arm answers for all of them whatever workbench is active.
// Pressing the LIT tool puts it down (back to Select), matching the
// sheet constructions above; pressing the lit Select is already Select.
if let Some(tool) = crate::workbench::sketch::draw_tool(id) {
let engine = self.docs.engine_mut();
let armed = engine.sketch_active_tool().unwrap_or("select") == tool;
engine.sketch_set_tool(Some(if armed { "select" } else { tool }));
return true;
}
// The 3D sketch's tools and working-plane radio, shared the same way.
if let Some(tool) = crate::workbench::sketch3d::draw_tool(id) {
let engine = self.docs.engine_mut();
let armed = engine.sketch3d_active_tool().unwrap_or("select") == tool;
let _ = engine.sketch3d_set_tool(Some(if armed { "select" } else { tool }));
return true;
}
if let Some(plane) = crate::workbench::sketch3d::work_plane(id) {
let _ = self.docs.engine_mut().sketch3d_set_work_plane(plane);
return true;
}
match id {
// The sketch strip's one-shot action, now the row's: infer the
// constraints the rough-in geometry already implies.
crate::workbench::sketch::AUTOCONSTRAIN_BUTTON_ID => {
self.docs.engine_mut().sketch_auto_constrain();
}
// Sheet Metal's flat pattern: open the export modal in its DXF /
// SVG mode; the engine reports "no sheet-metal body in the part"
// as a toast on export.
"sheetmetal.flat_pattern" => {
self.file.dispatch(FileAction::ExportFlatPattern, &mut self.docs, self.model_store.as_ref());
}
// Assembly's Add Component: open the insert-component modal (the
// same flow as the ACOMP palette pick). ONE arm for both rows that
// carry it — PCB BORROWS this very button (`workbench::BORROWED`)
// rather than declaring a second id for the same action.
crate::workbench::assembly::ADD_COMPONENT_BUTTON_ID => {
self.file.dispatch(FileAction::InsertComponent, &mut self.docs, self.model_store.as_ref());
}
// Assembly's interference check: run the engine's pairwise
// intersect sweep NOW and open the results window.
"assembly.interference" => {
self.interference.open_and_run(self.docs.engine_mut());
}
// Assembly's Auto Constraints: open the inference window and scan
// the current placement NOW, so it opens showing real counts.
"assembly.auto_constrain" => {
self.auto_constraints.open_and_scan(self.docs.engine_mut());
}
// Assembly's step.parts library: open the online-library browser
// (search → thumbnails → import a STEP part → add as an ACOMP).
"assembly.step_parts_library" => {
self.step_parts.open();
}
// Wire harness's Declare connection point: the part's FIRST port
// group, with one point at the origin. It exists because the
// Qualify pane is on screen only while the part declares some — so
// without it a 3D-only part could never get its first one from the
// UI at all. The pane is surfaced by the dock's own ports door on
// the next frame, once the visibility pass has let it back in.
crate::workbench::wire_harness::DECLARE_POINT_BUTTON_ID => {
let engine = self.docs.engine_mut();
engine.history.set_ports_block(
Some(serde_json::json!([{
"name": crate::workbench::wire_harness::FIRST_GROUP,
"purpose": crate::workbench::wire_harness::FIRST_PURPOSE,
"points": [{
"name": crate::workbench::wire_harness::FIRST_POINT,
"transform": {
"position": [0.0, 0.0, 0.0],
"rotationEuler": [0.0, 0.0, 0.0],
},
}],
}])),
None,
);
}
// CAM's four actions: each hands the CAM pane its request and
// surfaces the pane, which performs it on the frame it draws (the
// pane owns the selection the action reads — which setup an
// operation goes into — so the pane performs it, not the shell).
crate::workbench::cam::NEW_SETUP_BUTTON_ID => {
self.cam.request(crate::panels::cam::CamRequest::NewSetup);
self.show_pane(PaneKind::Cam);
}
crate::workbench::cam::NEW_TOOL_BUTTON_ID => {
self.cam.request(crate::panels::cam::CamRequest::NewTool);
self.show_pane(PaneKind::Cam);
}
crate::workbench::cam::NEW_OPERATION_BUTTON_ID => {
self.cam.request(crate::panels::cam::CamRequest::NewOperation);
self.show_pane(PaneKind::Cam);
}
crate::workbench::cam::GENERATE_BUTTON_ID => {
self.cam.request(crate::panels::cam::CamRequest::Generate);
self.show_pane(PaneKind::Cam);
}
// Instructions: New from assembly opens the instructions in a new
// tab (the open assembly, saved, is the target); the wizard and a
// new step go to the pane, which owns the selected step.
crate::workbench::instructions::NEW_BUTTON_ID => {
let (_, docs, store) = self.file_docs_store();
match crate::instructions::new_instructions(docs, store, crate::instructions::Source::Active, true) {
Ok(_) => self.show_pane(PaneKind::Instructions),
Err(error) => self.docs.engine_mut().push_notice(format!("New instructions: {error}")),
}
}
crate::workbench::instructions::WIZARD_BUTTON_ID => {
self.instructions.request(crate::panels::instructions::InstructionsRequest::Wizard);
self.show_pane(PaneKind::Instructions);
}
crate::workbench::instructions::NEW_STEP_BUTTON_ID => {
self.instructions.request(crate::panels::instructions::InstructionsRequest::NewStep);
self.show_pane(PaneKind::Instructions);
}
// Play starts at the first step, or plays the shown one again.
crate::workbench::instructions::PLAY_BUTTON_ID => {
let shown = self.docs.engine().instructions_shown_step().is_some();
self.instructions_player.request(if shown { brep_render::engine_state::PlayTo::Replay } else { brep_render::engine_state::PlayTo::First });
self.show_pane(PaneKind::InstructionsPlayer);
}
// Simulation: the pane performs them (it draws the timeline the
// new study or motor lands on).
crate::workbench::simulation::NEW_STUDY_BUTTON_ID => {
self.simulation.request(crate::panels::simulation::SimulationRequest::NewStudy);
self.show_pane(PaneKind::Simulation);
}
crate::workbench::simulation::ADD_MOTOR_BUTTON_ID => {
self.simulation.request(crate::panels::simulation::SimulationRequest::AddMotor);
self.show_pane(PaneKind::Simulation);
}
// PMI's Capture view: snapshot the camera + visibility into a new
// active view and surface the PMI pane so its row is seen.
crate::workbench::pmi::CAPTURE_BUTTON_ID => {
self.docs.engine_mut().pmi_capture_view(None);
self.dock.show_pane(PaneKind::Pmi);
}
// Drawing's Back to 3D: leave the open sheet. The paper's own
// widgets go with it, and the sheet keeps its contents — the same
// edit the Sheets pane's Close sheet makes.
crate::workbench::drawing::SHEET_CLOSE_BUTTON_ID => {
let _ = self.docs.engine_mut().sheet_set_open(None);
self.viewport.forget_sheet();
}
// Drawing's Add sheet: a new sheet, opened on the paper, and the
// Sheets pane brought forward so its row is seen.
crate::workbench::drawing::ADD_SHEET_BUTTON_ID => {
self.docs.engine_mut().sheet_add(None);
self.dock.show_pane(PaneKind::Sheets);
}
// Drawing's Place view: the first saved PMI view on the open sheet;
// the placement's form opens (the door surfaces the pane), and its
// `view` field is where a different view is chosen.
crate::workbench::drawing::PLACE_VIEW_BUTTON_ID => {
let engine = self.docs.engine_mut();
let placed = match engine.sheet_pmi_view_ids().first().cloned() {
Some(view) => engine.sheet_place_view(None, &view, None, None).map(|_| ()),
None => Err("capture a PMI view in the PMI workbench first".to_string()),
};
if let Err(error) = placed {
engine.push_notice(format!("Place view: {error}"));
}
}
_ => return false,
}
true
}
}
impl eframe::App for BrepApp {
/// The browser's automation pump reaches the app between frames through
/// eframe's runner (`WebRunner::app_mut`, `web_pump`). A web-only method.
#[cfg(target_arch = "wasm32")]
fn as_any_mut(&mut self) -> Option<&mut dyn std::any::Any> {
Some(self)
}
/// Phase 1 of the automation frame (§4.4): queued input commands become
/// egui events; a screenshot that landed completes its reply. Called by
/// every eframe runner before `ui`; the headless host calls it itself.
#[cfg(feature = "automation")]
fn raw_input_hook(&mut self, ctx: &egui::Context, raw_input: &mut egui::RawInput) {
let view = self.viewport.last_rect();
// eframe runs a pass without `ui` when the window is not visible (a
// hidden tab's timer frames, an occluded or minimised window): input
// given to it reaches no widget. Automation input waits for a pass
// that draws (the loop's or the page pump's own, `on_demand`, which
// mark theirs visible), and `activate_widget` keeps the widgets of
// the last pass that drew. egui hit-tests pointer input against the
// PREVIOUS pass's widgets, so the pass right after one that did not
// draw only lays out again, and input waits for the one after it.
//
// A hidden TAB with a WebMCP host attached draws every pass instead:
// eframe runs its timer passes right after the page pump's (each
// repaint request schedules one), and a pass that skips `ui` between
// a press and its release makes egui forget the press. eframe reads
// the root viewport's visibility after this hook, so marking it
// visible here is what has it run `ui`.
#[cfg(target_arch = "wasm32")]
if crate::automation::registry::enabled() {
let root = raw_input.viewports.entry(egui::ViewportId::ROOT).or_default();
if root.occluded == Some(true) {
root.occluded = Some(false);
}
}
let draws = raw_input.viewports.get(&egui::ViewportId::ROOT).and_then(|v| v.visible()).unwrap_or(true);
let drew = crate::automation::actions::note_pass(ctx, draws);
if !draws || !drew {
return;
}
self.automation.drain_input(raw_input, ctx.cumulative_frame_nr(), ctx.pixels_per_point(), view, ctx);
}
fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
// Open this frame's timing window (banks the previous frame's phase
// totals and its period) and bracket the whole body. See `perf`: on a
// single-threaded shell a long frame IS a frozen UI, so what the frame
// spends is the whole diagnosis.
crate::perf::tick();
let _ui_span = crate::perf::span(crate::perf::Phase::Ui);
let _frame_trace = crate::perf::trace("frame");
// The native store and PLM queue (`plm::native`): every task whose I/O
// answered since the last frame finishes here, on this thread, FIRST —
// before any early return below (the mesh preview's replaces the whole
// shell) can skip it. The browser drains its microtasks between events
// the same way, whatever the page is showing.
#[cfg(not(target_arch = "wasm32"))]
crate::plm::native::run_pending();
// A PLM save made mid-rebuild is pictured once its run lands.
crate::plm::thumbnail::tick(|id| self.docs.iter().find(|d| d.id() == id).map(|d| &d.engine));
// Its player data too (instructions), and a refused upload is said.
crate::plm::player_data::tick(|id| self.docs.iter().find(|d| d.id() == id).map(|d| &d.engine));
if let Some(notice) = crate::plm::player_data::take_notice() {
self.docs.engine_mut().push_notice_as(brep_render::engine_state::NoticeSeverity::Warning, notice);
}
// The File menu's recent-file pictures that finished since last frame.
crate::plm::thumbnail::persist_recent(self.model_store.as_ref());
// --- global keyboard shortcuts (undo/redo/clear-selection) ------------
// Handled before any panel draws so a Ctrl+Z etc. this frame takes effect
// this frame. `ctx` is a cheap Arc clone (avoids borrowing `ui` across the
// `&mut self` call).
let ctx = ui.ctx().clone();
self.plugins.finish_installations(&mut self.docs, self.model_store.as_ref());
self.plugins.sync_documents(&mut self.docs);
self.javascript.poll(&ctx, &mut self.docs, self.model_store.as_ref());
self.plugins.poll(&ctx, self.docs.engine_mut(), self.model_store.as_ref());
// Phase 2 of the automation frame (§4.4): mutations run before any
// panel draws, so this frame shows their effect.
#[cfg(feature = "automation")]
{
let queue = self.automation.clone();
let frame = ctx.cumulative_frame_nr();
let _t = crate::perf::trace("drain mutate");
queue.drain_app(
crate::automation::command::Phase::Mutate,
&mut crate::automation::command::Ctx { app: self, egui: &ctx },
frame,
);
}
// A measured spin (`perf_spin`) advances one pointer step per frame, here
// — after the mutation drain that may have armed it, so the spin starts
// on the frame it was asked for.
#[cfg(feature = "automation")]
self.step_spin(&ctx);
// --- a different document is active than the panels were drawn for ----
// Checked FIRST, before anything draws: the switch itself happened late
// in some earlier frame (a tab click, a close, an Open), and every
// shared panel is still holding the previous document's transient state.
if self.active_document != self.docs.view_id() {
if !self.javascript.running_in(self.active_document) {
for doc in self.docs.iter_mut() { if doc.id() == self.active_document { doc.engine.cancel_plugin_action(); } }
}
// The Interference window is reset below, but its overlay lives in
// the document being left: take it down there, or switching back
// shows the volumes with no window to clear them.
for doc in self.docs.iter_mut() {
if doc.id() == self.active_document {
doc.engine.clear_interference_overlay();
}
}
self.plugins.close_action();
self.active_document = self.docs.view_id();
let same_tab = self.active_tab == self.docs.active_id();
self.active_tab = self.docs.active_id();
let bom = same_tab.then(|| std::mem::take(&mut self.bom));
self.reset_document_scoped_state();
if let Some(bom) = bom {
self.bom = bom;
}
}
// --- the workbench decides whether placed parts show their ports -------
// A workbench that shows the Wire Harness panel (Wire harness, All)
// draws the ports components carry; the others — Modeling included,
// though it offers the Port feature — keep the assembly clean. A no-op
// when nothing changed.
{
// The document's name lives in the SHELL (the tab, the file), and a
// drawing sheet's title block prints it. Told to the engine here,
// every frame, because that is where the active tab is known; a
// no-op when it has not changed. It is the DISPLAY name, the one the
// tab shows: a stamp reading `bracket.nbrep` would be naming our
// file format rather than the drawing.
let name = self
.docs
.active()
.name()
.map(crate::store::model_display_name)
.unwrap_or_default();
let engine = self.docs.engine_mut();
engine.set_document_name(&name);
let show = crate::workbench::panel_visible(
&engine.settings.workbench,
crate::workbench::wire_harness::PANEL_ID,
&crate::workbench::ButtonState::of(engine),
);
engine.set_component_ports_visible(show);
// The PMI workbench IS the PMI editing mode: a workbench that shows
// the PMI panel (PMI, All) enters it — the modeling camera /
// visibility / wireframe are remembered — and one that hides it
// leaves it, deactivating the view and restoring them.
let pmi_shown = crate::workbench::panel_visible(
&engine.settings.workbench,
crate::workbench::pmi::PANEL_ID,
&crate::workbench::ButtonState::of(engine),
);
// An exploded view is shown from the Assembly workbench's Constraints
// pane without entering PMI (a capture still remembers the modeling
// state), so that pane counts as an exploded view's home too. With
// neither on screen (nor the paper), the view is put away, so no one
// models on exploded displays.
let exploded_shown = crate::workbench::panel_visible(
&engine.settings.workbench,
crate::workbench::assembly::CONSTRAINTS_PANEL_ID,
&crate::workbench::ButtonState::of(engine),
) && engine
.pmi_active_view()
.is_some_and(|id| engine.is_exploded_view(id));
let paper_shown = crate::workbench::panel_visible(
&engine.settings.workbench,
crate::workbench::drawing::SHEETS_PANEL_ID,
&crate::workbench::ButtonState::of(engine),
);
// A step of the instructions shows through its view, so the
// Instructions and Player panes are a view's home too.
let instructions_shown = [crate::workbench::instructions::PANEL_ID, crate::workbench::instructions::PLAYER_PANEL_ID]
.into_iter()
.any(|panel| crate::workbench::panel_visible(&engine.settings.workbench, panel, &crate::workbench::ButtonState::of(engine)));
if pmi_shown && !engine.pmi_workbench_entered() {
engine.pmi_enter_workbench();
} else if !pmi_shown && !exploded_shown && !instructions_shown && engine.pmi_workbench_entered() {
engine.pmi_leave_workbench();
}
if !pmi_shown && !exploded_shown && !paper_shown && !instructions_shown && engine.pmi_active_view().is_some() {
engine.pmi_deactivate_view();
}
// The DRAWING workbench owns the paper: LEAVING it for a workbench
// that hides the Sheets pane closes an open sheet, so the central
// tile is not left drawing paper whose tools, pane and way back are
// all hidden. It is a MODE change — the sheets and their contents
// are untouched — and an edge, see `sheets_pane_shown`.
let sheets_shown = crate::workbench::panel_visible(
&engine.settings.workbench,
crate::workbench::drawing::SHEETS_PANEL_ID,
&crate::workbench::ButtonState::of(engine),
);
if self.sheets_pane_shown && !sheets_shown && engine.sheet_open().is_some() {
let _ = engine.sheet_set_open(None);
self.viewport.forget_sheet();
}
self.sheets_pane_shown = sheets_shown;
}
// --- GUI chrome theme -------------------------------------------------
// Apply the user's theme preference to the egui chrome every frame
// (idempotent: `set_theme` just stores the preference). Auto follows the
// OS/system theme (prefers-color-scheme on web); egui falls back to dark
// when no OS signal is available. This controls panels/windows/toolbar/
// text only — the 3D viewport `background` is a separate setting.
ctx.set_theme(match self.docs.engine().settings.theme {
ThemeMode::Auto => egui::ThemePreference::System,
ThemeMode::Light => egui::ThemePreference::Light,
ThemeMode::Dark => egui::ThemePreference::Dark,
});
// --- global UI size scale --------------------------------------------
// Apply the user's "UI scale" to the whole egui chrome every frame. This
// is idempotent when unchanged (`set_zoom_factor` only repaints on an
// actual change) and composes with the native device pixel ratio
// (pixels_per_point = zoom_factor * native_pixels_per_point).
//
// Defer live UI rescale while the user drags the Settings "UI scale" slider:
// the slider value updates continuously, but only commit it to the actual egui
// zoom once the pointer is released, so the whole UI doesn't rescale under the
// cursor mid-drag.
let pointer_down = ctx.input(|i| i.pointer.any_down());
if !pointer_down {
self.applied_ui_scale = self.docs.engine().settings.ui_scale;
}
ctx.set_zoom_factor(self.applied_ui_scale);
crate::viewport::touch::prepare_browser_input(ui);
// --- history-runner pump ---------------------------------------------
// Apply any completed background history run BEFORE panels read the scene.
// For the synchronous InlineRunner this is a no-op (`rerun_history` already
// pumped its own submit), so nothing changes today; it is the seam a future
// native-thread / wasm-worker runner lands its reply through. While a run is
// still in flight, keep the frame loop alive so its reply gets pumped — for
// Inline `run_pending()` is always false, so this never fires.
//
// EVERY open document is pumped, not just the active one — see
// `Documents::pump_all`, which also keeps the active document's parts
// library in the kernel's store when a background tab's run lands.
let work_in_flight = {
let _t = crate::perf::trace("pump_all");
self.docs.pump_all()
};
if work_in_flight {
ctx.request_repaint();
}
// The CAM simulation player, whether or not its pane is drawn.
let dt = ctx.input(|i| i.stable_dt as f64).clamp(0.0, 0.25);
if self.cam.sim.pump(self.docs.engine_mut(), dt) {
ctx.request_repaint();
}
// An exploded view's Explode / Collapse animation.
if self.docs.engine_mut().exploded_tick(dt) {
ctx.request_repaint();
}
// A motion study: its bake (a few frames per tick) and its playhead,
// driven only while the workbench offers the Simulation pane
// (Simulation, All); the frame that leaves it puts every posed part
// and the camera back, and a document edit made in another workbench
// never wakes the study there (`SimulationPanel::drive_playback`).
let simulation_shown = {
let engine = self.docs.engine();
crate::workbench::panel_visible(
&engine.settings.workbench,
crate::workbench::simulation::PANEL_ID,
&crate::workbench::ButtonState::of(engine),
)
};
if self.simulation.drive_playback(self.docs.engine_mut(), simulation_shown, dt) {
ctx.request_repaint();
}
// The pane's Export button: the app owns the viewport's render core.
if let Some(request) = self.simulation.take_export() {
#[cfg(not(target_arch = "wasm32"))]
let result = {
let spec = crate::simulation_export::ExportSpec {
study: Some(request.study.clone()),
format: request.format.clone(),
width: request.width,
height: request.height,
fps: Some(request.fps),
path: Some(request.path.clone()),
};
crate::simulation_export::export(self, &spec).map(|(report, _)| report.summary())
};
#[cfg(target_arch = "wasm32")]
let result = Err::<String, String>(format!("video export is native only (asked for {})", request.format));
self.simulation.report(result);
ctx.request_repaint();
}
// An instructions step's parts coming home along their arrows.
if self.docs.engine_mut().instructions_insertion_tick(dt) {
ctx.request_repaint();
}
self.docs.show_work_part_bar(ui, self.model_store.as_ref());
if self.show_stl_preview(ui) {
// The preview REPLACES the shell for the frame, and this early
// return used to take the frame's READ drain with it: while a
// preview was open every read-only automation command — `ping`,
// `state_get`, `hit_rects`, a screenshot — went unanswered until
// the host gave up on it, so no script could observe the preview it
// had just opened. The preview publishes its own state and rects
// above; this answers the reads that ask for them.
#[cfg(feature = "automation")]
self.drain_reads(&ctx);
return;
}
if crate::automation::registry::enabled() {
let _publish_span = crate::perf::span(crate::perf::Phase::Publish);
for name in ["__brepImportPreview", "__brepImportRegions"] {
crate::automation::registry::publish(name, crate::automation::state_blobs::doc(name), "null");
}
}
// The tab strip's dirty dots, refreshed once per frame (cheap — see
// `Document::refresh_dirty_marker`).
{
let _t = crate::perf::trace("refresh_dirty_markers");
self.docs.refresh_dirty_markers();
}
// --- boot-load (?loadModel=): apply the fetched model once it lands ----
// Replaces the seed with the URL-specified document (armed in `new`). The
// ehttp callback wakes the frame loop, so a plain per-frame drain suffices.
// `load_model_and_fit` arms deferred framing; the pump above reframes it
// next frame. `mark_clean` opens it as a non-dirty document.
if self.pending_boot_load.is_some() {
let received = self
.pending_boot_load
.as_ref()
.and_then(|rx| rx.try_recv().ok());
if let Some(result) = received {
self.pending_boot_load = None;
apply_linked_model(&mut self.docs, result);
}
}
// --- "Open in CAD" (?open=part/…/rev/…) --------------------------------
if let Some(launch) = self.plm_launch.as_mut() {
use crate::panels::plm_launch::Step;
let step = launch.step(self.model_store.as_ref(), &mut self.docs, &mut self.file);
match &step {
Step::SignIn(sentence) => {
self.settings.show_plm_tab();
self.docs.engine_mut().push_notice(sentence.clone());
}
Step::Refused(sentence) => self.docs.engine_mut().push_notice(sentence.clone()),
_ => {}
}
if step.settles() {
crate::panels::plm_launch::forget_query();
}
if launch.is_done() {
self.plm_launch = None;
} else {
ui.ctx().request_repaint();
}
}
crate::panels::ecad_parts::sync(self.docs.active_mut());
// A sheet's Duplicate past the last offered occurrence: another assembly component.
crate::panels::ecad_parts::answer_duplicate_requests(self.docs.active_mut());
let requests = crate::panels::ecad_parts::take_open_requests(self.docs.active_mut());
for key in requests {
self.file.open_document(&mut self.docs, self.model_store.as_ref(), &key);
}
// The parts pane's Add part button presses Assembly's Add Component: one arm.
if crate::panels::ecad_parts::take_add_part_request(self.docs.active_mut()) {
self.dispatch_workbench_button(crate::workbench::assembly::ADD_COMPONENT_BUTTON_ID);
}
// The eCAD host's Update parts button is Constraints' Update components.
if std::mem::take(&mut self.docs.active_mut().ecad_update_requested) {
if let Err(error) = self.update_components.run(self.docs.engine_mut(), self.model_store.as_ref()) {
self.docs.engine_mut().push_notice(format!("Update components: {error}"));
}
}
// --- update-components badge freshness ---------------------------------
// Keep the outdated-parts checker current BEFORE any assembly panel draws
// (the structure tree renders per-node badges ahead of the constraints
// header). Cheap: a real recompute happens only when an applied run or a
// successful store save moved the generation key.
let synced = self.docs.synced_signatures();
self.update_components.ensure_current(
self.docs.engine_mut(),
self.model_store.as_ref(),
self.file.save_generation(),
&synced,
);
// …and the eCAD host marks the components placed from those parts.
self.docs.active_mut().ecad_outdated = self.update_components.outdated().to_vec();
// An Update that would discard an edit made here asks first.
self.update_components.show_conflict(ui.ctx(), self.docs.engine_mut(), self.model_store.as_ref());
// --- async-safe first-model framing -----------------------------------
// The seed run is async under a background runner (native thread / wasm
// worker), so the boot `zoom_to_fit` may have run before any solid existed.
// Frame the model ONCE, the first frame the seed run has fully landed (solids
// present AND no run still in flight). Under the synchronous Inline runner
// (tests) both hold on the very first frame, so this is identical to today.
if !self.first_run_framed
&& !self.docs.engine().run_pending()
&& self.docs.engine().has_solids()
{
self.docs.engine_mut().zoom_to_fit();
self.first_run_framed = true;
}
self.handle_shortcuts(&ctx);
// A form field's reference pick on a touch or narrow window is a
// FOCUSED pick: every piece of chrome — toolbar and ribbon, workbench
// strip, status bar, docks and the dialog in them, the action overlay
// — steps aside and the view fills the window, with the PICK BAR
// (`panels::pick_bar`: what is picked, the picks, the filter button,
// Finish, Cancel) its only control. Finish or Cancel brings all of it
// back as it was. Decided once, here, before any of it is drawn.
let pick_layout = {
let engine = self.docs.engine();
crate::panels::pick_bar::PickLayout::of(
engine.ref_select_active(),
engine.ref_select_in_form(),
crate::viewport::touch::narrow(&ctx),
)
};
let focused_pick = pick_layout == crate::panels::pick_bar::PickLayout::Focused;
crate::viewport::touch::set_chrome_hidden(&ctx, focused_pick);
// --- top toolbar: primary actions, drawn FIRST so its top strip is
// reserved above the left panel + central viewport. A clicked File button
// returns an action the file dialog acts on (open its modal / save / new).
self.toolbar.read_only = !self.docs.active().access().is_editable();
if crate::viewport::touch::compact(&ctx) {
self.toolbar.documents = document_tabs::MenuState::from_documents(&self.docs);
}
let toolbar_outcome = if focused_pick {
self.toolbar.clear_hits();
crate::panels::toolbar::ToolbarOutcome::default()
} else {
let doc = self.docs.view_mut();
self.toolbar.show(
ui,
&mut doc.engine,
Some(&doc.ecad),
self.model_store.as_ref(),
&mut self.settings.open,
&mut self.part_properties.open,
&mut self.info.open,
)
};
crate::panels::set_chrome_bottom(&ctx, ui.available_rect_before_wrap().top());
if toolbar_outcome.toggle_mobile_panels { self.dock.toggle_mobile_drawer(); }
if crate::viewport::touch::compact(&ctx) {
self.apply_document_actions(toolbar_outcome.documents.activate, toolbar_outcome.documents.close);
self.document_tab_hits = toolbar_outcome.documents.hits;
}
if toolbar_outcome.plugins { self.plugins.open = !self.plugins.open; }
if toolbar_outcome.javascript { self.javascript.open = !self.javascript.open; }
if let Some(id) = toolbar_outcome.plugin_action {
if let Err(error) = self.plugins.open_action(self.docs.engine(), &id) { self.plugins.error = Some(error); }
}
self.plugins.active_document = self.docs.active_id();
self.plugins.show(&ctx, self.docs.engine_mut(), self.model_store.as_ref());
// Run goes to the app's own script runner (its server's code mode),
// started on the first run; installed once the window is opened.
#[cfg(all(not(target_arch = "wasm32"), feature = "mcp"))]
if self.javascript.open && self.javascript.host.is_none() {
self.javascript.host = Some(Box::new(crate::mcp::EditorHost::new(self.automation.clone(), self.diagnostics.adapter_line())));
}
// In the browser: the page's Web Worker runner (src/webmcp.rs).
#[cfg(all(target_arch = "wasm32", feature = "mcp", feature = "scripting"))]
if self.javascript.open && self.javascript.host.is_none() {
self.javascript.host = Some(Box::new(crate::webmcp::PageScripts));
}
self.javascript.show(&ctx, &mut self.docs, self.model_store.as_ref());
// The panels' exports (a plugin bundle, the script) leave through the
// file dialog's export door: the Save-file chooser on desktop, a
// download in the browser.
let exports = self.plugins.take_save_requests().into_iter().chain(self.javascript.take_save_requests());
for request in exports {
if let Err(error) = self.file.request_save_file(&mut self.docs, self.model_store.as_ref(), request) {
self.docs.engine_mut().push_notice(error);
}
}
// An inbox row opens its review in the PLM pane.
for event in toolbar_outcome.plm_review {
self.plm.open_review(event);
}
if let Some(name) = toolbar_outcome.recent_document {
self.file
.open_document(&mut self.docs, self.model_store.as_ref(), &name);
}
if let Some(action) = toolbar_outcome.file {
self.file
.dispatch(action, &mut self.docs, self.model_store.as_ref());
}
// Submit Bug: begin the screenshot-capture + report flow. `request`
// grabs the current frame (before its dialog exists) and the model, so
// it must run THIS frame while the shot is still dialog-free.
if toolbar_outcome.bug_report {
self.bug_report.request(&ctx, self.docs.engine(), &self.diagnostics);
}
// A workbench toolbar button click surfaces its id here; the dispatch
// itself is a METHOD so the `workbench_button` command runs the very
// same arms (a button an agent can only reach by clicking is a button
// an agent cannot reach when something covers it).
if let Some(id) = toolbar_outcome.workbench_button {
self.dispatch_workbench_button(id);
}
// Zoom-to-fit: the ONE button frames whatever the tile draws — the open
// sheet's paper, or the 3D scene. See `BrepApp::zoom_to_fit`.
if toolbar_outcome.zoom_to_fit {
self.zoom_to_fit();
}
// --- workbench actions toolbar: a second strip directly UNDER the
// primary toolbar (egui stacks top panels in call order) listing the
// active workbench's creatable features + the constraint types. Off via
// the Settings checkbox, and never in sketch / reference-selection mode
// (the panel decides — see `WorkbenchToolbarPanel::visible`). A feature
// click adds exactly what the palette pick would, so ACOMP still routes
// to the component selector; a constraint click is the context bar's
// constraint offer, seeded from the current selection.
// A read-only document (a PLM revision not checked out, or released)
// draws the strip disabled: every button on it creates something.
self.workbench_toolbar.read_only = !self.docs.active().access().is_editable();
let actions = if self.docs.engine().settings.toolbar_style
== brep_render::style::ToolbarStyle::Classic
{
self.workbench_toolbar.show(ui, self.docs.engine())
} else {
self.workbench_toolbar
.set_ribbon_hits(self.toolbar.home_hits());
toolbar_outcome.creation
};
if let Some(type_code) = actions.feature {
self.history
.add_feature_of_type(self.docs.engine_mut(), &type_code);
// No `show_pane` here: `HistoryPanel::open_form` moved the target and
// the dialog door surfaces History for it (`surface_open_dialogs`
// below). ACOMP opens the component-selector MODAL instead of a
// feature dialog, so it moves no target and surfaces no pane.
if self.history.take_insert_component_request() {
self.file.dispatch(
FileAction::InsertComponent,
&mut self.docs,
self.model_store.as_ref(),
);
}
}
if let Some(type_id) = actions.constraint {
// The strip offers every type regardless of the selection (unlike the
// context bar's gated offers), so a refusal has to be SAID: a toast,
// never a silent no-op. A document with no components has no assembly
// session to add to (the kernel's own message names the session, not
// the cause) — say what is actually missing.
let engine = self.docs.engine_mut();
if !engine.history_has_assembly() {
engine.push_notice(
"Add constraint: the document has no components — insert a component first"
.to_string(),
);
} else {
// The add leaves the new constraint OPEN (the kernel mints it
// that way), so the dialog door surfaces the Constraints pane.
if let Err(error) =
crate::panels::context_bar::add_constraint_from_selection(engine, &type_id)
{
engine.push_notice(format!("Add constraint: {error}"));
}
}
}
if let Some(type_id) = actions.annotation {
// The context bar's annotation offer, from the strip: seeded from
// the selection, and the new annotation's form opens through the
// dialog door. The strip offers every type whatever is selected, so
// a refusal — no active view to add to — is said, not swallowed.
let engine = self.docs.engine_mut();
if let Err(error) = crate::panels::context_bar::add_pmi_from_selection(engine, &type_id) {
engine.push_notice(format!("Add annotation: {error}"));
}
}
// --- sketch mode has NO strip of its own: its draw tools are workbench
// buttons in the row above (`workbench::sketch`), offered while a sketch
// is being edited, so the 3D viewport is the full-width sketching
// surface with nothing between it and the one toolbar.
// --- bottom STATUS BAR: a persistent, full-width strip whose CONTENT is
// chosen by context each frame. Drawn AFTER the top bars but BEFORE the
// left panel(s) so it reserves the FULL bottom width and the left column
// stops above it (egui resolves reserved space by call order). It is a
// HOST: the branch below picks what to draw. A NEW context is added by
// extending this branch (e.g. `else if engine.some_mode() { … }`) and
// routing through the owning panel's `show_status_bar` for DRY styling.
// Its top is the floor the toasts stack up from (`Toasts::show`).
//
// Its RIGHT END is the app-wide working indicator (`panels::busy`),
// whatever the context: collected here from every source, drawn first
// in a right-to-left row so the context content gets what is left of
// the width and never runs under it.
self.collect_busy(&ctx);
let status_bar_top = if focused_pick {
// The pick bar carries the filter button; nothing of the bar
// stays published.
self.busy.clear_hits();
self.selection.clear_hits();
ui.max_rect().bottom()
} else { egui::containers::panel::Panel::bottom("brep-status-bar")
.resizable(false)
.min_size(30.0)
.show(ui, |ui| {
ui.add_space(2.0);
// `Align::Min`, not `Center`: the bar's height is not known
// until its content is laid out, and centring against the
// unbounded space below doubled the bar with the indicator on
// a row of its own.
ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| {
self.busy.show_status_bar(ui);
ui.with_layout(egui::Layout::top_down(egui::Align::Min), |ui| {
if self.docs.engine().sketch_mode() {
// Sketch context: the status row (title / DOF / N selected /
// undo-redo / Lock). The selection-filter row is NOT drawn
// now, so drop its stale hit-rects (the verifier must never
// click a phantom rect for an off-screen widget).
self.selection.clear_hits();
self.sketch.show_status_bar(ui, self.docs.engine_mut());
} else if self.docs.engine().sketch3d_mode() {
// The 3D sketch's status row: DOF, selection, the
// working plane in force and what the last click hit.
self.selection.clear_hits();
self.sketch3d.show_status_bar(ui, self.docs.engine_mut());
} else if crate::workbench::ecad::Target::of_workbench(&self.docs.engine().settings.workbench).is_some() {
// An eCAD editor owns the central tile, and a click there
// picks nothing 3D: the pickable-kinds filter would be a
// row of dead checkboxes. The strip carries the editor's
// own line instead: where the pointer is and what is under it.
self.selection.clear_hits();
self.viewport.show_ecad_status(ui);
} else {
// Modeling context: the selection filter (pickable kinds).
let document = self.docs.active_id();
self.selection.show_status_bar(ui, self.docs.engine_mut(), document);
}
});
});
ui.add_space(2.0);
})
.response
.rect
.top() };
// Cancel on the working indicator: sent to the document whose run it
// named, which a background tab's run need not be the active one.
if let Some(activity) = self.busy.take_cancel() {
if let Some(doc) = self.docs.iter_mut().find(|doc| Some(doc.id()) == activity.document) {
doc.engine.cancel_run();
}
}
// --- central region: the dock tree, OR (special modes) the bare 3D view
// ---------------------------------------------------------------------
// Normal modeling mode: ONE egui_tiles tree fills the whole remaining
// area between the top toolbar and the bottom status bar. Every side-panel
// section AND the 3D viewport are tiles the user can split / tab / resize /
// drag-rearrange, and the layout persists. Which side panes are visible is
// filtered per-workbench inside the dock (`workbench::panel_visible`).
//
// Sketch mode and reference-selection are "special modes" that take over
// the shell: they BYPASS the tree and draw the viewport directly, so the
// modeling side panes don't appear (sketch's own entity-list panel + the
// top-right mode card own those flows). Drawing the viewport HERE — before
// the top-right overlay below — keeps `viewport.last_rect()` current-frame
// so the overlay anchors to the live 3D-view rect with no lag.
let sketch = self.docs.engine().any_sketch_mode();
// Only a CARD-based pick (a connection point's seat, a spline anchor's
// port) takes the shell over. A FORM FIELD's pick — a feature's, a
// constraint's, an annotation's, a sheet object's — keeps the dock: the
// field is drawn focused in its own pane and the picks go to it, the
// way other CAD applications keep the dialog up while you click in the
// view (`EngineState::ref_select_in_form`).
// Except on a touch or narrow window, where the dock and the dialog
// in it would cover the view and leave nothing to pick: there a form
// field's pick takes the shell over too (`focused_pick`, decided
// before the toolbar is drawn).
let compact = crate::viewport::touch::compact(&ctx);
let entity_drawer_id = egui::Id::new("brep-mobile-sketch-entities");
let mut entity_drawer_open = sketch && ctx.data(|d| d.get_temp::<bool>(entity_drawer_id)).unwrap_or(false);
let mut mobile_sketch_hits: std::collections::HashMap<String, egui::Rect> = std::collections::HashMap::new();
if sketch && compact {
egui::containers::panel::Panel::top("brep-mobile-sketch-controls").resizable(false).show(ui, |ui| {
ui.horizontal(|ui| {
self.mode_bar.card(ui, self.docs.engine_mut());
});
});
self.sketch.clear_list_hits();
self.sketch3d.clear_list_hits();
} else if sketch {
// Sketch entity lists (Curves / Points / Constraints) + solver
// settings — a dedicated left panel, drawn BEFORE the viewport so it
// reserves the left and the viewport fills the rest.
egui::containers::panel::Panel::left("sketch-entities")
.resizable(true)
.default_size(300.0)
.size_range(200.0..=560.0)
.show(ui, |ui| {
if self.docs.engine().sketch3d_mode() {
self.sketch3d.show_entity_lists(ui, self.docs.engine_mut());
} else {
self.sketch.show_entity_lists(ui, self.docs.engine_mut());
}
});
}
self.history.sync_palette_display(self.model_store.as_ref());
// The PLM panels (a PLM session only): each PLM document's access from
// its revision, and the revisions the user asked to open.
let plm = self.plm.sync(self.model_store.as_ref(), &mut self.docs, self.file.save_generation());
for name in plm.open {
self.file.open_document(&mut self.docs, self.model_store.as_ref(), &name);
}
for (name, folder) in plm.opened_from {
if let Some(doc) = self.docs.index_of(&name).and_then(|index| self.docs.get_mut(index)) {
doc.set_plm_folder(Some(folder));
}
}
// The CAM pane's chooser and save requests (a tool import, a library
// export) go through the shell's doors like the PLM pane's; the file
// the chooser delivers goes back to the pane.
let cam_requests = self.cam.take_shell_requests();
if let Some((tag, title)) = cam_requests.pick_file {
if let Err(error) = self.file.request_pick_file(self.model_store.as_ref(), &tag, &title) {
self.file.status = error;
}
}
for request in cam_requests.save_files {
if let Err(error) = self.file.request_save_file(&mut self.docs, self.model_store.as_ref(), request) {
self.docs.engine_mut().push_notice(error);
}
}
if let Some(file) = self.file.take_picked_file(crate::panels::cam::IMPORT_TOOLS_TAG) {
self.cam.deliver_picked(self.docs.engine_mut(), &file.name, &file.bytes);
}
// A PLM section asked for a file (Attach a file…, Add file…): the file
// chooser answers, and the file goes back to the section that asked.
if let Some((tag, title)) = plm.pick_file {
if let Err(error) = self.file.request_pick_file(self.model_store.as_ref(), &tag, &title) {
self.file.status = error;
}
}
for tag in self.plm.pick_tags() {
if let Some(file) = self.file.take_picked_file(&tag) {
self.plm.deliver_picked(&tag, file);
}
}
if plm.refresh_inbox {
self.toolbar.refresh_inbox();
}
if let Some(why) = plm.sign_in_again {
self.settings.ask_plm_sign_in(why);
}
// A PLM section's downloads leave through the same export door.
for request in plm.save_files {
if let Err(error) = self.file.request_save_file(&mut self.docs, self.model_store.as_ref(), request) {
self.docs.engine_mut().push_notice(error);
}
}
// Save As → "A new part" (D9): the PLM pane's New part form, shown.
if self.file.take_plm_new_part() {
self.plm.open_new_part();
self.show_pane(PaneKind::Plm);
}
if self.plm.busy() {
ui.ctx().request_repaint();
}
// Following other clients (`panels::plm_follow`): when the feed moved,
// the inbox asks again too; and the next question gets a frame.
if self.plm.take_followed() {
self.toolbar.refresh_inbox();
}
if let Some(next) = self.plm.next_follow_in() {
ui.ctx().request_repaint_after(std::time::Duration::from_secs_f64(next.max(0.1)));
}
if sketch || !pick_layout.shows_dock() {
self.viewport.set_chrome_obscured(compact && entity_drawer_open);
// Special mode: the viewport fills the remaining central area; no
// dock, no modeling side panes — and therefore no DOCUMENT TAB
// STRIP either, which is the guard that keeps a live sketch /
// reference-pick session from having its document swapped out from
// under it.
self.viewport.show(ui, self.docs.engine_mut());
// No dock, so no tab strip: its last-drawn rects must not stay
// published (the compact strip is the toolbar's, set above).
if !compact { self.document_tab_hits.clear(); }
if focused_pick {
let view = self.viewport.last_rect().unwrap_or(ui.max_rect());
self.pick_bar.show(&ctx, view, self.docs.engine_mut(), &mut self.selection);
// Settled as the dock's branch settles it: the bar claimed the
// pick, so this finishes a single field on its pick.
self.settle_form_pick();
} else {
self.pick_bar.clear_hits();
}
if compact && sketch {
let view = self.viewport.last_rect().unwrap_or(ui.max_rect());
egui::Area::new(egui::Id::new("brep-mobile-sketch-tab"))
.order(egui::Order::Middle).fixed_pos(view.left_top() + egui::vec2(2.0, 4.0)).movable(false)
.show(&ctx, |ui| {
let tab = crate::panels::dock::mobile_tab(ui, "Entities", entity_drawer_open);
mobile_sketch_hits.insert("toggle".into(), tab.rect);
if entity_drawer_open { mobile_sketch_hits.insert("close".into(), tab.rect); }
if tab.clicked() { entity_drawer_open = !entity_drawer_open; ctx.request_repaint(); }
});
if entity_drawer_open {
let panel = egui::Rect::from_min_size(view.left_top() + egui::vec2(52.0, 4.0),
egui::vec2((view.width() - 60.0).min(400.0).max(100.0), (view.height() - 8.0).max(100.0)));
crate::panels::dock::mobile_panel(&ctx, entity_drawer_id, panel, |ui| {
if self.docs.engine().sketch3d_mode() {
self.sketch3d.show_entity_lists(ui, self.docs.engine_mut());
} else {
self.sketch.show_entity_lists(ui, self.docs.engine_mut());
}
});
}
}
DockState::retract_all(DockContext {
docs: &mut self.docs,
plugins: &mut self.plugins,
viewport: &mut self.viewport,
history: &mut self.history,
bom: &mut self.bom,
assembly_constraints: &mut self.assembly_constraints,
wire_harness: &mut self.wire_harness,
pmi: &mut self.pmi,
sheets: &mut self.sheets,
scene: &mut self.scene,
expressions: &mut self.expressions,
qualify: &mut self.qualify,
family_table: &mut self.family_table,
update_components: &mut self.update_components,
model_store: self.model_store.as_ref(),
plm: &mut self.plm,
cam: &mut self.cam,
instructions: &mut self.instructions,
instructions_player: &mut self.instructions_player,
simulation: &mut self.simulation,
});
} else {
self.pick_bar.clear_hits();
// Normal mode: the dock owns the whole central area (the viewport is a
// pane). Cross-panel requests the panels can't act on while their
// borrows are held bubble OUT via the returned outcome — the SAME
// requests the old left-panel closure produced.
let outcome = self.dock.ui(
ui,
DockContext {
docs: &mut self.docs,
plugins: &mut self.plugins,
viewport: &mut self.viewport,
history: &mut self.history,
bom: &mut self.bom,
assembly_constraints: &mut self.assembly_constraints,
wire_harness: &mut self.wire_harness,
pmi: &mut self.pmi,
sheets: &mut self.sheets,
scene: &mut self.scene,
expressions: &mut self.expressions,
qualify: &mut self.qualify,
family_table: &mut self.family_table,
update_components: &mut self.update_components,
model_store: self.model_store.as_ref(),
plm: &mut self.plm,
cam: &mut self.cam,
instructions: &mut self.instructions,
instructions_player: &mut self.instructions_player,
simulation: &mut self.simulation,
},
);
// A form field's pick, settled for the frame: finished when its
// single field has its one pick, when its form asked (an edit
// elsewhere on it), or when no form drew it.
self.settle_form_pick();
// The ACOMP palette pick must open the COMPONENT SELECTOR, never a
// bare feature dialog — the file dialog is shell-owned.
if outcome.insert_component_requested {
self.file.dispatch(
FileAction::InsertComponent,
&mut self.docs,
self.model_store.as_ref(),
);
}
// The DOCUMENT TAB STRIP inside the viewport tile. Activation is
// immediate; a close routes through the file dialog because a dirty
// document has to be confirmed first, and that prompt lives there.
// A pick runs in ONE document's engine: switching or closing a
// document commits a form field's pick first, so it is never left
// running in a document that is no longer on screen.
self.apply_document_actions(outcome.document_tabs.activate, outcome.document_tabs.close);
if !compact { self.document_tab_hits = outcome.document_tabs.hits; }
// A structure-tree Edit — or a BOM row's action button, which
// reports through the same outcome field so there is one arm and
// not two — expands its feature in the history tree.
if let Some(focus) = outcome.feature_focus {
self.history.focus_feature(focus);
if compact { self.dock.show_pane(PaneKind::History); }
}
// Structure-tree interaction hooks route through the SAME dispatcher
// as the context bar (one truth per action); document-level flows
// (edit-in-place / open-part) come back as requests the shell runs.
// A BOM row menu's document-level flow: its engine-mutating half
// already ran inside the panel, through the same dispatcher.
// Double-clicking empty space leaves the Work part.
if self.viewport.take_empty_double_click() && self.docs.work_part_path().is_some() {
if let Err(error) = self.docs.return_to_assembly() {
self.docs.engine_mut().push_notice(format!("Work part: {error}"));
}
}
if let Some(request) = outcome.component_request.or_else(|| self.viewport.take_component_request()) {
self.run_component_request(request, compact);
}
}
self.history.sync_palette_display(self.model_store.as_ref());
// --- file dialog: a ctx-level modal (like the command palette), drawn
// after the panels so its backdrop dims the whole shell. Idempotent when
// closed; also polls for a completed async import each frame.
self.file
.show(&ctx, &mut self.docs, self.model_store.as_ref());
// --- crash recovery: the boot prompt, then the debounced autosave -----
// Drawn with the same ctx-level modal treatment as the file dialog. The
// autosave ticks only once the prompt has resolved (or never existed).
if self.recovery.is_open() {
if let Some(resolution) =
self.recovery.show(&ctx, &mut self.docs, self.model_store.as_ref())
{
self.autosave.note_cleared();
if let crate::recovery::Resolution::Restored(count) = resolution {
self.docs.engine_mut().push_notice_as(NoticeSeverity::Info, format!(
"Restored {count} unsaved document{}",
if count == 1 { "" } else { "s" }
));
}
}
} else {
self.recovery.clear_hits();
let now = ctx.input(|i| i.time);
if let Some(due) = self.autosave.tick(&self.docs, self.model_store.as_ref(), now) {
// The frame loop idles between inputs; wake it when the write is due.
ctx.request_repaint_after(std::time::Duration::from_secs_f64(due.max(0.05)));
}
}
// --- desktop update notice: an installed update closes the app through
// the close path below, which keeps unsaved work for recovery.
#[cfg(not(target_arch = "wasm32"))]
if self.updater.show(&ctx) {
self.close_asked = true;
}
self.close_when_written(&ctx);
if let Some((format, name, bytes)) = self.file.take_stl_import() {
self.start_mesh_preview(format, name, bytes);
ctx.request_repaint();
}
// --- Submit Bug: the screenshot-capture state machine + report modal.
// Drawn at ctx level like the file dialog; idempotent while idle. Draws
// NOTHING during capture, so the screenshot it requested never contains
// this dialog.
if let Some(report) = crate::form_view::take_problem_report_request(&ctx) {
self.bug_report.request_for_error(&ctx, self.docs.engine(), &self.diagnostics, &report.dialog, &report.error);
}
self.bug_report.show(&ctx, self.docs.engine_mut());
if let Some((id, summary)) = self.bug_report.take_submitted() {
self.report_followup.record(id, summary);
}
self.report_followup.tick(&ctx, self.docs.engine_mut());
self.sketch.show_text_dialog(&ctx, self.docs.engine_mut());
// --- Info: the licences + this session's diagnostics + the MCP start
// button, a floating window toggled from the toolbar's info button.
// Idempotent when closed. The server's state is read here, each frame
// the window is open, and a click is answered here: the shell owns the
// queue the server attaches to and the adapter line its session names.
#[cfg(all(not(target_arch = "wasm32"), feature = "mcp"))]
let mcp_view = if self.info.open { crate::mcp::info_view() } else { crate::panels::info::McpView::Unavailable };
#[cfg(not(all(not(target_arch = "wasm32"), feature = "mcp")))]
let mcp_view = crate::panels::info::McpView::Unavailable;
self.info.show(&ctx, &self.diagnostics, &mcp_view);
#[cfg(all(not(target_arch = "wasm32"), feature = "mcp"))]
if self.info.take_mcp_start() {
// The failure is already in `mcp::status()`, which the window
// shows next frame, and on stderr in the same words.
let _ = crate::mcp::start_from_window(self.automation.clone(), self.diagnostics.adapter_line());
}
// --- Settings: a floating (movable + resizable) window, toggled from the
// toolbar gear button, drawn at ctx level like Properties so it floats
// over the shell. Idempotent when closed. Replaces the old sidebar section.
// This preview runs every frame, including when Settings is closed.
// Exact dirty checks serialize the whole assembly; use the tab's cached
// marker here. finish_reconnect still checks the exact saved bytes before
// switching stores, including metadata edits the marker may not see.
self.settings.reconnect_blocker =
self.reconnect_blocker_with(crate::document::Document::dirty_marker);
self.settings
.show(&ctx, self.docs.engine_mut(), self.model_store.as_ref());
// Signed out here (Forget): the server's refusal that follows is the
// person's own doing, not news to prompt about.
if self.settings.take_plm_signed_out_here() {
self.plm.signed_out_here();
}
// Signed in again after the server refused this session's token: the
// live session takes the new token, for the same user, and goes on with
// its open tabs (no store switch, nothing to close).
if self.settings.plm_signed_out() {
if let (Some((username, token)), Some(client)) = (self.settings.take_fresh_plm_token(), self.model_store.plm_client()) {
if client.me().is_none_or(|me| me.username == username) {
client.replace_token(&token);
self.plm.follow_now();
} else {
self.docs.engine_mut().push_notice(format!(
"Signed in as {username}, not {}: restart the app (or Connect now) to use the PLM as {username}",
client.me().map(|me| me.username).unwrap_or_default()
));
}
}
}
// The PLM tab's Connect now: switch this session to the PLM live.
#[cfg(not(target_arch = "wasm32"))]
if let Some(root) = self.settings.take_reconnect_request() {
if let Err(why) = self.start_reconnect(root) {
self.settings.reconnected(Err(why));
}
}
#[cfg(not(target_arch = "wasm32"))]
{
self.poll_reconnect();
self.settings.reconnecting = self.reconnecting.is_some();
if self.reconnecting.is_some() {
// The phase limit is checked here: wake for it on a quiet frame.
ctx.request_repaint_after(std::time::Duration::from_millis(250));
}
}
// --- Part Properties: the active document's own BOM attribute record,
// in a floating window beside Settings. The title is passed in because
// the panel takes only the engine, and a user with several tabs open
// must be able to see WHICH part they are annotating.
let part_title = self.docs.active().title();
let part_document = self.docs.active().id();
self.part_properties.configure_plm(self.model_store.plm_client(), self.docs.active().name());
self.part_properties
.show(&ctx, self.docs.engine_mut(), &part_title, part_document);
if std::mem::take(&mut self.assembly_constraints.review_replace_requested) {
self.review_replace();
}
self.replace_dialog
.show(&ctx, self.docs.engine_mut(), self.model_store.as_ref());
ctx.data_mut(|d| d.insert_temp(entity_drawer_id, entity_drawer_open));
// --- top-right overlay column: the special-mode EXIT card (Finish/Cancel
// for reference-selection / sketch mode) stacked ABOVE the selection-driven
// CONTEXT ACTION rail. Both cards live in ONE ctx-level Area anchored
// top-right so they never overlap, and the context rail uses the SAME
// renderer whether it is showing modeling actions or sketch actions
// (`panels::action_rail`). A modeling create/edit action returns a feature
// id to expand in the history tree.
#[cfg(all(feature = "automation", target_arch = "wasm32"))]
crate::automation::registry::publish("__brepMobileActions", "compact action menu button rectangle", "{}");
if focused_pick || (compact && (entity_drawer_open || (!sketch && self.dock.mobile_drawer_open()))) {
crate::panels::action_rail::close_mobile_actions(&ctx);
self.mode_bar.clear_hits();
self.sketch.clear_context_hits();
self.sketch3d.clear_context_hits();
self.context_bar.clear_view();
} else {
let mut focus: Option<String> = None;
let mut info_targets: Vec<String> = Vec::new();
let mut plugin_action = None;
let mut component_request: Option<ComponentActionRequest> = None;
// Anchor the overlay to the RIGHT edge of the 3D VIEW (the viewport
// tile), not the window — so it stays glued to the viewport wherever
// docking frames it. The viewport was drawn earlier THIS frame, so its
// rect is current. Before the first draw (`None`) fall back to the
// window's top-right.
let mut overlay = egui::Area::new(egui::Id::new("brep-top-right-overlay"))
.order(egui::Order::Foreground);
overlay = match self.viewport.last_rect() {
Some(rect) => overlay
.fixed_pos(rect.right_top() + egui::vec2(-12.0, 8.0))
.pivot(egui::Align2::RIGHT_TOP),
None => overlay.anchor(egui::Align2::RIGHT_TOP, egui::vec2(-12.0, 56.0)),
};
overlay
.show(&ctx, |ui| {
// 0. The family-row preview's card: which row, and Exit.
self.family_table.preview_card(ui, self.docs.engine_mut());
// 1. Exit controls for whatever special mode is active.
if !(compact && sketch) { self.mode_bar.card(ui, self.docs.engine_mut()); }
// 2. Context actions: sketch actions in sketch mode, else the
// modeling selection actions. Same rail, mode-appropriate items.
let has_actions = if self.docs.engine().sketch_mode() { self.docs.engine().sketch_selection_count() > 0 }
else if self.docs.engine().sketch3d_mode() { self.docs.engine().sketch3d_selection_count() > 0 }
else { self.docs.engine().has_selection() || self.docs.engine().selected_constraint().is_some() };
let mut draw_actions = |ui: &mut egui::Ui| {
if self.docs.engine().sketch_mode() {
self.sketch.context_card(ui, self.docs.engine_mut());
} else if self.docs.engine().sketch3d_mode() {
self.sketch3d.context_card(ui, self.docs.engine_mut());
} else {
// Room down to the ViewCube's top, from wherever the
// cards above left the cursor.
self.context_bar.max_height = if compact { None } else { self.viewport.last_rect().and_then(|view| {
let cube: serde_json::Value =
serde_json::from_str(&self.docs.engine().viewcube_rect_json()).ok()?;
let (top, h) = (cube["y"].as_f64()? as f32, cube["h"].as_f64()? as f32);
(h > 0.0).then(|| view.top() + top - 8.0 - ui.cursor().top())
}) };
let outcome = self.context_bar.card(ui, self.docs.engine_mut());
focus = outcome.focus;
info_targets = outcome.info_targets;
plugin_action = outcome.plugin_action;
component_request = outcome.component;
}
};
if compact {
if has_actions {
ui.set_min_width(110.0);
let button = ui.add_sized([110.0, 44.0], egui::Button::new("Actions"));
if button.clicked() { crate::panels::action_rail::open_mobile_actions(ui.ctx()); }
draw_actions(ui);
#[cfg(all(feature = "automation", target_arch = "wasm32"))]
crate::automation::registry::publish("__brepMobileActions", "shared action chooser and entry rectangles",
&crate::panels::action_rail::mobile_actions_json(ui.ctx(), button.rect));
} else {
crate::panels::action_rail::close_mobile_actions(ui.ctx());
self.sketch.clear_context_hits();
self.sketch3d.clear_context_hits();
self.context_bar.clear_view();
}
} else { draw_actions(ui); }
});
if let Some(focus) = focus {
self.history.focus_feature(focus);
if compact { self.dock.show_pane(PaneKind::History); }
}
// The Info action returns one target per selected entity — open (or, on
// dedup, keep) a pinned Info window for each. Drawn below.
if let Some(id) = plugin_action {
if let Err(e) = self.plugins.open_action(self.docs.engine(), &id) { self.plugins.error = Some(e); }
}
if !info_targets.is_empty() {
self.info_windows.open_for(&info_targets, self.viewport.last_rect());
}
// Component document-level flows (the engine-mutating component
// actions already ran inside the bar).
if let Some(request) = component_request {
self.run_component_request(request, false);
}
}
if compact { crate::panels::action_rail::show_mobile_actions(&ctx); }
// --- THE DIALOG DOOR: every dynamically generated dialog that opened
// this frame makes its host pane's tab current.
//
// ONE call, placed after everything that can open one — the dock's panes
// and viewport, and the context bar's overlay above — so a form opened
// anywhere is surfaced in the same frame, before `__brepDock` publishes
// the active tab below. It watches the TARGETS rather than the callers
// because most of them set the target inside the engine or inside a
// panel's draw, with no dock in reach; `panels::dock::DialogTargets`
// carries that reasoning and the openers it covers.
let targets = crate::panels::dock::DialogTargets::read(
&self.history,
self.docs.engine(),
self.docs.active_id(),
);
self.dock.surface_opened_dialogs(targets);
// --- Info windows: the pinned per-entity inspector windows, drawn at ctx
// level like the file dialog so they float over the shell. Each is pinned to
// its open-time object name (selection changes never retarget them); closed
// windows (their `×`) are pruned here. Drawn AFTER the context bar so a
// window opened THIS frame paints this frame.
self.info_windows.show(&ctx, self.docs.engine_mut());
// --- interference results window: same floating idiom, owned report;
// its Re-run button re-drives the engine check.
self.interference.show(&ctx, self.docs.engine_mut());
// --- auto-constraints window: the inference scan + its Create button.
self.auto_constraints.show(&ctx, self.docs.engine_mut());
self.step_parts
.show(&ctx, self.docs.engine_mut(), self.model_store.as_ref());
// --- transient toasts: drain the engine's queued notices (e.g. a sketch
// solve that failed after an edit) and show each briefly. Drawn last so
// the cards float over the whole shell.
let now = ctx.input(|i| i.time);
// All three sources gathered FIRST, because the automation queue must see
// every one of them: `Toasts::extend` keeps only the last few on screen,
// and a message dropped for space is exactly the one a host wants told.
use brep_render::engine_state::NoticeSeverity;
let raised = self.gather_notices();
self.toasts.extend(raised, now);
// An edit after a card was raised supersedes it (`Toasts::note_document`).
self.toasts
.note_document(self.docs.active_id(), self.docs.engine().history.undo_top(), now);
self.toasts.show(&ctx, self.viewport.last_rect(), status_bar_top);
// The state registry (`automation::registry`): the live app + engine
// state, published by name with a one-line doc so a host can read it
// (and, on wasm, mirrored to `window.__brep*` for the verify scripts).
// Purely additive; no render effect. Published AFTER the panels draw so
// the hit-rects are for THIS frame's layout. Off unless a host enabled it.
if crate::automation::registry::enabled() {
let _publish_span = crate::perf::span(crate::perf::Phase::Publish);
// Every blob that is plain app state, from the one producer
// `state_get` also asks at dispatch time (`state_blob_json`).
self.publish_state_blobs(&ctx);
// Keyboard focus, for a driver typing into a widget by its rect: the
// frame counter says a click has been drawn, `rect` is the widget
// egui gave the focus to (null while nothing has it). A typed
// string lands only in a focused field, so a verifier waits on this
// rather than on time (BREP_app/web/verify_plm_helpers.mjs).
let focused = crate::column_tree::focused_rect(ui.ctx());
// The focused value editor's text, taken (not left) so a frame
// with no focused editor publishes null.
let text: Option<String> = ui.ctx().data_mut(|d| d.remove_temp::<String>(crate::column_tree::focused_text_id())).filter(|_| focused.is_some());
crate::automation::registry::publish("__brepFocus", "keyboard focus {frame, rect:[x,y,w,h]|null (the focused widget), text:string|null (a value editor's in-progress string)}", &serde_json::json!({
"frame": ui.ctx().cumulative_frame_nr(),
"rect": focused.map(|r| [r.min.x, r.min.y, r.width(), r.height()]),
"text": text,
}).to_string());
crate::automation::registry::publish("__brepFileHit", "file dialog widget rects", &self.file.hits_json());
crate::automation::registry::publish("__brepStatusHit", "status bar widget rects: busy, busy:more, busy:cancel (present only while the working indicator shows)", &self.busy.hits_json());
crate::automation::registry::publish("__brepHit", "history panel widget rects: step:i edit:i del:i box:i add:menu form:* field:* panel:clip", &self.history.hits_json());
crate::automation::registry::publish("__brepPaletteHit", "add-feature palette widget rects in screen points: input top item:type display display#mode (empty while closed)", &self.history.palette_hits_json());
crate::automation::registry::publish("__brepExprHit", "expressions panel widget rects", &self.expressions.hits_json());
crate::automation::registry::publish("__brepToolbar", "toolbar rects: shared File menu file:* and workbench:item:<id>; Ribbon tabs/overflow ribbon:* and Home creation wbtb:*; command keys undo redo fit projection wireframe show:* help info bug workbench:btn:<id>. Classic wraps its controls; Ribbon compacts Large commands before whole-group overflow. Keys appear when their widgets are rendered; menu entries appear while open", &self.toolbar.hits_json());
// The workbench actions strip's button rects (`wbtb:feature:<type>` /
// `wbtb:constraint:<type>` / `wbtb:annotation:<type>`); an empty map
// while the strip is hidden.
crate::automation::registry::publish("__brepWorkbenchToolbar", "creation rects: wbtb:* feature:type constraint:type annotation:type, from the Classic workbench strip or Ribbon Home; empty while hidden or another Ribbon tab is selected", &self.workbench_toolbar.hits_json());
// The PLM pane (a PLM session only): its sections' widget rects,
// `plm/section:<id>` and `plm/<section>:<widget>`, and the active
// document's access and revision.
crate::automation::registry::publish("__brepPlmHit", "PLM pane rects: section:lifecycle, lifecycle:revision:<label>, lifecycle:verb:check-out, save:<yes|no|remember>, web:<summary|revisions|attachments|structure|history|parts|workspace|reviews>", &self.plm.hits_json());
crate::automation::registry::publish("__brepToastsHit", "toast card rects (toast:<i> top to bottom, toast:more)", &self.toasts.hits_json());
crate::automation::registry::publish("__brepBugHit", "bug report panel widget rects", &self.bug_report.hits_json());
// The JavaScript editor window: its outer rect against the surface
// it may fill, and its widget rects (`javascript/panel:clip`,
// `javascript/source`, `javascript/output`, the buttons).
crate::automation::registry::publish("__brepJavascript", "JavaScript editor window {open, rect:[x,y,w,h] or null while closed, surface:[w,h], running, output}", &self.javascript.state_json());
crate::automation::registry::publish("__brepJavascriptHit", "JavaScript editor window widget rects (panel:clip, run, cancel, save, open, export, help, example, source, output); empty while the window is closed", &self.javascript.hits_json());
// The wire harness: the document's connections + the last run's
// routing report (engine truth), and the panel's widget rects.
crate::automation::registry::publish("__brepWireHarnessHit", "wire harness panel widget rects", &self.wire_harness.hits_json());
// The CAM block (setups with their operations, tools, operations,
// NC programs) plus the pane's selection and last notice, and the
// pane's rects (`cam:*`).
self.cam.refresh_geometry(self.docs.engine());
crate::automation::registry::publish("__brepCamHit", "CAM pane widget rects (cam:*)", &self.cam.hits_json());
crate::automation::registry::publish("__brepInstructionsHit", "Instructions pane widget rects (instructions:*)", &self.instructions.hits_json());
crate::automation::registry::publish("__brepInstructionsPlayerHit", "Player pane widget rects (player:*)", &self.instructions_player.hits_json());
crate::automation::registry::publish("__brepSimulationHit", "Simulation pane widget rects (simulation:*)", &self.simulation.hits_json());
// The part's declared connection points (the `ports` block) and the
// ports tail's report, beside the Qualify panel's own selection and
// its rects (`qualify:*`).
crate::automation::registry::publish("__brepQualifyHit", "Qualify panel widget rects (qualify:*)", &self.qualify.hits_json());
// The family table as the Family table pane last read it, its
// problems, selection and the last Generate report; and its rects.
crate::automation::registry::publish("__brepFamilyHit", "Family table pane widget rects (family:*)", &self.family_table.hits_json());
// The PMI block + report + active view / open annotation, and the
// PMI panel's rects (`pmi:capture`, `pmi:add`, `pmi:add:<type>`,
// `pmi:row:<id>`, `pmi:cell:<id>:<column>`, `pmi:menu:<id>`, the
// form's `pmi:` keys).
crate::automation::registry::publish("__brepPluginsHit", "Declarative plugin panel action controls", &self.plugins.panel_hits_json());
crate::automation::registry::publish("__brepPmiHit", "PMI panel widget rects (pmi:*)", &self.pmi.hits_json());
// The `sheets` block, which sheet the viewport draws, and the OPEN
// sheet's whole projection — the paper, the visible edge runs and
// the annotations that passed the parallel rule, in millimetres.
// The panel's rects (`sheets:add`, `sheets:place:<view>`,
// `sheets:row:<id>`, the form's `sheets:` keys) ride beside it.
crate::automation::registry::publish("__brepSheetsHit", "Sheets panel widget rects (sheets:*)", &self.sheets.hits_json());
crate::automation::registry::publish("__brepInfoWindowsHit", "info window widget rects", &self.info_windows.hits_json());
crate::automation::registry::publish("__brepInterferenceHit", "interference panel widget rects", &self.interference.hits_json());
crate::automation::registry::publish("__brepAutoConstraintsHit", "auto-constraints widget rects", &self.auto_constraints.hits_json());
crate::automation::registry::publish("__brepStepPartsHit", "STEP parts library widget rects", &self.step_parts.hits_json());
crate::automation::registry::publish("__brepSelectionHit", "selection bar widget rects (filter:kind, clear, hide)", &self.selection.hits_json());
crate::automation::registry::publish("__brepContextHit", "context action bar widget rects", &self.context_bar.hits_json());
crate::automation::registry::publish("__brepPickBarHit", "focused pick bar widget rects (bar, finish, cancel, x<i>)", &self.pick_bar.hits_json());
crate::automation::registry::publish("__brepModeBarHit", "mode bar widget rects (refsel:*, Sketch:*)", &self.mode_bar.hits_json());
// The DOCUMENT TABS: the open models, which one is active, and the
// strip's per-tab hit-rects, so an e2e script can switch and close
// documents the way a user does.
crate::automation::registry::publish(
"__brepDocumentsHit",
"document tab strip widget rects",
&crate::automation::hit_rects::hits_json(
self.document_tab_hits.iter().map(|(key, rect)| (key, rect)),
),
);
// The boot-time recovery prompt: its entries and its two buttons.
crate::automation::registry::publish("__brepRecoveryHit", "recovery prompt widget rects", &self.recovery.hits_json());
#[cfg(target_arch = "wasm32")]
crate::automation::registry::publish("__brepMobileDock", "compact dock drawer controls", &self.dock.mobile_state_json());
#[cfg(target_arch = "wasm32")]
crate::automation::registry::publish("__brepMobileSketch", "compact sketch entity drawer controls", &serde_json::json!({
"compact": compact, "open": entity_drawer_open,
"rects": serde_json::from_str::<serde_json::Value>(&crate::automation::hit_rects::hits_json(mobile_sketch_hits.iter())).unwrap_or_default(),
}).to_string());
crate::automation::registry::publish("__brepSketchHit", "sketch-mode overlay widget rects: the SKETCH ACTIONS card (constraint:glyph fix construction cleanup delete) \u{2014} empty out of sketch mode. The draw tools are NOT here: they are workbench buttons in the toolbar row (toolbar/workbench:btn:sketch.tool.* and sketch.autoconstrain)", &self.sketch.hits_json(self.docs.engine()));
crate::automation::registry::publish("__brepSketchListHit", "sketch entity-list widget rects: section:* geometry:id point:id constraint:id del:kind:id panel:clip (empty out of sketch mode)", &self.sketch.list_hits_json(self.docs.engine()));
crate::automation::registry::publish("__brepSketch3dHit", "3D sketch-mode overlay widget rects: the 3D SKETCH ACTIONS card (constraint:type fix construction delete) \u{2014} empty out of 3D sketch mode. The draw tools and the working-plane radio are workbench buttons in the toolbar row (toolbar/workbench:btn:sketch3d.tool.* and sketch3d.plane.*)", &self.sketch3d.hits_json(self.docs.engine()));
crate::automation::registry::publish("__brepSketch3dListHit", "3D sketch entity-list widget rects: section:* geometry:id point:id constraint:id del:kind:id reference:name unlink:name panel:clip (empty out of 3D sketch mode)", &self.sketch3d.list_hits_json(self.docs.engine()));
// Viewport origin + projected probe points (viewport-local logical
// px) so the verifier can click precise spots ON the Box and ON the
// Pin during ref-select mode. Index 0 is a Box top-corner clear of the
// pin; indices 1..4 are points on the Pin's cylindrical stub that
// protrudes above the Box top (y=20), on the camera-facing sides — the
// verifier tries them until one picks "Pin".
crate::automation::registry::publish("__brepView", "the 3D viewport rect {x,y,w,h} in egui points", &self.viewport.viewport_rect_json());
// Dock layout snapshot (per-pane visible / rendered) so the verifier
// can see which side panels are on-screen and, once a user tabs panels
// together, activate the right tab before asserting on its widgets.
// `active=false` in sketch / ref-select (the dock is bypassed).
crate::automation::registry::publish("__brepDock", "dock layout in tab-strip order: per-pane visible/activeTab/rendered", &self.dock.state_json(!sketch && pick_layout.shows_dock()));
crate::automation::registry::publish("__brepProbe", "projected seed-model probe points (viewport-local px) for the verifier",
&self
.docs
.engine()
.world_to_screen_json(
"[[2.0,20.0,2.0],[14.243,22.5,14.243],[16.0,22.5,10.0],\
[10.0,22.5,16.0],[10.0,25.0,10.0]]",
)
.unwrap_or_else(|_| "[]".to_string()),
);
}
// The viewport renders only a dirty engine. A panel drawn after it this
// frame may have marked it dirty: draw that on the next frame instead of
// waiting for the next input event.
if self.docs.engine().dirty {
ctx.request_repaint();
}
// Phase 3 of the automation frame (§4.4): reads answer from THIS frame's
// registry and layout.
#[cfg(feature = "automation")]
self.drain_reads(&ctx);
#[cfg(target_arch = "wasm32")]
crate::viewport::touch::publish_editor(&ctx);
// NOTE: the 3D viewport is no longer drawn here — it is a dock tile drawn
// earlier this frame (normal mode) or drawn directly in the sketch /
// ref-select branch above. Drawing it before the top-right overlay is what
// keeps that overlay anchored to the live viewport rect.
}
}
/// Mirror an engine JSON string to `window.<name>` (wasm/verification only).
/// Whether a launch opens on [`seed_history_json`]: the caller asked for the
/// seed and the user has not turned it off with the "Start with an empty
/// document" setting (`disableStartupModel`).
pub(crate) fn starts_on_seed_model(seed: bool, settings: &brep_render::style::RenderSettings) -> bool {
seed && !settings.disable_startup_model
}
/// The seed model handed to the engine at startup: a 3-feature history so the
/// tree / roll / edit are real —
/// 0. `P.CU` "Box" — a 20 mm cube at the origin (spans `[0,20]³`).
/// 1. `P.CY` "Pin" — a r=6, h=30 cylinder (axis +Y) positioned to pierce the
/// cube through its centre in XZ (x=10, z=10) from below (y=-5) to above.
/// 2. `B` "Cut" — SUBTRACT: `targetSolid = Box`, tools `[Pin]` → the cube
/// with a cylindrical through-hole (the ref-select field is visible for the
/// next slice). Roll-to-step shows: cube → cube+cylinder → subtracted cube.
///
/// This is just the INITIAL document — once handed to `EngineState`, the engine
/// OWNS the mutable history; the app keeps no copy.
pub(crate) fn seed_history_json() -> String {
serde_json::json!({
"expressions": "",
"configurator": {},
"features": [
{
"type": "P.CU",
"inputParams": {
"id": "Box",
"sizeX": 20.0, "sizeY": 20.0, "sizeZ": 20.0,
"transform": {
"position": [0.0, 0.0, 0.0],
"rotationEuler": [0.0, 0.0, 0.0],
"scale": [1.0, 1.0, 1.0]
},
"boolean": { "targets": [], "operation": "NONE", "mergeCoplanarFaces": true }
},
"persistentData": {}
},
{
"type": "P.CY",
"inputParams": {
"id": "Pin",
"radius": 6.0, "height": 30.0,
"transform": {
"position": [10.0, -5.0, 10.0],
"rotationEuler": [0.0, 0.0, 0.0],
"scale": [1.0, 1.0, 1.0]
},
"boolean": { "targets": [], "operation": "NONE", "mergeCoplanarFaces": true }
},
"persistentData": {}
},
{
"type": "B",
"inputParams": {
"id": "Cut",
"targetSolid": "Box",
"boolean": { "operation": "SUBTRACT", "targets": ["Pin"], "mergeCoplanarFaces": true }
},
"persistentData": {}
}
]
})
.to_string()
}
/// Plain-state blobs whose whole input is cheap to compare: each is rebuilt
/// only when that input changed, not on every frame the registry publishes
/// (the browser build publishes for every page, as its WebMCP host attaches at
/// boot). A key holds EVERY input its producer reads, so a cached blob is
/// never stale; a blob whose inputs have no such key is built every frame.
#[derive(Default)]
struct StateBlobCache {
settings: Option<((brep_render::style::RenderSettings, bool), String)>,
workbench: Option<((String, serde_json::Value), String)>,
model: Option<(ModelSignatureKey, String)>,
}
/// `(rollback step, [(name, triangles, bbox min, bbox max)])`.
type ModelSignatureKey = (usize, Vec<(String, usize, [f64; 3], [f64; 3])>);
impl StateBlobCache {
/// The cached blob when its key is `key`.
fn hit<K: PartialEq>(slot: &Option<(K, String)>, key: &K) -> Option<String> {
slot.as_ref().filter(|(cached, _)| cached == key).map(|(_, json)| json.clone())
}
/// The blob for `key`: the cached one, else `build`'s, kept under `key`.
fn keyed<K: PartialEq>(slot: &mut Option<(K, String)>, key: K, build: impl FnOnce() -> String) -> String {
if let Some(json) = Self::hit(slot, &key) {
return json;
}
let json = build();
*slot = Some((key, json.clone()));
json
}
}
/// Everything `BrepApp::model_signature_json` reports, as a comparable key.
fn model_signature_key(engine: &EngineState) -> ModelSignatureKey {
let solids = engine
.scene
.solids()
.iter()
.map(|s| (s.name.clone(), s.mesh.indices.len() / 3, s.bbox.min, s.bbox.max))
.collect();
(engine.history_rollback(), solids)
}
/// Which door a file named on the command line goes through
/// ([`BrepApp::open_path_from_command_line`]).
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CommandLineLane {
/// A model document: File > Open's door.
Open,
/// A file File > Import takes: its importer, into a new untitled tab.
Import,
/// Neither: said so, nothing opened.
Refuse,
}
/// The lane for a command-line path, by its extension (any case). A name with
/// no extension keeps today's Open (the store adds `.nbrep`).
#[cfg(not(target_arch = "wasm32"))]
fn command_line_lane(path: &str) -> CommandLineLane {
if crate::panels::file::import_kind(path).is_some() {
return CommandLineLane::Import;
}
let base = path.rsplit(['/', '\\']).next().unwrap_or(path);
match base.rsplit_once('.') {
None => CommandLineLane::Open,
Some((_, ext)) if crate::store::MODEL_EXTENSIONS.contains(&ext.to_ascii_lowercase().as_str()) => CommandLineLane::Open,
Some(_) => CommandLineLane::Refuse,
}
}