pub struct EventState { /* private fields */ }
Expand description
Event context state
This struct encapsulates window-specific event-handling state and handling.
Most operations are only available via a EventCx
handle, though some
are available on this struct.
Besides event handling, this struct also configures widgets.
Some methods are intended only for usage by graphics and platform backends
and are hidden from generated documentation unless the internal_doc
feature is enabled. Event handling assumes winit.
Implementations§
Source§impl EventState
impl EventState
Sourcepub fn modifiers(&self) -> ModifiersState
pub fn modifiers(&self) -> ModifiersState
Get the current modifier state
Sourcepub fn has_key_focus(&self, w_id: &Id) -> (bool, bool)
pub fn has_key_focus(&self, w_id: &Id) -> (bool, bool)
Get whether this widget has (key_focus, sel_focus)
key_focus
: implies this widget receives keyboard inputsel_focus
: implies this widget is allowed to select things
Note that key_focus
implies sel_focus
.
Sourcepub fn request_key_focus(
&mut self,
target: Id,
ime: Option<ImePurpose>,
source: FocusSource,
)
pub fn request_key_focus( &mut self, target: Id, ime: Option<ImePurpose>, source: FocusSource, )
Request keyboard input focus
When granted, the widget will receive Event::KeyFocus
followed by
Event::Key
for each key press / release. Note that this disables
translation of key events to Event::Command
while key focus is
active.
Providing an ImePurpose
enables Input Method Editor events
(see Event::ImeFocus
). TODO: this feature is incomplete; winit does
not currently support setting surrounding text.
The source
parameter is used by Event::SelFocus
.
Key focus implies sel focus (see Self::request_sel_focus
) and
navigation focus.
Sourcepub fn request_sel_focus(&mut self, target: Id, source: FocusSource)
pub fn request_sel_focus(&mut self, target: Id, source: FocusSource)
Request selection focus
To prevent multiple simultaneous selections (e.g. of text) in the UI,
only widgets with “selection focus” are allowed to select things.
Selection focus is implied by character focus. Event::LostSelFocus
is sent when selection focus is lost; in this case any existing
selection should be cleared.
The source
parameter is used by Event::SelFocus
.
Selection focus implies navigation focus.
When key focus is lost, Event::LostSelFocus
is sent.
Sourcepub fn depress_with_key(
&mut self,
id: impl HasId,
code: impl Into<Option<PhysicalKey>>,
)
pub fn depress_with_key( &mut self, id: impl HasId, code: impl Into<Option<PhysicalKey>>, )
Visually depress a widget via a key code
When a button-like widget is activated by a key it may call this to ensure the widget is visually depressed until the key is released. The widget will not receive a notification of key-release but will be redrawn automatically.
Note that keyboard shortcuts and mnemonics should usually match against
the “logical key”. PhysicalKey
is used here since the the logical key
may be changed by modifier keys.
Does nothing when code
is None
.
Sourcepub fn cancel_ime_focus(&mut self, target: Id)
pub fn cancel_ime_focus(&mut self, target: Id)
End Input Method Editor focus on target
, if present
Sourcepub fn set_ime_cursor_area(&mut self, target: &Id, rect: Rect)
pub fn set_ime_cursor_area(&mut self, target: &Id, rect: Rect)
Set IME cursor area
This should be called after receiving Event::ImeFocus
, and any time
that the rect
parameter changes, until Event::LostImeFocus
is
received.
This sets the text cursor’s area, rect
, relative to the widget’s own
coordinate space. If never called, then the widget’s whole rect is used.
This does nothing if target
does not have IME-enabled input focus.
Source§impl EventState
impl EventState
Get whether this widget has navigation focus
Get the current navigation focus, if any
This is the widget selected by navigating the UI with the Tab key.
Note: changing navigation focus (e.g. via Self::clear_nav_focus
,
Self::request_nav_focus
or Self::next_nav_focus
) does not
immediately affect the result of this method.
Clear navigation focus
Request navigation focus directly
If id
already has navigation focus or navigation focus is disabled
globally then nothing happens. If widget id
supports
navigation focus, then it should receive
Event::NavFocus
; if not then the first supporting ancestor will
receive focus.
Advance the navigation focus
If target == Some(id)
, this looks for the next widget from id
(inclusive) which is navigable. Otherwise where
some widget id
has nav_focus
this looks for the
next navigable widget excluding id
. If no reference is available,
this instead looks for the first navigable widget.
If reverse
, instead search for the previous or last navigable widget.
Attempts to set a fallback to receive Event::Command
In case a navigation key is pressed (see Command
) but no widget has
navigation focus, then, if a fallback has been set, that widget will
receive the key via Event::Command
.
Only one widget can be a fallback, and the first to set itself wins. This is primarily used to allow scroll-region widgets to respond to navigation keys when no widget has focus.
Source§impl EventState
Mouse and touch methods
impl EventState
Mouse and touch methods
Sourcepub fn is_depressed(&self, w_id: &Id) -> bool
pub fn is_depressed(&self, w_id: &Id) -> bool
Check whether the given widget is visually depressed
Sourcepub fn is_under_mouse(&self, w_id: &Id) -> bool
pub fn is_under_mouse(&self, w_id: &Id) -> bool
Get whether the widget is under the mouse cursor
Sourcepub fn set_mouse_over_icon(&mut self, icon: CursorIcon)
pub fn set_mouse_over_icon(&mut self, icon: CursorIcon)
Set the cursor icon
This is normally called from Events::handle_mouse_over
. In other
cases, calling this method may be ineffective. The cursor is
automatically “unset” when the widget is no longer under the mouse.
See also EventCx::set_grab_cursor
: if a mouse grab
(PressStart::grab
) is active, its icon takes precedence.
Sourcepub fn set_grab_depress(
&mut self,
source: PressSource,
target: Option<Id>,
) -> bool
pub fn set_grab_depress( &mut self, source: PressSource, target: Option<Id>, ) -> bool
Set a grab’s depress target
When a grab on mouse or touch input is in effect
(PressStart::grab
), the widget owning the grab may set itself
or any other widget as depressed (“pushed down”). Each grab depresses
at most one widget, thus setting a new depress target clears any
existing target. Initially a grab depresses its owner.
This effect is purely visual. A widget is depressed when one or more grabs targets the widget to depress, or when a keyboard binding is used to activate a widget (for the duration of the key-press).
Assumption: this method will only be called by handlers of a grab (i.e.
recipients of Event::PressStart
after initiating a successful grab,
Event::PressMove
or Event::PressEnd
).
Queues a redraw and returns true
if the depress target changes,
otherwise returns false
.
Sourcepub fn any_grab_on(&self, id: &Id) -> bool
pub fn any_grab_on(&self, id: &Id) -> bool
Returns true if there is a mouse or touch grab on id
or any descendant of id
Sourcepub fn press_velocity(&self, source: PressSource) -> Option<Vec2>
pub fn press_velocity(&self, source: PressSource) -> Option<Vec2>
Get velocity of the mouse cursor or a touch
The velocity is calculated at the time this method is called using existing samples of motion.
Where the source
is a mouse this always succeeds.
For touch events this requires an active grab and is not
guaranteed to succeed; currently only a limited number of presses with
mode GrabMode::Grab
are tracked for velocity.
Source§impl EventState
impl EventState
Sourcepub fn send<M: Debug + 'static>(&mut self, id: Id, msg: M)
pub fn send<M: Debug + 'static>(&mut self, id: Id, msg: M)
Send a message to id
When calling this method, be aware that some widgets use an inner
component to handle events, thus calling with the outer widget’s id
may not have the desired effect. Layout::try_probe
and
EventState::next_nav_focus
are usually able to find the appropriate
event-handling target.
This uses a tree traversal as with event handling, thus ancestors will have a chance to handle an unhandled event and any messages on the stack after their child.
§Special cases sent as an Event
When M
is Command
, this will send Event::Command
to the widget.
When M
is ScrollDelta
, this will send Event::Scroll
to the
widget.
§Other messages
The message is pushed to the message stack. The target widget may
pop or peek the message from
Events::handle_messages
.
Sourcepub fn send_erased(&mut self, id: Id, msg: Erased)
pub fn send_erased(&mut self, id: Id, msg: Erased)
Push a type-erased message to the stack
This is a lower-level variant of Self::send
.
Sourcepub fn send_async<Fut, M>(&mut self, id: Id, fut: Fut)where
Fut: IntoFuture<Output = M> + 'static,
M: Debug + 'static,
pub fn send_async<Fut, M>(&mut self, id: Id, fut: Fut)where
Fut: IntoFuture<Output = M> + 'static,
M: Debug + 'static,
Send a message to id
via a Future
The future is polled after event handling and after drawing and is able
to wake the event loop. This future is executed on the main thread; for
high-CPU tasks use Self::send_spawn
instead.
The future must resolve to a message on completion. Its message is then
sent to id
via stack traversal identically to Self::send
.
Sourcepub fn send_async_erased<Fut>(&mut self, id: Id, fut: Fut)where
Fut: IntoFuture<Output = Erased> + 'static,
pub fn send_async_erased<Fut>(&mut self, id: Id, fut: Fut)where
Fut: IntoFuture<Output = Erased> + 'static,
Send a type-erased message to id
via a Future
This is a low-level variant of Self::send_async
.
Sourcepub fn send_spawn<Fut, M>(&mut self, id: Id, fut: Fut)
Available on crate feature spawn
only.
pub fn send_spawn<Fut, M>(&mut self, id: Id, fut: Fut)
spawn
only.Spawn a task, run on a thread pool
The future is spawned to a thread-pool before the event-handling loop
sleeps, and is able to wake the loop on completion. Tasks involving
significant CPU work should use this method over Self::send_async
.
This method is simply a wrapper around async_global_executor::spawn
and Self::send_async
; if a different multi-threaded executor is
available, that may be used instead. See also async_global_executor
documentation of configuration.
Source§impl EventState
impl EventState
Sourcepub fn request_timer(&mut self, id: Id, handle: TimerHandle, delay: Duration)
pub fn request_timer(&mut self, id: Id, handle: TimerHandle, delay: Duration)
Schedule a timed update
Widget updates may be used for delayed action. For animation, prefer to
use Draw::animate
or
Self::request_frame_timer
.
Widget id
will receive Event::Timer
with this handle
at
approximately time = now + delay
(or possibly a little later due to
frame-rate limiters and processing time).
Requesting an update with delay == 0
is valid, except from an
Event::Timer
handler (where it may cause an infinite loop).
Multiple timer requests with the same id
and handle
are merged
(see TimerHandle
documentation).
Sourcepub fn request_frame_timer(&mut self, id: Id, handle: TimerHandle)
pub fn request_frame_timer(&mut self, id: Id, handle: TimerHandle)
Schedule a frame timer update
Widget id
will receive Event::Timer
with this handle
either
before or soon after the next frame is drawn.
This may be useful for animations which mutate widget state. Animations
which don’t mutate widget state may use
Draw::animate
instead.
It is expected that handle.earliest() == true
(style guide).
Source§impl EventState
impl EventState
Source§impl EventState
impl EventState
Sourcepub fn is_disabled(&self, w_id: &Id) -> bool
pub fn is_disabled(&self, w_id: &Id) -> bool
Check whether a widget is disabled
A widget is disabled if any ancestor is.
Sourcepub fn config(&self) -> &WindowConfig
pub fn config(&self) -> &WindowConfig
Access event-handling configuration
Sourcepub fn config_enable_pan(&self, source: PressSource) -> bool
pub fn config_enable_pan(&self, source: PressSource) -> bool
Is mouse panning enabled?
Sourcepub fn config_enable_mouse_text_pan(&self) -> bool
pub fn config_enable_mouse_text_pan(&self) -> bool
Is mouse text panning enabled?
Sourcepub fn config_test_pan_thresh(&self, dist: Offset) -> bool
pub fn config_test_pan_thresh(&self, dist: Offset) -> bool
Test pan threshold against config, adjusted for scale factor
Returns true when dist
is large enough to switch to pan mode.
Sourcepub fn change_config(&mut self, msg: ConfigMsg)
pub fn change_config(&mut self, msg: ConfigMsg)
Update event configuration
Sourcepub fn set_disabled(&mut self, target: Id, disable: bool)
pub fn set_disabled(&mut self, target: Id, disable: bool)
Set/unset a widget as disabled
Disabled status applies to all descendants and blocks reception of
events (Unused
is returned automatically when the
recipient or any ancestor is disabled).
Disabling a widget clears navigation, selection and key focus when the target is disabled, and also cancels press/pan grabs.
Sourcepub fn redraw(&mut self, id: impl HasId)
pub fn redraw(&mut self, id: impl HasId)
Notify that a widget must be redrawn
This is equivalent to calling Self::action
with Action::REDRAW
.
Sourcepub fn resize(&mut self, id: impl HasId)
pub fn resize(&mut self, id: impl HasId)
Notify that a widget must be resized
This is equivalent to calling Self::action
with Action::RESIZE
.
Sourcepub fn region_moved(&mut self)
pub fn region_moved(&mut self)
Notify that widgets under self may have moved
Sourcepub fn action(&mut self, id: impl HasId, action: Action)
pub fn action(&mut self, id: impl HasId, action: Action)
Notify that an Action
should happen
This causes the given action to happen after event handling.
Whenever a widget is added, removed or replaced, a reconfigure action is required. Should a widget’s size requirements change, these will only affect the UI after a reconfigure action.
Sourcepub fn window_action(&mut self, action: Action)
pub fn window_action(&mut self, action: Action)
Notify that an Action
should happen for the whole window
Using Self::action
with a widget id
instead of this method is
potentially more efficient (supports future optimisations), but not
always possible.
Sourcepub fn request_update(&mut self, id: Id)
pub fn request_update(&mut self, id: Id)
Request update to widget id
This will call Events::update
on id
.
Auto Trait Implementations§
impl Freeze for EventState
impl !RefUnwindSafe for EventState
impl !Send for EventState
impl !Sync for EventState
impl Unpin for EventState
impl !UnwindSafe for EventState
Blanket Implementations§
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<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
impl<S, T> CastApprox<T> for Swhere
T: ConvApprox<S>,
Source§fn try_cast_approx(self) -> Result<T, Error>
fn try_cast_approx(self) -> Result<T, Error>
Source§fn cast_approx(self) -> T
fn cast_approx(self) -> T
Source§impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
impl<S, T> CastFloat<T> for Swhere
T: ConvFloat<S>,
Source§fn cast_trunc(self) -> T
fn cast_trunc(self) -> T
Source§fn cast_nearest(self) -> T
fn cast_nearest(self) -> T
Source§fn cast_floor(self) -> T
fn cast_floor(self) -> 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