jackdaw 0.4.0

A 3D level editor built with Bevy
Documentation
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
use std::path::{Path, PathBuf};

use bevy::{
    prelude::*,
    tasks::{AsyncComputeTaskPool, Task, futures_lite::future},
    window::{PrimaryWindow, RawHandleWrapper},
};
use jackdaw_feathers::{
    button::{ButtonVariant, IconButtonProps, icon_button},
    icons::{EditorFont, Icon},
    text_edit::{TextEditProps, TextEditValue, text_edit},
    tokens,
};
use rfd::{AsyncFileDialog, FileHandle};

use crate::{
    AppState,
    new_project::{ScaffoldError, TemplateLinkage, TemplatePreset, scaffold_project},
    project::{self, ProjectRoot},
    scene_io,
};

pub struct ProjectSelectPlugin;

impl Plugin for ProjectSelectPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<NewProjectState>()
            .init_resource::<crate::build_status::BuildStatus>()
            .add_systems(OnEnter(AppState::ProjectSelect), spawn_project_selector)
            .add_systems(
                Update,
                (
                    poll_folder_dialog,
                    poll_template_folder_dialog,
                    refresh_build_progress_ui,
                )
                    .run_if(in_state(AppState::ProjectSelect)),
            )
            // Build-progress polling and task draining run in BOTH
            // states. The static-template scaffold flow transitions
            // to Editor immediately after scaffolding succeeds, but
            // the build task keeps running in the background; the
            // status_bar's progress region depends on
            // `refresh_build_progress_snapshot` to keep updating,
            // and `poll_new_project_tasks` needs to drain the build
            // task when it completes (otherwise the progress
            // indicator would never collapse). They're cheap no-ops
            // when nothing is in flight.
            //
            // `drive_static_editor_build` runs in both states too:
            // it kicks off the user's editor-binary build in the
            // background after scaffold or open-existing, polls for
            // completion, and updates `BuildStatus` so the status
            // bar / click handler can render and dispatch the
            // handoff.
            .add_systems(
                Update,
                (
                    poll_new_project_tasks,
                    refresh_build_progress_snapshot,
                    drive_static_editor_build,
                ),
            )
            // The dylib-install step MUST run outside of `Update`'s
            // `schedule_scope`. The game's `GameApp::add_systems(Update, …)`
            // inserts into `Schedules`; doing that while bevy has
            // `Update` checked out via `schedule_scope` causes the
            // modification to be overwritten when the scope re-inserts
            // at exit. `Last` has its own scope and doesn't clash.
            .add_systems(
                Last,
                (apply_pending_install, apply_pending_static_open)
                    .run_if(in_state(AppState::ProjectSelect)),
            );
    }
}

/// Marker for the project selector root UI node.
#[derive(Component)]
struct ProjectSelectorRoot;

/// When set, the project selector will skip UI and auto-open the given project.
#[derive(Resource)]
pub struct PendingAutoOpen {
    pub path: PathBuf,
    /// `true` when we got here via a post-restart auto-open ;
    /// the parent process already built + installed the dylib,
    /// so we skip that step (preventing an infinite
    /// build→restart→auto-open→build loop).
    pub skip_build: bool,
}

/// Resource holding the async folder picker task.
#[derive(Resource)]
struct FolderDialogTask(Task<Option<rfd::FileHandle>>);

/// Resource holding the async folder picker task for the template-
/// folder Browse button in the New Project modal. Kept separate from
/// `FolderDialogTask` (which routes results into the Location field).
#[derive(Resource)]
struct TemplateFolderDialogTask(Task<Option<rfd::FileHandle>>);

/// Root marker for the New Project modal overlay. Spawned when the
/// user clicks **+ New Extension** / **+ New Game**; despawned on
/// Cancel or on successful scaffold.
#[derive(Component)]
struct NewProjectModalRoot;

/// Wraps the Name `TextEdit` so the Create handler can read its
/// current value.
#[derive(Component)]
struct NewProjectNameInput;

/// Wraps the Template URL `TextEdit`. Pre-filled with the default
/// URL for the active preset; always editable so users can paste
/// any Bevy-CLI-compatible URL.
#[derive(Component)]
struct NewProjectTemplateInput;

/// Wraps the local-path `TextEdit`. Empty by default; when
/// non-empty it takes precedence over the Git URL on Create.
#[derive(Component)]
struct NewProjectLocalTemplateInput;

/// Marks the Browse button next to the local template path field.
#[derive(Component)]
struct NewProjectLocalBrowseButton;

/// Wraps the Git branch `TextEdit`. Used only when scaffolding via
/// the Git URL field; ignored for local-path scaffolds. Default
/// value comes from `template_branch()`.
#[derive(Component)]
struct NewProjectBranchInput;

#[derive(Component)]
struct NewProjectLocationText;

#[derive(Component)]
struct NewProjectStatusText;

/// Marker for the "Open editor after creating" checkbox in the New
/// Project modal. The checkbox lets the user opt out of the
/// auto-build/auto-spawn flow for static templates: when checked
/// (default) the launcher kicks off `cargo build --bin editor
/// --features editor` and hands off to the user's editor binary.
/// When unchecked, the launcher stops at "files written" and
/// surfaces a dialog with the cargo command the user should run
/// from their terminal.
#[derive(Component)]
struct BuildAfterScaffoldCheckbox;

/// Outer container for the progress-bar + log-tail UI, toggled on
/// when a build is in flight so the idle modal doesn't leave a
/// visual gap.
#[derive(Component)]
struct NewProjectProgressContainer;

/// Wraps the "currently compiling `<crate>`" label.
#[derive(Component)]
struct NewProjectProgressCrateLabel;

/// Wraps the `progress_bar` widget so the refresh system can walk
/// its fill child.
#[derive(Component)]
struct NewProjectProgressBarSlot;

/// Wraps the log-tail text; refreshed with the last 20 lines of
/// cargo output each frame.
#[derive(Component)]
struct NewProjectLogText;

#[derive(Component)]
struct NewProjectCancelButton;

#[derive(Component)]
struct NewProjectCreateButton;

#[derive(Component)]
struct NewProjectBrowseButton;

/// One of the two segmented buttons that pick between the Static
/// and Dylib template variants. The enum value is stored on the
/// component so the click observer knows which linkage to apply
/// without needing separate marker types per button.
#[derive(Component, Clone, Copy)]
struct NewProjectLinkageButton(TemplateLinkage);

/// State for the static-editor build pipeline. The launcher
/// background-builds `<project>/target/debug/editor` (via
/// `cargo build --bin editor --features editor`) for static-game
/// projects, then hands off to that binary. Three pieces of state
/// belong together: the queued request, the in-flight task, and
/// the auto-reload flag.
#[derive(Default)]
struct StaticEditorBuild {
    /// Queued request to start a build. The bool is `auto_reload`:
    /// `true` from the post-scaffold path (driver auto-fires the
    /// handoff once the build is `Ready`); `false` from "open
    /// existing project" (user clicks the footer indicator to
    /// reload manually). `drive_static_editor_build` consumes this
    /// on the next `Update` and spawns the cargo task.
    pending: Option<(PathBuf, bool)>,
    /// In-flight static-editor cargo task. Polled each frame by
    /// `drive_static_editor_build`; on completion the driver
    /// updates `BuildStatus` to `Ready` or `Failed`.
    task: Option<Task<Result<PathBuf, crate::ext_build::BuildError>>>,
    /// Auto-reload flag remembered between dispatch and completion.
    /// Mirrors the second member of `pending`, stashed once the task
    /// is spawned because `pending` is drained on dispatch.
    auto_reload: bool,
}

/// Drives the modal's async operations. Internal to this module;
/// external systems that need to observe build progress read the
/// public `BuildStatus` resource instead.
#[derive(Resource, Default)]
struct NewProjectState {
    /// Which preset the user opened the dialog with. `None` when
    /// the modal isn't open.
    preset: Option<TemplatePreset>,
    /// Static vs dylib template choice. Ignored when `preset` is
    /// `Custom` (the user pastes a raw URL).
    linkage: TemplateLinkage,
    /// Parent directory the new project will be placed under.
    /// Scaffolder produces `location/name/`.
    location: PathBuf,
    /// In-flight folder picker (rfd).
    folder_task: Option<Task<Option<FileHandle>>>,
    /// In-flight scaffold (bevy-cli subprocess).
    scaffold_task: Option<Task<Result<PathBuf, ScaffoldError>>>,
    /// In-flight initial build after scaffold. Queued immediately
    /// after the scaffold task succeeds so the user lands in the
    /// editor with the game/extension dylib already installed.
    /// In-flight build task plus the linkage it was kicked off with.
    /// The linkage travels with the task so the poller branches on
    /// the actual in-flight linkage, not on the modal's stale
    /// `state.linkage` (which describes the modal's current selection,
    /// not whatever build is running).
    build_task: Option<(
        Task<Result<PathBuf, crate::ext_build::BuildError>>,
        TemplateLinkage,
    )>,
    /// Artifact waiting to be installed by `apply_pending_install`
    /// (runs in `Last`, not `Update`, so modifications to the
    /// `Update` schedule by the game's `GameApp::add_systems` don't
    /// collide with `Update`'s active `schedule_scope`).
    pending_install: Option<PathBuf>,
    /// Static scaffold whose pre-build finished. Picked up in `Last`
    /// by `apply_pending_static_open`, which calls `enter_project`.
    pending_static_open: Option<PathBuf>,
    /// Static-editor build pipeline state grouped into one field.
    /// See [`StaticEditorBuild`].
    static_editor: StaticEditorBuild,
    /// Whether the post-scaffold path should kick off the editor
    /// build immediately (default `true` — the user lands in their
    /// editor without leaving the launcher) or stop at "files
    /// written, run `cargo editor` from the project root yourself"
    /// (`false` — for users who'd rather drive the build from
    /// their own terminal). Read by the scaffold-success branch
    /// in `poll_new_project_tasks`; set from the New Project
    /// modal's checkbox in `on_create_new_project`.
    build_after_scaffold: bool,
    /// Shared progress sink the build task writes to. The
    /// `refresh_build_progress_ui` system reads a snapshot from
    /// here each frame and copies it into `build_progress_snapshot`
    /// so the modal's bar/log nodes can update without locking on
    /// the hot path.
    build_progress: Option<std::sync::Arc<std::sync::Mutex<crate::ext_build::BuildProgress>>>,
    /// Latest snapshot of `build_progress`, copied each frame.
    /// Used by `refresh_build_progress_ui` to render the dylib
    /// install modal's progress bar.
    build_progress_snapshot: Option<crate::ext_build::BuildProgress>,
    /// Path to the freshly-scaffolded project, kept around so the
    /// build-completion handler can transition into the editor
    /// pointing at the right root.
    pending_project: Option<PathBuf>,
    /// Last user-visible message (used for both progress and errors).
    status: Option<String>,
}

fn default_projects_dir() -> PathBuf {
    dirs::home_dir()
        .map(|h| h.join("Projects"))
        .unwrap_or_else(|| PathBuf::from("."))
}

fn spawn_project_selector(
    mut commands: Commands,
    editor_font: Res<EditorFont>,
    icon_font: Res<jackdaw_feathers::icons::IconFont>,
    pending: Option<Res<PendingAutoOpen>>,
) {
    // UI camera for the project selector screen. Spawned BEFORE the
    // auto-open early-return so the build-progress modal renders
    // against a valid camera even when the user lands directly in
    // an opening project. Without this, the launcher window stays
    // fully blank for several minutes during a static-editor build
    // because the modal had no camera to draw on.
    commands.spawn((ProjectSelectorRoot, Camera2d));

    if let Some(pending) = pending {
        let path = pending.path.clone();
        let skip_build = pending.skip_build;
        commands.remove_resource::<PendingAutoOpen>();
        commands.queue(move |world: &mut World| {
            enter_project_with(world, path, skip_build);
        });
        return;
    }

    let recent = project::read_recent_projects();
    let font = editor_font.0.clone();
    let icon_font_handle = icon_font.0.clone();

    // Detect CWD project candidate
    let cwd = std::env::current_dir().unwrap_or_default();
    let cwd_has_project = cwd.join(".jsn/project.jsn").is_file()
        || cwd.join("project.jsn").is_file()
        || cwd.join("assets").is_dir();

    commands
        .spawn((
            ProjectSelectorRoot,
            Node {
                width: Val::Percent(100.0),
                height: Val::Percent(100.0),
                justify_content: JustifyContent::Center,
                align_items: AlignItems::Center,
                ..Default::default()
            },
            BackgroundColor(tokens::WINDOW_BG),
        ))
        .with_children(|parent| {
            // Card container
            parent
                .spawn(Node {
                    flex_direction: FlexDirection::Column,
                    align_items: AlignItems::Center,
                    padding: UiRect::all(Val::Px(32.0)),
                    row_gap: Val::Px(24.0),
                    min_width: Val::Px(420.0),
                    max_width: Val::Px(520.0),
                    border: UiRect::all(Val::Px(1.0)),
                    border_radius: BorderRadius::all(Val::Px(8.0)),
                    ..Default::default()
                })
                .insert(BackgroundColor(tokens::PANEL_BG))
                .insert(BorderColor::all(tokens::BORDER_SUBTLE))
                .with_children(|card| {
                    // Title
                    card.spawn((
                        Text::new("jackdaw"),
                        TextFont {
                            font: font.clone(),
                            font_size: 28.0,
                            ..Default::default()
                        },
                        TextColor(tokens::TEXT_PRIMARY),
                    ));

                    // Subtitle
                    card.spawn((
                        Text::new("Select a project to open"),
                        TextFont {
                            font: font.clone(),
                            font_size: tokens::FONT_LG,
                            ..Default::default()
                        },
                        TextColor(tokens::TEXT_SECONDARY),
                    ));

                    // CWD option (if it looks like a project)
                    if cwd_has_project {
                        let cwd_name = cwd
                            .file_name()
                            .map(|n| n.to_string_lossy().to_string())
                            .unwrap_or_else(|| cwd.to_string_lossy().to_string());
                        let cwd_clone = cwd.clone();
                        spawn_project_row(
                            card,
                            &cwd_name,
                            &cwd.to_string_lossy(),
                            font.clone(),
                            icon_font_handle.clone(),
                            cwd_clone,
                            true,
                        );
                    }

                    // Recent projects
                    if !recent.projects.is_empty() {
                        card.spawn((
                            Text::new("Recent Projects"),
                            TextFont {
                                font: font.clone(),
                                font_size: tokens::FONT_MD,
                                ..Default::default()
                            },
                            TextColor(tokens::TEXT_SECONDARY),
                            Node {
                                margin: UiRect::top(Val::Px(8.0)),
                                ..Default::default()
                            },
                        ));

                        for entry in &recent.projects {
                            // Skip CWD if already shown above
                            if cwd_has_project && entry.path == cwd {
                                continue;
                            }
                            spawn_project_row(
                                card,
                                &entry.name,
                                &entry.path.to_string_lossy(),
                                font.clone(),
                                icon_font_handle.clone(),
                                entry.path.clone(),
                                false,
                            );
                        }
                    }

                    // New Extension / New Game row
                    let new_row = card
                        .spawn(Node {
                            flex_direction: FlexDirection::Row,
                            column_gap: Val::Px(8.0),
                            margin: UiRect::top(Val::Px(8.0)),
                            ..Default::default()
                        })
                        .id();
                    spawn_new_project_button(
                        card,
                        new_row,
                        "+ New Extension",
                        font.clone(),
                        TemplatePreset::Extension,
                    );
                    spawn_new_project_button(
                        card,
                        new_row,
                        "+ New Game",
                        font.clone(),
                        TemplatePreset::Game,
                    );
                    spawn_new_project_button(
                        card,
                        new_row,
                        "+ From URL…",
                        font.clone(),
                        TemplatePreset::Custom(String::new()),
                    );

                    // Browse button
                    let browse_entity = card
                        .spawn((
                            Node {
                                padding: UiRect::axes(Val::Px(20.0), Val::Px(10.0)),
                                border_radius: BorderRadius::all(Val::Px(tokens::BORDER_RADIUS_MD)),
                                margin: UiRect::top(Val::Px(4.0)),
                                justify_content: JustifyContent::Center,
                                ..Default::default()
                            },
                            BackgroundColor(tokens::SELECTED_BG),
                            children![(
                                Text::new("Open existing project..."),
                                TextFont {
                                    font: font.clone(),
                                    font_size: tokens::FONT_LG,
                                    ..Default::default()
                                },
                                TextColor(tokens::TEXT_PRIMARY),
                            )],
                        ))
                        .id();

                    // Hover effects for browse button
                    card.commands().entity(browse_entity).observe(
                        |hover: On<Pointer<Over>>, mut bg: Query<&mut BackgroundColor>| {
                            if let Ok(mut bg) = bg.get_mut(hover.event_target()) {
                                bg.0 = tokens::SELECTED_BORDER;
                            }
                        },
                    );
                    card.commands().entity(browse_entity).observe(
                        |out: On<Pointer<Out>>, mut bg: Query<&mut BackgroundColor>| {
                            if let Ok(mut bg) = bg.get_mut(out.event_target()) {
                                bg.0 = tokens::SELECTED_BG;
                            }
                        },
                    );
                    card.commands()
                        .entity(browse_entity)
                        .observe(spawn_browse_dialog);
                });
        });
}

fn spawn_project_row(
    parent: &mut ChildSpawnerCommands,
    name: &str,
    path_display: &str,
    font: Handle<Font>,
    icon_font: Handle<Font>,
    project_path: PathBuf,
    is_cwd: bool,
) {
    // Outer row: info column on left, optional X button on right
    let row_entity = parent
        .spawn((
            Node {
                flex_direction: FlexDirection::Row,
                width: Val::Percent(100.0),
                padding: UiRect::all(Val::Px(10.0)),
                border_radius: BorderRadius::all(Val::Px(tokens::BORDER_RADIUS_MD)),
                align_items: AlignItems::Center,
                ..Default::default()
            },
            BackgroundColor(tokens::TOOLBAR_BG),
        ))
        .id();

    // Left side: info column (flex_grow so it fills space)
    let info_column = parent
        .commands()
        .spawn((
            Node {
                flex_direction: FlexDirection::Column,
                flex_grow: 1.0,
                row_gap: Val::Px(2.0),
                ..Default::default()
            },
            children![
                (
                    Node {
                        flex_direction: FlexDirection::Row,
                        column_gap: Val::Px(8.0),
                        align_items: AlignItems::Center,
                        ..Default::default()
                    },
                    children![
                        (
                            Text::new(name.to_string()),
                            TextFont {
                                font: font.clone(),
                                font_size: tokens::FONT_LG,
                                ..Default::default()
                            },
                            TextColor(tokens::TEXT_PRIMARY),
                        ),
                        if_cwd_badge(is_cwd, font.clone()),
                    ],
                ),
                (
                    Text::new(path_display.to_string()),
                    TextFont {
                        font: font.clone(),
                        font_size: tokens::FONT_SM,
                        ..Default::default()
                    },
                    TextColor(tokens::TEXT_SECONDARY),
                ),
            ],
            Pickable::IGNORE,
        ))
        .id();

    parent.commands().entity(row_entity).add_child(info_column);

    // Right side: X button (only for recent projects, not CWD)
    if !is_cwd {
        let remove_path = project_path.clone();
        let x_button = parent
            .commands()
            .spawn(icon_button(
                IconButtonProps::new(Icon::X).variant(ButtonVariant::Ghost),
                &icon_font,
            ))
            .id();

        // X button click: remove from recent + despawn row
        parent.commands().entity(x_button).observe(
            move |mut click: On<Pointer<Click>>, mut commands: Commands| {
                click.propagate(false);
                let path = remove_path.clone();
                project::remove_recent(&path);
                commands.entity(row_entity).try_despawn();
            },
        );

        parent.commands().entity(row_entity).add_child(x_button);
    }

    // Hover effects on the row
    parent.commands().entity(row_entity).observe(
        |hover: On<Pointer<Over>>, mut bg: Query<&mut BackgroundColor>| {
            if let Ok(mut bg) = bg.get_mut(hover.event_target()) {
                bg.0 = tokens::HOVER_BG;
            }
        },
    );
    parent.commands().entity(row_entity).observe(
        |out: On<Pointer<Out>>, mut bg: Query<&mut BackgroundColor>| {
            if let Ok(mut bg) = bg.get_mut(out.event_target()) {
                bg.0 = tokens::TOOLBAR_BG;
            }
        },
    );

    // Click: select project
    parent.commands().entity(row_entity).observe(
        move |_: On<Pointer<Click>>, mut commands: Commands| {
            let path = project_path.clone();
            commands.queue(move |world: &mut World| {
                enter_project(world, path);
            });
        },
    );
}

fn if_cwd_badge(is_cwd: bool, font: Handle<Font>) -> impl Bundle {
    let text = if is_cwd { "current dir" } else { "" };
    (
        Text::new(text.to_string()),
        TextFont {
            font,
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_ACCENT),
    )
}

fn spawn_browse_dialog(
    _: On<Pointer<Click>>,
    mut commands: Commands,
    raw_handle: Query<&RawHandleWrapper, With<PrimaryWindow>>,
) {
    let mut dialog = AsyncFileDialog::new().set_title("Select project folder");

    if let Ok(rh) = raw_handle.single() {
        // SAFETY: called on the main thread during an observer
        let handle = unsafe { rh.get_handle() };
        dialog = dialog.set_parent(&handle);
    }

    let task = AsyncComputeTaskPool::get().spawn(async move { dialog.pick_folder().await });
    commands.insert_resource(FolderDialogTask(task));
}

fn poll_folder_dialog(world: &mut World) {
    let Some(mut task_res) = world.get_resource_mut::<FolderDialogTask>() else {
        return;
    };
    let Some(result) = future::block_on(future::poll_once(&mut task_res.0)) else {
        return;
    };
    world.remove_resource::<FolderDialogTask>();

    if let Some(handle) = result {
        let path = handle.path().to_path_buf();
        enter_project(world, path);
    }
}

/// Entry point for **every** "open a project" action from the
/// launcher (new-scaffold completion, recent-project click, manual
/// folder browse). If the project has a `Cargo.toml`, we kick off a
/// `cargo build` task and the poller decides whether to restart
/// (game) or transition into the editor (extension / non-building
/// project) once it finishes. If there's no `Cargo.toml`, we
/// transition straight to the editor.
///
/// All per-session rebuilds therefore happen at the launcher, never
/// mid-edit. Games' restart-to-activate requirement becomes
/// invisible; the launcher → editor transition already carries a
/// build step, so folding a process restart into it is just a
/// slightly-longer wait.
pub fn enter_project(world: &mut World, root: PathBuf) {
    enter_project_with(world, root, false);
}

/// Same as [`enter_project`] but lets the caller bypass the build
/// step. Used by the post-restart auto-open path: the parent
/// process already produced the dylib, the loader picked it up at
/// startup, so a second build-and-install would either be a no-op
/// or (for games) trigger another restart loop.
pub fn enter_project_with(world: &mut World, root: PathBuf, skip_build: bool) {
    if skip_build || !root.join("Cargo.toml").is_file() {
        transition_to_editor(world, root);
        return;
    }
    // Static-template games: don't open the launcher's editor at
    // all. The launcher's editor doesn't carry the user's
    // `MyGamePlugin` types (different binary, different
    // `AppTypeRegistry`), so the inspector + PIE Play would be
    // missing exactly the user-defined components the user wants
    // to author. Build the user's `editor` binary first, then
    // hand off (`drive_static_editor_build` -> `do_handoff`).
    if matches!(
        crate::new_project::detect_template_kind(&root),
        crate::new_project::TemplateKind::StaticGameWithEditorFeature
    ) {
        let project_name = root
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("project")
            .to_owned();
        let scaffold_modal_already_open = {
            let mut q = world.query_filtered::<Entity, With<NewProjectModalRoot>>();
            q.iter(world).next().is_some()
        };
        if !scaffold_modal_already_open {
            open_project_progress_modal(world, &project_name);
        }

        let progress = std::sync::Arc::new(std::sync::Mutex::new(
            crate::ext_build::BuildProgress::default(),
        ));
        let mut state = world.resource_mut::<NewProjectState>();
        state.pending_project = Some(root.clone());
        state.status = Some(format!("Building editor for `{project_name}`…"));
        state.build_progress = Some(std::sync::Arc::clone(&progress));
        state.build_progress_snapshot = Some(crate::ext_build::BuildProgress::default());
        state.static_editor.pending = Some((root, true));
        return;
    }
    // If the Cargo.toml is a plain binary crate (e.g., the editor's
    // own source tree, or any non-extension cargo project the user
    // points at) there's no cdylib for the loader to pick up. Skip
    // the build rather than compile the whole dep graph just to fail
    // the artifact check at the end.
    if !crate::ext_build::manifest_declares_cdylib(&root) {
        info!(
            "Project at {} has a Cargo.toml but no cdylib target; \
             opening without building.",
            root.display()
        );
        transition_to_editor(world, root);
        return;
    }

    let project_name = root
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or("project")
        .to_owned();

    // Show the "Opening project" modal so the user sees the build
    // + any auto-recovery retry rather than staring at a frozen
    // launcher. The scaffold flow already has its own modal; when
    // called from there we skip spawning a second one.
    let scaffold_modal_already_open = {
        let mut q = world.query_filtered::<Entity, With<NewProjectModalRoot>>();
        q.iter(world).next().is_some()
    };
    if !scaffold_modal_already_open {
        open_project_progress_modal(world, &project_name);
    }

    let progress = std::sync::Arc::new(std::sync::Mutex::new(
        crate::ext_build::BuildProgress::default(),
    ));
    {
        let mut state = world.resource_mut::<NewProjectState>();
        state.pending_project = Some(root.clone());
        state.status = Some(format!("Building `{project_name}`…"));
        state.build_progress = Some(std::sync::Arc::clone(&progress));
        state.build_progress_snapshot = Some(crate::ext_build::BuildProgress::default());
    }

    let root_for_task = root;
    let progress_for_task = std::sync::Arc::clone(&progress);
    // Non-cdylib projects took the early-out in `enter_project_with`.
    let task = AsyncComputeTaskPool::get().spawn(async move {
        crate::ext_build::build_extension_project_with_progress(
            &root_for_task,
            Some(progress_for_task),
            TemplateLinkage::Dylib,
        )
    });
    world.resource_mut::<NewProjectState>().build_task = Some((task, TemplateLinkage::Dylib));
}

/// Apply the project-root state change and flip `AppState` to
/// `Editor`. Called from [`enter_project`] (no build needed) and
/// from the build-complete poller (build finished, transitioning).
///
/// If the project has a file at `<root>/assets/scene.jsn`, that
/// scene is auto-loaded so the user lands in a populated editor
/// rather than an empty one. This is the convention the game
/// template ships with.
fn transition_to_editor(world: &mut World, root: PathBuf) {
    let config = project::load_project_config(&root)
        .unwrap_or_else(|| project::create_default_project(&root));

    project::touch_recent(&root, &config.project.name);

    world.insert_resource(ProjectRoot {
        root: root.clone(),
        config,
    });

    // Despawn the launcher UI.
    let mut to_despawn = Vec::new();
    let mut query = world.query_filtered::<Entity, With<ProjectSelectorRoot>>();
    for entity in query.iter(world) {
        to_despawn.push(entity);
    }
    for entity in to_despawn {
        if let Ok(ec) = world.get_entity_mut(entity) {
            ec.despawn();
        }
    }

    let mut next_state = world.resource_mut::<NextState<AppState>>();
    next_state.set(AppState::Editor);

    // Convention: auto-load `assets/scene.jsn` if present. The game
    // template ships one so scaffolded projects open populated.
    // Per-project last-opened-scene persistence is a follow-up.
    let scene_path = root.join("assets").join("scene.jsn");
    if scene_path.is_file() {
        crate::scene_io::load_scene_from_file(world, &scene_path);
    }
}

/// Spawn a pill-style button inside the "+ New Extension / + New
/// Game" row. Clicking opens the New Project modal with the given
/// preset already selected.
fn spawn_new_project_button(
    card: &mut ChildSpawnerCommands,
    parent: Entity,
    label: &str,
    font: Handle<Font>,
    preset: TemplatePreset,
) {
    let button = card
        .commands()
        .spawn((
            Node {
                padding: UiRect::axes(Val::Px(16.0), Val::Px(8.0)),
                border_radius: BorderRadius::all(Val::Px(tokens::BORDER_RADIUS_MD)),
                justify_content: JustifyContent::Center,
                ..Default::default()
            },
            BackgroundColor(tokens::TOOLBAR_BG),
            children![(
                Text::new(label.to_string()),
                TextFont {
                    font,
                    font_size: tokens::FONT_MD,
                    ..Default::default()
                },
                TextColor(tokens::TEXT_PRIMARY),
            )],
        ))
        .id();

    card.commands().entity(parent).add_child(button);

    card.commands().entity(button).observe(
        |hover: On<Pointer<Over>>, mut bg: Query<&mut BackgroundColor>| {
            if let Ok(mut bg) = bg.get_mut(hover.event_target()) {
                bg.0 = tokens::HOVER_BG;
            }
        },
    );
    card.commands().entity(button).observe(
        |out: On<Pointer<Out>>, mut bg: Query<&mut BackgroundColor>| {
            if let Ok(mut bg) = bg.get_mut(out.event_target()) {
                bg.0 = tokens::TOOLBAR_BG;
            }
        },
    );
    card.commands()
        .entity(button)
        .observe(move |_: On<Pointer<Click>>, mut commands: Commands| {
            let preset = preset.clone();
            commands.queue(move |world: &mut World| {
                open_new_project_modal(world, preset);
            });
        });
}

/// Spawn one segment of the Static/Dylib selector. `initial` picks
/// the starting highlighted button; subsequent clicks repaint via
/// `on_linkage_button_click`.
///
/// The project picker runs before any extension has registered
/// operators, so there's no rich hover tooltip available here. The
/// button label is the single visible affordance; explanatory text
/// is rendered as a subtitle below the row by the calling dialog.
fn spawn_linkage_button(
    world: &mut World,
    parent: Entity,
    label: &str,
    linkage: TemplateLinkage,
    initial: TemplateLinkage,
    font: Handle<Font>,
) {
    let selected = linkage == initial;
    let bg_color = if selected {
        tokens::SELECTED_BG
    } else {
        tokens::TOOLBAR_BG
    };
    let border_color = if selected {
        tokens::SELECTED_BORDER
    } else {
        tokens::BORDER_SUBTLE
    };

    let button = world
        .spawn((
            NewProjectLinkageButton(linkage),
            Node {
                padding: UiRect::axes(Val::Px(16.0), Val::Px(8.0)),
                border: UiRect::all(Val::Px(1.0)),
                border_radius: BorderRadius::all(Val::Px(tokens::BORDER_RADIUS_MD)),
                justify_content: JustifyContent::Center,
                ..Default::default()
            },
            BackgroundColor(bg_color),
            BorderColor::all(border_color),
            children![(
                Text::new(label.to_string()),
                TextFont {
                    font,
                    font_size: tokens::FONT_MD,
                    ..Default::default()
                },
                TextColor(tokens::TEXT_PRIMARY),
            )],
            ChildOf(parent),
        ))
        .id();

    // Hover and out handlers skip the currently-selected button so
    // its highlight isn't clobbered by hover/idle paints.
    world
        .entity_mut(button)
        .observe(on_linkage_button_click)
        .observe(
            |hover: On<Pointer<Over>>,
             buttons: Query<&NewProjectLinkageButton>,
             state: Res<NewProjectState>,
             mut bg: Query<&mut BackgroundColor>| {
                let Ok(button) = buttons.get(hover.event_target()) else {
                    return;
                };
                if button.0 == state.linkage {
                    return;
                }
                if let Ok(mut bg) = bg.get_mut(hover.event_target()) {
                    bg.0 = tokens::HOVER_BG;
                }
            },
        )
        .observe(
            |out: On<Pointer<Out>>,
             buttons: Query<&NewProjectLinkageButton>,
             state: Res<NewProjectState>,
             mut bg: Query<&mut BackgroundColor>| {
                let Ok(button) = buttons.get(out.event_target()) else {
                    return;
                };
                if button.0 == state.linkage {
                    return;
                }
                if let Ok(mut bg) = bg.get_mut(out.event_target()) {
                    bg.0 = tokens::TOOLBAR_BG;
                }
            },
        );
}

/// Click handler for Static/Dylib segmented buttons. Updates
/// `NewProjectState.linkage`, repaints the two buttons, and
/// rewrites the Template URL input to the new preset URL so the
/// user sees the change immediately. If the user has manually
/// edited the URL to a custom value, this overwrites it; by
/// design: toggling the linkage is a "reset to preset" action.
fn on_linkage_button_click(
    click: On<Pointer<Click>>,
    buttons: Query<&NewProjectLinkageButton>,
    mut commands: Commands,
) {
    let Ok(button) = buttons.get(click.event_target()) else {
        return;
    };
    let linkage = button.0;
    commands.queue(move |world: &mut World| {
        world.resource_mut::<NewProjectState>().linkage = linkage;

        // Repaint every linkage button against the new selection.
        let mut repaint: Vec<(Entity, bool)> = Vec::new();
        {
            let mut q = world.query::<(Entity, &NewProjectLinkageButton)>();
            for (entity, btn) in q.iter(world) {
                repaint.push((entity, btn.0 == linkage));
            }
        }
        for (entity, is_selected) in repaint {
            let bg_color = if is_selected {
                tokens::SELECTED_BG
            } else {
                tokens::TOOLBAR_BG
            };
            let border_color = if is_selected {
                tokens::SELECTED_BORDER
            } else {
                tokens::BORDER_SUBTLE
            };
            if let Ok(mut ec) = world.get_entity_mut(entity) {
                ec.insert(BackgroundColor(bg_color));
                ec.insert(BorderColor::all(border_color));
            }
        }

        let Some(preset) = world.resource::<NewProjectState>().preset.clone() else {
            return;
        };
        // Re-derive both fields so dev users see the right
        // pre-fills after toggling Static/Dylib: Local field
        // tracks `<checkout>/templates/<subdir>` and the Git URL
        // field stays a real remote.
        let new_local = preset
            .local_template_path(linkage)
            .map(|p| p.display().to_string())
            .unwrap_or_default();
        set_local_template_input_text(world, new_local);
        let new_url = preset.git_url_with_subdir(linkage);
        set_template_input_text(world, new_url);
    });
}

/// Push a new string into the Template URL text input.
fn set_template_input_text(world: &mut World, new_text: String) {
    use jackdaw_feathers::text_edit::{TextInputQueue, set_text_input_value};

    let mut q = world.query_filtered::<Entity, With<NewProjectTemplateInput>>();
    let Some(outer) = q.iter(world).next() else {
        return;
    };
    let Some((_wrapper, inner)) = find_text_edit_entities_for_template(world, outer) else {
        return;
    };
    if let Some(mut queue) = world.get_mut::<TextInputQueue>(inner) {
        set_text_input_value(&mut queue, new_text);
    }
}

/// Walk from the outer Template-field entity to its inner
/// `TextInputQueue`-bearing entity. Mirror of
/// `inspector::find_text_edit_entities_local`.
fn find_text_edit_entities_for_template(world: &World, outer: Entity) -> Option<(Entity, Entity)> {
    use jackdaw_feathers::text_edit::TextEditWrapper;
    let children = world.get::<Children>(outer)?;
    for child in children.iter() {
        if let Some(wrapper) = world.get::<TextEditWrapper>(child) {
            return Some((child, wrapper.0));
        }
        if let Some(grandchildren) = world.get::<Children>(child) {
            for gc in grandchildren.iter() {
                if let Some(wrapper) = world.get::<TextEditWrapper>(gc) {
                    return Some((gc, wrapper.0));
                }
            }
        }
    }
    None
}

/// Tear down any existing New Project modal. Idempotent.
pub fn close_new_project_modal(world: &mut World) {
    let mut q = world.query_filtered::<Entity, With<NewProjectModalRoot>>();
    let entities: Vec<Entity> = q.iter(world).collect();
    for entity in entities {
        if let Ok(ec) = world.get_entity_mut(entity) {
            ec.despawn();
        }
    }
    let mut state = world.resource_mut::<NewProjectState>();
    state.preset = None;
    state.linkage = TemplateLinkage::default();
    state.folder_task = None;
    state.scaffold_task = None;
    state.status = None;
    // `pending_static_open` is already drained by
    // `apply_pending_static_open` on the happy path, and isn't set
    // on cancel.
}

/// Lightweight modal shown while `enter_project_with` builds an
/// **existing** project; the user picked a recent entry or
/// browsed to a folder and we need something visual while cargo
/// runs + the auto-recovery retry may fire. Reuses the same
/// `NewProjectProgressContainer` / `NewProjectProgressCrateLabel`
/// / progress-bar / log-tail markers as the scaffold modal, so
/// the existing `refresh_build_progress_ui` system drives it
/// without extra wiring. Despawned via `close_new_project_modal`.
pub fn open_project_progress_modal(world: &mut World, project_name: &str) {
    close_new_project_modal(world);

    let editor_font = world.resource::<EditorFont>().0.clone();

    let scrim = world
        .spawn((
            NewProjectModalRoot,
            Node {
                position_type: PositionType::Absolute,
                left: Val::Px(0.0),
                top: Val::Px(0.0),
                width: Val::Percent(100.0),
                height: Val::Percent(100.0),
                justify_content: JustifyContent::Center,
                align_items: AlignItems::Center,
                ..Default::default()
            },
            BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.55)),
            GlobalZIndex(100),
        ))
        .id();

    let card = world
        .spawn((
            Node {
                flex_direction: FlexDirection::Column,
                row_gap: Val::Px(12.0),
                padding: UiRect::all(Val::Px(24.0)),
                min_width: Val::Px(480.0),
                max_width: Val::Px(720.0),
                border: UiRect::all(Val::Px(1.0)),
                border_radius: BorderRadius::all(Val::Px(tokens::BORDER_RADIUS_MD)),
                ..Default::default()
            },
            BackgroundColor(tokens::PANEL_BG),
            BorderColor::all(tokens::BORDER_SUBTLE),
            ChildOf(scrim),
        ))
        .id();

    world.spawn((
        Text::new(format!("Opening `{project_name}`")),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_LG,
            ..Default::default()
        },
        TextColor(tokens::TEXT_PRIMARY),
        ChildOf(card),
    ));

    // Up-front hint about how long the build can take. Without this
    // users see a blank-looking launcher for several minutes on a
    // first run and assume jackdaw has hung.
    world.spawn((
        Text::new(
            "Building the per-project editor binary. First run with a fresh \
             cargo cache can take 5 to 10 minutes (bevy is ~500 crates). \
             Subsequent opens are incremental and finish in seconds.",
        ),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        ChildOf(card),
    ));

    world.spawn((
        NewProjectStatusText,
        Text::new(String::new()),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        ChildOf(card),
    ));

    // Progress container + children mirror the scaffold modal so
    // `refresh_build_progress_ui` walks the same marker chain.
    // `display: Flex` (not None) so the user sees the placeholder
    // text + empty progress bar right away, instead of a blank
    // card while cargo's first artifact event is pending.
    let progress_container = world
        .spawn((
            NewProjectProgressContainer,
            Node {
                flex_direction: FlexDirection::Column,
                row_gap: Val::Px(6.0),
                margin: UiRect::top(Val::Px(8.0)),
                display: Display::Flex,
                ..Default::default()
            },
            ChildOf(card),
        ))
        .id();

    world.spawn((
        NewProjectProgressCrateLabel,
        Text::new("Preparing build...".to_string()),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        ChildOf(progress_container),
    ));

    let bar_slot = world
        .spawn((
            NewProjectProgressBarSlot,
            Node {
                width: Val::Percent(100.0),
                ..Default::default()
            },
            ChildOf(progress_container),
        ))
        .id();
    world.spawn((
        jackdaw_feathers::progress::progress_bar(0.0),
        ChildOf(bar_slot),
    ));

    world.spawn((
        NewProjectLogText,
        Text::new(String::new()),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        Node {
            max_height: Val::Px(220.0),
            overflow: Overflow::clip(),
            ..Default::default()
        },
        ChildOf(progress_container),
    ));
}

/// Show the New Project modal with the given preset pre-selected.
///
/// Callable from any `AppState`; the launcher (`ProjectSelect`)
/// and the editor's **File → New Project** menu both invoke this.
/// The modal is a full-window overlay so it renders regardless of
/// which camera is active.
pub fn open_new_project_modal(world: &mut World, preset: TemplatePreset) {
    close_new_project_modal(world);

    let location = default_projects_dir();
    let initial_linkage = TemplateLinkage::default();
    {
        let mut state = world.resource_mut::<NewProjectState>();
        state.preset = Some(preset.clone());
        state.linkage = initial_linkage;
        state.location = location.clone();
        state.status = None;
        state.build_after_scaffold = true;
    }

    let editor_font = world.resource::<EditorFont>().0.clone();
    let icon_font = world
        .resource::<jackdaw_feathers::icons::IconFont>()
        .0
        .clone();
    let (heading, name_placeholder) = match preset {
        TemplatePreset::Extension => ("New Extension", "my_extension"),
        TemplatePreset::Game => ("New Game", "my_game"),
        TemplatePreset::Custom(_) => ("New Project", "my_project"),
    };

    // Full-window scrim that catches clicks behind the modal.
    let scrim = world
        .spawn((
            NewProjectModalRoot,
            Node {
                position_type: PositionType::Absolute,
                left: Val::Px(0.0),
                top: Val::Px(0.0),
                width: Val::Percent(100.0),
                height: Val::Percent(100.0),
                justify_content: JustifyContent::Center,
                align_items: AlignItems::Center,
                ..Default::default()
            },
            BackgroundColor(Color::srgba(0.0, 0.0, 0.0, 0.55)),
            GlobalZIndex(100),
        ))
        .id();

    // Modal card.
    let card = world
        .spawn((
            Node {
                flex_direction: FlexDirection::Column,
                row_gap: Val::Px(12.0),
                padding: UiRect::all(Val::Px(24.0)),
                min_width: Val::Px(420.0),
                max_width: Val::Px(520.0),
                border: UiRect::all(Val::Px(1.0)),
                border_radius: BorderRadius::all(Val::Px(8.0)),
                ..Default::default()
            },
            BackgroundColor(tokens::PANEL_BG),
            BorderColor::all(tokens::BORDER_SUBTLE),
            ChildOf(scrim),
        ))
        .id();

    // Heading
    world.spawn((
        Text::new(heading.to_string()),
        TextFont {
            font: editor_font.clone(),
            font_size: 24.0,
            ..Default::default()
        },
        TextColor(tokens::TEXT_PRIMARY),
        ChildOf(card),
    ));

    // Name field
    world.spawn((
        Text::new("Name"),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        ChildOf(card),
    ));
    world.spawn((
        NewProjectNameInput,
        ChildOf(card),
        text_edit(
            TextEditProps::default()
                .with_placeholder(name_placeholder.to_string())
                .with_default_value(name_placeholder.to_string())
                .auto_focus(),
        ),
    ));

    // Location field
    world.spawn((
        Text::new("Location"),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        ChildOf(card),
    ));
    let location_row = world
        .spawn((
            Node {
                flex_direction: FlexDirection::Row,
                align_items: AlignItems::Center,
                column_gap: Val::Px(8.0),
                ..Default::default()
            },
            ChildOf(card),
        ))
        .id();
    world.spawn((
        NewProjectLocationText,
        Text::new(location.to_string_lossy().into_owned()),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_MD,
            ..Default::default()
        },
        TextColor(tokens::TEXT_PRIMARY),
        Node {
            flex_grow: 1.0,
            ..Default::default()
        },
        ChildOf(location_row),
    ));
    let browse = world
        .spawn((
            NewProjectBrowseButton,
            Node {
                padding: UiRect::axes(Val::Px(12.0), Val::Px(6.0)),
                border_radius: BorderRadius::all(Val::Px(tokens::BORDER_RADIUS_MD)),
                ..Default::default()
            },
            BackgroundColor(tokens::TOOLBAR_BG),
            children![(
                Text::new("Browse…"),
                TextFont {
                    font: editor_font.clone(),
                    font_size: tokens::FONT_SM,
                    ..Default::default()
                },
                TextColor(tokens::TEXT_PRIMARY),
            )],
            ChildOf(location_row),
        ))
        .id();
    world.entity_mut(browse).observe(on_browse_new_location);

    // Linkage selector only appears for the Extension and Game
    // presets; Custom pastes its own URL.
    if preset.supports_linkage_selector() {
        world.spawn((
            Text::new("Template type"),
            TextFont {
                font: editor_font.clone(),
                font_size: tokens::FONT_SM,
                ..Default::default()
            },
            TextColor(tokens::TEXT_SECONDARY),
            ChildOf(card),
        ));
        let linkage_row = world
            .spawn((
                Node {
                    flex_direction: FlexDirection::Row,
                    column_gap: Val::Px(8.0),
                    ..Default::default()
                },
                ChildOf(card),
            ))
            .id();
        spawn_linkage_button(
            world,
            linkage_row,
            "Static",
            TemplateLinkage::Static,
            initial_linkage,
            editor_font.clone(),
        );
        spawn_linkage_button(
            world,
            linkage_row,
            "Dylib (experimental)",
            TemplateLinkage::Dylib,
            initial_linkage,
            editor_font.clone(),
        );
        // Inline subtitle (visible always, no hover needed) so the
        // user knows what the two linkage options do without relying
        // on an operator-registered tooltip; operators aren't loaded
        // yet at the project-select stage.
        world.spawn((
            Text::new(
                "Static: plainly-compiled rlib/bin (recommended, ships with the bundled \
                 templates/game-static and templates/extension-static). \
                 Dylib: hot-reloadable cdylib, experimental and requires the editor's \
                 `dylib` feature.",
            ),
            TextFont {
                font: editor_font.clone(),
                font_size: tokens::FONT_SM,
                ..default()
            },
            TextColor(tokens::TEXT_SECONDARY),
            ChildOf(card),
        ));
    }

    // "Open editor after creating" checkbox: lets the user opt
    // out of the auto-build/auto-spawn flow. Default on. When off,
    // the post-scaffold path stops at "files written" and shows a
    // dialog with the cargo command to run from the project root.
    //
    // Hidden for static-extension scaffolds: there's no `editor`
    // binary to build (extensions launch via `cargo run` against
    // their own bin), so the checkbox would be a no-op. See
    // `on_create_new_project` for the consumer side; if the
    // checkbox isn't spawned, it falls back to `true` and the
    // post-scaffold instructions modal renders for the extension.
    let show_build_checkbox = !matches!(
        (initial_linkage, &preset),
        (TemplateLinkage::Static, TemplatePreset::Extension)
    );
    if show_build_checkbox {
        world.spawn((
            BuildAfterScaffoldCheckbox,
            ChildOf(card),
            jackdaw_feathers::checkbox::checkbox(
                jackdaw_feathers::checkbox::CheckboxProps::new("Open editor after creating")
                    .checked(true),
                &editor_font,
                &icon_font,
            ),
        ));
    }

    // Template section: shared parent label, then two sub-labelled
    // rows (Local path and Git URL). The Local path field is empty
    // by default; when non-empty it takes precedence over the Git
    // URL on Create.
    world.spawn((
        Text::new("Template"),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        ChildOf(card),
    ));

    // Sub-label: Local path
    world.spawn((
        Text::new("Local path"),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        Node {
            margin: UiRect::top(Val::Px(4.0)),
            ..Default::default()
        },
        ChildOf(card),
    ));
    // Row: local-path text input + Browse button
    let local_row = world
        .spawn((
            Node {
                flex_direction: FlexDirection::Row,
                align_items: AlignItems::Center,
                column_gap: Val::Px(8.0),
                ..Default::default()
            },
            ChildOf(card),
        ))
        .id();
    // Mirror the NewProjectTemplateInput pattern: marker + ChildOf
    // + text_edit in a single bundle. The text_edit widget supplies
    // its own Node, so we cannot stack another Node onto this entity
    // (Bevy rejects duplicate components). Layout falls back to the
    // text_edit's intrinsic width; flex_grow on the input is a
    // future polish if this looks too narrow next to the Browse
    // button.
    let initial_local_path = preset
        .local_template_path(initial_linkage)
        .map(|p| p.display().to_string())
        .unwrap_or_default();
    world.spawn((
        NewProjectLocalTemplateInput,
        ChildOf(local_row),
        text_edit(
            TextEditProps::default()
                .with_placeholder(
                    "Local folder (e.g. ~/Workspace/jackdaw/templates/game)".to_string(),
                )
                .with_default_value(initial_local_path)
                .allow_empty(),
        ),
    ));
    let local_browse = world
        .spawn((
            NewProjectLocalBrowseButton,
            Node {
                padding: UiRect::axes(Val::Px(12.0), Val::Px(6.0)),
                border_radius: BorderRadius::all(Val::Px(tokens::BORDER_RADIUS_MD)),
                ..Default::default()
            },
            BackgroundColor(tokens::TOOLBAR_BG),
            children![(
                Text::new("Browse..."),
                TextFont {
                    font: editor_font.clone(),
                    font_size: tokens::FONT_SM,
                    ..Default::default()
                },
                TextColor(tokens::TEXT_PRIMARY),
            )],
            ChildOf(local_row),
        ))
        .id();
    world
        .entity_mut(local_browse)
        .observe(on_browse_template_folder);

    // Sub-label: Git URL
    world.spawn((
        Text::new("Git URL"),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        Node {
            margin: UiRect::top(Val::Px(4.0)),
            ..Default::default()
        },
        ChildOf(card),
    ));
    // Git URL text input. Prefilled from preset+linkage, editable
    // so power users can point at a fork or custom template.
    world.spawn((
        NewProjectTemplateInput,
        ChildOf(card),
        text_edit(
            TextEditProps::default()
                .with_placeholder("https://github.com/.../your_template".to_string())
                .with_default_value(preset.git_url_with_subdir(initial_linkage))
                .allow_empty(),
        ),
    ));

    // Sub-label: Branch
    world.spawn((
        Text::new("Branch"),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        Node {
            margin: UiRect::top(Val::Px(4.0)),
            ..Default::default()
        },
        ChildOf(card),
    ));
    // Git branch text input. Pre-filled with the configured default
    // (`template_branch()` honors `JACKDAW_TEMPLATE_BRANCH` and
    // otherwise returns the compile-time default). Ignored for the
    // local-path scaffold; the local checkout's working tree IS the
    // branch.
    world.spawn((
        NewProjectBranchInput,
        ChildOf(card),
        text_edit(
            TextEditProps::default()
                .with_placeholder("main".to_string())
                .with_default_value(crate::new_project::template_branch())
                .allow_empty(),
        ),
    ));

    // Precedence hint. With both fields populated, the local path
    // wins; this line tells the user so they aren't surprised.
    world.spawn((
        Text::new("If both are filled, the local path is used."),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        Node {
            margin: UiRect::top(Val::Px(4.0)),
            ..Default::default()
        },
        ChildOf(card),
    ));

    // Status line
    world.spawn((
        NewProjectStatusText,
        Text::new(String::new()),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        ChildOf(card),
    ));

    // Build-progress UI (hidden until a build is in flight).
    let progress_container = world
        .spawn((
            NewProjectProgressContainer,
            Node {
                flex_direction: FlexDirection::Column,
                row_gap: Val::Px(6.0),
                margin: UiRect::top(Val::Px(8.0)),
                display: Display::None,
                ..Default::default()
            },
            ChildOf(card),
        ))
        .id();

    // "Compiling <crate>" label.
    world.spawn((
        NewProjectProgressCrateLabel,
        Text::new(String::new()),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        ChildOf(progress_container),
    ));

    // Progress bar slot wrapping the `progress_bar` widget.
    let bar_slot = world
        .spawn((
            NewProjectProgressBarSlot,
            Node {
                width: Val::Percent(100.0),
                ..Default::default()
            },
            ChildOf(progress_container),
        ))
        .id();
    world.spawn((
        jackdaw_feathers::progress::progress_bar(0.0),
        ChildOf(bar_slot),
    ));

    // Log tail; fixed-height scrollable-ish (we don't enable real
    // scrolling; text wraps naturally and oldest lines age out via
    // the 20-line ring buffer).
    world.spawn((
        NewProjectLogText,
        Text::new(String::new()),
        TextFont {
            font: editor_font.clone(),
            font_size: tokens::FONT_SM,
            ..Default::default()
        },
        TextColor(tokens::TEXT_SECONDARY),
        Node {
            max_height: Val::Px(220.0),
            overflow: Overflow::clip(),
            ..Default::default()
        },
        ChildOf(progress_container),
    ));

    // Action buttons
    let actions = world
        .spawn((
            Node {
                flex_direction: FlexDirection::Row,
                justify_content: JustifyContent::FlexEnd,
                column_gap: Val::Px(8.0),
                margin: UiRect::top(Val::Px(8.0)),
                ..Default::default()
            },
            ChildOf(card),
        ))
        .id();

    let cancel = world
        .spawn((
            NewProjectCancelButton,
            Node {
                padding: UiRect::axes(Val::Px(16.0), Val::Px(8.0)),
                border_radius: BorderRadius::all(Val::Px(tokens::BORDER_RADIUS_MD)),
                ..Default::default()
            },
            BackgroundColor(tokens::TOOLBAR_BG),
            children![(
                Text::new("Cancel"),
                TextFont {
                    font: editor_font.clone(),
                    font_size: tokens::FONT_MD,
                    ..Default::default()
                },
                TextColor(tokens::TEXT_PRIMARY),
            )],
            ChildOf(actions),
        ))
        .id();
    world.entity_mut(cancel).observe(on_cancel_new_project);

    let create = world
        .spawn((
            NewProjectCreateButton,
            Node {
                padding: UiRect::axes(Val::Px(20.0), Val::Px(8.0)),
                border_radius: BorderRadius::all(Val::Px(tokens::BORDER_RADIUS_MD)),
                ..Default::default()
            },
            BackgroundColor(tokens::SELECTED_BG),
            children![(
                Text::new("Create"),
                TextFont {
                    font: editor_font,
                    font_size: tokens::FONT_MD,
                    ..Default::default()
                },
                TextColor(tokens::TEXT_PRIMARY),
            )],
            ChildOf(actions),
        ))
        .id();
    world.entity_mut(create).observe(on_create_new_project);
}

fn on_cancel_new_project(_: On<Pointer<Click>>, mut commands: Commands) {
    commands.queue(close_new_project_modal);
}

fn on_browse_new_location(
    _: On<Pointer<Click>>,
    mut commands: Commands,
    raw_handle: Query<&RawHandleWrapper, With<PrimaryWindow>>,
) {
    let mut dialog = AsyncFileDialog::new().set_title("Choose parent directory");
    if let Ok(rh) = raw_handle.single() {
        // SAFETY: called on the main thread during an observer.
        let handle = unsafe { rh.get_handle() };
        dialog = dialog.set_parent(&handle);
    }
    let task = AsyncComputeTaskPool::get().spawn(async move { dialog.pick_folder().await });
    commands.queue(move |world: &mut World| {
        world.resource_mut::<NewProjectState>().folder_task = Some(task);
    });
}

/// Observer for the Browse button on the local template path field.
/// Opens a folder picker; on completion, writes the picked path into
/// the `NewProjectLocalTemplateInput` text field via `TextInputQueue`.
fn on_browse_template_folder(
    _: On<Pointer<Click>>,
    mut commands: Commands,
    raw_handle: Query<&RawHandleWrapper, With<PrimaryWindow>>,
) {
    let mut dialog = AsyncFileDialog::new().set_title("Select template folder");
    if let Ok(rh) = raw_handle.single() {
        // SAFETY: called on the main thread during an observer.
        let handle = unsafe { rh.get_handle() };
        dialog = dialog.set_parent(&handle);
    }
    // Default starting directory: in-tree templates dir for dev
    // checkouts, otherwise the user's home directory.
    let start_dir = crate::new_project::jackdaw_dev_checkout()
        .map(|p| p.join("templates"))
        .or_else(dirs::home_dir);
    if let Some(dir) = start_dir {
        dialog = dialog.set_directory(dir);
    }
    let task = AsyncComputeTaskPool::get().spawn(async move { dialog.pick_folder().await });
    commands.queue(move |world: &mut World| {
        world.insert_resource(TemplateFolderDialogTask(task));
    });
}

/// Push a new string into the local template path text input.
fn set_local_template_input_text(world: &mut World, new_text: String) {
    use jackdaw_feathers::text_edit::{TextInputQueue, set_text_input_value};

    let mut q = world.query_filtered::<Entity, With<NewProjectLocalTemplateInput>>();
    let Some(outer) = q.iter(world).next() else {
        return;
    };
    let Some((_wrapper, inner)) = find_text_edit_entities_for_template(world, outer) else {
        return;
    };
    if let Some(mut queue) = world.get_mut::<TextInputQueue>(inner) {
        set_text_input_value(&mut queue, new_text);
    }
}

/// Polling system for `TemplateFolderDialogTask`. Drains the task
/// when the picker resolves and writes the picked path into the
/// local template text input.
fn poll_template_folder_dialog(world: &mut World) {
    let Some(mut task_res) = world.get_resource_mut::<TemplateFolderDialogTask>() else {
        return;
    };
    let Some(result) = future::block_on(future::poll_once(&mut task_res.0)) else {
        return;
    };
    world.remove_resource::<TemplateFolderDialogTask>();
    if let Some(handle) = result {
        let path = handle.path().to_path_buf();
        set_local_template_input_text(world, path.to_string_lossy().into_owned());
    }
}

fn on_create_new_project(
    _: On<Pointer<Click>>,
    mut commands: Commands,
    name_inputs: Query<Entity, With<NewProjectNameInput>>,
    template_inputs: Query<Entity, With<NewProjectTemplateInput>>,
    local_template_inputs: Query<Entity, With<NewProjectLocalTemplateInput>>,
    branch_inputs: Query<Entity, With<NewProjectBranchInput>>,
    text_edit_values: Query<&TextEditValue>,
    build_checkbox: Query<
        &jackdaw_feathers::checkbox::CheckboxState,
        With<BuildAfterScaffoldCheckbox>,
    >,
) {
    // Read the name, local template path, git URL, and branch from
    // the text inputs' synced TextEditValue.
    let Some(name_entity) = name_inputs.iter().next() else {
        return;
    };
    let name = text_edit_values
        .get(name_entity)
        .map(|v| v.0.trim().to_string())
        .unwrap_or_default();
    let local_path_from_input = local_template_inputs
        .iter()
        .next()
        .and_then(|e| text_edit_values.get(e).ok())
        .map(|v| v.0.trim().to_string())
        .unwrap_or_default();
    let git_url_from_input = template_inputs
        .iter()
        .next()
        .and_then(|e| text_edit_values.get(e).ok())
        .map(|v| v.0.trim().to_string())
        .unwrap_or_default();
    let branch_from_input = branch_inputs
        .iter()
        .next()
        .and_then(|e| text_edit_values.get(e).ok())
        .map(|v| v.0.trim().to_string())
        .unwrap_or_default();
    // Snapshot the "Open editor after creating" checkbox into the
    // resource so the post-scaffold poller can branch on it. Falls
    // back to `true` if the checkbox isn't found (defensive, for
    // presets that don't render the checkbox UI). For static-
    // extension we forcibly reset to `false` below: extensions
    // don't ship an editor binary to build, so the auto-open flow
    // would fail and the instructions modal is the right path.
    let checkbox_value = build_checkbox
        .iter()
        .next()
        .map(|state| state.checked)
        .unwrap_or(true);

    commands.queue(move |world: &mut World| {
        let (location, linkage) = {
            let mut state = world.resource_mut::<NewProjectState>();
            if state.preset.is_none() {
                return;
            }
            if state.scaffold_task.is_some() {
                return; // already running
            }
            // Force `build_after_scaffold = false` for static-
            // extension scaffolds so the post-scaffold path goes to
            // the instructions modal rather than the (nonexistent)
            // editor-binary build.
            let build_after_scaffold = if matches!(
                (state.linkage, state.preset.as_ref()),
                (TemplateLinkage::Static, Some(TemplatePreset::Extension))
            ) {
                false
            } else {
                checkbox_value
            };
            state.build_after_scaffold = build_after_scaffold;
            (state.location.clone(), state.linkage)
        };

        let name = name.clone();
        if name.is_empty() {
            world.resource_mut::<NewProjectState>().status =
                Some("Please enter a project name.".into());
            return;
        }

        // Precedence: local path wins when non-empty; fall through to
        // git URL otherwise. Both empty is an error.
        let local_path = local_path_from_input.clone();
        let git_url = git_url_from_input.clone();
        let template_url = if !local_path.is_empty() {
            // Validate that the local path exists as a directory before
            // launching the scaffold subprocess.
            if !std::path::Path::new(&local_path).is_dir() {
                world.resource_mut::<NewProjectState>().status =
                    Some(format!("Local template path does not exist: {local_path}"));
                return;
            }
            local_path
        } else if !git_url.is_empty() {
            git_url
        } else {
            world.resource_mut::<NewProjectState>().status =
                Some("Provide a template (local path or git URL).".into());
            return;
        };

        let name_for_task = name.clone();
        let location_for_task = location.clone();
        let url_for_task = template_url.clone();
        // Branch field only matters for git URL scaffolds; the
        // local-path branch ignores it (the working tree IS the
        // branch). An empty branch falls through to
        // `template_branch()` inside `scaffold_project`.
        let branch_for_task = if branch_from_input.is_empty() {
            None
        } else {
            Some(branch_from_input.clone())
        };

        world.resource_mut::<NewProjectState>().status = Some(format!("Scaffolding `{name}`..."));

        let task = AsyncComputeTaskPool::get().spawn(async move {
            scaffold_project(
                &name_for_task,
                &location_for_task,
                &url_for_task,
                branch_for_task.as_deref(),
                linkage,
            )
        });
        world.resource_mut::<NewProjectState>().scaffold_task = Some(task);
    });
}

fn poll_new_project_tasks(
    mut commands: Commands,
    mut state: ResMut<NewProjectState>,
    mut location_texts: Query<&mut Text, With<NewProjectLocationText>>,
    mut status_texts: Query<
        &mut Text,
        (With<NewProjectStatusText>, Without<NewProjectLocationText>),
    >,
) {
    // Folder picker.
    if let Some(task) = state.folder_task.as_mut()
        && let Some(result) = future::block_on(future::poll_once(task))
    {
        state.folder_task = None;
        if let Some(handle) = result {
            state.location = handle.path().to_path_buf();
        }
    }

    // Scaffold.
    if let Some(task) = state.scaffold_task.as_mut()
        && let Some(result) = future::block_on(future::poll_once(task))
    {
        state.scaffold_task = None;
        match result {
            Ok(project_path) => {
                info!("Scaffolded project at {}", project_path.display());
                let project_name = project_path
                    .file_name()
                    .and_then(|s| s.to_str())
                    .unwrap_or("project")
                    .to_owned();

                // "Open editor after creating" was unchecked in the
                // modal: stop here. Close the modal and open the
                // info dialog inline via commands.queue, which runs
                // in apply_deferred (full World access, no
                // schedule_scope conflict).
                if !state.build_after_scaffold {
                    info!(
                        "Scaffolded `{project_name}` at {}; skipping build per modal toggle.",
                        project_path.display()
                    );
                    let dialog_root = project_path.clone();
                    let dialog_linkage = state.linkage;
                    let dialog_preset = state.preset.clone();
                    commands.queue(move |world: &mut World| {
                        close_new_project_modal(world);
                        open_skip_build_dialog(
                            world,
                            &dialog_root,
                            dialog_linkage,
                            dialog_preset.as_ref(),
                        );
                    });
                    state.pending_project = None;
                    state.scaffold_task = None;
                    state.status = None;
                    return;
                }

                state.status = Some(format!("Building `{project_name}` in background…"));
                state.pending_project = Some(project_path.clone());

                // Static linkage: open the editor immediately, and
                // background-build the user's `editor` binary so
                // they can later hand off into the full inspector +
                // PIE without leaving the editor session. The
                // launcher's own editor handles authoring with
                // built-in components in the meantime.
                //
                // Dylib linkage: still needs the build to finish
                // before transitioning, because the launcher
                // dlopens the cdylib at install time. The
                // build-completion branch below sets
                // `pending_install` for that path.
                let linkage = state.linkage;
                match linkage {
                    TemplateLinkage::Static => {
                        // Build the user's editor binary first;
                        // the launcher's modal stays up showing
                        // progress through the existing modal
                        // infrastructure (`refresh_build_progress_ui`
                        // reads `state.build_progress_snapshot`).
                        // When the build is `Ready`, the driver in
                        // `drive_static_editor_build` closes the
                        // modal, spawns the user's binary, and
                        // exits the launcher. We don't transition
                        // into the launcher's own editor for
                        // static templates — the launcher's editor
                        // doesn't carry the user's component types,
                        // so opening it would just confuse users.
                        //
                        // The shared `Arc<Mutex<BuildProgress>>` is
                        // assigned to `state.build_progress` here
                        // and re-used by the driver as the cargo
                        // task's sink, so both the modal and the
                        // `BuildStatus::Building` variant point at
                        // the same data.
                        let progress = std::sync::Arc::new(std::sync::Mutex::new(
                            crate::ext_build::BuildProgress::default(),
                        ));
                        state.build_progress = Some(std::sync::Arc::clone(&progress));
                        state.build_progress_snapshot =
                            Some(crate::ext_build::BuildProgress::default());
                        state.static_editor.pending = Some((project_path.clone(), true));
                    }
                    TemplateLinkage::Dylib => {
                        let progress = std::sync::Arc::new(std::sync::Mutex::new(
                            crate::ext_build::BuildProgress::default(),
                        ));
                        state.build_progress = Some(std::sync::Arc::clone(&progress));
                        state.build_progress_snapshot =
                            Some(crate::ext_build::BuildProgress::default());

                        let project_for_task = project_path;
                        let progress_for_task = std::sync::Arc::clone(&progress);
                        let task = AsyncComputeTaskPool::get().spawn(async move {
                            crate::ext_build::build_extension_project_with_progress(
                                &project_for_task,
                                Some(progress_for_task),
                                linkage,
                            )
                        });
                        state.build_task = Some((task, linkage));
                    }
                }
            }
            Err(err) => {
                warn!("Scaffold failed: {err}");
                state.status = Some(format!("Create failed: {err}"));
            }
        }
    }

    // Build task completed. Dylib: stash the artifact for the
    // install-in-Last step. Static: stash the project dir for
    // `apply_pending_static_open`, which calls `enter_project`.
    // The linkage comes off the task tuple so we branch on the
    // actual in-flight build's linkage, not on `state.linkage`
    // which describes the modal's current selection.
    if let Some((task, build_linkage)) = state.build_task.as_mut()
        && let Some(result) = future::block_on(future::poll_once(task))
    {
        let linkage = *build_linkage;
        state.build_task = None;
        match result {
            Ok(artifact_or_project) => match linkage {
                TemplateLinkage::Dylib => {
                    info!("Build produced {}", artifact_or_project.display());
                    state.pending_install = Some(artifact_or_project);
                }
                TemplateLinkage::Static => {
                    // Editor was already opened at scaffold time;
                    // this branch just logs success and clears the
                    // build-progress state so the footer collapses.
                    info!("Static build succeeded: {}", artifact_or_project.display());
                    state.pending_project = None;
                }
            },
            Err(err) => {
                warn!("Build failed: {err}");
                state.status = Some(format!(
                    "Build failed: {err}.\n\
                         Fix the issue and try opening the project again."
                ));
                state.pending_project = None;
            }
        }
    }

    // Sync UI.
    let desired_location = state.location.to_string_lossy().into_owned();
    for mut text in location_texts.iter_mut() {
        if text.0 != desired_location {
            text.0 = desired_location.clone();
        }
    }
    let desired_status = state.status.as_deref().unwrap_or("").to_string();
    for mut text in status_texts.iter_mut() {
        if text.0 != desired_status {
            text.0 = desired_status.clone();
        }
    }
}

/// Copy the shared `BuildProgress` into the per-frame snapshot so
/// the UI-refresh system can read from a plain struct without
/// holding the mutex across rendering.
fn refresh_build_progress_snapshot(mut state: ResMut<NewProjectState>) {
    let Some(ref arc) = state.build_progress else {
        return;
    };
    let snap = {
        let Ok(guard) = arc.lock() else {
            return;
        };
        guard.clone()
    };
    state.build_progress_snapshot = Some(snap);
}

/// Reflect the current snapshot into the modal's progress UI:
/// toggles the container, updates the "compiling `<crate>`" label,
/// scrubs the progress-bar fill, and sets the log-tail text.
fn refresh_build_progress_ui(
    state: Res<NewProjectState>,
    mut containers: Query<&mut Node, With<NewProjectProgressContainer>>,
    mut crate_labels: Query<
        &mut Text,
        (
            With<NewProjectProgressCrateLabel>,
            Without<NewProjectLogText>,
        ),
    >,
    mut log_texts: Query<
        &mut Text,
        (
            With<NewProjectLogText>,
            Without<NewProjectProgressCrateLabel>,
        ),
    >,
    bar_slots: Query<&Children, With<NewProjectProgressBarSlot>>,
    children_q: Query<&Children>,
    mut fill_q: Query<
        &mut Node,
        (
            With<jackdaw_feathers::progress::ProgressBarFill>,
            Without<NewProjectProgressContainer>,
        ),
    >,
) {
    let snapshot = state.build_progress_snapshot.as_ref();

    // Toggle container visibility based on whether a build is active.
    let show = snapshot.is_some();
    for mut node in containers.iter_mut() {
        let desired = if show { Display::Flex } else { Display::None };
        if node.display != desired {
            node.display = desired;
        }
    }

    let Some(progress) = snapshot else {
        return;
    };

    // "Compiling <crate>" or "Preparing…" if we don't know yet.
    let crate_line = match (&progress.current_crate, progress.artifacts_total) {
        (Some(name), Some(total)) => {
            format!("Compiling {name} ({}/{})", progress.artifacts_done, total)
        }
        (Some(name), None) => format!("Compiling {name} ({} so far)", progress.artifacts_done),
        (None, Some(total)) => format!("Preparing build… (0/{total})"),
        (None, None) => "Preparing build…".to_string(),
    };
    for mut t in crate_labels.iter_mut() {
        if t.0 != crate_line {
            t.0 = crate_line.clone();
        }
    }

    // Progress bar fill; walk slot → bar → bar children → fill.
    let fraction = progress.fraction().unwrap_or(0.0).clamp(0.0, 1.0);
    let desired_width = Val::Percent(fraction * 100.0);
    for bar_children in bar_slots.iter() {
        for bar_entity in bar_children.iter() {
            let Ok(inner) = children_q.get(bar_entity) else {
                continue;
            };
            for fill_entity in inner.iter() {
                if let Ok(mut node) = fill_q.get_mut(fill_entity)
                    && node.width != desired_width
                {
                    node.width = desired_width;
                }
            }
        }
    }

    // Log tail.
    let mut joined = String::new();
    for (i, line) in progress.recent_log_lines.iter().enumerate() {
        if i > 0 {
            joined.push('\n');
        }
        joined.push_str(line);
    }
    for mut t in log_texts.iter_mut() {
        if t.0 != joined {
            t.0 = joined.clone();
        }
    }
}

/// Install a freshly-built game/extension dylib, running in the
/// `Last` schedule so `GameApp::add_systems(Update, …)` inside the
/// game's build function mutates `Update` while nobody holds it in
/// `schedule_scope`. See the plugin-registration block at the top
/// of this file for context.
///
/// On success: closes the modal and transitions to the editor.
/// On `LoadError::SymbolMismatch`: closes the modal and opens an info
/// dialog with instructions to run `cargo clean -p <name> && cargo
/// build` from the project directory.
/// On any other error: closes the modal and opens an info dialog
/// with the error message.
fn apply_pending_install(world: &mut World) {
    let artifact_opt = world
        .resource_mut::<NewProjectState>()
        .pending_install
        .take();
    let Some(artifact) = artifact_opt else {
        return;
    };

    let result = crate::extensions_dialog::handle_install_from_path(world, artifact);

    match result {
        Ok(_) => {
            let project = world
                .resource_mut::<NewProjectState>()
                .pending_project
                .clone();
            close_new_project_modal(world);
            if let Some(p) = project {
                transition_to_editor(world, p);
            }
        }
        Err(ref err) if err.is_symbol_mismatch() => {
            let project_name = world
                .resource::<NewProjectState>()
                .pending_project
                .as_deref()
                .and_then(|p| p.file_name())
                .and_then(|s| s.to_str())
                .unwrap_or("project")
                .to_owned();
            warn!("Install failed (SDK mismatch) for `{project_name}`");
            close_new_project_modal(world);
            world.trigger(
                jackdaw_feathers::dialog::OpenDialogEvent::new("SDK mismatch", "OK")
                    .with_description(format!(
                        "Project `{project_name}` was built against a different jackdaw \
                         SDK build. Run this from the project directory to refresh:\n\n\
                         \tcargo clean -p {project_name}\n\
                         \tcargo build\n\n\
                         Then re-open the project."
                    ))
                    .without_cancel(),
            );
        }
        Err(err) => {
            warn!("Install failed: {err}");
            close_new_project_modal(world);
            world.trigger(
                jackdaw_feathers::dialog::OpenDialogEvent::new("Install failed", "OK")
                    .with_description(format!("{err}"))
                    .without_cancel(),
            );
        }
    }
}

/// Open the post-scaffold info dialog telling the user how to
/// launch the project from their terminal. Called inline from
/// `poll_new_project_tasks` via `commands.queue` when the user
/// unchecked "Open editor after creating" in the modal. Replaces
/// the older `apply_pending_skip_build` system that drained a
/// `pending_skip_build_dialog` resource field.
///
/// The body adapts per linkage and preset:
///
/// - Dylib: "open from launcher when ready" (no specific command).
/// - Static + Game: `cd <path> && cargo editor`.
/// - Static + Extension: `cd <path> && cargo run`.
/// - Static + Custom: `cd <path>` with a generic "follow the
///   template's README" pointer.
fn open_skip_build_dialog(
    world: &mut World,
    root: &Path,
    linkage: TemplateLinkage,
    preset: Option<&TemplatePreset>,
) {
    let display_path = root.display().to_string();
    let project_name = root
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or("project")
        .to_owned();
    let description = match (linkage, preset) {
        (TemplateLinkage::Dylib, _) => format!(
            "Files written to {display_path}.\n\nOpen the project from the launcher's recent-projects list when you're ready to build the cdylib and load it into the editor."
        ),
        (TemplateLinkage::Static, Some(TemplatePreset::Game)) => format!(
            "Files written to {display_path}.\n\nTo open in jackdaw with custom components, run:\n\n    cd {display_path}\n    cargo editor\n\n(equivalent to `cargo run --bin editor --features editor`).\n\nThe launcher's recent-projects list opens the project later."
        ),
        (TemplateLinkage::Static, Some(TemplatePreset::Extension)) => format!(
            "Files written to {display_path}.\n\nTo run your extension in jackdaw, run:\n\n    cd {display_path}\n    cargo run\n\nThe launcher's recent-projects list opens the project for level editing only and does NOT auto-build the extension binary."
        ),
        (TemplateLinkage::Static, Some(TemplatePreset::Custom(_)) | None) => format!(
            "Files written to {display_path}.\n\nFollow the template's README for the right cargo command. The launcher's recent-projects list can open the project for level editing later."
        ),
    };

    world.trigger(
        jackdaw_feathers::dialog::OpenDialogEvent::new(format!("`{project_name}` created"), "OK")
            .with_description(description)
            .without_cancel(),
    );
}

/// Static-scaffold sibling of [`apply_pending_install`]. Runs in
/// `Last` and hands off to `enter_project`, which opens the fresh
/// project directly (non-cdylib, so no second build).
fn apply_pending_static_open(world: &mut World) {
    let project = world
        .resource_mut::<NewProjectState>()
        .pending_static_open
        .take();
    let Some(project) = project else {
        return;
    };
    close_new_project_modal(world);
    enter_project(world, project);
}

/// Background-build the user's static editor binary so the
/// inspector can pick up `MyGamePlugin`'s reflected types and PIE
/// can run game code in-process. Runs in `Update` in BOTH
/// `AppState`s so the build keeps progressing after the launcher
/// transitions into the editor.
///
/// Three steps:
///   1. If `pending_static_editor_build` is queued and no task is
///      running, kick off `cargo build --bin editor --features
///      editor` and flip [`BuildStatus`](crate::build_status::BuildStatus) to `Building`.
///   2. If a task is running, poll it. On completion, flip
///      `BuildStatus` to `Ready { auto_reload }` or `Failed`.
///   3. If `BuildStatus` is `Ready` AND `auto_reload`, fire
///      [`do_handoff`] once and clear the flag. Otherwise wait
///      for the user to click the footer indicator.
fn drive_static_editor_build(world: &mut World) {
    use crate::build_status::BuildState;

    // Step 1: dispatch a queued request.
    let pending = world
        .resource_mut::<NewProjectState>()
        .static_editor
        .pending
        .take();
    if let Some((root, auto_reload)) = pending {
        // Don't dispatch if another build is already running.
        let already_running = world
            .resource::<NewProjectState>()
            .static_editor
            .task
            .is_some();
        if already_running {
            // Drop the request silently; the in-flight build will
            // produce a binary the next handoff can use. A cleaner
            // design would queue, but it'd just stack same-project
            // requests; one in-flight is enough.
        } else {
            // Re-use the `Arc<Mutex<BuildProgress>>` already
            // installed on `state.build_progress` (set by the
            // post-scaffold path or the recents-click path) so the
            // launcher's modal — already wired to render
            // `state.build_progress_snapshot` — and
            // `BuildStatus::Building` both observe the same sink.
            // If neither path set one, allocate a fresh sink and
            // install it ourselves so the modal still renders if
            // it's open.
            let progress = world
                .resource::<NewProjectState>()
                .build_progress
                .as_ref()
                .map(std::sync::Arc::clone)
                .unwrap_or_else(|| {
                    std::sync::Arc::new(std::sync::Mutex::new(
                        crate::ext_build::BuildProgress::default(),
                    ))
                });
            let progress_for_task = std::sync::Arc::clone(&progress);
            let root_for_task = root.clone();
            let task = AsyncComputeTaskPool::get().spawn(async move {
                crate::ext_build::build_static_editor_with_progress(
                    &root_for_task,
                    Some(progress_for_task),
                )
            });

            {
                let mut state = world.resource_mut::<NewProjectState>();
                state.static_editor.task = Some(task);
                state.static_editor.auto_reload = auto_reload;
                if state.build_progress.is_none() {
                    state.build_progress = Some(std::sync::Arc::clone(&progress));
                    state.build_progress_snapshot =
                        Some(crate::ext_build::BuildProgress::default());
                }
            }

            world
                .resource_mut::<crate::build_status::BuildStatus>()
                .state = BuildState::Building {
                project: root,
                started: std::time::Instant::now(),
                progress,
            };
        }
    }

    // Step 2: poll the in-flight task.
    let result_opt = {
        let mut state = world.resource_mut::<NewProjectState>();
        state
            .static_editor
            .task
            .as_mut()
            .and_then(|task| future::block_on(future::poll_once(task)))
    };
    if let Some(result) = result_opt {
        let auto_reload = {
            let mut state = world.resource_mut::<NewProjectState>();
            state.static_editor.task = None;
            state.static_editor.auto_reload
        };

        let project = match &world.resource::<crate::build_status::BuildStatus>().state {
            BuildState::Building { project, .. } => project.clone(),
            _ => return,
        };

        match result {
            Ok(bin) => {
                info!("Static editor build finished: {}", bin.display());
                world
                    .resource_mut::<crate::build_status::BuildStatus>()
                    .state = BuildState::Ready {
                    project,
                    bin,
                    auto_reload,
                };
            }
            Err(err) => {
                warn!("Static editor build failed: {err}");
                world
                    .resource_mut::<crate::build_status::BuildStatus>()
                    .state = BuildState::Failed {
                    project,
                    log_tail: format!("{err}"),
                };
            }
        }
    }

    // Step 3: auto-fire the handoff once for the scaffold case.
    let auto_handoff = matches!(
        world.resource::<crate::build_status::BuildStatus>().state,
        BuildState::Ready {
            auto_reload: true,
            ..
        }
    );
    if auto_handoff {
        // Drain the Ready, do the handoff. After this `do_handoff`
        // writes `AppExit`, so we won't loop.
        let (bin, project) = match std::mem::take(
            &mut world
                .resource_mut::<crate::build_status::BuildStatus>()
                .state,
        ) {
            BuildState::Ready { bin, project, .. } => (bin, project),
            other => {
                world
                    .resource_mut::<crate::build_status::BuildStatus>()
                    .state = other;
                return;
            }
        };
        // Close the launcher modal before spawning so the user's
        // editor window comes up against a clean launcher viewport
        // (briefly, until `AppExit` tears it down).
        close_new_project_modal(world);
        do_handoff(world, &bin, &project);
    }
}

/// Save the active scene if dirty, spawn the user's static editor
/// binary, and exit the launcher process. The user's binary loads
/// the same scene file from disk on its first frame, so anything
/// the user authored in the launcher's editor round-trips
/// transparently.
///
/// Called from [`drive_static_editor_build`]'s auto-fire branch
/// (post-scaffold case) and from the status-bar click observer
/// (user-driven reload).
pub(crate) fn do_handoff(world: &mut World, bin: &Path, project_root: &Path) {
    info!(
        "Handing off to static editor {} (cwd={})",
        bin.display(),
        project_root.display()
    );
    // The user's editor binary reads its scene from disk on its
    // first frame; if the launcher has unsaved edits and a path
    // is already set, persist them first so the round-trip is
    // transparent. If no path is set (untouched scaffold or
    // unsaved-as-yet scene), skip — opening a Save As dialog
    // mid-handoff would be jarring; the user can save manually
    // before clicking Reload.
    let has_scene_path = world
        .get_resource::<scene_io::SceneFilePath>()
        .is_some_and(|p| p.path.is_some());
    if has_scene_path {
        scene_io::save_scene(world);
    }

    // Touch the project in the launcher's recents list so it
    // shows up next launch. The static-handoff path doesn't go
    // through `transition_to_editor` (which is where dylib /
    // launcher-internal opens get touch_recent'd), so without
    // this the freshly-scaffolded project disappears from the
    // launcher between sessions.
    let project_name = project::load_project_config(project_root)
        .map(|cfg| cfg.project.name)
        .unwrap_or_else(|| {
            project_root
                .file_name()
                .and_then(|s| s.to_str())
                .unwrap_or("project")
                .to_string()
        });
    project::touch_recent(project_root, &project_name);

    // `JACKDAW_PROJECT` tells the spawned editor binary which
    // project to auto-open on its first frame. Without this, the
    // editor's `main()` defaults to `AppState::ProjectSelect` and
    // the user lands on the launcher's picker again instead of
    // the editor view for `project_root`.
    let spawn_result = std::process::Command::new(bin)
        .current_dir(project_root)
        .env("JACKDAW_PROJECT", project_root)
        .spawn();
    match spawn_result {
        Ok(_child) => {
            info!("Static editor spawned; exiting launcher");
            world.write_message(bevy::app::AppExit::Success);
        }
        Err(e) => {
            warn!(
                "Failed to spawn static editor binary {}: {e}",
                bin.display()
            );
            // Surface to the UI so the user can retry. Reset
            // BuildStatus to Ready (without auto_reload) so the
            // footer keeps offering the click target.
            let mut status = world.resource_mut::<crate::build_status::BuildStatus>();
            if let crate::build_status::BuildState::Ready { auto_reload, .. } = &mut status.state {
                *auto_reload = false;
            }
            world.resource_mut::<NewProjectState>().status =
                Some(format!("Failed to spawn editor binary: {e}"));
        }
    }
}