Event

Enum Event 

Source
#[non_exhaustive]
pub enum Event<'a> {
Show 20 variants Command(Command, Option<PhysicalKey>), Key(&'a KeyEvent, bool), ImePreedit(&'a str, Option<(usize, usize)>), ImeCommit(&'a str), Scroll(ScrollDelta), Pan(Affine), CursorMove { press: Press, }, PressStart(PressStart), PressMove { press: Press, delta: Vec2, }, PressEnd { press: Press, success: bool, }, Timer(TimerHandle), NavFocus(FocusSource), LostNavFocus, SelFocus(FocusSource), LostSelFocus, KeyFocus, LostKeyFocus, ImeFocus, LostImeFocus, MouseOver(bool),
}
Expand description

Events addressed to a widget

Note that a few events are received by disabled widgets; see Event::pass_when_disabled.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Command(Command, Option<PhysicalKey>)

Command input

A generic “command”. The source is often but not always a key press. In many cases (but not all) the target widget has navigation focus.

A PhysicalKey is attached when the command is caused by a key press. The recipient may use this to call EventState::depress_with_key.

If a widget has keyboard input focus (see EventState::request_key_focus) it will instead receive Event::Key for key presses (but may still receive Event::Command from other sources).

§

Key(&'a KeyEvent, bool)

Keyboard input: event, is_synthetic

This is only received by a widget with character focus (see EventState::request_key_focus).

On some platforms, synthetic key events are generated when a window gains or loses focus with a key held (see documentation of winit::event::WindowEvent::KeyboardInput). This is indicated by the second parameter, is_synthetic. Unless you need to track key states it is advised only to match Event::Key(event, false).

Some key presses can be mapped to a Command. To do this (normally only when event.state == ElementState::Pressed && !is_synthetic), use cx.config().shortcuts(|s| s.try_match(cx.modifiers(), &event.logical_key) or (omitting shortcut matching) Command::new(event.logical_key). Note that if the handler returns Unused the widget might then receive Event::Command for the same key press, but this is not guaranteed (behaviour may change in future versions).

For standard text input, simply consume event.text when event.state == ElementState::Pressed && !is_synthetic. NOTE: unlike Winit, we force text = None for control chars and when Ctrl, Alt or Super modifier keys are pressed. This is subject to change.

§

ImePreedit(&'a str, Option<(usize, usize)>)

Input Method Editor: composed text changed

Parameters are text, cursor.

This is only received after requesting key focus with some ime purpose.

§

ImeCommit(&'a str)

Input Method Editor: composed text committed

Parameters are text.

This is only received after requesting key focus with some ime purpose.

§

Scroll(ScrollDelta)

A mouse or touchpad scroll event

§

Pan(Affine)

A mouse or touch-screen move/zoom/rotate event

This event is sent for certain types of grab (PressStart::grab), enabling two-finger scale/rotate gestures as well as translation.

Mouse-grabs generate translation (delta component) only. Touch grabs optionally also generate rotation and scaling components, depending on the GrabMode.

§

CursorMove

Movement of mouse cursor without press

This event is only sent one case: when the mouse is moved while a Popup is open and there is not an active PressStart::grab on the mouse cursor.

This event may be sent 10+ times per frame, thus it is important that the handler be fast. It may be useful to schedule a pre-draw update with EventState::request_frame_timer to handle any post-move updates.

Fields

§press: Press
§

PressStart(PressStart)

A mouse button was pressed or touch event started

Call PressStart::grab in order to “grab” corresponding motion and release events.

This event is sent to the widget under the mouse or touch position. If no such widget is found, this event is not sent.

§

PressMove

Movement of mouse or a touch press

This event is only sent when a (PressStart::grab) is active. Motion events for the grabbed mouse pointer or touched finger are sent.

If cur_id is None, no widget was found at the coordinate (either outside the window or crate::Layout::try_probe failed).

This event may be sent 10+ times per frame, thus it is important that the handler be fast. It may be useful to schedule a pre-draw update with EventState::request_frame_timer to handle any post-move updates.

Fields

§press: Press
§delta: Vec2
§

PressEnd

End of a click/touch press

If success, this is a button-release or touch finish; otherwise this is a cancelled/interrupted grab. “Activation events” (e.g. clicking of a button or menu item) should only happen on success. “Movement events” such as panning, moving a slider or opening a menu should not be undone when cancelling: the panned item or slider should be released as is, or the menu should remain open.

This event is only sent when a (PressStart::grab) is active. Release/cancel events for the same mouse button or touched finger are sent.

If cur_id is None, no widget was found at the coordinate (either outside the window or crate::Layout::try_probe failed).

Fields

§press: Press
§success: bool
§

Timer(TimerHandle)

Update from a timer

This event must be requested by EventState::request_timer.

§

NavFocus(FocusSource)

Notification that a widget has gained navigation focus

Navigation focus implies that the widget is highlighted and will be the primary target of Event::Command, and is thus able to receive basic keyboard input (e.g. arrow keys). To receive full keyboard input (Event::Key), call EventState::request_key_focus.

With FocusSource::Pointer the widget should already have received Event::PressStart.

With FocusSource::Key, EventCx::set_scroll is called automatically (to ensure that the widget is visible) and the response will be forced to Used.

§

LostNavFocus

Notification that a widget has lost navigation focus

§

SelFocus(FocusSource)

Notification that a widget has gained selection focus

This focus must be requested by calling EventState::request_sel_focus or EventState::request_key_focus.

§

LostSelFocus

Notification that a widget has lost selection focus

In the case the widget also had character focus, Event::LostKeyFocus is received first.

§

KeyFocus

Notification that a widget has gained keyboard input focus

This focus must be requested by calling EventState::request_key_focus.

This is always preceeded by Event::SelFocus and is received prior to Event::Key events.

§

LostKeyFocus

Notification that a widget has lost keyboard input focus

§

ImeFocus

Notification that a widget has gained IME focus

The widget should call EventState::set_ime_cursor_area immediately and each time the area changes (relative to the widget’s coordinate space), until Event::LostImeFocus is received. Failure to do so will result in the widget’s entire rect being used as the IME cursor area.

§

LostImeFocus

Notification that a widget has lost IME focus

§

MouseOver(bool)

Notification that the mouse moves over or leaves a widget

The state is true on mouse over, false when the mouse leaves.

Implementations§

Source§

impl<'a> Event<'a>

Source

pub fn on_click<F: FnOnce(&mut EventCx<'_>)>( self, cx: &mut EventCx<'_>, id: Id, f: F, ) -> IsUsed

Call f on “click” or any “activation” event

This is a convenience method which calls f on any of the following:

  • Mouse click and release on the same widget
  • Touchscreen press and release on the same widget
  • Event::Command(cmd, _) where cmd.is_activate()

This method has some side effects:

Source

pub fn pass_when_disabled(&self) -> bool

Pass to disabled widgets?

When a widget is disabled:

  • New input events (Command, PressStart, Scroll) are not passed
  • Continuing input actions (PressMove, PressEnd) are passed (or the input sequence may be terminated).
  • New focus notifications are not passed
  • Focus-loss notifications are passed
  • Requested events like Timer are passed
Source

pub fn is_reusable(&self) -> bool

Can the event be received by Events::handle_event during unwinding?

Some events are sent to the widget with navigation focus (e.g. Event::Command). Others are sent to the widget under the mouse (e.g. Event::PressStart). All these events may be “reused” by an ancestor widget if not Used by the original target.

Other events are sent to a specific widget as a result of a request (e.g. Event::Key, Event::PressEnd), or as a notification of focus change (e.g. Event::LostKeyFocus). These events may never be “reused”.

Note: this could alternatively be seen as a property of the addressing mechanism, currently just an Id.

Trait Implementations§

Source§

impl<'a> Add<Offset> for Event<'a>

Source§

type Output = Event<'a>

The resulting type after applying the + operator.
Source§

fn add(self, offset: Offset) -> Self

Performs the + operation. Read more
Source§

impl<'a> AddAssign<Offset> for Event<'a>

Source§

fn add_assign(&mut self, offset: Offset)

Performs the += operation. Read more
Source§

impl<'a> Clone for Event<'a>

Source§

fn clone(&self) -> Event<'a>

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> Debug for Event<'a>

Source§

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

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

impl<'a> PartialEq for Event<'a>

Source§

fn eq(&self, other: &Event<'a>) -> 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<'a> StructuralPartialEq for Event<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Event<'a>

§

impl<'a> RefUnwindSafe for Event<'a>

§

impl<'a> !Send for Event<'a>

§

impl<'a> !Sync for Event<'a>

§

impl<'a> Unpin for Event<'a>

§

impl<'a> UnwindSafe for Event<'a>

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> 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<S, T> Cast<T> for S
where T: Conv<S>,

Source§

fn cast(self) -> T

Cast from Self to T Read more
Source§

fn try_cast(self) -> Result<T, Error>

Try converting from Self to T Read more
Source§

impl<S, T> CastApprox<T> for S
where T: ConvApprox<S>,

Source§

fn try_cast_approx(self) -> Result<T, Error>

Try approximate conversion from Self to T Read more
Source§

fn cast_approx(self) -> T

Cast approximately from Self to T Read more
Source§

impl<S, T> CastFloat<T> for S
where T: ConvFloat<S>,

Source§

fn cast_trunc(self) -> T

Cast to integer, truncating Read more
Source§

fn cast_nearest(self) -> T

Cast to the nearest integer Read more
Source§

fn cast_floor(self) -> T

Cast the floor to an integer Read more
Source§

fn cast_ceil(self) -> T

Cast the ceiling to an integer Read more
Source§

fn try_cast_trunc(self) -> Result<T, Error>

Try converting to integer with truncation Read more
Source§

fn try_cast_nearest(self) -> Result<T, Error>

Try converting to the nearest integer Read more
Source§

fn try_cast_floor(self) -> Result<T, Error>

Try converting the floor to an integer Read more
Source§

fn try_cast_ceil(self) -> Result<T, Error>

Try convert the ceiling to an integer 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> 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> 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> 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