-
Notifications
You must be signed in to change notification settings - Fork 62
Open
Description
Here is an example program:
[dependencies]
tray-icon = "0.21.2"
anyhow = "1.0.93"
winit = "0.30.12"
chrono = "0.4.42"use tray_icon::menu::{Menu, MenuEvent, MenuItem, PredefinedMenuItem};
use tray_icon::{Icon, TrayIcon, TrayIconBuilder, TrayIconEvent};
use winit::application::ApplicationHandler;
use winit::event_loop::{EventLoop};
fn main() -> anyhow::Result<()> {
#[derive(Debug)]
enum UserEvent {
TrayIconEvent(tray_icon::TrayIconEvent),
MenuEvent(tray_icon::menu::MenuEvent),
}
let event_loop = EventLoop::<UserEvent>::with_user_event().build()?;
// See tray-icon docs, "You should ..."
let proxy = event_loop.create_proxy();
TrayIconEvent::set_event_handler(Some(move |event| {
_ = proxy.send_event(UserEvent::TrayIconEvent(event));
}));
let proxy = event_loop.create_proxy();
MenuEvent::set_event_handler(Some(move |event| {
_ = proxy.send_event(UserEvent::MenuEvent(event));
}));
struct Application {
tray_icon: Option<TrayIcon>,
}
//noinspection RsSortImplTraitMembers
impl ApplicationHandler<UserEvent> for Application {
fn user_event(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop, event: UserEvent) {
println!("user_event");
println!("{event:?}");
}
// Copied from https://github.com/tauri-apps/tray-icon/blob/dev/examples/winit.rs
fn new_events(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop, cause: winit::event::StartCause) {
println!("{} new_events", chrono::Local::now().to_rfc3339());
if cause == winit::event::StartCause::Init {
// Generate a simple icon programmatically, a red square.
let icon = {
let (width, height) = (32, 32);
let mut rgba = Vec::with_capacity((width * height * 4) as usize);
for _ in 0..width * height { rgba.extend_from_slice(&[255, 0, 0, 255]); }
Icon::from_rgba(rgba, width, height).unwrap()
};
let tray_menu = Menu::new();
tray_menu.append_items(&[
&MenuItem::new("Tray icon example", false, None),
&PredefinedMenuItem::separator(),
&MenuItem::new("Test", true, None),
]).unwrap();
self.tray_icon = Some(TrayIconBuilder::new()
.with_menu(Box::new(tray_menu))
.with_tooltip("Tray icon example")
.with_icon(icon)
.build()
.unwrap());
}
}
fn resumed(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop) {}
fn window_event(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop, _window_id: winit::window::WindowId, _event: winit::event::WindowEvent) {
println!("window_event");
}
}
event_loop.run_app(&mut Application { tray_icon: None })?;
Ok(())
}When you run this (on Windows) and hover the tray icon, you will see lots of "new_events" log lines, but when the mouse cursor leaves the tray icon, these log lines stop, as expected.
But if you right click the tray icon and click "Test", you will see a never ending stream of "new_events" lines in the log output. Sometimes this stream of lines ends when you hover the icon and sometimes it doesn't.
Metadata
Metadata
Assignees
Labels
No labels