use alloc::collections::BTreeMap;
#[cfg(feature = "std")]
use alloc::vec::Vec;
use azul_core::{
dom::{DomId, NodeId, ScrollbarOrientation},
events::EasingFunction,
geom::{LogicalPosition, LogicalRect, LogicalSize},
hit_test::ScrollPosition,
styled_dom::NodeHierarchyItemId,
task::{Duration, Instant},
};
#[cfg(feature = "std")]
use std::sync::{Arc, Mutex};
use crate::managers::hover::InputPointId;
use crate::solver3::scrollbar::compute_scrollbar_geometry_with_button_size;
const SCROLL_CHANGE_EPSILON: f32 = 0.01;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScrollInputSource {
TrackpadContinuous,
TrackpadEnd,
WheelDiscrete,
Programmatic,
}
#[derive(Debug, Clone)]
pub struct ScrollInput {
pub dom_id: DomId,
pub node_id: NodeId,
pub delta: LogicalPosition,
pub timestamp: Instant,
pub source: ScrollInputSource,
}
#[cfg(feature = "std")]
#[derive(Debug, Clone, Default)]
pub struct ScrollInputQueue {
inner: Arc<Mutex<Vec<ScrollInput>>>,
}
#[cfg(feature = "std")]
impl ScrollInputQueue {
#[must_use] pub fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(Vec::new())),
}
}
pub fn push(&self, input: ScrollInput) {
if let Ok(mut queue) = self.inner.lock() {
queue.push(input);
}
}
#[must_use] pub fn take_all(&self) -> Vec<ScrollInput> {
self.inner.lock().map_or_else(
|_| Vec::new(),
|mut queue| core::mem::take(&mut *queue),
)
}
#[must_use] pub fn take_recent(&self, max_events: usize) -> Vec<ScrollInput> {
self.inner.lock().map_or_else(
|_| Vec::new(),
|mut queue| {
let mut events = core::mem::take(&mut *queue);
if events.len() > max_events {
events.sort_by(|a, b| a.timestamp.cmp(&b.timestamp));
events.drain(..events.len() - max_events);
}
events
},
)
}
#[must_use] pub fn has_pending(&self) -> bool {
self.inner
.lock()
.map(|q| !q.is_empty())
.unwrap_or(false)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ScrollbarComponent {
Track,
Thumb,
TopButton,
BottomButton,
}
#[derive(Copy, Debug, Clone)]
pub struct ScrollbarState {
pub visible: bool,
pub orientation: ScrollbarOrientation,
pub base_size: f32,
pub scale: LogicalPosition, pub thumb_position_ratio: f32,
pub thumb_size_ratio: f32,
pub track_rect: LogicalRect,
pub button_size: f32,
pub usable_track_length: f32,
pub thumb_length: f32,
pub thumb_offset: f32,
}
impl ScrollbarState {
#[must_use] pub fn hit_test_component(&self, local_pos: LogicalPosition) -> ScrollbarComponent {
match self.orientation {
ScrollbarOrientation::Vertical => {
if local_pos.y < self.button_size {
return ScrollbarComponent::TopButton;
}
let track_height = self.track_rect.size.height;
if local_pos.y > track_height - self.button_size {
return ScrollbarComponent::BottomButton;
}
let thumb_y_start = self.button_size + self.thumb_offset;
let thumb_y_end = thumb_y_start + self.thumb_length;
if local_pos.y >= thumb_y_start && local_pos.y <= thumb_y_end {
ScrollbarComponent::Thumb
} else {
ScrollbarComponent::Track
}
}
ScrollbarOrientation::Horizontal => {
if local_pos.x < self.button_size {
return ScrollbarComponent::TopButton;
}
let track_width = self.track_rect.size.width;
if local_pos.x > track_width - self.button_size {
return ScrollbarComponent::BottomButton;
}
let thumb_x_start = self.button_size + self.thumb_offset;
let thumb_x_end = thumb_x_start + self.thumb_length;
if local_pos.x >= thumb_x_start && local_pos.x <= thumb_x_end {
ScrollbarComponent::Thumb
} else {
ScrollbarComponent::Track
}
}
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct ScrollbarHit {
pub dom_id: DomId,
pub node_id: NodeId,
pub orientation: ScrollbarOrientation,
pub component: ScrollbarComponent,
pub local_position: LogicalPosition,
pub global_position: LogicalPosition,
}
#[derive(Debug, Clone, Default)]
pub struct ScrollManager {
states: BTreeMap<(DomId, NodeId), AnimatedScrollState>,
scrollbar_states: BTreeMap<(DomId, NodeId, ScrollbarOrientation), ScrollbarState>,
#[cfg(feature = "std")]
pub scroll_input_queue: ScrollInputQueue,
pub pending_wheel_event: Option<LogicalPosition>,
scroll_dirty: bool,
natural_scroll: bool,
}
#[derive(Debug, Clone)]
pub struct AnimatedScrollState {
pub current_offset: LogicalPosition,
pub animation: Option<ScrollAnimation>,
pub last_activity: Instant,
pub container_rect: LogicalRect,
pub content_rect: LogicalRect,
pub virtual_scroll_size: Option<LogicalSize>,
pub virtual_scroll_offset: Option<LogicalPosition>,
pub overscroll_behavior_x: azul_css::props::style::scrollbar::OverscrollBehavior,
pub overscroll_behavior_y: azul_css::props::style::scrollbar::OverscrollBehavior,
pub overflow_scrolling: azul_css::props::style::scrollbar::OverflowScrolling,
pub scrollbar_thickness: f32,
pub visual_width_px: f32,
pub has_horizontal_scrollbar: bool,
pub has_vertical_scrollbar: bool,
}
#[derive(Debug, Clone)]
struct ScrollAnimation {
start_time: Instant,
duration: Duration,
start_offset: LogicalPosition,
target_offset: LogicalPosition,
easing: EasingFunction,
}
#[derive(Copy, Debug, Clone)]
pub struct ScrollNodeInfo {
pub current_offset: LogicalPosition,
pub container_rect: LogicalRect,
pub content_rect: LogicalRect,
pub max_scroll_x: f32,
pub max_scroll_y: f32,
pub overscroll_behavior_x: azul_css::props::style::scrollbar::OverscrollBehavior,
pub overscroll_behavior_y: azul_css::props::style::scrollbar::OverscrollBehavior,
pub overflow_scrolling: azul_css::props::style::scrollbar::OverflowScrolling,
}
#[derive(Debug, Default)]
pub struct ScrollTickResult {
pub needs_repaint: bool,
pub updated_nodes: Vec<(DomId, NodeId)>,
}
impl ScrollManager {
#[must_use] pub fn new() -> Self {
let mut m = Self::default();
#[cfg(feature = "std")]
if let Some(v) = std::env::var_os("AZ_NATURAL_SCROLL") {
m.natural_scroll = matches!(v.to_str(), Some("1" | "true" | "TRUE"));
}
m
}
pub const fn set_natural_scroll(&mut self, natural: bool) {
self.natural_scroll = natural;
}
#[must_use] pub const fn is_natural_scroll(&self) -> bool {
self.natural_scroll
}
#[inline]
const fn scroll_sign(&self) -> f32 {
if self.natural_scroll {
1.0
} else {
-1.0
}
}
#[must_use] pub fn debug_counts(&self) -> (usize, usize) {
(self.states.len(), self.scrollbar_states.len())
}
pub(crate) const fn has_pending_scroll_changes(&self) -> bool {
self.scroll_dirty
}
pub const fn clear_scroll_dirty(&mut self) {
self.scroll_dirty = false;
}
#[must_use] pub fn build_scroll_offset_map(
&self,
dom_id: DomId,
scroll_ids: &std::collections::HashMap<usize, u64>,
) -> std::collections::HashMap<u64, (f32, f32)> {
let mut map = std::collections::HashMap::new();
for ((d, node_id), state) in &self.states {
if *d != dom_id { continue; }
let node_idx = node_id.index();
if let Some(&scroll_id) = scroll_ids.get(&node_idx) {
map.insert(scroll_id, (state.current_offset.x, state.current_offset.y));
}
}
map
}
#[cfg(feature = "std")]
pub fn record_scroll_input(&mut self, mut input: ScrollInput) -> bool {
let sign = self.scroll_sign();
input.delta.x *= sign;
input.delta.y *= sign;
let was_empty = !self.scroll_input_queue.has_pending();
self.scroll_input_queue.push(input);
was_empty }
#[cfg(feature = "std")]
pub fn record_scroll_from_hit_test(
&mut self,
delta_x: f32,
delta_y: f32,
source: ScrollInputSource,
hover_manager: &crate::managers::hover::HoverManager,
input_point_id: &InputPointId,
now: Instant,
) -> Option<(DomId, NodeId, bool)> {
self.pending_wheel_event = Some(LogicalPosition { x: delta_x, y: delta_y });
let hit_test = hover_manager.get_current(input_point_id)?;
let sign = self.scroll_sign();
let (eff_x, eff_y) = (delta_x * sign, delta_y * sign);
let target = self.select_scroll_target(
hit_test.hovered_nodes.iter().flat_map(|(dom_id, hit_node)| {
hit_node
.scroll_hit_test_nodes
.keys()
.rev()
.map(move |node_id| (*dom_id, *node_id))
}),
eff_x,
eff_y,
);
let (dom_id, node_id) = target?;
let input = ScrollInput {
dom_id,
node_id,
delta: LogicalPosition { x: delta_x, y: delta_y },
timestamp: now,
source,
};
let should_start_timer = self.record_scroll_input(input);
Some((dom_id, node_id, should_start_timer))
}
fn select_scroll_target<I>(
&self,
candidates: I,
eff_x: f32,
eff_y: f32,
) -> Option<(DomId, NodeId)>
where
I: Iterator<Item = (DomId, NodeId)>,
{
let mut fallback = None;
for (dom_id, node_id) in candidates {
if !self.is_node_scrollable(dom_id, node_id) {
continue;
}
if fallback.is_none() {
fallback = Some((dom_id, node_id));
}
if self.can_consume_delta(dom_id, node_id, eff_x, eff_y) {
return Some((dom_id, node_id));
}
}
fallback
}
#[must_use] pub fn a11y_scroll_info(
&self,
dom_id: DomId,
node_id: NodeId,
) -> Option<(LogicalPosition, f32, f32)> {
let state = self.states.get(&(dom_id, node_id))?;
let effective_width = state
.virtual_scroll_size
.map_or(state.content_rect.size.width, |s| s.width);
let effective_height = state
.virtual_scroll_size
.map_or(state.content_rect.size.height, |s| s.height);
let max_x = (effective_width - state.container_rect.size.width).max(0.0);
let max_y = (effective_height - state.container_rect.size.height).max(0.0);
if max_x <= 0.0 && max_y <= 0.0 {
return None;
}
Some((state.current_offset, max_x, max_y))
}
fn can_consume_delta(
&self,
dom_id: DomId,
node_id: NodeId,
eff_x: f32,
eff_y: f32,
) -> bool {
const EPS: f32 = 0.5;
let Some(state) = self.states.get(&(dom_id, node_id)) else {
return false;
};
let effective_width = state
.virtual_scroll_size
.map_or(state.content_rect.size.width, |s| s.width);
let effective_height = state
.virtual_scroll_size
.map_or(state.content_rect.size.height, |s| s.height);
let max_x = (effective_width - state.container_rect.size.width).max(0.0);
let max_y = (effective_height - state.container_rect.size.height).max(0.0);
let off = state.current_offset;
let x_ok = if eff_x > EPS {
off.x < max_x - EPS
} else if eff_x < -EPS {
off.x > EPS
} else {
false
};
let y_ok = if eff_y > EPS {
off.y < max_y - EPS
} else if eff_y < -EPS {
off.y > EPS
} else {
false
};
x_ok || y_ok
}
#[cfg(feature = "std")]
#[must_use] pub fn get_input_queue(&self) -> ScrollInputQueue {
self.scroll_input_queue.clone()
}
#[allow(clippy::suboptimal_flops)] #[allow(clippy::needless_pass_by_value)]
pub fn tick(&mut self, now: Instant) -> ScrollTickResult {
let mut result = ScrollTickResult::default();
for ((dom_id, node_id), state) in &mut self.states {
if let Some(anim) = &state.animation {
let elapsed = now.duration_since(&anim.start_time);
let t = elapsed.div(&anim.duration).min(1.0);
let eased_t = apply_easing(t, anim.easing);
state.current_offset = LogicalPosition {
x: anim.start_offset.x + (anim.target_offset.x - anim.start_offset.x) * eased_t,
y: anim.start_offset.y + (anim.target_offset.y - anim.start_offset.y) * eased_t,
};
result.needs_repaint = true;
result.updated_nodes.push((*dom_id, *node_id));
if t >= 1.0 {
state.animation = None;
}
}
}
result
}
#[must_use] pub fn has_active_animations(&self) -> bool {
self.states.values().any(|s| s.animation.is_some())
}
#[must_use] pub fn find_scroll_parent(
&self,
dom_id: DomId,
node_id: NodeId,
node_hierarchy: &[azul_core::styled_dom::NodeHierarchyItem],
) -> Option<NodeId> {
let mut current = Some(node_id);
while let Some(nid) = current {
if self.states.contains_key(&(dom_id, nid)) && nid != node_id {
return Some(nid);
}
current = node_hierarchy
.get(nid.index())
.and_then(azul_core::styled_dom::NodeHierarchyItem::parent_id);
}
None
}
fn is_node_scrollable(&self, dom_id: DomId, node_id: NodeId) -> bool {
let result = self.states.get(&(dom_id, node_id)).is_some_and(|state| {
let effective_width = state.virtual_scroll_size
.map_or(state.content_rect.size.width, |s| s.width);
let effective_height = state.virtual_scroll_size
.map_or(state.content_rect.size.height, |s| s.height);
let has_horizontal = effective_width > state.container_rect.size.width;
let has_vertical = effective_height > state.container_rect.size.height;
has_horizontal || has_vertical
});
result
}
pub fn set_scroll_position(
&mut self,
dom_id: DomId,
node_id: NodeId,
position: LogicalPosition,
now: Instant,
) {
let state = self
.states
.entry((dom_id, node_id))
.or_insert_with(|| AnimatedScrollState::new(now.clone()));
let clamped = state.clamp(position);
if (clamped.x - state.current_offset.x).abs() > SCROLL_CHANGE_EPSILON
|| (clamped.y - state.current_offset.y).abs() > SCROLL_CHANGE_EPSILON
{
self.scroll_dirty = true;
}
state.current_offset = clamped;
state.animation = None;
state.last_activity = now;
}
pub fn set_scroll_position_unclamped(
&mut self,
dom_id: DomId,
node_id: NodeId,
position: LogicalPosition,
now: Instant,
) {
let state = self
.states
.entry((dom_id, node_id))
.or_insert_with(|| AnimatedScrollState::new(now.clone()));
if (position.x - state.current_offset.x).abs() > SCROLL_CHANGE_EPSILON
|| (position.y - state.current_offset.y).abs() > SCROLL_CHANGE_EPSILON
{
self.scroll_dirty = true;
}
state.current_offset = position;
state.animation = None;
state.last_activity = now;
}
pub fn scroll_by(
&mut self,
dom_id: DomId,
node_id: NodeId,
delta: LogicalPosition,
duration: Duration,
easing: EasingFunction,
now: Instant,
) {
let current = self.get_current_offset(dom_id, node_id).unwrap_or_default();
let target = LogicalPosition {
x: current.x + delta.x,
y: current.y + delta.y,
};
self.scroll_to(dom_id, node_id, target, duration, easing, now);
}
pub fn scroll_to(
&mut self,
dom_id: DomId,
node_id: NodeId,
target: LogicalPosition,
duration: Duration,
easing: EasingFunction,
now: Instant,
) {
let is_zero = match &duration {
Duration::System(s) => s.secs == 0 && s.nanos == 0,
Duration::Tick(t) => t.tick_diff == 0,
};
if is_zero {
self.set_scroll_position(dom_id, node_id, target, now);
return;
}
let state = self
.states
.entry((dom_id, node_id))
.or_insert_with(|| AnimatedScrollState::new(now.clone()));
let clamped_target = state.clamp(target);
state.animation = Some(ScrollAnimation {
start_time: now.clone(),
duration,
start_offset: state.current_offset,
target_offset: clamped_target,
easing,
});
state.last_activity = now;
}
pub fn update_node_bounds(
&mut self,
dom_id: DomId,
node_id: NodeId,
container_rect: LogicalRect,
content_rect: LogicalRect,
now: Instant,
) {
let state = self
.states
.entry((dom_id, node_id))
.or_insert_with(|| AnimatedScrollState::new(now));
state.container_rect = container_rect;
state.content_rect = content_rect;
state.current_offset = state.clamp(state.current_offset);
}
pub fn update_virtual_scroll_bounds(
&mut self,
dom_id: DomId,
node_id: NodeId,
virtual_scroll_size: LogicalSize,
virtual_scroll_offset: Option<LogicalPosition>,
) {
let key = (dom_id, node_id);
let state = self.states.entry(key).or_insert_with(|| {
AnimatedScrollState::new(Instant::now())
});
state.virtual_scroll_size = Some(virtual_scroll_size);
state.virtual_scroll_offset = virtual_scroll_offset;
state.current_offset = state.clamp(state.current_offset);
}
#[must_use] pub fn get_current_offset(&self, dom_id: DomId, node_id: NodeId) -> Option<LogicalPosition> {
self.states
.get(&(dom_id, node_id))
.map(|s| s.current_offset)
}
#[must_use] pub fn get_last_activity_time(&self, dom_id: DomId, node_id: NodeId) -> Option<Instant> {
self.states
.get(&(dom_id, node_id))
.map(|s| s.last_activity.clone())
}
#[must_use] pub fn get_scroll_state(&self, dom_id: DomId, node_id: NodeId) -> Option<&AnimatedScrollState> {
self.states.get(&(dom_id, node_id))
}
#[must_use] pub fn get_scroll_node_info(
&self,
dom_id: DomId,
node_id: NodeId,
) -> Option<ScrollNodeInfo> {
let state = self.states.get(&(dom_id, node_id))?;
let effective_content_width = state.virtual_scroll_size
.map_or(state.content_rect.size.width, |s| s.width);
let effective_content_height = state.virtual_scroll_size
.map_or(state.content_rect.size.height, |s| s.height);
let max_x = (effective_content_width - state.container_rect.size.width).max(0.0);
let max_y = (effective_content_height - state.container_rect.size.height).max(0.0);
Some(ScrollNodeInfo {
current_offset: state.current_offset,
container_rect: state.container_rect,
content_rect: state.content_rect,
max_scroll_x: max_x,
max_scroll_y: max_y,
overscroll_behavior_x: state.overscroll_behavior_x,
overscroll_behavior_y: state.overscroll_behavior_y,
overflow_scrolling: state.overflow_scrolling,
})
}
#[must_use] pub fn get_scroll_states_for_dom(&self, dom_id: DomId) -> BTreeMap<NodeId, ScrollPosition> {
if self.states.is_empty() {
return BTreeMap::new();
}
self.states
.iter()
.filter(|((d, _), _)| *d == dom_id)
.map(|((_, node_id), state)| {
let effective_content_size = state.virtual_scroll_size
.unwrap_or(state.content_rect.size);
(
*node_id,
ScrollPosition {
parent_rect: state.container_rect,
children_rect: LogicalRect::new(
state.current_offset,
effective_content_size,
),
},
)
})
.collect()
}
pub fn register_or_update_scroll_node(
&mut self,
dom_id: DomId,
node_id: NodeId,
container_rect: LogicalRect,
content_size: LogicalSize,
now: Instant,
scrollbar_thickness: f32,
visual_width_px: f32,
has_horizontal_scrollbar: bool,
has_vertical_scrollbar: bool,
) {
let key = (dom_id, node_id);
let content_rect = LogicalRect {
origin: LogicalPosition::zero(),
size: content_size,
};
if let Some(existing) = self.states.get_mut(&key) {
existing.container_rect = container_rect;
existing.content_rect = content_rect;
existing.scrollbar_thickness = scrollbar_thickness;
existing.visual_width_px = visual_width_px;
existing.has_horizontal_scrollbar = has_horizontal_scrollbar;
existing.has_vertical_scrollbar = has_vertical_scrollbar;
existing.current_offset = existing.clamp(existing.current_offset);
} else {
self.states.insert(
key,
AnimatedScrollState {
current_offset: LogicalPosition::zero(),
animation: None,
last_activity: now,
container_rect,
content_rect,
virtual_scroll_size: None,
virtual_scroll_offset: None,
overscroll_behavior_x: azul_css::props::style::scrollbar::OverscrollBehavior::Auto,
overscroll_behavior_y: azul_css::props::style::scrollbar::OverscrollBehavior::Auto,
overflow_scrolling: azul_css::props::style::scrollbar::OverflowScrolling::Auto,
scrollbar_thickness,
visual_width_px,
has_horizontal_scrollbar,
has_vertical_scrollbar,
},
);
}
}
pub fn calculate_scrollbar_states(&mut self) {
self.scrollbar_states.clear();
for orientation in [ScrollbarOrientation::Vertical, ScrollbarOrientation::Horizontal] {
let states: Vec<_> = self
.states
.iter()
.filter(|(_, s)| {
let (effective, container) = match orientation {
ScrollbarOrientation::Vertical => (
s.virtual_scroll_size.map_or(s.content_rect.size.height, |vs| vs.height),
s.container_rect.size.height,
),
ScrollbarOrientation::Horizontal => (
s.virtual_scroll_size.map_or(s.content_rect.size.width, |vs| vs.width),
s.container_rect.size.width,
),
};
effective > container
})
.map(|((dom_id, node_id), scroll_state)| {
let state = Self::calculate_scrollbar_state_from_geometry(
scroll_state,
orientation,
);
((*dom_id, *node_id, orientation), state)
})
.collect();
self.scrollbar_states.extend(states);
}
}
fn calculate_scrollbar_state_from_geometry(
scroll_state: &AnimatedScrollState,
orientation: ScrollbarOrientation,
) -> ScrollbarState {
let scrollbar_thickness = if scroll_state.visual_width_px > 0.0 {
scroll_state.visual_width_px
} else if scroll_state.scrollbar_thickness > 0.0 {
scroll_state.scrollbar_thickness
} else {
crate::solver3::fc::DEFAULT_SCROLLBAR_WIDTH_PX
};
let content_size = scroll_state.virtual_scroll_size
.map_or(scroll_state.content_rect.size, |vs| vs);
let scroll_offset = match orientation {
ScrollbarOrientation::Vertical => scroll_state.current_offset.y,
ScrollbarOrientation::Horizontal => scroll_state.current_offset.x,
};
let has_other_scrollbar = match orientation {
ScrollbarOrientation::Vertical => scroll_state.has_horizontal_scrollbar,
ScrollbarOrientation::Horizontal => scroll_state.has_vertical_scrollbar,
};
let is_overlay = scroll_state.scrollbar_thickness == 0.0;
let button_size = if is_overlay { 0.0 } else { scrollbar_thickness };
let geom = compute_scrollbar_geometry_with_button_size(
orientation,
scroll_state.container_rect,
content_size,
scroll_offset,
scrollbar_thickness,
has_other_scrollbar,
button_size,
);
let scale = match orientation {
ScrollbarOrientation::Vertical => {
LogicalPosition::new(1.0, geom.track_rect.size.height / scrollbar_thickness)
}
ScrollbarOrientation::Horizontal => {
LogicalPosition::new(geom.track_rect.size.width / scrollbar_thickness, 1.0)
}
};
ScrollbarState {
visible: true,
orientation,
base_size: scrollbar_thickness,
scale,
thumb_position_ratio: geom.scroll_ratio,
thumb_size_ratio: geom.thumb_size_ratio,
track_rect: geom.track_rect,
button_size: geom.button_size,
usable_track_length: geom.usable_track_length,
thumb_length: geom.thumb_length,
thumb_offset: geom.thumb_offset,
}
}
#[must_use] pub fn get_scrollbar_state(
&self,
dom_id: DomId,
node_id: NodeId,
orientation: ScrollbarOrientation,
) -> Option<&ScrollbarState> {
self.scrollbar_states.get(&(dom_id, node_id, orientation))
}
pub(crate) fn iter_scrollbar_states(
&self,
) -> impl Iterator<Item = ((DomId, NodeId, ScrollbarOrientation), &ScrollbarState)> + '_ {
self.scrollbar_states.iter().map(|(k, v)| (*k, v))
}
pub(crate) fn hit_test_scrollbar(
&self,
dom_id: DomId,
node_id: NodeId,
global_pos: LogicalPosition,
) -> Option<ScrollbarHit> {
for orientation in [
ScrollbarOrientation::Vertical,
ScrollbarOrientation::Horizontal,
] {
let Some(scrollbar_state) = self.scrollbar_states.get(&(dom_id, node_id, orientation)) else {
continue;
};
if !scrollbar_state.visible {
continue;
}
if !scrollbar_state.track_rect.contains(global_pos) {
continue;
}
let local_pos = LogicalPosition::new(
global_pos.x - scrollbar_state.track_rect.origin.x,
global_pos.y - scrollbar_state.track_rect.origin.y,
);
let component = scrollbar_state.hit_test_component(local_pos);
return Some(ScrollbarHit {
dom_id,
node_id,
orientation,
component,
local_position: local_pos,
global_position: global_pos,
});
}
None
}
#[must_use] pub fn hit_test_scrollbars(&self, global_pos: LogicalPosition) -> Option<ScrollbarHit> {
for ((dom_id, node_id, orientation), scrollbar_state) in self.scrollbar_states.iter().rev()
{
if !scrollbar_state.visible {
continue;
}
if !scrollbar_state.track_rect.contains(global_pos) {
continue;
}
let local_pos = LogicalPosition::new(
global_pos.x - scrollbar_state.track_rect.origin.x,
global_pos.y - scrollbar_state.track_rect.origin.y,
);
let component = scrollbar_state.hit_test_component(local_pos);
return Some(ScrollbarHit {
dom_id: *dom_id,
node_id: *node_id,
orientation: *orientation,
component,
local_position: local_pos,
global_position: global_pos,
});
}
None
}
}
impl AnimatedScrollState {
pub(crate) const fn new(now: Instant) -> Self {
Self {
current_offset: LogicalPosition::zero(),
animation: None,
last_activity: now,
container_rect: LogicalRect::zero(),
content_rect: LogicalRect::zero(),
virtual_scroll_size: None,
virtual_scroll_offset: None,
overscroll_behavior_x: azul_css::props::style::scrollbar::OverscrollBehavior::Auto,
overscroll_behavior_y: azul_css::props::style::scrollbar::OverscrollBehavior::Auto,
overflow_scrolling: azul_css::props::style::scrollbar::OverflowScrolling::Auto,
scrollbar_thickness: crate::solver3::fc::DEFAULT_SCROLLBAR_WIDTH_PX,
visual_width_px: 0.0,
has_horizontal_scrollbar: false,
has_vertical_scrollbar: false,
}
}
pub(crate) fn clamp(&self, position: LogicalPosition) -> LogicalPosition {
let effective_width = self.virtual_scroll_size
.map_or(self.content_rect.size.width, |s| s.width);
let effective_height = self.virtual_scroll_size
.map_or(self.content_rect.size.height, |s| s.height);
let max_x = (effective_width - self.container_rect.size.width).max(0.0);
let max_y = (effective_height - self.container_rect.size.height).max(0.0);
LogicalPosition {
x: position.x.max(0.0).min(max_x),
y: position.y.max(0.0).min(max_y),
}
}
}
#[allow(clippy::suboptimal_flops)] pub(crate) fn apply_easing(t: f32, easing: EasingFunction) -> f32 {
match easing {
EasingFunction::Linear => t,
EasingFunction::EaseOut => 1.0 - (1.0 - t).powi(3),
EasingFunction::EaseInOut => {
if t < 0.5 {
4.0 * t * t * t
} else {
1.0 - (-2.0 * t + 2.0).powi(3) / 2.0
}
}
}
}
impl crate::managers::NodeIdRemap for ScrollManager {
fn remap_node_ids(&mut self, dom: DomId, map: &crate::managers::NodeIdMap) {
crate::managers::remap_dom_keys(&mut self.states, dom, map);
let old = core::mem::take(&mut self.scrollbar_states);
for ((d, old_node_id, orientation), state) in old {
if d != dom {
self.scrollbar_states
.insert((d, old_node_id, orientation), state);
} else if let Some(new_node_id) = map.resolve(old_node_id) {
self.scrollbar_states
.insert((d, new_node_id, orientation), state);
}
}
}
}
#[cfg(all(test, feature = "std"))]
mod natural_scroll_tests {
use super::*;
use azul_core::dom::{DomId, NodeId};
use azul_core::geom::LogicalPosition;
use azul_core::task::Instant;
fn raw_input(dx: f32, dy: f32) -> ScrollInput {
ScrollInput {
dom_id: DomId::ROOT_ID,
node_id: NodeId::new(0),
delta: LogicalPosition::new(dx, dy),
timestamp: Instant::from(std::time::Instant::now()),
source: ScrollInputSource::WheelDiscrete,
}
}
#[test]
#[allow(clippy::float_cmp)] fn default_is_traditional_and_inverts_raw_delta() {
let mut m = ScrollManager::new();
assert!(!m.is_natural_scroll(), "default must be traditional");
m.record_scroll_input(raw_input(3.0, 10.0));
let q = m.get_input_queue().take_all();
assert_eq!(q.len(), 1);
assert_eq!(q[0].delta.x, -3.0, "x must be inverted by the default sign");
assert_eq!(q[0].delta.y, -10.0, "y must be inverted by the default sign");
}
#[test]
#[allow(clippy::float_cmp)] fn natural_passes_raw_delta_through() {
let mut m = ScrollManager::new();
m.set_natural_scroll(true);
assert!(m.is_natural_scroll());
m.record_scroll_input(raw_input(3.0, 10.0));
let q = m.get_input_queue().take_all();
assert_eq!(q.len(), 1);
assert_eq!(q[0].delta.x, 3.0, "natural mode must NOT invert x");
assert_eq!(q[0].delta.y, 10.0, "natural mode must NOT invert y");
}
#[test]
#[allow(clippy::float_cmp)] fn toggling_flips_sign_for_subsequent_input() {
let mut m = ScrollManager::new();
m.record_scroll_input(raw_input(0.0, 5.0));
m.set_natural_scroll(true);
m.record_scroll_input(raw_input(0.0, 5.0));
let q = m.get_input_queue().take_all();
assert_eq!(q.len(), 2);
assert_eq!(q[0].delta.y, -5.0, "traditional first");
assert_eq!(q[1].delta.y, 5.0, "natural after toggle");
}
fn nested_setup() -> (ScrollManager, DomId, NodeId, NodeId) {
use azul_core::geom::{LogicalRect, LogicalSize};
let now = Instant::now();
let mut m = ScrollManager::new();
let dom = DomId::ROOT_ID;
let outer = NodeId::from_usize(1).unwrap();
let inner = NodeId::from_usize(9).unwrap();
m.register_or_update_scroll_node(
dom,
outer,
LogicalRect {
origin: LogicalPosition::zero(),
size: LogicalSize { width: 200.0, height: 200.0 },
},
LogicalSize { width: 200.0, height: 1000.0 },
now.clone(),
8.0,
8.0,
false,
true,
);
m.register_or_update_scroll_node(
dom,
inner,
LogicalRect {
origin: LogicalPosition::zero(),
size: LogicalSize { width: 100.0, height: 100.0 },
},
LogicalSize { width: 100.0, height: 300.0 },
now,
8.0,
8.0,
false,
true,
);
(m, dom, outer, inner)
}
#[test]
fn nested_scroll_prefers_innermost_with_room() {
let (m, dom, outer, inner) = nested_setup();
let picked = m.select_scroll_target(
[(dom, inner), (dom, outer)].into_iter(),
0.0,
1.0,
);
assert_eq!(picked, Some((dom, inner)), "inner has room → inner wins");
}
#[test]
fn nested_scroll_hands_off_to_ancestor_at_boundary() {
let (mut m, dom, outer, inner) = nested_setup();
m.states.get_mut(&(dom, inner)).unwrap().current_offset =
LogicalPosition { x: 0.0, y: 200.0 };
let down = m.select_scroll_target(
[(dom, inner), (dom, outer)].into_iter(),
0.0,
1.0,
);
assert_eq!(down, Some((dom, outer)), "inner pinned at bottom → handoff");
let up = m.select_scroll_target(
[(dom, inner), (dom, outer)].into_iter(),
0.0,
-1.0,
);
assert_eq!(up, Some((dom, inner)), "inner has room upward → inner again");
}
#[test]
fn nested_scroll_falls_back_to_innermost_when_all_pinned() {
let (mut m, dom, outer, inner) = nested_setup();
m.states.get_mut(&(dom, inner)).unwrap().current_offset =
LogicalPosition { x: 0.0, y: 200.0 };
m.states.get_mut(&(dom, outer)).unwrap().current_offset =
LogicalPosition { x: 0.0, y: 800.0 };
let picked = m.select_scroll_target(
[(dom, inner), (dom, outer)].into_iter(),
0.0,
1.0,
);
assert_eq!(
picked,
Some((dom, inner)),
"everything pinned → innermost fallback (gesture stays under pointer)"
);
}
}