kozan_core/input/mod.rs
1//! Input event types — the engine's public input API.
2//!
3//! Like Chrome's `blink/public/common/input/` — these types define the
4//! contract between the platform layer and the engine. The platform converts
5//! OS events (winit) into these types, then passes them to `FrameWidget`.
6//!
7//! # Architecture
8//!
9//! ```text
10//! Layer 0: winit events (OS abstraction)
11//! Layer 1: input::InputEvent (THIS MODULE — engine's public API)
12//! Crosses the main→view thread boundary.
13//! Self-contained data, no pointers, no handles.
14//! Layer 2: DOM Event (events/ module — Event, MouseEvent, KeyboardEvent)
15//! Created by EventHandler from InputEvent.
16//! Spec-compliant, dispatched through the DOM tree.
17//! ```
18//!
19//! # Why these types live in the engine (not the platform)
20//!
21//! Chrome defines `WebInputEvent` in blink (the engine), not in content (the
22//! platform). This prevents circular dependencies: the engine defines the
23//! types, the platform produces them, and the engine consumes them.
24
25pub(crate) mod default_action;
26pub mod keyboard;
27pub mod modifiers;
28pub mod mouse;
29pub mod wheel;
30
31// Re-export all types at the `input` level.
32pub use keyboard::{KeyCode, KeyboardEvent};
33pub use modifiers::Modifiers;
34pub use mouse::{
35 ButtonState, MouseButton, MouseButtonEvent, MouseEnterEvent, MouseLeaveEvent, MouseMoveEvent,
36};
37pub use wheel::{WheelDelta, WheelEvent};
38
39/// An input event — the engine's entry point for user interaction.
40///
41/// Chrome equivalent: `WebInputEvent` with a `Type` enum.
42/// In Rust, an enum of dedicated structs replaces a class hierarchy.
43#[derive(Debug, Clone)]
44pub enum InputEvent {
45 /// Mouse cursor moved within the view.
46 MouseMove(MouseMoveEvent),
47 /// Mouse button pressed or released.
48 MouseButton(MouseButtonEvent),
49 /// Mouse cursor entered the view.
50 MouseEnter(MouseEnterEvent),
51 /// Mouse cursor left the view.
52 MouseLeave(MouseLeaveEvent),
53 /// Mouse wheel or trackpad scroll.
54 Wheel(WheelEvent),
55 /// Keyboard key pressed or released.
56 Keyboard(KeyboardEvent),
57}
58
59#[cfg(test)]
60mod tests {
61 use super::*;
62
63 #[test]
64 fn input_event_is_send() {
65 fn assert_send<T: Send>() {}
66 assert_send::<InputEvent>();
67 }
68
69 #[test]
70 fn input_event_variants() {
71 let evt = InputEvent::MouseMove(MouseMoveEvent {
72 x: 10.0,
73 y: 20.0,
74 modifiers: Modifiers::EMPTY,
75 timestamp: std::time::Instant::now(),
76 });
77 assert!(matches!(evt, InputEvent::MouseMove(_)));
78
79 let evt = InputEvent::Keyboard(KeyboardEvent {
80 key: KeyCode::Enter,
81 state: ButtonState::Pressed,
82 modifiers: Modifiers::EMPTY,
83 text: None,
84 timestamp: std::time::Instant::now(),
85 });
86 assert!(matches!(evt, InputEvent::Keyboard(_)));
87 }
88}