tablero 0.4.3

A fast, native Wayland status bar for Hyprland
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
//! Wayland layer-shell front-end for tablero.
//!
//! Opens one top-anchored `wlr-layer-shell` surface **per output** under a
//! compositor such as Hyprland — tracking output hotplug to add and remove bars
//! as monitors come and go — renders a live clock through a shared-memory buffer,
//! and drives redraws from a [`calloop`] timer so the loop only wakes for clock
//! ticks, compositor events (output lifecycle, configure, scale), or shutdown —
//! never a busy redraw loop. Each output's bar is configured independently (see
//! [`outputs`]); producer messages and ticks fan out to every surface.
//!
//! Surface geometry is kept in logical pixels (the layer-shell size request and
//! exclusive zone), while the shared-memory buffer is allocated at the output's
//! physical pixel density: on a scaled output the buffer is `scale`× larger and
//! `set_buffer_scale` maps it back, so the bar stays crisp on HiDPI displays.
//! The logical-to-physical conversion lives entirely in [`crate::scale`].

pub mod blit;
pub mod clock;
pub mod config;
mod config_reload;
pub mod icon;
mod performance;
pub mod redraw;
pub mod render;
pub mod scale;
pub mod widget;

pub mod backlight;
pub mod bluetooth;
pub mod command;
pub mod hypridle;
pub mod hyprland;
pub mod networkmanager;
pub mod notifications;
pub mod outputs;
pub mod power_profiles;
pub mod producer;
pub mod sni;
pub mod sysmon;
pub mod updates;
pub mod upower;
pub mod volume;

use std::cell::Cell;
use std::error::Error;
use std::path::PathBuf;
use std::rc::Rc;
use std::time::{Duration, Instant};

use crate::blit::write_argb8888;
use crate::clock::millis_until_next_minute;
use crate::config::{Config, WidgetKind};
use crate::redraw::RedrawScheduler;
use crate::render::{
    Bounds, RenderContext, RenderSettings, RenderStats, SharedFonts, shared_fonts,
};
use crate::scale::Scale;
use crate::widget::{
    ClickButton, Command, Damage, Dashboard, Hover, Msg, ScrollDirection, Tooltip, TrayMenu,
    TrayMenuItem, TrayMenuToggleKind, TrayMenuToggleState,
};
use calloop::channel::Event as ChannelEvent;
use calloop::generic::Generic;
use calloop::timer::{TimeoutAction, Timer};
use calloop::{EventLoop, Interest, LoopHandle, Mode, PostAction};
use calloop_wayland_source::WaylandSource;
use log::{debug, error, info, warn};
use smithay_client_toolkit::reexports::protocols::xdg::shell::client::xdg_positioner;
use smithay_client_toolkit::{
    compositor::{CompositorHandler, CompositorState},
    delegate_compositor, delegate_layer, delegate_output, delegate_pointer, delegate_registry,
    delegate_seat, delegate_shm, delegate_xdg_popup, delegate_xdg_shell,
    output::{OutputHandler, OutputState},
    registry::{ProvidesRegistryState, RegistryState},
    registry_handlers,
    seat::{
        Capability, SeatHandler, SeatState,
        pointer::{
            BTN_LEFT, BTN_RIGHT, CursorIcon, PointerEvent, PointerEventKind, PointerHandler,
            ThemeSpec, ThemedPointer,
        },
    },
    shell::{
        WaylandSurface,
        wlr_layer::{
            Anchor, KeyboardInteractivity, Layer, LayerShell, LayerShellHandler, LayerSurface,
            LayerSurfaceConfigure,
        },
        xdg::{
            XdgPositioner, XdgShell,
            popup::{Popup, PopupConfigure, PopupHandler},
            window::{Window, WindowConfigure, WindowHandler},
        },
    },
    shm::{
        Shm, ShmHandler,
        slot::{Buffer, SlotPool},
    },
};

use crate::backlight::BacklightProducer;
use crate::bluetooth::BluetoothProducer;
use crate::command::{CommandSender, command_channel};
use crate::hypridle::HypridleProducer;
use crate::hyprland::HyprlandProducer;
use crate::networkmanager::NetworkProducer;
use crate::notifications::NotificationsProducer;
use crate::outputs::{OutputId, Outputs};
use crate::performance::PerformanceLogger;
use crate::power_profiles::{PowerProfilesProducer, PowerProfilesSettings};
use crate::producer::{Producer, ProducerBridge};
use crate::sni::SniHostProducer;
use crate::sysmon::SystemProducer;
use crate::updates::UpdatesProducer;
use crate::upower::UPowerProducer;
use crate::volume::VolumeProducer;
use wayland_client::{
    Connection, Dispatch, Proxy, QueueHandle,
    globals::registry_queue_init,
    protocol::{wl_output, wl_pointer, wl_region, wl_seat, wl_shm, wl_surface},
};

/// Layer-shell namespace (also the compositor-visible surface name).
const NAMESPACE: &str = "tablero";

/// Assumed width (px) for the initial shared-memory pool, before the compositor
/// reports the real output width via the first configure event.
const INITIAL_WIDTH: u32 = 1920;

/// Application-side phases captured for one rendered frame.
struct FrameTimings {
    layout: Option<Duration>,
    draw: Option<Duration>,
    blit: Option<Duration>,
    render: Option<Duration>,
    /// Time since this surface last painted. Separates a cold frame after a long
    /// idle (clocked-down CPU, evicted caches) from a warm one.
    idle_gap: Option<Duration>,
    stats: RenderStats,
    /// The part of the buffer this frame changed.
    damage: Damage,
}

/// One output's bar: its layer-shell surface plus the per-output render state.
///
/// The app holds one of these per Wayland output (see [`Outputs`]); each is
/// configured independently from the output's resolved [`Config`] and drawn into
/// the app's shared [`SlotPool`]. Geometry is kept in logical pixels; the buffer
/// is `scale`× larger and `set_buffer_scale` maps it back, exactly as the
/// single-surface bar did.
struct Surface {
    /// The output this surface is pinned to, so a `closed` event can find and
    /// drop just this bar.
    output_id: OutputId,
    output: wl_output::WlOutput,
    layer: LayerSurface,
    /// Surface dimensions in *logical* pixels (as the compositor reports them in
    /// `configure`). The shared-memory buffer is `scale`× larger; see
    /// [`Surface::draw`].
    width: u32,
    height: u32,
    /// The output's integer buffer scale. Drives the physical buffer size and the
    /// physical font size; `1` until the compositor reports otherwise.
    scale: Scale,
    /// This output's resolved configuration, retained so the physical font size
    /// can be re-resolved whenever the output scale changes.
    config: Config,
    /// Connector name (`DP-1`, …) for workspace scoping and config reloads.
    monitor: Option<String>,
    /// Widgets composing the bar, plus the dirty-flag redraw policy over them.
    dashboard: Dashboard,
    /// Reused software-render target (shared fonts + pixmap).
    ctx: RenderContext,
    /// Alternating SHM buffers so a free slot is reused without a new mmap when
    /// the compositor has released the previous frame.
    buffers: [Option<Buffer>; 2],
    /// Physical pixel size of the slots in [`buffers`].
    buffer_px: (u32, u32),
    /// Next double-buffer index to try (0 or 1).
    next_buffer: usize,
    /// When diagnostics are enabled, the start of the previous painted frame.
    last_frame: Option<Instant>,
    /// Set once the first configure has been received; drawing before that is
    /// invalid per the layer-shell protocol.
    configured: bool,
    /// Whether a frame has been committed yet, for the startup metric.
    presented: bool,
    /// Opt-in render timing diagnostics; disabled in the normal path.
    performance: PerformanceLogger,
    /// Pending-repaint state: changes request a frame, [`Surface::flush`] paints it.
    redraw: RedrawScheduler,
    /// Retained to request a frame callback with each commit.
    qh: QueueHandle<App>,
}

impl Surface {
    /// Build a bar pinned to `output`, configured from `config`.
    ///
    /// `monitor` is the output's connector name (`DP-1`, …), threaded into the
    /// dashboard so this surface's workspace widget shows only this monitor's
    /// workspaces and highlights its active one. `fonts` is the process-wide
    /// shared font set.
    #[allow(clippy::too_many_arguments)] // Wayland + config + shared fonts seed
    fn new(
        compositor: &CompositorState,
        layer_shell: &LayerShell,
        qh: &QueueHandle<App>,
        output: &wl_output::WlOutput,
        output_id: OutputId,
        monitor: Option<&str>,
        config: Config,
        fonts: SharedFonts,
        performance: PerformanceLogger,
    ) -> Self {
        // The bar reserves exactly its own height so windows tile beneath it.
        let height = config.height;
        let exclusive_zone = height as i32;

        let wl_surface = compositor.create_surface(qh);
        // Pinning the layer surface to `output` is what makes this one bar per
        // monitor instead of the compositor's default-output single surface.
        let layer = layer_shell.create_layer_surface(
            qh,
            wl_surface,
            Layer::Top,
            Some(NAMESPACE.to_string()),
            Some(output),
        );
        // Top bar spanning the full output width.
        layer.set_anchor(Anchor::TOP | Anchor::LEFT | Anchor::RIGHT);
        layer.set_keyboard_interactivity(KeyboardInteractivity::None);
        // Width 0 with left+right anchors lets the compositor stretch us to fit.
        layer.set_size(0, height);
        layer.set_exclusive_zone(exclusive_zone);
        // Initial commit with no buffer; the compositor replies with a configure.
        layer.commit();

        // The configured widget order drives which widgets are built and in what
        // order; `Dashboard::layout` tiles them into columns each frame, so these
        // initial bounds are just placeholders. The theme and font reach the
        // renderer through the context's settings.
        let full = Bounds::new(0, 0, INITIAL_WIDTH, height);
        let dashboard = config.build_dashboard(full, monitor);
        let ctx = RenderContext::with_fonts(INITIAL_WIDTH, height, config.render_settings(), fonts);

        Self {
            output_id,
            output: output.clone(),
            layer,
            width: INITIAL_WIDTH,
            height,
            // The compositor reports the real scale via `scale_factor_changed`,
            // typically before the first configure; until then assume an
            // unscaled output.
            scale: Scale::ONE,
            config,
            monitor: monitor.map(str::to_owned),
            dashboard,
            ctx,
            buffers: [None, None],
            buffer_px: (0, 0),
            next_buffer: 0,
            last_frame: None,
            configured: false,
            presented: false,
            performance,
            redraw: RedrawScheduler::new(),
            qh: qh.clone(),
        }
    }

    /// Rebuild layout and visuals from a new config (hot-reload).
    ///
    /// `seed` is replayed into the fresh dashboard so widgets keep the last
    /// producer snapshot instead of going blank until the next event.
    fn apply_config(&mut self, config: Config, seed: &[Msg]) {
        let height = config.height;
        let height_changed = self.height != height;
        if height_changed {
            self.height = height;
            self.layer.set_size(0, height);
            self.layer.set_exclusive_zone(height as i32);
            // Size change requests a new configure; still repaint at the current
            // geometry so theme/widget changes appear immediately.
            self.layer.commit();
            // Only drop SHM slots when geometry changes — dropping an attached
            // buffer while the compositor still holds it can tear down the surface.
            self.buffers = [None, None];
            self.buffer_px = (0, 0);
        }
        self.config = config;
        let full = Bounds::new(0, 0, self.width.max(1), self.height.max(1));
        self.dashboard = self.config.build_dashboard(full, self.monitor.as_deref());
        self.ctx
            .set_settings(self.config.scaled_render_settings(self.scale));
        // Rebuild clears widget state; replay the last producer snapshots and
        // a fresh clock tick so the bar does not go empty until the next event.
        replay_snapshot(&mut self.dashboard, seed);
        self.redraw.request("config-reload");
    }

    /// Whether this surface owns `wl_surface` — for routing pointer and scale
    /// events, which arrive keyed by the raw surface.
    fn owns(&self, wl_surface: &wl_surface::WlSurface) -> bool {
        self.layer.wl_surface() == wl_surface
    }

    /// Whether this surface is driven by `layer` — for routing configure and
    /// close events, which arrive keyed by the layer surface.
    fn is_layer(&self, layer: &LayerSurface) -> bool {
        self.layer.wl_surface() == layer.wl_surface()
    }

    /// Apply a message to the dashboard; request a repaint only if a widget
    /// reported a visible change. This is the steady-state redraw policy: the
    /// loop stays idle when an update changes nothing on screen, and a burst of
    /// changes shares the one frame [`Surface::flush`] paints.
    fn handle(&mut self, msg: &Msg) -> bool {
        let changed = self.dashboard.update(msg);
        if changed {
            self.redraw.request(message_kind(msg));
        }
        changed
    }

    /// Adopt a new output buffer scale.
    ///
    /// Re-resolves the physical font size from this output's configuration so
    /// text stays crisp at the new density, then requests a repaint so the
    /// buffer is reallocated at the new physical size. A no-op when the scale is
    /// unchanged.
    fn set_scale(&mut self, scale: Scale) -> bool {
        if self.scale == scale {
            return false;
        }
        self.scale = scale;
        self.ctx
            .set_settings(self.config.scaled_render_settings(scale));
        self.redraw.request("scale-change");
        true
    }

    /// Resolve a `button` press at surface coordinates `(x, y)` to a
    /// [`Command`], if any. Pure: the caller fans the command out to the
    /// executors. Clicks that hit no interactive region — empty space,
    /// display-only widgets, or off-surface negative coordinates — yield `None`.
    fn on_click(&self, x: f64, y: f64, button: ClickButton) -> Option<Command> {
        if x < 0.0 || y < 0.0 {
            return None;
        }
        // Pointer coordinates are surface-local *logical* pixels, but the widgets
        // are laid out in physical pixels, so the click is scaled by the same
        // factor before the half-open hit-test — the one conversion that keeps
        // input and layout in the same space.
        let s = self.scale.get() as f64;
        self.dashboard
            .on_click((x * s) as u32, (y * s) as u32, button)
    }

    /// Resolve what the pointer is over at surface-local logical coordinates:
    /// whether it is clickable and which tooltip it shows, in one hit-test.
    fn hover_at(&self, x: f64, y: f64) -> Hover {
        if x < 0.0 || y < 0.0 {
            return Hover::default();
        }
        let scale = self.scale.get() as f64;
        self.dashboard
            .hover_at((x * scale) as u32, (y * scale) as u32)
    }

    /// Resolve one logical scroll step against the widget under `(x, y)`.
    fn on_scroll(&self, x: f64, y: f64, direction: ScrollDirection) -> Option<Command> {
        if x < 0.0 || y < 0.0 {
            return None;
        }
        let scale = self.scale.get() as f64;
        self.dashboard
            .on_scroll((x * scale) as u32, (y * scale) as u32, direction)
    }

    /// Adopt the compositor's configure, seeding and requesting the first frame.
    fn configure(&mut self, configure: LayerSurfaceConfigure) {
        // A zero dimension means "you decide"; keep our current value.
        if configure.new_size.0 != 0 {
            self.width = configure.new_size.0;
        }
        if configure.new_size.1 != 0 {
            self.height = configure.new_size.1;
        }

        let first = !self.configured;
        self.configured = true;
        if first {
            // Lifecycle-forced frame: seed the clock so the bar shows the time
            // immediately, then paint whether or not anything changed.
            self.dashboard.update(&Msg::tick_now());
            self.redraw.request("configure");
        }
    }

    /// Paint the requested frame, if there is one and the compositor is ready for
    /// it. Returns whether a frame was committed.
    ///
    /// The host loop calls this once per dispatch, after every pending message
    /// has been applied, so however many changes arrived they share one repaint.
    /// Nothing is painted before the first configure (drawing earlier is invalid
    /// per the layer-shell protocol) or while the previous commit's frame
    /// callback is outstanding; the request stays pending in both cases.
    fn flush(&mut self, pool: &mut SlotPool) -> bool {
        if !self.configured {
            return false;
        }
        match self.redraw.take_due(Instant::now()) {
            Some(cause) => self.draw(pool, cause),
            None => false,
        }
    }

    /// Render the current dashboard state and commit it through the app's shared
    /// shared-memory pool. Returns whether the frame was committed.
    ///
    /// Uses two alternating SHM slots: when the compositor has released the
    /// next slot, its mmap is reused instead of allocating a new one.
    fn draw(&mut self, pool: &mut SlotPool, cause: &'static str) -> bool {
        let started = self.performance.start();

        // Logical surface dimensions scale up to the physical buffer the
        // compositor maps back down via `set_buffer_scale`. Everything below this
        // point — buffer, render target, layout, font — works in physical pixels.
        let (width, height) = self.scale.to_physical_size(self.width, self.height);
        let stride = width as i32 * 4;

        if self.buffer_px != (width, height) {
            self.buffers = [None, None];
            self.buffer_px = (width, height);
            // Nothing on screen matches a buffer of another size.
            self.dashboard.invalidate();
        }

        let idx = self.next_buffer;
        self.next_buffer = 1 - self.next_buffer;

        let can_reuse = self.buffers[idx]
            .as_ref()
            .is_some_and(|buf| !buf.slot().has_active_buffers());

        let timings = if can_reuse {
            match pool.canvas(self.buffers[idx].as_ref().unwrap()) {
                Some(canvas) => Some(self.paint_frame(canvas, width, height)),
                None => {
                    // Race: became active between the check and canvas(). Allocate.
                    self.paint_new_buffer(pool, idx, width, height, stride)
                }
            }
        } else {
            self.paint_new_buffer(pool, idx, width, height, stride)
        };
        let damage = timings.as_ref().map(|timings| timings.damage);
        let committed = damage.is_some_and(|damage| self.commit_buffer(idx, width, height, damage));
        if !committed {
            // The compositor never saw this frame, so the next one cannot be
            // described as a change relative to it.
            self.dashboard.invalidate();
        }
        let total_elapsed = started.map(|started| started.elapsed());
        let output = self.monitor.as_deref().unwrap_or("unknown");
        self.performance.record_duration(
            "frame-total",
            total_elapsed,
            format_args!(
                "cause={cause} output={output} width={width} height={height} reused={can_reuse} committed={committed} damage={}",
                damage.map_or_else(|| "none".to_owned(), damage_label)
            ),
        );
        if let Some(timings) = timings {
            self.record_frame_timings(timings, cause, width, height);
        }
        if committed && !self.presented {
            self.presented = true;
            self.performance.record_process_elapsed(
                "startup-to-first-commit",
                format_args!(
                    "output={} width={width} height={height}",
                    self.monitor.as_deref().unwrap_or("unknown")
                ),
            );
        }
        committed
    }

    fn paint_frame(&mut self, canvas: &mut [u8], width: u32, height: u32) -> FrameTimings {
        let started = self.performance.start();
        // Zero for a surface's first frame, so its counters are still logged.
        let idle_gap = started.map(|now| {
            self.last_frame
                .map_or(Duration::ZERO, |last| now.duration_since(last))
        });
        self.last_frame = started;
        self.ctx.resize(width, height);

        let phase_started = self.performance.start();
        self.dashboard.layout(&mut self.ctx, width, height);
        let damage = self.dashboard.take_damage();
        let layout = phase_started.map(|started| started.elapsed());

        let phase_started = self.performance.start();
        self.dashboard.draw(&mut self.ctx);
        let draw = phase_started.map(|started| started.elapsed());

        let phase_started = self.performance.start();
        write_argb8888(self.ctx.pixels(), canvas);
        let blit = phase_started.map(|started| started.elapsed());
        let render = started.map(|started| started.elapsed());
        FrameTimings {
            layout,
            draw,
            blit,
            render,
            idle_gap,
            // Taken even when diagnostics are off so the counters never wrap.
            stats: self.ctx.take_stats(),
            damage,
        }
    }

    fn record_frame_timings(
        &self,
        timings: FrameTimings,
        cause: &'static str,
        width: u32,
        height: u32,
    ) {
        let output = self.monitor.as_deref().unwrap_or("unknown");
        for (metric, elapsed) in [
            ("frame-layout", timings.layout),
            ("frame-draw", timings.draw),
            ("frame-blit", timings.blit),
            ("frame-render", timings.render),
        ] {
            self.performance.record_duration(
                metric,
                elapsed,
                format_args!("cause={cause} output={output} width={width} height={height}"),
            );
        }
        let RenderStats {
            text_shapes,
            text_cache_hits,
            glyph_pixels,
        } = timings.stats;
        self.performance.record_duration(
            "frame-idle-gap",
            timings.idle_gap,
            format_args!(
                "cause={cause} output={output} text_shapes={text_shapes} text_cache_hits={text_cache_hits} glyph_pixels={glyph_pixels}"
            ),
        );
    }

    fn paint_new_buffer(
        &mut self,
        pool: &mut SlotPool,
        idx: usize,
        width: u32,
        height: u32,
        stride: i32,
    ) -> Option<FrameTimings> {
        let (buffer, canvas) = match pool.create_buffer(
            width as i32,
            height as i32,
            stride,
            wl_shm::Format::Argb8888,
        ) {
            Ok(parts) => parts,
            Err(e) => {
                error!("failed to create shm buffer: {e}");
                return None;
            }
        };
        let timings = self.paint_frame(canvas, width, height);
        self.buffers[idx] = Some(buffer);
        Some(timings)
    }

    /// Attach slot `idx` and commit it, damaging only what `damage` covers so the
    /// compositor re-composites (and re-blurs) just that part of the strip. The
    /// buffer itself is always painted in full, so either slot is a complete
    /// frame whichever the compositor shows.
    fn commit_buffer(&mut self, idx: usize, width: u32, height: u32, damage: Damage) -> bool {
        let Some(buffer) = self.buffers[idx].as_ref() else {
            return false;
        };
        let surface = self.layer.wl_surface();
        // Tell the compositor the buffer holds `scale`× physical pixels per
        // logical pixel, so it maps the larger buffer back to the logical size.
        surface.set_buffer_scale(self.scale.get() as i32);
        let (x, y, w, h) = damage_rect(damage, width, height);
        surface.damage_buffer(x, y, w, h);
        if let Err(e) = buffer.attach_to(surface) {
            error!("failed to attach buffer: {e}");
            return false;
        }
        // Ask to be told when this frame has been shown; repaints wait for it.
        surface.frame(&self.qh, surface.clone());
        self.layer.commit();
        self.redraw.committed(Instant::now());
        true
    }
}

/// `damage` as a `wl_surface.damage_buffer` rectangle, clipped to the buffer.
fn damage_rect(damage: Damage, width: u32, height: u32) -> (i32, i32, i32, i32) {
    let full = Bounds::new(0, 0, width, height);
    let region = match damage {
        Damage::Full => full,
        Damage::Region(region) => region,
    };
    let x = region.x.min(width);
    let y = region.y.min(height);
    let w = region.width.min(width - x);
    let h = region.height.min(height - y);
    if w == 0 || h == 0 {
        // A change that paints nothing visible still has to present the buffer.
        return (0, 0, width as i32, height as i32);
    }
    (x as i32, y as i32, w as i32, h as i32)
}

fn damage_label(damage: Damage) -> String {
    match damage {
        Damage::Full => "full".to_owned(),
        Damage::Region(region) => format!("{}x{}", region.width, region.height),
    }
}

/// One visible hover tooltip, implemented as an xdg popup parented to a bar.
struct TooltipSurface {
    popup: Popup,
    output_id: OutputId,
    text: String,
    width: u32,
    height: u32,
    scale: Scale,
    background: (u8, u8, u8, u8),
    foreground: (u8, u8, u8, u8),
    ctx: RenderContext,
    configured: bool,
    performance: PerformanceLogger,
    requested_at: Option<Instant>,
}

const MENU_ROW_HEIGHT: u32 = 28;
const MENU_SEPARATOR_HEIGHT: u32 = 8;
const POPUP_RADIUS: f32 = 6.0;
const POPUP_PADDING_X: u32 = 10;
const POPUP_PADDING_Y: u32 = 5;
const POPUP_FALLBACK_BACKGROUND: (u8, u8, u8, u8) = (0x20, 0x22, 0x27, 0xF8);

#[derive(Clone)]
struct MenuRow {
    id: i32,
    depth: u32,
    label: String,
    enabled: bool,
    separator: bool,
    toggle: Option<(TrayMenuToggleKind, TrayMenuToggleState)>,
    has_children: bool,
}

impl MenuRow {
    fn height(&self) -> u32 {
        if self.separator {
            MENU_SEPARATOR_HEIGHT
        } else {
            MENU_ROW_HEIGHT
        }
    }

    fn activatable(&self) -> bool {
        self.enabled && !self.separator && !self.has_children
    }
}

fn flatten_menu(items: &[TrayMenuItem], depth: u32, rows: &mut Vec<MenuRow>) {
    for item in items.iter().filter(|item| item.visible) {
        rows.push(MenuRow {
            id: item.id,
            depth,
            label: item.label.clone(),
            enabled: item.enabled,
            separator: item.separator,
            toggle: item.toggle.map(|toggle| (toggle.kind, toggle.state)),
            has_children: !item.children.iter().all(|child| !child.visible),
        });
        flatten_menu(&item.children, depth + 1, rows);
    }
}

struct PendingTrayMenu {
    key: String,
    parent: LayerSurface,
    output_id: OutputId,
    anchor: (i32, i32),
    scale: Scale,
    settings: RenderSettings,
    serial: u32,
    seat: Option<wl_seat::WlSeat>,
    requested_at: Option<Instant>,
}

/// One interactive tray menu, rendered as an XDG popup parented to its bar.
struct TrayMenuSurface {
    popup: Popup,
    output_id: OutputId,
    key: String,
    revision: u32,
    rows: Vec<MenuRow>,
    width: u32,
    height: u32,
    scale: Scale,
    background: (u8, u8, u8, u8),
    foreground: (u8, u8, u8, u8),
    accent: (u8, u8, u8, u8),
    ctx: RenderContext,
    configured: bool,
    performance: PerformanceLogger,
    requested_at: Option<Instant>,
}

impl TrayMenuSurface {
    fn owns(&self, popup: &Popup) -> bool {
        self.popup == *popup
    }

    fn owns_surface(&self, surface: &wl_surface::WlSurface) -> bool {
        self.popup.wl_surface() == surface
    }

    fn row_at(&self, y: f64) -> Option<&MenuRow> {
        if y < 0.0 {
            return None;
        }
        let mut top = 0u32;
        for row in &self.rows {
            let bottom = top + row.height();
            if (y as u32) < bottom {
                return Some(row);
            }
            top = bottom;
        }
        None
    }

    fn command_at(&self, y: f64) -> Option<Command> {
        let row = self.row_at(y)?;
        row.activatable().then(|| Command::ActivateTrayMenuItem {
            key: self.key.clone(),
            id: row.id,
        })
    }

    fn update(&mut self, menu: &TrayMenu, pool: &mut SlotPool) -> bool {
        if menu.revision < self.revision {
            return true;
        }
        let mut rows = Vec::new();
        flatten_menu(&menu.items, 0, &mut rows);
        let height: u32 = rows.iter().map(MenuRow::height).sum();
        if height != self.height {
            // A structural resize requires a new popup position/configure. Close
            // this one rather than drawing against stale compositor geometry;
            // reopening immediately fetches the new revision.
            return false;
        }
        self.revision = menu.revision;
        self.rows = rows;
        self.draw(pool);
        true
    }

    fn draw(&mut self, pool: &mut SlotPool) {
        if !self.configured {
            return;
        }
        let scale = self.scale.get();
        let width = self.width * scale;
        let height = self.height * scale;
        let stride = width as i32 * 4;
        let (buffer, canvas) = match pool.create_buffer(
            width as i32,
            height as i32,
            stride,
            wl_shm::Format::Argb8888,
        ) {
            Ok(parts) => parts,
            Err(error) => {
                warn!("failed to create tray menu buffer: {error}");
                return;
            }
        };

        self.ctx.resize(width, height);
        // The context outlives a popup and the panel is translucent, so start
        // from clear pixels rather than blending over the previous paint.
        self.ctx.fill_background();
        self.ctx.fill_rounded_rect(
            Bounds::new(0, 0, width, height),
            self.background,
            POPUP_RADIUS * scale as f32,
        );
        let mut top = 0u32;
        for row in &self.rows {
            let row_height = row.height() * scale;
            if row.separator {
                self.ctx.fill_rounded_rect(
                    Bounds::new(
                        POPUP_PADDING_X * scale,
                        top + row_height / 2,
                        width - 2 * POPUP_PADDING_X * scale,
                        scale,
                    ),
                    dim_color(self.foreground),
                    0.0,
                );
                top += row_height;
                continue;
            }
            let prefix = match row.toggle {
                Some((TrayMenuToggleKind::Checkmark, TrayMenuToggleState::On)) => "[x] ",
                Some((TrayMenuToggleKind::Checkmark, _)) => "[ ] ",
                Some((TrayMenuToggleKind::Radio, TrayMenuToggleState::On)) => "(o) ",
                Some((TrayMenuToggleKind::Radio, _)) => "( ) ",
                None => "",
            };
            let suffix = if row.has_children { "  >" } else { "" };
            let label = format!("{prefix}{}{suffix}", row.label);
            let indent = (POPUP_PADDING_X + row.depth * 16) * scale;
            self.ctx.draw_text(
                &label,
                Bounds::new(
                    indent,
                    top,
                    width.saturating_sub(indent + POPUP_PADDING_X * scale),
                    row_height,
                ),
                if row.enabled {
                    if row
                        .toggle
                        .is_some_and(|(_, state)| state == TrayMenuToggleState::On)
                    {
                        self.accent
                    } else {
                        self.foreground
                    }
                } else {
                    dim_color(self.foreground)
                },
            );
            top += row_height;
        }
        write_argb8888(self.ctx.pixels(), canvas);
        self.popup
            .wl_surface()
            .set_buffer_scale(self.scale.get() as i32);
        self.popup
            .wl_surface()
            .damage_buffer(0, 0, width as i32, height as i32);
        if let Err(error) = buffer.attach_to(self.popup.wl_surface()) {
            warn!("failed to attach tray menu buffer: {error}");
            return;
        }
        self.popup.wl_surface().commit();
        self.performance.record_since(
            "tray-menu-input-to-commit",
            self.requested_at.take(),
            format_args!("output={} rows={}", self.output_id, self.rows.len()),
        );
    }
}

fn dim_color((r, g, b, a): (u8, u8, u8, u8)) -> (u8, u8, u8, u8) {
    (r / 2, g / 2, b / 2, a)
}

fn popup_background((r, g, b, a): (u8, u8, u8, u8)) -> (u8, u8, u8, u8) {
    if a < 0xC0 {
        POPUP_FALLBACK_BACKGROUND
    } else {
        (r, g, b, a.max(0xF0))
    }
}

impl TooltipSurface {
    fn owns(&self, popup: &Popup) -> bool {
        self.popup == *popup
    }

    fn owns_surface(&self, surface: &wl_surface::WlSurface) -> bool {
        self.popup.wl_surface() == surface
    }

    fn draw(&mut self, pool: &mut SlotPool) {
        if !self.configured {
            return;
        }
        let scale = self.scale.get();
        let width = self.width * scale;
        let height = self.height * scale;
        let stride = width as i32 * 4;
        let (buffer, canvas) = match pool.create_buffer(
            width as i32,
            height as i32,
            stride,
            wl_shm::Format::Argb8888,
        ) {
            Ok(parts) => parts,
            Err(error) => {
                warn!("failed to create tooltip buffer: {error}");
                return;
            }
        };

        paint_tooltip(
            &mut self.ctx,
            &self.text,
            (width, height),
            self.background,
            self.foreground,
        );
        write_argb8888(self.ctx.pixels(), canvas);
        self.popup
            .wl_surface()
            .set_buffer_scale(self.scale.get() as i32);
        self.popup
            .wl_surface()
            .damage_buffer(0, 0, width as i32, height as i32);
        if let Err(error) = buffer.attach_to(self.popup.wl_surface()) {
            warn!("failed to attach tooltip buffer: {error}");
            return;
        }
        self.popup.wl_surface().commit();
        self.performance.record_since(
            "tooltip-input-to-commit",
            self.requested_at.take(),
            format_args!("output={} width={width} height={height}", self.output_id),
        );
    }
}

/// Paint a tooltip panel of physical `size` holding `text` into `ctx`.
fn paint_tooltip(
    ctx: &mut RenderContext,
    text: &str,
    (width, height): (u32, u32),
    background: (u8, u8, u8, u8),
    foreground: (u8, u8, u8, u8),
) {
    let scale = ctx.scale_factor();
    ctx.resize(width, height);
    // The context outlives a popup and the panel is translucent, so start from
    // clear pixels rather than blending over the previous paint.
    ctx.fill_background();
    ctx.fill_rounded_rect(
        Bounds::new(0, 0, width, height),
        background,
        POPUP_RADIUS * scale as f32,
    );
    let padding_x = POPUP_PADDING_X * scale;
    let padding_y = POPUP_PADDING_Y * scale;
    let line_height = tooltip_line_height(ctx);
    for (index, line) in text.lines().enumerate() {
        ctx.draw_text(
            line,
            Bounds::new(
                padding_x,
                padding_y + index as u32 * line_height,
                width.saturating_sub(2 * padding_x),
                line_height,
            ),
            foreground,
        );
    }
}

fn tooltip_line_height(ctx: &RenderContext) -> u32 {
    (ctx.settings().font_size * 1.15).ceil() as u32
}

fn tooltip_size(ctx: &mut RenderContext, text: &str) -> (u32, u32) {
    let scale = ctx.scale_factor();
    let width = text
        .lines()
        .map(|line| ctx.measure_text(line))
        .max()
        .unwrap_or(0)
        + 2 * POPUP_PADDING_X * scale;
    let lines = text.lines().count().max(1) as u32;
    let height = lines * tooltip_line_height(ctx) + 2 * POPUP_PADDING_Y * scale;
    (width.max(1), height.max(1))
}

/// The shared application state and the calloop data type.
///
/// Owns everything common to every output — the Wayland registry, seat, shm and
/// shared [`SlotPool`], the compositor and layer-shell handles needed to build
/// new surfaces, the seat pointer, and the command executors — plus the
/// per-output bars in [`Outputs`]. Output hotplug drives surface create/teardown
/// through the [`OutputHandler`] callbacks; producer messages and clock ticks
/// fan out to every surface.
struct App {
    registry_state: RegistryState,
    output_state: OutputState,
    seat_state: SeatState,
    shm: Shm,
    /// Shared shared-memory pool every surface draws into; it grows as outputs
    /// and scales demand.
    pool: SlotPool,
    /// Retained to create a fresh surface each time an output appears.
    compositor: CompositorState,
    /// Retained to create a fresh layer surface per output.
    layer_shell: LayerShell,
    /// XDG shell global used to create layer-shell-associated tooltip popups.
    xdg_shell: XdgShell,
    /// The seat's pointer, created once the seat advertises the capability.
    pointer: Option<ThemedPointer>,
    pointer_seat: Option<wl_seat::WlSeat>,
    pointer_cursor: CursorIcon,
    /// Fractional logical vertical scroll steps retained across pointer frames.
    scroll_remainder: f64,
    /// Outbound command channels into the producer runtime, one per command
    /// executor (Hyprland workspace switching, SNI tray activation). A click's
    /// command is fanned out to every executor; each ignores the commands it does
    /// not handle. Empty when no bridge is running, in which case clicks are
    /// dropped.
    commands: Vec<CommandSender>,
    /// The per-output bars, keyed by output id. The whole multi-monitor lifecycle
    /// lives here.
    outputs: Outputs<Surface>,
    /// The currently visible tooltip; at most one pointer hover exists per seat.
    tooltip: Option<TooltipSurface>,
    pending_tray_menu: Option<PendingTrayMenu>,
    tray_menu: Option<TrayMenuSurface>,
    /// The render context of the last popup hidden, reused by the next one.
    popup_ctx: Option<RenderContext>,
    /// Process-wide font set shared by every surface and popup.
    fonts: SharedFonts,
    /// Watches app config and active/candidate theme dependencies.
    config_watcher: Option<config_reload::ConfigWatcher>,
    /// Latest producer messages, replayed after a dashboard rebuild on reload.
    producer_snapshot: ProducerSnapshot,
    /// Opt-in latency diagnostics shared with surfaces and popups.
    performance: PerformanceLogger,
    /// Workspace click awaiting the matching Hyprland state.
    pending_workspace: Option<PendingWorkspace>,
    /// Workspace click whose state has arrived, awaiting the bar commit.
    committing_workspace: Option<PendingWorkspace>,
    exit: bool,
}

struct PendingWorkspace {
    target: i32,
    monitor: Option<String>,
    started: Option<Instant>,
}

/// Latest producer payloads retained for config hot-reload reseeding.
///
/// Rebuilds replace every widget; without a snapshot the bar goes blank until
/// each source next emits (workspace/title/volume can stay empty for a long
/// time). Transient menu messages are not stored.
#[derive(Default)]
struct ProducerSnapshot {
    messages: Vec<Msg>,
}

fn replay_snapshot(dashboard: &mut Dashboard, seed: &[Msg]) {
    for msg in seed {
        let _ = dashboard.update(msg);
    }
    let _ = dashboard.update(&Msg::tick_now());
}

impl ProducerSnapshot {
    fn note(&mut self, msg: &Msg) {
        if matches!(
            msg,
            Msg::Tick(_) | Msg::TrayMenu(_) | Msg::TrayMenuUnavailable(_)
        ) {
            return;
        }
        let key = snapshot_key(msg);
        if let Some(slot) = self
            .messages
            .iter_mut()
            .find(|existing| snapshot_key(existing) == key)
        {
            *slot = msg.clone();
        } else {
            self.messages.push(msg.clone());
        }
    }

    fn as_slice(&self) -> &[Msg] {
        &self.messages
    }
}

/// Stable key for the latest-message map. Active-window is per-monitor so a
/// dual-head setup keeps both titles across reload.
fn message_kind(msg: &Msg) -> &'static str {
    match msg {
        Msg::Tick(_) => "tick",
        Msg::Workspaces(_) => "workspaces",
        Msg::Battery(_) => "battery",
        Msg::Backlight(_) => "backlight",
        Msg::System(_) => "system",
        Msg::Network(_) => "network",
        Msg::Bluetooth(_) => "bluetooth",
        Msg::Tray(_) => "tray",
        Msg::TrayMenu(_) => "tray-menu",
        Msg::TrayMenuUnavailable(_) => "tray-menu-unavailable",
        Msg::ActiveWindow { .. } => "active-window",
        Msg::Volume(_) => "volume",
        Msg::Notifications(_) => "notifications",
        Msg::PowerProfiles(_) => "power-profiles",
        Msg::Updates(_) => "updates",
        Msg::Hypridle(_) => "hypridle",
    }
}

fn snapshot_key(msg: &Msg) -> String {
    match msg {
        Msg::Tick(_) => "tick".into(),
        Msg::Workspaces(_) => "workspaces".into(),
        Msg::Battery(_) => "battery".into(),
        Msg::Backlight(_) => "backlight".into(),
        Msg::System(_) => "system".into(),
        Msg::Network(_) => "network".into(),
        Msg::Bluetooth(_) => "bluetooth".into(),
        Msg::Tray(_) => "tray".into(),
        Msg::TrayMenu(_) => "tray-menu".into(),
        Msg::TrayMenuUnavailable(_) => "tray-menu-unavail".into(),
        Msg::ActiveWindow { monitor, .. } => format!("aw:{monitor}"),
        Msg::Volume(_) => "volume".into(),
        Msg::Notifications(_) => "notifications".into(),
        Msg::PowerProfiles(_) => "power-profiles".into(),
        Msg::Updates(_) => "updates".into(),
        Msg::Hypridle(_) => "hypridle".into(),
    }
}

impl App {
    /// Create — or, if already tracked, keep — the bar for `output`.
    ///
    /// Idempotent via [`Outputs::ensure`]: a repeated `new_output`/`update_output`
    /// for an output we already show never rebuilds or replaces its surface. The
    /// connector name selects the output's resolved config and scopes its
    /// workspace widget to that monitor.
    fn add_output(&mut self, output: wl_output::WlOutput, qh: &QueueHandle<App>) {
        let id = output_key(&output);
        let name = self.output_state.info(&output).and_then(|info| info.name);
        // Pre-borrow the build inputs as locals so `ensure`'s mutable borrow of
        // `self.outputs` and these shared borrows of other fields stay disjoint.
        let compositor = &self.compositor;
        let layer_shell = &self.layer_shell;
        let fonts = self.fonts.clone();
        let performance = self.performance.clone();
        let built = self.outputs.ensure(id, name.as_deref(), |config| {
            Surface::new(
                compositor,
                layer_shell,
                qh,
                &output,
                id,
                name.as_deref(),
                config,
                fonts,
                performance,
            )
        });
        if built {
            debug!(
                "output {id} ({}) added; {} bar(s) live",
                name.as_deref().unwrap_or("<unnamed>"),
                self.outputs.len()
            );
        }
    }

    /// Apply a freshly loaded config to every live bar (theme, layout, widgets).
    ///
    /// Producers already running stay as-is; newly required modules need a
    /// process restart. Invalid files are logged and ignored by the watcher.
    fn reload_config(&mut self, config: Config) {
        self.outputs.set_base(config);
        self.hide_tooltip();
        self.hide_tray_menu();
        let base = self.outputs.base().clone();
        let seed: Vec<Msg> = self.producer_snapshot.as_slice().to_vec();
        let reloads: Vec<(OutputId, Config)> = self
            .outputs
            .values()
            .map(|surface| {
                let name = self
                    .output_state
                    .info(&surface.output)
                    .and_then(|info| info.name);
                (surface.output_id, base.resolve_for_output(name.as_deref()))
            })
            .collect();
        for (id, resolved) in reloads {
            if let Some(surface) = self.outputs.get_mut(id) {
                surface.apply_config(resolved, &seed);
            }
        }
        info!("config reloaded");
    }

    /// Poll app/theme dependencies and apply only fully validated candidates.
    ///
    /// Debounces ~400ms so editors that truncate-then-write do not apply an
    /// empty mid-save document (which would parse as defaults and wipe the bar).
    fn poll_config_reload(&mut self) {
        if let Some(candidate) = self
            .config_watcher
            .as_mut()
            .and_then(|watcher| watcher.poll(Instant::now()))
        {
            match candidate {
                Ok(config) if &config != self.outputs.base() => self.reload_config(config),
                Ok(_) => {}
                Err(error) => warn!("config reload ignored: {error}"),
            }
        }
    }

    /// Tear down the bar for `output`, if any. The dropped [`LayerSurface`]
    /// destroys the layer-shell surface, so an unplugged monitor leaves no stale
    /// state behind.
    fn remove_output(&mut self, output: &wl_output::WlOutput) {
        let id = output_key(output);
        if self
            .tray_menu
            .as_ref()
            .is_some_and(|menu| menu.output_id == id)
        {
            self.hide_tray_menu();
        }
        if self.outputs.remove(id).is_some() {
            debug!("output {id} removed; {} bar(s) live", self.outputs.len());
        }
    }

    /// Fan a message out to every output's dashboard; the ones that changed
    /// repaint at the next [`App::flush_redraws`]. The clock timer and every
    /// producer reach all bars this way.
    fn handle_all(&mut self, msg: &Msg) {
        self.producer_snapshot.note(msg);
        let mut changed = false;
        for surface in self.outputs.values_mut() {
            changed |= surface.handle(msg);
        }
        if changed && matches!(msg, Msg::PowerProfiles(_)) {
            self.hide_tooltip();
        }
        if changed
            && let Msg::Workspaces(workspaces) = msg
            && self.pending_workspace.as_ref().is_some_and(|pending| {
                pending.monitor.as_deref().map_or_else(
                    || workspaces.active() == pending.target,
                    |monitor| workspaces.active_for(monitor) == Some(pending.target),
                )
            })
        {
            self.committing_workspace = self.pending_workspace.take();
        }
    }

    /// Paint every bar with a repaint pending. Runs once per loop dispatch, after
    /// all of that dispatch's messages and Wayland events have been applied.
    fn flush_redraws(&mut self) {
        let App { pool, outputs, .. } = self;
        let mut committed = false;
        for surface in outputs.values_mut() {
            committed |= surface.flush(pool);
        }
        if committed && let Some(pending) = self.committing_workspace.take() {
            self.performance.record_since(
                "workspace-input-to-commit",
                pending.started,
                format_args!(
                    "target={} output={}",
                    pending.target,
                    pending.monitor.as_deref().unwrap_or("unknown")
                ),
            );
        }
    }

    fn handle_message(&mut self, msg: &Msg, qh: &QueueHandle<App>) {
        if let Msg::TrayMenu(menu) = msg {
            if let Some(shown) = self
                .tray_menu
                .as_mut()
                .filter(|shown| shown.key == menu.key)
            {
                if !shown.update(menu, &mut self.pool) {
                    self.tray_menu = None;
                }
            } else {
                self.show_tray_menu(menu, qh);
            }
        } else if let Msg::TrayMenuUnavailable(key) = msg {
            if self
                .pending_tray_menu
                .as_ref()
                .is_some_and(|pending| pending.key == *key)
            {
                self.pending_tray_menu = None;
            }
        } else {
            self.handle_all(msg);
        }
    }

    fn hide_tooltip(&mut self) {
        if let Some(tooltip) = self.tooltip.take() {
            self.popup_ctx = Some(tooltip.ctx);
        }
    }

    fn hide_tray_menu(&mut self) {
        self.pending_tray_menu = None;
        if let Some(menu) = self.tray_menu.take() {
            self.popup_ctx = Some(menu.ctx);
        }
    }

    /// A render context for a new popup, painting with `settings`.
    ///
    /// The context of the last popup hidden is handed back here rather than
    /// dropped, so a tooltip shown again finds its lines already shaped in the
    /// text cache instead of starting from an empty context on every hover.
    fn popup_context(&mut self, settings: RenderSettings) -> RenderContext {
        match self.popup_ctx.take() {
            Some(mut ctx) => {
                ctx.set_settings(settings);
                ctx
            }
            None => RenderContext::with_fonts(1, 1, settings, self.fonts.clone()),
        }
    }

    fn set_pointer_cursor(&mut self, conn: &Connection, icon: CursorIcon, force: bool) {
        if !force && self.pointer_cursor == icon {
            return;
        }
        let Some(pointer) = &self.pointer else {
            return;
        };
        match pointer.set_cursor(conn, icon) {
            Ok(()) => self.pointer_cursor = icon,
            Err(error) => warn!("failed to set pointer cursor: {error}"),
        }
    }

    /// Show `tooltip` under the bar on `output_id`, or hide the current one when
    /// the pointer is over nothing that has one.
    fn update_tooltip(
        &mut self,
        output_id: OutputId,
        tooltip: Option<Tooltip>,
        qh: &QueueHandle<App>,
    ) {
        let Some(tooltip) = tooltip else {
            self.hide_tooltip();
            return;
        };
        // Most motion stays over the widget whose tooltip is already up, so that
        // is settled before anything is cloned or measured.
        if self
            .tooltip
            .as_ref()
            .is_some_and(|shown| shown.output_id == output_id && shown.text == tooltip.text)
        {
            return;
        }
        let requested_at = self.performance.start();
        let Some(bar) = self.outputs.get(output_id) else {
            self.hide_tooltip();
            return;
        };
        let parent = bar.layer.clone();
        let scale = bar.scale;
        let mut settings = bar.config.scaled_render_settings(scale);

        let background = popup_background(settings.background);
        let foreground = settings.foreground;
        settings.background = (0, 0, 0, 0);
        self.hide_tooltip();
        let mut ctx = self.popup_context(settings);
        let (physical_width, physical_height) = tooltip_size(&mut ctx, &tooltip.text);
        let divisor = scale.get();
        let width = physical_width.div_ceil(divisor);
        let height = physical_height.div_ceil(divisor);
        let anchor = Bounds::new(
            tooltip.bounds.x / divisor,
            tooltip.bounds.y / divisor,
            tooltip.bounds.width.div_ceil(divisor),
            tooltip.bounds.height.div_ceil(divisor),
        );

        let positioner = match XdgPositioner::new(&self.xdg_shell) {
            Ok(positioner) => positioner,
            Err(error) => {
                warn!("failed to create tooltip positioner: {error}");
                return;
            }
        };
        positioner.set_size(width as i32, height as i32);
        positioner.set_anchor_rect(
            anchor.x as i32,
            anchor.y as i32,
            anchor.width.max(1) as i32,
            anchor.height.max(1) as i32,
        );
        positioner.set_anchor(xdg_positioner::Anchor::Bottom);
        positioner.set_gravity(xdg_positioner::Gravity::Bottom);
        positioner.set_constraint_adjustment(xdg_positioner::ConstraintAdjustment::SlideX);

        let popup_surface = self.compositor.create_surface(qh);
        let popup = match Popup::from_surface(None, &positioner, qh, popup_surface, &self.xdg_shell)
        {
            Ok(popup) => popup,
            Err(error) => {
                warn!("failed to create tooltip popup: {error}");
                return;
            }
        };
        parent.get_popup(popup.xdg_popup());
        let input_region = self.compositor.wl_compositor().create_region(qh, ());
        popup.wl_surface().set_input_region(Some(&input_region));
        input_region.destroy();
        popup.wl_surface().commit();
        self.tooltip = Some(TooltipSurface {
            popup,
            output_id,
            text: tooltip.text,
            width,
            height,
            scale,
            background,
            foreground,
            ctx,
            configured: false,
            performance: self.performance.clone(),
            requested_at,
        });
    }

    fn show_tray_menu(&mut self, menu: &TrayMenu, qh: &QueueHandle<App>) {
        let Some(pending) = self
            .pending_tray_menu
            .take()
            .filter(|pending| pending.key == menu.key)
        else {
            return;
        };
        let mut rows = Vec::new();
        flatten_menu(&menu.items, 0, &mut rows);
        if rows.is_empty() {
            return;
        }

        let mut settings = pending.settings;
        let background = popup_background(settings.background);
        let foreground = settings.foreground;
        let accent = settings.accent;
        settings.background = (0, 0, 0, 0);
        self.hide_tooltip();
        let mut ctx = self.popup_context(settings);
        let scale = pending.scale.get();
        let physical_width = rows
            .iter()
            .filter(|row| !row.separator)
            .map(|row| {
                let indicators = if row.toggle.is_some() { 4 } else { 0 };
                let submenu = if row.has_children { 3 } else { 0 };
                let text = format!(
                    "{}{}{}",
                    " ".repeat(indicators),
                    row.label,
                    " ".repeat(submenu)
                );
                ctx.measure_text(&text) + (32 + row.depth * 16) * scale
            })
            .max()
            .unwrap_or(1);
        let width = physical_width.div_ceil(scale).clamp(120, 420);
        let height: u32 = rows.iter().map(MenuRow::height).sum();

        let positioner = match XdgPositioner::new(&self.xdg_shell) {
            Ok(positioner) => positioner,
            Err(error) => {
                warn!("failed to create tray menu positioner: {error}");
                return;
            }
        };
        positioner.set_size(width as i32, height as i32);
        positioner.set_anchor_rect(pending.anchor.0, pending.anchor.1, 1, 1);
        positioner.set_anchor(xdg_positioner::Anchor::BottomLeft);
        positioner.set_gravity(xdg_positioner::Gravity::BottomRight);
        positioner.set_constraint_adjustment(xdg_positioner::ConstraintAdjustment::SlideX);

        let popup_surface = self.compositor.create_surface(qh);
        let popup = match Popup::from_surface(None, &positioner, qh, popup_surface, &self.xdg_shell)
        {
            Ok(popup) => popup,
            Err(error) => {
                warn!("failed to create tray menu popup: {error}");
                return;
            }
        };
        pending.parent.get_popup(popup.xdg_popup());
        if let Some(seat) = &pending.seat {
            popup.xdg_popup().grab(seat, pending.serial);
        }
        popup.wl_surface().commit();
        self.tray_menu = Some(TrayMenuSurface {
            popup,
            output_id: pending.output_id,
            key: menu.key.clone(),
            revision: menu.revision,
            rows,
            width,
            height,
            scale: pending.scale,
            background,
            foreground,
            accent,
            ctx,
            configured: false,
            performance: self.performance.clone(),
            requested_at: pending.requested_at,
        });
    }
}

/// The stable per-output key: the `wl_output`'s protocol object id.
///
/// Available in both `new_output` and `output_destroyed` (unlike the output's
/// advertised info, which the compositor may already have dropped by teardown),
/// and unique per output for its lifetime — exactly the key the registry needs.
fn output_key(output: &wl_output::WlOutput) -> OutputId {
    output.id().protocol_id()
}

fn command_kind(command: &Command) -> &'static str {
    match command {
        Command::SwitchWorkspace(_) => "switch-workspace",
        Command::ActivateTrayItem { .. } => "activate-tray-item",
        Command::OpenTrayMenu { .. } => "open-tray-menu",
        Command::ActivateTrayMenuItem { .. } => "activate-tray-menu-item",
        Command::RunProgram(_) => "run-program",
        Command::ToggleNotificationPanel => "toggle-notification-panel",
        Command::ToggleNotificationsDnd => "toggle-notifications-dnd",
        Command::AdjustBacklight { .. } => "adjust-backlight",
        Command::SetPowerProfile(_) => "set-power-profile",
        Command::SetHypridle(_) => "set-hypridle",
    }
}

fn set_tray_command_position(command: &mut Command, origin: (i32, i32), local: (f64, f64)) {
    let screen = (
        origin.0.saturating_add(local.0 as i32),
        origin.1.saturating_add(local.1 as i32),
    );
    match command {
        Command::ActivateTrayItem { x, y, .. } | Command::OpenTrayMenu { x, y, .. } => {
            *x = screen.0;
            *y = screen.1;
        }
        _ => {}
    }
}

/// Open the bar and run its event loop until the compositor closes the surface.
///
/// The bar's height, theme, font, spacing, and widget order all come from
/// `config` (see [`crate::config::Config`]). Wires producers for the widgets
/// that appear in the global layout or any per-monitor override
/// ([`Config::uses_widget`]): Hyprland always runs (workspaces / title);
/// UPower, sysmon, NetworkManager, BlueZ, PipeWire volume, the SNI tray host,
/// swaync, power-profiles-daemon, backlight, Arch updates, and Hypridle start
/// only when their module is configured. The volume source uses a dedicated OS
/// thread (PipeWire's main loop is synchronous). The clock is driven by the
/// synchronous tick timer. When `config_path` is set, the file is polled for
/// app/theme file changes and the bar hot-reloads theme/layout (producers keep their
/// original set until restart). See [`run_with_producers`] for a custom set.
pub fn run(config: Config, config_path: Option<PathBuf>) -> Result<(), Box<dyn Error>> {
    let mut producers: Vec<Box<dyn Producer>> = vec![Box::new(HyprlandProducer::new())];
    // Gate non-Hyprland sources so a minimal bar does not open PipeWire, host a
    // StatusNotifierWatcher, or poll sysfs/backlight unnecessarily.
    if config.uses_widget(WidgetKind::Battery) {
        producers.push(Box::new(UPowerProducer::new()));
    }
    if config.uses_widget(WidgetKind::Backlight) {
        producers.push(Box::new(BacklightProducer::new()));
    }
    if config.uses_widget(WidgetKind::System) {
        producers.push(Box::new(
            config
                .widget
                .system
                .interval()
                .map_or_else(SystemProducer::new, SystemProducer::with_interval),
        ));
    }
    if config.uses_widget(WidgetKind::Network) {
        producers.push(Box::new(NetworkProducer::new()));
    }
    if config.uses_widget(WidgetKind::Bluetooth) {
        producers.push(Box::new(BluetoothProducer::new()));
    }
    if config.uses_widget(WidgetKind::Volume) {
        producers.push(Box::new(VolumeProducer::new()));
    }
    if config.uses_widget(WidgetKind::Tray) {
        producers.push(Box::new(SniHostProducer::new()));
    }
    if config.uses_widget(WidgetKind::Notifications) {
        producers.push(Box::new(NotificationsProducer::new()));
    }
    if config.uses_widget(WidgetKind::PowerProfilesDaemon) {
        producers.push(Box::new(
            PowerProfilesProducer::new().with_settings(power_profiles_settings(&config)),
        ));
    }
    if config.uses_widget(WidgetKind::Updates) {
        producers.push(Box::new(
            config
                .widget
                .updates
                .interval()
                .map_or_else(UpdatesProducer::new, UpdatesProducer::with_interval),
        ));
    }
    if config.uses_widget(WidgetKind::Hypridle) {
        producers.push(Box::new(
            config
                .widget
                .hypridle
                .interval()
                .map_or_else(HypridleProducer::new, HypridleProducer::with_interval),
        ));
    }
    run_with_producers(config, producers, config_path)
}

/// Run the config poll timer until the watcher no longer needs one. `polling`
/// keeps a burst of directory events from stacking up timers.
fn start_config_polling(handle: &LoopHandle<'static, App>, polling: &Rc<Cell<bool>>) {
    if polling.replace(true) {
        return;
    }
    let polling = polling.clone();
    let inserted = handle.insert_source(Timer::immediate(), move |_deadline, _, app| {
        app.poll_config_reload();
        match app.config_watcher.as_ref().and_then(|w| w.next_poll()) {
            Some(delay) => TimeoutAction::ToDuration(delay),
            None => {
                polling.set(false);
                TimeoutAction::Drop
            }
        }
    });
    if let Err(error) = inserted {
        error!("config reload timer could not start: {error}");
    }
}

fn power_profiles_settings(config: &Config) -> PowerProfilesSettings {
    PowerProfilesSettings::new(
        config
            .widget
            .power_profiles_daemon
            .hardware_control
            .unwrap_or(true),
        config
            .widget
            .power_profiles_daemon
            .hardware_helper
            .as_ref()
            .map(PathBuf::from),
    )
}

/// Open the bar and run its event loop, additionally driving `producers` on an
/// off-thread Tokio runtime.
///
/// The render loop stays fully synchronous: it owns the dashboards, rendering,
/// and Wayland commits. Each producer runs on the [`ProducerBridge`] runtime and
/// reaches the loop only by sending [`Msg`]s through a calloop channel, which is
/// fanned out to every output's dashboard via the app's message handler exactly like
/// the clock timer. With an empty `producers` list no runtime is started at all.
///
/// During setup, one Wayland roundtrip discovers the initial outputs and opens
/// their layer surfaces before producers start. Later outputs still arrive
/// through [`OutputHandler::new_output`], and `output_destroyed` tears each down,
/// so plugging or unplugging a monitor adds or removes its bar without restarting
/// the loop. `config_path` enables hot-reload of the TOML config and selected theme.
pub fn run_with_producers(
    config: Config,
    producers: Vec<Box<dyn Producer>>,
    config_path: Option<PathBuf>,
) -> Result<(), Box<dyn Error>> {
    let performance = PerformanceLogger::from_env();
    let power_settings = power_profiles_settings(&config);
    let height = config.height;
    let config_watcher = config_path.map(|path| config_reload::ConfigWatcher::new(path, &config));
    let fonts = shared_fonts();

    let conn = Connection::connect_to_env()?;
    let (globals, mut event_queue) = registry_queue_init::<App>(&conn)?;
    let qh = event_queue.handle();

    let compositor = CompositorState::bind(&globals, &qh)?;
    let layer_shell = LayerShell::bind(&globals, &qh)?;
    let xdg_shell = XdgShell::bind(&globals, &qh)?;
    let shm = Shm::bind(&globals, &qh)?;

    // Size the shared pool for one full-width bar at the default height; it grows
    // automatically as further outputs and higher scales demand more buffers.
    // Double-buffering uses two slots per surface, so seed a little larger.
    let pool = SlotPool::new((INITIAL_WIDTH * height * 4 * 2) as usize, &shm)?;

    let mut app = App {
        registry_state: RegistryState::new(&globals),
        output_state: OutputState::new(&globals, &qh),
        seat_state: SeatState::new(&globals, &qh),
        shm,
        pool,
        compositor,
        layer_shell,
        xdg_shell,
        pointer: None,
        pointer_seat: None,
        pointer_cursor: CursorIcon::Default,
        scroll_remainder: 0.0,
        commands: Vec::new(),
        outputs: Outputs::new(config),
        tooltip: None,
        pending_tray_menu: None,
        tray_menu: None,
        popup_ctx: None,
        fonts,
        config_watcher,
        producer_snapshot: ProducerSnapshot::default(),
        performance,
        pending_workspace: None,
        committing_workspace: None,
        exit: false,
    };

    // Finish initial output discovery before producers can emit snapshots. If a
    // producer starts first, its one-shot initial state is dispatched while
    // `outputs` is empty and is lost until that source changes again.
    event_queue.roundtrip(&mut app)?;

    let mut event_loop: EventLoop<App> = EventLoop::try_new()?;
    let handle = event_loop.handle();

    // Wayland events (output advertisements, configure, close, ...) wake the loop.
    WaylandSource::new(conn, event_queue).insert(handle.clone())?;

    // A timer aligned to the wall-clock minute wakes the loop for each tick. The
    // clock renders HH:MM, so once-per-minute keeps it correct while the loop
    // stays idle the rest of the minute.
    let timer = Timer::from_duration(Duration::from_millis(millis_until_next_minute()));
    handle.insert_source(timer, |_deadline, _, app| {
        app.handle_all(&Msg::tick_now());
        TimeoutAction::ToDuration(Duration::from_millis(millis_until_next_minute()))
    })?;

    // Theme/layout edits apply without restarting. Directory events say when to
    // look, so an idle bar has no reload timer at all: the poll timer runs only
    // while an edit settles (and for the first validation of the on-disk state),
    // or permanently if the files cannot be watched.
    if let Some(watcher) = &app.config_watcher {
        let polling = Rc::new(Cell::new(false));
        if let Some(fd) = watcher.event_fd() {
            let (timer_handle, polling) = (handle.clone(), polling.clone());
            let events = Generic::new(fd, Interest::READ, Mode::Level);
            handle.insert_source(events, move |_, fd, _app| {
                config_reload::drain_events(&**fd);
                start_config_polling(&timer_handle, &polling);
                Ok(PostAction::Continue)
            })?;
        }
        start_config_polling(&handle, &polling);
    }

    // Bring up the async producer bridge only when there is async work to do.
    // The bridge owns the Tokio runtime and must outlive the loop, so it is held
    // in `_bridge` until the function returns.
    let _bridge = if producers.is_empty() {
        None
    } else {
        let (bridge, channel) = ProducerBridge::new()?;
        let message_qh = qh.clone();
        handle.insert_source(channel, move |event, _, app| {
            // Producer messages cross the channel into the same synchronous
            // app-state update path the clock timer uses, fanned out to every bar.
            if let ChannelEvent::Msg(msg) = event {
                app.handle_message(&msg, &message_qh);
            }
        })?;
        for producer in producers {
            bridge.spawn(producer);
        }
        // The reverse path: clicks become commands the executors run against the
        // compositor, the session bus, or a user-configured program. The loop
        // holds a sender per executor and fans each command out to all of them;
        // an executor ignores commands it does not handle, so workspace
        // switches reach Hyprland, tray activations reach the SNI items, and
        // configured on-click programs spawn directly via
        // `command::run_commands` — without the loop routing them.
        let (hypr_tx, hypr_rx) = command_channel();
        bridge.spawn_task("hyprland-commands", hyprland::run_commands(hypr_rx));
        let (sni_tx, sni_rx) = command_channel();
        let sni_updates = bridge.sender();
        bridge.spawn_task("sni-commands", sni::run_commands(sni_rx, sni_updates));
        let (run_tx, run_rx) = command_channel();
        bridge.spawn_task("run-commands", command::run_commands(run_rx));
        let (notif_tx, notif_rx) = command_channel();
        bridge.spawn_task(
            "notifications-commands",
            notifications::run_commands(notif_rx),
        );
        let (backlight_tx, backlight_rx) = command_channel();
        let backlight_updates = bridge.sender();
        bridge.spawn_task(
            "backlight-commands",
            backlight::run_commands(backlight_rx, backlight_updates),
        );
        let (power_tx, power_rx) = command_channel();
        bridge.spawn_task(
            "power-profiles-commands",
            power_profiles::run_commands(power_rx, power_settings),
        );
        let (hypridle_tx, hypridle_rx) = command_channel();
        let hypridle_updates = bridge.sender();
        bridge.spawn_task(
            "hypridle-commands",
            hypridle::run_commands(hypridle_rx, hypridle_updates),
        );
        app.commands = vec![
            hypr_tx,
            sni_tx,
            run_tx,
            notif_tx,
            backlight_tx,
            power_tx,
            hypridle_tx,
        ];
        Some(bridge)
    };

    // A configure handled during the roundtrip above has a frame waiting.
    app.flush_redraws();

    let signal = event_loop.get_signal();
    event_loop.run(None, &mut app, move |app| {
        // Requests made here go out before the loop next sleeps.
        app.flush_redraws();
        if app.exit {
            info!("all surfaces closed; shutting down");
            signal.stop();
        }
    })?;

    Ok(())
}

impl CompositorHandler for App {
    fn scale_factor_changed(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        surface: &wl_surface::WlSurface,
        new_factor: i32,
    ) {
        // The compositor reports a surface's preferred integer buffer scale.
        // Route it to the owning bar so each output renders at its own density.
        let App {
            outputs,
            tooltip,
            tray_menu,
            ..
        } = self;
        let scale = Scale::new(new_factor);
        let changed_output = outputs
            .values_mut()
            .find(|bar| bar.owns(surface))
            .and_then(|bar| bar.set_scale(scale).then_some(bar.output_id));
        if tooltip.as_ref().is_some_and(|shown| {
            (shown.owns_surface(surface) && shown.scale != scale)
                || changed_output == Some(shown.output_id)
        }) {
            *tooltip = None;
        }
        if tray_menu.as_ref().is_some_and(|shown| {
            (shown.owns_surface(surface) && shown.scale != scale)
                || changed_output == Some(shown.output_id)
        }) {
            *tray_menu = None;
        }
    }

    fn transform_changed(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        _surface: &wl_surface::WlSurface,
        _new_transform: wl_output::Transform,
    ) {
    }

    fn frame(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        surface: &wl_surface::WlSurface,
        _time: u32,
    ) {
        // Requested only alongside a commit, so this never becomes a redraw
        // cycle: it releases the repaint (if any) that waited for this frame,
        // which the loop's post-dispatch flush then paints.
        if let Some(bar) = self.outputs.values_mut().find(|bar| bar.owns(surface)) {
            bar.redraw.frame_done();
        }
    }

    fn surface_enter(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        _surface: &wl_surface::WlSurface,
        _output: &wl_output::WlOutput,
    ) {
    }

    fn surface_leave(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        _surface: &wl_surface::WlSurface,
        _output: &wl_output::WlOutput,
    ) {
    }
}

impl OutputHandler for App {
    fn output_state(&mut self) -> &mut OutputState {
        &mut self.output_state
    }

    fn new_output(
        &mut self,
        _conn: &Connection,
        qh: &QueueHandle<Self>,
        output: wl_output::WlOutput,
    ) {
        // A monitor appeared: open its bar.
        self.add_output(output, qh);
    }

    fn update_output(
        &mut self,
        _conn: &Connection,
        qh: &QueueHandle<Self>,
        output: wl_output::WlOutput,
    ) {
        // A property change on an output we already show is a no-op (ensure keeps
        // the live surface); a brand-new output we somehow missed gets its bar.
        self.add_output(output, qh);
    }

    fn output_destroyed(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        output: wl_output::WlOutput,
    ) {
        // A monitor was unplugged: drop its bar, leaving the others untouched.
        self.remove_output(&output);
    }
}

impl LayerShellHandler for App {
    fn closed(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, layer: &LayerSurface) {
        // The compositor closed one of our layers: drop just that surface. Exit
        // only once the last bar is gone, matching the single-output shutdown.
        if let Some(id) = self
            .outputs
            .values()
            .find(|bar| bar.is_layer(layer))
            .map(|bar| bar.output_id)
        {
            self.outputs.remove(id);
        }
        if self.outputs.is_empty() {
            self.exit = true;
        }
    }

    fn configure(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        layer: &LayerSurface,
        configure: LayerSurfaceConfigure,
        _serial: u32,
    ) {
        if let Some(bar) = self.outputs.values_mut().find(|bar| bar.is_layer(layer)) {
            bar.configure(configure);
        }
    }
}

impl PopupHandler for App {
    fn configure(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        popup: &Popup,
        _config: PopupConfigure,
    ) {
        if let Some(tooltip) = self.tooltip.as_mut().filter(|tooltip| tooltip.owns(popup)) {
            tooltip.configured = true;
            tooltip.draw(&mut self.pool);
        } else if let Some(menu) = self.tray_menu.as_mut().filter(|menu| menu.owns(popup)) {
            menu.configured = true;
            menu.draw(&mut self.pool);
        }
    }

    fn done(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, popup: &Popup) {
        if self
            .tooltip
            .as_ref()
            .is_some_and(|tooltip| tooltip.owns(popup))
        {
            self.hide_tooltip();
        } else if self.tray_menu.as_ref().is_some_and(|menu| menu.owns(popup)) {
            self.hide_tray_menu();
        }
    }
}

// XdgShell's dispatch helper also covers toplevel objects. Tablero creates only
// popups, so these callbacks are unreachable but satisfy the shared dispatcher.
impl WindowHandler for App {
    fn request_close(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _window: &Window) {}

    fn configure(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        _window: &Window,
        _configure: WindowConfigure,
        _serial: u32,
    ) {
    }
}

impl ShmHandler for App {
    fn shm_state(&mut self) -> &mut Shm {
        &mut self.shm
    }
}

impl SeatHandler for App {
    fn seat_state(&mut self) -> &mut SeatState {
        &mut self.seat_state
    }

    fn new_seat(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _seat: wl_seat::WlSeat) {}

    fn new_capability(
        &mut self,
        _conn: &Connection,
        qh: &QueueHandle<Self>,
        seat: wl_seat::WlSeat,
        capability: Capability,
    ) {
        // Bind the pointer once, when the seat first advertises one. The bar is
        // pointer-only; keyboard and touch capabilities are ignored.
        if capability == Capability::Pointer && self.pointer.is_none() {
            let cursor_surface = self.compositor.create_surface(qh);
            match self.seat_state.get_pointer_with_theme(
                qh,
                &seat,
                self.shm.wl_shm(),
                cursor_surface,
                ThemeSpec::default(),
            ) {
                Ok(pointer) => {
                    self.pointer = Some(pointer);
                    self.pointer_seat = Some(seat);
                }
                Err(e) => error!("failed to create pointer: {e}"),
            }
        }
    }

    fn remove_capability(
        &mut self,
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
        _seat: wl_seat::WlSeat,
        capability: Capability,
    ) {
        if capability == Capability::Pointer {
            self.pointer = None;
            self.pointer_seat = None;
            self.pointer_cursor = CursorIcon::Default;
        }
    }

    fn remove_seat(&mut self, _conn: &Connection, _qh: &QueueHandle<Self>, _seat: wl_seat::WlSeat) {
    }
}

impl PointerHandler for App {
    fn pointer_frame(
        &mut self,
        conn: &Connection,
        _qh: &QueueHandle<Self>,
        _pointer: &wl_pointer::WlPointer,
        events: &[PointerEvent],
    ) {
        for event in events {
            if matches!(
                event.kind,
                PointerEventKind::Enter { .. } | PointerEventKind::Motion { .. }
            ) {
                let (x, y) = event.position;
                if let Some(menu) = self
                    .tray_menu
                    .as_ref()
                    .filter(|menu| menu.owns_surface(&event.surface))
                {
                    self.set_pointer_cursor(
                        conn,
                        if menu.row_at(y).is_some_and(MenuRow::activatable) {
                            CursorIcon::Pointer
                        } else {
                            CursorIcon::Default
                        },
                        matches!(event.kind, PointerEventKind::Enter { .. }),
                    );
                    continue;
                }
                let hover = self
                    .outputs
                    .values()
                    .find(|bar| bar.owns(&event.surface))
                    .map(|bar| (bar.output_id, bar.hover_at(x, y)));
                if let Some((output_id, hover)) = hover {
                    self.update_tooltip(output_id, hover.tooltip, _qh);
                    self.set_pointer_cursor(
                        conn,
                        if hover.clickable {
                            CursorIcon::Pointer
                        } else {
                            CursorIcon::Default
                        },
                        matches!(event.kind, PointerEventKind::Enter { .. }),
                    );
                } else if matches!(event.kind, PointerEventKind::Enter { .. }) {
                    warn!("pointer enter on unknown surface id={}", event.surface.id());
                }
            } else if matches!(event.kind, PointerEventKind::Leave { .. }) {
                if self.outputs.values().any(|bar| bar.owns(&event.surface)) {
                    self.hide_tooltip();
                    self.pointer_cursor = CursorIcon::Default;
                } else if self
                    .tray_menu
                    .as_ref()
                    .is_some_and(|menu| menu.owns_surface(&event.surface))
                {
                    self.pointer_cursor = CursorIcon::Default;
                }
            } else if let PointerEventKind::Press { button, serial, .. } = event.kind {
                let input_started = self.performance.start();
                // Normalize the kernel input code to the typed button the
                // widgets branch on; other buttons (middle, side, scroll
                // clicks) are ignored here so widgets never see them.
                let click = match button {
                    BTN_LEFT => ClickButton::Left,
                    BTN_RIGHT => ClickButton::Right,
                    _ => continue,
                };
                let (x, y) = event.position;
                if self
                    .tray_menu
                    .as_ref()
                    .is_some_and(|menu| menu.owns_surface(&event.surface))
                {
                    if click == ClickButton::Left {
                        let command = self.tray_menu.as_ref().and_then(|menu| menu.command_at(y));
                        if let Some(command) = command {
                            self.hide_tray_menu();
                            for sender in &self.commands {
                                if sender.send(command.clone()).is_err() {
                                    warn!("command channel closed; dropping menu command");
                                }
                            }
                        }
                    }
                    continue;
                }
                // Resolve the click against the surface it landed on, then fan the
                // resulting command out to the executors. The immutable lookup ends
                // before `self.commands` is borrowed, so the two never conflict.
                let interaction = self
                    .outputs
                    .values()
                    .find(|bar| bar.owns(&event.surface))
                    .and_then(|bar| {
                        Some((
                            bar.on_click(x, y, click)?,
                            bar.layer.clone(),
                            bar.output.clone(),
                            bar.output_id,
                            bar.scale,
                            bar.height,
                            bar.config.scaled_render_settings(bar.scale),
                            bar.monitor.clone(),
                        ))
                    });
                if let Some((
                    mut command,
                    parent,
                    output,
                    output_id,
                    scale,
                    bar_height,
                    settings,
                    monitor,
                )) = interaction
                {
                    let origin = self
                        .output_state
                        .info(&output)
                        .map(|info| info.logical_position.unwrap_or(info.location))
                        .unwrap_or((0, 0));
                    set_tray_command_position(&mut command, origin, (x, y));
                    if let Command::OpenTrayMenu { key, .. } = &command {
                        self.hide_tray_menu();
                        self.hide_tooltip();
                        self.pending_tray_menu = Some(PendingTrayMenu {
                            key: key.clone(),
                            parent,
                            output_id,
                            anchor: (x as i32, bar_height as i32),
                            scale,
                            settings,
                            serial,
                            seat: self.pointer_seat.clone(),
                            requested_at: input_started,
                        });
                    }
                    if let (Command::SwitchWorkspace(target), Some(started)) =
                        (&command, input_started)
                    {
                        self.pending_workspace = Some(PendingWorkspace {
                            target: *target,
                            monitor,
                            started: Some(started),
                        });
                    }
                    let kind = command_kind(&command);
                    for sender in &self.commands {
                        if sender.send(command.clone()).is_err() {
                            warn!("command channel closed; dropping click command");
                        }
                    }
                    self.performance.record_since(
                        "click-to-command-queue",
                        input_started,
                        format_args!("command={kind} output={output_id}"),
                    );
                }
            } else if let PointerEventKind::Axis { vertical, .. } = event.kind {
                let directions = scroll_directions(vertical, &mut self.scroll_remainder);
                for direction in directions {
                    let (x, y) = event.position;
                    let command = self
                        .outputs
                        .values()
                        .find(|bar| bar.owns(&event.surface))
                        .and_then(|bar| bar.on_scroll(x, y, direction));
                    if let Some(command) = command {
                        for sender in &self.commands {
                            if sender.send(command.clone()).is_err() {
                                warn!("command channel closed; dropping scroll command");
                            }
                        }
                    }
                }
            }
        }
    }
}

impl Dispatch<wl_region::WlRegion, ()> for App {
    fn event(
        _state: &mut Self,
        _proxy: &wl_region::WlRegion,
        _event: wl_region::Event,
        _data: &(),
        _conn: &Connection,
        _qh: &QueueHandle<Self>,
    ) {
    }
}

/// Normalize wheel, high-resolution wheel, and touchpad motion into logical steps.
fn scroll_directions(
    vertical: smithay_client_toolkit::seat::pointer::AxisScroll,
    remainder: &mut f64,
) -> Vec<ScrollDirection> {
    let delta = if vertical.value120 != 0 {
        vertical.value120 as f64 / 120.0
    } else if vertical.discrete != 0 {
        vertical.discrete as f64
    } else {
        // Continuous devices report pixels; ten pixels is one deliberate step.
        vertical.absolute / 10.0
    };
    *remainder += delta;
    let mut directions = Vec::new();
    while *remainder >= 1.0 {
        directions.push(ScrollDirection::Decrease);
        *remainder -= 1.0;
    }
    while *remainder <= -1.0 {
        directions.push(ScrollDirection::Increase);
        *remainder += 1.0;
    }
    directions
}

#[cfg(test)]
mod scroll_tests {
    use super::*;
    use smithay_client_toolkit::seat::pointer::AxisScroll;

    #[test]
    fn theme_reload_replays_latest_producers_into_rebuilt_dashboards() {
        use crate::widget::{ActiveWindow, DeviceKind, Volume, Workspaces};
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("config.toml");
        let theme = dir.path().join("theme.toml");
        std::fs::write(&path, "[appearance]\ntheme_file = 'theme.toml'\n[bar]\nmodules-left = ['workspaces']\nmodules-center = ['title']\nmodules-right = ['volume']").unwrap();
        let text = include_str!("../tests/fixtures/swatches.toml");
        std::fs::write(&theme, text).unwrap();
        let mut snapshot = ProducerSnapshot::default();
        snapshot.note(&Msg::Workspaces(Workspaces::new([1], 1)));
        snapshot.note(&Msg::Workspaces(Workspaces::with_monitors(
            [(3, "DP-1"), (7, "HDMI-A-1")],
            [("DP-1", 3), ("HDMI-A-1", 7)],
            3,
        )));
        for monitor in ["DP-1", "HDMI-A-1"] {
            snapshot.note(&Msg::ActiveWindow {
                monitor: monitor.into(),
                window: Some(ActiveWindow::new("app", "superseded title")),
            });
            snapshot.note(&Msg::ActiveWindow {
                monitor: monitor.into(),
                window: Some(ActiveWindow::new("app", monitor)),
            });
        }
        snapshot.note(&Msg::Volume(Some(Volume::new(
            0.12,
            true,
            DeviceKind::Speakers,
        ))));
        let volume = Msg::Volume(Some(Volume::new(0.73, false, DeviceKind::Headphones)));
        snapshot.note(&volume);
        snapshot.note(&Msg::tick_now());
        snapshot.note(&Msg::TrayMenuUnavailable("stale".into()));
        assert_eq!(snapshot.as_slice().len(), 4);
        for accent in ["#80D4FF", "#010203"] {
            std::fs::write(&theme, text.replace("#80D4FF", accent)).unwrap();
            let config = Config::load_for_reload(&path).unwrap();
            for monitor in ["DP-1", "HDMI-A-1"] {
                let bounds = Bounds::new(0, 0, 800, 32);
                let mut dashboard = config.build_dashboard(bounds, Some(monitor));
                replay_snapshot(&mut dashboard, snapshot.as_slice());
                let title = Msg::ActiveWindow {
                    monitor: monitor.into(),
                    window: Some(ActiveWindow::new("app", monitor)),
                };
                // A fresh dashboard must consume these readings, whereas the
                // replayed one already has the latest title and volume. This
                // guards against accidentally testing an absent widget.
                let mut fresh = config.build_dashboard(bounds, Some(monitor));
                assert!(fresh.update(&title));
                assert!(fresh.update(&volume));
                assert!(
                    !dashboard.update(&title),
                    "latest title restored for {monitor}"
                );
                assert!(
                    !dashboard.update(&volume),
                    "latest volume restored for {monitor}"
                );

                let mut ctx = RenderContext::with_settings(800, 32, config.render_settings());
                dashboard.layout(&mut ctx, 800, 32);
                dashboard.draw(&mut ctx);
                let restored_pixels = ctx.pixels().to_vec();
                assert_eq!(
                    dashboard.on_click(10, 16, ClickButton::Left),
                    Some(Command::SwitchWorkspace(if monitor == "DP-1" {
                        3
                    } else {
                        7
                    }))
                );

                // Each restored value must be visible, not merely retained in
                // the snapshot map. Clearing it changes pixels; restoring it
                // recreates the exact pre-clear frame without another producer.
                for (clear, restore) in [
                    (
                        Msg::ActiveWindow {
                            monitor: monitor.into(),
                            window: None,
                        },
                        title,
                    ),
                    (Msg::Volume(None), volume.clone()),
                ] {
                    assert!(dashboard.update(&clear));
                    dashboard.layout(&mut ctx, 800, 32);
                    dashboard.draw(&mut ctx);
                    assert_ne!(ctx.pixels(), restored_pixels.as_slice());
                    assert!(dashboard.update(&restore));
                    dashboard.layout(&mut ctx, 800, 32);
                    dashboard.draw(&mut ctx);
                    assert_eq!(ctx.pixels(), restored_pixels.as_slice());
                }
            }
            assert_eq!(snapshot.as_slice().len(), 4);
            assert_eq!(
                popup_background(config.render_settings().background),
                (16, 37, 63, 255)
            );
        }
    }

    #[test]
    fn wheel_steps_map_up_to_increase_and_down_to_decrease() {
        let mut remainder = 0.0;
        assert_eq!(
            scroll_directions(
                AxisScroll {
                    value120: -120,
                    ..AxisScroll::default()
                },
                &mut remainder,
            ),
            vec![ScrollDirection::Increase]
        );
        assert_eq!(
            scroll_directions(
                AxisScroll {
                    value120: 120,
                    ..AxisScroll::default()
                },
                &mut remainder,
            ),
            vec![ScrollDirection::Decrease]
        );
    }

    #[test]
    fn smooth_motion_accumulates_before_emitting_a_step() {
        let mut remainder = 0.0;
        let half = AxisScroll {
            absolute: -5.0,
            ..AxisScroll::default()
        };
        assert!(scroll_directions(half, &mut remainder).is_empty());
        assert_eq!(
            scroll_directions(half, &mut remainder),
            vec![ScrollDirection::Increase]
        );
    }

    #[test]
    fn tray_coordinates_include_the_output_logical_origin() {
        let mut command = Command::OpenTrayMenu {
            key: ":1.7/Menu".into(),
            x: 0,
            y: 0,
        };
        set_tray_command_position(&mut command, (1920, -40), (24.8, 18.9));
        assert_eq!(
            command,
            Command::OpenTrayMenu {
                key: ":1.7/Menu".into(),
                x: 1944,
                y: -22,
            }
        );
    }

    #[test]
    fn tray_menu_flattens_visible_nested_entries_and_preserves_depth() {
        let leaf = TrayMenuItem {
            id: 2,
            label: "Child".into(),
            enabled: true,
            visible: true,
            separator: false,
            toggle: None,
            children: vec![],
        };
        let parent = TrayMenuItem {
            id: 1,
            label: "Parent".into(),
            enabled: true,
            visible: true,
            separator: false,
            toggle: None,
            children: vec![leaf],
        };
        let hidden = TrayMenuItem {
            id: 3,
            label: "Hidden".into(),
            enabled: true,
            visible: false,
            separator: false,
            toggle: None,
            children: vec![],
        };
        let mut rows = Vec::new();
        flatten_menu(&[parent, hidden], 0, &mut rows);

        assert_eq!(rows.len(), 2);
        assert_eq!((rows[0].id, rows[0].depth), (1, 0));
        assert!(rows[0].has_children);
        assert!(!rows[0].activatable());
        assert_eq!((rows[1].id, rows[1].depth), (2, 1));
        assert!(rows[1].activatable());
    }

    #[test]
    fn transparent_bar_background_uses_an_opaque_popup_surface() {
        assert_eq!(
            popup_background((0x18, 0x18, 0x18, 0x00)),
            POPUP_FALLBACK_BACKGROUND
        );
        assert_eq!(
            popup_background((0x30, 0x32, 0x38, 0xD0)),
            (0x30, 0x32, 0x38, 0xF0)
        );
    }

    #[test]
    fn full_damage_covers_the_whole_buffer() {
        assert_eq!(damage_rect(Damage::Full, 3840, 76), (0, 0, 3840, 76));
    }

    #[test]
    fn region_damage_is_passed_through_in_buffer_pixels() {
        let region = Damage::Region(Bounds::new(3600, 8, 200, 60));
        assert_eq!(damage_rect(region, 3840, 76), (3600, 8, 200, 60));
    }

    #[test]
    fn region_damage_is_clipped_to_the_buffer() {
        let region = Damage::Region(Bounds::new(3800, 70, 200, 60));
        assert_eq!(damage_rect(region, 3840, 76), (3800, 70, 40, 6));
    }

    #[test]
    fn an_empty_or_offscreen_region_falls_back_to_full_damage() {
        for region in [Bounds::new(10, 10, 0, 20), Bounds::new(5000, 0, 10, 10)] {
            assert_eq!(
                damage_rect(Damage::Region(region), 3840, 76),
                (0, 0, 3840, 76)
            );
        }
    }

    fn tooltip_pixels(ctx: &mut RenderContext, text: &str, size: (u32, u32)) -> Vec<u8> {
        paint_tooltip(
            ctx,
            text,
            size,
            POPUP_FALLBACK_BACKGROUND,
            (0xFF, 0xFF, 0xFF, 0xFF),
        );
        ctx.pixels().to_vec()
    }

    fn popup_settings() -> RenderSettings {
        RenderSettings {
            background: (0, 0, 0, 0),
            ..Config::default().render_settings()
        }
    }

    #[test]
    fn a_reused_popup_context_paints_what_a_fresh_one_would() {
        // One panel size for both, so the second paint lands on the first one's
        // pixels rather than on a reallocated pixmap.
        let mut reused = RenderContext::with_settings(1, 1, popup_settings());
        let size = tooltip_size(&mut reused, "Hypridle inactive");
        tooltip_pixels(&mut reused, "Hypridle inactive", size);

        let mut fresh = RenderContext::with_settings(1, 1, popup_settings());
        assert_eq!(
            tooltip_pixels(&mut reused, "Hypridle active", size),
            tooltip_pixels(&mut fresh, "Hypridle active", size)
        );
    }

    #[test]
    fn a_reused_popup_context_does_not_reshape_a_tooltip_it_has_shown() {
        let mut ctx = RenderContext::with_settings(1, 1, popup_settings());
        let size = tooltip_size(&mut ctx, "Hypridle active");
        tooltip_pixels(&mut ctx, "Hypridle active", size);
        ctx.take_stats();

        ctx.set_settings(popup_settings());
        let size = tooltip_size(&mut ctx, "Hypridle active");
        tooltip_pixels(&mut ctx, "Hypridle active", size);

        assert_eq!(ctx.take_stats().text_shapes, 0);
    }
}

impl ProvidesRegistryState for App {
    fn registry(&mut self) -> &mut RegistryState {
        &mut self.registry_state
    }
    registry_handlers![OutputState, SeatState];
}

delegate_compositor!(App);
delegate_output!(App);
delegate_shm!(App);
delegate_layer!(App);
delegate_xdg_shell!(App);
delegate_xdg_popup!(App);
delegate_seat!(App);
delegate_pointer!(App);
delegate_registry!(App);