use nativeview_store::{NodeId, NodeStore};
use nativeview_tables::tables::OverscrollBehavior;
use super::ScrollOffsets;
#[repr(transparent)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ScrollTimestampMs(pub u64);
impl ScrollTimestampMs {
#[inline]
pub const fn new(ms: u64) -> Self {
Self(ms)
}
#[inline]
pub const fn as_u64(self) -> u64 {
self.0
}
#[inline]
pub const fn saturating_sub(self, earlier: Self) -> u64 {
self.0.saturating_sub(earlier.0)
}
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ScrollTouchPhase {
#[default]
None = 0,
Started = 1,
Moved = 2,
Ended = 3,
Cancelled = 4,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScrollInputKind {
DiscreteLines,
SmoothPixels {
phase: ScrollTouchPhase,
timestamp_ms: u64,
},
Instant,
}
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ScrollAxis {
X = 0,
Y = 1,
}
#[inline]
pub const fn default_px_per_line() -> f32 {
#[cfg(target_os = "windows")]
{
48.0
}
#[cfg(not(target_os = "windows"))]
{
40.0
}
}
#[inline]
pub fn normalize_shift_wheel(dx: f32, dy: f32, shift: bool) -> (f32, f32) {
let dx = if dx.is_finite() { dx } else { 0.0 };
let dy = if dy.is_finite() { dy } else { 0.0 };
if shift && dx.abs() < 1e-4 && dy.abs() >= 1e-4 {
(dy, 0.0)
} else {
(dx, dy)
}
}
#[derive(Clone, Copy)]
pub struct ScrollContainerCtx<'a> {
pub store: &'a NodeStore,
pub node: NodeId,
pub viewport: (f32, f32),
pub offset: (f32, f32),
pub max_offset: (f32, f32),
pub allow_axes: (bool, bool),
pub overscroll_behavior: (OverscrollBehavior, OverscrollBehavior),
pub bounce_requested: bool,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct ScrollDeltaResult {
pub next_offset: (f32, f32),
pub remaining: (f32, f32),
pub consumed_x: bool,
pub consumed_y: bool,
pub visual_changed: bool,
pub clamped_changed: bool,
pub auto_overscroll_x: bool,
pub auto_overscroll_y: bool,
}
pub trait ScrollBoundsLookup {
fn scroll_bounds(&self, store: &NodeStore, node: NodeId) -> Option<(f32, f32, f32, f32)>;
#[inline]
fn is_top_layer_boundary(&self, _node: NodeId) -> bool {
false
}
}
pub trait ScrollPhysicsEngine: Send {
fn on_scroll_delta(
&mut self,
ctx: &ScrollContainerCtx<'_>,
dx: f32,
dy: f32,
kind: ScrollInputKind,
out: &mut ScrollDeltaResult,
);
fn on_gesture_start(
&mut self,
_store: &NodeStore,
_bounds: &dyn ScrollBoundsLookup,
_start: NodeId,
_now_ms: ScrollTimestampMs,
) {
}
fn latched_target(&self) -> Option<NodeId> {
None
}
fn on_gesture_move(&mut self, _now_ms: ScrollTimestampMs) {}
fn on_outermost_overscroll(
&mut self,
_ctx: &ScrollContainerCtx<'_>,
_axis: ScrollAxis,
_rem: f32,
_kind: ScrollInputKind,
) -> bool {
false
}
fn on_gesture_end(
&mut self,
_store: &NodeStore,
_scroll: &mut ScrollOffsets,
_now_ms: ScrollTimestampMs,
_cancelled: bool,
_out_visual: &mut Vec<NodeId>,
_out_clamped: &mut Vec<NodeId>,
) {
}
fn step(
&mut self,
_store: &NodeStore,
_bounds: &dyn ScrollBoundsLookup,
_scroll: &mut ScrollOffsets,
_dt_s: f32,
_out_visual: &mut Vec<NodeId>,
_out_clamped: &mut Vec<NodeId>,
) -> bool {
false
}
fn overscroll(&self, _node: NodeId) -> (f32, f32) {
(0.0, 0.0)
}
fn has_active(&self) -> bool {
false
}
fn px_per_line(&self) -> f32 {
default_px_per_line()
}
fn cancel_node(&mut self, _node: NodeId) {}
fn reconcile_node(
&mut self,
node: NodeId,
_new_pos: (f32, f32),
_max: (f32, f32),
_anchor_delta: (f32, f32),
explicit_seek: bool,
) {
if explicit_seek {
self.cancel_node(node);
}
}
fn prune_missing(&mut self, _store: &NodeStore) {}
fn clear(&mut self) {}
fn set_reduced_motion(
&mut self,
_reduced: bool,
_scroll: &mut ScrollOffsets,
_out_visual: &mut Vec<NodeId>,
_out_clamped: &mut Vec<NodeId>,
) -> bool {
false
}
fn set_synthetic_momentum(&mut self, _enabled: bool) {}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct InstantScrollPhysics;
const _: () = assert!(std::mem::size_of::<InstantScrollPhysics>() == 0);
impl InstantScrollPhysics {
#[inline]
fn apply_axis(
allow: bool,
ob: OverscrollBehavior,
cur: f32,
max: f32,
mut rem: f32,
) -> (f32, f32, bool) {
if !rem.is_finite() {
return (cur, 0.0, false);
}
if rem.abs() <= 1e-4 {
return (cur, rem, false);
}
let mut next = cur;
let mut consumed = false;
if allow && max > 0.0 {
let used = (cur + rem).clamp(0.0, max) - cur;
if used.abs() > 1e-4 {
next = cur + used;
rem -= used;
consumed = true;
}
}
if rem.abs() > 1e-4 && allow && ob != OverscrollBehavior::Auto {
rem = 0.0;
}
(next, rem, consumed)
}
}
impl ScrollPhysicsEngine for InstantScrollPhysics {
fn on_scroll_delta(
&mut self,
ctx: &ScrollContainerCtx<'_>,
dx: f32,
dy: f32,
_kind: ScrollInputKind,
out: &mut ScrollDeltaResult,
) {
let (next_x, rem_x, consumed_x) = Self::apply_axis(
ctx.allow_axes.0,
ctx.overscroll_behavior.0,
ctx.offset.0,
ctx.max_offset.0,
dx,
);
let (next_y, rem_y, consumed_y) = Self::apply_axis(
ctx.allow_axes.1,
ctx.overscroll_behavior.1,
ctx.offset.1,
ctx.max_offset.1,
dy,
);
let changed = consumed_x || consumed_y;
out.next_offset = (next_x, next_y);
out.remaining = (rem_x, rem_y);
out.consumed_x = consumed_x;
out.consumed_y = consumed_y;
out.visual_changed = changed;
out.clamped_changed = changed;
}
}