Event

Enum Event 

Source
pub enum Event<'a, T>
where T: 'static,
{ NewEvents(StartCause), WindowEvent { window_id: WindowId, event: WindowEvent<'a>, }, DeviceEvent { device_id: DeviceId, event: DeviceEvent, }, UserEvent(T), Suspended, Resumed, MainEventsCleared, RedrawRequested(WindowId), RedrawEventsCleared, LoopDestroyed, }
Expand description

Describes a generic event.

See the module-level docs for more information on the event loop manages each event.

Variants§

§

NewEvents(StartCause)

Emitted when new events arrive from the OS to be processed.

This event type is useful as a place to put code that should be done before you start processing events, such as updating frame timing information for benchmarking or checking the StartCause to see if a timer set by ControlFlow::WaitUntil has elapsed.

§

WindowEvent

Emitted when the OS sends an event to a winit window.

Fields

§window_id: WindowId
§event: WindowEvent<'a>
§

DeviceEvent

Emitted when the OS sends an event to a device.

Fields

§device_id: DeviceId
§

UserEvent(T)

Emitted when an event is sent from EventLoopProxy::send_event

§

Suspended

Emitted when the application has been suspended.

§Portability

Not all platforms support the notion of suspending applications, and there may be no technical way to guarantee being able to emit a Suspended event if the OS has no formal application lifecycle (currently only Android and iOS do). For this reason, Winit does not currently try to emit pseudo Suspended events before the application quits on platforms without an application lifecycle.

Considering that the implementation of Suspended and Resumed events may be internally driven by multiple platform-specific events, and that there may be subtle differences across platforms with how these internal events are delivered, it’s recommended that applications be able to gracefully handle redundant (i.e. back-to-back) Suspended or Resumed events.

Also see Resumed notes.

§Android

On Android, the Suspended event is only sent when the application’s associated SurfaceView is destroyed. This is expected to closely correlate with the onPause lifecycle event but there may technically be a discrepancy.

Applications that need to run on Android should assume their SurfaceView has been destroyed, which indirectly invalidates any existing render surfaces that may have been created outside of Winit (such as an EGLSurface, VkSurfaceKHR or wgpu::Surface).

After being Suspended on Android applications must drop all render surfaces before the event callback completes, which may be re-created when the application is next Resumed.

§iOS

On iOS, the Suspended event is currently emitted in response to an applicationWillResignActive callback which means that the application is about to transition from the active to inactive state (according to the iOS application lifecycle).

§

Resumed

Emitted when the application has been resumed.

For consistency, all platforms emit a Resumed event even if they don’t themselves have a formal suspend/resume lifecycle. For systems without a standard suspend/resume lifecycle the Resumed event is always emitted after the NewEvents(StartCause::Init) event.

§Portability

It’s recommended that applications should only initialize their graphics context and create a window after they have received their first Resumed event. Some systems (specifically Android) won’t allow applications to create a render surface until they are resumed.

Considering that the implementation of Suspended and Resumed events may be internally driven by multiple platform-specific events, and that there may be subtle differences across platforms with how these internal events are delivered, it’s recommended that applications be able to gracefully handle redundant (i.e. back-to-back) Suspended or Resumed events.

Also see Suspended notes.

§Android

On Android, the Resumed event is sent when a new SurfaceView has been created. This is expected to closely correlate with the onResume lifecycle event but there may technically be a discrepancy.

Applications that need to run on Android must wait until they have been Resumed before they will be able to create a render surface (such as an EGLSurface, VkSurfaceKHR or wgpu::Surface) which depend on having a SurfaceView. Applications must also assume that if they are Suspended, then their render surfaces are invalid and should be dropped.

Also see Suspended notes.

§iOS

On iOS, the Resumed event is emitted in response to an applicationDidBecomeActive callback which means the application is “active” (according to the iOS application lifecycle).

§

MainEventsCleared

Emitted when all of the event loop’s input events have been processed and redraw processing is about to begin.

This event is useful as a place to put your code that should be run after all state-changing events have been handled and you want to do stuff (updating state, performing calculations, etc) that happens as the “main body” of your event loop. If your program only draws graphics when something changes, it’s usually better to do it in response to Event::RedrawRequested, which gets emitted immediately after this event. Programs that draw graphics continuously, like most games, can render here unconditionally for simplicity.

§

RedrawRequested(WindowId)

Emitted after MainEventsCleared when a window should be redrawn.

This gets triggered in two scenarios:

  • The OS has performed an operation that’s invalidated the window’s contents (such as resizing the window).
  • The application has explicitly requested a redraw via [Window::request_redraw].

During each iteration of the event loop, Winit will aggregate duplicate redraw requests into a single event, to help avoid duplicating rendering work.

Mainly of interest to applications with mostly-static graphics that avoid redrawing unless something changes, like most non-game GUIs.

§Platform-specific
  • macOS / iOS: Due to implementation difficulties, this will often, but not always, be emitted directly inside drawRect:, with neither a preceding MainEventsCleared nor subsequent RedrawEventsCleared. See #2640 for work on this.
§

RedrawEventsCleared

Emitted after all RedrawRequested events have been processed and control flow is about to be taken away from the program. If there are no RedrawRequested events, it is emitted immediately after MainEventsCleared.

This event is useful for doing any cleanup or bookkeeping work after all the rendering tasks have been completed.

§

LoopDestroyed

Emitted when the event loop is being shut down.

This is irreversible - if this event is emitted, it is guaranteed to be the last event that gets emitted. You generally want to treat this as an “do on quit” event.

Implementations§

Source§

impl<'a, T> Event<'a, T>

Source

pub fn map_nonuser_event<U>(self) -> Result<Event<'a, U>, Event<'a, T>>

Source

pub fn to_static(self) -> Option<Event<'static, T>>

If the event doesn’t contain a reference, turn it into an event with a 'static lifetime. Otherwise, return None.

Trait Implementations§

Source§

impl<T> Clone for Event<'static, T>
where T: Clone,

Source§

fn clone(&self) -> Event<'static, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a, T> Debug for Event<'a, T>
where T: Debug + 'static,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a, T> PartialEq for Event<'a, T>
where T: PartialEq + 'static,

Source§

fn eq(&self, other: &Event<'a, T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl System<Event<'static, ()>> for InputSystem

Source§

fn run(&mut self, world: &mut World, event: &Event<'static, ()>)

Source§

impl<'a, T> StructuralPartialEq for Event<'a, T>
where T: 'static,

Auto Trait Implementations§

§

impl<'a, T> Freeze for Event<'a, T>
where T: Freeze,

§

impl<'a, T> RefUnwindSafe for Event<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Event<'a, T>
where T: Send,

§

impl<'a, T> Sync for Event<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Event<'a, T>
where T: Unpin,

§

impl<'a, T> !UnwindSafe for Event<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Source§

fn type_name(&self) -> &'static str

Gets the type name of self
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ComponentValueBase for T
where T: Send + Sync + 'static,

Source§

fn type_name(&self) -> &'static str

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert 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>

Convert 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)

Convert &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)

Convert &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> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ComponentValue for T

Source§

impl<T> ErasedDestructor for T
where T: 'static,