zng_app::event

Struct Event

source
pub struct Event<A: EventArgs> { /* private fields */ }
Expand description

Represents an event.

Use the event! macro to declare events.

Implementations§

source§

impl<A: EventArgs> Event<A>

source

pub fn as_any(&self) -> AnyEvent

Gets the event without the args type.

source

pub fn subscribe(&self, widget_id: WidgetId) -> EventHandle

Register the widget to receive targeted events from this event.

Widgets only receive events if they are in the delivery list generated by the event args and are subscribers to the event. App extensions receive all events.

source

pub fn is_subscriber(&self, widget_id: WidgetId) -> bool

Returns true if the widget is subscribed to this event.

source

pub fn has_subscribers(&self) -> bool

Returns true if at least one widget is subscribed to this event.

source

pub fn visit_subscribers<T>( &self, visit: impl FnMut(WidgetId) -> ControlFlow<T>, ) -> Option<T>

Calls visit for each widget subscribed to this event.

Note that trying to subscribe or add hook inside visit will deadlock. Inside visit you can notify the event and generate event updates.

source

pub fn name(&self) -> &'static str

Name of the event static item.

source

pub fn has(&self, update: &EventUpdate) -> bool

Returns true if the update is for this event.

source

pub fn on<'a>(&self, update: &'a EventUpdate) -> Option<&'a A>

Get the event update args if the update is for this event.

source

pub fn on_unhandled<'a>(&self, update: &'a EventUpdate) -> Option<&'a A>

Get the event update args if the update is for this event and propagation is not stopped.

source

pub fn handle<R>( &self, update: &EventUpdate, handler: impl FnOnce(&A) -> R, ) -> Option<R>

Calls handler if the update is for this event and propagation is not stopped, after the handler is called propagation is stopped.

source

pub fn new_update(&self, args: A) -> EventUpdate

Create an event update for this event with delivery list filtered by the event subscribers.

source

pub fn new_update_custom( &self, args: A, delivery_list: UpdateDeliveryList, ) -> EventUpdate

Create and event update for this event with a custom delivery list.

source

pub fn notify(&self, args: A)

Schedule an event update.

source

pub fn on_pre_event<H>(&self, handler: H) -> EventHandle
where H: AppHandler<A>,

Creates a preview event handler.

The event handler is called for every update that has not stopped propagation. The handler is called before widget handlers and on_event handlers. The handler is called after all previous registered preview handlers.

Returns an EventHandle that can be dropped to unsubscribe, you can also unsubscribe from inside the handler by calling unsubscribe in the third parameter of app_hn! or async_app_hn!.

§Examples
let handle = FOCUS_CHANGED_EVENT.on_pre_event(app_hn!(|args: &FocusChangedArgs, _| {
    println!("focused: {:?}", args.new_focus);
}));

The example listens to all FOCUS_CHANGED_EVENT events, independent of widget context and before all UI handlers.

§Handlers

the event handler can be any type that implements AppHandler, there are multiple flavors of handlers, including async handlers that allow calling .await. The handler closures can be declared using app_hn!, async_app_hn!, app_hn_once! and async_app_hn_once!.

§Async

Note that for async handlers only the code before the first .await is called in the preview moment, code after runs in subsequent event updates, after the event has already propagated, so stopping propagation only causes the desired effect before the first .await.

source

pub fn on_event(&self, handler: impl AppHandler<A>) -> EventHandle

Creates an event handler.

The event handler is called for every update that has not stopped propagation. The handler is called after all on_pre_event all widget handlers and all on_event handlers registered before this one.

Returns an EventHandle that can be dropped to unsubscribe, you can also unsubscribe from inside the handler by calling unsubscribe in the third parameter of app_hn! or async_app_hn!.

§Examples
let handle = FOCUS_CHANGED_EVENT.on_event(app_hn!(|args: &FocusChangedArgs, _| {
    println!("focused: {:?}", args.new_focus);
}));

The example listens to all FOCUS_CHANGED_EVENT events, independent of widget context, after the UI was notified.

§Handlers

the event handler can be any type that implements AppHandler, there are multiple flavors of handlers, including async handlers that allow calling .await. The handler closures can be declared using app_hn!, async_app_hn!, app_hn_once! and async_app_hn_once!.

§Async

Note that for async handlers only the code before the first .await is called in the preview moment, code after runs in subsequent event updates, after the event has already propagated, so stopping propagation only causes the desired effect before the first .await.

source

pub fn receiver(&self) -> EventReceiver<A>
where A: Send,

Creates a receiver channel for the event. The event updates are send on hook, before even preview handlers. The receiver is unbounded, it will fill indefinitely if not drained. The receiver can be used in any thread, including non-app threads.

Drop the receiver to stop listening.

source

pub fn sender(&self) -> EventSender<A>
where A: Send,

Creates a sender channel that can notify the event.

Events can notify from any app thread, this sender can notify other threads too.

source

pub fn has_hooks(&self) -> bool

Returns true if any app level callback is registered for this event.

This includes AnyEvent::hook, Event::on_pre_event, Event::on_event and Event::receiver.

Trait Implementations§

source§

impl<A: EventArgs> Clone for Event<A>

source§

fn clone(&self) -> Self

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<A: EventArgs> Debug for Event<A>

source§

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

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

impl<A: EventArgs> PartialEq<AnyEvent> for Event<A>

source§

fn eq(&self, other: &AnyEvent) -> 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: EventArgs> PartialEq<Event<A>> for AnyEvent

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: EventArgs> PartialEq for Event<A>

source§

fn eq(&self, other: &Self) -> 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: EventArgs> Copy for Event<A>

source§

impl<A: EventArgs> Eq 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> AnyVarValue for T
where T: VarValue,

source§

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

Access to dyn Any methods.
source§

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

Access to mut dyn Any methods.
source§

fn clone_boxed(&self) -> Box<dyn AnyVarValue>

Clone the value.
source§

fn clone_boxed_var(&self) -> Box<dyn AnyVar>

Clone the value into a new boxed LocalVar<Self>.
source§

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

Access to Box<dyn Any> methods.
source§

fn eq_any(&self, other: &(dyn AnyVarValue + 'static)) -> bool

Gets if self equals other.
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, dst: *mut T)

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

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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.

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> IntoVar<T> for T
where T: VarValue,

source§

type Var = LocalVar<T>

Variable type that will wrap the T value. Read more
source§

fn into_var(self) -> <T as IntoVar<T>>::Var

Converts the source value into a var.
source§

fn into_boxed_var(self) -> Box<dyn VarBoxed<T>>
where Self: Sized,

Converts into BoxedVar<T>. Read more
§

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,

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

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

impl<T> IntoValue<T> for T
where T: VarValue,

source§

impl<T> StateValue for T
where T: Any + Send + Sync,

source§

impl<T> VarValue for T
where T: Debug + Clone + PartialEq + Any + Send + Sync,