tablero 0.2.0

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
//! 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;
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::error::Error;
use std::time::Duration;

use crate::blit::write_argb8888;
use crate::clock::millis_until_next_minute;
use crate::config::{Config, WidgetKind};
use crate::render::{Bounds, RenderContext, RenderSettings};
use crate::scale::Scale;
use crate::widget::{
    ClickButton, Command, Dashboard, Msg, ScrollDirection, Tooltip, TrayMenu, TrayMenuItem,
    TrayMenuToggleKind, TrayMenuToggleState,
};
use calloop::EventLoop;
use calloop::channel::Event as ChannelEvent;
use calloop::timer::{TimeoutAction, Timer};
use calloop_wayland_source::WaylandSource;
use log::{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::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::power_profiles::PowerProfilesProducer;
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;

/// 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,
    /// Widgets composing the bar, plus the dirty-flag redraw policy over them.
    dashboard: Dashboard,
    /// Reused software-render target (font machinery + pixmap).
    ctx: RenderContext,
    /// Set once the first configure has been received; drawing before that is
    /// invalid per the layer-shell protocol.
    configured: bool,
}

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.
    fn new(
        compositor: &CompositorState,
        layer_shell: &LayerShell,
        qh: &QueueHandle<App>,
        output: &wl_output::WlOutput,
        output_id: OutputId,
        monitor: Option<&str>,
        config: Config,
    ) -> 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_settings(INITIAL_WIDTH, height, config.render_settings());

        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,
            dashboard,
            ctx,
            configured: false,
        }
    }

    /// 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; redraw 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.
    fn handle(&mut self, msg: &Msg, pool: &mut SlotPool) -> bool {
        let changed = self.dashboard.update(msg);
        if changed {
            self.draw(pool);
        }
        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 repaints (once configured) 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, pool: &mut SlotPool) -> bool {
        if self.scale == scale {
            return false;
        }
        self.scale = scale;
        self.ctx
            .set_settings(self.config.scaled_render_settings(scale));
        info!("output scale changed to {}x", scale.get());
        self.draw(pool);
        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)
    }

    fn is_clickable_at(&self, x: f64, y: f64) -> bool {
        if x < 0.0 || y < 0.0 {
            return false;
        }
        let scale = self.scale.get() as f64;
        self.dashboard
            .is_clickable_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)
    }

    /// Resolve tooltip content at surface-local logical coordinates.
    fn tooltip_at(&self, x: f64, y: f64) -> Option<Tooltip> {
        if x < 0.0 || y < 0.0 {
            return None;
        }
        let scale = self.scale.get() as f64;
        self.dashboard
            .tooltip_at((x * scale) as u32, (y * scale) as u32)
    }

    /// Adopt the compositor's configure, seeding and drawing the first frame.
    fn configure(&mut self, configure: LayerSurfaceConfigure, pool: &mut SlotPool) {
        // 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 draw regardless of the dirty flag.
            self.dashboard.update(&Msg::tick_now());
            self.draw(pool);
        }
    }

    /// Render the current dashboard state and commit it through the app's shared
    /// shared-memory pool. Called on a visible change or when the Wayland
    /// lifecycle (first configure, resize) requires a fresh frame regardless.
    fn draw(&mut self, pool: &mut SlotPool) {
        if !self.configured {
            return;
        }

        // 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;

        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;
            }
        };

        self.ctx.resize(width, height);
        self.dashboard.layout(&mut self.ctx, width, height);
        self.dashboard.draw(&mut self.ctx);
        write_argb8888(self.ctx.pixels(), canvas);

        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);
        surface.damage_buffer(0, 0, width as i32, height as i32);
        if let Err(e) = buffer.attach_to(surface) {
            error!("failed to attach buffer: {e}");
            return;
        }
        self.layer.commit();
    }
}

/// 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,
}

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>,
}

/// 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,
}

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);
        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();
    }
}

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;
            }
        };

        self.ctx.resize(width, height);
        self.ctx.fill_rounded_rect(
            Bounds::new(0, 0, width, height),
            self.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(&self.ctx);
        for (index, line) in self.text.lines().enumerate() {
            self.ctx.draw_text(
                line,
                Bounds::new(
                    padding_x,
                    padding_y + index as u32 * line_height,
                    width.saturating_sub(2 * padding_x),
                    line_height,
                ),
                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();
    }
}

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>,
    exit: bool,
}

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 built = self.outputs.ensure(id, name.as_deref(), |config| {
            Surface::new(
                compositor,
                layer_shell,
                qh,
                &output,
                id,
                name.as_deref(),
                config,
            )
        });
        if built {
            info!(
                "output {id} ({}) added; {} bar(s) live",
                name.as_deref().unwrap_or("<unnamed>"),
                self.outputs.len()
            );
        }
    }

    /// 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() {
            info!("output {id} removed; {} bar(s) live", self.outputs.len());
        }
    }

    /// Fan a message out to every output's dashboard, redrawing the ones that
    /// changed. The clock timer and every producer reach all bars this way.
    fn handle_all(&mut self, msg: &Msg) {
        let App { pool, outputs, .. } = self;
        let mut changed = false;
        for surface in outputs.values_mut() {
            changed |= surface.handle(msg, pool);
        }
        if changed && matches!(msg, Msg::PowerProfiles(_)) {
            self.hide_tooltip();
        }
    }

    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) {
        self.tooltip = None;
    }

    fn hide_tray_menu(&mut self) {
        self.pending_tray_menu = None;
        self.tray_menu = None;
    }

    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}"),
        }
    }

    fn update_tooltip(
        &mut self,
        surface: &wl_surface::WlSurface,
        x: f64,
        y: f64,
        qh: &QueueHandle<App>,
    ) {
        let request = self
            .outputs
            .values()
            .find(|bar| bar.owns(surface))
            .and_then(|bar| {
                let tooltip = bar.tooltip_at(x, y)?;
                Some((
                    bar.output_id,
                    bar.layer.clone(),
                    bar.scale,
                    bar.config.scaled_render_settings(bar.scale),
                    tooltip,
                ))
            });
        let Some((output_id, parent, scale, mut settings, tooltip)) = request else {
            self.hide_tooltip();
            return;
        };
        if self
            .tooltip
            .as_ref()
            .is_some_and(|shown| shown.output_id == output_id && shown.text == tooltip.text)
        {
            return;
        }

        let background = popup_background(settings.background);
        let foreground = settings.foreground;
        settings.background = (0, 0, 0, 0);
        let mut ctx = RenderContext::with_settings(1, 1, 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,
        });
    }

    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);
        let mut ctx = RenderContext::with_settings(1, 1, 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.tooltip = None;
        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,
        });
    }
}

/// 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 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 the default producer
/// set — the Hyprland workspace source, the UPower battery source, the procfs
/// system-stats source, the NetworkManager connectivity source, the BlueZ
/// bluetooth source, the native PipeWire volume source, the
/// StatusNotifierItem tray host, swaync notification source, and optional Arch
/// package-update and Hypridle process pollers — so the
/// bar shows live workspaces, battery, CPU/memory load, network state,
/// Bluetooth adapter state, the active output sink's volume, tray icons, and
/// the notification indicator alongside the clock. The bluetooth, volume,
/// tray, and notifications producers run even when their widgets are not in
/// any zone; the widget only appears when the user adds `"bluetooth"`,
/// `"volume"`, `"tray"`, or `"notifications"` to a zone. The update poller is
/// the exception: it starts only when `"updates"` is configured, avoiding
/// needless subprocess and network work. Hypridle process polling is likewise
/// enabled only when `"hypridle"` is configured. The volume source runs on a dedicated
/// OS thread —
/// PipeWire's main loop is synchronous, unlike the other producers' zbus
/// backends. The clock itself is still driven by the synchronous tick timer;
/// see [`run_with_producers`] to supply a custom producer set.
pub fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let mut producers: Vec<Box<dyn Producer>> = vec![
        Box::new(HyprlandProducer::new()),
        Box::new(UPowerProducer::new()),
        Box::new(BacklightProducer::new()),
        Box::new(SystemProducer::new()),
        Box::new(NetworkProducer::new()),
        Box::new(BluetoothProducer::new()),
        Box::new(VolumeProducer::new()),
        Box::new(SniHostProducer::new()),
        Box::new(NotificationsProducer::new()),
        Box::new(PowerProfilesProducer::new()),
    ];
    // Unlike DBus subscriptions, checking package repositories performs network
    // and subprocess work, so keep this producer dormant unless some output uses
    // the opt-in module.
    if config.uses_widget(WidgetKind::Updates) {
        producers.push(Box::new(UpdatesProducer::new()));
    }
    if config.uses_widget(WidgetKind::Hypridle) {
        producers.push(Box::new(HypridleProducer::new()));
    }
    run_with_producers(config, producers)
}

/// 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.
///
/// No surface is created up front: the bar opens one layer surface per output as
/// the compositor advertises them through [`OutputHandler::new_output`], and
/// tears each down on `output_destroyed`, so plugging or unplugging a monitor
/// adds or removes its bar without restarting the loop.
pub fn run_with_producers(
    config: Config,
    producers: Vec<Box<dyn Producer>>,
) -> Result<(), Box<dyn Error>> {
    let height = config.height;

    let conn = Connection::connect_to_env()?;
    let (globals, 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.
    let pool = SlotPool::new((INITIAL_WIDTH * height * 4) 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,
        exit: false,
    };

    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()))
    })?;

    // 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);
            }
        })?;
        let count = producers.len();
        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),
        );
        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,
        ];
        info!("producer bridge started with {count} producer(s)");
        Some(bridge)
    };

    info!("tablero started: one {height}px bar per output");

    let signal = event_loop.get_signal();
    event_loop.run(None, &mut app, move |app| {
        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 {
            pool,
            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, pool).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,
    ) {
        // We never request frame callbacks: redraws are driven solely by the
        // tick timer, which keeps the loop free of a busy redraw cycle.
    }

    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,
    ) {
        let App { pool, outputs, .. } = self;
        if let Some(bar) = outputs.values_mut().find(|bar| bar.is_layer(layer)) {
            bar.configure(configure, pool);
        }
    }
}

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 clickable = self
                    .outputs
                    .values()
                    .find(|bar| bar.owns(&event.surface))
                    .is_some_and(|bar| bar.is_clickable_at(x, y));
                if self.outputs.values().any(|bar| bar.owns(&event.surface)) {
                    self.update_tooltip(&event.surface, x, y, _qh);
                    self.set_pointer_cursor(
                        conn,
                        if clickable {
                            CursorIcon::Pointer
                        } else {
                            CursorIcon::Default
                        },
                        matches!(event.kind, PointerEventKind::Enter { .. }),
                    );
                }
            } 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 {
                // 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),
                        ))
                    });
                if let Some((mut command, parent, output, output_id, scale, bar_height, settings)) =
                    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(),
                        });
                    }
                    for sender in &self.commands {
                        if sender.send(command.clone()).is_err() {
                            warn!("command channel closed; dropping click command");
                        }
                    }
                }
            } 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 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)
        );
    }
}

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);