hyprforge-tray 0.1.3

A StatusNotifierItem tray library, plus the hyprforge-trayd daemon that puts network (Wi-Fi and Ethernet), Bluetooth, keep-awake, night-light, battery/power-profile and display-layout icons in whatever bar is running, drawing its own right-click menu through hyprforge-traymenu.
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
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
//! `hyprforge-trayd`: six `org.kde.StatusNotifierItem`s — network (Wi-Fi
//! and Ethernet in one icon) and Bluetooth, joined to `hyprforge-network`
//! and `hyprforge-bluetooth`,
//! keep-awake (`hyprforge-power`, over `systemd-logind`), night light
//! (`hyprforge-ecosystem::sunset_control`, over `hyprctl hyprsunset`),
//! battery and power profile (`hyprforge-power` again, over UPower and
//! `power-profiles-daemon`), and display layouts (`hyprforge-displayd`,
//! through `hyprforge_core::displayd_proxy`).
//!
//! Everything that decides *which icon* a state deserves lives in the
//! pure functions below (`network_item`, `bluetooth_item`, `keep_awake_item`,
//! `night_light_item`, `power_item`, `displays_item`) — no D-Bus, no
//! I/O — so the
//! interesting question is unit-tested without a bus, a bar, a radio, or
//! hyprsunset, the same split `crate::item` documents. Everything else
//! here is plumbing: polling the backends, keeping one `TrayIcon` per
//! item in sync, and forwarding clicks to `hyprforge-settings`.
//!
//! Whether an icon is shown at all is `hyprforge_tray::prefs`, re-read
//! every poll tick (`refresh_prefs`) so a toggle in Settings takes effect
//! within one tick without restarting this daemon. Switching an icon off
//! **drops its `TrayIcon`** — see `sync_icon` and [`IconAction`] — rather
//! than setting it `Status::Passive`, because not every tray host hides a
//! Passive item, and dropping is the only thing that reliably releases
//! the bus name a host is showing.
//!
//! Every menu (`network_menu`, `bluetooth_menu`, `keep_awake_menu`,
//! `night_light_menu`, `power_menu`, `displays_menu`) is built through
//! `hyprforge_tray::menu::radio_menu`, which is the one shape all of them
//! follow: a toggle (or, when there is
//! nothing to toggle, a single disabled row saying why), the content that
//! toggle governs, and a settings row — never omitted, unavailable states
//! included. See `radio_menu`'s own doc for the reasoning.
//!
//! # Where every menu's settings row goes
//!
//! All of them land somewhere real. `--screen network` and
//! `--screen bluetooth` open those screens directly; `--screen power`
//! opens the Power screen — for the power icon, and for keep-awake,
//! which lives there now too (it moved off the
//! Desktop screen's `Idle` tab); `--screen night-light` opens Settings'
//! Night light page (`hyprforge-settings`'s `screen_from_cli`) — which is
//! also what `TrayItem::activate_screen`
//! already sends a left click on either icon to. Neither destination
//! existed when this daemon's keep-awake and night-light icons were
//! first added; both do now, so a settings row that used to have nowhere
//! honest to send anyone finally does, and the two icons stop being the
//! only ones without a way out of their own menu.

use hyprforge_bluetooth::backend::{for_display as bt_for_display, BluetoothBackend};
use hyprforge_bluetooth::{Address, AdapterState, BlueZBackend, Device};
use hyprforge_bluetooth::Status as BtStatus;
use futures_util::StreamExt;
use hyprforge_core::displayd_proxy::DisplaydProxy;
use hyprforge_ecosystem::sunset_control::{Hyprsunset, SunsetBackend, SunsetControlError};
use hyprforge_network::backend::{for_display as net_for_display, NetworkBackend, SavedNetwork};
use hyprforge_network::{
    AccessPoint, NetworkError, NetworkManagerBackend, RadioState, Ssid, WiredState, WiredStatus,
};
use hyprforge_network::Status as NetStatus;
use hyprforge_power::{
    BatteryBackend, BatteryInfo, BatteryState, DetachedBackend, InhibitBackend, InhibitorInfo,
    PowerProfile, PowerProfilesBackend, PowerProfilesDaemonBackend, UPowerBackend, WhatSet,
};
use hyprforge_tray::menu::{radio_menu, Menu, MenuItem};
use hyprforge_tray::prefs::{self, Prefs};
use hyprforge_tray::{watcher_present, Category, TrayIcon, TrayItem};
use hyprforge_tray::Status as TrayStatus;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;

/// Where one icon's registration lives between ticks: `None` when the
/// icon is switched off (or not yet registered), `Some` while it's up.
/// Shared between `poll_loop`, which is the only writer, and
/// `reannounce_loop`, which only ever reads it to call `announce` again.
type IconSlot = Arc<Mutex<Option<Arc<TrayIcon>>>>;

/// One managed icon's registration slot, together with the tray-icon
/// index it was registered under.
///
/// `poll_loop`, `reannounce_loop` and `main` used to thread
/// `network_slot` and `bluetooth_slot` as separate named variables. Two
/// was already about the limit of what reads cleanly that way; adding
/// `keep_awake` and `night_light` on top as two more named parameters
/// would have meant every one of those three functions growing a pair of
/// arguments it just forwards along, and the `for slot in [&a, &b]` in
/// `reannounce_loop` growing into a four-item literal that says nothing
/// about *why* those four belong together. A `Vec` of these instead means
/// "every icon" is a single collection everywhere that question is asked,
/// and a fifth icon later is one more entry in the vec `main` builds, not
/// one more parameter threaded through three functions.
#[derive(Clone)]
struct ManagedIcon {
    slot: IconSlot,
    index: u32,
}

impl ManagedIcon {
    fn new(icon: Arc<TrayIcon>, index: u32) -> Self {
        ManagedIcon { slot: Arc::new(Mutex::new(Some(icon))), index }
    }
}

/// How often both backends are re-polled. Ten seconds is what the
/// Settings app's Network and Bluetooth screens already poll at while
/// open — matching it means the tray icon and an open Settings window
/// never visibly disagree for long.
const POLL_INTERVAL: Duration = Duration::from_secs(10);

/// How often a failed `TrayIcon::register` or a not-yet-present watcher
/// is retried. Short enough that a bar starting up is followed quickly,
/// long enough not to spin.
const RETRY_INTERVAL: Duration = Duration::from_secs(3);

/// How often the watcher's presence is polled to notice a bar restart.
///
/// A `NameOwnerChanged` subscription would need matching on
/// `org.kde.StatusNotifierWatcher` specifically and filtering out the
/// "still has an owner, just a different one during a graceful restart"
/// case — `watcher_present` (which `main` already uses to wait at
/// startup) answers the only question that matters here just as well,
/// and a tray host is not a hot path, so polling it is not a real cost.
const WATCHER_POLL_INTERVAL: Duration = Duration::from_secs(5);

/// How often `tray.toml` is *stat*ed — not how often it is parsed.
///
/// Toggling "Show in tray" used to take ten or twelve seconds to show on
/// screen, because the preference file was only re-read on the ordinary
/// poll tick. A setting that takes that long reads as broken, and the
/// user tries again.
///
/// A stat is one syscall on a file that is always in the page cache;
/// twice a second costs nothing measurable, and only a changed stamp
/// causes an actual read. An inotify watch would be tidier still, but it
/// is a dependency and a set of edge cases (the file is replaced by
/// rename, so the watch has to follow the directory) for a saving that
/// does not exist at this size.
const PREFS_WATCH_INTERVAL: Duration = Duration::from_millis(500);

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Keep awake's holder: this binary run only to hold the inhibit — see
    // `keep_awake_backend`. Before anything else, and before registering
    // a single tray icon.
    if std::env::args().nth(1).as_deref() == Some(hyprforge_power::keep_awake::HOLDER_ARG) {
        hyprforge_power::keep_awake::hold().await?;
        return Ok(());
    }
    // Default to `info` rather than whatever `from_default_env` alone
    // gives with RUST_LOG unset, which is nothing — a daemon with no GUI
    // that prints nothing on start is indistinguishable from a hung one.
    // RUST_LOG still overrides this when set.
    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
        )
        .init();

    // One session-bus connection, kept for the lifetime of the process,
    // used only to ask the watcher whether it's there — `TrayIcon::register`
    // opens its own connection per item, for the reason on `TrayIcon`.
    let probe = zbus::Connection::session().await?;

    if !watcher_present(&probe).await {
        // No bar is an ordinary state (`hl.exec_cmd` starts this before
        // much of the session exists), not a failure — wait rather than
        // exit, or the daemon that starts before the bar is the one that
        // never shows an icon.
        tracing::info!("no tray host yet; waiting for one to start");
        loop {
            tokio::time::sleep(WATCHER_POLL_INTERVAL).await;
            if watcher_present(&probe).await {
                break;
            }
        }
    }
    tracing::info!("tray host found; registering icons");

    let (clicks_tx, clicks_rx) = tokio::sync::mpsc::unbounded_channel();
    // Separate from `clicks_tx`: a click on the icon itself says which
    // settings screen to open, a click inside its menu says which
    // operation to perform. `sni.rs` keeps the two apart for the same
    // reason — see `TrayIcon::register_with_menu`'s doc comment.
    let (menu_clicks_tx, menu_clicks_rx) = tokio::sync::mpsc::unbounded_channel();
    // Lets a menu open pull the next poll forward, so the scan it kicks
    // off is actually reflected before the ten-second tick would.
    let (refresh_tx, refresh_rx) = tokio::sync::mpsc::unbounded_channel();

    // Registered unavailable-looking to start: the first real poll is at
    // most one `POLL_INTERVAL` away, and an icon that starts blank until
    // then would look identical to a daemon that never started. Both
    // start registered regardless of `tray.toml` — `poll_loop`'s first
    // tick (which fires immediately) reads preferences and drops
    // whichever one the user has already switched off, so there is only
    // one place that ever decides "is this icon supposed to be up".
    let network_icon = register_with_retry(
        network_item(None, None, None, true),
        network_menu(None, None, true, &[], &[]),
        0,
        clicks_tx.clone(),
        menu_clicks_tx.clone(),
    )
    .await;
    let bluetooth_icon = register_with_retry(
        bluetooth_item(None, None, true),
        bluetooth_menu(None, true, &[]),
        1,
        clicks_tx.clone(),
        menu_clicks_tx.clone(),
    )
    .await;
    let keep_awake_icon = register_with_retry(
        keep_awake_item(None, &[], true),
        keep_awake_menu(None, &[], true),
        2,
        clicks_tx.clone(),
        menu_clicks_tx.clone(),
    )
    .await;
    let night_light_icon = register_with_retry(
        night_light_item(&NightLightState::CouldNotCheck),
        night_light_menu(&NightLightState::CouldNotCheck),
        3,
        clicks_tx.clone(),
        menu_clicks_tx.clone(),
    )
    .await;

    let power_icon = register_with_retry(
        power_item(&BatteryReading::Unavailable, &ProfileReading::Unavailable),
        power_menu(&BatteryReading::Unavailable, &ProfileReading::Unavailable),
        4,
        clicks_tx.clone(),
        menu_clicks_tx.clone(),
    )
    .await;

    let displays_icon = register_with_retry(
        displays_item(&DisplaysReading::Unavailable, false),
        displays_menu(&DisplaysReading::Unavailable, false),
        5,
        clicks_tx.clone(),
        menu_clicks_tx.clone(),
    )
    .await;

    let icons = vec![
        ManagedIcon::new(network_icon, 0),
        ManagedIcon::new(bluetooth_icon, 1),
        ManagedIcon::new(keep_awake_icon, 2),
        ManagedIcon::new(night_light_icon, 3),
        ManagedIcon::new(power_icon, 4),
        ManagedIcon::new(displays_icon, 5),
    ];

    // This daemon's own memory of whether night light is on — see
    // `NightLightBelief`'s doc comment for why hyprsunset itself cannot
    // answer that question. Shared between `poll_loop`, which reads it to
    // render the icon, and `handle_menu_clicks`, the only writer.
    let night_light_belief: NightLightBelief = Arc::new(std::sync::Mutex::new(true));
    let revert_deadline: RevertDeadline = Arc::new(std::sync::Mutex::new(None));

    let poll_task = tokio::spawn(poll_loop(
        icons.clone(),
        night_light_belief.clone(),
        revert_deadline.clone(),
        clicks_tx.clone(),
        menu_clicks_tx.clone(),
        refresh_rx,
    ));
    let click_task = tokio::spawn(handle_clicks(clicks_rx));
    let menu_click_task = tokio::spawn(handle_menu_clicks(
        menu_clicks_rx,
        refresh_tx.clone(),
        night_light_belief,
        revert_deadline.clone(),
    ));
    let revert_task = tokio::spawn(watch_display_reverts(revert_deadline, refresh_tx.clone()));
    let prefs_watch_task = tokio::spawn(prefs_watch_loop(refresh_tx));
    let reannounce_task = tokio::spawn(reannounce_loop(probe, icons));

    // None of these loops return under normal operation. If one panics,
    // that is worth knowing about rather than leaving the others running
    // silently short-handed — a dead prefs watcher, in particular, looks
    // exactly like the ten-second lag it was written to remove.
    tokio::select! {
        r = poll_task => log_task_exit("poll", r),
        r = click_task => log_task_exit("clicks", r),
        r = menu_click_task => log_task_exit("menu-clicks", r),
        r = reannounce_task => log_task_exit("reannounce", r),
        r = prefs_watch_task => log_task_exit("prefs-watch", r),
        r = revert_task => log_task_exit("display-reverts", r),
    }

    Ok(())
}

fn log_task_exit(name: &str, result: Result<(), tokio::task::JoinError>) {
    match result {
        Ok(()) => tracing::warn!(task = name, "background task exited unexpectedly"),
        Err(e) => tracing::error!(task = name, error = %e, "background task panicked"),
    }
}

/// Registers one tray icon, retrying indefinitely rather than giving up.
///
/// `main` already waited for a tray host before calling this, but
/// registration can still fail — a host that goes away between the
/// watcher check and `register_status_notifier_item`, for instance — and
/// exiting the whole daemon over that would take *both* icons down for
/// one host hiccup, which defeats the reason there are two independent
/// icons in the first place.
async fn register_with_retry(
    item: TrayItem,
    menu: Menu,
    index: u32,
    clicks: tokio::sync::mpsc::UnboundedSender<String>,
    menu_clicks: tokio::sync::mpsc::UnboundedSender<String>,
) -> Arc<TrayIcon> {
    loop {
        match TrayIcon::register_with_menu(
            item.clone(),
            menu.clone(),
            index,
            clicks.clone(),
            menu_clicks.clone(),
        )
        .await
        {
            Ok(icon) => return Arc::new(icon),
            Err(e) => {
                tracing::warn!(error = %e, icon = %item.id, "failed to register tray icon; retrying");
                tokio::time::sleep(RETRY_INTERVAL).await;
            }
        }
    }
}

/// Holds a backend connection once one succeeds, and keeps retrying on
/// every tick until it does.
///
/// Caching a *failed* connection attempt would repeat exactly the
/// mistake documented on `LazyNetworkManagerBackend` in
/// `hyprforge-settings`: a `NetworkError::Unavailable` tells the user how
/// to fix it (`systemctl start NetworkManager`), and a daemon that stops
/// checking after the first failure means following that instruction and
/// watching the tray icon say the same thing forever.
struct Reconnecting<B> {
    backend: Option<Arc<B>>,
}

impl<B> Reconnecting<B> {
    fn new() -> Self {
        Reconnecting { backend: None }
    }

    /// Returns the cached backend, or runs `connect` again. Only
    /// `Ok` is ever cached, which is the whole property this type
    /// exists to guarantee — see the struct doc.
    async fn get_or_connect<F, Fut, E>(&mut self, connect: F) -> Result<Arc<B>, E>
    where
        F: FnOnce() -> Fut,
        Fut: std::future::Future<Output = Result<B, E>>,
    {
        if let Some(backend) = &self.backend {
            return Ok(backend.clone());
        }
        let backend = Arc::new(connect().await?);
        self.backend = Some(backend.clone());
        Ok(backend)
    }
}

async fn network_backend(
    state: &mut Reconnecting<NetworkManagerBackend>,
) -> Option<Arc<NetworkManagerBackend>> {
    match state.get_or_connect(NetworkManagerBackend::connect).await {
        Ok(backend) => Some(backend),
        Err(e) => {
            tracing::warn!(error = %e, "could not connect to NetworkManager; will retry");
            None
        }
    }
}

async fn bluetooth_backend(state: &mut Reconnecting<BlueZBackend>) -> Option<Arc<BlueZBackend>> {
    match state.get_or_connect(BlueZBackend::connect).await {
        Ok(backend) => Some(backend),
        Err(e) => {
            tracing::warn!(error = %e, "could not connect to BlueZ; will retry");
            None
        }
    }
}

/// Keep awake through a holder process of its own — see
/// `hyprforge_power::keep_awake` — so it outlives this daemon restarting,
/// and so the icon, this daemon's menu and the Settings app all read the
/// same answer from logind rather than each from a descriptor of its own.
/// (They used to disagree: the menu took the lock through one backend and
/// the icon asked another, which had never taken anything.)
async fn keep_awake_backend(state: &mut Reconnecting<DetachedBackend>) -> Option<Arc<DetachedBackend>> {
    match state.get_or_connect(|| async { DetachedBackend::this_binary() }).await {
        Ok(backend) => Some(backend),
        Err(e) => {
            tracing::warn!(error = %e, "could not find this program to run keep awake's holder; will retry");
            None
        }
    }
}

async fn upower_backend(state: &mut Reconnecting<UPowerBackend>) -> Option<Arc<UPowerBackend>> {
    match state.get_or_connect(UPowerBackend::connect).await {
        Ok(backend) => Some(backend),
        Err(e) => {
            tracing::warn!(error = %e, "could not connect to UPower; will retry");
            None
        }
    }
}

async fn profiles_backend(
    state: &mut Reconnecting<PowerProfilesDaemonBackend>,
) -> Option<Arc<PowerProfilesDaemonBackend>> {
    match state.get_or_connect(PowerProfilesDaemonBackend::connect).await {
        Ok(backend) => Some(backend),
        Err(e) => {
            tracing::warn!(error = %e, "could not connect to power-profiles-daemon; will retry");
            None
        }
    }
}

/// This daemon's own belief about whether night light is on, shared
/// between the loop that samples it and the loop that acts on menu
/// clicks.
///
/// hyprsunset has **no request that reports whether identity mode is
/// active** — `hyprforge_ecosystem::sunset_control`'s module doc explains
/// why in full — so unlike every other piece of state this daemon shows,
/// "is night light on" is never read back from the thing it describes.
/// It is only ever this daemon's memory of the last command *it* sent.
/// Starts `true`: a restart forgets any earlier "off" and the least
/// surprising default is to assume whatever a schedule or a previous
/// session already had running is still running, rather than claim off
/// with no basis for it either.
type NightLightBelief = Arc<std::sync::Mutex<bool>>;

/// One tick's worth of the network icon.
///
/// The connection, once opened, is a handle to the *session bus name*
/// `org.freedesktop.NetworkManager` — not a socket to the daemon itself —
/// so it is kept even when a call against it fails; only the call result
/// is reported as unavailable that tick. A dead bus connection (the
/// exceedingly rare case) would fail `network_backend` itself and go
/// through the retry path above rather than this one.
/// One tick's worth of the network icon *and* its menu — fetched
/// together because both need the same access-point scan and saved-
/// network list, and asking for that twice would be a second round trip
/// against NetworkManager for no new information.
async fn sample_network(state: &mut Reconnecting<NetworkManagerBackend>) -> (TrayItem, Menu) {
    let Some(backend) = network_backend(state).await else {
        return (network_item(None, None, None, true), network_menu(None, None, true, &[], &[]));
    };
    match backend.status().await {
        Ok(status) => {
            // The connected network can briefly be missing from a fresh
            // scan; that is not "unavailable", just "nothing read this
            // tick" — an empty list here still lets the item and menu
            // render, just without a signal reading or any other rows.
            // `NoWifiDevice` is the one failure that means something: a
            // machine with no Wi-Fi at all, whose icon is the wire's.
            let (points, wifi_present) = match backend.access_points().await {
                Ok(points) => (points, true),
                Err(NetworkError::NoWifiDevice) => (Vec::new(), false),
                Err(_) => (Vec::new(), true),
            };
            // Likewise a failed wired read degrades to "no wired rows
            // this tick", never to the whole icon going unavailable.
            let wired = backend.wired().await.unwrap_or_default();
            let strength = status
                .connected_to
                .as_ref()
                .and_then(|ssid| points.iter().find(|ap| &ap.ssid == ssid).map(|ap| ap.strength));
            // Likewise: a failed saved-list read must not make an
            // otherwise-fine menu disappear, only omit the "already
            // saved" distinction for this tick.
            let saved = backend.saved_networks().await.unwrap_or_default();
            let item = with_wired(
                network_item(Some(&status), status.connected_to.as_ref(), strength, false),
                status.connected_to.as_ref(),
                &wired,
                wifi_present,
            );
            let menu = with_wired_menu(
                network_menu(Some(&status), status.connected_to.as_ref(), false, &points, &saved),
                &wired,
                wifi_present,
                false,
            );
            (item, menu)
        }
        Err(e) => {
            tracing::warn!(error = %e, "NetworkManager status call failed");
            (network_item(None, None, None, true), network_menu(None, None, true, &[], &[]))
        }
    }
}

async fn sample_bluetooth(state: &mut Reconnecting<BlueZBackend>) -> (TrayItem, Menu) {
    let Some(backend) = bluetooth_backend(state).await else {
        return (bluetooth_item(None, None, true), bluetooth_menu(None, true, &[]));
    };
    match backend.status().await {
        Ok(status) => {
            let devices = backend.devices().await.unwrap_or_default();
            let connected = devices.iter().find(|d| d.connected);
            let item = bluetooth_item(Some(&status), connected, false);
            let menu = bluetooth_menu(Some(&status), false, &devices);
            (item, menu)
        }
        Err(e) => {
            tracing::warn!(error = %e, "BlueZ status call failed");
            (bluetooth_item(None, None, true), bluetooth_menu(None, true, &[]))
        }
    }
}

/// One tick's worth of the keep-awake icon and its menu.
///
/// `list_inhibitors` is asked every tick regardless of whether *we* are
/// currently holding one — the whole point of showing other holders (see
/// `keep_awake_menu`) is answering "why won't this machine sleep"
/// independent of whether this daemon is the reason, so it would be
/// wrong to skip that call just because our own inhibit is off.
async fn sample_keep_awake(state: &mut Reconnecting<DetachedBackend>) -> (TrayItem, Menu) {
    let Some(backend) = keep_awake_backend(state).await else {
        return (keep_awake_item(None, &[], true), keep_awake_menu(None, &[], true));
    };
    match backend.held().await {
        Ok(held) => {
            // A failed `list_inhibitors` must not blank an otherwise-fine
            // icon — same reasoning as a failed saved-network read in
            // `sample_network` — so it degrades to "no other holders
            // known this tick" rather than "keep awake unavailable".
            let others = backend.list_inhibitors().await.unwrap_or_default();
            let others = other_inhibitors(others);
            (
                keep_awake_item(Some(held.is_some()), &others, false),
                keep_awake_menu(Some(held.is_some()), &others, false),
            )
        }
        Err(e) => {
            tracing::warn!(error = %e, "logind held() call failed");
            (keep_awake_item(None, &[], true), keep_awake_menu(None, &[], true))
        }
    }
}

/// One tick's worth of the power icon and its menu.
///
/// Two daemons, asked independently, and each failure kept to its own
/// half — see [`BatteryReading`] and [`ProfileReading`].
async fn sample_power(
    upower: &mut Reconnecting<UPowerBackend>,
    profiles: &mut Reconnecting<PowerProfilesDaemonBackend>,
) -> (TrayItem, Menu) {
    let battery = match upower_backend(upower).await {
        None => BatteryReading::Unavailable,
        Some(backend) => match backend.battery().await {
            Ok(Some(info)) => BatteryReading::Present(info),
            Ok(None) => BatteryReading::Absent,
            Err(e) => {
                tracing::warn!(error = %e, "UPower battery call failed");
                BatteryReading::Unavailable
            }
        },
    };
    let profile = match profiles_backend(profiles).await {
        None => ProfileReading::Unavailable,
        Some(backend) => match backend.active_profile().await {
            Ok(active) => {
                // A failed list read still knows the active profile, and
                // offering only that one is honest; offering all three
                // would be a guess about what this machine supports.
                let offered = backend.profiles().await.unwrap_or_else(|_| vec![active]);
                ProfileReading::Known { active, offered }
            }
            Err(e) => {
                tracing::warn!(error = %e, "power-profiles-daemon call failed");
                ProfileReading::Unavailable
            }
        },
    };
    (power_item(&battery, &profile), power_menu(&battery, &profile))
}

/// How long a displayd query may take — `command::TIMEOUT`, the bound
/// every other wait on another process in this suite uses. zbus has no
/// reply timeout on a `#[proxy]` method, so it is applied at the call.
const DISPLAYD_CALL_TIMEOUT: Duration = hyprforge_core::command::TIMEOUT;

/// Applying and reverting commit to the compositor and wait for it to
/// settle — up to 18s of legitimate work inside displayd — so they get
/// the same longer bound the Settings app's Monitors screen gives them,
/// for the reason written there: a shorter one abandons an apply after
/// the mode has already changed.
const DISPLAYD_APPLY_TIMEOUT: Duration = Duration::from_secs(25);

/// Bounds one displayd call, naming it in the error the log will carry.
async fn displayd_bounded<T>(
    what: &str,
    within: Duration,
    call: impl std::future::Future<Output = zbus::Result<T>>,
) -> anyhow::Result<T> {
    match tokio::time::timeout(within, call).await {
        Ok(result) => Ok(result?),
        Err(_) => anyhow::bail!("{what} timed out after {}s", within.as_secs()),
    }
}

/// The session bus, connected once and kept — displayd is reached by
/// name over it, so the connection outlives the daemon restarting.
async fn session_bus(state: &mut Reconnecting<zbus::Connection>) -> Option<Arc<zbus::Connection>> {
    let connect = || async {
        match tokio::time::timeout(DISPLAYD_CALL_TIMEOUT, zbus::Connection::session()).await {
            Ok(result) => result.map_err(anyhow::Error::from),
            Err(_) => Err(anyhow::anyhow!("the session bus isn't answering")),
        }
    };
    match state.get_or_connect(connect).await {
        Ok(conn) => Some(conn),
        Err(e) => {
            tracing::warn!(error = %e, "could not connect to the session bus; will retry");
            None
        }
    }
}

async fn read_displays(conn: &zbus::Connection) -> anyhow::Result<DisplaysReading> {
    let proxy =
        displayd_bounded("connecting to displayd", DISPLAYD_CALL_TIMEOUT, DisplaydProxy::new(conn))
            .await?;
    let profiles = displayd_bounded("listing layouts", DISPLAYD_CALL_TIMEOUT, proxy.list_profiles())
        .await?
        .into_iter()
        .map(|(id, name, _heads, last_used)| DisplayProfile { id, name, last_used })
        .collect();
    let current =
        displayd_bounded("reading the current layout", DISPLAYD_CALL_TIMEOUT, proxy.get_current_profile())
            .await?;
    Ok(DisplaysReading::Known {
        profiles,
        // `""` is displayd's "nothing matches", by its own contract.
        current: Some(current).filter(|id| !id.is_empty()),
    })
}

/// One tick's worth of the displays icon and its menu.
async fn sample_displays(
    bus: &mut Reconnecting<zbus::Connection>,
    deadline: &RevertDeadline,
) -> (TrayItem, Menu) {
    let pending = revert_pending(
        *deadline.lock().unwrap_or_else(|e| e.into_inner()),
        tokio::time::Instant::now(),
    );
    let reading = match session_bus(bus).await {
        None => DisplaysReading::Unavailable,
        Some(conn) => read_displays(&conn).await.unwrap_or_else(|e| {
            // Not running is the common case, and is exactly what the
            // icon says; the detail is for the log.
            tracing::debug!(error = %e, "displayd didn't answer");
            DisplaysReading::Unavailable
        }),
    };
    (displays_item(&reading, pending), displays_menu(&reading, pending))
}

/// Records that displayd will roll back in `seconds`, and makes sure the
/// icon is redrawn both now and once the deadline passes — the second so
/// that a countdown which simply runs out (no `RevertResolved` from a
/// daemon that died) does not leave the icon asking.
fn set_revert_deadline(
    deadline: &RevertDeadline,
    seconds: u32,
    refresh: &tokio::sync::mpsc::UnboundedSender<()>,
) {
    let wait = Duration::from_secs(u64::from(seconds));
    *deadline.lock().unwrap_or_else(|e| e.into_inner()) = Some(tokio::time::Instant::now() + wait);
    let _ = refresh.send(());
    let refresh = refresh.clone();
    tokio::spawn(async move {
        tokio::time::sleep(wait + Duration::from_secs(1)).await;
        let _ = refresh.send(());
    });
}

fn clear_revert_deadline(deadline: &RevertDeadline, refresh: &tokio::sync::mpsc::UnboundedSender<()>) {
    *deadline.lock().unwrap_or_else(|e| e.into_inner()) = None;
    let _ = refresh.send(());
}

/// Follows displayd's revert signals for as long as this daemon runs.
///
/// Signals rather than polling because a revert window is shorter than
/// two poll ticks: polled, a layout applied from the Settings app could
/// come and go without this icon ever asking to keep it. Always
/// subscribed, whether or not the icon is shown — a match rule costs
/// nothing while no signal arrives, and subscribing only once the icon
/// is switched on would miss a countdown already running.
async fn watch_display_reverts(deadline: RevertDeadline, refresh: tokio::sync::mpsc::UnboundedSender<()>) {
    loop {
        if let Err(e) = follow_display_reverts(&deadline, &refresh).await {
            tracing::debug!(error = %e, "not following displayd's revert signals; will retry");
        }
        tokio::time::sleep(RETRY_INTERVAL).await;
    }
}

async fn follow_display_reverts(
    deadline: &RevertDeadline,
    refresh: &tokio::sync::mpsc::UnboundedSender<()>,
) -> anyhow::Result<()> {
    let conn = match tokio::time::timeout(DISPLAYD_CALL_TIMEOUT, zbus::Connection::session()).await {
        Ok(result) => result?,
        Err(_) => anyhow::bail!("the session bus isn't answering"),
    };
    let proxy =
        displayd_bounded("connecting to displayd", DISPLAYD_CALL_TIMEOUT, DisplaydProxy::new(&conn))
            .await?;
    let mut pending =
        displayd_bounded("subscribing to RevertPending", DISPLAYD_CALL_TIMEOUT, proxy.receive_revert_pending())
            .await?;
    let mut resolved = displayd_bounded(
        "subscribing to RevertResolved",
        DISPLAYD_CALL_TIMEOUT,
        proxy.receive_revert_resolved(),
    )
    .await?;
    loop {
        tokio::select! {
            Some(signal) = pending.next() => {
                set_revert_deadline(deadline, signal.args()?.seconds, refresh);
            }
            Some(_) = resolved.next() => clear_revert_deadline(deadline, refresh),
            else => anyhow::bail!("displayd's signal streams ended"),
        }
    }
}

/// Where night light stands, as far as this daemon can honestly claim to
/// know — see [`NightLightBelief`] for why "on" is never a direct read.
#[derive(Debug, Clone, Copy, PartialEq)]
enum NightLightState {
    /// Confirmed absent: `pgrep -x hyprsunset` found nothing.
    NotRunning,
    /// The check itself failed (`pgrep` didn't start or answer), or
    /// `hyprctl` refused/timed out — not evidence of anything about
    /// hyprsunset, so this is never rendered as "off".
    CouldNotCheck,
    /// hyprsunset answered. `temperature` is what it is currently holding
    /// (meaningful only when `on`); `on` is this daemon's own belief, not
    /// something hyprsunset reported.
    Known { temperature: i64, on: bool },
}

/// One tick's worth of the night-light icon and its menu.
///
/// `Hyprsunset::current_temperature` shells out to `hyprctl` via
/// `hyprforge_core::command::output`, which is bounded but still
/// synchronous — run inside `spawn_blocking` so a slow `hyprctl` stalls
/// only this one poll, not every other task on the runtime. The same
/// pattern `hyprforge-settings::modules::desktop` already uses for
/// `apply_sunset`.
async fn sample_night_light(believed_on: &NightLightBelief) -> (TrayItem, Menu) {
    let believed_on = *believed_on.lock().unwrap_or_else(|e| e.into_inner());
    let state = tokio::task::spawn_blocking(move || match Hyprsunset.current_temperature() {
        Ok(temperature) => NightLightState::Known { temperature, on: believed_on },
        Err(SunsetControlError::NotRunning) => NightLightState::NotRunning,
        Err(e) => {
            tracing::warn!(error = %e, "hyprsunset temperature check failed");
            NightLightState::CouldNotCheck
        }
    })
    .await
    .unwrap_or_else(|e| {
        tracing::warn!(error = %e, "night light check panicked");
        NightLightState::CouldNotCheck
    });
    (night_light_item(&state), night_light_menu(&state))
}

/// What to do with one icon's registration, given whether the user wants
/// it shown and whether it is currently up.
///
/// Pure — no D-Bus, no I/O — so the on/off transition is unit-tested
/// directly, the same split `network_item`/`bluetooth_item` already use
/// for "what should this icon say". `currently_up` is passed in rather
/// than read from the `IconSlot` itself, so a test needs no `Mutex`, no
/// `TrayIcon`, and no async runtime to exercise every case.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum IconAction {
    /// Already in the state the preference wants; nothing to do besides
    /// (if it's up) pushing this tick's new content to it.
    Keep,
    /// Wanted, and not currently registered.
    Register,
    /// Currently registered, and no longer wanted. The `TrayIcon` itself
    /// must be dropped — see the module doc for why `Status::Passive`
    /// alone does not reliably hide an item.
    Drop,
}

fn icon_action(wanted: bool, currently_up: bool) -> IconAction {
    match (wanted, currently_up) {
        (true, false) => IconAction::Register,
        (false, true) => IconAction::Drop,
        (true, true) | (false, false) => IconAction::Keep,
    }
}

/// Whether this tick has to ask the backend how the icon's subject is
/// doing.
///
/// Sampling is not free: between them the four samplers open a logind
/// connection and call `ListInhibitors`, walk BlueZ's object tree, read
/// NetworkManager's devices and access points, and spawn `pgrep` and
/// `hyprctl` as subprocesses. All of it every [`POLL_INTERVAL`],
/// forever.
///
/// An icon that is switched off *and* not currently registered needs
/// none of it: [`icon_action`] answers `Keep` for that pair, and `Keep`
/// with nothing registered does nothing at all with what was sampled.
/// Keep-awake and night light are **off by default**, so a stock
/// install was paying for two icons nobody had asked to see.
///
/// Still sampled when the icon is up but no longer wanted: that tick is
/// the one that drops it, and taking this decision from the same pair
/// `icon_action` reads is what keeps the two from disagreeing.
fn needs_sampling(wanted: bool, currently_up: bool) -> bool {
    !matches!((wanted, currently_up), (false, false))
}

/// Re-reads `tray.toml`, updating `current` only on success.
///
/// A failure here — the file exists and will not parse — must never
/// change what is currently shown: that is the "typo hides the whole
/// tray" failure mode the task exists to rule out. `warned` suppresses
/// repeat warnings for as long as the same failure persists, so a broken
/// file logs once rather than once every `POLL_INTERVAL` forever; it
/// resets as soon as a read succeeds, so a *later* failure warns again.
fn refresh_prefs(current: &mut Prefs, warned: &mut bool) {
    match prefs::load() {
        Ok(loaded) => {
            *current = loaded;
            *warned = false;
        }
        Err(e) => {
            if !*warned {
                tracing::warn!(
                    error = %e,
                    "could not read tray.toml; leaving tray icons as they are"
                );
                *warned = true;
            }
        }
    }
}

/// Brings one icon's `IconSlot` in line with `wanted` and pushes this
/// tick's content to it if it ends up (or stays) registered.
async fn sync_icon(
    slot: &IconSlot,
    wanted: bool,
    item: TrayItem,
    menu: Menu,
    index: u32,
    clicks: &tokio::sync::mpsc::UnboundedSender<String>,
    menu_clicks: &tokio::sync::mpsc::UnboundedSender<String>,
) {
    let id = item.id.clone();
    let mut guard = slot.lock().await;
    match icon_action(wanted, guard.is_some()) {
        IconAction::Keep => {
            if let Some(icon) = guard.as_ref() {
                if let Err(e) = icon.update(item).await {
                    tracing::warn!(error = %e, icon = %id, "failed to update tray icon");
                }
                // `update_menu` already skips the work (and the revision
                // bump) when the menu is unchanged, so calling it every
                // tick costs nothing extra when nothing changed.
                if let Err(e) = icon.update_menu(menu).await {
                    tracing::warn!(error = %e, icon = %id, "failed to update tray menu");
                }
            }
        }
        IconAction::Drop => {
            tracing::info!(icon = %id, "tray icon switched off; releasing its bus name");
            // Dropping the `Arc<TrayIcon>` (assuming this is the last
            // reference, which it is — nothing else holds one) drops its
            // `zbus::Connection`, which is what actually releases the bus
            // name. Setting `Status::Passive` instead would leave the
            // name — and the item — registered, which is the mistake
            // this whole feature exists to avoid; see the module doc.
            *guard = None;
        }
        IconAction::Register => {
            match TrayIcon::register_with_menu(item, menu, index, clicks.clone(), menu_clicks.clone())
                .await
            {
                Ok(icon) => *guard = Some(Arc::new(icon)),
                Err(e) => {
                    tracing::warn!(error = %e, icon = %id, "failed to register tray icon; will retry next tick");
                }
            }
        }
    }
}

/// Something that changes whenever the file does.
///
/// `None` for a file that is not there, which is a state rather than an
/// error: deleting `tray.toml` is a reset to the defaults, and has to
/// take effect like any other edit. Length is included alongside the
/// modification time so an edit inside one timestamp's resolution is
/// still seen.
fn prefs_stamp(path: &std::path::Path) -> Option<(std::time::SystemTime, u64)> {
    let meta = std::fs::metadata(path).ok()?;
    Some((meta.modified().ok()?, meta.len()))
}

/// Pulls the next poll forward the moment the preference file changes.
async fn prefs_watch_loop(refresh: tokio::sync::mpsc::UnboundedSender<()>) {
    let path = hyprforge_tray::prefs::path();
    let mut seen = prefs_stamp(&path);
    let mut ticker = tokio::time::interval(PREFS_WATCH_INTERVAL);
    loop {
        ticker.tick().await;
        let now = prefs_stamp(&path);
        if now == seen {
            continue;
        }
        seen = now;
        // A closed channel means the poll loop is gone, so there is
        // nothing left to tell.
        if refresh.send(()).is_err() {
            return;
        }
    }
}

async fn poll_loop(
    icons: Vec<ManagedIcon>,
    night_light_belief: NightLightBelief,
    revert_deadline: RevertDeadline,
    clicks: tokio::sync::mpsc::UnboundedSender<String>,
    menu_clicks: tokio::sync::mpsc::UnboundedSender<String>,
    mut refresh_rx: tokio::sync::mpsc::UnboundedReceiver<()>,
) {
    let mut net_state = Reconnecting::new();
    let mut bt_state = Reconnecting::new();
    let mut power_state = Reconnecting::new();
    let mut upower_state = Reconnecting::new();
    let mut profiles_state = Reconnecting::new();
    let mut bus_state = Reconnecting::new();
    // Every icon starts registered (see `main`), so the preferences this
    // loop starts from must match that — otherwise an icon the user
    // already switched off would flash on screen for up to one
    // `POLL_INTERVAL` before this loop notices.
    let mut prefs = Prefs::default();
    let mut prefs_load_failed = false;
    // `interval` fires its first tick immediately, which is wanted here:
    // every icon started out saying "unavailable" in `main`, and the
    // first real read (of every backend and `tray.toml`) should not wait
    // a further `POLL_INTERVAL`.
    let mut ticker = tokio::time::interval(POLL_INTERVAL);
    loop {
        // Either the ordinary tick, or someone opening a menu asked for
        // the next one early. A refresh that arrives while this loop is
        // mid-sample is not lost: the channel holds it and the next
        // `select!` takes it immediately.
        tokio::select! {
            _ = ticker.tick() => {}
            _ = refresh_rx.recv() => {}
        }

        refresh_prefs(&mut prefs, &mut prefs_load_failed);

        // What each icon wants, and whether it is currently registered —
        // both settled before anything is sampled, because together they
        // decide whether sampling is needed at all (`needs_sampling`).
        let wanted = [
            prefs.network,
            prefs.bluetooth,
            prefs.keep_awake,
            prefs.night_light,
            prefs.power,
            prefs.displays,
        ];
        let mut up = [false; 6];
        for (slot, is_up) in icons.iter().zip(up.iter_mut()) {
            *is_up = slot.slot.lock().await.is_some();
        }

        let net = needs_sampling(wanted[0], up[0]).then_some(());
        let net = match net {
            Some(()) => Some(sample_network(&mut net_state).await),
            None => None,
        };
        let bt = match needs_sampling(wanted[1], up[1]) {
            true => Some(sample_bluetooth(&mut bt_state).await),
            false => None,
        };
        let ka = match needs_sampling(wanted[2], up[2]) {
            true => Some(sample_keep_awake(&mut power_state).await),
            false => None,
        };
        let nl = match needs_sampling(wanted[3], up[3]) {
            true => Some(sample_night_light(&night_light_belief).await),
            false => None,
        };
        let pw = match needs_sampling(wanted[4], up[4]) {
            true => Some(sample_power(&mut upower_state, &mut profiles_state).await),
            false => None,
        };
        let dp = match needs_sampling(wanted[5], up[5]) {
            true => Some(sample_displays(&mut bus_state, &revert_deadline).await),
            false => None,
        };

        // One row per icon, lined up against `icons` in the same fixed
        // order `main` registered them in — see `ManagedIcon`'s doc
        // comment for why this is a loop rather than four repeated
        // `sync_icon` calls.
        let ticks: [Option<(TrayItem, Menu)>; 6] = [net, bt, ka, nl, pw, dp];
        for ((icon, sampled), want) in icons.iter().zip(ticks).zip(wanted) {
            let Some((item, menu)) = sampled else { continue };
            sync_icon(&icon.slot, want, item, menu, icon.index, &clicks, &menu_clicks).await;
        }
    }
}

/// Forwards left-clicks (and the button-agnostic activations `sni.rs`
/// maps onto them) to `hyprforge-settings --screen <name>`.
///
/// Spawned and never waited on. The click arrived over `activate`, whose
/// D-Bus caller — the bar — is blocked on the method returning; waiting
/// here for the child to exit would make the whole bar stutter on every
/// click, and a launcher that fails to start is a warning, not a reason
/// to stop handling further clicks.
async fn handle_clicks(mut clicks: tokio::sync::mpsc::UnboundedReceiver<String>) {
    while let Some(screen) = clicks.recv().await {
        spawn_settings(&screen);
    }
}

/// Launches `hyprforge-settings --screen <screen>`, never waited on.
///
/// Shared by an icon click (`handle_clicks`) and a menu row that opens
/// Settings instead of acting directly — a network needing a passphrase
/// with no saved connection, for instance, since a tray menu has nowhere
/// to type one.
fn spawn_settings(screen: &str) {
    match tokio::process::Command::new("hyprforge-settings")
        .arg("--screen")
        .arg(screen)
        // The child's output goes nowhere rather than inheriting this
        // daemon's. A GUI started from here prints its own renderer
        // chatter — wgpu alone logs its adapter and surface formats at
        // startup — and inheriting it puts all of that in the tray
        // daemon's log, once per click, for as long as the session
        // lasts. The daemon's log is then unreadable and unbounded, and
        // the one thing it exists to record — a failed registration — is
        // buried. The child keeps its own logging; it does not need
        // ours.
        .stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()
    {
        Ok(_) => {}
        Err(e) => {
            tracing::warn!(error = %e, screen = %screen, "failed to launch hyprforge-settings")
        }
    }
}

/// Re-announces both icons whenever the tray host reappears.
///
/// A bar restart takes every registration on the old connection with it;
/// `TrayIcon::announce`'s doc comment covers why re-registering is the
/// only way back. This only fires on the *transition* into "present" —
/// re-announcing every poll while a host is already there would ask it to
/// re-add an icon it already has for no reason.
async fn reannounce_loop(probe: zbus::Connection, icons: Vec<ManagedIcon>) {
    // `main` only reaches this point after `watcher_present` returned
    // true, so the host is presumed present until proven otherwise.
    let mut present = true;
    let mut ticker = tokio::time::interval(WATCHER_POLL_INTERVAL);
    loop {
        ticker.tick().await;
        let now_present = watcher_present(&probe).await;
        if now_present && !present {
            tracing::info!("tray host reappeared; re-announcing icons");
            // An icon currently switched off has no `TrayIcon` to
            // re-announce — `poll_loop` will register it fresh (which
            // announces on its own) if and when the user turns it back
            // on, so there is nothing missing here to re-announce.
            for icon in &icons {
                if let Some(tray_icon) = icon.slot.lock().await.as_ref() {
                    if let Err(e) = tray_icon.announce().await {
                        tracing::warn!(error = %e, "failed to re-announce a tray icon");
                    }
                }
            }
        }
        present = now_present;
    }
}

/// Freedesktop icon names below the boundary in [`u8`] percent, matching
/// the row NetworkManager applets themselves use: 80/60/40/20.
///
/// `-symbolic`, not the plain name most NetworkManager applets use: the
/// plain form is a full-colour icon, and the other three items in this
/// tray (Bluetooth, keep awake, night light) are all symbolic already.
/// One colourful icon among three monochrome ones was the whole
/// complaint this rule exists to prevent. Confirmed present at both
/// `status/22` and `status/24` in `breeze` and `breeze-dark` on this
/// machine — the family every name in this file now shares.
fn wifi_signal_icon(strength: u8) -> &'static str {
    match strength {
        80..=u8::MAX => "network-wireless-signal-excellent-symbolic",
        60..=79 => "network-wireless-signal-good-symbolic",
        40..=59 => "network-wireless-signal-ok-symbolic",
        20..=39 => "network-wireless-signal-weak-symbolic",
        _ => "network-wireless-signal-none-symbolic",
    }
}

/// What the Wi-Fi icon should say, from plain data — no D-Bus, no I/O.
///
/// `connected_ssid` and `strength` are passed separately from `status`
/// rather than derived from it here, so a test can assert the tooltip and
/// icon for a specific signal reading without constructing a full access
/// point list. `unavailable` is checked before `status` is even looked
/// at, since a `None` status on its own already means the same thing:
/// nothing was read this tick, so say so rather than guessing.
fn network_item(
    status: Option<&NetStatus>,
    connected_ssid: Option<&Ssid>,
    strength: Option<u8>,
    unavailable: bool,
) -> TrayItem {
    let id = NETWORK_ITEM_ID.to_string();
    // "Network", not "Wi-Fi": this one icon covers Ethernet too — see
    // `with_wired`.
    let title = "Network".to_string();

    let Some(status) = status.filter(|_| !unavailable) else {
        return TrayItem {
            id,
            category: Category::Hardware,
            // Visible, not hidden — an icon that disappears exactly when
            // the daemon behind it can't be reached looks identical to
            // "nothing to report", which is the one thing pillar 3
            // forbids.
            status: TrayStatus::Active,
            title,
            icon_name: "dialog-warning".to_string(),
            tooltip_title: "Network unavailable".to_string(),
            tooltip_body: "NetworkManager isn't running.".to_string(),
        };
    };

    match status.radio {
        // Off (switched off in software) and HardwareOff (rfkill) both
        // mean the radio is not on.
        //
        // `Active`, not `Passive`. This used to hide, on the reasoning
        // that a radio switched off elsewhere is not something the bar
        // can act on — which the menu a few lines down has contradicted
        // for as long as it has existed: it offers `wifi:radio:on`. So
        // the icon vanished at exactly the moment its menu became most
        // useful, and the only way back was the Settings app. That is
        // the same argument `keep_awake_item` already makes about a
        // switch having to be visible in both positions; it just was not
        // applied here.
        //
        // "Disabled" is the icon and the tooltip, because that is all
        // StatusNotifierItem offers: there is no opacity, no sensitivity
        // flag, nothing a host is obliged to dim. A distinct name is the
        // whole vocabulary.
        RadioState::Off | RadioState::HardwareOff => TrayItem {
            id,
            category: Category::Hardware,
            status: TrayStatus::Active,
            title,
            icon_name: "network-wireless-disconnected-symbolic".to_string(),
            tooltip_title: "Wi-Fi is off".to_string(),
            tooltip_body: "Click for the menu to turn it back on".to_string(),
        },
        RadioState::On => match connected_ssid {
            Some(ssid) => TrayItem {
                id,
                category: Category::Hardware,
                status: TrayStatus::Active,
                title,
                icon_name: wifi_signal_icon(strength.unwrap_or(0)).to_string(),
                tooltip_title: ssid.to_display_string(),
                tooltip_body: match strength {
                    Some(s) => format!("Signal {s}%"),
                    None => "Connected".to_string(),
                },
            },
            // On and unconnected is exactly when someone goes looking
            // for this icon.
            //
            // A *different* name from the radio-off case above, which is
            // new: while "off" was hidden, both states could share
            // `disconnected` and nobody could see both at once. Now that
            // off is visible too, one name for two states would mean a
            // user cannot tell "the radio is off" from "the radio is on
            // and found nothing" — which are the two states with
            // completely different things to do about them. Empty signal
            // arcs for a radio that is on and searching; the crossed-out
            // name for one that is off.
            None => TrayItem {
                id,
                category: Category::Hardware,
                status: TrayStatus::Active,
                title,
                icon_name: "network-wireless-signal-none-symbolic".to_string(),
                tooltip_title: "Not connected".to_string(),
                tooltip_body: "Wi-Fi is on".to_string(),
            },
        },
    }
}

/// What the Bluetooth icon should say, from plain data — no D-Bus, no
/// I/O. Same shape and same reasoning as [`network_item`].
fn bluetooth_item(
    status: Option<&BtStatus>,
    connected_device: Option<&Device>,
    unavailable: bool,
) -> TrayItem {
    let id = "hyprforge-bluetooth".to_string();
    let title = "Bluetooth".to_string();

    let Some(status) = status.filter(|_| !unavailable) else {
        return TrayItem {
            id,
            category: Category::Hardware,
            status: TrayStatus::Active,
            title,
            icon_name: "dialog-warning".to_string(),
            tooltip_title: "Bluetooth unavailable".to_string(),
            tooltip_body: "bluetoothd isn't running.".to_string(),
        };
    };

    match status.state {
        // HardwareBlocked is rfkill, not "off" in software, but both
        // mean the adapter is not on. `Active` for the same reason as
        // Wi-Fi above: this item's own menu can switch it back on, so
        // hiding it took away the one control that mattered.
        AdapterState::Off | AdapterState::HardwareBlocked => TrayItem {
            id,
            category: Category::Hardware,
            status: TrayStatus::Active,
            title,
            icon_name: "network-bluetooth-inactive-symbolic".to_string(),
            tooltip_title: "Bluetooth is off".to_string(),
            tooltip_body: "Click for the menu to turn it back on".to_string(),
        },
        AdapterState::On | AdapterState::Changing => match connected_device {
            Some(device) => TrayItem {
                id,
                category: Category::Hardware,
                status: TrayStatus::Active,
                title,
                icon_name: "network-bluetooth-activated-symbolic".to_string(),
                // `alias` always exists (falls back to `Name`, then the
                // address) — never the raw address alone, and never
                // anything derived from pairing secrets, of which this
                // crate exposes none anyway.
                tooltip_title: device.alias.clone(),
                tooltip_body: device.kind.label().to_string(),
            },
            // Plain, not `activated`: same reasoning as Wi-Fi's empty
            // arcs above. With the off state now visible, three states
            // share this icon's position — off, on and idle, on and
            // connected — and they need three names between them, or the
            // icon stops carrying information.
            None => TrayItem {
                id,
                category: Category::Hardware,
                status: TrayStatus::Active,
                title,
                icon_name: "network-bluetooth-symbolic".to_string(),
                tooltip_title: "Not connected".to_string(),
                tooltip_body: "Bluetooth is on".to_string(),
            },
        },
    }
}

/// How many networks the Wi-Fi menu shows before it starts truncating.
///
/// A scan in a dense apartment building can return dozens of access
/// points; a menu that lists all of them stops being a menu. Eight is
/// enough to show a home network plus a handful of neighbours without the
/// popup running off the bottom of the screen.
const MAX_NETWORKS_SHOWN: usize = 8;

/// Renders a byte slice as lowercase hex.
///
/// [`Ssid`] is arbitrary bytes, not necessarily UTF-8 (see its doc
/// comment), so it cannot be embedded in an action string as text without
/// either losing information or risking a `:` inside the name being
/// mistaken for a field separator. Hex side-steps both: every byte
/// round-trips, and the result contains none of the vocabulary's own
/// delimiters.
fn hex_encode(bytes: &[u8]) -> String {
    use std::fmt::Write;
    let mut out = String::with_capacity(bytes.len() * 2);
    for b in bytes {
        let _ = write!(out, "{b:02x}");
    }
    out
}

fn hex_decode(s: &str) -> Option<Vec<u8>> {
    if !s.len().is_multiple_of(2) {
        return None;
    }
    let bytes = s.as_bytes();
    let mut out = Vec::with_capacity(bytes.len() / 2);
    for pair in bytes.chunks_exact(2) {
        let hi = (pair[0] as char).to_digit(16)?;
        let lo = (pair[1] as char).to_digit(16)?;
        out.push(((hi << 4) | lo) as u8);
    }
    Some(out)
}

/// Why [`Security::Enterprise`](hyprforge_network::Security::Enterprise) is disabled, shortened for a menu row
/// rather than the full sentence [`hyprforge_network::Security::unsupported_reason`]
/// gives a whole screen to.
const ENTERPRISE_ROW_REASON: &str = "enterprise sign-in not supported";

/// What the Wi-Fi menu should contain, from plain data — no D-Bus, no I/O.
///
/// `access_points` is expected in scan order; this calls
/// [`hyprforge_network::backend::for_display`] itself, the same
/// dedup-and-sort the Network settings screen uses, so the two never
/// disagree about which of several BSSIDs sharing an SSID is "the"
/// network. `saved` decides whether a passphrase-protected network gets a
/// connect action or a Settings one — see the module-level task doc: a
/// tray menu has nowhere to type a passphrase, so a network this daemon
/// has no stored credential for can only be joined from Settings.
fn network_menu(
    status: Option<&NetStatus>,
    connected_ssid: Option<&Ssid>,
    unavailable: bool,
    access_points: &[AccessPoint],
    saved: &[SavedNetwork],
) -> Menu {
    let settings_row = MenuItem::standard("Network settings…", "wifi:settings");

    let Some(status) = status.filter(|_| !unavailable) else {
        return radio_menu(vec![MenuItem::disabled("Network unavailable")], Vec::new(), settings_row);
    };

    let top = match status.radio {
        RadioState::Off => {
            return radio_menu(
                vec![MenuItem::checkmark("Wi-Fi", false, "wifi:radio:on")],
                Vec::new(),
                settings_row,
            );
        }
        // rfkill: no amount of D-Bus flips this back on, so the row is
        // shown but not offered as a toggle — the same call
        // `network_item` makes for the icon itself.
        RadioState::HardwareOff => {
            return radio_menu(
                vec![MenuItem::disabled_checkmark("Wi-Fi (blocked by hardware switch)", false)],
                Vec::new(),
                settings_row,
            );
        }
        RadioState::On => vec![MenuItem::checkmark("Wi-Fi", true, "wifi:radio:off")],
    };

    let mut content = Vec::new();
    let displayed = net_for_display(access_points.to_vec());
    let total = displayed.len();
    for ap in displayed.into_iter().take(MAX_NETWORKS_SHOWN) {
        let is_connected = connected_ssid == Some(&ap.ssid);
        let label = ap.ssid.to_display_string();

        if ap.security.unsupported_reason().is_some() {
            content.push(MenuItem::disabled(format!("{label} ({ENTERPRISE_ROW_REASON})")));
            continue;
        }

        let has_saved = saved.iter().any(|s| s.ssid == ap.ssid);
        let action = if ap.security.needs_passphrase() && !has_saved {
            // Nowhere to type a passphrase in a tray menu — see the
            // module doc.
            "wifi:settings".to_string()
        } else {
            format!("wifi:connect:{}", hex_encode(ap.ssid.as_bytes()))
        };

        if is_connected {
            content.push(MenuItem::checkmark(label, true, action));
        } else {
            content.push(MenuItem::standard(label, action));
        }
    }
    if total > MAX_NETWORKS_SHOWN {
        content.push(MenuItem::disabled(format!(
            "+{} more — see Settings",
            total - MAX_NETWORKS_SHOWN
        )));
    }

    radio_menu(top, content, settings_row)
}

/// How a wired interface is named in a row: plainly "Wired" when it is
/// the only one, and with its interface name when there are several, so
/// a laptop's port and a dock's can be told apart.
fn wired_label(port: &WiredStatus, several: bool) -> String {
    if several {
        format!("Wired ({})", port.interface)
    } else {
        "Wired".to_string()
    }
}

/// The network icon, given what the Wi-Fi half already decided.
///
/// A connected cable outranks Wi-Fi, because NetworkManager routes over it
/// ahead of Wi-Fi (its default metrics put Ethernet at 100 and Wi-Fi at
/// 600): the icon should name the link traffic actually uses. Otherwise
/// Wi-Fi's own icon stands — unless this machine has no Wi-Fi hardware at
/// all, where a Wi-Fi icon saying "not connected" would describe a radio
/// that is not there, and the icon is the wire's instead.
fn with_wired(
    wifi: TrayItem,
    connected_ssid: Option<&Ssid>,
    wired: &[WiredStatus],
    wifi_present: bool,
) -> TrayItem {
    let several = wired.len() > 1;
    let wired_item = |icon: &str, tooltip_title: String, tooltip_body: String| TrayItem {
        icon_name: icon.to_string(),
        tooltip_title,
        tooltip_body,
        ..wifi.clone()
    };

    if let Some(port) = wired.iter().find(|p| p.state == WiredState::Connected) {
        let mut body: Vec<String> = Vec::new();
        if several {
            body.push(port.interface.clone());
        }
        if let Some(speed) = port.speed_mbps {
            body.push(format!("{speed} Mb/s"));
        }
        if let Some(ssid) = connected_ssid {
            body.push(format!("also on Wi-Fi ({})", ssid.to_display_string()));
        }
        return wired_item(
            "network-wired-activated-symbolic",
            match &port.connection {
                Some(name) => format!("Wired — {name}"),
                None => "Wired".to_string(),
            },
            body.join(" · "),
        );
    }
    // Wi-Fi connected while a cable is still getting an address: Wi-Fi is
    // what works right now, so it keeps the icon until the wire is up.
    if connected_ssid.is_none() {
        if let Some(port) = wired.iter().find(|p| p.state == WiredState::Connecting) {
            return wired_item(
                "network-wired-symbolic",
                format!("{} — connecting…", wired_label(port, several)),
                String::new(),
            );
        }
    }
    if wifi_present {
        return wifi;
    }
    match wired.iter().find(|p| p.state == WiredState::Disconnected) {
        Some(port) => wired_item(
            "network-wired-disconnected-symbolic",
            "Not connected".to_string(),
            format!("{} has a cable, but no connection is active", wired_label(port, several)),
        ),
        None if wired.is_empty() => wired_item(
            "network-wired-unavailable-symbolic",
            "No network devices".to_string(),
            "NetworkManager sees no Wi-Fi or Ethernet here.".to_string(),
        ),
        None => wired_item(
            "network-wired-unavailable-symbolic",
            "Cable unplugged".to_string(),
            String::new(),
        ),
    }
}

/// The network menu, given the Wi-Fi half already built.
///
/// Wired interfaces come first, above the Wi-Fi toggle and in a section
/// of their own, because the toggle governs only the radio and a cable
/// listed under it would read as switched by it. A connected port is a
/// checkmark that disconnects it; a port with a cable and nothing active
/// is a row that connects it; an unplugged one is information, since no
/// menu can plug a cable in. With no Wi-Fi hardware the Wi-Fi half is
/// dropped entirely rather than offering a radio toggle for a radio that
/// is not there.
fn with_wired_menu(wifi: Menu, wired: &[WiredStatus], wifi_present: bool, unavailable: bool) -> Menu {
    if unavailable {
        return wifi;
    }
    let several = wired.len() > 1;
    let rows: Vec<MenuItem> = wired
        .iter()
        .map(|port| {
            let label = wired_label(port, several);
            match port.state {
                WiredState::Connected => MenuItem::checkmark(
                    match &port.connection {
                        Some(name) => format!("{label} — {name}"),
                        None => label,
                    },
                    true,
                    format!("wired:disconnect:{}", port.interface),
                ),
                WiredState::Connecting => MenuItem::disabled(format!("{label} — connecting…")),
                WiredState::Disconnected => {
                    MenuItem::standard(label, format!("wired:connect:{}", port.interface))
                }
                WiredState::CableUnplugged => MenuItem::disabled(format!("{label} — cable unplugged")),
            }
        })
        .collect();

    if !wifi_present {
        let settings_row = MenuItem::standard("Network settings…", "wifi:settings");
        let top = if rows.is_empty() {
            vec![MenuItem::disabled("No network devices")]
        } else {
            rows
        };
        return radio_menu(top, Vec::new(), settings_row);
    }
    if rows.is_empty() {
        return wifi;
    }
    let mut items = rows;
    items.push(MenuItem::separator());
    items.extend(wifi.items);
    Menu::new(items)
}

/// What the Bluetooth menu should contain, from plain data — no D-Bus, no
/// I/O. Same shape and reasoning as [`network_menu`].
fn bluetooth_menu(status: Option<&BtStatus>, unavailable: bool, devices: &[Device]) -> Menu {
    let settings_row = MenuItem::standard("Bluetooth settings…", "bt:settings");

    let Some(status) = status.filter(|_| !unavailable) else {
        return radio_menu(vec![MenuItem::disabled("Bluetooth unavailable")], Vec::new(), settings_row);
    };

    let top = match status.state {
        AdapterState::Off => {
            return radio_menu(
                vec![MenuItem::checkmark("Bluetooth", false, "bt:radio:on")],
                Vec::new(),
                settings_row,
            );
        }
        AdapterState::HardwareBlocked => {
            return radio_menu(
                vec![MenuItem::disabled_checkmark("Bluetooth (blocked by hardware switch)", false)],
                Vec::new(),
                settings_row,
            );
        }
        AdapterState::On | AdapterState::Changing => {
            vec![MenuItem::checkmark("Bluetooth", true, "bt:radio:off")]
        }
    };

    let mut content = Vec::new();
    for device in bt_for_display(devices.to_vec()) {
        if !device.paired {
            // A menu has nowhere to show six digits and ask whether they
            // match, so pairing is handed to the screen that does — the
            // same call as a Wi-Fi network whose passphrase we do not
            // have. It used to say "pairing not supported yet", which
            // stopped being true the moment the agent landed.
            content.push(MenuItem::standard(
                format!("{} — pair in Settings", device.alias),
                "bt:settings",
            ));
            continue;
        }

        let action = if device.connected {
            format!("bt:disconnect:{}", device.address)
        } else {
            format!("bt:connect:{}", device.address)
        };

        if device.connected {
            content.push(MenuItem::checkmark(device.alias.clone(), true, action));
        } else {
            content.push(MenuItem::standard(device.alias.clone(), action));
        }
    }

    radio_menu(top, content, settings_row)
}

/// The name this daemon's inhibit went by before keep awake moved into a
/// holder process (`hyprforge_power::keep_awake`). Nothing takes one
/// under it now; it is still recognised in [`other_inhibitors`] so the
/// lock a not-yet-restarted older daemon holds is not listed as somebody
/// else's.
const KEEP_AWAKE_WHO: &str = "hyprforge-trayd";
const KEEP_AWAKE_WHY: &str = "user requested via the tray";
const KEEP_AWAKE_ITEM_ID: &str = "hyprforge-keep-awake";
const NIGHT_LIGHT_ITEM_ID: &str = "hyprforge-night-light";

/// How many other inhibitors the keep-awake menu shows before truncating.
/// A busy session can rack up several (a video call, a download manager,
/// a game); a menu that lists all of them stops being a menu — same
/// reasoning as [`MAX_NETWORKS_SHOWN`].
const MAX_OTHER_INHIBITORS_SHOWN: usize = 5;

/// Everyone else's inhibitor — [`InhibitBackend::list_inhibitors`]
/// reports every one logind holds, ours included, and this is what keeps
/// our own toggle from appearing a second time in the list of *other*
/// holders right next to the checkmark that already represents it.
fn other_inhibitors(all: Vec<InhibitorInfo>) -> Vec<InhibitorInfo> {
    all.into_iter()
        .filter(|i| i.who != KEEP_AWAKE_WHO && i.who != hyprforge_power::keep_awake::HOLDER_WHO)
        .collect()
}

/// What the keep-awake icon should say, from plain data — no D-Bus, no
/// I/O. `others` is expected already filtered by [`other_inhibitors`].
///
/// `held` is whether keep awake is on — its holder is listed by logind,
/// wherever it was turned on from. The item is `Status::Active` either
/// way: a host hides a Passive icon, and an icon the host hides is a
/// toggle nobody can reach (CLAUDE.md, "An icon that asks a host to hide
/// it"); the icon *name* is what says on or off.
fn keep_awake_item(held: Option<bool>, others: &[InhibitorInfo], unavailable: bool) -> TrayItem {
    let id = KEEP_AWAKE_ITEM_ID.to_string();
    let title = "Keep awake".to_string();

    let Some(held) = held.filter(|_| !unavailable) else {
        return TrayItem {
            id,
            category: Category::SystemServices,
            status: TrayStatus::Active,
            title,
            icon_name: "dialog-warning".to_string(),
            tooltip_title: "Keep awake unavailable".to_string(),
            tooltip_body: "systemd-logind isn't answering.".to_string(),
        };
    };

    let plural = if others.len() == 1 { "" } else { "es" };
    if held {
        TrayItem {
            id,
            category: Category::SystemServices,
            status: TrayStatus::Active,
            title,
            icon_name: "changes-prevent-symbolic".to_string(),
            tooltip_title: "Keeping this machine awake".to_string(),
            tooltip_body: if others.is_empty() {
                String::new()
            } else {
                format!("Also held by {} other process{plural}", others.len())
            },
        }
    } else {
        TrayItem {
            id,
            category: Category::SystemServices,
            // Active, not Passive, even though nothing is being held.
            //
            // `Passive` asks the host to hide the item, and hosts obey —
            // so switching this icon on in Settings made it appear and
            // then immediately vanish, because "not currently keeping
            // the machine awake" is the state it starts in. An icon you
            // can only see once it is already on is a toggle you cannot
            // reach.
            //
            // This is a switch, and a switch has to be visible in both
            // positions. That used to read "the radios are the case
            // Passive is right for" — which was wrong about this
            // daemon's own code even as it was written: the Wi-Fi and
            // Bluetooth menus have always offered to switch the radio
            // back on, so hiding those icons took away the control at
            // the moment it was wanted. They are `Active` in every state
            // now, and nothing in this file asks a host to hide
            // anything.
            status: TrayStatus::Active,
            title,
            icon_name: "changes-allow-symbolic".to_string(),
            tooltip_title: "Not keeping this machine awake".to_string(),
            tooltip_body: if others.is_empty() {
                "Nothing is preventing sleep".to_string()
            } else {
                format!("{} other process{plural} preventing sleep", others.len())
            },
        }
    }
}

/// What the keep-awake menu should contain, from plain data — no D-Bus,
/// no I/O. Same expectation on `others` as [`keep_awake_item`].
///
/// The checkmark is the only actionable row. Every other holder listed
/// beneath it is disabled: this daemon only ever manages its own
/// inhibit, never someone else's — `InhibitBackend::list_inhibitors`'s
/// doc comment says as much — so the value of listing them is answering
/// "why won't this machine sleep" from the tray, not offering to end
/// them.
fn keep_awake_menu(held: Option<bool>, others: &[InhibitorInfo], unavailable: bool) -> Menu {
    let settings_row = MenuItem::standard("Keep awake settings…", "keepawake:settings");

    let Some(held) = held.filter(|_| !unavailable) else {
        return radio_menu(vec![MenuItem::disabled("Keep awake unavailable")], Vec::new(), settings_row);
    };

    let toggle = MenuItem::checkmark(
        "Keep awake",
        held,
        if held { "keepawake:off" } else { "keepawake:on" },
    );

    let mut content = Vec::new();
    if !others.is_empty() {
        content.push(MenuItem::disabled("Also preventing sleep:"));
        for other in others.iter().take(MAX_OTHER_INHIBITORS_SHOWN) {
            content.push(MenuItem::disabled(format!("{} — {}", other.who, other.why)));
        }
        if others.len() > MAX_OTHER_INHIBITORS_SHOWN {
            content.push(MenuItem::disabled(format!(
                "+{} more",
                others.len() - MAX_OTHER_INHIBITORS_SHOWN
            )));
        }
    }

    radio_menu(vec![toggle], content, settings_row)
}

/// The warm presets offered as menu rows — the one thing a tray menu can
/// do that a plain on/off toggle cannot. 4500K matches
/// `sunset_control::DEFAULT_WARM_TEMPERATURE` deliberately, so turning
/// night light on from the checkmark and picking the first preset are
/// the same action.
const NIGHT_LIGHT_PRESETS: &[i64] = &[4500, 3500, 2700];

/// What the night-light icon should say, from plain data — no D-Bus, no
/// I/O.
///
/// Three distinct rows for three distinct facts, none of them collapsed
/// into another: [`NightLightState::NotRunning`] is hyprsunset confirmed
/// absent, [`NightLightState::CouldNotCheck`] is the check itself
/// failing (evidence about `pgrep`/`hyprctl`, not about hyprsunset), and
/// [`NightLightState::Known`] is an actual reading. Rendering
/// `NotRunning` with the same icon as "off" would be exactly the mistake
/// the task this module was built against calls out by name: a daemon
/// that isn't there must never look like a night light setting.
fn night_light_item(state: &NightLightState) -> TrayItem {
    let id = NIGHT_LIGHT_ITEM_ID.to_string();
    let title = "Night light".to_string();
    match *state {
        NightLightState::NotRunning => TrayItem {
            id,
            category: Category::SystemServices,
            status: TrayStatus::Active,
            title,
            icon_name: "dialog-warning".to_string(),
            tooltip_title: "Night light unavailable".to_string(),
            tooltip_body: "hyprsunset isn't running.".to_string(),
        },
        NightLightState::CouldNotCheck => TrayItem {
            id,
            category: Category::SystemServices,
            status: TrayStatus::Active,
            title,
            icon_name: "dialog-information".to_string(),
            tooltip_title: "Night light status unknown".to_string(),
            tooltip_body: "Couldn't tell whether hyprsunset is running.".to_string(),
        },
        NightLightState::Known { temperature, on: true } => TrayItem {
            id,
            category: Category::SystemServices,
            status: TrayStatus::Active,
            title,
            icon_name: "redshift-status-on-symbolic".to_string(),
            tooltip_title: "Night light is on".to_string(),
            tooltip_body: format!("{temperature}K"),
        },
        // `Active`, like every other state this daemon publishes. Off
        // was `Passive` — hidden — which made this a toggle that
        // disappeared in one of its two positions, the same defect the
        // radios had and for the same reason: the menu behind this icon
        // turns night light back on. Off and on already look different
        // (`redshift-status-off-symbolic` against `-on-symbolic`), so
        // nothing else was needed here.
        NightLightState::Known { on: false, .. } => TrayItem {
            id,
            category: Category::SystemServices,
            status: TrayStatus::Active,
            title,
            icon_name: "redshift-status-off-symbolic".to_string(),
            tooltip_title: "Night light is off".to_string(),
            tooltip_body: String::new(),
        },
    }
}

/// What the night-light menu should contain, from plain data — no D-Bus,
/// no I/O. Same three-states rule as [`night_light_item`]: the
/// unavailable rows say which of the two distinct reasons applies rather
/// than sharing one "can't do anything" row.
fn night_light_menu(state: &NightLightState) -> Menu {
    let settings_row = MenuItem::standard("Night light settings…", "nightlight:settings");

    let (temperature, on) = match *state {
        // Names the feature, like the other three unavailable rows, but
        // keeps the detail that makes this one actionable: hyprsunset not
        // running is expected until something starts it, not a bug.
        NightLightState::NotRunning => {
            return radio_menu(
                vec![MenuItem::disabled("Night light unavailable — hyprsunset isn't running")],
                Vec::new(),
                settings_row,
            );
        }
        // Distinct from `NotRunning`: this is the check itself failing,
        // not evidence that hyprsunset is absent — see `NightLightState`'s
        // own doc. Collapsing the two into one row would lose exactly the
        // distinction CLAUDE.md calls out.
        NightLightState::CouldNotCheck => {
            return radio_menu(
                vec![MenuItem::disabled("Night light status unknown")],
                Vec::new(),
                settings_row,
            );
        }
        NightLightState::Known { temperature, on } => (temperature, on),
    };

    let toggle_action = if on {
        "nightlight:off".to_string()
    } else {
        format!("nightlight:set:{}", NIGHT_LIGHT_PRESETS[0])
    };
    let toggle = MenuItem::checkmark("Night light", on, toggle_action);

    let mut content = Vec::new();
    for &kelvin in NIGHT_LIGHT_PRESETS {
        let is_current = on && temperature == kelvin;
        let action = format!("nightlight:set:{kelvin}");
        if is_current {
            content.push(MenuItem::checkmark(format!("{kelvin}K"), true, action));
        } else {
            content.push(MenuItem::standard(format!("{kelvin}K"), action));
        }
    }

    radio_menu(vec![toggle], content, settings_row)
}

const POWER_ITEM_ID: &str = "hyprforge-power";

/// What the battery half of the power icon knows this tick.
///
/// Three states, not two, for the reason `BatteryBackend::battery` is an
/// `Option` inside a `Result`: a desktop with no battery is an ordinary
/// machine, and UPower not answering is a failure — the icon has to be
/// able to say the second without implying the first.
#[derive(Debug, Clone, Copy, PartialEq)]
enum BatteryReading {
    Present(BatteryInfo),
    /// UPower answered: there is no system battery.
    Absent,
    /// UPower did not answer.
    Unavailable,
}

/// The power-profile half. Independent of [`BatteryReading`] because it
/// is a different daemon — `power-profiles-daemon` down leaves the
/// battery readable, and the other way round.
#[derive(Debug, Clone, PartialEq)]
enum ProfileReading {
    Known {
        active: PowerProfile,
        /// What the daemon says this machine can switch between, in its
        /// own order — read rather than assumed to be all three.
        offered: Vec<PowerProfile>,
    },
    Unavailable,
}

fn profile_label(profile: PowerProfile) -> &'static str {
    match profile {
        PowerProfile::PowerSaver => "Power saver",
        PowerProfile::Balanced => "Balanced",
        PowerProfile::Performance => "Performance",
    }
}

/// Breeze's own names for the three profiles — `battery-profile-*`, not
/// GNOME's `power-profile-*`, which exist only in Adwaita and are
/// unreachable from this machine's theme chain (see the icon-name notes
/// in the tests below).
fn profile_icon(profile: PowerProfile) -> &'static str {
    match profile {
        PowerProfile::PowerSaver => "battery-profile-powersave-symbolic",
        PowerProfile::Balanced => "battery-profile-balanced-symbolic",
        PowerProfile::Performance => "battery-profile-performance-symbolic",
    }
}

/// Whether the battery is taking charge, which is what the `-charging`
/// icons draw — a plug over the level.
fn is_charging(state: BatteryState) -> bool {
    matches!(state, BatteryState::Charging | BatteryState::PendingCharge)
}

/// The level icon for a battery reading.
///
/// Breeze draws eleven levels in steps of ten, and the percentage is
/// rounded to the *nearest* one: rounding down would show an empty
/// battery at 9%, which reads as "about to die" a good while early.
/// Fully charged gets its own name, because it is a plugged-in state
/// that nothing about the level alone distinguishes from 100% on battery.
fn battery_icon(info: &BatteryInfo) -> String {
    if info.state == BatteryState::FullyCharged {
        return "battery-full-charged-symbolic".to_string();
    }
    let level = ((u16::from(info.percentage) + 5) / 10 * 10).min(100);
    let charging = if is_charging(info.state) { "-charging" } else { "" };
    format!("battery-{level:03}{charging}-symbolic")
}

/// What the battery is doing, in the words a tooltip and a menu row
/// share.
fn battery_words(info: &BatteryInfo) -> String {
    let pct = info.percentage;
    match info.state {
        BatteryState::FullyCharged => "Fully charged".to_string(),
        BatteryState::Charging => match info.time_remaining_words() {
            Some(t) => format!("{pct}% — charging, full in {t}"),
            None => format!("{pct}% — charging"),
        },
        BatteryState::Discharging => match info.time_remaining_words() {
            Some(t) => format!("{pct}% — {t} left"),
            None => format!("{pct}% — on battery"),
        },
        // Plugged in and held below full (a charge limit, or the
        // controller waiting): neither "charging" nor "on battery" is
        // true, and saying either sends someone to check the cable.
        BatteryState::PendingCharge => format!("{pct}% — plugged in, not charging"),
        BatteryState::PendingDischarge | BatteryState::Empty | BatteryState::Unknown => {
            format!("{pct}%")
        }
    }
}

/// What the power icon should say, from plain data — no D-Bus, no I/O.
///
/// The icon is the battery level when there is a battery, and the active
/// profile when there is not: a desktop has no battery to draw, and the
/// profile is then the only thing this icon can tell anyone. Each half
/// failing is reported on its own, so UPower being down never hides a
/// working profile switch and never reads as "this is a desktop".
fn power_item(battery: &BatteryReading, profile: &ProfileReading) -> TrayItem {
    let item = |icon_name: String, tooltip_title: String, tooltip_body: String| TrayItem {
        id: POWER_ITEM_ID.to_string(),
        category: Category::Hardware,
        status: TrayStatus::Active,
        title: "Power".to_string(),
        icon_name,
        tooltip_title,
        tooltip_body,
    };
    let profile_words = match profile {
        ProfileReading::Known { active, .. } => Some(format!("{} profile", profile_label(*active))),
        ProfileReading::Unavailable => None,
    };

    match (battery, profile) {
        (BatteryReading::Present(info), _) => item(
            battery_icon(info),
            battery_words(info),
            // No profile sentence at all when it can't be read, rather
            // than a guess — the menu says why.
            profile_words.unwrap_or_default(),
        ),
        (BatteryReading::Absent, ProfileReading::Known { active, .. }) => item(
            profile_icon(*active).to_string(),
            profile_words.unwrap_or_default(),
            "No battery".to_string(),
        ),
        (BatteryReading::Unavailable, ProfileReading::Known { active, .. }) => item(
            profile_icon(*active).to_string(),
            profile_words.unwrap_or_default(),
            "Battery status unavailable — UPower isn't answering.".to_string(),
        ),
        // A desktop without power-profiles-daemon: nothing is broken
        // about the battery, but this icon has nothing left to show.
        (BatteryReading::Absent, ProfileReading::Unavailable) => item(
            "dialog-warning".to_string(),
            "Power profiles unavailable".to_string(),
            "power-profiles-daemon isn't running.".to_string(),
        ),
        (BatteryReading::Unavailable, ProfileReading::Unavailable) => item(
            "dialog-warning".to_string(),
            "Power status unavailable".to_string(),
            "Neither UPower nor power-profiles-daemon is answering.".to_string(),
        ),
    }
}

/// What the power menu should contain, from plain data — no D-Bus, no
/// I/O.
///
/// There is no toggle to put at the top, so the top row is the battery
/// reading, disabled: it is information, and the one thing people open
/// this menu to read. The profiles are the content — one checkmark row
/// per profile the daemon offers, the active one checked, and choosing
/// one switches to it.
fn power_menu(battery: &BatteryReading, profile: &ProfileReading) -> Menu {
    let settings_row = MenuItem::standard("Power settings…", "power:settings");

    let top = match battery {
        BatteryReading::Present(info) => MenuItem::disabled(format!("Battery {}", battery_words(info))),
        BatteryReading::Absent => MenuItem::disabled("Power profile"),
        BatteryReading::Unavailable => MenuItem::disabled("Battery status unavailable"),
    };

    let content = match profile {
        ProfileReading::Known { active, offered } => offered
            .iter()
            .map(|&p| {
                let action = format!("power:profile:{}", p.as_str());
                if p == *active {
                    MenuItem::checkmark(profile_label(p), true, action)
                } else {
                    MenuItem::standard(profile_label(p), action)
                }
            })
            .collect(),
        ProfileReading::Unavailable => vec![MenuItem::disabled(
            "Power profiles unavailable — power-profiles-daemon isn't running",
        )],
    };

    radio_menu(vec![top], content, settings_row)
}

const DISPLAYS_ITEM_ID: &str = "hyprforge-displays";

/// How many saved layouts the displays menu lists before truncating —
/// the same reasoning as [`MAX_NETWORKS_SHOWN`].
const MAX_DISPLAY_PROFILES_SHOWN: usize = 8;

/// One saved display layout, as `ListProfiles` reports it.
#[derive(Debug, Clone, PartialEq)]
struct DisplayProfile {
    id: String,
    name: String,
    /// RFC 3339, which is why sorting the strings sorts the times.
    last_used: String,
}

/// What `hyprforge-displayd` said this tick.
#[derive(Debug, Clone, PartialEq)]
enum DisplaysReading {
    Known {
        profiles: Vec<DisplayProfile>,
        /// The profile in effect, or `None` when none matches what is
        /// plugged in — a normal state until displayd learns the new
        /// set, not a failure.
        current: Option<String>,
    },
    Unavailable,
}

/// When displayd will roll a provisional layout back, if one is waiting
/// to be kept.
///
/// Written from two places: the signal watcher, which hears
/// `RevertPending`/`RevertResolved` however the apply was started (this
/// menu, the Settings app, `displayctl`), and this daemon's own menu
/// actions, so its icon changes the moment it is clicked rather than when
/// the signal arrives. Read by the poll loop. A deadline rather than a
/// flag so that a daemon which dies mid-countdown — and so never sends
/// `RevertResolved` — cannot leave the icon asking forever.
type RevertDeadline = Arc<std::sync::Mutex<Option<tokio::time::Instant>>>;

fn revert_pending(deadline: Option<tokio::time::Instant>, now: tokio::time::Instant) -> bool {
    deadline.is_some_and(|d| d > now)
}

/// What the displays icon should say, from plain data — no D-Bus, no I/O.
///
/// A layout waiting to be kept outranks everything else: it is the one
/// state here with a deadline, and missing it undoes what someone just
/// chose. It is the one item in this daemon that uses `NeedsAttention`,
/// with a name of its own, because the icon name is all a host is
/// obliged to draw. Nothing here is ever `Passive` — see the rule in
/// CLAUDE.md about an icon a host is asked to hide.
fn displays_item(reading: &DisplaysReading, pending: bool) -> TrayItem {
    let item = |status, icon: &str, tooltip_title: String, tooltip_body: String| TrayItem {
        id: DISPLAYS_ITEM_ID.to_string(),
        category: Category::Hardware,
        status,
        title: "Displays".to_string(),
        icon_name: icon.to_string(),
        tooltip_title,
        tooltip_body,
    };
    if pending {
        // No seconds in the text: this is rebuilt on the poll tick, so a
        // number would be up to a whole tick stale, counting down in
        // jumps. What matters is that it reverts on its own.
        return item(
            TrayStatus::NeedsAttention,
            "preferences-desktop-display-randr-symbolic",
            "Keep this display layout?".to_string(),
            "It reverts on its own unless you keep it.".to_string(),
        );
    }
    match reading {
        DisplaysReading::Unavailable => item(
            TrayStatus::Active,
            "dialog-warning",
            "Displays unavailable".to_string(),
            "hyprforge-displayd isn't running.".to_string(),
        ),
        DisplaysReading::Known { profiles, current } => {
            let saved = match profiles.len() {
                1 => "1 saved layout".to_string(),
                n => format!("{n} saved layouts"),
            };
            let current_name = current
                .as_ref()
                .and_then(|id| profiles.iter().find(|p| &p.id == id))
                .map(|p| p.name.clone());
            item(
                TrayStatus::Active,
                "monitor-symbolic",
                current_name.unwrap_or_else(|| "No saved layout matches".to_string()),
                saved,
            )
        }
    }
}

/// What the displays menu should contain, from plain data — no D-Bus, no
/// I/O.
///
/// While a layout waits to be kept, the menu is only Keep and Revert:
/// offering to switch to a third layout in the middle of confirming a
/// second is a way to lose track of which one is on trial. Otherwise it
/// lists saved layouts, most recently used first, the current one
/// checked — every one of them, as the Monitors screen offers Apply on
/// every one, since displayd fits a layout to whatever is connected.
fn displays_menu(reading: &DisplaysReading, pending: bool) -> Menu {
    let settings_row = MenuItem::standard("Display settings…", "display:settings");

    if pending {
        return radio_menu(
            vec![MenuItem::disabled("Keep this layout? It reverts on its own.")],
            vec![
                MenuItem::standard("Keep this layout", "display:keep"),
                MenuItem::standard("Revert now", "display:revert"),
            ],
            settings_row,
        );
    }

    let (profiles, current) = match reading {
        DisplaysReading::Unavailable => {
            return radio_menu(
                vec![MenuItem::disabled("Displays unavailable — hyprforge-displayd isn't running")],
                Vec::new(),
                settings_row,
            );
        }
        DisplaysReading::Known { profiles, current } => (profiles, current),
    };
    if profiles.is_empty() {
        return radio_menu(vec![MenuItem::disabled("No saved layouts yet")], Vec::new(), settings_row);
    }

    let mut ordered: Vec<&DisplayProfile> = profiles.iter().collect();
    ordered.sort_by(|a, b| b.last_used.cmp(&a.last_used));
    let mut content: Vec<MenuItem> = ordered
        .iter()
        .take(MAX_DISPLAY_PROFILES_SHOWN)
        .map(|p| {
            let action = format!("display:apply:{}", p.id);
            if current.as_deref() == Some(p.id.as_str()) {
                MenuItem::checkmark(p.name.clone(), true, action)
            } else {
                MenuItem::standard(p.name.clone(), action)
            }
        })
        .collect();
    if ordered.len() > MAX_DISPLAY_PROFILES_SHOWN {
        content.push(MenuItem::disabled(format!(
            "+{} more — see Settings",
            ordered.len() - MAX_DISPLAY_PROFILES_SHOWN
        )));
    }

    radio_menu(vec![MenuItem::disabled("Display layouts")], content, settings_row)
}

/// The typed operation behind an action string a menu click sends back.
///
/// Parsed in exactly one place ([`parse_menu_action`]) so every route a
/// click can take is visible in one match, and an unknown string — a
/// typo'd prefix, or a stale action from a menu revision that no longer
/// exists — resolves to nothing rather than to whichever arm happens to
/// match a prefix of it.
#[derive(Debug, Clone, PartialEq, Eq)]
enum MenuAction {
    /// Opens `hyprforge-settings --screen <screen>`.
    OpenSettings(&'static str),
    WifiRadio(bool),
    /// The SSID's raw bytes, decoded from the hex in the action string.
    WifiConnect(Vec<u8>),
    BtRadio(bool),
    BtConnect(Address),
    BtDisconnect(Address),
    /// `true` to take the inhibit, `false` to release it.
    KeepAwake(bool),
    /// Turns night light off (`hyprctl hyprsunset identity`).
    NightLightOff,
    /// Sets a specific colour temperature, turning night light on if it
    /// was off.
    NightLightSet(i64),
    /// Switches `power-profiles-daemon` to this profile.
    PowerProfile(PowerProfile),
    /// Brings a wired interface up, by its interface name.
    WiredConnect(String),
    WiredDisconnect(String),
    /// Applies a saved display layout, reversibly.
    DisplayApply(String),
    DisplayKeep,
    DisplayRevert,
}

/// A Linux interface name, as far as an action string can carry one:
/// non-empty, at most the kernel's fifteen bytes, no whitespace and no
/// `/` — the characters the kernel itself refuses.
fn interface_name(name: &str) -> Option<String> {
    let valid = !name.is_empty()
        && name.len() <= 15
        && !name.bytes().any(|b| b == b'/' || b.is_ascii_whitespace());
    valid.then(|| name.to_string())
}

fn parse_menu_action(action: &str) -> Option<MenuAction> {
    match action {
        "wifi:settings" => Some(MenuAction::OpenSettings("network")),
        "wifi:radio:on" => Some(MenuAction::WifiRadio(true)),
        "wifi:radio:off" => Some(MenuAction::WifiRadio(false)),
        "bt:settings" => Some(MenuAction::OpenSettings("bluetooth")),
        "bt:radio:on" => Some(MenuAction::BtRadio(true)),
        "bt:radio:off" => Some(MenuAction::BtRadio(false)),
        "keepawake:on" => Some(MenuAction::KeepAwake(true)),
        "keepawake:off" => Some(MenuAction::KeepAwake(false)),
        // Keep-awake moved off the Desktop screen's `Idle` tab and onto
        // its own Power screen — see this module's doc comment — so this
        // opens `--screen power`, not the `idle` stand-in it used to.
        "keepawake:settings" => Some(MenuAction::OpenSettings("power")),
        "nightlight:off" => Some(MenuAction::NightLightOff),
        "nightlight:settings" => Some(MenuAction::OpenSettings("night-light")),
        "power:settings" => Some(MenuAction::OpenSettings("power")),
        "display:settings" => Some(MenuAction::OpenSettings("displays")),
        "display:keep" => Some(MenuAction::DisplayKeep),
        "display:revert" => Some(MenuAction::DisplayRevert),
        _ => {
            if let Some(hex) = action.strip_prefix("wifi:connect:") {
                hex_decode(hex).map(MenuAction::WifiConnect)
            } else if let Some(addr) = action.strip_prefix("bt:connect:") {
                Some(MenuAction::BtConnect(Address::new(addr)))
            } else if let Some(addr) = action.strip_prefix("bt:disconnect:") {
                Some(MenuAction::BtDisconnect(Address::new(addr)))
            } else if let Some(interface) = action.strip_prefix("wired:connect:") {
                interface_name(interface).map(MenuAction::WiredConnect)
            } else if let Some(interface) = action.strip_prefix("wired:disconnect:") {
                interface_name(interface).map(MenuAction::WiredDisconnect)
            } else if let Some(id) = action.strip_prefix("display:apply:") {
                // A profile id is a blake3 hex digest. Anything else did
                // not come from a menu this daemon built.
                (!id.is_empty() && id.bytes().all(|b| b.is_ascii_alphanumeric()))
                    .then(|| MenuAction::DisplayApply(id.to_string()))
            } else if let Some(name) = action.strip_prefix("power:profile:") {
                // Through `PowerProfile::parse`, so a name the daemon has
                // never offered is ignored here rather than sent to it.
                PowerProfile::parse(name).map(MenuAction::PowerProfile)
            } else {
                action
                    .strip_prefix("nightlight:set:")
                    .and_then(|kelvin| kelvin.parse::<i64>().ok())
                    .map(MenuAction::NightLightSet)
            }
        }
    }
}

/// Receives menu clicks and carries out the operation behind each one.
///
/// Runs on its own task, never on the D-Bus dispatch path — `sni.rs`'s
/// `context_menu` already spawns `hyprforge_tray::launch::show` onto its
/// own task and returns, which is what makes that safe to do here: a slow
/// or hanging backend call blocks this loop, not the host waiting on the
/// D-Bus method call that triggered the click in the first place.
/// Keeps its own [`Reconnecting`] state, independent of `poll_loop`'s —
/// a second connection to NetworkManager or BlueZ is unremarkable, and
/// sharing one would need a lock held across every backend call this loop
/// makes, for no benefit worth that cost.
/// Asks NetworkManager to scan, then has the menu rebuilt once the
/// results have had time to land.
///
/// The rebuild is spawned rather than awaited: this runs on the task that
/// reads menu events, and sleeping here would stall every click that
/// arrived during the wait.
async fn scan_then_refresh(
    net_state: &mut Reconnecting<hyprforge_network::NetworkManagerBackend>,
    refresh: tokio::sync::mpsc::UnboundedSender<()>,
) {
    let Some(backend) = network_backend(net_state).await else {
        return;
    };
    if let Err(e) = backend.request_scan().await {
        // Not worth a warning at every menu open on a machine with no
        // Wi-Fi; the menu simply shows what is already known.
        tracing::debug!(error = %e, "scan on menu open was refused");
        return;
    }
    tokio::spawn(async move {
        tokio::time::sleep(SCAN_SETTLE).await;
        let _ = refresh.send(());
    });
}

/// How long to wait after asking for a scan before rebuilding the menu.
///
/// NetworkManager answers `RequestScan` immediately and finds access
/// points over the next few seconds, so refreshing straight away would
/// rebuild from the same stale list. This is long enough for results to
/// land and short enough that the menu is fresh by the time someone
/// closes it and opens it again.
/// The ids the two items register under. `TrayItem::activate_screen`
/// matches on these too, so they are not free-form strings.
const NETWORK_ITEM_ID: &str = "hyprforge-network";

const SCAN_SETTLE: std::time::Duration = std::time::Duration::from_secs(3);

/// The menu-click loop's own connections, one per service — kept apart
/// from `poll_loop`'s for the reason on [`handle_menu_clicks`].
struct MenuClickState {
    net: Reconnecting<NetworkManagerBackend>,
    bt: Reconnecting<BlueZBackend>,
    logind: Reconnecting<DetachedBackend>,
    profiles: Reconnecting<PowerProfilesDaemonBackend>,
    bus: Reconnecting<zbus::Connection>,
}

async fn handle_menu_clicks(
    mut actions: tokio::sync::mpsc::UnboundedReceiver<String>,
    refresh: tokio::sync::mpsc::UnboundedSender<()>,
    night_light_belief: NightLightBelief,
    revert_deadline: RevertDeadline,
) {
    let mut state = MenuClickState {
        net: Reconnecting::new(),
        bt: Reconnecting::new(),
        logind: Reconnecting::new(),
        profiles: Reconnecting::new(),
        bus: Reconnecting::new(),
    };
    while let Some(action) = actions.recv().await {
        // A menu being opened is an event, not a click, and only the
        // Wi-Fi one wants anything done about it.
        if let Some(id) = action.strip_prefix(hyprforge_tray::OPENED_PREFIX) {
            if id == NETWORK_ITEM_ID {
                scan_then_refresh(&mut state.net, refresh.clone()).await;
            }
            // Bluetooth deliberately does nothing: starting discovery
            // costs battery on every device in range, and a popup opening
            // is not someone asking for that. Keep awake and night light
            // have nothing analogous to refresh on open either.
            continue;
        }
        let Some(parsed) = parse_menu_action(&action) else {
            tracing::warn!(action = %action, "unknown tray menu action; ignoring");
            continue;
        };
        // A profile switch pulls the next poll forward, so the icon and
        // the menu's checkmark follow the click rather than lagging it by
        // up to a whole `POLL_INTERVAL`.
        // Display actions refresh on their own, through the revert
        // deadline — see `set_revert_deadline`.
        let refresh_after = matches!(
            parsed,
            MenuAction::PowerProfile(_) | MenuAction::WiredConnect(_) | MenuAction::WiredDisconnect(_)
        );
        match perform_menu_action(parsed, &mut state, &night_light_belief, &revert_deadline, &refresh)
            .await
        {
            Ok(()) if refresh_after => {
                let _ = refresh.send(());
            }
            Ok(()) => {}
            Err(e) => tracing::warn!(error = %e, "tray menu action failed"),
        }
    }
}

async fn perform_menu_action(
    action: MenuAction,
    state: &mut MenuClickState,
    night_light_belief: &NightLightBelief,
    revert_deadline: &RevertDeadline,
    refresh: &tokio::sync::mpsc::UnboundedSender<()>,
) -> anyhow::Result<()> {
    match action {
        MenuAction::OpenSettings(screen) => {
            spawn_settings(screen);
            Ok(())
        }
        MenuAction::WifiRadio(on) => {
            let backend = network_backend(&mut state.net)
                .await
                .ok_or_else(|| anyhow::anyhow!("NetworkManager unavailable"))?;
            backend.set_radio(on).await?;
            Ok(())
        }
        MenuAction::WifiConnect(ssid_bytes) => {
            let backend = network_backend(&mut state.net)
                .await
                .ok_or_else(|| anyhow::anyhow!("NetworkManager unavailable"))?;
            let ssid = Ssid::new(ssid_bytes);
            let points = backend.access_points().await?;
            let Some(ap) = points.into_iter().find(|ap| ap.ssid == ssid) else {
                // Out of range since the menu was built, most likely.
                anyhow::bail!("network no longer in range");
            };
            if ap.security.needs_passphrase() {
                let saved = backend.saved_networks().await.unwrap_or_default();
                let Some(saved) = saved.iter().find(|s| s.ssid == ap.ssid) else {
                    // The menu does not offer a `wifi:connect:` for this
                    // case, so reaching it means the network changed out
                    // from under a click already in flight. A tray menu
                    // has nowhere to type a passphrase, so hand it to the
                    // screen that has.
                    spawn_settings("network");
                    return Ok(());
                };
                // `connect` *adds* a connection and needs a passphrase to
                // build one with; it refuses without one. Rejoining a
                // known network is a different operation, on the secret
                // NetworkManager already stores — which is the only way
                // this can work at all, since the daemon never sees a
                // passphrase and must not.
                backend.connect_saved(&saved.id).await?;
                return Ok(());
            }
            // Open and OWE: nothing to type, nothing stored.
            backend.connect(&ap, None).await?;
            Ok(())
        }
        MenuAction::BtRadio(on) => {
            let backend = bluetooth_backend(&mut state.bt)
                .await
                .ok_or_else(|| anyhow::anyhow!("BlueZ unavailable"))?;
            backend.set_powered(on).await?;
            Ok(())
        }
        MenuAction::BtConnect(address) => {
            let backend = bluetooth_backend(&mut state.bt)
                .await
                .ok_or_else(|| anyhow::anyhow!("BlueZ unavailable"))?;
            backend.connect(&address).await?;
            Ok(())
        }
        MenuAction::BtDisconnect(address) => {
            let backend = bluetooth_backend(&mut state.bt)
                .await
                .ok_or_else(|| anyhow::anyhow!("BlueZ unavailable"))?;
            backend.disconnect(&address).await?;
            Ok(())
        }
        MenuAction::KeepAwake(on) => {
            let backend = keep_awake_backend(&mut state.logind)
                .await
                .ok_or_else(|| anyhow::anyhow!("systemd-logind unavailable"))?;
            if on {
                backend.take(WhatSet::keep_awake(), KEEP_AWAKE_WHO, KEEP_AWAKE_WHY).await?;
            } else {
                backend.release().await?;
            }
            Ok(())
        }
        MenuAction::NightLightOff => {
            // `hyprctl`/`pgrep` are both synchronous and bounded (see
            // `command::output`'s own timeout) — `spawn_blocking` keeps a
            // slow one from stalling the runtime, not from running
            // forever; nothing here waits without a bound of its own.
            tokio::task::spawn_blocking(|| Hyprsunset.turn_off()).await??;
            *night_light_belief.lock().unwrap_or_else(|e| e.into_inner()) = false;
            Ok(())
        }
        MenuAction::NightLightSet(kelvin) => {
            tokio::task::spawn_blocking(move || Hyprsunset.set_temperature(kelvin)).await??;
            *night_light_belief.lock().unwrap_or_else(|e| e.into_inner()) = true;
            Ok(())
        }
        MenuAction::PowerProfile(profile) => {
            let backend = profiles_backend(&mut state.profiles)
                .await
                .ok_or_else(|| anyhow::anyhow!("power-profiles-daemon unavailable"))?;
            backend.set_active_profile(profile).await?;
            Ok(())
        }
        MenuAction::WiredConnect(interface) => {
            let backend = network_backend(&mut state.net)
                .await
                .ok_or_else(|| anyhow::anyhow!("NetworkManager unavailable"))?;
            backend.wired_connect(&interface).await?;
            Ok(())
        }
        MenuAction::WiredDisconnect(interface) => {
            let backend = network_backend(&mut state.net)
                .await
                .ok_or_else(|| anyhow::anyhow!("NetworkManager unavailable"))?;
            backend.wired_disconnect(&interface).await?;
            Ok(())
        }
        MenuAction::DisplayApply(id) => {
            let conn = session_bus(&mut state.bus)
                .await
                .ok_or_else(|| anyhow::anyhow!("session bus unavailable"))?;
            let proxy = displayd_bounded(
                "connecting to displayd",
                DISPLAYD_CALL_TIMEOUT,
                DisplaydProxy::new(&conn),
            )
            .await?;
            // Always the reversible apply, the one the Monitors screen
            // uses: a layout chosen from a menu can blank the screen the
            // menu was on, and the countdown is what brings it back.
            let seconds = displayd_bounded(
                "applying the layout",
                DISPLAYD_APPLY_TIMEOUT,
                proxy.apply_profile_reversible(&id),
            )
            .await?;
            set_revert_deadline(revert_deadline, seconds, refresh);
            Ok(())
        }
        MenuAction::DisplayKeep => {
            let conn = session_bus(&mut state.bus)
                .await
                .ok_or_else(|| anyhow::anyhow!("session bus unavailable"))?;
            let proxy = displayd_bounded(
                "connecting to displayd",
                DISPLAYD_CALL_TIMEOUT,
                DisplaydProxy::new(&conn),
            )
            .await?;
            displayd_bounded("keeping the layout", DISPLAYD_CALL_TIMEOUT, proxy.confirm_layout())
                .await?;
            clear_revert_deadline(revert_deadline, refresh);
            Ok(())
        }
        MenuAction::DisplayRevert => {
            let conn = session_bus(&mut state.bus)
                .await
                .ok_or_else(|| anyhow::anyhow!("session bus unavailable"))?;
            let proxy = displayd_bounded(
                "connecting to displayd",
                DISPLAYD_CALL_TIMEOUT,
                DisplaydProxy::new(&conn),
            )
            .await?;
            displayd_bounded("reverting the layout", DISPLAYD_APPLY_TIMEOUT, proxy.revert_layout())
                .await?;
            clear_revert_deadline(revert_deadline, refresh);
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use hyprforge_bluetooth::DeviceKind;
    use hyprforge_tray::menu::ItemKind;

    // The exact freedesktop names this daemon is allowed to hand to a
    // tray host. Not every name the doc comment lists has to appear here
    // — only every name the functions below can actually *return* has to
    // be a member — which is what catches an invented name before it
    // ships: an unresolvable icon name renders as a blank gap, with
    // nothing logged anywhere to say why.
    const NETWORK_ICON_NAMES: &[&str] = &[
        "network-wireless-signal-excellent-symbolic",
        "network-wireless-signal-good-symbolic",
        "network-wireless-signal-ok-symbolic",
        "network-wireless-signal-weak-symbolic",
        "network-wireless-signal-none-symbolic",
        "network-wireless-disconnected-symbolic",
        "network-wired-activated-symbolic",
        "network-wired-symbolic",
        "network-wired-disconnected-symbolic",
        "network-wired-unavailable-symbolic",
        "dialog-warning",
    ];
    const BLUETOOTH_ICON_NAMES: &[&str] = &[
        "network-bluetooth-activated-symbolic",
        "network-bluetooth-symbolic",
        "network-bluetooth-inactive-symbolic",
        "dialog-warning",
    ];
    // Every name in this file is resolved against a real installed icon
    // theme by `live_icons.rs`, because an unresolvable name is the
    // quietest failure in this daemon: the bar draws a blank gap, and
    // nothing logs anything at any layer.
    //
    // All four items now draw from the same family on purpose: a
    // `-symbolic` name under `status/` in KDE's Breeze icon set, which
    // this machine's configured theme (Dracula) actually reaches —
    // `Dracula`'s `Inherits=` line names `Papirus-Dark`, `breeze-dark`,
    // `Zafiro`, `ubuntu-mono-dark`, `Mint-X`, `elementary` and `gnome`,
    // but only `breeze-dark` (and, through it, `breeze`) is actually
    // installed here. Adwaita is not in that chain at all, no matter how
    // standard a name of its sounds — it was the wrong place to look for
    // any of these four, which is why picking a name because it "sounds
    // right" is exactly the mistake this whole file's live-checked
    // approach exists to catch.
    //
    // Before this, Wi-Fi alone used the plain (full-colour) name —
    // `network-wireless-signal-*` rather than `-*-symbolic` — which is
    // the one colourful icon the rest of this tray's monochrome row was
    // complained about. It is now `-symbolic` throughout, matching
    // Bluetooth's `network-bluetooth-*-symbolic`, which was already
    // right.
    //
    // `redshift-status-on`/`-off` reads better as GNOME's
    // `night-light-symbolic`, but that name exists only in Adwaita and
    // is unreachable from this chain, exactly like the Adwaita names
    // above; `redshift-status-*-symbolic` is a real file (a symlink to
    // the plain SVG, itself already monochrome enough that Breeze never
    // bothered to draw a separate symbolic version) in both `breeze` and
    // `breeze-dark`, so it is what stays.
    //
    // `changes-prevent-symbolic`/`changes-allow-symbolic` needed no
    // change — already symbolic, already present in `breeze-dark`.
    //
    // `dialog-warning`/`dialog-information` are the shared error
    // fallback all four items use identically when their backend is
    // unavailable, so a full-colour dialog icon appearing on top of an
    // otherwise-symbolic row is not the same defect as one item's normal
    // icon permanently differing from the other three's — it is a rare,
    // uniform, attention-getting state, and Breeze (like hicolor, the
    // end of this chain) ships no `-symbolic` variant of either to
    // switch to. Adwaita has both — `symbolic/status/dialog-warning-
    // symbolic.svg` — but is unreachable here, so naming it would trade
    // a real icon for a blank gap. Left as-is.
    const KEEP_AWAKE_ICON_NAMES: &[&str] =
        &["changes-prevent-symbolic", "changes-allow-symbolic", "dialog-warning"];
    const NIGHT_LIGHT_ICON_NAMES: &[&str] = &[
        "redshift-status-on-symbolic",
        "redshift-status-off-symbolic",
        "dialog-warning",
        "dialog-information",
    ];

    fn net_status(radio: RadioState, connected: Option<&str>) -> NetStatus {
        NetStatus { radio, connected_to: connected.map(Ssid::new) }
    }

    fn bt_status(state: AdapterState) -> BtStatus {
        BtStatus { state, discovering: false, alias: "mock-adapter".to_string() }
    }

    fn device(alias: &str, connected: bool) -> Device {
        Device {
            address: hyprforge_bluetooth::Address::new("AA:BB:CC:DD:EE:FF"),
            alias: alias.to_string(),
            name: Some(alias.to_string()),
            kind: DeviceKind::Headset,
            paired: true,
            trusted: true,
            connected,
            rssi: Some(-50),
        }
    }

    // --- radio off vs on-but-unconnected -----------------------------

    /// A radio switched off keeps its icon. It used to be `Passive`,
    /// which asks the host to hide it — so the icon disappeared at
    /// exactly the moment its own menu could have switched the radio
    /// back on, and the only way back was the Settings app.
    #[test]
    fn a_switched_off_wifi_radio_still_shows_an_icon() {
        let off = network_item(Some(&net_status(RadioState::Off, None)), None, None, false);
        assert_eq!(off.status, TrayStatus::Active);

        let hw_off =
            network_item(Some(&net_status(RadioState::HardwareOff, None)), None, None, false);
        assert_eq!(hw_off.status, TrayStatus::Active, "rfkill hides the icon no more than a software switch does");

        let on_unconnected = network_item(Some(&net_status(RadioState::On, None)), None, None, false);
        assert_eq!(on_unconnected.status, TrayStatus::Active);
    }

    #[test]
    fn a_switched_off_bluetooth_adapter_still_shows_an_icon() {
        let off = bluetooth_item(Some(&bt_status(AdapterState::Off)), None, false);
        assert_eq!(off.status, TrayStatus::Active);

        let blocked = bluetooth_item(Some(&bt_status(AdapterState::HardwareBlocked)), None, false);
        assert_eq!(blocked.status, TrayStatus::Active);

        let on_unconnected = bluetooth_item(Some(&bt_status(AdapterState::On)), None, false);
        assert_eq!(on_unconnected.status, TrayStatus::Active);
    }

    /// The property that makes the change above worth having. A
    /// StatusNotifierItem has no opacity and no sensitivity flag — the
    /// icon *name* is the entire vocabulary for "this is off" — so if
    /// off and on-but-idle shared a name, keeping both visible would
    /// show the user the same picture for the two states with the most
    /// different things to do about them. While off was hidden they
    /// could share one; now they cannot.
    #[test]
    fn a_radio_that_is_off_does_not_look_like_one_that_is_on_and_idle() {
        let wifi_off = network_item(Some(&net_status(RadioState::Off, None)), None, None, false);
        let wifi_idle = network_item(Some(&net_status(RadioState::On, None)), None, None, false);
        assert_ne!(wifi_off.icon_name, wifi_idle.icon_name, "off and searching must be distinguishable");

        let bt_off = bluetooth_item(Some(&bt_status(AdapterState::Off)), None, false);
        let bt_idle = bluetooth_item(Some(&bt_status(AdapterState::On)), None, false);
        let bt_connected = bluetooth_item(Some(&bt_status(AdapterState::On)), Some(&device("Headset", true)), false);
        assert_ne!(bt_off.icon_name, bt_idle.icon_name);
        assert_ne!(bt_idle.icon_name, bt_connected.icon_name, "idle and connected are three states, not two");
    }

    /// Nothing in this daemon asks a host to hide an icon any more. Every
    /// item it can build is one the user switched on in `tray.toml`, and
    /// a host hiding it is a control they cannot reach — the reasoning
    /// `keep_awake_item` has carried since it was written, now true of
    /// all four.
    #[test]
    fn no_item_this_daemon_builds_ever_asks_to_be_hidden() {
        let items = [
            network_item(Some(&net_status(RadioState::Off, None)), None, None, false),
            network_item(Some(&net_status(RadioState::HardwareOff, None)), None, None, false),
            network_item(Some(&net_status(RadioState::On, None)), None, None, false),
            network_item(None, None, None, false),
            bluetooth_item(Some(&bt_status(AdapterState::Off)), None, false),
            bluetooth_item(Some(&bt_status(AdapterState::HardwareBlocked)), None, false),
            bluetooth_item(Some(&bt_status(AdapterState::On)), None, false),
            bluetooth_item(None, None, false),
            night_light_item(&NightLightState::Known { temperature: 3500, on: false }),
            night_light_item(&NightLightState::Known { temperature: 3500, on: true }),
            night_light_item(&NightLightState::NotRunning),
            keep_awake_item(Some(false), &[], false),
            keep_awake_item(Some(true), &[], false),
            keep_awake_item(None, &[], true),
            power_item(&BatteryReading::Present(battery(40, BatteryState::Discharging)), &balanced()),
            power_item(&BatteryReading::Absent, &balanced()),
            power_item(&BatteryReading::Absent, &ProfileReading::Unavailable),
            power_item(&BatteryReading::Unavailable, &ProfileReading::Unavailable),
            displays_item(&DisplaysReading::Unavailable, false),
            displays_item(&desk_and_laptop(), false),
        ];
        for item in items {
            assert_eq!(item.status, TrayStatus::Active, "{} asked to be hidden", item.id);
        }
    }

    // --- signal-strength boundaries ------------------------------------

    #[test]
    fn signal_strength_maps_to_the_right_icon_at_its_boundaries() {
        let cases: &[(u8, &str)] = &[
            (100, "network-wireless-signal-excellent-symbolic"),
            (80, "network-wireless-signal-excellent-symbolic"),
            (79, "network-wireless-signal-good-symbolic"),
            (60, "network-wireless-signal-good-symbolic"),
            (59, "network-wireless-signal-ok-symbolic"),
            (40, "network-wireless-signal-ok-symbolic"),
            (39, "network-wireless-signal-weak-symbolic"),
            (20, "network-wireless-signal-weak-symbolic"),
            (19, "network-wireless-signal-none-symbolic"),
            (0, "network-wireless-signal-none-symbolic"),
        ];
        for &(strength, expected) in cases {
            assert_eq!(
                wifi_signal_icon(strength),
                expected,
                "strength {strength} should map to {expected}"
            );
        }
    }

    // --- unavailable daemons say so, distinctly from "nothing connected" --

    #[test]
    fn an_unavailable_network_daemon_produces_an_icon_and_tooltip_that_say_so() {
        let item = network_item(None, None, None, true);
        assert_eq!(item.status, TrayStatus::Active, "must stay visible to be seen at all");
        assert_eq!(item.icon_name, "dialog-warning");
        assert!(item.tooltip_title.to_lowercase().contains("unavailable"));

        let nothing_connected = network_item(Some(&net_status(RadioState::On, None)), None, None, false);
        assert_ne!(
            item.tooltip_title, nothing_connected.tooltip_title,
            "unavailable and merely unconnected must not read the same"
        );
        assert_ne!(item.icon_name, nothing_connected.icon_name);
    }

    #[test]
    fn a_missing_status_reads_the_same_as_an_explicit_unavailable_flag() {
        // Defensive: a caller that forgets to set `unavailable` but has
        // no status at all (nothing was ever polled) must not fall
        // through to a default that looks like "off" or "connected".
        let item = network_item(None, None, None, false);
        assert!(item.tooltip_title.to_lowercase().contains("unavailable"));
    }

    #[test]
    fn an_unavailable_bluetooth_daemon_produces_an_icon_and_tooltip_that_say_so() {
        let item = bluetooth_item(None, None, true);
        assert_eq!(item.status, TrayStatus::Active);
        assert_eq!(item.icon_name, "dialog-warning");
        assert!(item.tooltip_title.to_lowercase().contains("unavailable"));

        let nothing_connected = bluetooth_item(Some(&bt_status(AdapterState::On)), None, false);
        assert_ne!(item.tooltip_title, nothing_connected.tooltip_title);
        assert_ne!(item.icon_name, nothing_connected.icon_name);
    }

    // --- a connected network's tooltip names it ------------------------

    #[test]
    fn a_connected_networks_tooltip_names_it() {
        let ssid = Ssid::new("Coffee Shop");
        let item = network_item(
            Some(&net_status(RadioState::On, Some("Coffee Shop"))),
            Some(&ssid),
            Some(72),
            false,
        );
        assert_eq!(item.tooltip_title, "Coffee Shop");
        assert!(item.tooltip_body.contains("72"));
        assert_eq!(item.icon_name, "network-wireless-signal-good-symbolic");
    }

    #[test]
    fn a_connected_bluetooth_devices_tooltip_names_it() {
        let d = device("WH-1000XM4", true);
        let item = bluetooth_item(Some(&bt_status(AdapterState::On)), Some(&d), false);
        assert_eq!(item.tooltip_title, "WH-1000XM4");
        assert_eq!(item.tooltip_body, "Headset");
    }

    // --- no invented icon names ------------------------------------------

    #[test]
    fn every_network_icon_the_function_can_return_is_a_documented_freedesktop_name() {
        let ssid = Ssid::new("home");
        let items = [
            network_item(None, None, None, true),
            network_item(Some(&net_status(RadioState::Off, None)), None, None, false),
            network_item(Some(&net_status(RadioState::HardwareOff, None)), None, None, false),
            network_item(Some(&net_status(RadioState::On, None)), None, None, false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(0), false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(19), false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(20), false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(39), false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(40), false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(59), false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(60), false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(79), false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(80), false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), Some(100), false),
            network_item(Some(&net_status(RadioState::On, Some("home"))), Some(&ssid), None, false),
        ];
        for item in items {
            assert!(
                NETWORK_ICON_NAMES.contains(&item.icon_name.as_str()),
                "invented icon name: {}",
                item.icon_name
            );
        }
    }

    #[test]
    fn every_bluetooth_icon_the_function_can_return_is_a_documented_freedesktop_name() {
        let connected = device("Headset", true);
        let items = [
            bluetooth_item(None, None, true),
            bluetooth_item(Some(&bt_status(AdapterState::Off)), None, false),
            bluetooth_item(Some(&bt_status(AdapterState::HardwareBlocked)), None, false),
            bluetooth_item(Some(&bt_status(AdapterState::On)), None, false),
            bluetooth_item(Some(&bt_status(AdapterState::Changing)), None, false),
            bluetooth_item(Some(&bt_status(AdapterState::On)), Some(&connected), false),
        ];
        for item in items {
            assert!(
                BLUETOOTH_ICON_NAMES.contains(&item.icon_name.as_str()),
                "invented icon name: {}",
                item.icon_name
            );
        }
    }

    // --- click-to-screen mapping, with no process spawned ----------------

    #[test]
    fn a_click_on_either_icon_maps_to_its_own_settings_screen() {
        let network = network_item(Some(&net_status(RadioState::On, None)), None, None, false);
        assert_eq!(network.activate_screen(), Some("network"));

        let bluetooth = bluetooth_item(Some(&bt_status(AdapterState::On)), None, false);
        assert_eq!(bluetooth.activate_screen(), Some("bluetooth"));
    }

    // --- Reconnecting never caches a failure -----------------------------

    #[tokio::test]
    async fn a_failed_connect_is_retried_on_the_next_tick_rather_than_cached() {
        use std::sync::atomic::{AtomicUsize, Ordering};

        let attempts = AtomicUsize::new(0);
        let mut state: Reconnecting<u32> = Reconnecting::new();

        // First tick: the backend isn't there yet.
        let first = state
            .get_or_connect(|| async {
                attempts.fetch_add(1, Ordering::SeqCst);
                Err::<u32, &'static str>("not running")
            })
            .await;
        assert!(first.is_err());
        assert!(
            state.backend.is_none(),
            "a failed connect must leave nothing cached, or a later tick would never try again"
        );

        // Second tick: it comes up. This must actually run the connect
        // closure again — a cached-failure design (`Option<Result<..>>`)
        // would skip straight to returning the same error without ever
        // calling it.
        let second = state
            .get_or_connect(|| async {
                attempts.fetch_add(1, Ordering::SeqCst);
                Ok::<u32, &'static str>(42)
            })
            .await;
        assert_eq!(*second.unwrap(), 42);
        assert_eq!(attempts.load(Ordering::SeqCst), 2, "both ticks must have called connect");

        // Third tick: a successful connect *is* cached, so this must not
        // call connect again.
        let calls_before_third = attempts.load(Ordering::SeqCst);
        let third = state
            .get_or_connect(|| async {
                attempts.fetch_add(1, Ordering::SeqCst);
                Ok::<u32, &'static str>(99)
            })
            .await;
        assert_eq!(*third.unwrap(), 42, "a cached success is reused, not replaced");
        assert_eq!(
            attempts.load(Ordering::SeqCst),
            calls_before_third,
            "a cached success must not reconnect"
        );
    }

    // --- icon_action: the pure on/off decision ---------------------------

    #[test]
    fn a_wanted_icon_that_is_not_up_yet_should_be_registered() {
        assert_eq!(icon_action(true, false), IconAction::Register);
    }

    #[test]
    fn an_unwanted_icon_that_is_currently_up_should_be_dropped_not_left_registered() {
        assert_eq!(icon_action(false, true), IconAction::Drop);
    }

    #[test]
    fn an_icon_already_matching_the_preference_is_left_alone_either_way() {
        assert_eq!(icon_action(true, true), IconAction::Keep);
        assert_eq!(icon_action(false, false), IconAction::Keep);
    }

    /// The one pair that needs no backend call at all — and the pair a
    /// stock install is in for two of the four icons, since keep-awake
    /// and night light default to off.
    ///
    /// Sampling those meant a logind connection and a `ListInhibitors`,
    /// plus a `pgrep` and an `hyprctl` subprocess, every poll interval
    /// forever, to build an item that `icon_action` then threw away.
    #[test]
    fn an_icon_switched_off_and_not_showing_is_never_sampled() {
        assert!(!needs_sampling(false, false));
    }

    /// Every pair `icon_action` does something for is still sampled —
    /// including the tick that drops an icon just switched off, which
    /// is why this reads the same two booleans rather than guessing
    /// from the action.
    #[test]
    fn every_icon_that_is_shown_or_about_to_change_is_still_sampled() {
        assert!(needs_sampling(true, false), "about to be registered");
        assert!(needs_sampling(true, true), "showing, and still wanted");
        assert!(needs_sampling(false, true), "showing, and about to be dropped");
    }

    // --- refresh_prefs: a bad file never changes what's already shown ----

    /// Serialises every test in this module that repoints
    /// `$XDG_CONFIG_HOME` — it's process-global, so two such tests running
    /// concurrently could read each other's temp directory. Same reasoning
    /// as `hyprforge-settings::modules::CONFIG_ENV_LOCK`.
    static CONFIG_ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    fn with_temp_config_home<T>(f: impl FnOnce(&std::path::Path) -> T) -> T {
        let _lock = CONFIG_ENV_LOCK.lock().unwrap_or_else(|e| e.into_inner());
        let dir = tempfile::tempdir().unwrap();
        let previous = std::env::var_os("XDG_CONFIG_HOME");
        unsafe {
            std::env::set_var("XDG_CONFIG_HOME", dir.path());
        }
        let out = f(dir.path());
        match previous {
            Some(p) => unsafe { std::env::set_var("XDG_CONFIG_HOME", p) },
            None => unsafe { std::env::remove_var("XDG_CONFIG_HOME") },
        }
        out
    }

    #[test]
    fn a_missing_tray_toml_refreshes_to_the_defaults() {
        with_temp_config_home(|_dir| {
            let mut current = Prefs { network: false, bluetooth: false, keep_awake: false, night_light: false, power: false, displays: false, menu_y_offset: 32, menu_closes_on_click_outside: true };
            let mut warned = false;
            refresh_prefs(&mut current, &mut warned);
            assert!(current.network, "a missing file is first run: both icons shown");
            assert!(current.bluetooth);
            assert!(!warned);
        });
    }

    /// The property this whole feature is built around: a `tray.toml` a
    /// typo has broken must not be treated as "hide everything" (or
    /// "show everything") — whatever was already showing keeps showing.
    #[test]
    fn an_unreadable_tray_toml_leaves_the_current_preferences_untouched() {
        with_temp_config_home(|dir| {
            let tray_toml = dir.join("hyprforge").join("tray.toml");
            std::fs::create_dir_all(tray_toml.parent().unwrap()).unwrap();
            std::fs::write(&tray_toml, "network = yes please\n").unwrap();

            let mut current = Prefs { network: true, bluetooth: false, keep_awake: false, night_light: false, power: false, displays: false, menu_y_offset: 32, menu_closes_on_click_outside: true };
            let mut warned = false;
            refresh_prefs(&mut current, &mut warned);
            assert_eq!(
                current,
                Prefs { network: true, bluetooth: false, keep_awake: false, night_light: false, power: false, displays: false, menu_y_offset: 32, menu_closes_on_click_outside: true },
                "a failed read must not change what is currently shown"
            );
            assert!(warned, "the failure is reported");
        });
    }

    /// A persistent parse error must warn once, not once per tick — a
    /// daemon that logs the same line every ten seconds forever makes the
    /// one thing worth noticing (a *new* failure) impossible to spot.
    #[test]
    fn a_persistent_parse_failure_warns_only_once() {
        with_temp_config_home(|dir| {
            let tray_toml = dir.join("hyprforge").join("tray.toml");
            std::fs::create_dir_all(tray_toml.parent().unwrap()).unwrap();
            std::fs::write(&tray_toml, "network = yes please\n").unwrap();

            let mut current = Prefs::default();
            let mut warned = false;
            refresh_prefs(&mut current, &mut warned);
            assert!(warned);

            // A second tick against the same broken file must not flip
            // `warned` back to logging again.
            let warned_before = warned;
            refresh_prefs(&mut current, &mut warned);
            assert_eq!(warned, warned_before, "still warned, but not re-logged");

            // Once the file is fixed, a later failure must be able to
            // warn again — `warned` is reset on success, not stuck `true`
            // forever.
            std::fs::write(&tray_toml, "network = false\n").unwrap();
            refresh_prefs(&mut current, &mut warned);
            assert!(!warned, "a successful read clears the suppression");
        });
    }

    // --- menu content: network_menu / bluetooth_menu ---------------------

    use hyprforge_network::Security;

    fn access_point(ssid: &str, security: Security) -> AccessPoint {
        AccessPoint {
            ssid: Ssid::new(ssid),
            bssid: "00:11:22:33:44:55".to_string(),
            strength: 70,
            frequency_mhz: 5180,
            security,
        }
    }

    fn saved(ssid: &str) -> SavedNetwork {
        SavedNetwork { ssid: Ssid::new(ssid), id: format!("conn-{ssid}"), autoconnect: true }
    }

    fn bt_device(alias: &str, paired: bool, connected: bool) -> Device {
        Device {
            address: Address::new("AA:BB:CC:DD:EE:FF"),
            alias: alias.to_string(),
            name: Some(alias.to_string()),
            kind: DeviceKind::Headset,
            paired,
            trusted: paired,
            connected,
            rssi: Some(-50),
        }
    }

    #[test]
    fn the_connected_network_appears_as_a_checked_row() {
        let ssid = Ssid::new("home");
        let menu = network_menu(
            Some(&net_status(RadioState::On, Some("home"))),
            Some(&ssid),
            false,
            &[access_point("home", Security::Wpa2Personal), access_point("cafe", Security::Open)],
            &[saved("home")],
        );
        let row = menu.flatten().into_iter().find(|i| i.label == "home").unwrap();
        assert_eq!(row.kind, ItemKind::Checkmark);
        assert_eq!(row.toggle, Some(true));

        let other = menu.flatten().into_iter().find(|i| i.label == "cafe").unwrap();
        assert_ne!(other.toggle, Some(true));
    }

    #[test]
    fn an_enterprise_network_is_listed_but_disabled_and_says_why() {
        let menu = network_menu(
            Some(&net_status(RadioState::On, None)),
            None,
            false,
            &[access_point("corp-wifi", Security::Enterprise)],
            &[],
        );
        let row = menu.flatten().into_iter().find(|i| i.label.contains("corp-wifi")).unwrap();
        assert!(!row.enabled);
        assert!(row.action.is_none());
        assert!(
            row.label.to_lowercase().contains("enterprise"),
            "row must say why it can't be joined: {}",
            row.label
        );
    }

    #[test]
    fn an_unpaired_device_is_listed_and_hands_pairing_to_the_screen_that_can_do_it() {
        let menu = bluetooth_menu(
            Some(&bt_status(AdapterState::On)),
            false,
            &[bt_device("New Headphones", false, false)],
        );
        let row = menu
            .flatten()
            .into_iter()
            .find(|i| i.label.contains("New Headphones"))
            .expect("an unpaired device is still listed");

        // It was disabled once, when nothing could pair at all. Now
        // something can — just not a menu, which has nowhere to show six
        // digits and ask whether they match.
        assert!(row.enabled, "a row that can do something must be clickable");
        assert_eq!(
            row.action.as_deref(),
            Some("bt:settings"),
            "pairing is handed to the screen, never attempted from the menu"
        );
        assert!(
            row.label.contains("Settings"),
            "the row says where it goes rather than looking like a connect: {}",
            row.label
        );
    }

    #[test]
    fn a_radio_that_is_off_still_produces_a_usable_wifi_menu_with_a_settings_entry() {
        let menu = network_menu(Some(&net_status(RadioState::Off, None)), None, false, &[], &[]);
        assert!(!menu.items.is_empty(), "an empty popup reads as broken");
        assert!(menu.flatten().iter().any(|i| i.label.contains("Network settings")));
    }

    #[test]
    fn a_radio_that_is_off_still_produces_a_usable_bluetooth_menu_with_a_settings_entry() {
        let menu = bluetooth_menu(Some(&bt_status(AdapterState::Off)), false, &[]);
        assert!(!menu.items.is_empty());
        assert!(menu.flatten().iter().any(|i| i.label.contains("Bluetooth settings")));
    }

    #[test]
    fn an_unavailable_network_daemon_produces_a_menu_that_says_so_rather_than_an_empty_one() {
        let menu = network_menu(None, None, true, &[], &[]);
        assert!(!menu.items.is_empty());
        assert!(menu.flatten().iter().any(|i| i.label.to_lowercase().contains("unavailable")));
        assert!(menu.flatten().iter().any(|i| i.label.contains("Network settings")));
    }

    #[test]
    fn an_unavailable_bluetooth_daemon_produces_a_menu_that_says_so_rather_than_an_empty_one() {
        let menu = bluetooth_menu(None, true, &[]);
        assert!(!menu.items.is_empty());
        assert!(menu.flatten().iter().any(|i| i.label.to_lowercase().contains("unavailable")));
        assert!(menu.flatten().iter().any(|i| i.label.contains("Bluetooth settings")));
    }

    /// The one case this whole feature exists to route away from a join
    /// attempt: a network that needs a passphrase this daemon has never
    /// been given anywhere to type.
    #[test]
    fn a_passphrase_network_with_no_saved_connection_routes_to_settings_not_a_join_attempt() {
        let menu = network_menu(
            Some(&net_status(RadioState::On, None)),
            None,
            false,
            &[access_point("neighbour", Security::Wpa2Personal)],
            &[], // nothing saved
        );
        let row = menu.flatten().into_iter().find(|i| i.label == "neighbour").unwrap();
        assert_eq!(row.action.as_deref(), Some("wifi:settings"));
        assert_eq!(parse_menu_action(row.action.as_deref().unwrap()), Some(MenuAction::OpenSettings("network")));
    }

    /// The mirror image of the test above: a saved passphrase network is
    /// offered a real connect action rather than being bounced to
    /// Settings every time.
    #[test]
    fn a_passphrase_network_with_a_saved_connection_gets_a_connect_action() {
        let menu = network_menu(
            Some(&net_status(RadioState::On, None)),
            None,
            false,
            &[access_point("home", Security::Wpa2Personal)],
            &[saved("home")],
        );
        let row = menu.flatten().into_iter().find(|i| i.label == "home").unwrap();
        let action = row.action.as_deref().unwrap();
        assert!(action.starts_with("wifi:connect:"), "expected a connect action, got {action:?}");
    }

    // --- the action vocabulary --------------------------------------------

    /// A menu open is handled before actions are parsed. If it ever fell
    /// through to the parser it would be logged as an unknown action on
    /// every single open — noise that would bury the warnings that
    /// matter.
    /// The watcher only reads the file when this changes, so anything it
    /// cannot see is a change that never takes effect.
    ///
    /// Absence counts: deleting `tray.toml` is a reset to the defaults
    /// and has to act like any other edit.
    #[test]
    fn the_prefs_stamp_sees_a_file_appear_change_and_vanish() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("tray.toml");

        assert_eq!(prefs_stamp(&path), None, "a file that is not there");

        std::fs::write(&path, "network = true\n").unwrap();
        let created = prefs_stamp(&path);
        assert!(created.is_some(), "a file that appeared");

        std::fs::write(&path, "network = false\nbluetooth = false\n").unwrap();
        assert_ne!(prefs_stamp(&path), created, "a file whose contents changed");

        std::fs::remove_file(&path).unwrap();
        assert_eq!(prefs_stamp(&path), None, "a file that was deleted");
    }

    #[test]
    fn a_menu_open_is_not_mistaken_for_a_click() {
        let opened = format!(
            "{}{}",
            hyprforge_tray::OPENED_PREFIX,
            NETWORK_ITEM_ID
        );
        assert!(
            opened.strip_prefix(hyprforge_tray::OPENED_PREFIX).is_some(),
            "the open event must be recognisable by its prefix"
        );
        assert!(
            parse_menu_action(&opened).is_none(),
            "an open is an event, not an action"
        );
    }

    /// The id in the open event has to be the id the item registers
    /// under, or the Wi-Fi menu opening scans nothing.
    #[test]
    fn the_network_items_id_is_the_one_the_open_event_carries() {
        let (item, _menu) = {
            let status = net_status(RadioState::On, None);
            (network_item(Some(&status), None, None, false), ())
        };
        assert_eq!(item.id, NETWORK_ITEM_ID);
    }

    #[test]
    fn an_unknown_action_string_is_ignored_rather_than_misrouted() {
        assert_eq!(parse_menu_action(""), None);
        assert_eq!(parse_menu_action("wifi:teleport"), None);
        assert_eq!(parse_menu_action("bt:levitate:AA:BB"), None);
        assert_eq!(parse_menu_action("settings"), None, "the vocabulary is domain-prefixed");
    }

    #[test]
    fn every_parseable_action_round_trips_to_a_distinct_operation() {
        assert_eq!(parse_menu_action("wifi:settings"), Some(MenuAction::OpenSettings("network")));
        assert_eq!(parse_menu_action("bt:settings"), Some(MenuAction::OpenSettings("bluetooth")));
        assert_eq!(parse_menu_action("wifi:radio:on"), Some(MenuAction::WifiRadio(true)));
        assert_eq!(parse_menu_action("wifi:radio:off"), Some(MenuAction::WifiRadio(false)));
        assert_eq!(parse_menu_action("bt:radio:on"), Some(MenuAction::BtRadio(true)));
        assert_eq!(parse_menu_action("bt:radio:off"), Some(MenuAction::BtRadio(false)));
        assert_eq!(
            parse_menu_action("wifi:connect:686f6d65"),
            Some(MenuAction::WifiConnect(b"home".to_vec()))
        );
        assert_eq!(
            parse_menu_action("bt:connect:AA:BB:CC:DD:EE:FF"),
            Some(MenuAction::BtConnect(Address::new("AA:BB:CC:DD:EE:FF")))
        );
        assert_eq!(
            parse_menu_action("bt:disconnect:AA:BB:CC:DD:EE:FF"),
            Some(MenuAction::BtDisconnect(Address::new("AA:BB:CC:DD:EE:FF")))
        );
    }

    #[test]
    fn a_malformed_hex_ssid_in_a_connect_action_is_ignored_rather_than_panicking() {
        assert_eq!(parse_menu_action("wifi:connect:zz"), None);
        assert_eq!(parse_menu_action("wifi:connect:abc"), None, "odd length is not valid hex");
    }

    /// The property the task calls out by name: a menu built from real
    /// (mocked) data must never contain an action string that fails to
    /// parse back — that is exactly the failure mode of a typo'd prefix,
    /// which otherwise does nothing when clicked and nothing tells you
    /// why.
    #[test]
    fn every_action_a_built_menu_can_contain_parses_back_to_the_operation_that_built_it() {
        let net_menus = [
            network_menu(None, None, true, &[], &[]),
            network_menu(Some(&net_status(RadioState::Off, None)), None, false, &[], &[]),
            network_menu(Some(&net_status(RadioState::HardwareOff, None)), None, false, &[], &[]),
            network_menu(
                Some(&net_status(RadioState::On, Some("home"))),
                Some(&Ssid::new("home")),
                false,
                &[
                    access_point("home", Security::Wpa2Personal),
                    access_point("open-cafe", Security::Open),
                    access_point("corp", Security::Enterprise),
                    access_point("neighbour", Security::Wpa3Personal),
                ],
                &[saved("home")],
            ),
        ];
        let bt_menus = [
            bluetooth_menu(None, true, &[]),
            bluetooth_menu(Some(&bt_status(AdapterState::Off)), false, &[]),
            bluetooth_menu(Some(&bt_status(AdapterState::HardwareBlocked)), false, &[]),
            bluetooth_menu(
                Some(&bt_status(AdapterState::On)),
                false,
                &[
                    bt_device("Connected Buds", true, true),
                    bt_device("Paired Keyboard", true, false),
                    bt_device("Stranger Phone", false, false),
                ],
            ),
        ];

        for menu in net_menus.iter().chain(bt_menus.iter()) {
            for item in menu.flatten() {
                let Some(action) = item.action.as_deref() else { continue };
                if action.is_empty() {
                    continue;
                }
                assert!(
                    parse_menu_action(action).is_some(),
                    "action {action:?} on row {:?} does not parse back to an operation",
                    item.label
                );
            }
        }
    }

    #[test]
    fn hex_round_trips_arbitrary_bytes_including_non_utf8_ones() {
        let bytes = vec![0u8, 1, 254, 255, b'a'];
        assert_eq!(hex_decode(&hex_encode(&bytes)), Some(bytes));
    }

    // --- keep awake ------------------------------------------------------

    fn inhibitor(who: &str, why: &str) -> InhibitorInfo {
        InhibitorInfo {
            what: "sleep:idle".to_string(),
            who: who.to_string(),
            why: why.to_string(),
            mode: "block".to_string(),
            uid: 1000,
            pid: 4242,
        }
    }

    #[test]
    fn every_keep_awake_icon_the_function_can_return_is_a_documented_freedesktop_name() {
        let other = [inhibitor("systemd-inhibit", "working")];
        let items = [
            keep_awake_item(None, &[], true),
            keep_awake_item(Some(false), &[], false),
            keep_awake_item(Some(true), &[], false),
            keep_awake_item(Some(true), &other, false),
            keep_awake_item(Some(false), &other, false),
        ];
        for item in items {
            assert!(
                KEEP_AWAKE_ICON_NAMES.contains(&item.icon_name.as_str()),
                "invented icon name: {}",
                item.icon_name
            );
        }
    }

    /// A switch has to be visible in both positions.
    ///
    /// This asserted `Passive` while off, on the reasoning that there is
    /// nothing to monitor when nothing is being held. `Passive` asks the
    /// host to *hide* the item and hosts obey, so switching the icon on
    /// in Settings made it appear and then vanish — "not currently
    /// keeping the machine awake" being the state it starts in. The
    /// toggle was unreachable from the bar it lived in.
    ///
    /// The radios are what `Passive` is genuinely for: they hide when the
    /// hardware is off, which is a state chosen elsewhere and not
    /// actionable from a tray icon anyway.
    #[test]
    fn keep_awake_stays_visible_whether_or_not_it_is_holding_the_machine_awake() {
        let off = keep_awake_item(Some(false), &[], false);
        assert_eq!(
            off.status,
            TrayStatus::Active,
            "a switch the user cannot see is a switch they cannot flip"
        );
        assert_eq!(on_icon(&off), "changes-allow-symbolic");

        let on = keep_awake_item(Some(true), &[], false);
        assert_eq!(on.status, TrayStatus::Active);
        assert_eq!(on_icon(&on), "changes-prevent-symbolic");
    }

    /// The two states still look different, which is the whole reason
    /// the icon is worth having when it is always visible.
    fn on_icon(item: &TrayItem) -> &str {
        &item.icon_name
    }

    #[test]
    fn an_unavailable_logind_produces_an_icon_and_tooltip_that_say_so() {
        let item = keep_awake_item(None, &[], true);
        assert_eq!(item.status, TrayStatus::Active, "must stay visible to be seen at all");
        assert!(item.tooltip_title.to_lowercase().contains("unavailable"));
    }

    /// The property this daemon's own filtering exists for: logind
    /// reports every inhibitor it holds, ours included, so `main`'s "who"
    /// string has to be filtered back out before it reaches a menu built
    /// to show *other* holders.
    #[test]
    fn our_own_inhibitor_is_excluded_from_the_list_of_other_holders() {
        let all = vec![inhibitor(KEEP_AWAKE_WHO, KEEP_AWAKE_WHY), inhibitor("mpv", "playing a video")];
        let others = other_inhibitors(all);
        assert_eq!(others.len(), 1);
        assert_eq!(others[0].who, "mpv");
    }

    /// Keep awake's holder — started from here or from Settings — is
    /// keep awake itself, never one of the "other" holders listed under
    /// it.
    #[test]
    fn the_keep_awake_holder_is_not_listed_as_another_holder() {
        let all = vec![
            inhibitor(hyprforge_power::keep_awake::HOLDER_WHO, "Keep awake is on"),
            inhibitor("mpv", "playing a video"),
        ];
        let others = other_inhibitors(all);
        assert_eq!(others.len(), 1);
        assert_eq!(others[0].who, "mpv");
    }

    /// Another process's inhibitor shows up in the menu as a disabled
    /// row, and ours never appears a second time next to the checkmark
    /// that already represents it.
    #[test]
    fn another_processs_inhibitor_appears_in_the_keep_awake_menu_and_ours_does_not_appear_twice() {
        let all = vec![inhibitor(KEEP_AWAKE_WHO, KEEP_AWAKE_WHY), inhibitor("mpv", "playing a video")];
        let others = other_inhibitors(all);
        let menu = keep_awake_menu(Some(true), &others, false);
        let rows = menu.flatten();

        let mpv_rows: Vec<_> = rows.iter().filter(|i| i.label.contains("mpv")).collect();
        assert_eq!(mpv_rows.len(), 1, "the other holder should appear exactly once");
        assert!(!mpv_rows[0].enabled, "another process's lock cannot be released from here");

        let own_rows: Vec<_> = rows.iter().filter(|i| i.label.contains(KEEP_AWAKE_WHO)).collect();
        assert!(own_rows.is_empty(), "our own inhibitor must not appear in the disabled list too");
    }

    #[test]
    fn the_keep_awake_menus_checkmark_reflects_whether_we_are_holding_it() {
        let held = keep_awake_menu(Some(true), &[], false);
        let row = held.flatten().into_iter().find(|i| i.kind == ItemKind::Checkmark).unwrap();
        assert_eq!(row.toggle, Some(true));
        assert_eq!(row.action.as_deref(), Some("keepawake:off"));

        let not_held = keep_awake_menu(Some(false), &[], false);
        let row = not_held.flatten().into_iter().find(|i| i.kind == ItemKind::Checkmark).unwrap();
        assert_eq!(row.toggle, Some(false));
        assert_eq!(row.action.as_deref(), Some("keepawake:on"));
    }

    // --- night light -------------------------------------------------------

    #[test]
    fn every_night_light_icon_the_function_can_return_is_a_documented_freedesktop_name() {
        let items = [
            night_light_item(&NightLightState::NotRunning),
            night_light_item(&NightLightState::CouldNotCheck),
            night_light_item(&NightLightState::Known { temperature: 4500, on: true }),
            night_light_item(&NightLightState::Known { temperature: 4500, on: false }),
        ];
        for item in items {
            assert!(
                NIGHT_LIGHT_ICON_NAMES.contains(&item.icon_name.as_str()),
                "invented icon name: {}",
                item.icon_name
            );
        }
    }

    /// The property the task calls out by name: a daemon that is not
    /// running must never be reported the same way as night light being
    /// deliberately switched off, in either the icon or the menu.
    #[test]
    fn hyprsunset_not_running_produces_a_menu_that_says_so_distinct_from_night_light_off() {
        let not_running = night_light_item(&NightLightState::NotRunning);
        let off = night_light_item(&NightLightState::Known { temperature: 4500, on: false });
        assert_ne!(not_running.icon_name, off.icon_name);
        assert_ne!(not_running.tooltip_title, off.tooltip_title);
        assert!(not_running.tooltip_title.to_lowercase().contains("unavailable"));
        assert!(off.tooltip_title.to_lowercase().contains("off"));

        let not_running_menu = night_light_menu(&NightLightState::NotRunning);
        let off_menu = night_light_menu(&NightLightState::Known { temperature: 4500, on: false });
        assert_ne!(not_running_menu, off_menu);
        assert!(not_running_menu
            .flatten()
            .iter()
            .any(|i| i.label.to_lowercase().contains("running")));
        // The "off" menu, unlike the "not running" one, offers a
        // checkmark to act on — there is something here to toggle.
        assert!(off_menu.flatten().iter().any(|i| i.kind == ItemKind::Checkmark));
        assert!(!not_running_menu.flatten().iter().any(|i| i.kind == ItemKind::Checkmark));
    }

    /// The third state: the check itself failing must read differently
    /// from both "not running" and "off", not collapse into either.
    #[test]
    fn a_failed_check_reads_differently_from_not_running_and_from_off() {
        let could_not_check = night_light_item(&NightLightState::CouldNotCheck);
        let not_running = night_light_item(&NightLightState::NotRunning);
        let off = night_light_item(&NightLightState::Known { temperature: 4500, on: false });
        assert_ne!(could_not_check.icon_name, not_running.icon_name);
        assert_ne!(could_not_check.icon_name, off.icon_name);
        assert_ne!(could_not_check.tooltip_title, not_running.tooltip_title);
        assert_ne!(could_not_check.tooltip_title, off.tooltip_title);
    }

    #[test]
    fn a_known_reading_shows_the_temperature_only_while_believed_on() {
        let on = night_light_item(&NightLightState::Known { temperature: 3500, on: true });
        assert!(on.tooltip_body.contains("3500"));
        assert_eq!(on.status, TrayStatus::Active);

        let off = night_light_item(&NightLightState::Known { temperature: 3500, on: false });
        assert_eq!(off.status, TrayStatus::Active, "a toggle has to be visible in both positions");
        assert!(!off.tooltip_body.contains("3500"), "off has nothing numeric to show");
    }

    #[test]
    fn the_current_preset_is_shown_checked_and_others_are_not() {
        let menu = night_light_menu(&NightLightState::Known { temperature: 3500, on: true });
        let current = menu.flatten().into_iter().find(|i| i.label == "3500K").unwrap();
        assert_eq!(current.toggle, Some(true));

        let other = menu.flatten().into_iter().find(|i| i.label == "4500K").unwrap();
        assert_ne!(other.toggle, Some(true));
    }

    // --- the action vocabulary, extended -----------------------------------

    /// Mirrors `every_action_a_built_menu_can_contain_parses_back_to_the_operation_that_built_it`
    /// for the two new menus, rather than folding them into that test —
    /// each set of menus is built from its own state enum, and keeping
    /// them apart makes it obvious which menu a failure came from.
    #[test]
    fn every_keep_awake_and_night_light_action_parses_back_to_an_operation() {
        let other = [inhibitor("mpv", "playing a video")];
        let keep_awake_menus = [
            keep_awake_menu(None, &[], true),
            keep_awake_menu(Some(false), &[], false),
            keep_awake_menu(Some(true), &other, false),
        ];
        let night_light_menus = [
            night_light_menu(&NightLightState::NotRunning),
            night_light_menu(&NightLightState::CouldNotCheck),
            night_light_menu(&NightLightState::Known { temperature: 4500, on: true }),
            night_light_menu(&NightLightState::Known { temperature: 2700, on: false }),
        ];

        for menu in keep_awake_menus.iter().chain(night_light_menus.iter()) {
            for item in menu.flatten() {
                let Some(action) = item.action.as_deref() else { continue };
                if action.is_empty() {
                    continue;
                }
                assert!(
                    parse_menu_action(action).is_some(),
                    "action {action:?} on row {:?} does not parse back to an operation",
                    item.label
                );
            }
        }
    }

    #[test]
    fn keep_awake_and_night_light_actions_round_trip_to_distinct_operations() {
        assert_eq!(parse_menu_action("keepawake:on"), Some(MenuAction::KeepAwake(true)));
        assert_eq!(parse_menu_action("keepawake:off"), Some(MenuAction::KeepAwake(false)));
        assert_eq!(parse_menu_action("nightlight:off"), Some(MenuAction::NightLightOff));
        assert_eq!(parse_menu_action("nightlight:set:2700"), Some(MenuAction::NightLightSet(2700)));
        assert_eq!(parse_menu_action("nightlight:set:not-a-number"), None);
    }

    // --- the one menu shape, applied to all four ---------------------------

    /// Every menu ends in a settings row, unavailable states included —
    /// the property this whole task was about. Before this, keep-awake
    /// and night-light's unavailable menus offered nothing at all: a
    /// dead end with no way out, the exact thing CLAUDE.md warns against.
    #[test]
    fn every_menus_unavailable_state_still_offers_its_settings_row() {
        let menus = [
            network_menu(None, None, true, &[], &[]),
            bluetooth_menu(None, true, &[]),
            keep_awake_menu(None, &[], true),
            night_light_menu(&NightLightState::NotRunning),
            night_light_menu(&NightLightState::CouldNotCheck),
            power_menu(&BatteryReading::Unavailable, &ProfileReading::Unavailable),
            displays_menu(&DisplaysReading::Unavailable, false),
            displays_menu(&DisplaysReading::Known { profiles: vec![], current: None }, false),
            displays_menu(&DisplaysReading::Unavailable, true),
        ];
        for menu in menus {
            let last = menu.items.last().expect("a menu with no rows at all");
            assert!(last.enabled, "the settings row must still be clickable: {}", last.label);
            assert!(
                last.label.to_lowercase().contains("settings"),
                "the last row of an unavailable menu must be its settings row, got {:?}",
                last.label
            );
        }
    }

    /// All four menus end with a settings row in their ordinary
    /// (available) state too, not only when something is wrong — that
    /// symmetry is the actual complaint this task set out to fix.
    #[test]
    fn every_menus_ordinary_state_ends_with_its_own_settings_row() {
        let net = network_menu(Some(&net_status(RadioState::On, None)), None, false, &[], &[]);
        assert_eq!(net.items.last().unwrap().label, "Network settings…");

        let bt = bluetooth_menu(Some(&bt_status(AdapterState::On)), false, &[]);
        assert_eq!(bt.items.last().unwrap().label, "Bluetooth settings…");

        let ka = keep_awake_menu(Some(false), &[], false);
        assert_eq!(ka.items.last().unwrap().label, "Keep awake settings…");

        let nl = night_light_menu(&NightLightState::Known { temperature: 4500, on: true });
        assert_eq!(nl.items.last().unwrap().label, "Night light settings…");

        let pw = power_menu(&BatteryReading::Absent, &balanced());
        assert_eq!(pw.items.last().unwrap().label, "Power settings…");

        let dp = displays_menu(&desk_and_laptop(), false);
        assert_eq!(dp.items.last().unwrap().label, "Display settings…");
    }

    /// The settings rows for keep awake and night light are new: neither
    /// menu offered one before this task, because neither destination
    /// existed. Both do now (`--screen power`, `--screen night-light`),
    /// which is also what a left click on either icon already opens —
    /// see `TrayItem::activate_screen`. This pins the two rows to those
    /// same names, so the menu and the icon can never disagree about
    /// where they send someone.
    #[test]
    fn the_new_settings_rows_open_the_same_screens_a_left_click_on_that_icon_does() {
        let ka = keep_awake_menu(Some(false), &[], false);
        let ka_row = ka.items.last().unwrap();
        assert_eq!(
            parse_menu_action(ka_row.action.as_deref().unwrap()),
            Some(MenuAction::OpenSettings("power"))
        );

        let nl = night_light_menu(&NightLightState::Known { temperature: 4500, on: true });
        let nl_row = nl.items.last().unwrap();
        assert_eq!(
            parse_menu_action(nl_row.action.as_deref().unwrap()),
            Some(MenuAction::OpenSettings("night-light"))
        );

        // The power icon's row and its left click both open Power — see
        // `TrayItem::activate_screen`.
        let pw = power_menu(&BatteryReading::Absent, &balanced());
        assert_eq!(
            parse_menu_action(pw.items.last().unwrap().action.as_deref().unwrap()),
            Some(MenuAction::OpenSettings("power"))
        );

        let dp = displays_menu(&desk_and_laptop(), false);
        assert_eq!(
            parse_menu_action(dp.items.last().unwrap().action.as_deref().unwrap()),
            Some(MenuAction::OpenSettings("displays"))
        );
    }

    /// Toggle labels are all nouns now — "Keep awake" replaces "Keep this
    /// machine awake", to match "Wi-Fi", "Bluetooth" and "Night light".
    #[test]
    fn the_keep_awake_toggles_label_is_a_noun_like_the_other_three() {
        let menu = keep_awake_menu(Some(true), &[], false);
        let toggle = menu.flatten().into_iter().find(|i| i.kind == ItemKind::Checkmark).unwrap();
        assert_eq!(toggle.label, "Keep awake");
    }

    /// A row's id must not move depending on how much content comes
    /// between the toggle and the settings row — the whole reason
    /// `menu.rs` documents `id` as assigned from meaning, not position.
    /// Here: the keep-awake toggle is always row 1, whether or not any
    /// other process is holding an inhibitor.
    #[test]
    fn the_keep_awake_toggles_id_does_not_move_when_other_inhibitors_appear() {
        let alone = keep_awake_menu(Some(true), &[], false);
        let others = [inhibitor("mpv", "playing a video")];
        let with_others = keep_awake_menu(Some(true), &others, false);

        let alone_toggle = alone.flatten().into_iter().find(|i| i.kind == ItemKind::Checkmark).unwrap();
        let with_others_toggle =
            with_others.flatten().into_iter().find(|i| i.kind == ItemKind::Checkmark).unwrap();
        assert_eq!(alone_toggle.id, with_others_toggle.id);
    }

    // --- power: battery and profile ------------------------------------

    fn battery(percentage: u8, state: BatteryState) -> BatteryInfo {
        BatteryInfo { percentage, state, time_to_empty: None, time_to_full: None }
    }

    fn balanced() -> ProfileReading {
        ProfileReading::Known {
            active: PowerProfile::Balanced,
            offered: vec![PowerProfile::PowerSaver, PowerProfile::Balanced, PowerProfile::Performance],
        }
    }

    /// Every name `power_item` can hand a host, checked against Breeze's
    /// own files on this machine: eleven levels, each with and without
    /// `-charging`, plus the charged, profile and fallback names.
    fn power_icon_names() -> Vec<String> {
        let mut names = Vec::new();
        for level in (0..=100).step_by(10) {
            names.push(format!("battery-{level:03}-symbolic"));
            names.push(format!("battery-{level:03}-charging-symbolic"));
        }
        names.extend(
            [
                "battery-full-charged-symbolic",
                "battery-profile-powersave-symbolic",
                "battery-profile-balanced-symbolic",
                "battery-profile-performance-symbolic",
                "dialog-warning",
            ]
            .map(String::from),
        );
        names
    }

    /// Every percentage through every state, so a rounding slip that
    /// invents `battery-105-symbolic` — or a state that forgets its
    /// suffix — fails here rather than drawing a blank gap in the bar.
    #[test]
    fn every_power_icon_the_function_can_return_is_a_name_breeze_ships() {
        let known = power_icon_names();
        let states = [
            BatteryState::Charging,
            BatteryState::Discharging,
            BatteryState::Empty,
            BatteryState::FullyCharged,
            BatteryState::PendingCharge,
            BatteryState::PendingDischarge,
            BatteryState::Unknown,
        ];
        let profiles = [
            ProfileReading::Unavailable,
            ProfileReading::Known { active: PowerProfile::PowerSaver, offered: vec![] },
            ProfileReading::Known { active: PowerProfile::Balanced, offered: vec![] },
            ProfileReading::Known { active: PowerProfile::Performance, offered: vec![] },
        ];
        let mut readings = vec![BatteryReading::Absent, BatteryReading::Unavailable];
        for pct in 0..=100 {
            for state in states {
                readings.push(BatteryReading::Present(battery(pct, state)));
            }
        }
        for reading in &readings {
            for profile in &profiles {
                let icon = power_item(reading, profile).icon_name;
                assert!(known.contains(&icon), "{icon:?} is not a name this daemon may emit");
            }
        }
    }

    /// Nearest ten, not rounded down: 9% is not an empty battery.
    #[test]
    fn the_battery_level_rounds_to_the_nearest_icon_step() {
        let icon = |pct| battery_icon(&battery(pct, BatteryState::Discharging));
        assert_eq!(icon(0), "battery-000-symbolic");
        assert_eq!(icon(4), "battery-000-symbolic");
        assert_eq!(icon(5), "battery-010-symbolic");
        assert_eq!(icon(9), "battery-010-symbolic");
        assert_eq!(icon(94), "battery-090-symbolic");
        assert_eq!(icon(95), "battery-100-symbolic");
        assert_eq!(icon(100), "battery-100-symbolic");
    }

    #[test]
    fn a_charging_battery_draws_the_plug_and_a_full_one_its_own_icon() {
        assert_eq!(
            battery_icon(&battery(62, BatteryState::Charging)),
            "battery-060-charging-symbolic"
        );
        assert_eq!(
            battery_icon(&battery(62, BatteryState::PendingCharge)),
            "battery-060-charging-symbolic"
        );
        assert_eq!(
            battery_icon(&battery(100, BatteryState::FullyCharged)),
            "battery-full-charged-symbolic"
        );
    }

    /// A desktop has no battery, and that is not a failure: the icon
    /// shows the profile, and nothing in it reads as a warning.
    #[test]
    fn a_machine_with_no_battery_shows_its_power_profile_not_a_warning() {
        let item = power_item(&BatteryReading::Absent, &balanced());
        assert_eq!(item.icon_name, "battery-profile-balanced-symbolic");
        assert_eq!(item.tooltip_title, "Balanced profile");
        assert_eq!(item.tooltip_body, "No battery");
    }

    /// UPower being down must neither hide a working profile switch nor
    /// read as "this machine has no battery" — the two failures this
    /// daemon keeps apart everywhere else.
    #[test]
    fn upower_being_down_keeps_the_profile_and_does_not_claim_there_is_no_battery() {
        let item = power_item(&BatteryReading::Unavailable, &balanced());
        assert_eq!(item.icon_name, "battery-profile-balanced-symbolic");
        assert!(item.tooltip_body.contains("UPower"), "{}", item.tooltip_body);
        assert!(!item.tooltip_body.contains("No battery"));

        let menu = power_menu(&BatteryReading::Unavailable, &balanced());
        assert_eq!(menu.items[0].label, "Battery status unavailable");
        assert!(menu.items.iter().any(|i| i.label == "Balanced" && i.action.is_some()));
    }

    /// And the other way round: the battery still reads when the profile
    /// daemon is down, and the menu says why there are no profiles.
    #[test]
    fn the_profile_daemon_being_down_keeps_the_battery_and_says_why_profiles_are_missing() {
        let reading = BatteryReading::Present(BatteryInfo {
            time_to_empty: Some(Duration::from_secs(92 * 60)),
            ..battery(73, BatteryState::Discharging)
        });
        let item = power_item(&reading, &ProfileReading::Unavailable);
        assert_eq!(item.icon_name, "battery-070-symbolic");
        assert_eq!(item.tooltip_title, "73% — 1h 32m left");
        assert_eq!(item.tooltip_body, "", "no profile claimed when it could not be read");

        let menu = power_menu(&reading, &ProfileReading::Unavailable);
        assert_eq!(menu.items[0].label, "Battery 73% — 1h 32m left");
        assert!(!menu.items[0].enabled, "the battery row is information, not a control");
        assert!(menu
            .items
            .iter()
            .any(|i| i.label.contains("power-profiles-daemon isn't running") && !i.enabled));
    }

    /// Only what the daemon offers is listed, the active one is the one
    /// checked, and each row's action switches to exactly that profile.
    #[test]
    fn the_profile_rows_are_what_the_daemon_offers_with_the_active_one_checked() {
        let profile = ProfileReading::Known {
            active: PowerProfile::PowerSaver,
            offered: vec![PowerProfile::PowerSaver, PowerProfile::Balanced],
        };
        let menu = power_menu(&BatteryReading::Absent, &profile);
        let rows: Vec<_> = menu
            .items
            .iter()
            .filter(|i| i.action.as_deref().is_some_and(|a| a.starts_with("power:profile:")))
            .collect();
        assert_eq!(rows.len(), 2, "Performance was not offered and must not be listed");
        assert_eq!(rows[0].label, "Power saver");
        assert_eq!((rows[0].kind, rows[0].toggle), (ItemKind::Checkmark, Some(true)));
        assert_eq!(rows[1].label, "Balanced");
        assert_eq!(
            parse_menu_action(rows[1].action.as_deref().unwrap()),
            Some(MenuAction::PowerProfile(PowerProfile::Balanced))
        );
    }

    #[test]
    fn a_profile_name_the_daemon_never_defined_is_ignored_rather_than_sent() {
        assert_eq!(parse_menu_action("power:profile:turbo"), None);
        assert_eq!(parse_menu_action("power:profile:"), None);
    }

    /// "Plugged in, not charging" is neither of the two things people
    /// would otherwise be told, and both of those send them to check the
    /// cable.
    #[test]
    fn a_battery_held_below_full_on_mains_says_so() {
        assert_eq!(
            battery_words(&battery(80, BatteryState::PendingCharge)),
            "80% — plugged in, not charging"
        );
    }

    // --- displays ------------------------------------------------------

    fn profile(id: &str, name: &str, last_used: &str) -> DisplayProfile {
        DisplayProfile { id: id.to_string(), name: name.to_string(), last_used: last_used.to_string() }
    }

    /// Two saved layouts, the desk one in effect.
    fn desk_and_laptop() -> DisplaysReading {
        DisplaysReading::Known {
            profiles: vec![
                profile("aa11", "Laptop only", "2026-09-20T08:00:00Z"),
                profile("bb22", "Desk", "2026-09-23T09:00:00Z"),
            ],
            current: Some("bb22".to_string()),
        }
    }

    const DISPLAYS_ICON_NAMES: &[&str] =
        &["monitor-symbolic", "preferences-desktop-display-randr-symbolic", "dialog-warning"];

    #[test]
    fn every_displays_icon_the_function_can_return_is_in_the_allowed_set() {
        let readings = [
            DisplaysReading::Unavailable,
            DisplaysReading::Known { profiles: vec![], current: None },
            desk_and_laptop(),
        ];
        for reading in &readings {
            for pending in [false, true] {
                let icon = displays_item(reading, pending).icon_name;
                assert!(DISPLAYS_ICON_NAMES.contains(&icon.as_str()), "{icon:?}");
            }
        }
    }

    /// A layout on trial outranks everything, and is the one state here
    /// that asks for attention — never to be hidden.
    #[test]
    fn a_layout_waiting_to_be_kept_asks_for_attention_with_an_icon_of_its_own() {
        let waiting = displays_item(&desk_and_laptop(), true);
        assert_eq!(waiting.status, TrayStatus::NeedsAttention);
        assert_eq!(waiting.icon_name, "preferences-desktop-display-randr-symbolic");

        let settled = displays_item(&desk_and_laptop(), false);
        assert_eq!(settled.status, TrayStatus::Active);
        assert_ne!(settled.icon_name, waiting.icon_name);
    }

    /// While a layout is on trial, the menu is Keep and Revert and
    /// nothing else — no switching to a third layout mid-confirmation.
    #[test]
    fn a_pending_revert_offers_keep_and_revert_and_no_other_layouts() {
        let menu = displays_menu(&desk_and_laptop(), true);
        let actions: Vec<_> = menu.items.iter().filter_map(|i| i.action.as_deref()).collect();
        assert!(actions.contains(&"display:keep"));
        assert!(actions.contains(&"display:revert"));
        assert!(!actions.iter().any(|a| a.starts_with("display:apply:")), "{actions:?}");
        assert_eq!(parse_menu_action("display:keep"), Some(MenuAction::DisplayKeep));
        assert_eq!(parse_menu_action("display:revert"), Some(MenuAction::DisplayRevert));
    }

    /// Most recently used first, the current one checked, and each row's
    /// action applies exactly that profile.
    #[test]
    fn saved_layouts_are_listed_newest_first_with_the_current_one_checked() {
        let menu = displays_menu(&desk_and_laptop(), false);
        let rows: Vec<_> = menu
            .items
            .iter()
            .filter(|i| i.action.as_deref().is_some_and(|a| a.starts_with("display:apply:")))
            .collect();
        assert_eq!(rows.iter().map(|r| r.label.as_str()).collect::<Vec<_>>(), ["Desk", "Laptop only"]);
        assert_eq!((rows[0].kind, rows[0].toggle), (ItemKind::Checkmark, Some(true)));
        assert_eq!(rows[1].kind, ItemKind::Standard);
        assert_eq!(
            parse_menu_action(rows[1].action.as_deref().unwrap()),
            Some(MenuAction::DisplayApply("aa11".to_string()))
        );
    }

    #[test]
    fn the_tooltip_names_the_layout_in_effect_or_says_none_matches() {
        let item = displays_item(&desk_and_laptop(), false);
        assert_eq!(item.tooltip_title, "Desk");
        assert_eq!(item.tooltip_body, "2 saved layouts");

        let unmatched = DisplaysReading::Known {
            profiles: vec![profile("aa11", "Laptop only", "2026-09-20T08:00:00Z")],
            current: None,
        };
        let item = displays_item(&unmatched, false);
        assert_eq!(item.tooltip_title, "No saved layout matches");
        assert_eq!(item.tooltip_body, "1 saved layout");
    }

    /// displayd not running is its own state with its own words, not an
    /// empty list of layouts.
    #[test]
    fn displayd_not_running_reads_differently_from_having_no_saved_layouts() {
        let down = displays_menu(&DisplaysReading::Unavailable, false);
        let empty = displays_menu(&DisplaysReading::Known { profiles: vec![], current: None }, false);
        assert!(down.items[0].label.contains("hyprforge-displayd isn't running"));
        assert_eq!(empty.items[0].label, "No saved layouts yet");
    }

    #[test]
    fn an_apply_action_that_did_not_come_from_a_built_menu_is_ignored() {
        assert_eq!(parse_menu_action("display:apply:"), None);
        assert_eq!(parse_menu_action("display:apply:../etc"), None);
        assert_eq!(parse_menu_action("display:apply:ab cd"), None);
    }

    /// A deadline in the past is not a pending revert — the case of a
    /// daemon that died mid-countdown and never said it was resolved.
    #[test]
    fn a_revert_deadline_that_has_passed_is_not_pending() {
        let now = tokio::time::Instant::now();
        assert!(revert_pending(Some(now + Duration::from_secs(5)), now));
        assert!(!revert_pending(Some(now), now));
        assert!(!revert_pending(None, now));
    }

    #[test]
    fn more_layouts_than_fit_are_counted_rather_than_dropped() {
        let profiles = (0..10)
            .map(|i| profile(&format!("id{i}"), &format!("Layout {i}"), &format!("2026-09-{:02}T00:00:00Z", i + 1)))
            .collect();
        let menu = displays_menu(&DisplaysReading::Known { profiles, current: None }, false);
        assert!(menu.items.iter().any(|i| i.label == "+2 more — see Settings"));
    }

    // --- wired ---------------------------------------------------------

    fn wired(interface: &str, state: WiredState, connection: Option<&str>) -> WiredStatus {
        WiredStatus {
            interface: interface.to_string(),
            state,
            connection: connection.map(str::to_string),
            speed_mbps: (state == WiredState::Connected).then_some(1000),
        }
    }

    fn wifi_on(ssid: Option<&str>) -> (TrayItem, Option<Ssid>) {
        let connected = ssid.map(Ssid::new);
        let status = net_status(RadioState::On, ssid);
        (network_item(Some(&status), connected.as_ref(), Some(80), false), connected)
    }

    #[test]
    fn every_wired_icon_the_function_can_return_is_in_the_allowed_set() {
        let states = [
            WiredState::Connected,
            WiredState::Connecting,
            WiredState::Disconnected,
            WiredState::CableUnplugged,
        ];
        for ssid in [None, Some("home")] {
            for wifi_present in [false, true] {
                let mut cases: Vec<Vec<WiredStatus>> = vec![vec![]];
                cases.extend(states.iter().map(|&s| vec![wired("enp3s0", s, Some("LAN"))]));
                for ports in cases {
                    let (item, connected) = wifi_on(ssid);
                    let icon = with_wired(item, connected.as_ref(), &ports, wifi_present).icon_name;
                    assert!(NETWORK_ICON_NAMES.contains(&icon.as_str()), "{icon:?}");
                }
            }
        }
    }

    /// NetworkManager routes over the cable ahead of Wi-Fi, so the icon
    /// names the cable — and still says Wi-Fi is up too.
    #[test]
    fn a_connected_cable_outranks_wifi_and_says_wifi_is_up_too() {
        let (item, connected) = wifi_on(Some("home"));
        let item = with_wired(
            item,
            connected.as_ref(),
            &[wired("enp3s0", WiredState::Connected, Some("Home LAN"))],
            true,
        );
        assert_eq!(item.icon_name, "network-wired-activated-symbolic");
        assert_eq!(item.tooltip_title, "Wired — Home LAN");
        assert_eq!(item.tooltip_body, "1000 Mb/s · also on Wi-Fi (home)");
        assert_eq!(item.title, "Network");
    }

    /// Wi-Fi that works keeps the icon while a cable is still getting an
    /// address, rather than showing a link that isn't up yet.
    #[test]
    fn wifi_keeps_the_icon_while_a_cable_is_still_connecting() {
        let (item, connected) = wifi_on(Some("home"));
        let wifi_icon = item.icon_name.clone();
        let item = with_wired(
            item,
            connected.as_ref(),
            &[wired("enp3s0", WiredState::Connecting, None)],
            true,
        );
        assert_eq!(item.icon_name, wifi_icon);
    }

    /// A machine with no Wi-Fi hardware must not show a Wi-Fi icon saying
    /// "not connected" about a radio that isn't there.
    #[test]
    fn with_no_wifi_hardware_the_icon_is_the_wires_not_a_missing_radios() {
        let (item, _) = wifi_on(None);
        let unplugged = with_wired(
            item.clone(),
            None,
            &[wired("enp3s0", WiredState::CableUnplugged, None)],
            false,
        );
        assert_eq!(unplugged.icon_name, "network-wired-unavailable-symbolic");
        assert_eq!(unplugged.tooltip_title, "Cable unplugged");

        let idle = with_wired(item, None, &[wired("enp3s0", WiredState::Disconnected, None)], false);
        assert_eq!(idle.icon_name, "network-wired-disconnected-symbolic");
        assert_ne!(idle.icon_name, unplugged.icon_name, "plugged-in-idle and unplugged look different");
    }

    /// With Wi-Fi hardware and no cable story to tell, the Wi-Fi icon
    /// stands exactly as it was.
    #[test]
    fn an_unplugged_cable_does_not_displace_wifi() {
        let (item, connected) = wifi_on(Some("home"));
        let before = item.clone();
        let after = with_wired(
            item,
            connected.as_ref(),
            &[wired("enp3s0", WiredState::CableUnplugged, None)],
            true,
        );
        assert_eq!(after, before);
    }

    fn menu_with(ports: &[WiredStatus], wifi_present: bool) -> Menu {
        let status = net_status(RadioState::On, None);
        with_wired_menu(network_menu(Some(&status), None, false, &[], &[]), ports, wifi_present, false)
    }

    /// Wired rows sit above the Wi-Fi toggle in their own section, so
    /// they can't be read as governed by it.
    #[test]
    fn wired_rows_come_first_in_a_section_of_their_own() {
        let menu = menu_with(&[wired("enp3s0", WiredState::Connected, Some("Home LAN"))], true);
        assert_eq!(menu.items[0].label, "Wired — Home LAN");
        assert_eq!((menu.items[0].kind, menu.items[0].toggle), (ItemKind::Checkmark, Some(true)));
        assert_eq!(menu.items[1].kind, ItemKind::Separator);
        assert_eq!(menu.items[2].label, "Wi-Fi");
        assert_eq!(
            parse_menu_action(menu.items[0].action.as_deref().unwrap()),
            Some(MenuAction::WiredDisconnect("enp3s0".to_string()))
        );
    }

    #[test]
    fn a_plugged_in_idle_port_offers_to_connect_and_an_unplugged_one_only_says_so() {
        let menu = menu_with(
            &[
                wired("enp3s0", WiredState::Disconnected, None),
                wired("enx0011", WiredState::CableUnplugged, None),
            ],
            true,
        );
        assert_eq!(menu.items[0].label, "Wired (enp3s0)", "several ports are told apart by name");
        assert_eq!(
            parse_menu_action(menu.items[0].action.as_deref().unwrap()),
            Some(MenuAction::WiredConnect("enp3s0".to_string()))
        );
        assert_eq!(menu.items[1].label, "Wired (enx0011) — cable unplugged");
        assert!(!menu.items[1].enabled);
    }

    /// No Wi-Fi hardware: no Wi-Fi toggle for a radio that isn't there.
    #[test]
    fn with_no_wifi_hardware_the_menu_offers_no_wifi_toggle() {
        let menu = menu_with(&[wired("enp3s0", WiredState::Connected, Some("LAN"))], false);
        assert!(!menu.items.iter().any(|i| i.label == "Wi-Fi"), "{:?}", menu.items);
        assert_eq!(menu.items.last().unwrap().label, "Network settings…");

        let bare = menu_with(&[], false);
        assert_eq!(bare.items[0].label, "No network devices");
    }

    #[test]
    fn with_no_wired_ports_the_wifi_menu_is_unchanged() {
        let status = net_status(RadioState::On, None);
        let wifi = network_menu(Some(&status), None, false, &[], &[]);
        assert_eq!(with_wired_menu(wifi.clone(), &[], true, false), wifi);
    }

    #[test]
    fn an_interface_name_the_kernel_would_refuse_is_ignored() {
        assert_eq!(parse_menu_action("wired:connect:"), None);
        assert_eq!(parse_menu_action("wired:connect:en p3s0"), None);
        assert_eq!(parse_menu_action("wired:connect:../eth0"), None);
        assert_eq!(parse_menu_action("wired:connect:abcdefghijklmnop"), None, "sixteen bytes");
        assert_eq!(
            parse_menu_action("wired:connect:enp197s0f4u1"),
            Some(MenuAction::WiredConnect("enp197s0f4u1".to_string()))
        );
    }
}