From 79a348464e562d16e53552117631f591975a2855 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 1/4] 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 354abceb28..0d73e41b4b 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -289,6 +289,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, @@ -310,6 +311,7 @@ where is_focused: false, last_click: None, drag_click: None, + partial_scroll: 0.0, highlighter: RefCell::new(Highlighter::new( &self.highlighter_settings, )), @@ -404,6 +406,11 @@ where shell.publish(on_edit(action)); } + Update::Scroll(mut lines) => { + lines += state.partial_scroll; + state.partial_scroll = lines.fract(); + shell.publish(on_edit(Action::Scroll { lines: lines as i32 })) + } Update::Unfocus => { state.is_focused = false; state.drag_click = None; @@ -577,6 +584,7 @@ where enum Update { Click(mouse::Click), + Scroll(f32), Unfocus, Release, Action(Action), @@ -630,21 +638,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, }, From 066ee088d97acddbfae3d5cbe302cf17d23fcc2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 3 Feb 2024 14:15:40 +0100 Subject: [PATCH 2/4] Update `CHANGELOG` --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 739eae3cee..e4557f69fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Incorrect unit in `system::Information`. [#2223](https://github.com/iced-rs/iced/pull/2223) - `size_hint` not being called from `element::Map`. [#2224](https://github.com/iced-rs/iced/pull/2224) - `size_hint` not being called from `element::Explain`. [#2225](https://github.com/iced-rs/iced/pull/2225) +- Slow touch scrolling for `TextEditor` widget. [#2140](https://github.com/iced-rs/iced/pull/2140) Many thanks to... @@ -93,6 +94,7 @@ Many thanks to... - @arslee07 - @AustinMReppert - @avsaase +- @blazra - @brianch - @bungoboingo - @Calastrophe From db9ca1a147f2c57c362dbdb90cde56d1646dccc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 3 Feb 2024 14:18:11 +0100 Subject: [PATCH 3/4] Run `cargo fmt` --- widget/src/text_editor.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index 0d73e41b4b..edc2e386cd 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -409,7 +409,9 @@ where Update::Scroll(mut lines) => { lines += state.partial_scroll; state.partial_scroll = lines.fract(); - shell.publish(on_edit(Action::Scroll { lines: lines as i32 })) + shell.publish(on_edit(Action::Scroll { + lines: lines as i32, + })) } Update::Unfocus => { state.is_focused = false; From 719798441db735149018e2eab47f377692c29196 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Sat, 3 Feb 2024 14:19:08 +0100 Subject: [PATCH 4/4] Fix `clippy` lints --- widget/src/text_editor.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/widget/src/text_editor.rs b/widget/src/text_editor.rs index edc2e386cd..8d4319911b 100644 --- a/widget/src/text_editor.rs +++ b/widget/src/text_editor.rs @@ -406,12 +406,13 @@ where shell.publish(on_edit(action)); } - Update::Scroll(mut lines) => { - lines += state.partial_scroll; + Update::Scroll(lines) => { + let lines = lines + state.partial_scroll; state.partial_scroll = lines.fract(); + shell.publish(on_edit(Action::Scroll { lines: lines as i32, - })) + })); } Update::Unfocus => { state.is_focused = false;