Enum kas::event::Event

source ·
#[non_exhaustive]
pub enum Event {
Show 16 variants Command(Command, Option<PhysicalKey>), Key(KeyEvent, bool), Scroll(ScrollDelta), Pan { alpha: DVec2, delta: DVec2, }, CursorMove { press: Press, }, PressStart { press: Press, }, PressMove { press: Press, delta: Offset, }, PressEnd { press: Press, success: bool, }, Timer(u64), NavFocus(FocusSource), LostNavFocus, SelFocus(FocusSource), LostSelFocus, KeyFocus, LostKeyFocus, MouseHover(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(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.

§

Scroll(ScrollDelta)

A mouse or touchpad scroll event

§

Pan

Fields

§alpha: DVec2

Rotation and scale component

§delta: DVec2

Translation component

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

This event is sent for certain types of grab (Press::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.

In general, a point p on the screen should be transformed as follows:

let mut p = Coord::ZERO; // or whatever
p = (alpha.complex_mul(p.cast()) + delta).cast_nearest();

When it is known that there is no rotational component, one can use a simpler transformation: alpha.0 * p + delta. When there is also no scaling component, we just have a translation: p + delta. Note however that if events are generated with rotation and/or scaling components, these simplifications are invalid.

Two such transforms may be combined as follows:

let alpha = alpha2.complex_mul(alpha1);
let delta = alpha2.complex_mul(delta1) + delta2;

If instead one uses a transform to map screen-space to world-space, this transform should be adjusted as follows:

world_alpha = world_alpha.complex_div(alpha.into());
world_delta = world_delta - world_alpha.complex_mul(delta.into());

Those familiar with complex numbers may recognise that alpha = a * e^{i*t} where a is the scale component and t is the angle of rotation. Calculate these components as follows:

let a = (alpha.0 * alpha.0 + alpha.1 * alpha.1).sqrt();
let t = (alpha.1).atan2(alpha.0);
§

CursorMove

Fields

§press: Press

Movement of mouse cursor without press

This event is sent only when:

  1. No Press::grab is active
  2. A Popup is open

The parent (or an ancestor) of a Popup should handle or explicitly ignore this event.

§

PressStart

Fields

§press: Press

A mouse button was pressed or touch event started

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

This event is sent in exactly two cases, in this order:

  1. When a Popup is open. A Popup will close itself if press.id is not a descendant of itself, but will still return Unused. The parent (or an ancestor) of the Popup should handle this event.
  2. If a widget is found under the mouse when pressed or where a touch event starts, this event is sent to the widget.

If start_id is None, then no widget was found at the coordinate and the event will only be delivered to pop-up layer owners.

§

PressMove

Fields

§press: Press
§delta: Offset

Movement of mouse or a touch press

This event is only sent when a (Press::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::find_id failed).

§

PressEnd

Fields

§press: Press
§success: bool

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 (Press::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::find_id failed).

§

Timer(u64)

Update from a timer

This event is received after requesting timed wake-up(s) (see EventState::request_timer).

The u64 payload is copied from 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

§

MouseHover(bool)

Notification that a widget gains or loses mouse hover

The payload is true when focus is gained, false when lost.

Implementations§

source§

impl Event

source

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

Call f on any “activation” event

Activation is considered:

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

The method calls EventState::depress_with_key on activation.

source

pub fn pass_when_disabled(&self) -> bool

Pass to disabled widgets?

Disabled status should disable input handling but not prevent other notifications.

source

pub fn is_reusable(&self) -> bool

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

Events which may be sent to the widget under the mouse or to the keyboard navigation target may be acted on by an ancestor if unused. Other events may not be; e.g. Event::PressMove and Event::PressEnd are only received by the widget requesting them while Event::LostKeyFocus (and similar events) are only sent to a specific widget.

Trait Implementations§

source§

impl Add<Offset> for Event

§

type Output = Event

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl AddAssign<Offset> for Event

source§

fn add_assign(&mut self, offset: Offset)

Performs the += operation. Read more
source§

impl Clone for Event

source§

fn clone(&self) -> Event

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

source§

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

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

impl PartialEq for Event

source§

fn eq(&self, other: &Event) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for Event

Auto Trait Implementations§

§

impl RefUnwindSafe for Event

§

impl !Send for Event

§

impl !Sync for Event

§

impl Unpin for Event

§

impl UnwindSafe for Event

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
§

impl<S, T> Cast<T> for S
where T: Conv<S>,

§

fn cast(self) -> T

Cast from Self to T Read more
§

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

Try converting from Self to T Read more
§

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

§

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

Try approximate conversion from Self to T Read more
§

fn cast_approx(self) -> T

Cast approximately from Self to T Read more
§

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

§

fn cast_trunc(self) -> T

Cast to integer, truncating Read more
§

fn cast_nearest(self) -> T

Cast to the nearest integer Read more
§

fn cast_floor(self) -> T

Cast the floor to an integer Read more
§

fn cast_ceil(self) -> T

Cast the ceiling to an integer Read more
§

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

Try converting to integer with truncation Read more
§

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

Try converting to the nearest integer Read more
§

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

Try converting the floor to an integer Read more
§

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

Try convert the ceiling to an integer Read more
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

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

§

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.
§

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.
§

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.
§

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.

§

impl<T> Instrument for T

§

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

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

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.

§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

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,

§

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

§

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

§

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.
§

impl<T> Upcast<T> for T

§

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

§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self>

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