dioxus/packages/desktop/src/events.rs

220 lines
7.1 KiB
Rust
Raw Normal View History

2022-02-13 18:56:11 +00:00
//! 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};
2022-06-22 18:57:05 +00:00
use dioxus_html::event_bubbles;
2021-11-07 03:11:17 +00:00
use dioxus_html::on::*;
2022-02-13 18:56:11 +00:00
#[derive(serde::Serialize, serde::Deserialize)]
pub(crate) struct IpcMessage {
method: String,
params: serde_json::Value,
}
impl IpcMessage {
pub(crate) fn method(&self) -> &str {
self.method.as_str()
}
pub(crate) fn params(self) -> serde_json::Value {
self.params
}
}
pub(crate) fn parse_ipc_message(payload: &str) -> Option<IpcMessage> {
2022-02-15 18:38:06 +00:00
match serde_json::from_str(payload) {
2022-02-13 18:56:11 +00:00
Ok(message) => Some(message),
Err(e) => {
2022-02-19 04:30:09 +00:00
log::error!("could not parse IPC message, error: {}", e);
2022-02-13 18:56:11 +00:00
None
}
}
}
#[derive(serde::Serialize, serde::Deserialize)]
struct ImEvent {
event: String,
mounted_dom_id: 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-10-04 06:54:20 +00:00
let ImEvent {
event,
mounted_dom_id,
contents,
2022-02-13 18:56:11 +00:00
} = serde_json::from_value(val).unwrap();
2021-10-04 06:54:20 +00:00
let mounted_dom_id = Some(ElementId(mounted_dom_id as usize));
2022-02-13 18:56:11 +00:00
let name = event_name_from_type(&event);
2022-06-22 18:57:05 +00:00
2021-10-04 06:54:20 +00:00
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,
2022-06-22 18:57:05 +00:00
bubbles: event_bubbles(name),
2021-11-15 14:49:01 +00:00
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" | "focusin" => {
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
}
2022-03-11 15:49:38 +00:00
"click" | "contextmenu" | "dblclick" | "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
}
}
2022-02-13 18:56:11 +00:00
fn event_name_from_type(typ: &str) -> &'static str {
2021-10-04 05:28:04 +00:00
match typ {
"copy" => "copy",
"cut" => "cut",
"paste" => "paste",
"compositionend" => "compositionend",
"compositionstart" => "compositionstart",
"compositionupdate" => "compositionupdate",
"keydown" => "keydown",
"keypress" => "keypress",
"keyup" => "keyup",
"focus" => "focus",
"focusout" => "focusout",
"focusin" => "focusin",
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",
2022-03-11 15:49:38 +00:00
"dblclick" => "dblclick",
2021-10-04 05:28:04 +00:00
"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",
2022-03-11 15:49:38 +00:00
a => {
panic!("unsupported event type {:?}", a);
2021-10-04 05:28:04 +00:00
}
}
}