2020-11-02 21:32:16 +03:00
|
|
|
use mux::pane::Pane;
|
2020-06-13 19:04:13 +03:00
|
|
|
use wezterm_term::StableRowIndex;
|
2019-12-27 22:37:45 +03:00
|
|
|
|
2021-08-15 23:04:26 +03:00
|
|
|
pub struct ScrollHit {
|
2019-12-27 22:37:45 +03:00
|
|
|
/// Offset from the top of the window in pixels
|
|
|
|
pub top: usize,
|
|
|
|
/// Height of the thumb, in pixels.
|
|
|
|
pub height: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ScrollHit {
|
|
|
|
/// Compute the y-coordinate for the top of the scrollbar thumb
|
|
|
|
/// and the height of the thumb and return them.
|
2021-10-04 22:04:19 +03:00
|
|
|
pub fn thumb(
|
|
|
|
pane: &dyn Pane,
|
|
|
|
viewport: Option<StableRowIndex>,
|
2022-03-29 14:58:36 +03:00
|
|
|
max_thumb_height: usize,
|
|
|
|
min_thumb_size: usize,
|
2021-10-04 22:04:19 +03:00
|
|
|
) -> Self {
|
2020-11-02 21:32:16 +03:00
|
|
|
let render_dims = pane.get_dimensions();
|
2020-01-03 19:57:32 +03:00
|
|
|
|
|
|
|
let scroll_top = render_dims
|
|
|
|
.physical_top
|
2022-03-29 14:58:36 +03:00
|
|
|
.saturating_sub(viewport.unwrap_or(render_dims.physical_top))
|
|
|
|
as f32;
|
2020-01-03 19:57:32 +03:00
|
|
|
|
2021-08-15 23:04:26 +03:00
|
|
|
let scroll_size = render_dims.scrollback_rows as f32;
|
2020-01-03 07:27:35 +03:00
|
|
|
|
2022-03-29 14:58:36 +03:00
|
|
|
let thumb_size = (render_dims.viewport_rows as f32 / scroll_size) * max_thumb_height as f32;
|
2019-12-27 22:37:45 +03:00
|
|
|
|
2022-03-29 14:58:36 +03:00
|
|
|
let min_thumb_size = min_thumb_size as f32;
|
|
|
|
let thumb_size = if thumb_size < min_thumb_size {
|
|
|
|
min_thumb_size
|
2019-12-27 22:37:45 +03:00
|
|
|
} else {
|
2022-03-29 14:58:36 +03:00
|
|
|
thumb_size
|
|
|
|
}
|
|
|
|
.ceil() as usize;
|
2019-12-27 22:37:45 +03:00
|
|
|
|
2022-03-29 14:58:36 +03:00
|
|
|
let scroll_percent = 1.0 - (scroll_top / render_dims.physical_top as f32);
|
|
|
|
let thumb_top = (scroll_percent * (max_thumb_height - thumb_size) as f32).ceil() as usize;
|
2019-12-27 22:37:45 +03:00
|
|
|
|
2021-08-15 23:04:26 +03:00
|
|
|
Self {
|
2019-12-27 22:37:45 +03:00
|
|
|
top: thumb_top,
|
|
|
|
height: thumb_size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 19:57:32 +03:00
|
|
|
/// Given a new thumb top coordinate (produced by dragging the thumb),
|
2019-12-27 22:37:45 +03:00
|
|
|
/// compute the equivalent viewport offset.
|
|
|
|
pub fn thumb_top_to_scroll_top(
|
|
|
|
thumb_top: usize,
|
2020-11-02 21:32:16 +03:00
|
|
|
pane: &dyn Pane,
|
2020-01-03 19:57:32 +03:00
|
|
|
viewport: Option<StableRowIndex>,
|
2022-03-29 14:58:36 +03:00
|
|
|
max_thumb_height: usize,
|
|
|
|
min_thumb_size: usize,
|
2020-01-03 19:57:32 +03:00
|
|
|
) -> StableRowIndex {
|
2022-03-29 14:58:36 +03:00
|
|
|
let thumb = Self::thumb(pane, viewport, max_thumb_height, min_thumb_size);
|
|
|
|
let available_height = max_thumb_height - thumb.height;
|
|
|
|
let scroll_percent = thumb_top.min(available_height) as f32 / available_height as f32;
|
2020-01-03 19:57:32 +03:00
|
|
|
|
2022-03-29 14:58:36 +03:00
|
|
|
let render_dims = pane.get_dimensions();
|
2019-12-27 22:37:45 +03:00
|
|
|
|
2020-01-03 19:57:32 +03:00
|
|
|
render_dims
|
|
|
|
.scrollback_top
|
2022-03-29 14:58:36 +03:00
|
|
|
.saturating_add((render_dims.physical_top as f32 * scroll_percent) as StableRowIndex)
|
2019-12-27 22:37:45 +03:00
|
|
|
}
|
|
|
|
}
|