Event

Struct Event 

Source
pub struct Event<A>(/* private fields */)
where
    A: EventArgs;
Expand description

Represents an event variable.

Implementations§

Source§

impl<A> Event<A>
where A: EventArgs,

Source

pub fn var(&self) -> Var<EventUpdates<A>>

Variable that tracks all the args notified in the last update cycle.

Note that the event variable is only cleared when new notifications are requested.

Source

pub fn var_latest(&self) -> Var<Option<A>>

Variable that tracks the latest update.

Is only None if this event has never notified yet.

Source

pub fn var_map<O>( &self, filter_map: impl FnMut(&A) -> Option<O> + Send + 'static, fallback_init: impl Fn() -> O + Send + 'static, ) -> Var<O>
where O: VarValue,

Filter map the latest args.

The variable tracks the latest args that passes the filter_map. Every event update calls the closure for each pending args, latest first, and stops on the first args that produces a new value.

Source

pub fn var_bind<O>( &self, other: &Var<O>, filter_map: impl FnMut(&A) -> Option<O> + Send + 'static, ) -> VarHandle
where O: VarValue,

Bind filter the latest args to the variable.

The other variable will be updated with the latest args that passes the filter_map. Every event update calls the closure for each pending args, latest first, and stops on the first args that produces a new value.

Source

pub fn notify(&self, args: A)

Modify the event variable to include the args in the next update.

Source

pub fn each_update(&self, ignore_propagation: bool, handler: impl FnMut(&A))

Visit each new update, oldest first, that target the context widget.

If not called inside an widget visits all updates.

If ignore_propagation is false only calls the handler if the propagation is not stopped.

Source

pub fn find_update<O>( &self, ignore_propagation: bool, handler: impl FnMut(&A) -> Option<O>, ) -> Option<O>

Visit each_update, returns on the first args that produces an O.

Source

pub fn any_update( &self, ignore_propagation: bool, handler: impl FnMut(&A) -> bool, ) -> bool

Visit each_update, returns on the first args that produces true.

Source

pub fn latest_update<O>( &self, ignore_propagation: bool, handler: impl FnOnce(&A) -> O, ) -> Option<O>

Visit the latest update that targets the context widget.

If not called inside an widget visits the latest in general.

If ignore_propagation is false only calls the handler if the propagation is not stopped.

Source

pub fn has_update(&self, ignore_propagation: bool) -> bool

If has at least one update for the context widget.

If ignore_propagation is false only returns true if any propagation is not stopped.

Source

pub fn subscribe(&self, op: UpdateOp, widget_id: WidgetId) -> VarHandle

Subscribe the widget to receive updates when events are relevant to it.

Source

pub fn subscribe_when( &self, op: UpdateOp, widget_id: WidgetId, predicate: impl Fn(&A) -> bool + Send + Sync + 'static, ) -> VarHandle

Subscribe the widget to receive updates when events are relevant to it and the latest args passes the predicate.

Source

pub fn on_pre_event( &self, ignore_propagation: bool, handler: Box<dyn FnMut(&A) -> HandlerResult + Send>, ) -> VarHandle

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.

If ignore_propagation is set also call handlers for args with stopped propagation.

Returns a VarHandle that can be dropped to unsubscribe, you can also unsubscribe from inside the handler by calling unsubscribe.

§Examples
let handle = FOCUS_CHANGED_EVENT.on_pre_event(
    false,
    hn!(|args| {
        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 Handler<A>, there are multiple flavors of handlers, including async handlers that allow calling .await. The handler closures can be declared using hn!, async_hn!, hn_once! and async_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, ignore_propagation: bool, handler: Box<dyn FnMut(&A) -> HandlerResult + Send>, ) -> VarHandle

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.

If ignore_propagation is set also call handlers for args with stopped propagation.

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

§Examples
let handle = FOCUS_CHANGED_EVENT.on_event(
    false,
    hn!(|args| {
        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 Handler<A>, there are multiple flavors of handlers, including async handlers that allow calling .await. The handler closures can be declared using hn!, async_hn!, hn_once! and async_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) -> Receiver<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 as_any(&self) -> &AnyEvent

Deref as AnyEvent.

Source

pub fn hook( &self, handler: impl FnMut(&A) -> bool + Send + 'static, ) -> VarHandle

Setups a callback for just after the event notifications are listed, the closure runs in the root app context, just like var modify and hook closures.

The closure must return true to be retained and false to be dropped.

Any event notification or var modification done in the handler will apply on the same update that notifies this event.

Source

pub async fn wait_match( &self, predicate: impl Fn(&A) -> bool + Send + Sync + 'static, )

Wait until any args, current or new passes the predicate.

Source

pub fn with<R>(&self, visitor: impl FnOnce(&EventUpdates<A>) -> R) -> R

Visit the args current value of var.

Methods from Deref<Target = AnyEvent>§

Source

pub fn subscribe(&self, op: UpdateOp, widget_id: WidgetId) -> VarHandle

Subscribe the widget to receive updates when events are relevant to it.

Source

pub fn var(&self) -> AnyVar

Variable that tracks all the args notified in the last update cycle.

Note that the event variable is only cleared when new notifications are requested.

Source

pub fn hook( &self, handler: impl FnMut(&(dyn AnyEventArgs + 'static)) -> bool + Send + 'static, ) -> VarHandle

Setups a callback for just after the event notifications are listed, the closure runs in the root app context, just like var modify and hook closures.

The closure must return true to be retained and false to be dropped.

Any event notification or var modification done in the handler will apply on the same update that notifies this event.

Trait Implementations§

Source§

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

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>
where A: EventArgs,

Source§

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

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

impl<A> Deref for Event<A>
where A: EventArgs,

Source§

type Target = AnyEvent

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Event<A> as Deref>::Target

Dereferences the value.
Source§

impl<A> Hash for Event<A>
where A: EventArgs,

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<A> PartialEq for Event<A>
where A: EventArgs,

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> Copy for Event<A>
where A: EventArgs,

Source§

impl<A> Eq for Event<A>
where A: EventArgs,

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
§

impl<T> AnyEq for T
where T: Any + PartialEq,

§

fn equals(&self, other: &(dyn Any + 'static)) -> bool

§

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

Source§

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

Source§

fn clone_boxed(&self) -> BoxAnyVarValue

Clone the value.
Source§

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

Gets if self and other are equal.
Source§

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

Value type name. Read more
Source§

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

Swap value with other if both are of the same type.
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
§

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

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

§

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

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<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

Source§

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

Source§

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

Access any.
§

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
§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

§

fn into_sample(self) -> T

Source§

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

Source§

fn into_var(self) -> Var<T>

§

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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
§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

§

fn to_sample_(self) -> U

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
§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

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