pub struct EventLoop { /* private fields */ }Expand description
Provides a way to retrieve events from the system and from the windows that were registered to the events loop.
An EventLoop can be seen more or less as a “context”. Calling EventLoop::new
initializes everything that will be required to create windows. For example on Linux creating
an event loop opens a connection to the X or Wayland server.
To wake up an EventLoop from a another thread, see the EventLoopProxy docs.
Note that this cannot be shared across threads (due to platform-dependant logic
forbidding it), as such it is neither Send nor Sync. If you need cross-thread access,
the Window created from this can be sent to an other thread, and the
EventLoopProxy allows you to wake up an EventLoop from another thread.
Implementations§
Source§impl EventLoop
impl EventLoop
Sourcepub fn new() -> Result<EventLoop, EventLoopError>
pub fn new() -> Result<EventLoop, EventLoopError>
Create the event loop.
This is an alias of EventLoop::builder().build().
Sourcepub fn builder() -> EventLoopBuilder
pub fn builder() -> EventLoopBuilder
Start building a new event loop.
This returns an EventLoopBuilder, to allow configuring the event loop before creation.
To get the actual event loop, call build on that.
Sourcepub fn run_app<A>(self, app: A) -> Result<(), EventLoopError>where
A: ApplicationHandler + 'static,
pub fn run_app<A>(self, app: A) -> Result<(), EventLoopError>where
A: ApplicationHandler + 'static,
Run the event loop with the given application on the calling thread.
The app is dropped when the event loop is shut down.
§Event loop flow
This function internally handles the different parts of a traditional event-handling loop. You can imagine this method as being implemented like this:
let mut start_cause = StartCause::Init;
// Run the event loop.
while !event_loop.exiting() {
// Wake up.
app.new_events(event_loop, start_cause);
// Indicate that surfaces can now safely be created.
if start_cause == StartCause::Init {
app.can_create_surfaces(event_loop);
}
// Handle proxy wake-up event.
if event_loop.proxy_wake_up_set() {
event_loop.proxy_wake_up_clear();
app.proxy_wake_up(event_loop);
}
// Handle actions done by the user / system such as moving the cursor, resizing the
// window, changing the window theme, etc.
for event in event_loop.events() {
match event {
window event => app.window_event(event_loop, window_id, event),
device event => app.device_event(event_loop, device_id, event),
}
}
// Handle redraws.
for window_id in event_loop.pending_redraws() {
app.window_event(event_loop, window_id, WindowEvent::RedrawRequested);
}
// Done handling events, wait until we're woken up again.
app.about_to_wait(event_loop);
start_cause = event_loop.wait_if_necessary();
}
// Finished running, drop application state.
drop(app);This is of course a very coarse-grained overview, and leaves out timing details like
ControlFlow::WaitUntil and life-cycle methods like ApplicationHandler::resumed, but
it should give you an idea of how things fit together.
§Returns
The semantics of this function can be a bit confusing, because the way different platforms control their event loop varies significantly.
On most platforms (Android, macOS, Orbital, X11, Wayland, Windows), this blocks the caller,
runs the event loop internally, and then returns once ActiveEventLoop::exit is called.
See run_app_on_demand for more detailed semantics.
On iOS, this will register the application handler, and then call UIApplicationMain
(which is the only way to run the system event loop), which never returns to the caller
(the process instead exits after the handler has been dropped). See also
run_app_never_return.
On the web, this works by registering the application handler, and then immediately
returning to the caller. This is necessary because WebAssembly (and JavaScript) is always
executed in the context of the browser’s own (internal) event loop, and thus we need to
return to avoid blocking that and allow events to later be delivered asynchronously. See
also register_app.
If you call this function inside fn main, you usually do not need to think about these
details.
§Static
To alleviate the issues noted above, this function requires that you pass in a 'static
handler, to ensure that any state your application uses will be alive as long as the
application is running.
To be clear, you should avoid doing e.g. event_loop.run_app(&mut app)?, and prefer
event_loop.run_app(app)? instead.
If this requirement is prohibitive for you, consider using run_app_on_demand instead
(though note that this is not available on iOS and web).
Sourcepub fn create_proxy(&self) -> EventLoopProxy
pub fn create_proxy(&self) -> EventLoopProxy
Creates an EventLoopProxy that can be used to dispatch user events
to the main event loop, possibly from another thread.
Sourcepub fn owned_display_handle(&self) -> OwnedDisplayHandle
pub fn owned_display_handle(&self) -> OwnedDisplayHandle
Gets a persistent reference to the underlying platform display.
See the OwnedDisplayHandle type for more information.
Sourcepub fn listen_device_events(&self, allowed: DeviceEvents)
pub fn listen_device_events(&self, allowed: DeviceEvents)
Change if or when DeviceEvents are captured.
See ActiveEventLoop::listen_device_events for details.
Sourcepub fn set_control_flow(&self, control_flow: ControlFlow)
pub fn set_control_flow(&self, control_flow: ControlFlow)
Sets the ControlFlow.
Sourcepub fn create_custom_cursor(
&self,
custom_cursor: CustomCursorSource,
) -> Result<CustomCursor, RequestError>
pub fn create_custom_cursor( &self, custom_cursor: CustomCursorSource, ) -> Result<CustomCursor, RequestError>
Trait Implementations§
Source§impl AsFd for EventLoop
Available on x11_platform or wayland_platform only.
impl AsFd for EventLoop
x11_platform or wayland_platform only.Source§fn as_fd(&self) -> BorrowedFd<'_>
fn as_fd(&self) -> BorrowedFd<'_>
Get the underlying EventLoop’s fd which you can register
into other event loop, like calloop or mio. When doing so, the
loop must be polled with the pump_app_events API.
Source§impl EventLoopExtPumpEvents for EventLoop
Available on windows_platform or macos_platform or android_platform or x11_platform or wayland_platform or docsrs only.
impl EventLoopExtPumpEvents for EventLoop
windows_platform or macos_platform or android_platform or x11_platform or wayland_platform or docsrs only.Source§fn pump_app_events<A>(
&mut self,
timeout: Option<Duration>,
app: A,
) -> PumpStatuswhere
A: ApplicationHandler,
fn pump_app_events<A>(
&mut self,
timeout: Option<Duration>,
app: A,
) -> PumpStatuswhere
A: ApplicationHandler,
EventLoop to check for and dispatch pending events. Read moreSource§impl EventLoopExtRunOnDemand for EventLoop
Available on windows_platform or macos_platform or android_platform or orbital_platform or x11_platform or wayland_platform or docsrs only.
impl EventLoopExtRunOnDemand for EventLoop
windows_platform or macos_platform or android_platform or orbital_platform or x11_platform or wayland_platform or docsrs only.Source§fn run_app_on_demand<A>(&mut self, app: A) -> Result<(), EventLoopError>where
A: ApplicationHandler,
fn run_app_on_demand<A>(&mut self, app: A) -> Result<(), EventLoopError>where
A: ApplicationHandler,
Source§impl EventLoopExtWayland for EventLoop
Available on wayland_platform only.
impl EventLoopExtWayland for EventLoop
wayland_platform only.Source§fn is_wayland(&self) -> bool
fn is_wayland(&self) -> bool
EventLoop uses Wayland.Source§impl EventLoopExtX11 for EventLoop
Available on x11_platform only.
impl EventLoopExtX11 for EventLoop
x11_platform only.Source§impl HasDisplayHandle for EventLoop
impl HasDisplayHandle for EventLoop
Source§fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError>
fn display_handle(&self) -> Result<DisplayHandle<'_>, HandleError>
Auto Trait Implementations§
impl !Freeze for EventLoop
impl !RefUnwindSafe for EventLoop
impl !Send for EventLoop
impl !Sync for EventLoop
impl Unpin for EventLoop
impl UnsafeUnpin for EventLoop
impl !UnwindSafe for EventLoop
Blanket Implementations§
Source§impl<T> HasRawDisplayHandle for Twhere
T: HasDisplayHandle + ?Sized,
impl<T> HasRawDisplayHandle for Twhere
T: HasDisplayHandle + ?Sized,
Source§fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError>
fn raw_display_handle(&self) -> Result<RawDisplayHandle, HandleError>
Use HasDisplayHandle instead
Source§impl<T> AsSource for Twhere
T: AsFd,
impl<T> AsSource for Twhere
T: AsFd,
Source§fn source(&self) -> BorrowedFd<'_>
fn source(&self) -> BorrowedFd<'_>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more