Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update winit to 0.30 #220

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ core-foundation = "0.10"
png = "0.17"

[dev-dependencies]
winit = "0.29"
winit = "0.30"
tao = "0.30"
image = "0.25"
eframe = "0.27"
eframe = "0.30"
serde_json = "1"
2 changes: 1 addition & 1 deletion examples/egui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() -> Result<(), eframe::Error> {
.borrow_mut()
.replace(TrayIconBuilder::new().with_icon(icon).build().unwrap());
}
Box::<MyApp>::default()
Ok(Box::<MyApp>::default())
}),
)
}
Expand Down
144 changes: 89 additions & 55 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,92 +6,126 @@

use tray_icon::{
menu::{AboutMetadata, Menu, MenuEvent, MenuItem, PredefinedMenuItem},
TrayIconBuilder, TrayIconEvent,
TrayIcon, TrayIconBuilder, TrayIconEvent, TrayIconEventReceiver,
};
use winit::{
application::ApplicationHandler,
event::Event,
event_loop::{ControlFlow, EventLoopBuilder},
event_loop::{ControlFlow, EventLoop, EventLoopBuilder},
};

#[derive(Debug)]
enum UserEvent {
TrayIconEvent(tray_icon::TrayIconEvent),
MenuEvent(tray_icon::menu::MenuEvent),
}

fn main() {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/icon.png");
struct Application {
tray_icon: Option<TrayIcon>,
}

// Since winit doesn't use gtk on Linux, and we need gtk for
// the tray icon to show up, we need to spawn a thread
// where we initialize gtk and create the tray_icon
#[cfg(target_os = "linux")]
std::thread::spawn(|| {
use tray_icon::menu::Menu;
impl Application {
fn new() -> Application {
Application { tray_icon: None }
}

fn new_tray_icon() -> TrayIcon {
let path = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/icon.png");
let icon = load_icon(std::path::Path::new(path));

gtk::init().unwrap();
let _tray_icon = TrayIconBuilder::new()
.with_menu(Box::new(Menu::new()))
TrayIconBuilder::new()
.with_menu(Box::new(Self::new_tray_menu()))
.with_tooltip("winit - awesome windowing lib")
.with_icon(icon)
.with_title("x")
.build()
.unwrap();
.unwrap()
}

fn new_tray_menu() -> Menu {
let menu = Menu::new();
let item1 = MenuItem::new("item1", true, None);
if let Err(err) = menu.append(&item1) {
println!("{err:?}");
}
menu
}
}

gtk::main();
});
impl ApplicationHandler<UserEvent> for Application {
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,
) {
}

fn new_events(
&mut self,
_event_loop: &winit::event_loop::ActiveEventLoop,
cause: winit::event::StartCause,
) {
// We create the icon once the event loop is actually running
// to prevent issues like https://github.com/tauri-apps/tray-icon/issues/90
if winit::event::StartCause::Init == cause {
#[cfg(not(target_os = "linux"))]
{
self.tray_icon = Some(Self::new_tray_icon());
}

// We have to request a redraw here to have the icon actually show up.
// Winit only exposes a redraw method on the Window so we use core-foundation directly.
#[cfg(target_os = "macos")]
unsafe {
use core_foundation::runloop::{CFRunLoopGetMain, CFRunLoopWakeUp};

let rl = CFRunLoopGetMain();
CFRunLoopWakeUp(rl);
}
}
}

let event_loop = EventLoopBuilder::<UserEvent>::with_user_event()
.build()
.unwrap();
fn user_event(&mut self, _event_loop: &winit::event_loop::ActiveEventLoop, event: UserEvent) {
println!("{event:?}");
}
}

fn main() {
let event_loop = EventLoop::<UserEvent>::with_user_event().build().unwrap();

// set a tray event handler that forwards the event and wakes up the event loop
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));
}));

#[cfg(not(target_os = "linux"))]
let mut tray_icon = None;
let mut app = Application::new();

let menu_channel = MenuEvent::receiver();
let tray_channel = TrayIconEvent::receiver();

event_loop.run(move |event, event_loop| {
event_loop.set_control_flow(ControlFlow::Wait);

match event {
#[cfg(not(target_os = "linux"))]
Event::NewEvents(winit::event::StartCause::Init) => {
let icon = load_icon(std::path::Path::new(path));

// We create the icon once the event loop is actually running
// to prevent issues like https://github.com/tauri-apps/tray-icon/issues/90
tray_icon = Some(
TrayIconBuilder::new()
.with_menu(Box::new(Menu::new()))
.with_tooltip("winit - awesome windowing lib")
.with_icon(icon)
.with_title("x")
.build()
.unwrap(),
);
// We have to request a redraw here to have the icon actually show up.
// Winit only exposes a redraw method on the Window so we use core-foundation directly.
#[cfg(target_os = "macos")]
unsafe {
use core_foundation::runloop::{CFRunLoopGetMain, CFRunLoopWakeUp};

let rl = CFRunLoopGetMain();
CFRunLoopWakeUp(rl);
}
}
// Since winit doesn't use gtk on Linux, and we need gtk for
// the tray icon to show up, we need to spawn a thread
// where we initialize gtk and create the tray_icon
#[cfg(target_os = "linux")]
std::thread::spawn(|| {
gtk::init().unwrap();

Event::UserEvent(UserEvent::TrayIconEvent(event)) => {
println!("{event:?}");
}
let _tray_icon = Application::new_tray_icon();

_ => {}
}
gtk::main();
});

if let Err(err) = event_loop.run_app(&mut app) {
println!("Error: {:?}", err);
}
}

fn load_icon(path: &std::path::Path) -> tray_icon::Icon {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@
//! Same can be done for menu events using [`MenuEvent::set_event_handler`].
//!
//! ```no_run
//! # use winit::event_loop::EventLoopBuilder;
//! # use winit::event_loop::EventLoop;
//! enum UserEvent {
//! TrayIconEvent(tray_icon::TrayIconEvent),
//! MenuEvent(tray_icon::menu::MenuEvent)
//! }
//!
//! let event_loop = EventLoopBuilder::<UserEvent>::with_user_event().build().unwrap();
//! let event_loop = EventLoop::<UserEvent>::with_user_event().build().unwrap();
//!
//! let proxy = event_loop.create_proxy();
//! tray_icon::TrayIconEvent::set_event_handler(Some(move |event| {
Expand Down
Loading