From 18f2ca563f0cd17f6e7c8f287ad75586fe0fb2e9 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 13 Nov 2023 09:49:52 -0700 Subject: [PATCH] Implement touchpad scroll --- Cargo.lock | 8 ++++---- src/text_box.rs | 30 ++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 29c9385..80ae01f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -258,15 +258,15 @@ dependencies = [ [[package]] name = "async-executor" -version = "1.7.1" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0de517d5a758a65a16d18d8f605e7a6beed477444cca270116af40fd3cd59d27" +checksum = "4b0c4a4f319e45986f347ee47fef8bf5e81c9abc3f6f58dc2391439f30df65f0" dependencies = [ - "async-lock 3.1.0", + "async-lock 2.8.0", "async-task", "concurrent-queue", "fastrand 2.0.1", - "futures-lite 2.0.1", + "futures-lite 1.13.0", "slab", ] diff --git a/src/text_box.rs b/src/text_box.rs index 7797192..afa1fb9 100644 --- a/src/text_box.rs +++ b/src/text_box.rs @@ -533,12 +533,32 @@ where } Event::Mouse(MouseEvent::WheelScrolled { delta }) => match delta { ScrollDelta::Lines { x, y } => { - editor.action(Action::Scroll { - lines: (-y * 6.0) as i32, - }); + //TODO: this adjustment is just a guess! + state.scroll_pixels = 0.0; + let lines = (-y * 6.0) as i32; + if lines != 0 { + editor.action(Action::Scroll { lines }); + } + status = Status::Captured; + } + ScrollDelta::Pixels { x, y } => { + //TODO: this adjustment is just a guess! + state.scroll_pixels -= y * 6.0; + let mut lines = 0; + let metrics = editor.buffer().metrics(); + while state.scroll_pixels <= -metrics.line_height { + lines -= 1; + state.scroll_pixels += metrics.line_height; + } + while state.scroll_pixels >= metrics.line_height { + lines += 1; + state.scroll_pixels -= metrics.line_height; + } + if lines != 0 { + editor.action(Action::Scroll { lines }); + } status = Status::Captured; } - _ => (), }, _ => (), } @@ -566,6 +586,7 @@ pub struct State { modifiers: Modifiers, dragging: Option, scale_factor: Cell, + scroll_pixels: f32, scrollbar_rect: Cell>, handle: Mutex, } @@ -577,6 +598,7 @@ impl State { modifiers: Modifiers::empty(), dragging: None, scale_factor: Cell::new(1.0), + scroll_pixels: 0.0, scrollbar_rect: Cell::new(Rectangle::default()), //TODO: make option! handle: Mutex::new(image::Handle::from_pixels(1, 1, vec![0, 0, 0, 0])),