UI Input State
A library for stateful tracking of current input state.
Frame-oriented input state built on ui-events.
This crate provides simple state containers to make input handling easier in
immediate-mode or frame-based UIs. Instead of reacting to each event
individually, you feed pointer and keyboard events into the state, query the
current and per-frame information during your update, and then call
[InputState::clear_frame] at the end of the frame.
What it provides:
- [
PrimaryPointerState]: current pointer state, coalesced and predicted motion, per-frame button transitions, and helpers for motion in physical/logical units. - [
KeyboardState]: current modifiers, keys down, and per-frame key transitions. - [
InputState]: a convenience container bundling both states and a per-frame clear.
Typical lifecycle per frame:
- Receive backend events and convert them to
ui-eventstypes. - Update
PrimaryPointerStateandKeyboardStatewith the events. - Read state during your UI update (e.g. check just pressed, motion, etc.).
- Call [
InputState::clear_frame] before the next frame.
Example (sketch):
use ;
use PointerEvent;
use KeyboardEvent;
let mut input = default;
// 1-2) In your event loop, feed events into state
// 3) During your update pass, read state
// 4) At the end of the frame, clear per-frame transitions
Coordinates and units
Pointer positions are stored in physical pixels with a Y-down axis, as in
ui-events. Use [PrimaryPointerState::current_logical_position] and
[PrimaryPointerState::logical_motion] to work in logical units.
Features
std(enabled by default): Use the Rust standard library.libm: Enableui-events/libmtransitively forno_stdenvironments.
Minimum supported Rust Version (MSRV)
This version of UI Input State has been verified to compile with Rust 1.85 and later.
Future versions of UI Input State 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 Input State'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 Input State 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.