On Web, implement DeviceEvents (#2871)

This commit is contained in:
daxpedda 2023-06-14 10:26:26 +02:00 committed by GitHub
parent 9a9c9b15ba
commit 6300cf915e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 405 additions and 85 deletions

View file

@ -12,6 +12,8 @@ use crate::window::{WindowAttributes, WindowId as RootWindowId};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use js_sys::Promise;
use smol_str::SmolStr;
@ -24,6 +26,7 @@ use web_sys::{Event, FocusEvent, HtmlCanvasElement, KeyboardEvent, WheelEvent};
pub struct Canvas {
common: Common,
id: WindowId,
pub has_focus: Arc<AtomicBool>,
on_touch_start: Option<EventListenerHandle<dyn FnMut(Event)>>,
on_touch_end: Option<EventListenerHandle<dyn FnMut(Event)>>,
on_focus: Option<EventListenerHandle<dyn FnMut(FocusEvent)>>,
@ -91,6 +94,7 @@ impl Canvas {
wants_fullscreen: Rc::new(RefCell::new(false)),
},
id,
has_focus: Arc::new(AtomicBool::new(false)),
on_touch_start: None,
on_touch_end: None,
on_blur: None,

View file

@ -57,6 +57,17 @@ pub fn mouse_button(event: &MouseEvent) -> Option<MouseButton> {
}
}
impl MouseButton {
pub fn to_id(self) -> u32 {
match self {
MouseButton::Left => 0,
MouseButton::Right => 1,
MouseButton::Middle => 2,
MouseButton::Other(value) => value.into(),
}
}
}
pub fn mouse_position(event: &MouseEvent) -> LogicalPosition<f64> {
LogicalPosition {
x: event.offset_x() as f64,

View file

@ -1,5 +1,5 @@
mod canvas;
mod event;
pub mod event;
mod event_handle;
mod media_query_handle;
mod pointer;
@ -8,6 +8,7 @@ mod timeout;
pub use self::canvas::Canvas;
pub use self::event::ButtonsState;
pub use self::event_handle::EventListenerHandle;
pub use self::resize_scaling::ResizeScaleHandle;
pub use self::timeout::{IdleCallback, Timeout};