use crate::EventAttribFn;
use std::cell::{Cell, RefCell};
use std::fmt::{Debug, Formatter};
use std::ops::Deref;
use std::rc::Rc;
#[derive(Clone)]
pub enum EventHandler {
NoArgs(Rc<RefCell<dyn FnMut()>>),
MouseEvent(Rc<RefCell<dyn FnMut(MouseEvent)>>),
UnsupportedSignature(EventAttribFn),
}
#[derive(Clone)]
pub struct MouseEvent {
event: web_sys::MouseEvent,
should_propagate: Rc<Cell<bool>>,
}
impl MouseEvent {
pub fn new(event: web_sys::MouseEvent) -> Self {
MouseEvent {
event,
should_propagate: Rc::new(Cell::new(true)),
}
}
pub fn stop_propagation(&self) {
self.should_propagate.set(false);
self.event.stop_propagation();
}
pub fn should_propagate(&self) -> &Rc<Cell<bool>> {
&self.should_propagate
}
}
impl Deref for MouseEvent {
type Target = web_sys::MouseEvent;
fn deref(&self) -> &Self::Target {
&self.event
}
}
impl PartialEq for EventHandler {
fn eq(&self, _other: &Self) -> bool {
true
}
}
impl Debug for EventHandler {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("event handler")
}
}