From 4f8c4ccd5dce6830b837bf114ac8fd8e13e5e599 Mon Sep 17 00:00:00 2001 From: xda <150917089+xda2023@users.noreply.github.com> Date: Wed, 11 Sep 2024 09:37:07 +0800 Subject: [PATCH] scrollbar: Keep the original thumb position when mouse down to dragging (#234) ![2024-09-10 23 18 50](https://github.com/user-attachments/assets/58c480a3-9c92-4c7c-8e7a-b37c2ffc3526) fix #224 --- crates/ui/src/scroll/scrollbar.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/ui/src/scroll/scrollbar.rs b/crates/ui/src/scroll/scrollbar.rs index 679e1aaa..067fe324 100644 --- a/crates/ui/src/scroll/scrollbar.rs +++ b/crates/ui/src/scroll/scrollbar.rs @@ -522,19 +522,25 @@ impl Element for Scrollbar { // We need to keep the thumb bar still at the origin down position let drag_pos = state.get().drag_pos; - let percentage = if is_vertical { + let percentage = (if is_vertical { (event.position.y - drag_pos.y - bounds.origin.y) / (bounds.size.height - thumb_length) } else { (event.position.x - drag_pos.x - bounds.origin.x) - / (bounds.size.width - thumb_length) - } - .min(1.); + / (bounds.size.width - thumb_length - margin_end) + }) + .clamp(0., 1.); let offset = if is_vertical { - point(scroll_handle.offset().x, -scroll_area_size * percentage) + point( + scroll_handle.offset().x, + -(scroll_area_size - container_size) * percentage, + ) } else { - point(-scroll_area_size * percentage, scroll_handle.offset().y) + point( + -(scroll_area_size - container_size) * percentage, + scroll_handle.offset().y, + ) }; scroll_handle.set_offset(offset);