quickgui 0.1.5

A damage-driven, GPU-accelerated desktop GUI foundation
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
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
use std::{
    cell::RefCell,
    fmt,
    future::Future,
    path::{Path, PathBuf},
    pin::Pin,
    rc::Rc,
    sync::{
        Arc,
        atomic::{AtomicU64, Ordering},
    },
    task::{Context, Poll, Waker},
};
use web_time::SystemTime;

use thiserror::Error;
#[cfg(target_os = "macos")]
use winit::event_loop::EventLoopProxy;

#[cfg(target_os = "macos")]
use crate::runtime::RuntimeEvent;
use crate::{Image, Menu, TrayIconOptions, WindowHandle};

/// Maximum native platform operations one event callback may queue.
pub const MAX_PLATFORM_REQUESTS_PER_EVENT: usize = 32;
/// Maximum deferred platform operations retained by one application effect cycle.
pub const MAX_PENDING_PLATFORM_REQUESTS: usize = 128;
/// Maximum simultaneously visible native prompts or file panels in one application.
pub const MAX_ACTIVE_PLATFORM_DIALOGS: usize = 32;
/// Maximum UTF-8 bytes retained by one native dialog text field.
pub const MAX_PLATFORM_TEXT_BYTES: usize = 64 * 1024;
/// Maximum UTF-8 bytes retained by one native prompt button label.
pub const MAX_PROMPT_BUTTON_BYTES: usize = 1_024;
/// Maximum UTF-8 bytes retained by one native message-box checkbox label.
pub const MAX_MESSAGE_BOX_CHECKBOX_BYTES: usize = 4 * 1_024;
/// Maximum UTF-8 bytes retained by one Quick Look preview display name.
pub const MAX_FILE_PREVIEW_NAME_BYTES: usize = 1_024;
/// Maximum items accepted by one native share sheet.
pub const MAX_SHARE_ITEMS: usize = 32;
/// Maximum UTF-8 bytes accepted for one share-sheet text or URL item.
pub const MAX_SHARE_ITEM_TEXT_BYTES: usize = 64 * 1024;
/// Maximum UTF-8 bytes accepted for one biometric-authentication reason.
pub const MAX_BIOMETRIC_REASON_BYTES: usize = 4 * 1_024;
/// Maximum buttons accepted by one native prompt.
pub const MAX_PROMPT_BUTTONS: usize = 16;
/// Maximum platform-native bytes accepted for one filesystem path.
pub const MAX_PLATFORM_PATH_BYTES: usize = 16 * 1024;
/// Maximum UTF-8 bytes accepted for one URL shell action.
pub const MAX_PLATFORM_URL_BYTES: usize = 16 * 1024;
/// Maximum paths copied out of one native open panel.
pub const MAX_SELECTED_PATHS: usize = 4_096;
/// Maximum aggregate filesystem bytes copied out of one native open panel.
pub const MAX_SELECTED_PATHS_TOTAL_BYTES: usize = 16 * 1024 * 1024;
/// Maximum named filters accepted by one native file dialog.
pub const MAX_FILE_DIALOG_FILTERS: usize = 64;
/// Maximum extensions retained across all filters in one native file dialog.
pub const MAX_FILE_DIALOG_FILTER_EXTENSIONS: usize = 256;
/// Maximum aggregate UTF-8 bytes retained by file-dialog filter names and extensions.
pub const MAX_FILE_DIALOG_FILTER_BYTES: usize = 64 * 1024;
/// Maximum URLs accepted from one native application-open callback.
pub const MAX_OPEN_URLS: usize = 256;
/// Maximum aggregate UTF-8 bytes retained by one native application-open callback.
pub const MAX_OPEN_URLS_TOTAL_BYTES: usize = 1024 * 1024;
/// Maximum UTF-8 bytes retained by a system-notification identity tag.
pub const MAX_SYSTEM_NOTIFICATION_TAG_BYTES: usize = 1_024;
/// Maximum UTF-8 bytes retained by a system-notification title.
pub const MAX_SYSTEM_NOTIFICATION_TITLE_BYTES: usize = 4 * 1024;
/// Maximum UTF-8 bytes retained by a system-notification body.
pub const MAX_SYSTEM_NOTIFICATION_BODY_BYTES: usize = 64 * 1024;
/// Maximum action buttons accepted by one system notification.
pub const MAX_SYSTEM_NOTIFICATION_ACTIONS: usize = 16;
/// Maximum UTF-8 bytes retained by one notification action identifier or label.
pub const MAX_SYSTEM_NOTIFICATION_ACTION_BYTES: usize = 1_024;
/// Maximum media attachments accepted by one system notification.
pub const MAX_SYSTEM_NOTIFICATION_ATTACHMENTS: usize = 8;
/// Maximum UTF-8 bytes retained by an attachment identifier, reply placeholder, or sound name.
pub const MAX_SYSTEM_NOTIFICATION_OPTION_BYTES: usize = 1_024;
/// Maximum UTF-8 bytes copied out of one native inline-reply response.
pub const MAX_SYSTEM_NOTIFICATION_REPLY_BYTES: usize = 64 * 1024;
/// Maximum icon bytes copied into a desktop notification portal request.
pub const MAX_SYSTEM_NOTIFICATION_ICON_BYTES: usize = 16 * 1024 * 1024;
/// Maximum distinct native notification action sets retained by one application.
pub const MAX_SYSTEM_NOTIFICATION_CATEGORIES: usize = 64;
/// Maximum distinct notification tags retained while native authorization is pending.
pub const MAX_PENDING_SYSTEM_NOTIFICATIONS: usize = 64;
/// Maximum permission futures coalesced behind one native notification authorization operation.
pub const MAX_PENDING_NOTIFICATION_PERMISSION_REQUESTS: usize = 64;
/// Maximum UTF-8 bytes accepted for a macOS Dock badge label.
pub const MAX_DOCK_BADGE_BYTES: usize = 1_024;
/// Maximum UTF-8 bytes retained by one About-panel field.
pub const MAX_ABOUT_PANEL_TEXT_BYTES: usize = 64 * 1_024;
/// Maximum UTF-8 bytes retained by a taskbar-overlay accessibility description.
pub const MAX_TASKBAR_OVERLAY_DESCRIPTION_BYTES: usize = 4 * 1_024;
/// Maximum Windows Jump List user tasks accepted in one replacement.
pub const MAX_USER_TASKS: usize = 32;
/// Maximum UTF-8 bytes retained by one user-task text field.
pub const MAX_USER_TASK_TEXT_BYTES: usize = 16 * 1_024;

// Apple reserves these identifiers for activating or dismissing the notification itself. Letting
// an application reuse them would make a response ambiguous at the cross-platform API boundary.
const SYSTEM_NOTIFICATION_DEFAULT_ACTION_ID: &str =
    "com.apple.UNNotificationDefaultActionIdentifier";
const SYSTEM_NOTIFICATION_DISMISS_ACTION_ID: &str =
    "com.apple.UNNotificationDismissActionIdentifier";

/// A platform-service request could not be accepted or completed.
#[derive(Clone, Debug, Eq, Error, PartialEq)]
pub enum PlatformError {
    #[error("this context is not attached to a native window")]
    Unavailable,
    #[error(
        "one event cannot queue more than {MAX_PLATFORM_REQUESTS_PER_EVENT} platform operations"
    )]
    QueueFull,
    #[error(
        "one effect cycle cannot retain more than {MAX_PENDING_PLATFORM_REQUESTS} platform operations"
    )]
    PendingQueueFull,
    #[error("the application or a related window already owns an active native dialog")]
    DialogBusy,
    #[error("the application already owns {MAX_ACTIVE_PLATFORM_DIALOGS} active native dialogs")]
    TooManyDialogs,
    #[error("this platform service is not implemented on the current operating system")]
    Unsupported,
    #[error("the native menu declaration is invalid or exceeds its public bounds")]
    InvalidMenu,
    #[error("a native popup-menu position must be finite and within the supported desktop range")]
    InvalidMenuPosition,
    #[error("a native prompt message cannot be empty")]
    EmptyPromptMessage,
    #[error(
        "native dialog text must be NUL-free and at most {MAX_PLATFORM_TEXT_BYTES} UTF-8 bytes"
    )]
    InvalidText,
    #[error("a native prompt requires 1 to {MAX_PROMPT_BUTTONS} valid buttons")]
    InvalidButtons,
    #[error(
        "native prompt button labels must be nonempty, NUL-free, and at most {MAX_PROMPT_BUTTON_BYTES} UTF-8 bytes"
    )]
    InvalidButton,
    #[error(
        "a path must be nonempty, NUL-free, and at most {MAX_PLATFORM_PATH_BYTES} platform bytes"
    )]
    InvalidPath,
    #[error("a URL must be nonempty, NUL-free, and at most {MAX_PLATFORM_URL_BYTES} UTF-8 bytes")]
    InvalidUrl,
    #[error(
        "a system-notification tag must be nonempty, NUL-free, and at most {MAX_SYSTEM_NOTIFICATION_TAG_BYTES} UTF-8 bytes"
    )]
    InvalidNotificationTag,
    #[error("system-notification text is invalid or exceeds the title/body byte limits")]
    InvalidNotificationText,
    #[error(
        "a system notification cannot contain more than {MAX_SYSTEM_NOTIFICATION_ACTIONS} actions"
    )]
    InvalidNotificationActions,
    #[error(
        "notification action ids must be unique and non-reserved; ids and labels must be nonempty, NUL-free, and at most {MAX_SYSTEM_NOTIFICATION_ACTION_BYTES} UTF-8 bytes"
    )]
    InvalidNotificationAction,
    #[error("system-notification media, sound, schedule, or reply options are invalid")]
    InvalidNotificationOptions,
    #[error("a Dock badge must be NUL-free and at most {MAX_DOCK_BADGE_BYTES} UTF-8 bytes")]
    InvalidDockBadge,
    #[error("About-panel options contain NULs or exceed their public text bounds")]
    InvalidAboutPanelOptions,
    #[error("a user-task declaration is invalid or exceeds its public bounds")]
    InvalidUserTasks,
    #[error("an open panel must allow files, directories, or both")]
    InvalidPathSelection,
    #[error("native file-dialog filters are invalid or exceed their count or byte limits")]
    InvalidFileDialogFilter,
    #[error("the native path selection exceeded QuickGUI's bounded result size")]
    SelectionTooLarge,
    #[error(
        "a share sheet requires 1 to {MAX_SHARE_ITEMS} items whose text, URLs, and paths are nonempty, NUL-free, and within their byte bounds"
    )]
    InvalidShareItems,
    #[error("a share-sheet anchor must be finite with non-negative dimensions")]
    InvalidShareAnchor,
    #[error("native platform operation failed: {0}")]
    Platform(Arc<str>),
}

/// Native prompt severity, matching the platform's informational, warning, and critical styles.
/// How the application appears to the operating system's window and application switchers.
///
/// This is AppKit's `NSApplicationActivationPolicy`. Other platforms retain the requested value
/// and report [`PlatformError::Unsupported`].
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum ActivationPolicy {
    /// An ordinary application with a Dock tile and a menu bar.
    #[default]
    Regular,
    /// A background application that can still show windows but owns no Dock tile.
    Accessory,
    /// A background application that cannot be activated at all.
    Prohibited,
}

/// Urgency of a request for the user's attention.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum DockAttention {
    /// Bounce until the application is activated or the request is cancelled.
    Critical,
    /// Bounce once.
    Informational,
}

/// Identifier for one in-flight [`DockAttention`] request.
///
/// Pass it to `cancel_dock_attention` to stop a critical bounce early. `0` is never a valid
/// request identifier.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct DockAttentionRequest(i64);

impl DockAttentionRequest {
    pub(crate) const fn new(value: i64) -> Self {
        Self(value)
    }

    pub const fn get(self) -> i64 {
        self.0
    }
}

/// Whether the running process can relocate itself into `/Applications`.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub struct ApplicationsFolderSupport {
    /// Whether the platform implements the move at all.
    pub supported: bool,
    /// Whether the executable is already inside an `/Applications` directory.
    pub already_installed: bool,
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum PromptLevel {
    Info,
    Warning,
    Critical,
}

/// URLs supplied by the operating system when launching or reopening the application.
///
/// The collection is shared across callback clones and is always bounded by
/// [`MAX_OPEN_URLS`] and [`MAX_OPEN_URLS_TOTAL_BYTES`]. File opens are represented by ordinary
/// `file:` URLs, matching AppKit's modern `application:openURLs:` contract.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OpenUrls {
    urls: Arc<[Arc<str>]>,
}

impl OpenUrls {
    pub(crate) fn from_bounded(urls: Vec<Arc<str>>) -> Self {
        debug_assert!(urls.len() <= MAX_OPEN_URLS);
        debug_assert!(urls.iter().map(|url| url.len()).sum::<usize>() <= MAX_OPEN_URLS_TOTAL_BYTES);
        Self {
            urls: Arc::from(urls),
        }
    }

    pub fn as_slice(&self) -> &[Arc<str>] {
        &self.urls
    }

    pub fn iter(&self) -> impl ExactSizeIterator<Item = &str> {
        self.urls.iter().map(AsRef::as_ref)
    }

    pub fn len(&self) -> usize {
        self.urls.len()
    }

    pub fn is_empty(&self) -> bool {
        self.urls.is_empty()
    }
}

/// One button exposed by an operating-system notification.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct SystemNotificationAction {
    pub id: Arc<str>,
    pub label: Arc<str>,
    pub kind: SystemNotificationActionKind,
}

/// Interaction requested for a notification action.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum SystemNotificationActionKind {
    Button,
    TextInput { placeholder: Option<Arc<str>> },
}

impl SystemNotificationAction {
    pub fn new(id: impl Into<Arc<str>>, label: impl Into<Arc<str>>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            kind: SystemNotificationActionKind::Button,
        }
    }

    /// Request an inline text response from the operating system.
    pub fn text_input(mut self, placeholder: impl Into<Arc<str>>) -> Self {
        self.kind = SystemNotificationActionKind::TextInput {
            placeholder: Some(placeholder.into()),
        };
        self
    }

    pub fn text_input_without_placeholder(mut self) -> Self {
        self.kind = SystemNotificationActionKind::TextInput { placeholder: None };
        self
    }
}

/// Audio policy for one operating-system notification.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub enum SystemNotificationSound {
    #[default]
    Default,
    Silent,
    /// A platform-recognized bundled or system sound name.
    Named(Arc<str>),
}

/// One local file attached to a rich operating-system notification.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct SystemNotificationAttachment {
    pub id: Arc<str>,
    pub path: PathBuf,
}

impl SystemNotificationAttachment {
    pub fn new(id: impl Into<Arc<str>>, path: impl Into<PathBuf>) -> Self {
        Self {
            id: id.into(),
            path: path.into(),
        }
    }
}

/// A notification posted to the operating system rather than rendered in a QuickGUI window.
///
/// Posting another notification with the same `tag` replaces the earlier request where the
/// platform supports replacement.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SystemNotification {
    pub tag: Arc<str>,
    pub title: Arc<str>,
    pub subtitle: Option<Arc<str>>,
    pub body: Arc<str>,
    pub actions: Vec<SystemNotificationAction>,
    pub sound: SystemNotificationSound,
    /// Local image used as the notification icon where the platform supports an explicit icon.
    pub icon: Option<PathBuf>,
    pub attachments: Vec<SystemNotificationAttachment>,
    /// Absolute delivery time. `None` posts immediately.
    pub delivery_at: Option<SystemTime>,
}

impl SystemNotification {
    pub fn new(
        tag: impl Into<Arc<str>>,
        title: impl Into<Arc<str>>,
        body: impl Into<Arc<str>>,
    ) -> Self {
        Self {
            tag: tag.into(),
            title: title.into(),
            subtitle: None,
            body: body.into(),
            actions: Vec::new(),
            sound: SystemNotificationSound::Default,
            icon: None,
            attachments: Vec::new(),
            delivery_at: None,
        }
    }

    pub fn subtitle(mut self, subtitle: impl Into<Arc<str>>) -> Self {
        self.subtitle = Some(subtitle.into());
        self
    }

    pub fn action(mut self, action: SystemNotificationAction) -> Self {
        self.actions.push(action);
        self
    }

    pub fn sound(mut self, sound: SystemNotificationSound) -> Self {
        self.sound = sound;
        self
    }

    pub fn icon(mut self, path: impl Into<PathBuf>) -> Self {
        self.icon = Some(path.into());
        self
    }

    pub fn attachment(mut self, attachment: SystemNotificationAttachment) -> Self {
        self.attachments.push(attachment);
        self
    }

    pub fn deliver_at(mut self, delivery_at: SystemTime) -> Self {
        self.delivery_at = Some(delivery_at);
        self
    }
}

/// The user's activation of an operating-system notification.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SystemNotificationResponse {
    pub tag: Arc<str>,
    /// The selected action id, or `None` when the notification body was activated.
    pub action_id: Option<Arc<str>>,
    /// Bounded inline text supplied for a text-input action.
    pub reply: Option<Arc<str>>,
}

/// Current operating-system authorization for application notifications.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum NotificationPermissionStatus {
    NotDetermined,
    Granted,
    Denied,
    Unsupported,
}

/// Requested native file-icon size.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum FileIconSize {
    Small,
    #[default]
    Normal,
    Large,
}

impl FileIconSize {
    #[cfg_attr(not(any(target_os = "macos", target_os = "windows")), allow(dead_code))]
    pub(crate) const fn pixels(self) -> u32 {
        match self {
            Self::Small => 16,
            Self::Normal => 32,
            Self::Large => 48,
        }
    }
}

/// Metadata shown by the operating system's standard About UI.
///
/// Omitted fields fall back to package metadata on platforms that provide such defaults.
#[derive(Clone, Debug, Default)]
pub struct AboutPanelOptions {
    pub application_name: Option<Arc<str>>,
    pub application_version: Option<Arc<str>>,
    pub version: Option<Arc<str>>,
    pub copyright: Option<Arc<str>>,
    pub credits: Option<Arc<str>>,
    pub icon: Option<Image>,
}

impl AboutPanelOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn application_name(mut self, value: impl Into<Arc<str>>) -> Self {
        self.application_name = Some(value.into());
        self
    }

    pub fn application_version(mut self, value: impl Into<Arc<str>>) -> Self {
        self.application_version = Some(value.into());
        self
    }

    pub fn version(mut self, value: impl Into<Arc<str>>) -> Self {
        self.version = Some(value.into());
        self
    }

    pub fn copyright(mut self, value: impl Into<Arc<str>>) -> Self {
        self.copyright = Some(value.into());
        self
    }

    pub fn credits(mut self, value: impl Into<Arc<str>>) -> Self {
        self.credits = Some(value.into());
        self
    }

    pub fn icon(mut self, icon: Image) -> Self {
        self.icon = Some(icon);
        self
    }
}

/// One command exposed in the Windows taskbar Jump List.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UserTask {
    pub title: Arc<str>,
    pub program: Option<PathBuf>,
    pub arguments: Arc<str>,
    pub description: Option<Arc<str>>,
    pub working_directory: Option<PathBuf>,
    pub icon_path: Option<PathBuf>,
    pub icon_index: i32,
}

impl UserTask {
    /// Create a task that launches the current executable with `arguments`.
    pub fn new(title: impl Into<Arc<str>>, arguments: impl Into<Arc<str>>) -> Self {
        Self {
            title: title.into(),
            program: None,
            arguments: arguments.into(),
            description: None,
            working_directory: None,
            icon_path: None,
            icon_index: 0,
        }
    }

    pub fn program(mut self, path: impl Into<PathBuf>) -> Self {
        self.program = Some(path.into());
        self
    }

    pub fn description(mut self, value: impl Into<Arc<str>>) -> Self {
        self.description = Some(value.into());
        self
    }

    pub fn working_directory(mut self, path: impl Into<PathBuf>) -> Self {
        self.working_directory = Some(path.into());
        self
    }

    pub fn icon(mut self, path: impl Into<PathBuf>, index: i32) -> Self {
        self.icon_path = Some(path.into());
        self.icon_index = index;
        self
    }
}

/// Future-like result of an operating-system file-icon lookup.
pub type FileIconResponse = PlatformResponse<Image>;

/// One semantically classified native prompt button.
///
/// The first `Ok` button keeps the native Return-key equivalent. `Cancel` receives Escape on
/// macOS. `Other` is useful for alternatives such as “Don't Save”.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum PromptButton {
    Ok(Arc<str>),
    Cancel(Arc<str>),
    Other(Arc<str>),
}

impl PromptButton {
    pub fn ok(label: impl Into<Arc<str>>) -> Self {
        Self::Ok(label.into())
    }

    pub fn cancel(label: impl Into<Arc<str>>) -> Self {
        Self::Cancel(label.into())
    }

    pub fn new(label: impl Into<Arc<str>>) -> Self {
        Self::Other(label.into())
    }

    pub fn label(&self) -> &str {
        match self {
            Self::Ok(label) | Self::Cancel(label) | Self::Other(label) => label,
        }
    }

    pub fn is_cancel(&self) -> bool {
        matches!(self, Self::Cancel(_))
    }
}

impl From<&str> for PromptButton {
    fn from(label: &str) -> Self {
        Self::new(label)
    }
}

impl From<String> for PromptButton {
    fn from(label: String) -> Self {
        Self::new(label)
    }
}

/// One named native file-dialog filter. Extensions omit the leading dot; `*` matches all files.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct FileDialogFilter {
    pub name: Arc<str>,
    pub extensions: Vec<Arc<str>>,
}

impl FileDialogFilter {
    pub fn new<I, S>(name: impl Into<Arc<str>>, extensions: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<Arc<str>>,
    {
        Self {
            name: name.into(),
            extensions: extensions.into_iter().map(Into::into).collect(),
        }
    }
}

/// Options for a native open panel.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PathPromptOptions {
    pub files: bool,
    pub directories: bool,
    pub multiple: bool,
    pub title: Option<Arc<str>>,
    pub prompt: Option<Arc<str>>,
    pub directory: Option<PathBuf>,
    pub suggested_name: Option<Arc<str>>,
    pub filters: Vec<FileDialogFilter>,
    pub shows_hidden_files: bool,
    /// Whether the panel offers its "New Folder" affordance.
    pub can_create_directories: bool,
    /// Whether the panel resolves a selected alias to its original item.
    pub resolves_aliases: bool,
    /// Whether bundles such as `.app` are browsed as directories instead of chosen as files.
    pub treats_file_packages_as_directories: bool,
    /// Explanatory text shown above the panel's browser.
    pub message: Option<Arc<str>>,
}

impl Default for PathPromptOptions {
    fn default() -> Self {
        Self {
            files: true,
            directories: false,
            multiple: false,
            title: None,
            prompt: None,
            directory: None,
            suggested_name: None,
            filters: Vec::new(),
            shows_hidden_files: false,
            can_create_directories: true,
            resolves_aliases: false,
            treats_file_packages_as_directories: false,
            message: None,
        }
    }
}

impl PathPromptOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn files(mut self, files: bool) -> Self {
        self.files = files;
        self
    }

    pub fn directories(mut self, directories: bool) -> Self {
        self.directories = directories;
        self
    }

    pub fn multiple(mut self, multiple: bool) -> Self {
        self.multiple = multiple;
        self
    }

    pub fn title(mut self, title: impl Into<Arc<str>>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn prompt(mut self, prompt: impl Into<Arc<str>>) -> Self {
        self.prompt = Some(prompt.into());
        self
    }

    pub fn directory(mut self, directory: impl Into<PathBuf>) -> Self {
        self.directory = Some(directory.into());
        self
    }

    pub fn suggested_name(mut self, suggested_name: impl Into<Arc<str>>) -> Self {
        self.suggested_name = Some(suggested_name.into());
        self
    }

    pub fn filters(mut self, filters: impl IntoIterator<Item = FileDialogFilter>) -> Self {
        self.filters = filters.into_iter().collect();
        self
    }

    pub fn shows_hidden_files(mut self, shows_hidden_files: bool) -> Self {
        self.shows_hidden_files = shows_hidden_files;
        self
    }

    /// Offer or hide the panel's "New Folder" affordance. Enabled by default.
    pub fn can_create_directories(mut self, can_create_directories: bool) -> Self {
        self.can_create_directories = can_create_directories;
        self
    }

    /// Resolve a selected alias or symbolic link to its original item.
    pub fn resolves_aliases(mut self, resolves_aliases: bool) -> Self {
        self.resolves_aliases = resolves_aliases;
        self
    }

    /// Browse bundles such as `.app` as ordinary directories.
    pub fn treats_file_packages_as_directories(mut self, treats: bool) -> Self {
        self.treats_file_packages_as_directories = treats;
        self
    }

    /// Explanatory text shown above the panel's browser.
    pub fn message(mut self, message: impl Into<Arc<str>>) -> Self {
        self.message = Some(message.into());
        self
    }
}

/// Options for a native save panel.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SavePathOptions {
    pub directory: PathBuf,
    pub title: Option<Arc<str>>,
    pub suggested_name: Option<Arc<str>>,
    pub prompt: Option<Arc<str>>,
    pub filters: Vec<FileDialogFilter>,
    pub shows_hidden_files: bool,
    /// Label shown beside the file-name field.
    pub name_field_label: Option<Arc<str>>,
    /// Whether the panel offers the Finder tag field.
    pub shows_tag_field: bool,
}

impl SavePathOptions {
    pub fn new(directory: impl Into<PathBuf>) -> Self {
        Self {
            directory: directory.into(),
            title: None,
            suggested_name: None,
            prompt: None,
            filters: Vec::new(),
            shows_hidden_files: false,
            name_field_label: None,
            shows_tag_field: true,
        }
    }

    pub fn title(mut self, title: impl Into<Arc<str>>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn suggested_name(mut self, suggested_name: impl Into<Arc<str>>) -> Self {
        self.suggested_name = Some(suggested_name.into());
        self
    }

    pub fn prompt(mut self, prompt: impl Into<Arc<str>>) -> Self {
        self.prompt = Some(prompt.into());
        self
    }

    pub fn filters(mut self, filters: impl IntoIterator<Item = FileDialogFilter>) -> Self {
        self.filters = filters.into_iter().collect();
        self
    }

    pub fn shows_hidden_files(mut self, shows_hidden_files: bool) -> Self {
        self.shows_hidden_files = shows_hidden_files;
        self
    }

    /// Label shown beside the file-name field.
    pub fn name_field_label(mut self, label: impl Into<Arc<str>>) -> Self {
        self.name_field_label = Some(label.into());
        self
    }

    /// Show or hide the Finder tag field. Shown by default.
    pub fn shows_tag_field(mut self, shows_tag_field: bool) -> Self {
        self.shows_tag_field = shows_tag_field;
        self
    }
}

/// When a system color panel reports the user's selection.
#[derive(Clone, Copy, Debug, Default, Eq, Hash, PartialEq)]
pub enum ColorPanelMode {
    /// Report every intermediate color while the user drags inside the panel.
    #[default]
    Continuous,
    /// Report only the final color, once the panel is dismissed.
    OnClose,
}

/// One item offered to the operating system's share sheet.
#[derive(Clone, Debug, PartialEq)]
pub enum ShareItem {
    Text(Arc<str>),
    Url(Arc<str>),
    File(PathBuf),
    Image(Image),
}

/// One optional suppression checkbox shown inside a native message box.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MessageBoxCheckbox {
    pub label: Arc<str>,
    pub checked: bool,
}

/// The user's answer to a native message box.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct MessageBoxResponse {
    /// Index of the chosen button in the declared button order.
    pub button: usize,
    /// Final checkbox state, always `false` when no checkbox was declared.
    pub checkbox_checked: bool,
}

/// A native message box with an optional checkbox, custom icon, and explicit key buttons.
///
/// This is the richer sibling of [`crate::EventContext::prompt`]. Every field is validated and
/// bounded before the request is retained, and the response carries the checkbox state so a
/// "do not ask again" affordance needs no second round trip.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct MessageBoxOptions {
    pub level: Option<PromptLevel>,
    pub message: Arc<str>,
    pub detail: Option<Arc<str>>,
    pub buttons: Vec<PromptButton>,
    pub default_button: Option<usize>,
    pub cancel_button: Option<usize>,
    pub checkbox: Option<MessageBoxCheckbox>,
    pub icon: Option<Image>,
}

impl MessageBoxOptions {
    /// Start a message box with one `OK` button at [`PromptLevel::Info`].
    pub fn new(message: impl Into<Arc<str>>) -> Self {
        Self {
            level: None,
            message: message.into(),
            detail: None,
            buttons: Vec::new(),
            default_button: None,
            cancel_button: None,
            checkbox: None,
            icon: None,
        }
    }

    pub fn level(mut self, level: PromptLevel) -> Self {
        self.level = Some(level);
        self
    }

    /// Secondary explanatory text shown below the message.
    pub fn detail(mut self, detail: impl Into<Arc<str>>) -> Self {
        self.detail = Some(detail.into());
        self
    }

    pub fn buttons(mut self, buttons: impl IntoIterator<Item = PromptButton>) -> Self {
        self.buttons = buttons.into_iter().collect();
        self
    }

    /// Index of the button activated by Return.
    pub fn default_button(mut self, index: usize) -> Self {
        self.default_button = Some(index);
        self
    }

    /// Index of the button activated by Escape.
    pub fn cancel_button(mut self, index: usize) -> Self {
        self.cancel_button = Some(index);
        self
    }

    /// Add a suppression checkbox whose final state is returned with the answer.
    pub fn checkbox(mut self, label: impl Into<Arc<str>>, initially_checked: bool) -> Self {
        self.checkbox = Some(MessageBoxCheckbox {
            label: label.into(),
            checked: initially_checked,
        });
        self
    }

    /// Replace the level's stock artwork with an application image.
    pub fn icon(mut self, icon: Image) -> Self {
        self.icon = Some(icon);
        self
    }

    /// Declared buttons, or the implicit single `OK` button.
    pub fn resolved_buttons(&self) -> Vec<PromptButton> {
        if self.buttons.is_empty() {
            vec![PromptButton::ok("OK")]
        } else {
            self.buttons.clone()
        }
    }
}

/// A pending native message-box answer.
pub type MessageBoxResponseFuture = PlatformResponse<MessageBoxResponse>;

struct PlatformResponseState<T> {
    result: RefCell<Option<Result<T, PlatformError>>>,
    waker: RefCell<Option<Waker>>,
    receiver_alive: std::cell::Cell<bool>,
    #[cfg(target_os = "macos")]
    cancellation: RefCell<Option<PlatformCancellation>>,
}

impl<T> Default for PlatformResponseState<T> {
    fn default() -> Self {
        Self {
            result: RefCell::new(None),
            waker: RefCell::new(None),
            receiver_alive: std::cell::Cell::new(true),
            #[cfg(target_os = "macos")]
            cancellation: RefCell::new(None),
        }
    }
}

/// A single-use, main-thread future resolved by a native platform operation.
///
/// It stores no timer and performs no polling while the operating system owns the prompt. Native
/// completion wakes QuickGUI's foreground executor exactly once.
#[must_use = "await the platform response from a foreground task"]
pub struct PlatformResponse<T> {
    state: Rc<PlatformResponseState<T>>,
}

/// Future returned by a native open panel.
pub type PathPromptResponse = PlatformResponse<Option<Vec<PathBuf>>>;
/// Future returned by a native save panel.
pub type SavePathResponse = PlatformResponse<Option<PathBuf>>;
/// Future returned by a notification authorization status query or explicit request.
pub type NotificationPermissionResponse = PlatformResponse<NotificationPermissionStatus>;
/// Future returned after the operating system accepts or rejects one shell integration request.
pub type ShellResponse = PlatformResponse<()>;

impl<T> Future for PlatformResponse<T> {
    type Output = Result<T, PlatformError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        if let Some(result) = self.state.result.borrow_mut().take() {
            self.state.waker.borrow_mut().take();
            return Poll::Ready(result);
        }
        let mut waker = self.state.waker.borrow_mut();
        if waker
            .as_ref()
            .is_none_or(|current| !current.will_wake(cx.waker()))
        {
            *waker = Some(cx.waker().clone());
        }
        Poll::Pending
    }
}

impl<T> Drop for PlatformResponse<T> {
    fn drop(&mut self) {
        self.state.receiver_alive.set(false);
        self.state.waker.borrow_mut().take();
        #[cfg(target_os = "macos")]
        if let Some(cancellation) = self.state.cancellation.borrow_mut().take() {
            let _ = cancellation
                .proxy
                .send_event(RuntimeEvent::PlatformDialogCancelled(
                    cancellation.owner,
                    cancellation.id,
                ));
        }
    }
}

impl<T> fmt::Debug for PlatformResponse<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PlatformResponse")
            .field("ready", &self.state.result.borrow().is_some())
            .finish_non_exhaustive()
    }
}

pub(crate) struct PlatformResponder<T> {
    state: Rc<PlatformResponseState<T>>,
}

#[cfg(target_os = "macos")]
struct PlatformCancellation {
    proxy: EventLoopProxy<RuntimeEvent>,
    owner: Option<WindowHandle>,
    id: PlatformDialogId,
}

impl<T> Clone for PlatformResponder<T> {
    fn clone(&self) -> Self {
        Self {
            state: self.state.clone(),
        }
    }
}

impl<T> PlatformResponder<T> {
    pub(crate) fn is_cancelled(&self) -> bool {
        !self.state.receiver_alive.get()
    }

    pub(crate) fn complete(&self, result: Result<T, PlatformError>) {
        #[cfg(target_os = "macos")]
        self.state.cancellation.borrow_mut().take();
        if self.is_cancelled() {
            return;
        }
        let mut slot = self.state.result.borrow_mut();
        if slot.is_some() {
            return;
        }
        *slot = Some(result);
        drop(slot);
        if let Some(waker) = self.state.waker.borrow_mut().take() {
            waker.wake();
        }
    }

    #[cfg(target_os = "macos")]
    fn bind_cancellation(
        &self,
        proxy: EventLoopProxy<RuntimeEvent>,
        owner: Option<WindowHandle>,
        id: PlatformDialogId,
    ) -> bool {
        if self.is_cancelled() {
            return false;
        }
        *self.state.cancellation.borrow_mut() = Some(PlatformCancellation { proxy, owner, id });
        true
    }
}

impl<T> fmt::Debug for PlatformResponder<T> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PlatformResponder")
            .finish_non_exhaustive()
    }
}

pub(crate) fn response_channel<T>() -> (PlatformResponder<T>, PlatformResponse<T>) {
    let state = Rc::new(PlatformResponseState::default());
    (
        PlatformResponder {
            state: state.clone(),
        },
        PlatformResponse { state },
    )
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(crate) struct PlatformDialogId(u64);

impl PlatformDialogId {
    pub(crate) fn next() -> Self {
        static NEXT_DIALOG_ID: AtomicU64 = AtomicU64::new(1);
        Self(NEXT_DIALOG_ID.fetch_add(1, Ordering::Relaxed).max(1))
    }
}

#[derive(Debug)]
pub(crate) enum PlatformRequest {
    Prompt {
        window: Option<WindowHandle>,
        level: PromptLevel,
        message: Arc<str>,
        detail: Option<Arc<str>>,
        buttons: Vec<PromptButton>,
        responder: PlatformResponder<usize>,
    },
    OpenPaths {
        window: Option<WindowHandle>,
        options: PathPromptOptions,
        responder: PlatformResponder<Option<Vec<PathBuf>>>,
    },
    SavePath {
        window: Option<WindowHandle>,
        options: SavePathOptions,
        responder: PlatformResponder<Option<PathBuf>>,
    },
    MessageBox {
        window: Option<WindowHandle>,
        options: Box<MessageBoxOptions>,
        responder: PlatformResponder<MessageBoxResponse>,
    },
    PreviewFile {
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        path: PathBuf,
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        display_name: Option<Arc<str>>,
        responder: Option<PlatformResponder<()>>,
    },
    CloseFilePreview {
        responder: Option<PlatformResponder<()>>,
    },
    ShowColorPanel {
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        initial: crate::Color,
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        mode: ColorPanelMode,
        responder: Option<PlatformResponder<()>>,
    },
    CloseColorPanel {
        responder: Option<PlatformResponder<()>>,
    },
    ShowFontPanel {
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        font: Box<crate::Font>,
        responder: Option<PlatformResponder<()>>,
    },
    ShareItems {
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        window: Option<WindowHandle>,
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        items: Vec<ShareItem>,
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        anchor: crate::Rect,
        responder: Option<PlatformResponder<()>>,
    },
    AuthenticateWithBiometrics {
        #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
        reason: Arc<str>,
        responder: PlatformResponder<bool>,
    },
    ShowSystemNotification(SystemNotification),
    DismissSystemNotification(Arc<str>),
    NotificationPermissionStatus {
        responder: PlatformResponder<NotificationPermissionStatus>,
    },
    RequestNotificationPermission {
        responder: PlatformResponder<NotificationPermissionStatus>,
    },
    OpenUrl {
        url: Arc<str>,
        responder: Option<PlatformResponder<()>>,
    },
    OpenPath {
        path: PathBuf,
        responder: Option<PlatformResponder<()>>,
    },
    RevealPath {
        path: PathBuf,
        responder: Option<PlatformResponder<()>>,
    },
    TrashPath {
        path: PathBuf,
        responder: Option<PlatformResponder<()>>,
    },
    SetDockBadge(Option<Arc<str>>),
    SetDockIcon(Option<Image>),
    SetDockMenu(Option<Menu>),
    SetTrayIcon(TrayIconOptions),
    RemoveTrayIcon(u32),
    ShowTrayMenu(u32),
    AddRecentDocument(PathBuf),
    ClearRecentDocuments,
    ShowAboutPanel(AboutPanelOptions),
    GetFileIcon {
        path: PathBuf,
        size: FileIconSize,
        responder: PlatformResponder<Image>,
    },
    SetUserTasks {
        tasks: Vec<UserTask>,
        responder: PlatformResponder<()>,
    },
    SetActivationPolicy {
        policy: ActivationPolicy,
        responder: PlatformResponder<()>,
    },
    ActivateApplication {
        force: bool,
    },
    HideApplication,
    UnhideApplication,
    RequestDockAttention {
        attention: DockAttention,
        responder: PlatformResponder<DockAttentionRequest>,
    },
    CancelDockAttention(DockAttentionRequest),
    SetDockVisible {
        visible: bool,
        responder: PlatformResponder<()>,
    },
    SetSecureKeyboardEntry(bool),
    Beep,
    MoveToApplicationsFolder {
        responder: PlatformResponder<bool>,
    },
}

impl PlatformRequest {
    pub(crate) fn prompt(
        window: WindowHandle,
        level: PromptLevel,
        message: impl Into<Arc<str>>,
        detail: Option<Arc<str>>,
        buttons: &[PromptButton],
    ) -> Result<(Self, PlatformResponse<usize>), PlatformError> {
        Self::prompt_with_owner(Some(window), level, message, detail, buttons)
    }

    pub(crate) fn application_prompt(
        level: PromptLevel,
        message: impl Into<Arc<str>>,
        detail: Option<Arc<str>>,
        buttons: &[PromptButton],
    ) -> Result<(Self, PlatformResponse<usize>), PlatformError> {
        Self::prompt_with_owner(None, level, message, detail, buttons)
    }

    fn prompt_with_owner(
        window: Option<WindowHandle>,
        level: PromptLevel,
        message: impl Into<Arc<str>>,
        detail: Option<Arc<str>>,
        buttons: &[PromptButton],
    ) -> Result<(Self, PlatformResponse<usize>), PlatformError> {
        let message = message.into();
        validate_required_text(&message)?;
        if let Some(detail) = &detail {
            validate_optional_text(detail)?;
        }
        validate_prompt_buttons(buttons)?;
        let (responder, response) = response_channel();
        Ok((
            Self::Prompt {
                window,
                level,
                message,
                detail,
                buttons: buttons.to_vec(),
                responder,
            },
            response,
        ))
    }

    pub(crate) fn open_paths(
        window: WindowHandle,
        options: PathPromptOptions,
    ) -> Result<(Self, PathPromptResponse), PlatformError> {
        Self::open_paths_with_owner(Some(window), options)
    }

    pub(crate) fn application_open_paths(
        options: PathPromptOptions,
    ) -> Result<(Self, PathPromptResponse), PlatformError> {
        Self::open_paths_with_owner(None, options)
    }

    fn open_paths_with_owner(
        window: Option<WindowHandle>,
        options: PathPromptOptions,
    ) -> Result<(Self, PathPromptResponse), PlatformError> {
        validate_path_prompt_options(&options)?;
        let (responder, response) = response_channel();
        Ok((
            Self::OpenPaths {
                window,
                options,
                responder,
            },
            response,
        ))
    }

    pub(crate) fn save_path(
        window: WindowHandle,
        options: SavePathOptions,
    ) -> Result<(Self, PlatformResponse<Option<PathBuf>>), PlatformError> {
        Self::save_path_with_owner(Some(window), options)
    }

    pub(crate) fn application_save_path(
        options: SavePathOptions,
    ) -> Result<(Self, PlatformResponse<Option<PathBuf>>), PlatformError> {
        Self::save_path_with_owner(None, options)
    }

    fn save_path_with_owner(
        window: Option<WindowHandle>,
        options: SavePathOptions,
    ) -> Result<(Self, PlatformResponse<Option<PathBuf>>), PlatformError> {
        validate_save_path_options(&options)?;
        let (responder, response) = response_channel();
        Ok((
            Self::SavePath {
                window,
                options,
                responder,
            },
            response,
        ))
    }

    pub(crate) fn message_box(
        window: WindowHandle,
        options: MessageBoxOptions,
    ) -> Result<(Self, MessageBoxResponseFuture), PlatformError> {
        Self::message_box_with_owner(Some(window), options)
    }

    pub(crate) fn application_message_box(
        options: MessageBoxOptions,
    ) -> Result<(Self, MessageBoxResponseFuture), PlatformError> {
        Self::message_box_with_owner(None, options)
    }

    fn message_box_with_owner(
        window: Option<WindowHandle>,
        options: MessageBoxOptions,
    ) -> Result<(Self, MessageBoxResponseFuture), PlatformError> {
        validate_message_box_options(&options)?;
        let (responder, response) = response_channel();
        Ok((
            Self::MessageBox {
                window,
                options: Box::new(options),
                responder,
            },
            response,
        ))
    }

    pub(crate) fn preview_file(
        path: impl Into<PathBuf>,
        display_name: Option<Arc<str>>,
    ) -> Result<Self, PlatformError> {
        let (path, display_name) = validated_preview_file(path, display_name)?;
        Ok(Self::PreviewFile {
            path,
            display_name,
            responder: None,
        })
    }

    pub(crate) fn preview_file_response(
        path: impl Into<PathBuf>,
        display_name: Option<Arc<str>>,
    ) -> Result<(Self, ShellResponse), PlatformError> {
        let (path, display_name) = validated_preview_file(path, display_name)?;
        let (responder, response) = response_channel();
        Ok((
            Self::PreviewFile {
                path,
                display_name,
                responder: Some(responder),
            },
            response,
        ))
    }

    pub(crate) fn close_file_preview() -> Self {
        Self::CloseFilePreview { responder: None }
    }

    pub(crate) fn close_file_preview_response() -> (Self, ShellResponse) {
        let (responder, response) = response_channel();
        (
            Self::CloseFilePreview {
                responder: Some(responder),
            },
            response,
        )
    }

    pub(crate) fn show_color_panel(initial: crate::Color, mode: ColorPanelMode) -> Self {
        Self::ShowColorPanel {
            initial,
            mode,
            responder: None,
        }
    }

    pub(crate) fn close_color_panel() -> Self {
        Self::CloseColorPanel { responder: None }
    }

    pub(crate) fn show_font_panel(font: crate::Font) -> Self {
        Self::ShowFontPanel {
            font: Box::new(font),
            responder: None,
        }
    }

    pub(crate) fn share_items(
        window: Option<WindowHandle>,
        items: &[ShareItem],
        anchor: crate::Rect,
    ) -> Result<Self, PlatformError> {
        validate_share_items(items)?;
        validate_share_anchor(anchor)?;
        Ok(Self::ShareItems {
            window,
            items: items.to_vec(),
            anchor,
            responder: None,
        })
    }

    pub(crate) fn share_items_response(
        window: Option<WindowHandle>,
        items: &[ShareItem],
        anchor: crate::Rect,
    ) -> Result<(Self, ShellResponse), PlatformError> {
        validate_share_items(items)?;
        validate_share_anchor(anchor)?;
        let (responder, response) = response_channel();
        Ok((
            Self::ShareItems {
                window,
                items: items.to_vec(),
                anchor,
                responder: Some(responder),
            },
            response,
        ))
    }

    pub(crate) fn authenticate_with_biometrics(
        reason: impl Into<Arc<str>>,
    ) -> Result<(Self, PlatformResponse<bool>), PlatformError> {
        let reason = reason.into();
        if reason.is_empty() || reason.len() > MAX_BIOMETRIC_REASON_BYTES || reason.contains('\0') {
            return Err(PlatformError::InvalidText);
        }
        let (responder, response) = response_channel();
        Ok((
            Self::AuthenticateWithBiometrics { reason, responder },
            response,
        ))
    }

    pub(crate) fn open_url(url: impl Into<Arc<str>>) -> Result<Self, PlatformError> {
        let url = url.into();
        validate_url(&url)?;
        Ok(Self::OpenUrl {
            url,
            responder: None,
        })
    }

    pub(crate) fn open_url_response(
        url: impl Into<Arc<str>>,
    ) -> Result<(Self, ShellResponse), PlatformError> {
        let url = url.into();
        validate_url(&url)?;
        let (responder, response) = response_channel();
        Ok((
            Self::OpenUrl {
                url,
                responder: Some(responder),
            },
            response,
        ))
    }

    pub(crate) fn show_system_notification(
        notification: SystemNotification,
    ) -> Result<Self, PlatformError> {
        let notification = normalize_system_notification(notification)?;
        Ok(Self::ShowSystemNotification(notification))
    }

    pub(crate) fn dismiss_system_notification(
        tag: impl Into<Arc<str>>,
    ) -> Result<Self, PlatformError> {
        let tag = tag.into();
        validate_notification_tag(&tag)?;
        Ok(Self::DismissSystemNotification(tag))
    }

    pub(crate) fn notification_permission_status() -> (Self, NotificationPermissionResponse) {
        let (responder, response) = response_channel();
        (Self::NotificationPermissionStatus { responder }, response)
    }

    pub(crate) fn request_notification_permission() -> (Self, NotificationPermissionResponse) {
        let (responder, response) = response_channel();
        (Self::RequestNotificationPermission { responder }, response)
    }

    pub(crate) fn open_path(path: impl Into<PathBuf>) -> Result<Self, PlatformError> {
        let path = path.into();
        validate_path(&path)?;
        Ok(Self::OpenPath {
            path,
            responder: None,
        })
    }

    pub(crate) fn open_path_response(
        path: impl Into<PathBuf>,
    ) -> Result<(Self, ShellResponse), PlatformError> {
        let path = path.into();
        validate_path(&path)?;
        let (responder, response) = response_channel();
        Ok((
            Self::OpenPath {
                path,
                responder: Some(responder),
            },
            response,
        ))
    }

    pub(crate) fn reveal_path(path: impl Into<PathBuf>) -> Result<Self, PlatformError> {
        let path = path.into();
        validate_path(&path)?;
        Ok(Self::RevealPath {
            path,
            responder: None,
        })
    }

    pub(crate) fn trash_path(path: impl Into<PathBuf>) -> Result<Self, PlatformError> {
        let path = path.into();
        validate_path(&path)?;
        Ok(Self::TrashPath {
            path,
            responder: None,
        })
    }

    pub(crate) fn reveal_path_response(
        path: impl Into<PathBuf>,
    ) -> Result<(Self, ShellResponse), PlatformError> {
        let path = path.into();
        validate_path(&path)?;
        let (responder, response) = response_channel();
        Ok((
            Self::RevealPath {
                path,
                responder: Some(responder),
            },
            response,
        ))
    }

    pub(crate) fn trash_path_response(
        path: impl Into<PathBuf>,
    ) -> Result<(Self, ShellResponse), PlatformError> {
        let path = path.into();
        validate_path(&path)?;
        let (responder, response) = response_channel();
        Ok((
            Self::TrashPath {
                path,
                responder: Some(responder),
            },
            response,
        ))
    }

    pub(crate) fn set_dock_badge(value: Option<Arc<str>>) -> Result<Self, PlatformError> {
        if value
            .as_deref()
            .is_some_and(|value| value.len() > MAX_DOCK_BADGE_BYTES || value.contains('\0'))
        {
            return Err(PlatformError::InvalidDockBadge);
        }
        Ok(Self::SetDockBadge(value))
    }

    pub(crate) fn set_dock_icon(icon: Option<Image>) -> Self {
        Self::SetDockIcon(icon)
    }

    pub(crate) fn set_tray_icon(options: TrayIconOptions) -> Self {
        Self::SetTrayIcon(options)
    }

    pub(crate) fn remove_tray_icon(id: u32) -> Self {
        Self::RemoveTrayIcon(id)
    }

    pub(crate) fn show_tray_menu(id: u32) -> Self {
        Self::ShowTrayMenu(id)
    }

    pub(crate) fn set_dock_menu(menu: Option<Menu>) -> Result<Self, PlatformError> {
        if let Some(menu) = &menu {
            crate::menu::validate_menus(std::slice::from_ref(menu))
                .map_err(|_| PlatformError::InvalidMenu)?;
        }
        Ok(Self::SetDockMenu(menu))
    }

    pub(crate) fn add_recent_document(path: impl Into<PathBuf>) -> Result<Self, PlatformError> {
        let path = path.into();
        validate_path(&path)?;
        Ok(Self::AddRecentDocument(path))
    }

    pub(crate) fn show_about_panel(options: AboutPanelOptions) -> Result<Self, PlatformError> {
        validate_about_panel_options(&options)?;
        Ok(Self::ShowAboutPanel(options))
    }

    pub(crate) fn get_file_icon(
        path: impl Into<PathBuf>,
        size: FileIconSize,
    ) -> Result<(Self, FileIconResponse), PlatformError> {
        let path = path.into();
        validate_path(&path)?;
        let (responder, response) = response_channel();
        Ok((
            Self::GetFileIcon {
                path,
                size,
                responder,
            },
            response,
        ))
    }

    pub(crate) fn set_user_tasks(
        tasks: Vec<UserTask>,
    ) -> Result<(Self, ShellResponse), PlatformError> {
        validate_user_tasks(&tasks)?;
        let (responder, response) = response_channel();
        Ok((Self::SetUserTasks { tasks, responder }, response))
    }

    #[cfg_attr(not(target_os = "macos"), allow(dead_code))]
    pub(crate) fn window(&self) -> Option<WindowHandle> {
        match self {
            Self::Prompt { window, .. }
            | Self::OpenPaths { window, .. }
            | Self::SavePath { window, .. }
            | Self::MessageBox { window, .. } => *window,
            Self::ShareItems { window, .. } => *window,
            Self::PreviewFile { .. }
            | Self::CloseFilePreview { .. }
            | Self::ShowColorPanel { .. }
            | Self::CloseColorPanel { .. }
            | Self::ShowFontPanel { .. }
            | Self::AuthenticateWithBiometrics { .. }
            | Self::ShowSystemNotification(_)
            | Self::DismissSystemNotification(_)
            | Self::NotificationPermissionStatus { .. }
            | Self::RequestNotificationPermission { .. }
            | Self::OpenUrl { .. }
            | Self::OpenPath { .. }
            | Self::RevealPath { .. }
            | Self::TrashPath { .. }
            | Self::SetDockBadge(_)
            | Self::SetDockIcon(_)
            | Self::SetDockMenu(_)
            | Self::SetTrayIcon(_)
            | Self::RemoveTrayIcon(_)
            | Self::ShowTrayMenu(_)
            | Self::AddRecentDocument(_)
            | Self::ClearRecentDocuments
            | Self::ShowAboutPanel(_)
            | Self::GetFileIcon { .. }
            | Self::SetUserTasks { .. }
            | Self::SetActivationPolicy { .. }
            | Self::ActivateApplication { .. }
            | Self::HideApplication
            | Self::UnhideApplication
            | Self::RequestDockAttention { .. }
            | Self::CancelDockAttention(_)
            | Self::SetDockVisible { .. }
            | Self::SetSecureKeyboardEntry(_)
            | Self::Beep
            | Self::MoveToApplicationsFolder { .. } => None,
        }
    }

    pub(crate) fn response_cancelled(&self) -> bool {
        match self {
            Self::Prompt { responder, .. } => responder.is_cancelled(),
            Self::OpenPaths { responder, .. } => responder.is_cancelled(),
            Self::SavePath { responder, .. } => responder.is_cancelled(),
            Self::OpenUrl { responder, .. }
            | Self::OpenPath { responder, .. }
            | Self::RevealPath { responder, .. }
            | Self::TrashPath { responder, .. } => responder
                .as_ref()
                .is_some_and(PlatformResponder::is_cancelled),
            Self::NotificationPermissionStatus { responder }
            | Self::RequestNotificationPermission { responder } => responder.is_cancelled(),
            Self::GetFileIcon { responder, .. } => responder.is_cancelled(),
            Self::SetUserTasks { responder, .. } => responder.is_cancelled(),
            Self::SetActivationPolicy { responder, .. }
            | Self::SetDockVisible { responder, .. } => responder.is_cancelled(),
            Self::RequestDockAttention { responder, .. } => responder.is_cancelled(),
            Self::MoveToApplicationsFolder { responder } => responder.is_cancelled(),
            Self::MessageBox { responder, .. } => responder.is_cancelled(),
            Self::PreviewFile { responder, .. }
            | Self::CloseFilePreview { responder }
            | Self::ShowColorPanel { responder, .. }
            | Self::CloseColorPanel { responder }
            | Self::ShowFontPanel { responder, .. }
            | Self::ShareItems { responder, .. } => responder
                .as_ref()
                .is_some_and(PlatformResponder::is_cancelled),
            Self::AuthenticateWithBiometrics { responder, .. } => responder.is_cancelled(),
            Self::ShowSystemNotification(_)
            | Self::DismissSystemNotification(_)
            | Self::SetDockBadge(_)
            | Self::SetDockIcon(_)
            | Self::SetDockMenu(_)
            | Self::SetTrayIcon(_)
            | Self::RemoveTrayIcon(_)
            | Self::ShowTrayMenu(_)
            | Self::AddRecentDocument(_)
            | Self::ClearRecentDocuments
            | Self::ActivateApplication { .. }
            | Self::HideApplication
            | Self::UnhideApplication
            | Self::CancelDockAttention(_)
            | Self::SetSecureKeyboardEntry(_)
            | Self::Beep
            | Self::ShowAboutPanel(_) => false,
        }
    }

    #[cfg(target_os = "macos")]
    pub(crate) fn bind_cancellation(
        &self,
        proxy: EventLoopProxy<RuntimeEvent>,
        owner: Option<WindowHandle>,
        id: PlatformDialogId,
    ) -> bool {
        match self {
            Self::Prompt { responder, .. } => responder.bind_cancellation(proxy, owner, id),
            Self::OpenPaths { responder, .. } => responder.bind_cancellation(proxy, owner, id),
            Self::SavePath { responder, .. } => responder.bind_cancellation(proxy, owner, id),
            Self::MessageBox { responder, .. } => responder.bind_cancellation(proxy, owner, id),
            Self::PreviewFile { .. }
            | Self::CloseFilePreview { .. }
            | Self::ShowColorPanel { .. }
            | Self::CloseColorPanel { .. }
            | Self::ShowFontPanel { .. }
            | Self::ShareItems { .. }
            | Self::AuthenticateWithBiometrics { .. }
            | Self::ShowSystemNotification(_)
            | Self::DismissSystemNotification(_)
            | Self::NotificationPermissionStatus { .. }
            | Self::RequestNotificationPermission { .. }
            | Self::OpenUrl { .. }
            | Self::OpenPath { .. }
            | Self::RevealPath { .. }
            | Self::TrashPath { .. }
            | Self::SetDockBadge(_)
            | Self::SetDockIcon(_)
            | Self::SetDockMenu(_)
            | Self::SetTrayIcon(_)
            | Self::RemoveTrayIcon(_)
            | Self::ShowTrayMenu(_)
            | Self::AddRecentDocument(_)
            | Self::ClearRecentDocuments
            | Self::ShowAboutPanel(_)
            | Self::GetFileIcon { .. }
            | Self::SetUserTasks { .. }
            | Self::SetActivationPolicy { .. }
            | Self::ActivateApplication { .. }
            | Self::HideApplication
            | Self::UnhideApplication
            | Self::RequestDockAttention { .. }
            | Self::CancelDockAttention(_)
            | Self::SetDockVisible { .. }
            | Self::SetSecureKeyboardEntry(_)
            | Self::Beep
            | Self::MoveToApplicationsFolder { .. } => false,
        }
    }

    pub(crate) fn complete_error(self, error: PlatformError) {
        match self {
            Self::Prompt { responder, .. } => responder.complete(Err(error)),
            Self::OpenPaths { responder, .. } => responder.complete(Err(error)),
            Self::SavePath { responder, .. } => responder.complete(Err(error)),
            Self::OpenUrl { responder, .. }
            | Self::OpenPath { responder, .. }
            | Self::RevealPath { responder, .. }
            | Self::TrashPath { responder, .. } => {
                if let Some(responder) = responder {
                    responder.complete(Err(error));
                }
            }
            Self::NotificationPermissionStatus { responder }
            | Self::RequestNotificationPermission { responder } => {
                responder.complete(Err(error));
            }
            Self::GetFileIcon { responder, .. } => responder.complete(Err(error)),
            Self::SetUserTasks { responder, .. } => responder.complete(Err(error)),
            Self::SetActivationPolicy { responder, .. }
            | Self::SetDockVisible { responder, .. } => responder.complete(Err(error)),
            Self::RequestDockAttention { responder, .. } => responder.complete(Err(error)),
            Self::MoveToApplicationsFolder { responder } => responder.complete(Err(error)),
            Self::MessageBox { responder, .. } => responder.complete(Err(error)),
            Self::PreviewFile { responder, .. }
            | Self::CloseFilePreview { responder }
            | Self::ShowColorPanel { responder, .. }
            | Self::CloseColorPanel { responder }
            | Self::ShowFontPanel { responder, .. }
            | Self::ShareItems { responder, .. } => {
                if let Some(responder) = responder {
                    responder.complete(Err(error));
                }
            }
            Self::AuthenticateWithBiometrics { responder, .. } => responder.complete(Err(error)),
            Self::ShowSystemNotification(_)
            | Self::DismissSystemNotification(_)
            | Self::SetDockBadge(_)
            | Self::SetDockIcon(_)
            | Self::SetDockMenu(_)
            | Self::SetTrayIcon(_)
            | Self::RemoveTrayIcon(_)
            | Self::ShowTrayMenu(_)
            | Self::AddRecentDocument(_)
            | Self::ClearRecentDocuments
            | Self::ActivateApplication { .. }
            | Self::HideApplication
            | Self::UnhideApplication
            | Self::CancelDockAttention(_)
            | Self::SetSecureKeyboardEntry(_)
            | Self::Beep
            | Self::ShowAboutPanel(_) => {}
        }
    }
}

impl PlatformRequest {
    pub(crate) fn set_activation_policy(policy: ActivationPolicy) -> (Self, PlatformResponse<()>) {
        let (responder, response) = response_channel();
        (Self::SetActivationPolicy { policy, responder }, response)
    }

    pub(crate) fn request_dock_attention(
        attention: DockAttention,
    ) -> (Self, PlatformResponse<DockAttentionRequest>) {
        let (responder, response) = response_channel();
        (
            Self::RequestDockAttention {
                attention,
                responder,
            },
            response,
        )
    }

    pub(crate) fn set_dock_visible(visible: bool) -> (Self, PlatformResponse<()>) {
        let (responder, response) = response_channel();
        (Self::SetDockVisible { visible, responder }, response)
    }

    pub(crate) fn move_to_applications_folder() -> (Self, PlatformResponse<bool>) {
        let (responder, response) = response_channel();
        (Self::MoveToApplicationsFolder { responder }, response)
    }
}

/// Whether this process is running from an installed application bundle.
///
/// The answer is a documented heuristic, not a security boundary:
///
/// * **macOS** — the executable path contains a `.app/Contents/MacOS/` component and is not under
///   a Cargo build directory (`target/debug`, `target/release`, or any `target/<triple>/…`).
///   `cargo run` therefore reports `false` even when it produced a bundle-shaped path.
/// * **Windows** — the executable is not under a Cargo build directory.
/// * **Linux and the BSDs** — the executable is not under a Cargo build directory and does not
///   live in the current working directory tree used by `cargo run`.
///
/// It reads `std::env::current_exe` once per call and installs no observer, cache, or timer.
pub fn is_application_packaged() -> bool {
    let Ok(path) = std::env::current_exe() else {
        return false;
    };
    if path_is_cargo_build_output(&path) {
        return false;
    }
    #[cfg(target_os = "macos")]
    {
        let components: Vec<_> = path
            .components()
            .map(|component| component.as_os_str().to_string_lossy().into_owned())
            .collect();
        components.windows(3).any(|window| {
            window[0].ends_with(".app") && window[1] == "Contents" && window[2] == "MacOS"
        })
    }
    #[cfg(not(target_os = "macos"))]
    {
        true
    }
}

fn path_is_cargo_build_output(path: &Path) -> bool {
    let mut components = path.components().peekable();
    while let Some(component) = components.next() {
        if component.as_os_str() != "target" {
            continue;
        }
        let Some(next) = components.peek() else {
            continue;
        };
        let next = next.as_os_str().to_string_lossy();
        // `target/debug`, `target/release`, and `target/<triple>/<profile>` all mark build output.
        if next == "debug" || next == "release" || next.contains('-') {
            return true;
        }
    }
    false
}

fn validate_about_panel_options(options: &AboutPanelOptions) -> Result<(), PlatformError> {
    let fields = [
        options.application_name.as_deref(),
        options.application_version.as_deref(),
        options.version.as_deref(),
        options.copyright.as_deref(),
        options.credits.as_deref(),
    ];
    if fields
        .into_iter()
        .flatten()
        .any(|value| value.len() > MAX_ABOUT_PANEL_TEXT_BYTES || value.as_bytes().contains(&0))
    {
        Err(PlatformError::InvalidAboutPanelOptions)
    } else {
        Ok(())
    }
}

fn validate_user_tasks(tasks: &[UserTask]) -> Result<(), PlatformError> {
    if tasks.len() > MAX_USER_TASKS {
        return Err(PlatformError::InvalidUserTasks);
    }
    for task in tasks {
        let required = [task.title.as_ref()];
        let optional = [
            task.arguments.as_ref(),
            task.description.as_deref().unwrap_or_default(),
        ];
        if required.into_iter().any(|value| {
            value.is_empty()
                || value.len() > MAX_USER_TASK_TEXT_BYTES
                || value.as_bytes().contains(&0)
        }) || optional
            .into_iter()
            .any(|value| value.len() > MAX_USER_TASK_TEXT_BYTES || value.as_bytes().contains(&0))
        {
            return Err(PlatformError::InvalidUserTasks);
        }
        for path in [
            task.program.as_deref(),
            task.working_directory.as_deref(),
            task.icon_path.as_deref(),
        ]
        .into_iter()
        .flatten()
        {
            validate_path(path).map_err(|_| PlatformError::InvalidUserTasks)?;
        }
    }
    Ok(())
}

fn validate_required_text(value: &str) -> Result<(), PlatformError> {
    if value.is_empty() {
        return Err(PlatformError::EmptyPromptMessage);
    }
    validate_optional_text(value)
}

fn validate_optional_text(value: &str) -> Result<(), PlatformError> {
    if value.len() > MAX_PLATFORM_TEXT_BYTES || value.contains('\0') {
        Err(PlatformError::InvalidText)
    } else {
        Ok(())
    }
}

fn validate_prompt_buttons(buttons: &[PromptButton]) -> Result<(), PlatformError> {
    if buttons.is_empty() || buttons.len() > MAX_PROMPT_BUTTONS {
        return Err(PlatformError::InvalidButtons);
    }
    if buttons.iter().any(|button| {
        let label = button.label();
        label.is_empty() || label.len() > MAX_PROMPT_BUTTON_BYTES || label.contains('\0')
    }) {
        return Err(PlatformError::InvalidButton);
    }
    Ok(())
}

fn validate_share_items(items: &[ShareItem]) -> Result<(), PlatformError> {
    if items.is_empty() || items.len() > MAX_SHARE_ITEMS {
        return Err(PlatformError::InvalidShareItems);
    }
    for item in items {
        match item {
            ShareItem::Text(value) | ShareItem::Url(value) => {
                if value.is_empty()
                    || value.len() > MAX_SHARE_ITEM_TEXT_BYTES
                    || value.contains('\0')
                {
                    return Err(PlatformError::InvalidShareItems);
                }
            }
            ShareItem::File(path) => validate_path(path)?,
            ShareItem::Image(_) => {}
        }
    }
    Ok(())
}

fn validate_share_anchor(anchor: crate::Rect) -> Result<(), PlatformError> {
    if anchor.x.is_finite()
        && anchor.y.is_finite()
        && anchor.width.is_finite()
        && anchor.height.is_finite()
        && anchor.width >= 0.0
        && anchor.height >= 0.0
    {
        Ok(())
    } else {
        Err(PlatformError::InvalidShareAnchor)
    }
}

fn validated_preview_file(
    path: impl Into<PathBuf>,
    display_name: Option<Arc<str>>,
) -> Result<(PathBuf, Option<Arc<str>>), PlatformError> {
    let path = path.into();
    validate_path(&path)?;
    if let Some(display_name) = &display_name
        && (display_name.is_empty()
            || display_name.len() > MAX_FILE_PREVIEW_NAME_BYTES
            || display_name.contains('\0'))
    {
        return Err(PlatformError::InvalidText);
    }
    Ok((path, display_name))
}

fn validate_message_box_options(options: &MessageBoxOptions) -> Result<(), PlatformError> {
    validate_required_text(&options.message)?;
    if let Some(detail) = &options.detail {
        validate_optional_text(detail)?;
    }
    let buttons = options.resolved_buttons();
    validate_prompt_buttons(&buttons)?;
    if [options.default_button, options.cancel_button]
        .into_iter()
        .flatten()
        .any(|index| index >= buttons.len())
    {
        return Err(PlatformError::InvalidButtons);
    }
    if let Some(checkbox) = &options.checkbox
        && (checkbox.label.is_empty()
            || checkbox.label.len() > MAX_MESSAGE_BOX_CHECKBOX_BYTES
            || checkbox.label.contains('\0'))
    {
        return Err(PlatformError::InvalidText);
    }
    Ok(())
}

fn validate_path_prompt_options(options: &PathPromptOptions) -> Result<(), PlatformError> {
    if !options.files && !options.directories {
        return Err(PlatformError::InvalidPathSelection);
    }
    if let Some(title) = &options.title {
        validate_optional_text(title)?;
    }
    if let Some(prompt) = &options.prompt {
        validate_optional_text(prompt)?;
    }
    if let Some(message) = &options.message {
        validate_optional_text(message)?;
    }
    if let Some(directory) = &options.directory {
        validate_path(directory)?;
    }
    if let Some(name) = &options.suggested_name {
        validate_optional_text(name)?;
    }
    validate_file_dialog_filters(&options.filters)?;
    Ok(())
}

fn validate_save_path_options(options: &SavePathOptions) -> Result<(), PlatformError> {
    validate_path(&options.directory)?;
    if let Some(title) = &options.title {
        validate_optional_text(title)?;
    }
    if let Some(name) = &options.suggested_name {
        validate_optional_text(name)?;
    }
    if let Some(prompt) = &options.prompt {
        validate_optional_text(prompt)?;
    }
    if let Some(label) = &options.name_field_label {
        validate_optional_text(label)?;
    }
    validate_file_dialog_filters(&options.filters)?;
    Ok(())
}

fn validate_file_dialog_filters(filters: &[FileDialogFilter]) -> Result<(), PlatformError> {
    if filters.len() > MAX_FILE_DIALOG_FILTERS {
        return Err(PlatformError::InvalidFileDialogFilter);
    }
    let mut extension_count = 0_usize;
    let mut total_bytes = 0_usize;
    for filter in filters {
        if filter.name.is_empty() || filter.name.contains('\0') || filter.extensions.is_empty() {
            return Err(PlatformError::InvalidFileDialogFilter);
        }
        total_bytes = total_bytes.saturating_add(filter.name.len());
        extension_count = extension_count.saturating_add(filter.extensions.len());
        for extension in &filter.extensions {
            if extension.is_empty()
                || extension.contains(['\0', '/', '\\'])
                || extension.starts_with('.')
            {
                return Err(PlatformError::InvalidFileDialogFilter);
            }
            total_bytes = total_bytes.saturating_add(extension.len());
        }
    }
    if extension_count > MAX_FILE_DIALOG_FILTER_EXTENSIONS
        || total_bytes > MAX_FILE_DIALOG_FILTER_BYTES
    {
        Err(PlatformError::InvalidFileDialogFilter)
    } else {
        Ok(())
    }
}

fn validate_path(path: &Path) -> Result<(), PlatformError> {
    let bytes = path.as_os_str().as_encoded_bytes();
    if bytes.is_empty() || bytes.len() > MAX_PLATFORM_PATH_BYTES || bytes.contains(&0) {
        Err(PlatformError::InvalidPath)
    } else {
        Ok(())
    }
}

fn validate_url(url: &str) -> Result<(), PlatformError> {
    if url.is_empty() || url.len() > MAX_PLATFORM_URL_BYTES || url.contains('\0') {
        Err(PlatformError::InvalidUrl)
    } else {
        Ok(())
    }
}

fn validate_notification_tag(tag: &str) -> Result<(), PlatformError> {
    if tag.is_empty() || tag.len() > MAX_SYSTEM_NOTIFICATION_TAG_BYTES || tag.contains('\0') {
        Err(PlatformError::InvalidNotificationTag)
    } else {
        Ok(())
    }
}

fn validate_system_notification(notification: &SystemNotification) -> Result<(), PlatformError> {
    validate_notification_tag(&notification.tag)?;
    if notification.title.is_empty()
        || notification.title.len() > MAX_SYSTEM_NOTIFICATION_TITLE_BYTES
        || notification.title.contains('\0')
        || notification.subtitle.as_deref().is_some_and(|subtitle| {
            subtitle.len() > MAX_SYSTEM_NOTIFICATION_TITLE_BYTES || subtitle.contains('\0')
        })
        || notification.body.len() > MAX_SYSTEM_NOTIFICATION_BODY_BYTES
        || notification.body.contains('\0')
    {
        return Err(PlatformError::InvalidNotificationText);
    }
    if notification.actions.len() > MAX_SYSTEM_NOTIFICATION_ACTIONS {
        return Err(PlatformError::InvalidNotificationActions);
    }
    for (index, action) in notification.actions.iter().enumerate() {
        if action.id.is_empty()
            || action.label.is_empty()
            || matches!(
                action.id.as_ref(),
                SYSTEM_NOTIFICATION_DEFAULT_ACTION_ID | SYSTEM_NOTIFICATION_DISMISS_ACTION_ID
            )
            || action.id.len() > MAX_SYSTEM_NOTIFICATION_ACTION_BYTES
            || action.label.len() > MAX_SYSTEM_NOTIFICATION_ACTION_BYTES
            || action.id.contains('\0')
            || action.label.contains('\0')
            || notification.actions[..index]
                .iter()
                .any(|previous| previous.id == action.id)
        {
            return Err(PlatformError::InvalidNotificationAction);
        }
        if let SystemNotificationActionKind::TextInput { placeholder } = &action.kind
            && placeholder.as_deref().is_some_and(|placeholder| {
                placeholder.is_empty()
                    || placeholder.len() > MAX_SYSTEM_NOTIFICATION_OPTION_BYTES
                    || placeholder.contains('\0')
            })
        {
            return Err(PlatformError::InvalidNotificationOptions);
        }
    }
    if notification.attachments.len() > MAX_SYSTEM_NOTIFICATION_ATTACHMENTS {
        return Err(PlatformError::InvalidNotificationOptions);
    }
    if let Some(icon) = notification.icon.as_deref() {
        validate_path(icon).map_err(|_| PlatformError::InvalidNotificationOptions)?;
    }
    for (index, attachment) in notification.attachments.iter().enumerate() {
        if attachment.id.is_empty()
            || attachment.id.len() > MAX_SYSTEM_NOTIFICATION_OPTION_BYTES
            || attachment.id.contains('\0')
            || notification.attachments[..index]
                .iter()
                .any(|previous| previous.id == attachment.id)
            || validate_path(&attachment.path).is_err()
        {
            return Err(PlatformError::InvalidNotificationOptions);
        }
    }
    if let SystemNotificationSound::Named(name) = &notification.sound
        && (name.is_empty()
            || name.len() > MAX_SYSTEM_NOTIFICATION_OPTION_BYTES
            || name.contains('\0'))
    {
        return Err(PlatformError::InvalidNotificationOptions);
    }
    Ok(())
}

fn normalize_system_notification(
    mut notification: SystemNotification,
) -> Result<SystemNotification, PlatformError> {
    validate_system_notification(&notification)?;
    notification.actions.shrink_to_fit();
    notification.attachments.shrink_to_fit();
    Ok(notification)
}

#[cfg(test)]
mod tests {
    use std::{
        future::Future,
        pin::pin,
        sync::{
            Arc,
            atomic::{AtomicUsize, Ordering},
        },
        task::{Context, Wake, Waker},
    };

    use super::*;

    struct WakeCounter(AtomicUsize);

    impl Wake for WakeCounter {
        fn wake(self: Arc<Self>) {
            self.0.fetch_add(1, Ordering::Relaxed);
        }
    }

    #[test]
    fn native_response_is_single_shot_and_wakes_once() {
        let (responder, response) = response_channel::<usize>();
        let counter = Arc::new(WakeCounter(AtomicUsize::new(0)));
        let waker = Waker::from(counter.clone());
        let mut context = Context::from_waker(&waker);
        let mut response = pin!(response);

        assert!(response.as_mut().poll(&mut context).is_pending());
        responder.complete(Ok(7));
        responder.complete(Ok(8));
        assert_eq!(counter.0.load(Ordering::Relaxed), 1);
        assert_eq!(response.as_mut().poll(&mut context), Poll::Ready(Ok(7)));
    }

    #[test]
    fn dropping_a_response_cancels_unstarted_native_work() {
        let (responder, response) = response_channel::<usize>();
        assert!(!responder.is_cancelled());
        drop(response);
        assert!(responder.is_cancelled());
        responder.complete(Ok(1));
        assert!(responder.state.result.borrow().is_none());
    }

    #[test]
    fn prompt_validation_happens_before_retaining_native_work() {
        let window = WindowHandle::next();
        assert!(matches!(
            PlatformRequest::prompt(window, PromptLevel::Info, "", None, &["OK".into()]),
            Err(PlatformError::EmptyPromptMessage)
        ));
        assert!(matches!(
            PlatformRequest::prompt(window, PromptLevel::Info, "Message", None, &[]),
            Err(PlatformError::InvalidButtons)
        ));
        assert!(matches!(
            PlatformRequest::prompt(
                window,
                PromptLevel::Info,
                "Message",
                None,
                &[PromptButton::ok("x".repeat(MAX_PROMPT_BUTTON_BYTES + 1))],
            ),
            Err(PlatformError::InvalidButton)
        ));

        let (request, response) = PlatformRequest::application_prompt(
            PromptLevel::Info,
            "Message",
            None,
            &[PromptButton::ok("OK")],
        )
        .unwrap();
        assert_eq!(request.window(), None);
        drop(response);
    }

    #[test]
    fn path_options_keep_native_ux_configuration_and_bounds() {
        let options = PathPromptOptions::new()
            .files(false)
            .directories(true)
            .multiple(true)
            .title("Open content")
            .prompt("Choose")
            .directory("/tmp")
            .suggested_name("notes.md")
            .filters([FileDialogFilter::new("Markdown", ["md"])])
            .shows_hidden_files(true);
        assert!(!options.files);
        assert!(options.directories);
        assert!(options.multiple);
        assert_eq!(options.title.as_deref(), Some("Open content"));
        assert_eq!(options.prompt.as_deref(), Some("Choose"));
        assert_eq!(options.directory.as_deref(), Some(Path::new("/tmp")));
        assert_eq!(options.suggested_name.as_deref(), Some("notes.md"));
        assert_eq!(options.filters[0].extensions[0].as_ref(), "md");
        assert!(options.shows_hidden_files);

        let invalid = PathPromptOptions::new().files(false);
        assert_eq!(
            PlatformRequest::open_paths(WindowHandle::next(), invalid).unwrap_err(),
            PlatformError::InvalidPathSelection
        );

        let invalid_filter =
            PathPromptOptions::new().filters([FileDialogFilter::new("Images", [".png"])]);
        assert_eq!(
            PlatformRequest::open_paths(WindowHandle::next(), invalid_filter).unwrap_err(),
            PlatformError::InvalidFileDialogFilter
        );

        let (request, response) =
            PlatformRequest::application_open_paths(PathPromptOptions::new()).unwrap();
        assert_eq!(request.window(), None);
        drop(response);

        let (request, response) =
            PlatformRequest::application_save_path(SavePathOptions::new("/tmp")).unwrap();
        assert_eq!(request.window(), None);
        drop(response);
    }

    #[test]
    fn shell_arguments_are_validated_before_queueing() {
        assert_eq!(
            PlatformRequest::open_url("").unwrap_err(),
            PlatformError::InvalidUrl
        );
        assert_eq!(
            PlatformRequest::open_path(PathBuf::new()).unwrap_err(),
            PlatformError::InvalidPath
        );
        assert!(PlatformRequest::reveal_path("/tmp").is_ok());
        assert!(PlatformRequest::trash_path("/tmp/discarded-item").is_ok());
    }

    #[test]
    fn desktop_shell_declarations_are_bounded_before_queueing() {
        assert_eq!(
            PlatformRequest::set_dock_badge(Some(Arc::from("x".repeat(MAX_DOCK_BADGE_BYTES + 1))))
                .unwrap_err(),
            PlatformError::InvalidDockBadge
        );
        assert_eq!(
            PlatformRequest::show_about_panel(
                AboutPanelOptions::new().credits("x".repeat(MAX_ABOUT_PANEL_TEXT_BYTES + 1))
            )
            .unwrap_err(),
            PlatformError::InvalidAboutPanelOptions
        );
        let tasks = (0..=MAX_USER_TASKS)
            .map(|index| UserTask::new(format!("Task {index}"), "--task"))
            .collect();
        assert_eq!(
            PlatformRequest::set_user_tasks(tasks).unwrap_err(),
            PlatformError::InvalidUserTasks
        );
        assert_eq!(
            PlatformRequest::set_user_tasks(vec![UserTask::new("", "--task")]).unwrap_err(),
            PlatformError::InvalidUserTasks
        );

        let (request, response) = PlatformRequest::get_file_icon("/tmp", FileIconSize::Large)
            .expect("a bounded path should produce an asynchronous lookup");
        assert!(!request.response_cancelled());
        drop(response);
        assert!(request.response_cancelled());
    }

    #[test]
    fn system_notifications_are_validated_before_retention() {
        let valid = SystemNotification::new("build", "Build finished", "All checks passed")
            .action(SystemNotificationAction::new("open", "Open"));
        assert!(PlatformRequest::show_system_notification(valid).is_ok());

        assert_eq!(
            PlatformRequest::show_system_notification(SystemNotification::new("", "Title", "Body"))
                .unwrap_err(),
            PlatformError::InvalidNotificationTag
        );
        assert_eq!(
            PlatformRequest::show_system_notification(SystemNotification::new("tag", "", "Body"))
                .unwrap_err(),
            PlatformError::InvalidNotificationText
        );
        let duplicate = SystemNotification::new("tag", "Title", "Body")
            .action(SystemNotificationAction::new("open", "Open"))
            .action(SystemNotificationAction::new("open", "Open again"));
        assert_eq!(
            PlatformRequest::show_system_notification(duplicate).unwrap_err(),
            PlatformError::InvalidNotificationAction
        );
        for reserved in [
            SYSTEM_NOTIFICATION_DEFAULT_ACTION_ID,
            SYSTEM_NOTIFICATION_DISMISS_ACTION_ID,
        ] {
            let notification = SystemNotification::new("tag", "Title", "Body")
                .action(SystemNotificationAction::new(reserved, "Reserved"));
            assert_eq!(
                PlatformRequest::show_system_notification(notification).unwrap_err(),
                PlatformError::InvalidNotificationAction
            );
        }
        let mut oversized_allocation = Vec::with_capacity(65_536);
        oversized_allocation.push(SystemNotificationAction::new("open", "Open"));
        let mut notification = SystemNotification::new("tag", "Title", "Body");
        notification.actions = oversized_allocation;
        let normalized = PlatformRequest::show_system_notification(notification).unwrap();
        let PlatformRequest::ShowSystemNotification(normalized) = normalized else {
            unreachable!("the constructor returns the matching request variant");
        };
        assert_eq!(normalized.actions.len(), 1);
        assert!(normalized.actions.capacity() <= MAX_SYSTEM_NOTIFICATION_ACTIONS);
        assert_eq!(
            PlatformRequest::dismiss_system_notification("").unwrap_err(),
            PlatformError::InvalidNotificationTag
        );
    }

    #[test]
    fn message_box_options_are_validated_before_the_request_is_retained() {
        let handle = WindowHandle::next();
        assert_eq!(
            PlatformRequest::message_box(handle, MessageBoxOptions::new(""))
                .err()
                .unwrap(),
            PlatformError::EmptyPromptMessage
        );
        assert_eq!(
            PlatformRequest::message_box(handle, MessageBoxOptions::new("Save?").default_button(1))
                .err()
                .unwrap(),
            PlatformError::InvalidButtons,
            "an implicit single OK button has no index 1"
        );
        assert_eq!(
            PlatformRequest::message_box(
                handle,
                MessageBoxOptions::new("Save?")
                    .buttons([PromptButton::ok("Save"), PromptButton::cancel("Cancel")])
                    .cancel_button(2)
            )
            .err()
            .unwrap(),
            PlatformError::InvalidButtons
        );
        assert_eq!(
            PlatformRequest::message_box(
                handle,
                MessageBoxOptions::new("Save?").checkbox("", true)
            )
            .err()
            .unwrap(),
            PlatformError::InvalidText
        );
        assert_eq!(
            PlatformRequest::message_box(
                handle,
                MessageBoxOptions::new("Save?")
                    .checkbox("x".repeat(MAX_MESSAGE_BOX_CHECKBOX_BYTES + 1), false)
            )
            .err()
            .unwrap(),
            PlatformError::InvalidText
        );
        assert_eq!(
            PlatformRequest::message_box(
                handle,
                MessageBoxOptions::new("Save?").detail("a\0b".to_owned())
            )
            .err()
            .unwrap(),
            PlatformError::InvalidText
        );
    }

    #[test]
    fn message_box_requests_record_every_declared_option() {
        let handle = WindowHandle::next();
        let options = MessageBoxOptions::new("Discard the draft?")
            .level(PromptLevel::Warning)
            .detail("This cannot be undone.")
            .buttons([
                PromptButton::new("Discard"),
                PromptButton::new("Keep"),
                PromptButton::cancel("Cancel"),
            ])
            .default_button(1)
            .cancel_button(2)
            .checkbox("Do not ask again", true);
        let (request, _response) = PlatformRequest::message_box(handle, options).unwrap();
        assert_eq!(request.window(), Some(handle));
        let PlatformRequest::MessageBox { options, .. } = &request else {
            unreachable!("the constructor returns the matching request variant");
        };
        assert_eq!(options.level, Some(PromptLevel::Warning));
        assert_eq!(options.detail.as_deref(), Some("This cannot be undone."));
        assert_eq!(options.resolved_buttons().len(), 3);
        assert_eq!(options.default_button, Some(1));
        assert_eq!(options.cancel_button, Some(2));
        assert_eq!(
            options
                .checkbox
                .as_ref()
                .map(|checkbox| (checkbox.label.as_ref().to_owned(), checkbox.checked)),
            Some(("Do not ask again".to_owned(), true))
        );

        // An application-modal box carries no owning window.
        let (request, _response) =
            PlatformRequest::application_message_box(MessageBoxOptions::new("Ready")).unwrap();
        assert_eq!(request.window(), None);
        assert!(!request.response_cancelled());
    }

    #[test]
    fn message_box_responses_travel_through_the_platform_queue() {
        let (request, response) =
            PlatformRequest::application_message_box(MessageBoxOptions::new("Ready")).unwrap();
        let PlatformRequest::MessageBox { responder, .. } = request else {
            unreachable!("the constructor returns the matching request variant");
        };
        let waker = Waker::from(Arc::new(WakeCounter(AtomicUsize::new(0))));
        let mut context = Context::from_waker(&waker);
        let mut response = pin!(response);
        assert!(response.as_mut().poll(&mut context).is_pending());
        responder.complete(Ok(MessageBoxResponse {
            button: 2,
            checkbox_checked: true,
        }));
        assert_eq!(
            response.as_mut().poll(&mut context),
            Poll::Ready(Ok(MessageBoxResponse {
                button: 2,
                checkbox_checked: true,
            }))
        );
    }

    #[test]
    fn file_preview_requests_bound_their_path_and_display_name() {
        assert_eq!(
            PlatformRequest::preview_file("", None).unwrap_err(),
            PlatformError::InvalidPath
        );
        assert_eq!(
            PlatformRequest::preview_file("/tmp/report.pdf", Some(Arc::from(""))).unwrap_err(),
            PlatformError::InvalidText
        );
        assert_eq!(
            PlatformRequest::preview_file(
                "/tmp/report.pdf",
                Some(Arc::from("x".repeat(MAX_FILE_PREVIEW_NAME_BYTES + 1)))
            )
            .unwrap_err(),
            PlatformError::InvalidText
        );
        let request =
            PlatformRequest::preview_file("/tmp/report.pdf", Some(Arc::from("Report"))).unwrap();
        assert_eq!(request.window(), None);
        assert!(!request.response_cancelled());
        let PlatformRequest::PreviewFile {
            path, display_name, ..
        } = &request
        else {
            unreachable!("the constructor returns the matching request variant");
        };
        assert_eq!(path, Path::new("/tmp/report.pdf"));
        assert_eq!(display_name.as_deref(), Some("Report"));

        let (request, _response) = PlatformRequest::close_file_preview_response();
        assert!(!request.response_cancelled());
    }

    #[test]
    fn file_dialog_options_validate_their_new_fields() {
        let options = PathPromptOptions::new()
            .can_create_directories(false)
            .resolves_aliases(true)
            .treats_file_packages_as_directories(true)
            .message("Choose a workspace");
        assert!(!options.can_create_directories);
        assert!(options.resolves_aliases);
        assert!(options.treats_file_packages_as_directories);
        let handle = WindowHandle::next();
        assert!(PlatformRequest::open_paths(handle, options.clone()).is_ok());
        assert_eq!(
            PlatformRequest::open_paths(handle, options.message("a\0b".to_owned()))
                .err()
                .unwrap(),
            PlatformError::InvalidText
        );

        let save = SavePathOptions::new("/tmp")
            .name_field_label("Export as:")
            .shows_tag_field(false);
        assert_eq!(save.name_field_label.as_deref(), Some("Export as:"));
        assert!(!save.shows_tag_field);
        assert!(PlatformRequest::save_path(handle, save.clone()).is_ok());
        assert_eq!(
            PlatformRequest::save_path(handle, save.name_field_label("a\0b".to_owned()))
                .err()
                .unwrap(),
            PlatformError::InvalidText
        );
        assert!(SavePathOptions::new("/tmp").shows_tag_field);
        assert!(PathPromptOptions::new().can_create_directories);
    }

    #[test]
    fn share_items_are_bounded_before_the_request_is_retained() {
        let handle = WindowHandle::next();
        let anchor = crate::Rect::new(0.0, 0.0, 10.0, 10.0);
        assert_eq!(
            PlatformRequest::share_items(Some(handle), &[], anchor).unwrap_err(),
            PlatformError::InvalidShareItems
        );
        let too_many = vec![ShareItem::Text(Arc::from("x")); MAX_SHARE_ITEMS + 1];
        assert_eq!(
            PlatformRequest::share_items(Some(handle), &too_many, anchor).unwrap_err(),
            PlatformError::InvalidShareItems
        );
        assert_eq!(
            PlatformRequest::share_items(Some(handle), &[ShareItem::Url(Arc::from(""))], anchor)
                .unwrap_err(),
            PlatformError::InvalidShareItems
        );
        assert_eq!(
            PlatformRequest::share_items(
                Some(handle),
                &[ShareItem::Text(Arc::from(
                    "x".repeat(MAX_SHARE_ITEM_TEXT_BYTES + 1)
                ))],
                anchor
            )
            .unwrap_err(),
            PlatformError::InvalidShareItems
        );
        assert_eq!(
            PlatformRequest::share_items(Some(handle), &[ShareItem::File(PathBuf::new())], anchor)
                .unwrap_err(),
            PlatformError::InvalidPath
        );
        assert_eq!(
            PlatformRequest::share_items(
                Some(handle),
                &[ShareItem::Text(Arc::from("Report"))],
                crate::Rect::new(f32::NAN, 0.0, 1.0, 1.0)
            )
            .unwrap_err(),
            PlatformError::InvalidShareAnchor
        );

        let request = PlatformRequest::share_items(
            Some(handle),
            &[
                ShareItem::Text(Arc::from("Report")),
                ShareItem::File(PathBuf::from("/tmp/report.pdf")),
            ],
            anchor,
        )
        .unwrap();
        assert_eq!(request.window(), Some(handle));
        let PlatformRequest::ShareItems { items, .. } = &request else {
            unreachable!("the constructor returns the matching request variant");
        };
        assert_eq!(items.len(), 2);
    }

    #[test]
    fn native_panel_requests_carry_their_declarations_and_bounds() {
        let request = PlatformRequest::show_color_panel(
            crate::Color::rgb8(10, 20, 30),
            ColorPanelMode::OnClose,
        );
        assert_eq!(request.window(), None);
        let PlatformRequest::ShowColorPanel { mode, initial, .. } = &request else {
            unreachable!("the constructor returns the matching request variant");
        };
        assert_eq!(*mode, ColorPanelMode::OnClose);
        assert_eq!(*initial, crate::Color::rgb8(10, 20, 30));
        assert_eq!(ColorPanelMode::default(), ColorPanelMode::Continuous);
        assert!(matches!(
            PlatformRequest::close_color_panel(),
            PlatformRequest::CloseColorPanel { .. }
        ));

        let request = PlatformRequest::show_font_panel(crate::Font::default().bold());
        let PlatformRequest::ShowFontPanel { font, .. } = &request else {
            unreachable!("the constructor returns the matching request variant");
        };
        assert_eq!(font.weight, crate::FontWeight::BOLD);

        assert_eq!(
            PlatformRequest::authenticate_with_biometrics("").unwrap_err(),
            PlatformError::InvalidText
        );
        assert_eq!(
            PlatformRequest::authenticate_with_biometrics(
                "x".repeat(MAX_BIOMETRIC_REASON_BYTES + 1)
            )
            .unwrap_err(),
            PlatformError::InvalidText
        );
        let (request, response) =
            PlatformRequest::authenticate_with_biometrics("Unlock the vault").unwrap();
        assert!(!request.response_cancelled());
        let PlatformRequest::AuthenticateWithBiometrics { reason, responder } = request else {
            unreachable!("the constructor returns the matching request variant");
        };
        assert_eq!(reason.as_ref(), "Unlock the vault");
        let waker = Waker::from(Arc::new(WakeCounter(AtomicUsize::new(0))));
        let mut context = Context::from_waker(&waker);
        let mut response = pin!(response);
        assert!(response.as_mut().poll(&mut context).is_pending());
        responder.complete(Ok(true));
        assert_eq!(response.as_mut().poll(&mut context), Poll::Ready(Ok(true)));
    }

    #[test]
    fn native_open_urls_keep_a_shared_bounded_collection() {
        let urls = OpenUrls::from_bounded(vec![
            Arc::from("quickgui://open/7"),
            Arc::from("file:///tmp/example.txt"),
        ]);
        let clone = urls.clone();
        assert_eq!(urls.len(), 2);
        assert_eq!(
            clone.iter().collect::<Vec<_>>(),
            ["quickgui://open/7", "file:///tmp/example.txt"]
        );
        assert!(Arc::ptr_eq(&urls.urls, &clone.urls));
    }
}