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:
-
Collect mode (default): Events are collected in a Vec and returned via
take_emitted_events()after the handler completes. -
Direct mode (with sender): Events are sent directly to a channel via the attached
EventSender. Usewith_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>
impl<'a> HandlerContext<'a>
Sourcepub const fn new() -> Self
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.
Sourcepub const fn with_sender(self, sender: &'a EventSender) -> Self
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 channelSourcepub fn with_scope(self, scope: Option<EventScope>) -> Self
pub fn with_scope(self, scope: Option<EventScope>) -> Self
Attach a scope for lifecycle tracking.
Events emitted via emit() will inherit this scope.
Sourcepub const fn scope(&self) -> Option<&EventScope>
pub const fn scope(&self) -> Option<&EventScope>
Get a reference to the current scope, if any.
Sourcepub fn emit<E: Event>(&mut self, event: E)
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:
scope.increment()is called- Event is wrapped with the scope
- After dispatch,
scope.decrement()is called by the processor
§Example
ctx.emit(BufferModified { buffer_id: 1 });Sourcepub fn emit_dyn(&mut self, event: DynEvent)
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.
Sourcepub const fn request_render(&mut self)
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.
Sourcepub const fn request_quit(&mut self)
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.
Sourcepub const fn render_requested(&self) -> bool
pub const fn render_requested(&self) -> bool
Check if render was requested.
Sourcepub const fn quit_requested(&self) -> bool
pub const fn quit_requested(&self) -> bool
Check if quit was requested.
Sourcepub fn take_emitted_events(&mut self) -> Vec<DynEvent>
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.
Sourcepub const fn has_emitted_events(&self) -> bool
pub const fn has_emitted_events(&self) -> bool
Check if any events were emitted (collect mode only).
Sourcepub const fn emitted_event_count(&self) -> usize
pub const fn emitted_event_count(&self) -> usize
Get the number of emitted events (collect mode only).
Sourcepub const fn has_sender(&self) -> bool
pub const fn has_sender(&self) -> bool
Check if this context is in direct emission mode.