use std::cell::Cell;
use std::ops::Deref;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum EventType {
Click,
DblClick,
ContextMenu,
AuxClick,
MouseDown,
MouseUp,
MouseMove,
MouseEnter,
MouseLeave,
MouseOver,
MouseOut,
PointerDown,
PointerUp,
PointerMove,
KeyDown,
KeyUp,
Input,
Change,
Submit,
Focus,
Blur,
Wheel,
Scroll,
DragStart,
Drag,
DragEnter,
DragOver,
DragLeave,
Drop,
DragEnd,
TransitionRun,
TransitionStart,
TransitionEnd,
TransitionCancel,
AnimationStart,
AnimationIteration,
AnimationEnd,
AnimationCancel,
Resize,
Custom(&'static str),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventPhase {
None,
Capturing,
AtTarget,
Bubbling,
}
#[derive(Clone, Debug)]
pub struct Event {
pub event_type: EventType,
pub phase: Cell<EventPhase>,
pub target_id: Option<u32>,
pub current_target_id: Cell<Option<u32>>,
pub propagation_stopped: std::rc::Rc<Cell<bool>>,
pub default_prevented: std::rc::Rc<Cell<bool>>,
pub value: String,
pub data: Option<String>,
pub input_type: String,
pub client_x: f64,
pub client_y: f64,
pub screen_x: f64,
pub screen_y: f64,
pub page_x: f64,
pub page_y: f64,
pub offset_x: f64,
pub offset_y: f64,
pub movement_x: f64,
pub movement_y: f64,
pub button: i16,
pub buttons: u16,
pub detail: i32,
pub pointer_id: i32,
pub pointer_capture_requested: std::rc::Rc<Cell<Option<bool>>>,
pub cursor_warp_requested: std::rc::Rc<Cell<Option<(f64, f64)>>>,
pub width: f64,
pub height: f64,
pub scroll_top: f64,
pub scroll_left: f64,
pub scroll_height: f64,
pub scroll_width: f64,
pub client_height: f64,
pub client_width: f64,
pub files: Vec<std::path::PathBuf>,
pub data_transfer: std::rc::Rc<std::cell::RefCell<std::collections::HashMap<String, String>>>,
pub key: String,
pub code: String,
pub repeat: bool,
pub location: u32,
pub ctrl_key: bool,
pub shift_key: bool,
pub alt_key: bool,
pub meta_key: bool,
pub delta_x: f64,
pub delta_y: f64,
pub delta_z: f64,
pub delta_mode: u32,
pub timestamp: f64,
pub property_name: String,
pub animation_name: String,
pub elapsed_time: f32,
}
impl Default for Event {
fn default() -> Self {
Self {
event_type: EventType::Click,
phase: Cell::new(EventPhase::None),
target_id: None,
current_target_id: Cell::new(None),
propagation_stopped: std::rc::Rc::new(Cell::new(false)),
default_prevented: std::rc::Rc::new(Cell::new(false)),
value: String::new(),
data: None,
input_type: String::new(),
client_x: 0.0,
client_y: 0.0,
screen_x: 0.0,
screen_y: 0.0,
page_x: 0.0,
page_y: 0.0,
offset_x: 0.0,
offset_y: 0.0,
movement_x: 0.0,
movement_y: 0.0,
button: 0,
buttons: 0,
detail: 0,
pointer_id: 1,
pointer_capture_requested: std::rc::Rc::new(Cell::new(None)),
cursor_warp_requested: std::rc::Rc::new(Cell::new(None)),
width: 0.0,
height: 0.0,
scroll_top: 0.0,
scroll_left: 0.0,
scroll_height: 0.0,
scroll_width: 0.0,
client_height: 0.0,
client_width: 0.0,
files: Vec::new(),
data_transfer: std::rc::Rc::new(std::cell::RefCell::new(
std::collections::HashMap::new(),
)),
key: String::new(),
code: String::new(),
repeat: false,
location: 0,
ctrl_key: false,
shift_key: false,
alt_key: false,
meta_key: false,
delta_x: 0.0,
delta_y: 0.0,
delta_z: 0.0,
delta_mode: 0,
timestamp: 0.0,
property_name: String::new(),
animation_name: String::new(),
elapsed_time: 0.0,
}
}
}
impl Event {
pub fn new(event_type: EventType) -> Self {
Self {
event_type,
..Default::default()
}
}
pub fn set_data(&self, format: impl Into<String>, data: impl Into<String>) {
self.data_transfer
.borrow_mut()
.insert(format.into(), data.into());
}
pub fn get_data(&self, format: &str) -> Option<String> {
self.data_transfer.borrow().get(format).cloned()
}
pub fn set_pointer_capture(&self, _pointer_id: i32) {
self.pointer_capture_requested.set(Some(true));
}
pub fn release_pointer_capture(&self, _pointer_id: i32) {
self.pointer_capture_requested.set(Some(false));
}
pub fn set_cursor_position(&self, x: f64, y: f64) {
self.cursor_warp_requested.set(Some((x, y)));
}
pub fn warp_cursor(&self, x: f64, y: f64) {
self.set_cursor_position(x, y);
}
pub fn with_value(event_type: EventType, val: impl Into<String>) -> Self {
Self {
event_type,
value: val.into(),
..Default::default()
}
}
pub fn transition(
event_type: EventType,
property: impl Into<String>,
elapsed_time: f32,
) -> Self {
Self {
event_type,
property_name: property.into(),
elapsed_time,
..Default::default()
}
}
pub fn animation(
event_type: EventType,
animation_name: impl Into<String>,
elapsed_time: f32,
) -> Self {
Self {
event_type,
animation_name: animation_name.into(),
elapsed_time,
..Default::default()
}
}
pub fn stop_propagation(&self) {
self.propagation_stopped.set(true);
}
pub fn prevent_default(&self) {
self.default_prevented.set(true);
}
pub fn is_propagation_stopped(&self) -> bool {
self.propagation_stopped.get()
}
pub fn is_default_prevented(&self) -> bool {
self.default_prevented.get()
}
pub fn phase(&self) -> EventPhase {
self.phase.get()
}
pub fn current_target(&self) -> Option<u32> {
self.current_target_id.get()
}
pub fn is_primary(&self) -> bool {
self.button == 0
}
pub fn is_auxiliary(&self) -> bool {
self.button == 1
}
pub fn is_secondary(&self) -> bool {
self.button == 2
}
pub fn has_modifier(&self) -> bool {
self.ctrl_key || self.shift_key || self.alt_key || self.meta_key
}
}
impl Deref for Event {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.value
}
}
pub struct EventDispatcher;
impl EventDispatcher {
pub fn dispatch<F>(ancestry_path: &[u32], event: &Event, mut listener_invoker: F)
where
F: FnMut(u32, EventPhase, &Event),
{
if ancestry_path.is_empty() {
return;
}
let target_node = *ancestry_path.last().unwrap();
event.phase.set(EventPhase::Capturing);
if ancestry_path.len() > 1 {
for &node_id in &ancestry_path[..ancestry_path.len() - 1] {
if event.is_propagation_stopped() {
return;
}
event.current_target_id.set(Some(node_id));
listener_invoker(node_id, EventPhase::Capturing, event);
}
}
if event.is_propagation_stopped() {
return;
}
event.phase.set(EventPhase::AtTarget);
event.current_target_id.set(Some(target_node));
listener_invoker(target_node, EventPhase::AtTarget, event);
if event.is_propagation_stopped() {
return;
}
event.phase.set(EventPhase::Bubbling);
if ancestry_path.len() > 1 {
for &node_id in ancestry_path[..ancestry_path.len() - 1].iter().rev() {
if event.is_propagation_stopped() {
return;
}
event.current_target_id.set(Some(node_id));
listener_invoker(node_id, EventPhase::Bubbling, event);
}
}
}
}