ui-events

Crates.ioui-events
lib.rsui-events
version0.3.0
created_at2025-05-01 17:31:13.957132+00
updated_at2026-01-18 06:19:12.368334+00
descriptionA crate for working with UI events and input types.
homepage
repositoryhttps://github.com/endoli/ui-events
max_upload_size
id1656575
size108,763
Aaron Muir Hamilton (xorgy)

documentation

README

UI Events

A library for working with UI events and input types.

Linebender Zulip, #general channel dependency status Apache 2.0 or MIT license. Build status Crates.io Docs

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

Minimum supported Rust Version (MSRV)

This version of UI Events has been verified to compile with Rust 1.85 and later.

Future versions of UI Events 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.

Click here if compiling fails.

As time has passed, some of UI Events' 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
cargo update -p package_name --precise 0.1.1

Community

Linebender Zulip

Discussion of UI Events development happens in the Linebender Zulip, specifically the #general channel. All public content can be read without logging in.

License

Licensed under either of

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.

Commit count: 82

cargo fmt