use std::rc::Rc;
use crate::{JsJson, dev::JsJsonListDecoder, struct_mut::ValueMut};
#[derive(Clone, Debug, Default)]
pub struct ClickEvent {
pub alt_key: bool,
pub ctrl_key: bool,
pub shift_key: bool,
pub meta_key: bool,
inner: Rc<ValueMut<ClickEventInner>>,
}
#[derive(Clone, Debug, Default)]
pub struct ClickEventInner {
stop_propagation: bool,
prevent_default: bool,
}
impl ClickEvent {
pub fn stop_propagation(&self) {
self.inner.change(|inner| inner.stop_propagation = true);
}
pub fn prevent_default(&self) {
self.inner.change(|inner| inner.prevent_default = true);
}
pub(crate) fn from_driver(data: JsJson) -> Self {
let keys = data.map_list(|mut params: JsJsonListDecoder| {
let alt_key = params.get_bool("altKey")?;
let ctrl_key = params.get_bool("ctrlKey")?;
let shift_key = params.get_bool("shiftKey")?;
let meta_key = params.get_bool("metaKey")?;
params.expect_no_more()?;
Ok((alt_key, ctrl_key, shift_key, meta_key))
});
let (alt_key, ctrl_key, shift_key, meta_key) = keys.unwrap_or_default();
Self {
alt_key,
ctrl_key,
shift_key,
meta_key,
inner: Rc::default(),
}
}
}
impl From<ClickEvent> for JsJson {
fn from(val: ClickEvent) -> JsJson {
let inner = val.inner.get();
JsJson::Object(
[
(
"stop_propagation".to_string(),
JsJson::from(inner.stop_propagation),
),
(
"prevent_default".to_string(),
JsJson::from(inner.prevent_default),
),
]
.into_iter()
.collect(),
)
}
}