dioxus/packages/desktop/src/events.rs

195 lines
6.5 KiB
Rust
Raw Normal View History

//! Convert a serialized event to an event Trigger
//!
2021-11-29 16:10:40 +00:00
use std::any::Any;
2021-10-01 06:07:12 +00:00
use std::sync::Arc;
2021-11-29 16:10:40 +00:00
use dioxus_core::{ElementId, EventPriority, UserEvent};
2021-11-07 03:11:17 +00:00
use dioxus_html::on::*;
#[derive(serde::Serialize, serde::Deserialize)]
struct ImEvent {
event: String,
mounted_dom_id: u64,
2021-11-12 04:55:57 +00:00
// scope: u64,
2021-10-04 06:54:20 +00:00
contents: serde_json::Value,
}
2021-10-04 06:54:20 +00:00
pub fn trigger_from_serialized(val: serde_json::Value) -> UserEvent {
2021-11-29 16:10:40 +00:00
let ims: Vec<ImEvent> = serde_json::from_value(val).unwrap();
2021-10-04 06:54:20 +00:00
let ImEvent {
event,
mounted_dom_id,
contents,
} = ims.into_iter().next().unwrap();
2021-11-12 04:55:57 +00:00
// let scope_id = ScopeId(scope as usize);
2021-10-04 06:54:20 +00:00
let mounted_dom_id = Some(ElementId(mounted_dom_id as usize));
2021-10-04 06:54:20 +00:00
let name = event_name_from_typ(&event);
let event = make_synthetic_event(&event, contents);
2021-10-04 05:28:04 +00:00
UserEvent {
2021-10-04 06:54:20 +00:00
name,
2021-11-07 03:23:56 +00:00
priority: EventPriority::Low,
2021-11-12 04:55:57 +00:00
scope_id: None,
2021-11-15 14:49:01 +00:00
element: mounted_dom_id,
data: event,
2021-08-27 02:05:09 +00:00
}
}
2021-11-12 03:18:16 +00:00
fn make_synthetic_event(name: &str, val: serde_json::Value) -> Arc<dyn Any + Send + Sync> {
2021-10-04 06:54:20 +00:00
match name {
2021-10-14 16:46:50 +00:00
"copy" | "cut" | "paste" => {
//
2022-01-03 07:06:42 +00:00
Arc::new(ClipboardData {})
2021-10-14 16:46:50 +00:00
}
2021-10-04 06:54:20 +00:00
"compositionend" | "compositionstart" | "compositionupdate" => {
2022-01-03 07:06:42 +00:00
Arc::new(serde_json::from_value::<CompositionData>(val).unwrap())
2021-10-14 16:46:50 +00:00
}
"keydown" | "keypress" | "keyup" => {
2022-01-03 07:06:42 +00:00
let evt = serde_json::from_value::<KeyboardData>(val).unwrap();
2021-11-12 03:18:16 +00:00
Arc::new(evt)
2021-10-04 06:54:20 +00:00
}
"focus" | "blur" | "focusout" => {
2021-10-14 16:46:50 +00:00
//
2022-01-03 07:06:42 +00:00
Arc::new(FocusData {})
2021-10-04 06:54:20 +00:00
}
// todo: these handlers might get really slow if the input box gets large and allocation pressure is heavy
// don't have a good solution with the serialized event problem
2021-10-14 16:46:50 +00:00
"change" | "input" | "invalid" | "reset" | "submit" => {
2022-01-03 07:06:42 +00:00
Arc::new(serde_json::from_value::<FormData>(val).unwrap())
2021-10-14 16:46:50 +00:00
}
2021-10-04 06:54:20 +00:00
"click" | "contextmenu" | "doubleclick" | "drag" | "dragend" | "dragenter" | "dragexit"
| "dragleave" | "dragover" | "dragstart" | "drop" | "mousedown" | "mouseenter"
| "mouseleave" | "mousemove" | "mouseout" | "mouseover" | "mouseup" => {
2022-01-03 07:06:42 +00:00
Arc::new(serde_json::from_value::<MouseData>(val).unwrap())
2021-10-04 06:54:20 +00:00
}
"pointerdown" | "pointermove" | "pointerup" | "pointercancel" | "gotpointercapture"
| "lostpointercapture" | "pointerenter" | "pointerleave" | "pointerover" | "pointerout" => {
2022-01-03 07:06:42 +00:00
Arc::new(serde_json::from_value::<PointerData>(val).unwrap())
2021-10-14 16:46:50 +00:00
}
"select" => {
//
2022-01-03 07:06:42 +00:00
Arc::new(serde_json::from_value::<SelectionData>(val).unwrap())
2021-10-04 06:54:20 +00:00
}
2021-10-14 16:46:50 +00:00
"touchcancel" | "touchend" | "touchmove" | "touchstart" => {
2022-01-03 07:06:42 +00:00
Arc::new(serde_json::from_value::<TouchData>(val).unwrap())
2021-10-14 16:46:50 +00:00
}
2021-10-04 06:54:20 +00:00
2021-11-12 03:18:16 +00:00
"scroll" => Arc::new(()),
2021-10-04 06:54:20 +00:00
2022-01-03 07:06:42 +00:00
"wheel" => Arc::new(serde_json::from_value::<WheelData>(val).unwrap()),
2021-10-04 06:54:20 +00:00
2021-10-14 16:46:50 +00:00
"animationstart" | "animationend" | "animationiteration" => {
2022-01-03 07:06:42 +00:00
Arc::new(serde_json::from_value::<AnimationData>(val).unwrap())
2021-10-14 16:46:50 +00:00
}
2021-10-04 06:54:20 +00:00
2022-01-03 07:06:42 +00:00
"transitionend" => Arc::new(serde_json::from_value::<TransitionData>(val).unwrap()),
2021-10-04 06:54:20 +00:00
"abort" | "canplay" | "canplaythrough" | "durationchange" | "emptied" | "encrypted"
| "ended" | "error" | "loadeddata" | "loadedmetadata" | "loadstart" | "pause" | "play"
| "playing" | "progress" | "ratechange" | "seeked" | "seeking" | "stalled" | "suspend"
| "timeupdate" | "volumechange" | "waiting" => {
2021-10-14 16:46:50 +00:00
//
2022-01-03 07:06:42 +00:00
Arc::new(MediaData {})
2021-10-04 06:54:20 +00:00
}
2022-01-03 07:06:42 +00:00
"toggle" => Arc::new(ToggleData {}),
2021-10-04 06:54:20 +00:00
2021-11-12 03:18:16 +00:00
_ => Arc::new(()),
2021-10-04 06:54:20 +00:00
}
}
2021-10-04 05:28:04 +00:00
fn event_name_from_typ(typ: &str) -> &'static str {
match typ {
"copy" => "copy",
"cut" => "cut",
"paste" => "paste",
"compositionend" => "compositionend",
"compositionstart" => "compositionstart",
"compositionupdate" => "compositionupdate",
"keydown" => "keydown",
"keypress" => "keypress",
"keyup" => "keyup",
"focus" => "focus",
"focusout" => "focusout",
2021-10-04 05:28:04 +00:00
"blur" => "blur",
"change" => "change",
"input" => "input",
"invalid" => "invalid",
"reset" => "reset",
"submit" => "submit",
"click" => "click",
"contextmenu" => "contextmenu",
"doubleclick" => "doubleclick",
"drag" => "drag",
"dragend" => "dragend",
"dragenter" => "dragenter",
"dragexit" => "dragexit",
"dragleave" => "dragleave",
"dragover" => "dragover",
"dragstart" => "dragstart",
"drop" => "drop",
"mousedown" => "mousedown",
"mouseenter" => "mouseenter",
"mouseleave" => "mouseleave",
"mousemove" => "mousemove",
"mouseout" => "mouseout",
"mouseover" => "mouseover",
"mouseup" => "mouseup",
"pointerdown" => "pointerdown",
"pointermove" => "pointermove",
"pointerup" => "pointerup",
"pointercancel" => "pointercancel",
"gotpointercapture" => "gotpointercapture",
"lostpointercapture" => "lostpointercapture",
"pointerenter" => "pointerenter",
"pointerleave" => "pointerleave",
"pointerover" => "pointerover",
"pointerout" => "pointerout",
"select" => "select",
"touchcancel" => "touchcancel",
"touchend" => "touchend",
"touchmove" => "touchmove",
"touchstart" => "touchstart",
"scroll" => "scroll",
"wheel" => "wheel",
"animationstart" => "animationstart",
"animationend" => "animationend",
"animationiteration" => "animationiteration",
"transitionend" => "transitionend",
"abort" => "abort",
"canplay" => "canplay",
"canplaythrough" => "canplaythrough",
"durationchange" => "durationchange",
"emptied" => "emptied",
"encrypted" => "encrypted",
"ended" => "ended",
"error" => "error",
"loadeddata" => "loadeddata",
"loadedmetadata" => "loadedmetadata",
"loadstart" => "loadstart",
"pause" => "pause",
"play" => "play",
"playing" => "playing",
"progress" => "progress",
"ratechange" => "ratechange",
"seeked" => "seeked",
"seeking" => "seeking",
"stalled" => "stalled",
"suspend" => "suspend",
"timeupdate" => "timeupdate",
"volumechange" => "volumechange",
"waiting" => "waiting",
"toggle" => "toggle",
_ => {
panic!("unsupported event type")
}
}
}