pub struct EventLoopBuilder<T: 'static> { /* private fields */ }
Expand description
Object that allows building the event loop.
This is used to make specifying options that affect the whole application easier. But note that constructing multiple event loops is not supported.
This can be created using EventLoop::new
or EventLoop::with_user_event
.
Implementations§
Source§impl EventLoopBuilder<()>
impl EventLoopBuilder<()>
Source§impl<T> EventLoopBuilder<T>
impl<T> EventLoopBuilder<T>
Sourcepub fn build(&mut self) -> Result<EventLoop<T>, EventLoopError>
pub fn build(&mut self) -> Result<EventLoop<T>, EventLoopError>
Builds a new event loop.
For cross-platform compatibility, the EventLoop
must be created on the main thread,
and only once per application.
Calling this function will result in display backend initialisation.
§Panics
Attempting to create the event loop off the main thread will panic. This
restriction isn’t strictly necessary on all platforms, but is imposed to
eliminate any nasty surprises when porting to platforms that require it.
EventLoopBuilderExt::any_thread
functions are exposed in the relevant
platform
module if the target platform supports creating an event
loop on any thread.
§Platform-specific
- Wayland/X11: to prevent running under
Wayland
orX11
unsetWAYLAND_DISPLAY
orDISPLAY
respectively when building the event loop. - Android: must be configured with an
AndroidApp
fromandroid_main()
by calling.with_android_app(app)
before calling.build()
, otherwise it’ll panic.
Examples found in repository?
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
fn main() -> Result<(), Box<dyn Error>> {
#[cfg(web_platform)]
console_error_panic_hook::set_once();
tracing::init();
let event_loop = EventLoop::<UserEvent>::with_user_event().build()?;
let _event_loop_proxy = event_loop.create_proxy();
// Wire the user event from another thread.
#[cfg(not(web_platform))]
std::thread::spawn(move || {
// Wake up the `event_loop` once every second and dispatch a custom event
// from a different thread.
info!("Starting to send user event every second");
loop {
let _ = _event_loop_proxy.send_event(UserEvent::WakeUp);
std::thread::sleep(std::time::Duration::from_secs(1));
}
});
let mut state = Application::new(&event_loop);
event_loop.run_app(&mut state).map_err(Into::into)
}