Skip to main content

kozan_core/events/
event.rs

1//! Event trait and dispatch properties.
2//!
3//! Unlike Chrome where `Event` is a class carrying dispatch state,
4//! in Kozan the event data is separate from dispatch state (`EventContext`).
5//! This lets event structs be simple data carriers — no mutable dispatch fields.
6
7use core::any::Any;
8
9/// Whether an event bubbles up through the tree.
10#[derive(Copy, Clone, Debug, PartialEq, Eq)]
11pub enum Bubbles {
12    Yes,
13    No,
14}
15
16/// Whether an event can be cancelled via `prevent_default()`.
17#[derive(Copy, Clone, Debug, PartialEq, Eq)]
18pub enum Cancelable {
19    Yes,
20    No,
21}
22
23/// Base trait for all events.
24///
25/// Every event type (`MouseEvent`, `KeyboardEvent`, custom events) implements this.
26/// The trait is object-safe — events are dispatched as `&dyn Event`.
27///
28/// # Chrome equivalence
29///
30/// Chrome stores `bubbles_`, `cancelable_`, `type_` on the Event object.
31/// In Kozan, `bubbles()` and `cancelable()` are trait methods (usually const),
32/// and the type is identified via `TypeId` (no string matching).
33pub trait Event: Any + 'static {
34    /// Whether this event bubbles up through the tree after reaching the target.
35    fn bubbles(&self) -> Bubbles;
36
37    /// Whether this event can be cancelled via `prevent_default()`.
38    fn cancelable(&self) -> Cancelable;
39
40    /// Downcast to `&dyn Any` for type-safe casting in handlers.
41    fn as_any(&self) -> &dyn Any;
42}