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
use ksni::TrayMethods; // provides the spawn method
#[derive(Debug)]
struct MyTray {
selected_option: usize,
checked: bool,
}
impl ksni::Tray for MyTray {
fn id(&self) -> String {
env!("CARGO_PKG_NAME").into()
}
fn icon_name(&self) -> String {
"help-about".into()
}
fn title(&self) -> String {
if self.checked { "CHECKED!" } else { "MyTray" }.into()
}
fn menu(&self) -> Vec<ksni::MenuItem<Self>> {
use ksni::menu::*;
vec![
SubMenu {
label: "a".into(),
submenu: vec![
SubMenu {
label: "a1".into(),
submenu: vec![
StandardItem {
label: "a1.1".into(),
..Default::default()
}
.into(),
StandardItem {
label: "a1.2".into(),
..Default::default()
}
.into(),
],
..Default::default()
}
.into(),
StandardItem {
label: "a2".into(),
..Default::default()
}
.into(),
],
..Default::default()
}
.into(),
MenuItem::Separator,
RadioGroup {
selected: self.selected_option,
select: Box::new(|this: &mut Self, current| {
this.selected_option = current;
}),
options: vec![
RadioItem {
label: "Option 0".into(),
..Default::default()
},
RadioItem {
label: "Option 1".into(),
..Default::default()
},
RadioItem {
label: "Option 2".into(),
..Default::default()
},
],
..Default::default()
}
.into(),
CheckmarkItem {
label: "Checkable".into(),
checked: self.checked,
activate: Box::new(|this: &mut Self| this.checked = !this.checked),
..Default::default()
}
.into(),
StandardItem {
label: "Exit".into(),
icon_name: "application-exit".into(),
activate: Box::new(|_| std::process::exit(0)),
..Default::default()
}
.into(),
]
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let tray = MyTray {
selected_option: 0,
checked: false,
};
let handle = tray.spawn().await.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
// We can modify the tray
handle.update(|tray: &mut MyTray| tray.checked = true).await;
// Run forever
std::future::pending().await
}