Module scroll

Source
Expand description

Allocation-free vertical scrolling in logical UI units, positive Y down.

Content rectangles start at the content’s top-left, before scrolling. Keep widget IDs independent of offsets and list positions. A host chooses focus, calls ScrollState::ensure_visible before painting, and uses the same translated rectangles and viewport clip for paint and pointer input. This module has no clock, input ownership, inertia, horizontal or nested routing.

use ferravex_play::ui::{Rect, scroll::ScrollState};
let viewport = Rect { min: [12., 24.], size: [200., 100.] };
let focused = Rect { min: [0., 180.], size: [200., 40.] };
let mut scroll = ScrollState::new(100., 400.)?;
assert!(scroll.ensure_visible(focused.min[1], focused.size[1])?);
assert_eq!(scroll.offset(), 120.);
let displayed = scroll.translate_rect(viewport, focused)?;
assert_eq!(displayed.min, [12., 84.]);
assert_eq!(scroll.content_point(viewport, [20., 90.]), Some([8., 186.]));
assert_eq!(scroll.content_point(viewport, [20., 124.]), None);
scroll.set_extents(300., 400.)?; // Resize clamps the old offset.
assert_eq!(scroll.offset(), 100.);
scroll.reset();
assert_eq!(scroll.offset(), 0.);

Structs§

ScrollState
Two finite, nonnegative extents and a clamped offset, with no dynamic storage. Zero viewport height suspends scrolling at offset zero. Invalid operations return UiError::InvalidLayout without changing the state.