Skip to main content

HandlerContext

Struct HandlerContext 

Source
pub struct HandlerContext<'a> { /* private fields */ }
Expand description

Context passed to event handlers during dispatch.

HandlerContext enables handlers to interact with the event system:

  • Emit new events with automatic scope tracking
  • Signal render requests
  • Signal quit requests

§Emission Modes

HandlerContext supports two emission modes:

  1. Collect mode (default): Events are collected in a Vec and returned via take_emitted_events() after the handler completes.

  2. Direct mode (with sender): Events are sent directly to a channel via the attached EventSender. Use with_sender() to enable this.

§Scope Inheritance

When emit() is called and the context has an attached scope, the new event inherits the scope. This ensures proper lifecycle tracking for chains of related events.

§Thread Safety

HandlerContext is not Send or Sync - it’s designed to be used within a single handler invocation.

Implementations§

Source§

impl<'a> HandlerContext<'a>

Source

pub const fn new() -> Self

Create a new handler context in collect mode.

Events emitted via emit() are collected and can be retrieved with take_emitted_events() after the handler completes.

Typically called by the event processor before invoking handlers.

Source

pub const fn with_sender(self, sender: &'a EventSender) -> Self

Attach an event sender for direct emission mode.

When a sender is attached, emit() sends events directly to the channel instead of collecting them. This matches the runtime’s expected behavior where emitted events re-enter the event queue.

§Example
let sender = bus.sender().unwrap();
let ctx = HandlerContext::new().with_sender(&sender);
// Now ctx.emit() sends directly via channel
Source

pub fn with_scope(self, scope: Option<EventScope>) -> Self

Attach a scope for lifecycle tracking.

Events emitted via emit() will inherit this scope.

Source

pub const fn scope(&self) -> Option<&EventScope>

Get a reference to the current scope, if any.

Source

pub fn emit<E: Event>(&mut self, event: E)

Emit a new event from within a handler.

Behavior depends on mode:

  • Collect mode (no sender): Event is collected in internal Vec
  • Direct mode (with sender): Event is sent directly to channel

If this context has a scope, the event inherits it for proper lifecycle tracking.

§Scope Tracking

When a scope is present:

  1. scope.increment() is called
  2. Event is wrapped with the scope
  3. After dispatch, scope.decrement() is called by the processor
§Example
ctx.emit(BufferModified { buffer_id: 1 });
Source

pub fn emit_dyn(&mut self, event: DynEvent)

Emit a pre-boxed dynamic event.

Useful when you already have a DynEvent and want to emit it. The event’s existing scope (if any) is preserved.

Source

pub const fn request_render(&mut self)

Request a render update after event processing completes.

This is a hint to the runtime that the UI should be refreshed. The actual render is performed by the runtime, not the kernel.

Source

pub const fn request_quit(&mut self)

Request application quit.

This signals that the handler wants the application to terminate. The runtime decides how to handle this request.

Source

pub const fn render_requested(&self) -> bool

Check if render was requested.

Source

pub const fn quit_requested(&self) -> bool

Check if quit was requested.

Source

pub fn take_emitted_events(&mut self) -> Vec<DynEvent>

Take collected emitted events.

Called by the event processor after handler execution to get the events that need to be dispatched.

Note: In direct mode (with sender), this will always return an empty Vec since events are sent immediately.

Source

pub const fn has_emitted_events(&self) -> bool

Check if any events were emitted (collect mode only).

Source

pub const fn emitted_event_count(&self) -> usize

Get the number of emitted events (collect mode only).

Source

pub const fn has_sender(&self) -> bool

Check if this context is in direct emission mode.

Trait Implementations§

Source§

impl Debug for HandlerContext<'_>

Source§

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

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

impl Default for HandlerContext<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for HandlerContext<'a>

§

impl<'a> !RefUnwindSafe for HandlerContext<'a>

§

impl<'a> Send for HandlerContext<'a>

§

impl<'a> Sync for HandlerContext<'a>

§

impl<'a> Unpin for HandlerContext<'a>

§

impl<'a> UnsafeUnpin for HandlerContext<'a>

§

impl<'a> !UnwindSafe for HandlerContext<'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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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