1use std::fmt;
5
6use tokio::sync::broadcast;
7use tray_icon::menu::MenuId;
8
9#[derive(Clone)]
15pub struct MenuItem {
16 pub(crate) id: MenuId,
17 pub(crate) label: String,
18 pub(crate) clicked: broadcast::Sender<()>,
19}
20
21impl fmt::Debug for MenuItem {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 f.debug_struct("MenuItem")
24 .field("label", &self.label)
25 .finish_non_exhaustive()
26 }
27}
28
29impl MenuItem {
30 pub fn new(label: impl Into<String>) -> Self {
32 Self {
33 id: MenuId::new(format!("hypersig-desktop-item-{}", next_id())),
34 label: label.into(),
35 clicked: broadcast::channel(16).0,
36 }
37 }
38
39 #[must_use]
41 pub fn clicks(&self) -> Clicks {
42 Clicks {
43 receiver: self.clicked.subscribe(),
44 }
45 }
46}
47
48#[derive(Debug)]
50pub struct Clicks {
51 receiver: broadcast::Receiver<()>,
52}
53
54impl Clicks {
55 pub async fn next(&mut self) -> bool {
57 loop {
58 match self.receiver.recv().await {
59 Ok(()) => return true,
60 Err(broadcast::error::RecvError::Lagged(_)) => {}
61 Err(broadcast::error::RecvError::Closed) => return false,
62 }
63 }
64 }
65}
66
67fn next_id() -> u64 {
69 use std::sync::atomic::{AtomicU64, Ordering};
70 static NEXT: AtomicU64 = AtomicU64::new(0);
71 NEXT.fetch_add(1, Ordering::Relaxed)
72}