From 6e7ba0230eb0105ea7b08d1632386fc33406c5c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Radovan=20Bla=C5=BEek?= Date: Fri, 17 Nov 2023 16:52:26 +0100 Subject: [PATCH] Improve `TextEditor` slow scrolling behavior with touchpads. If you scroll by only a fraction of a line, the TextEditor stores this fraction and adds it on the next scroll event. --- widget/src/text_editor.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 1708a2e5ce..308c4f72a2 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -278,6 +278,7 @@ struct State { is_focused: bool, last_click: Option, drag_click: Option, + partial_scroll: f32, highlighter: RefCell, highlighter_settings: Highlighter::Settings, highlighter_format_address: usize, @@ -299,6 +300,7 @@ where is_focused: false, last_click: None, drag_click: None, + partial_scroll: 0.0, highlighter: RefCell::new(Highlighter::new( &self.highlighter_settings, )), @@ -394,6 +396,11 @@ where shell.publish(on_edit(action)); } + Update::Scroll(lines) => { + let l = state.partial_scroll + lines; + state.partial_scroll = l.fract(); + shell.publish(on_edit(Action::Scroll { lines: l as i32 })) + } Update::Unfocus => { state.is_focused = false; state.drag_click = None; @@ -565,6 +572,7 @@ where enum Update { Click(mouse::Click), + Scroll(f32), Unfocus, Release, Action(Action), @@ -617,21 +625,16 @@ impl Update { mouse::Event::WheelScrolled { delta } if cursor.is_over(bounds) => { - action(Action::Scroll { - lines: match delta { - mouse::ScrollDelta::Lines { y, .. } => { - if y.abs() > 0.0 { - (y.signum() * -(y.abs() * 4.0).max(1.0)) - as i32 - } else { - 0 - } - } - mouse::ScrollDelta::Pixels { y, .. } => { - (-y / 4.0) as i32 + Some(Update::Scroll(match delta { + mouse::ScrollDelta::Lines { y, .. } => { + if y.abs() > 0.0 { + y.signum() * -(y.abs() * 4.0).max(1.0) + } else { + 0.0 } - }, - }) + } + mouse::ScrollDelta::Pixels { y, .. } => -y / 4.0, + })) } _ => None, },