use std::{borrow::Cow, rc::Rc, sync::Arc};
use super::element::Listener;
use crate::{EventPayload, JsonValue, PayloadValue, Rect, Size, TouchEvent};
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ScrollEvent {
pub x: f32,
pub y: f32,
pub content_width: f32,
pub content_height: f32,
pub width: f32,
pub height: f32,
pub zoom_scale: f32,
}
impl Default for ScrollEvent {
fn default() -> Self {
Self {
x: 0.0,
y: 0.0,
content_width: 0.0,
content_height: 0.0,
width: 0.0,
height: 0.0,
zoom_scale: 1.0,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct ScrollOffset {
pub x: f32,
pub y: f32,
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct PanEvent {
pub translation_x: f32,
pub translation_y: f32,
pub velocity_x: f32,
pub velocity_y: f32,
pub value_x: Option<f32>,
pub value_y: Option<f32>,
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct GestureEvent {
pub value: f32,
pub velocity: f32,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Selection {
pub start: u32,
pub end: u32,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct WebNavigation {
pub url: String,
pub title: String,
pub loading: bool,
pub can_go_back: bool,
pub can_go_forward: bool,
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct VideoInfo {
pub duration: f32,
pub width: f32,
pub height: f32,
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct VideoProgress {
pub current_time: f32,
pub duration: f32,
}
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct ImageProgress {
pub loaded: f64,
pub total: f64,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NativeEvent {
pub name: Arc<str>,
pub data: JsonValue,
}
fn field<'a>(payload: &'a EventPayload, key: &str) -> Option<&'a PayloadValue> {
match payload {
EventPayload::Map(entries) => entries.iter().find(|(k, _)| *k == key).map(|(_, v)| v),
_ => None,
}
}
fn number(payload: &EventPayload, key: &str) -> f32 {
match field(payload, key) {
Some(PayloadValue::Number(n)) => *n as f32,
_ => 0.0,
}
}
fn flag(payload: &EventPayload, key: &str) -> bool {
matches!(field(payload, key), Some(PayloadValue::Bool(true)))
}
fn string<'a>(payload: &'a EventPayload, key: &str) -> Option<&'a str> {
match field(payload, key) {
Some(PayloadValue::Str(s)) => Some(s),
_ => None,
}
}
pub(crate) type Decode<T> = for<'a> fn(&'a EventPayload) -> Option<Cow<'a, T>>;
pub(crate) fn typed<T: ?Sized + ToOwned + 'static>(
listener: Listener<T>,
decode: Decode<T>,
) -> Listener<EventPayload> {
let inner = listener.0;
Listener(Rc::new(move |state, props, payload: &EventPayload, cx| {
if let Some(value) = decode(payload) {
inner(state, props, &value, cx);
}
}))
}
pub(crate) fn unit(_: &EventPayload) -> Option<Cow<'_, ()>> {
Some(Cow::Owned(()))
}
pub(crate) fn layout(p: &EventPayload) -> Option<Cow<'_, Rect>> {
match *p {
EventPayload::Layout {
x,
y,
width,
height,
} => Some(Cow::Owned(Rect {
x,
y,
width,
height,
})),
_ => None,
}
}
pub(crate) fn text(p: &EventPayload) -> Option<Cow<'_, str>> {
match p {
EventPayload::Text { text, .. } => Some(Cow::Borrowed(text)),
_ => None,
}
}
pub(crate) fn key(p: &EventPayload) -> Option<Cow<'_, str>> {
string(p, "key").map(Cow::Borrowed)
}
pub(crate) fn action(p: &EventPayload) -> Option<Cow<'_, str>> {
string(p, "actionName").map(Cow::Borrowed)
}
pub(crate) fn message(p: &EventPayload) -> Option<Cow<'_, str>> {
match p {
EventPayload::Error { message } => Some(Cow::Borrowed(message)),
_ => string(p, "message").map(Cow::Borrowed),
}
}
pub(crate) fn data(p: &EventPayload) -> Option<Cow<'_, str>> {
string(p, "data").map(Cow::Borrowed)
}
pub(crate) fn selection(p: &EventPayload) -> Option<Cow<'_, Selection>> {
Some(Cow::Owned(Selection {
start: number(p, "start") as u32,
end: number(p, "end") as u32,
}))
}
pub(crate) fn size(p: &EventPayload) -> Option<Cow<'_, Size>> {
match *p {
EventPayload::Image { width, height } => Some(Cow::Owned(Size::new(width, height))),
_ => Some(Cow::Owned(Size::new(
number(p, "width"),
number(p, "height"),
))),
}
}
pub(crate) fn scroll(p: &EventPayload) -> Option<Cow<'_, ScrollEvent>> {
Some(Cow::Owned(match *p {
EventPayload::Scroll { x, y } => ScrollEvent {
x,
y,
..ScrollEvent::default()
},
_ => ScrollEvent {
x: number(p, "x"),
y: number(p, "y"),
content_width: number(p, "contentWidth"),
content_height: number(p, "contentHeight"),
width: number(p, "width"),
height: number(p, "height"),
zoom_scale: match field(p, "zoomScale") {
Some(PayloadValue::Number(n)) => *n as f32,
_ => 1.0,
},
},
}))
}
pub(crate) fn offset(p: &EventPayload) -> Option<Cow<'_, ScrollOffset>> {
Some(Cow::Owned(match *p {
EventPayload::Scroll { x, y } => ScrollOffset { x, y },
_ => ScrollOffset {
x: number(p, "x"),
y: number(p, "y"),
},
}))
}
pub(crate) fn pan(p: &EventPayload) -> Option<Cow<'_, PanEvent>> {
match *p {
EventPayload::Pan {
translation_x,
translation_y,
velocity_x,
velocity_y,
value_x,
value_y,
} => Some(Cow::Owned(PanEvent {
translation_x,
translation_y,
velocity_x,
velocity_y,
value_x,
value_y,
})),
_ => None,
}
}
pub(crate) fn pinch(p: &EventPayload) -> Option<Cow<'_, GestureEvent>> {
Some(Cow::Owned(GestureEvent {
value: number(p, "scale"),
velocity: number(p, "velocity"),
}))
}
pub(crate) fn rotation(p: &EventPayload) -> Option<Cow<'_, GestureEvent>> {
Some(Cow::Owned(GestureEvent {
value: number(p, "rotation"),
velocity: number(p, "velocity"),
}))
}
pub(crate) fn touch(p: &EventPayload) -> Option<Cow<'_, TouchEvent>> {
match p {
EventPayload::Touch(touch) => Some(Cow::Borrowed(touch)),
_ => None,
}
}
pub(crate) fn navigation(p: &EventPayload) -> Option<Cow<'_, WebNavigation>> {
Some(Cow::Owned(WebNavigation {
url: string(p, "url").unwrap_or_default().to_owned(),
title: string(p, "title").unwrap_or_default().to_owned(),
loading: flag(p, "loading"),
can_go_back: flag(p, "canGoBack"),
can_go_forward: flag(p, "canGoForward"),
}))
}
pub(crate) fn video(p: &EventPayload) -> Option<Cow<'_, VideoInfo>> {
Some(Cow::Owned(VideoInfo {
duration: number(p, "duration"),
width: number(p, "width"),
height: number(p, "height"),
}))
}
pub(crate) fn progress(p: &EventPayload) -> Option<Cow<'_, VideoProgress>> {
Some(Cow::Owned(VideoProgress {
current_time: number(p, "currentTime"),
duration: number(p, "duration"),
}))
}
pub(crate) fn image_progress(p: &EventPayload) -> Option<Cow<'_, ImageProgress>> {
let number = |key| match field(p, key) {
Some(PayloadValue::Number(n)) => *n,
_ => 0.0,
};
Some(Cow::Owned(ImageProgress {
loaded: number("loaded"),
total: number("total"),
}))
}
pub(crate) fn buffering(p: &EventPayload) -> Option<Cow<'_, bool>> {
Some(Cow::Owned(flag(p, "isBuffering")))
}
pub(crate) fn native(p: &EventPayload) -> Option<Cow<'_, NativeEvent>> {
match p {
EventPayload::Native { name, data } => Some(Cow::Owned(NativeEvent {
name: name.clone(),
data: data.clone(),
})),
_ => None,
}
}