Crate ui_events

Crate ui_events 

Source
Expand description

Cross-platform input event types modeled after W3C UI Events.

This crate provides small, portable data types for working with pointer (mouse, touch, pen) and keyboard input in a platform-agnostic way. It aims to closely follow W3C terminology while remaining practical for native application development.

§What you get:

  • Pointer events: button down/up, move, enter/leave, scroll, gestures
  • Rich pointer state: position, pressure, tilt, contact size, modifiers
  • Keyboard types re-exported from keyboard-types
  • A stable vocabulary you can adapt from windowing backends

This crate is intentionally focused on data structures — it does not open windows or read events. For integrations, see the adapter crates:

§Coordinate system and units

  • Positions are in physical pixels (dpi::PhysicalPosition<f64>), with the Y axis increasing downward.
  • Use PointerState::logical_position to obtain logical coordinates using a scale factor.
  • Scroll deltas are expressed via ScrollDelta; see its docs for details on page/line/pixel semantics.

§Primary pointer

Some interactions need a notion of a “primary” pointer (e.g. left mouse button, first touch). The reserved id PointerId::PRIMARY marks this. Helper methods like PointerEvent::is_primary_pointer and PointerInfo::is_primary_pointer are provided for convenience.

§Feature flags

  • std (default): Use the Rust standard library.
  • kurbo: Add convenience methods for converting positions to kurbo::Point.

§Examples

Basic matching on pointer events:

use ui_events::pointer::{PointerEvent, PointerButton, PointerButtonEvent, PointerInfo, PointerState, PointerType};
use ui_events::ScrollDelta;
use keyboard_types::Modifiers;
use dpi::{PhysicalPosition, PhysicalSize};

fn handle_event(ev: PointerEvent) {
    match ev {
        PointerEvent::Down(PointerButtonEvent { button, state, .. }) => {
            if button == Some(PointerButton::Primary) {
                // Start a drag, for example
                let pos = state.position;
                let _ = (pos.x, pos.y);
            }
        }
        PointerEvent::Move(upd) => {
            let logical = upd.current.logical_position();
            let _ = (logical.x, logical.y);
        }
        PointerEvent::Scroll(s) => {
            match s.delta {
                ScrollDelta::PageDelta(x, y) => { let _ = (x, y); }
                ScrollDelta::LineDelta(x, y) => { let _ = (x, y); }
                ScrollDelta::PixelDelta(p) => { let _ = (p.x, p.y); }
            }
        }
        _ => {}
    }
}

// Construct a minimal primary-pointer Down event
let ev = PointerEvent::Down(PointerButtonEvent{
    button: Some(PointerButton::Primary),
    pointer: PointerInfo{
        pointer_id: Some(ui_events::pointer::PointerId::PRIMARY),
        persistent_device_id: None,
        pointer_type: PointerType::Mouse,
    },
    state: PointerState{
        time: 0,
        position: PhysicalPosition { x: 10.0, y: 20.0 },
        buttons: Default::default(),
        modifiers: Modifiers::empty(),
        count: 1,
        contact_geometry: PhysicalSize { width: 1.0, height: 1.0 },
        orientation: Default::default(),
        pressure: 0.5,
        tangential_pressure: 0.0,
        scale_factor: 2.0,
    },
});
handle_event(ev);

§See also

Modules§

keyboard
Keyboard types
pointer
Pointer event types

Enums§

ScrollDelta
Scroll delta.