Compute a new scroll position after applying `delta`, clamped so the last viewport of content remains visible. `current` - current scroll offset `delta` - lines to scroll (positive = down, negative = up) `total` - total number of lines/items `viewport` - visible line count (0 before first draw, treated as 1)
(current: usize, delta: isize, total: usize, viewport: usize)
| 2818 | /// * `total` - total number of lines/items |
| 2819 | /// * `viewport` - visible line count (0 before first draw, treated as 1) |
| 2820 | fn clamped_scroll(current: usize, delta: isize, total: usize, viewport: usize) -> usize { |
| 2821 | let max = total.saturating_sub(viewport.max(1)); |
| 2822 | if delta < 0 { |
| 2823 | current.saturating_sub(delta.unsigned_abs()) |
| 2824 | } else { |
| 2825 | #[allow(clippy::cast_sign_loss)] |
| 2826 | let stepped = current + delta as usize; |
| 2827 | stepped.min(max) |
| 2828 | } |
| 2829 | } |
| 2830 | |
| 2831 | #[cfg(test)] |
| 2832 | mod tests { |