Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use DragEvent not MouseEvent for drag events #3137

Merged
merged 6 commits into from
Oct 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions packages/web/src/events/drag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,78 @@ use dioxus_html::{
},
HasDragData, HasFileData, HasMouseData,
};
use web_sys::MouseEvent;
use wasm_bindgen::JsCast;
use web_sys::{DragEvent, MouseEvent};

use super::{Synthetic, WebEventExt};

pub(crate) struct WebDragData {
raw: Synthetic<MouseEvent>,
drag: Synthetic<DragEvent>,
mouse: Synthetic<MouseEvent>,
}

impl WebDragData {
pub fn new(raw: MouseEvent) -> Self {
pub fn new(raw: DragEvent) -> Self {
let mouse = raw
.clone()
.dyn_into::<MouseEvent>()
.expect("Inconceivable! DragEvent is not MouseEvent?");
Self {
raw: Synthetic::new(raw),
drag: Synthetic::new(raw),
mouse: Synthetic::new(mouse),
}
}
}

impl HasDragData for WebDragData {
fn as_any(&self) -> &dyn std::any::Any {
&self.raw as &dyn std::any::Any
&self.drag.event as &dyn std::any::Any
}
}

impl HasMouseData for WebDragData {
fn as_any(&self) -> &dyn std::any::Any {
&self.raw as &dyn std::any::Any
&self.mouse.event as &dyn std::any::Any
}
}

impl PointerInteraction for WebDragData {
fn trigger_button(&self) -> Option<dioxus_html::input_data::MouseButton> {
self.raw.trigger_button()
self.mouse.trigger_button()
}

fn held_buttons(&self) -> dioxus_html::input_data::MouseButtonSet {
self.raw.held_buttons()
self.mouse.held_buttons()
}
}

impl ModifiersInteraction for WebDragData {
fn modifiers(&self) -> dioxus_html::prelude::Modifiers {
self.raw.modifiers()
self.mouse.modifiers()
}
}

impl InteractionElementOffset for WebDragData {
fn coordinates(&self) -> dioxus_html::geometry::Coordinates {
self.raw.coordinates()
self.mouse.coordinates()
}

fn element_coordinates(&self) -> dioxus_html::geometry::ElementPoint {
self.raw.element_coordinates()
self.mouse.element_coordinates()
}
}

impl InteractionLocation for WebDragData {
fn client_coordinates(&self) -> dioxus_html::geometry::ClientPoint {
self.raw.client_coordinates()
self.mouse.client_coordinates()
}

fn screen_coordinates(&self) -> dioxus_html::geometry::ScreenPoint {
self.raw.screen_coordinates()
self.mouse.screen_coordinates()
}

fn page_coordinates(&self) -> dioxus_html::geometry::PagePoint {
self.raw.page_coordinates()
self.mouse.page_coordinates()
}
}

Expand All @@ -78,7 +85,7 @@ impl HasFileData for WebDragData {
use wasm_bindgen::JsCast;

let files = self
.raw
.drag
.event
.dyn_ref::<web_sys::DragEvent>()
.and_then(|drag_event| {
Expand All @@ -97,12 +104,12 @@ impl HasFileData for WebDragData {
}

impl WebEventExt for dioxus_html::DragData {
type WebEvent = web_sys::MouseEvent;
type WebEvent = web_sys::DragEvent;

#[inline(always)]
fn try_as_web_event(&self) -> Option<web_sys::MouseEvent> {
fn try_as_web_event(&self) -> Option<web_sys::DragEvent> {
self.downcast::<WebDragData>()
.map(|data| &data.raw.event)
.map(|data| &data.drag.event)
.cloned()
}
}
Loading