12 releases
0.2.4 | Apr 27, 2020 |
---|---|
0.2.3 | Apr 27, 2020 |
0.1.6 | Feb 13, 2020 |
0.1.4 | Jan 27, 2020 |
0.1.0 | Dec 30, 2019 |
#5 in #responsive
50 downloads per month
Used in libscroll-re
54KB
909 lines
libscroll [WIP]
A drop-in solution to add smooth, responsive, scrolling to any view. Takes in individual input events, outputs a simple pan amount.
Main library interface is implemented in src/lib.rs
Usage:
- Allocate a scrollview with Scrollview::new(), the returned scrollview is the object to which all following directions are applied (through method calls)
- Use set_geometry with the size of both the content of the scrollview and the size of the viewport at the current time
- Use set_avg_frametime based on current FPS or some other metric to allow position prediction. If this information isn't available, a safe default is 0ms, but this will introduce additional perceptible lag
- If render and event loop are separate, split them here. Place the scrollview in an Arc<Mutex<>> to ensure atomic access. Note: scrollviews may be made atomic and internally mutable in the future. This change should not alter backwards compatibility, but should improve usability for this use case
Within event loop:
- Take any outstanding events from platform driver/provider (SDL, Libinput, Wayland event provider) and use push_event() to add them to the internal queue.
- Loop back to 1
Within render loop:
- Call set_next_frame_predict() to set approximately how long it will be from now until content is rendered to screen, or 0 if unsure (at cost of additional latency)
- Call step_frame() to both account for any newly emplaced events, and to advance any ongoing animations by one tick
- If animating() is true, use either get_position_absolute() or get_position_relative() to see where to move the viewport, or by how much. These calls are idempotent and non-mutating. Call them whenever is convenient after step_frame()
That's it! Everything else is handled behind the scenes
Dependencies
~2MB
~32K SLoC