1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2025 the UI Events Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! This crate bridges [`web_sys`] DOM input events — Pointer Events (mouse, touch, pen),
//! Wheel, and Keyboard — into the [`ui-events`] model.
//!
//! It provides lightweight helpers to convert browser events into portable
//! `ui-events` types you can feed into your input handling. It supports
//! Pointer Events (mouse, touch, pen) and keyboard.
//!
//! ## Keyboard
//!
//! - [`keyboard::from_web_keyboard_event`]
//! - Optional helpers: [`keyboard::from_web_keydown_event`], [`keyboard::from_web_keyup_event`]
//!
//! ## Pointer (Pointer Events)
//!
//! - One‑shot DOM conversion: [`pointer::pointer_event_from_dom_event`]
//! - Multi-touch aware DOM conversion (may return multiple events):
//! [`pointer::pointer_events_from_dom_event`]
//! - Per‑event helpers (preferred):
//! [`pointer::down_from_pointer_event`], [`pointer::up_from_pointer_event`],
//! [`pointer::move_from_pointer_event`], [`pointer::enter_from_pointer_event`],
//! [`pointer::leave_from_pointer_event`], [`pointer::cancel_from_pointer_event`]
//! - Mouse‑only helpers (legacy and less portable):
//! [`pointer::down_from_mouse_event`], [`pointer::up_from_mouse_event`],
//! [`pointer::move_from_mouse_event`], [`pointer::enter_from_mouse_event`],
//! [`pointer::leave_from_mouse_event`], [`pointer::scroll_from_wheel_event`]
//! - Conversion options: [`pointer::Options`] (controls scale/coalesced/predicted)
//! - Pointer capture helpers: [`pointer::set_pointer_capture`],
//! [`pointer::release_pointer_capture`], [`pointer::has_pointer_capture`]
//!
//! ## Notes
//!
//! - Positions use `clientX` / `clientY` scaled by `Options::scale_factor`. Pass the
//! current device-pixel-ratio for physical pixels.
//! - **Element-local coordinates (recipe):** DOM pointer coordinates are reported in viewport
//! space even when you register the listener on an element. For canvas/SVG apps you often
//! want coordinates relative to a specific element’s top-left corner. A common recipe is:
//!
//! ```no_run
//! # use web_sys::{window, Element, PointerEvent};
//! # let (el, e): (Element, PointerEvent) = unimplemented!();
//! let rect = el.get_bounding_client_rect();
//! let dpr = window().unwrap().device_pixel_ratio();
//! let x = (e.client_x() as f64 - rect.left()) * dpr;
//! let y = (e.client_y() as f64 - rect.top()) * dpr;
//! ```
//!
//! This does *not* undo arbitrary CSS transforms, and for SVG you may want a true transform
//! inversion (e.g. `getScreenCTM()`).
//! - Coalesced and predicted move samples are opt‑in via `Options`.
//! - Touch events (`touchstart`/`touchmove`/`touchend`/`touchcancel`) may correspond to multiple
//! changed touches; use `pointer_events_from_dom_event` to receive all of them.
//! - Stylus fields:
//! - `pressure`, `tangential_pressure`, `contact_geometry`, and `orientation` are populated
//! from Pointer Events data when available (preferring `altitudeAngle`/`azimuthAngle`,
//! otherwise falling back to `tiltX`/`tiltY`).
//! - Stylus rotation/twist (Pointer Events `twist`) is not currently exposed by `ui-events`,
//! so it is not mapped.
//! - Keyboard: unknown `key`/`code` map to `Unidentified`; `is_composing` reflects the DOM flag.
//!
//! ## Example
//!
//! ```no_run
//! use web_sys::wasm_bindgen::JsCast;
//! use web_sys::{window, Event, KeyboardEvent};
//! use ui_events_web::{keyboard, pointer};
//!
//! // Inside an event listener closure…
//! # {
//! let ev: Event = /* from DOM */
//! # unimplemented!();
//! let win = window().unwrap();
//! let opts = pointer::Options::default()
//! .with_scale(win.device_pixel_ratio())
//! .with_coalesced(true)
//! .with_predicted(true);
//!
//! if let Some(pe) = pointer::pointer_event_from_dom_event(&ev, &opts) {
//! match pe {
//! ui_events::pointer::PointerEvent::Move(update) => {
//! // Use update.current; update.coalesced / update.predicted may be empty
//! }
//! ui_events::pointer::PointerEvent::Down(_) => {}
//! ui_events::pointer::PointerEvent::Up(_) => {}
//! _ => {}
//! }
//! }
//!
//! if let Some(ke) = ev.dyn_ref::<KeyboardEvent>() {
//! let k = keyboard::from_web_keyboard_event(ke);
//! // Use k.state, k.code, k.key, k.modifiers …
//! }
//! # }
//! ```
//!
//! [`ui-events`]: https://docs.rs/ui-events/
// LINEBENDER LINT SET - lib.rs - v3
// See https://linebender.org/wiki/canonical-lints/
// These lints shouldn't apply to examples or tests.
// These lints shouldn't apply to examples.
// Targeting e.g. 32-bit means structs containing usize can give false positives for 64-bit.
// END LINEBENDER LINT SET
extern crate alloc;