pub trait EventTarget: HasHandle {
// Provided methods
fn on<E: Event>(
&self,
callback: impl FnMut(&E, &EventContext) + 'static,
) -> ListenerId { ... }
fn on_with_options<E: Event>(
&self,
callback: impl FnMut(&E, &EventContext) + 'static,
options: ListenerOptions,
) -> ListenerId { ... }
fn on_capture<E: Event>(
&self,
callback: impl FnMut(&E, &EventContext) + 'static,
) -> ListenerId { ... }
fn on_once<E: Event>(
&self,
callback: impl FnMut(&E, &EventContext) + 'static,
) -> ListenerId { ... }
fn off(&self, id: ListenerId) { ... }
fn dispatch_event(&self, event: &dyn Event) -> bool { ... }
}Expand description
Base trait for anything that can receive events.
Like Chrome’s EventTarget class. Both DOM nodes and Window implement this.
§Methods
| Method | Description |
|---|---|
on() | Add a typed event listener |
on_capture() | Add a capture-phase listener |
on_once() | Add a one-shot listener (auto-removed) |
on_with_options() | Add with explicit options |
off() | Remove a listener by ID |
dispatch_event() | Dispatch an event (capture → target → bubble) |
§Example
// Add a click listener to a button.
btn.on::<ClickEvent>(|event, ctx| {
println!("Button clicked!");
});
// Dispatch an event.
btn.dispatch_event(&ClickEvent);Provided Methods§
Sourcefn on<E: Event>(
&self,
callback: impl FnMut(&E, &EventContext) + 'static,
) -> ListenerId
fn on<E: Event>( &self, callback: impl FnMut(&E, &EventContext) + 'static, ) -> ListenerId
Add a typed event listener. Returns an ID for removal.
The callback receives &E (the concrete event) and &EventContext
(dispatch state: phase, target, propagation control).
Sourcefn on_with_options<E: Event>(
&self,
callback: impl FnMut(&E, &EventContext) + 'static,
options: ListenerOptions,
) -> ListenerId
fn on_with_options<E: Event>( &self, callback: impl FnMut(&E, &EventContext) + 'static, options: ListenerOptions, ) -> ListenerId
Add a listener with explicit options.
Options: capture (fire during capture phase), passive (no preventDefault),
once (auto-remove after first call).
Sourcefn on_capture<E: Event>(
&self,
callback: impl FnMut(&E, &EventContext) + 'static,
) -> ListenerId
fn on_capture<E: Event>( &self, callback: impl FnMut(&E, &EventContext) + 'static, ) -> ListenerId
Add a capture-phase listener.
Fires during the capture phase (root → target) instead of bubble phase.
Sourcefn on_once<E: Event>(
&self,
callback: impl FnMut(&E, &EventContext) + 'static,
) -> ListenerId
fn on_once<E: Event>( &self, callback: impl FnMut(&E, &EventContext) + 'static, ) -> ListenerId
Add a one-shot listener that auto-removes after first call.
Sourcefn off(&self, id: ListenerId)
fn off(&self, id: ListenerId)
Remove a listener by its ID.
Sourcefn dispatch_event(&self, event: &dyn Event) -> bool
fn dispatch_event(&self, event: &dyn Event) -> bool
Dispatch an event to this target.
Runs the full capture → target → bubble pipeline.
Returns true if the default action was NOT prevented.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.