From 8e73287646396b23278a646b9a7d09f8e7decf03 Mon Sep 17 00:00:00 2001 From: satrix321 Date: Wed, 14 Aug 2019 00:09:34 +0200 Subject: [PATCH] Change 'proxy.rs' into 'custom_events.rs" (#1101) * changed i32 to CustomEvent enum * added a match case for custom event * minor cleanup * fixes #953 --- examples/custom_events.rs | 41 +++++++++++++++++++++++++++++++++++++++ examples/proxy.rs | 37 ----------------------------------- 2 files changed, 41 insertions(+), 37 deletions(-) create mode 100644 examples/custom_events.rs delete mode 100644 examples/proxy.rs diff --git a/examples/custom_events.rs b/examples/custom_events.rs new file mode 100644 index 00000000..c85d4ecd --- /dev/null +++ b/examples/custom_events.rs @@ -0,0 +1,41 @@ +use winit::{ + event::{Event, WindowEvent}, + event_loop::{ControlFlow, EventLoop}, + window::WindowBuilder, +}; + +#[derive(Debug, Clone, Copy)] +enum CustomEvent { + Timer, +} + +fn main() { + let event_loop = EventLoop::::with_user_event(); + + let _window = WindowBuilder::new() + .with_title("A fantastic window!") + .build(&event_loop) + .unwrap(); + + // `EventLoopProxy` allows you to dispatch custom events to the main Winit event + // loop from any thread. + let event_loop_proxy = event_loop.create_proxy(); + + std::thread::spawn(move || { + // Wake up the `event_loop` once every second and dispatch a custom event + // from a different thread. + loop { + std::thread::sleep(std::time::Duration::from_secs(1)); + event_loop_proxy.send_event(CustomEvent::Timer).ok(); + } + }); + + event_loop.run(move |event, _, control_flow| match event { + Event::UserEvent(event) => println!("user event: {:?}", event), + Event::WindowEvent { + event: WindowEvent::CloseRequested, + .. + } => *control_flow = ControlFlow::Exit, + _ => *control_flow = ControlFlow::Wait, + }); +} diff --git a/examples/proxy.rs b/examples/proxy.rs deleted file mode 100644 index d078f230..00000000 --- a/examples/proxy.rs +++ /dev/null @@ -1,37 +0,0 @@ -use winit::{ - event::{Event, WindowEvent}, - event_loop::{ControlFlow, EventLoop}, - window::WindowBuilder, -}; - -fn main() { - let event_loop: EventLoop = EventLoop::with_user_event(); - - let _window = WindowBuilder::new() - .with_title("A fantastic window!") - .build(&event_loop) - .unwrap(); - - let proxy = event_loop.create_proxy(); - - std::thread::spawn(move || { - let mut counter = 0; - // Wake up the `event_loop` once every second. - loop { - std::thread::sleep(std::time::Duration::from_secs(1)); - proxy.send_event(counter).unwrap(); - counter += 1; - } - }); - - event_loop.run(move |event, _, control_flow| { - println!("{:?}", event); - match event { - Event::WindowEvent { - event: WindowEvent::CloseRequested, - .. - } => *control_flow = ControlFlow::Exit, - _ => *control_flow = ControlFlow::Wait, - } - }); -}