Crates.io | betrayer |
lib.rs | betrayer |
version | 0.3.0 |
source | src |
created_at | 2023-12-29 14:03:47.037705 |
updated_at | 2024-11-24 20:03:47.549739 |
description | Simple tray icons that integrate well with winit |
homepage | |
repository | https://github.com/sidit77/betrayer |
max_upload_size | |
id | 1083372 |
size | 131,608 |
Betrayer is a library for creating tray icons specifically designed to integrate well with winit
and other existing libraries.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum Signal {
Profile(u32),
Open,
Quit
}
fn main() -> Result<()> {
let event_loop = EventLoopBuilder::with_user_event()
.build()?;
let mut selected = 0;
let tray = TrayIconBuilder::new()
.with_icon(Icon::from_rgba(vec![255u8; 32 * 32 * 4], 32, 32)?)
.with_tooltip("Demo System Tray")
.with_menu(build_menu(selected))
// with `winit` feature:
.build_event_loop(&event_loop, |e| Some(e))?;
// without:
//.build({
// let proxy = event_loop.create_proxy();
// move |s| {let _ = proxy.send_event(s); }
//})?;
event_loop.set_control_flow(ControlFlow::Wait);
event_loop.run(|event, evtl| {
match event {
Event::UserEvent(event) => {
println!("tray event: {:?}", event);
if let TrayEvent::Menu(signal) = event {
match signal {
Signal::Profile(i) => {
if selected != i {
selected = i;
tray.set_tooltip(format!("Active Profile: {selected}"));
tray.set_menu(build_menu(selected));
}
},
Signal::Open => {}
Signal::Quit => evtl.exit()
}
}
}
_ => {}
}
})?;
Ok(())
}
fn build_menu(selected: u32) -> Menu<Signal> {
Menu::new([
MenuItem::menu("Profiles", (0..5)
.map(|i| MenuItem::check_button(format!("Profile {}", i + 1), Signal::Profile(i),selected == i))),
MenuItem::separator(),
MenuItem::button("Open", Signal::Open),
MenuItem::button("Quit", Signal::Quit)
])
}
On Windows and Mac this library uses the platform native toolkits and therefore needs a running eventloop on the same thread.
On Linux this library uses the org.kde.StatusNotifierItem
DBus api and therefore requires a Desktop Environment that supports this api. Ubuntu possibly requires the libayatana-appindicator
package.
This library will spawn its own thread to handle DBus communication so no extra eventloop is required.
Standalone mode that takes control of the main thread
More menu elements
More options for the tray