-
I want to make an eframe app that can be minimized to the tray - the window can fully disappear at some point. How would I go about doing this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
2 ish years too late but it was a pain to figure out, so here's my newfound knowledge for those that come after: egui/eframe doesn't support that even now, years later as far as I can see. Using the egui::viewport commands also doesn't work - #3655. I think it's because egui doesn't update once the window is not interactable e.g. invisible/minimized. It also doesn't allow for running eframe on an existing event loop - #2875. This means you can't have your own loop where you actually honest to god close the window and re-create it every time to "minimize to tray". I really do wish it exposed But aftrer like 2 days of screwing around, this is the simplest solution I found. This is for Windows, however, you can very likely substitute platform specific code here in much the same way: eframe::run_native(
// --snip--
Box::new(|cc| {
let winit::raw_window_handle::RawWindowHandle::Win32(handle) = cc.window_handle().unwrap().as_raw() else {
panic!("Unsupported platform");
};
let context = cc.egui_ctx.clone();
// tray-icon crate
// https://docs.rs/tray-icon/0.12.0/tray_icon/struct.TrayIconEvent.html#method.set_event_handler
TrayIconEvent::set_event_handler(Some(move |event: TrayIconEvent| {
println!("TrayIconEvent: {:?}", event);
if event.click_type != tray_icon::ClickType::Double {
return;
}
// Just a static Mutex<bool>
let mut visible = VISIBLE.lock().unwrap();
if *visible {
let window_handle = windows::Win32::Foundation::HWND(handle.hwnd.into());
let hide = windows::Win32::UI::WindowsAndMessaging::SW_HIDE;
unsafe {
windows::Win32::UI::WindowsAndMessaging::ShowWindow(window_handle, hide);
}
*visible = false;
} else {
let window_handle = windows::Win32::Foundation::HWND(handle.hwnd.into());
// You can show the window in all sorts of ways:
// https://learn.microsoft.com/en-gb/windows/win32/api/winuser/nf-winuser-showwindow
let show = windows::Win32::UI::WindowsAndMessaging::SW_SHOWDEFAULT;
unsafe {
windows::Win32::UI::WindowsAndMessaging::ShowWindow(window_handle, show);
}
*visible = true;
}
}
));
Box::new(...)
}), |
Beta Was this translation helpful? Give feedback.
-
I tried modifying it as follows so that it can be used in the latest version, such as egui 0.28.1.
|
Beta Was this translation helpful? Give feedback.
2 ish years too late but it was a pain to figure out, so here's my newfound knowledge for those that come after:
egui/eframe doesn't support that even now, years later as far as I can see.
Using the egui::viewport commands also doesn't work - #3655. I think it's because egui doesn't update once the window is not interactable e.g. invisible/minimized.
It also doesn't allow for running eframe on an existing event loop - #2875. This means you can't have your own loop where you actually honest to god close the window and re-create it every time to "minimize to tray". I really do wish it exposed
native
or something.But aftrer like 2 days of screwing around, this is the simplest solution I fou…