From ee32af2606e7c24cbe9e1c221af0ed1a2c44be50 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Wed, 12 Apr 2023 10:32:35 -0700 Subject: [PATCH 1/2] Don't publish redundant on_scroll offsets --- native/src/widget/scrollable.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index d9cdf29618..23fa20f4aa 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -895,7 +895,7 @@ pub fn draw( } fn notify_on_scroll( - state: &State, + state: &mut State, on_scroll: &Option Message + '_>>, bounds: Rectangle, content_bounds: Rectangle, @@ -916,7 +916,21 @@ fn notify_on_scroll( .absolute(bounds.height, content_bounds.height) / (content_bounds.height - bounds.height); - shell.publish(on_scroll(RelativeOffset { x, y })) + let new_offset = RelativeOffset { x, y }; + + // Don't publish redundant offsets to shell + if let Some(prev_offset) = state.last_notified { + let unchanged = |a: f32, b: f32| (a - b).abs() <= f32::EPSILON; + + if unchanged(prev_offset.x, new_offset.x) + && unchanged(prev_offset.y, new_offset.y) + { + return; + } + } + + shell.publish(on_scroll(new_offset)); + state.last_notified = Some(new_offset); } } @@ -929,6 +943,7 @@ pub struct State { offset_x: Offset, x_scroller_grabbed_at: Option, keyboard_modifiers: keyboard::Modifiers, + last_notified: Option, } impl Default for State { @@ -940,6 +955,7 @@ impl Default for State { offset_x: Offset::Absolute(0.0), x_scroller_grabbed_at: None, keyboard_modifiers: keyboard::Modifiers::default(), + last_notified: None, } } } From 4125c034f5c512365c6ef73678dc0801db79b249 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Wed, 12 Apr 2023 12:47:24 -0700 Subject: [PATCH 2/2] Include NaN in unchaged logic --- native/src/widget/scrollable.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/native/src/widget/scrollable.rs b/native/src/widget/scrollable.rs index 23fa20f4aa..c0590b1eb5 100644 --- a/native/src/widget/scrollable.rs +++ b/native/src/widget/scrollable.rs @@ -920,7 +920,9 @@ fn notify_on_scroll( // Don't publish redundant offsets to shell if let Some(prev_offset) = state.last_notified { - let unchanged = |a: f32, b: f32| (a - b).abs() <= f32::EPSILON; + let unchanged = |a: f32, b: f32| { + (a - b).abs() <= f32::EPSILON || (a.is_nan() && b.is_nan()) + }; if unchanged(prev_offset.x, new_offset.x) && unchanged(prev_offset.y, new_offset.y)