From 038493cada96da39bca04be1ab9b76484c34d6ee Mon Sep 17 00:00:00 2001 From: Lu Jing Date: Mon, 30 Nov 2026 21:31:29 +0800 Subject: [PATCH] fix: keep scrolled text visible after resize --- CHANGELOG.md | 1 + src/components/utils/scroll_vertical.rs | 43 +++++++++++++++++++------ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1ca0eaac2..ff21795d5e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * crash when opening submodule ([#2895](https://github.com/gitui-org/gitui/issues/2895)) * when staging the last file in a directory, the first item after the directory is no longer skipped [[@Tillerino](https://github.com/Tillerino)] ([#2748](https://github.com/gitui-org/gitui/issues/2748)) * index-out-of-bounds panic when unstaging lines near the end of a diff ([#2953](https://github.com/gitui-org/gitui/issues/2953)) +* keep scrolled text visible when terminal resizing changes wrapped line counts [[@jlu-lujing](https://github.com/jlu-lujing)] ([#2149](https://github.com/gitui-org/gitui/issues/2149)) ## [0.28.1] - 2026-03-21 diff --git a/src/components/utils/scroll_vertical.rs b/src/components/utils/scroll_vertical.rs index 1f7ed77918..cf0289235c 100644 --- a/src/components/utils/scroll_vertical.rs +++ b/src/components/utils/scroll_vertical.rs @@ -90,21 +90,23 @@ impl VerticalScroll { ) -> usize { self.visual_height.set(visual_height); + let new_max = if visual_height == 0 { + 0 + } else { + selection_max.saturating_sub(visual_height) + }; + self.max_top.set(new_max); + + let current_top = self.top.get().min(new_max); let new_top = calc_scroll_top( - self.get_top(), + current_top, visual_height, selection, selection_max, - ); + ) + .min(new_max); self.top.set(new_top); - if visual_height == 0 { - self.max_top.set(0); - } else { - let new_max = selection_max.saturating_sub(visual_height); - self.max_top.set(new_max); - } - new_top } @@ -165,6 +167,29 @@ mod tests { assert_eq!(calc_scroll_top(4, 0, 4, 3), 0); } + #[test] + fn test_scroll_clamps_top_when_content_reflows_shorter() { + let scroll = VerticalScroll::new(); + scroll.max_top.set(20); + scroll.top.set(20); + + assert_eq!(scroll.update_no_selection(20, 10), 10); + assert_eq!(scroll.get_top(), 10); + + assert_eq!(scroll.update_no_selection(12, 10), 2); + assert_eq!(scroll.get_top(), 2); + } + + #[test] + fn test_scroll_clamps_top_when_viewport_grows() { + let scroll = VerticalScroll::new(); + scroll.max_top.set(10); + scroll.top.set(10); + + assert_eq!(scroll.update_no_selection(20, 15), 5); + assert_eq!(scroll.get_top(), 5); + } + #[test] fn test_scroll_bottom_into_view() { let visual_height = 10;