UI Events for Web
A library for bridging web-sys events into the ui-events model.
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/clientYscaled byOptions::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:
let rect = el.get_bounding_client_rect; let dpr = window.unwrap.device_pixel_ratio; let x = * dpr; let y = * 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; usepointer_events_from_dom_eventto receive all of them. -
Stylus fields:
pressure,tangential_pressure,contact_geometry, andorientationare populated from Pointer Events data when available (preferringaltitudeAngle/azimuthAngle, otherwise falling back totiltX/tiltY).- Stylus rotation/twist (Pointer Events
twist) is not currently exposed byui-events, so it is not mapped.
-
Keyboard: unknown
key/codemap toUnidentified;is_composingreflects the DOM flag.
Example
use JsCast;
use ;
use ;
// Inside an event listener closure…
let ev: Event = /* from DOM */
let win = window.unwrap;
let opts = default
.with_scale
.with_coalesced
.with_predicted;
if let Some = pointer_event_from_dom_event
if let Some = ev.
Minimum supported Rust Version (MSRV)
This version of UI Events for Web has been verified to compile with Rust 1.85 and later.
Future versions of UI Events for Web might increase the Rust version requirement. It will not be treated as a breaking change and as such can even happen with small patch releases.
As time has passed, some of UI Events for Web's dependencies could have released versions with a higher Rust requirement. If you encounter a compilation issue due to a dependency and don't want to upgrade your Rust toolchain, then you could downgrade the dependency.
# Use the problematic dependency's name and version
Community
Discussion of UI Events for Web development happens in the Linebender Zulip, specifically the #general channel. All public content can be read without logging in.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Contributions are welcome by pull request. The Rust code of conduct applies. Please feel free to add your name to the AUTHORS file in any substantive pull request.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.