Skip to content

Commit dc74a9f

Browse files
committed
Feat: init tray-icon
1 parent 8be5589 commit dc74a9f

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

src/system-tray/main.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
use tao::{
2+
event::{Event},
3+
event_loop::{ControlFlow, EventLoop},
4+
menu::{ContextMenu, MenuId, MenuItemAttributes},
5+
system_tray::{SystemTray, SystemTrayBuilder, Icon},
6+
};
7+
use image::io::Reader as ImageReader;
8+
use std::io::Cursor;
9+
10+
fn load_icon(path: &str) -> Icon {
11+
let file = std::fs::read(path).expect("Failed to read icon file");
12+
let image = ImageReader::new(Cursor::new(file))
13+
.with_guessed_format()
14+
.expect("Failed to guess image format")
15+
.decode()
16+
.expect("Failed to decode image");
17+
let rgba = image.into_rgba8();
18+
let (width, height) = rgba.dimensions();
19+
Icon::from_rgba(rgba.into_raw(), width, height).expect("Failed to create icon")
20+
}
21+
22+
fn main() {
23+
let event_loop = EventLoop::new();
24+
25+
let mut tray_menu = ContextMenu::new();
26+
let show_id = MenuId::new("0");
27+
let exit_id = MenuId::new("1");
28+
tray_menu.add_item(MenuItemAttributes::new("Show").with_id(show_id));
29+
tray_menu.add_item(MenuItemAttributes::new("Exit").with_id(exit_id));
30+
31+
let tray_icon_path = concat!(env!("CARGO_MANIFEST_DIR"), "image path");
32+
let tray_icon = load_icon(tray_icon_path);
33+
34+
let system_tray = SystemTrayBuilder::new(tray_icon, Some(tray_menu))
35+
.build(&event_loop)
36+
.unwrap();
37+
38+
event_loop.run(move |event, _, control_flow| {
39+
*control_flow = ControlFlow::Wait;
40+
41+
match event {
42+
Event::MenuEvent {
43+
menu_id,
44+
origin: _,
45+
..
46+
} => {
47+
if menu_id == show_id {
48+
println!("Show clicked");
49+
// Handle Show action
50+
} else if menu_id == exit_id {
51+
println!("Exit clicked");
52+
*control_flow = ControlFlow::Exit;
53+
}
54+
}
55+
_ => (),
56+
}
57+
});
58+
}

0 commit comments

Comments
 (0)