diff --git a/src/frontend/gui/scrollbar.rs b/src/frontend/gui/scrollbar.rs index f78a4a2f1..a3295259e 100644 --- a/src/frontend/gui/scrollbar.rs +++ b/src/frontend/gui/scrollbar.rs @@ -1,7 +1,7 @@ use crate::mux::renderable::Renderable; use ::window::*; use portable_pty::PtySize; -use term::VisibleRowIndex; +use term::StableRowIndex; pub enum ScrollHit { Above, @@ -26,8 +26,14 @@ impl ScrollHit { /// Given a mouse y value, determine whether the cursor is above, over /// or below the thumb. /// If above the thumb, return the offset from the top of the thumb. - pub fn test(y: isize, render: &dyn Renderable, size: PtySize, dims: &Dimensions) -> Self { - let info = Self::thumb(render, size, dims); + pub fn test( + y: isize, + render: &dyn Renderable, + viewport: Option, + size: PtySize, + dims: &Dimensions, + ) -> Self { + let info = Self::thumb(render, viewport, size, dims); if y < info.top as isize { Self::Above } else if y < (info.top + info.height) as isize { @@ -39,9 +45,18 @@ impl ScrollHit { /// Compute the y-coordinate for the top of the scrollbar thumb /// and the height of the thumb and return them. - pub fn thumb(render: &dyn Renderable, size: PtySize, dims: &Dimensions) -> ThumbInfo { + pub fn thumb( + render: &dyn Renderable, + viewport: Option, + size: PtySize, + dims: &Dimensions, + ) -> ThumbInfo { let render_dims = render.get_dimensions(); - let scroll_top = render_dims.viewport_offset; + + let scroll_top = render_dims + .physical_top + .saturating_sub(viewport.unwrap_or(render_dims.physical_top)); + let scroll_size = render_dims.scrollback_rows; let thumb_size = (size.rows as f32 / scroll_size as f32) * dims.pixel_height as f32; @@ -55,7 +70,7 @@ impl ScrollHit { (thumb_size, size.rows as usize) }; - let thumb_top = (1. - (scroll_top + rows as i64) as f32 / scroll_size as f32) + let thumb_top = (1. - (scroll_top + rows as StableRowIndex) as f32 / scroll_size as f32) * size.pixel_height as f32; let thumb_size = thumb_size.ceil() as usize; @@ -68,20 +83,23 @@ impl ScrollHit { } } - /// Given a new thumb top coordinate (produced by draggin the thumb), + /// Given a new thumb top coordinate (produced by dragging the thumb), /// compute the equivalent viewport offset. pub fn thumb_top_to_scroll_top( thumb_top: usize, render: &dyn Renderable, + viewport: Option, size: PtySize, dims: &Dimensions, - ) -> VisibleRowIndex { + ) -> StableRowIndex { let render_dims = render.get_dimensions(); let scroll_size = render_dims.scrollback_rows; - let thumb = Self::thumb(render, size, dims); + let thumb = Self::thumb(render, viewport, size, dims); - let rows_from_top = - ((thumb_top as f32 + thumb.height as f32) / thumb.height as f32) * thumb.rows as f32; - scroll_size.saturating_sub(rows_from_top as usize) as VisibleRowIndex + let rows_from_top = ((thumb_top as f32) / thumb.height as f32) * thumb.rows as f32; + + render_dims + .scrollback_top + .saturating_add(rows_from_top as StableRowIndex) } } diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index 5a2899eb9..bbd7f0ef7 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -19,6 +19,7 @@ use ::window::*; use anyhow::{anyhow, bail, ensure}; use portable_pty::PtySize; use std::any::Any; +use std::collections::HashMap; use std::convert::TryInto; use std::ops::Range; use std::ops::{Add, Sub}; @@ -27,7 +28,7 @@ use std::sync::Arc; use std::sync::Mutex; use std::time::{Duration, Instant}; use term::color::ColorPalette; -use term::{CursorPosition, Line, Underline}; +use term::{CursorPosition, Line, StableRowIndex, Underline}; use termwiz::color::RgbColor; use termwiz::surface::CursorShape; @@ -100,6 +101,14 @@ impl PrevCursorPos { } } +#[derive(Debug, Default)] +struct TabState { + /// If is_some(), the top row of the visible screen. + /// Otherwise, the viewport is at the bottom of the + /// scrollback. + viewport: Option, +} + pub struct TermWindow { window: Option, /// When we most recently received keyboard focus @@ -122,6 +131,8 @@ pub struct TermWindow { prev_cursor: PrevCursorPos, last_scroll_info: RenderableDimensions, + tab_state: HashMap, + /// Gross workaround for managing async keyboard fetching /// just for middle mouse button paste function clipboard_contents: Arc>>, @@ -233,15 +244,18 @@ impl WindowCallbacks for TermWindow { // adjust viewport let mut render = tab.renderer(); let dims = render.get_dimensions(); - render.set_viewport_position( - dims.viewport_offset - .saturating_add(amount.into()) - .min(dims.scrollback_rows.try_into().unwrap()), - ); + let position = self + .get_viewport(tab.tab_id()) + .unwrap_or(dims.physical_top) + .saturating_sub(amount.into()); + self.set_viewport(tab.tab_id(), Some(position), dims); + render.make_all_lines_dirty(); + context.invalidate(); return; } WMEK::Move => { + let current_viewport = self.get_viewport(tab.tab_id()); if let Some(from_top) = self.scroll_drag_start.as_ref() { // Dragging the scroll bar let mux = Mux::get().unwrap(); @@ -251,6 +265,7 @@ impl WindowCallbacks for TermWindow { }; let mut render = tab.renderer(); + let dims = render.get_dimensions(); let effective_thumb_top = event.coords.y.saturating_sub(*from_top).max(0) as usize; @@ -260,10 +275,13 @@ impl WindowCallbacks for TermWindow { let row = ScrollHit::thumb_top_to_scroll_top( effective_thumb_top, &*render, + current_viewport, self.terminal_size, &self.dimensions, ); - render.set_viewport_position(row); + self.set_viewport(tab.tab_id(), Some(row), dims); + render.make_all_lines_dirty(); + context.invalidate(); return; } } @@ -306,27 +324,42 @@ impl WindowCallbacks for TermWindow { if let WMEK::Press(MousePress::Left) = event.kind { let mut render = tab.renderer(); let dims = render.get_dimensions(); + let current_viewport = self.get_viewport(tab.tab_id()); match ScrollHit::test( event.coords.y, &*render, + current_viewport, self.terminal_size, &self.dimensions, ) { ScrollHit::Above => { // Page up - render.set_viewport_position( - dims.viewport_offset - .saturating_add(self.terminal_size.rows as i64) - .min(dims.scrollback_rows.try_into().unwrap()), + self.set_viewport( + tab.tab_id(), + Some( + current_viewport + .unwrap_or(dims.physical_top) + .saturating_sub(self.terminal_size.rows.try_into().unwrap()), + ), + dims, ); + render.make_all_lines_dirty(); + context.invalidate(); } ScrollHit::Below => { // Page down - render.set_viewport_position( - dims.viewport_offset - .saturating_sub(self.terminal_size.rows as i64), + self.set_viewport( + tab.tab_id(), + Some( + current_viewport + .unwrap_or(dims.physical_top) + .saturating_add(self.terminal_size.rows.try_into().unwrap()), + ), + dims, ); + render.make_all_lines_dirty(); + context.invalidate(); } ScrollHit::OnThumb(from_top) => { // Start a scroll drag @@ -654,6 +687,7 @@ impl TermWindow { prev_cursor: PrevCursorPos::new(), last_scroll_info: RenderableDimensions::default(), clipboard_contents: Arc::clone(&clipboard_contents), + tab_state: HashMap::new(), }), )?; @@ -1355,24 +1389,35 @@ impl TermWindow { } }; self.prev_cursor.update(&cursor); + let current_viewport = self.get_viewport(tab.tab_id()); if self.show_tab_bar { self.render_screen_line(ctx, 0, self.tab_bar.line(), 0..0, &cursor, &*term, &palette)?; } { - let dirty_lines = term.get_dirty_lines(); + let dims = term.get_dimensions(); + let stable_range = match current_viewport { + Some(top) => top..top + dims.viewport_rows as StableRowIndex, + None => dims.physical_top..dims.physical_top + dims.viewport_rows as StableRowIndex, + }; - for (line_idx, line, selrange) in dirty_lines { - self.render_screen_line( - ctx, - line_idx + first_line_offset, - &line, - selrange, - &cursor, - &*term, - &palette, - )?; + let (stable_top, lines) = term.get_lines(stable_range); + + for (line_idx, line) in lines.iter().enumerate() { + if line.is_dirty() { + let selrange = 0..0; // FIXME: local selection + + self.render_screen_line( + ctx, + line_idx + first_line_offset, + &line, + selrange, + &cursor, // FIXME: cursor offset when scrolling + &*term, + &palette, + )?; + } } } @@ -1440,7 +1485,13 @@ impl TermWindow { ); if self.show_scroll_bar { - let info = ScrollHit::thumb(&*term, self.terminal_size, &self.dimensions); + let current_viewport = self.get_viewport(tab.tab_id()); + let info = ScrollHit::thumb( + &*term, + current_viewport, + self.terminal_size, + &self.dimensions, + ); let thumb_size = info.height as isize; let thumb_top = info.top as isize; @@ -1492,6 +1543,21 @@ impl TermWindow { }; self.prev_cursor.update(&cursor); + let current_viewport = self.get_viewport(tab.tab_id()); + let (stable_top, lines); + + { + let dims = term.get_dimensions(); + let stable_range = match current_viewport { + Some(top) => top..top + dims.viewport_rows as StableRowIndex, + None => dims.physical_top..dims.physical_top + dims.viewport_rows as StableRowIndex, + }; + + let (top, vp_lines) = term.get_lines(stable_range); + stable_top = top; + lines = vp_lines; + } + let gl_state = self.render_state.opengl(); let mut vb = gl_state.glyph_vertex_buffer.borrow_mut(); let mut quads = gl_state.quads.map(&mut vb); @@ -1510,7 +1576,12 @@ impl TermWindow { { let (thumb_top, thumb_size, color) = if self.show_scroll_bar { - let info = ScrollHit::thumb(&*term, self.terminal_size, &self.dimensions); + let info = ScrollHit::thumb( + &*term, + current_viewport, + self.terminal_size, + &self.dimensions, + ); let thumb_top = info.top as f32; let thumb_size = info.height as f32; let color = rgbcolor_to_window_color(palette.scrollbar_thumb); @@ -1545,15 +1616,15 @@ impl TermWindow { quad.set_cursor_color(rgbcolor_to_window_color(background_color)); } - { - let dirty_lines = term.get_dirty_lines(); + for (line_idx, line) in lines.iter().enumerate() { + if line.is_dirty() { + let selrange = 0..0; // FIXME: local selection - for (line_idx, line, selrange) in dirty_lines { self.render_screen_line_opengl( line_idx + first_line_offset, &line, selrange, - &cursor, + &cursor, // FIXME: cursor offset when scrolling &*term, &palette, &mut quads, @@ -2237,6 +2308,36 @@ impl TermWindow { (fg_color, bg_color, cursor_shape) } + + fn tab_state(&mut self, tab_id: TabId) -> &mut TabState { + self.tab_state + .entry(tab_id) + .or_insert_with(TabState::default) + } + + fn get_viewport(&mut self, tab_id: TabId) -> Option { + self.tab_state(tab_id).viewport.clone() + } + + fn set_viewport( + &mut self, + tab_id: TabId, + position: Option, + dims: RenderableDimensions, + ) { + let pos = match position { + Some(pos) => { + // Drop out of scrolling mode if we're off the bottom + if pos >= dims.physical_top { + None + } else { + Some(pos.max(0)) + } + } + None => None, + }; + self.tab_state(tab_id).viewport = pos; + } } fn rgbcolor_to_window_color(color: RgbColor) -> Color { diff --git a/src/mux/renderable.rs b/src/mux/renderable.rs index 88791b92e..1128d63e1 100644 --- a/src/mux/renderable.rs +++ b/src/mux/renderable.rs @@ -2,9 +2,7 @@ use downcast_rs::{impl_downcast, Downcast}; use std::borrow::Cow; use std::ops::Range; use std::sync::Arc; -use term::{ - CursorPosition, Line, ScrollbackOrVisibleRowIndex, Terminal, TerminalState, VisibleRowIndex, -}; +use term::{CursorPosition, Line, StableRowIndex, Terminal, TerminalState, VisibleRowIndex}; use termwiz::hyperlink::Hyperlink; #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] @@ -18,6 +16,14 @@ pub struct RenderableDimensions { /// The offset is measured from the top of the physical viewable /// screen with larger numbers going backwards. pub viewport_offset: VisibleRowIndex, + + /// The top of the physical, non-scrollback, screen expressed + /// as a stable index. It is envisioned that this will be used + /// to compute row/cols for mouse events and to produce a range + /// for the `get_lines` call when the scroll position is at the + /// bottom of the screen. + pub physical_top: StableRowIndex, + pub scrollback_top: StableRowIndex, } /// Renderable allows passing something that isn't an actual term::Terminal @@ -36,8 +42,17 @@ pub trait Renderable: Downcast { fn get_dirty_lines(&self) -> Vec<(usize, Cow, Range)>; /// Returns a set of lines from the scrollback or visible portion of - /// the display. - fn get_lines(&self, lines: Range) -> Vec>; + /// the display. The lines are indexed using StableRowIndex, which + /// can be invalidated if the scrollback is busy, or when switching + /// to the alternate screen. + /// To deal with this, this function will adjust the input so that + /// a range that has been scrolled off the top will return the top + /// n rows of the scrollback (where n is the size of the input range), + /// or the bottom n rows of the scrollback when switching to the alt + /// screen and the index would go off the bottom. + /// Because of this, we also return the adjusted StableRowIndex for + /// the first row in the range. + fn get_lines(&self, lines: Range) -> (StableRowIndex, Vec>); fn has_dirty_lines(&self) -> bool; @@ -68,16 +83,19 @@ impl Renderable for Terminal { .collect() } - fn get_lines(&self, lines: Range) -> Vec> { + fn get_lines(&self, lines: Range) -> (StableRowIndex, Vec>) { let screen = self.screen(); - let phys_range = screen.scrollback_or_visible_range(&lines); - screen - .lines - .iter() - .skip(phys_range.start) - .take(phys_range.end - phys_range.start) - .map(|line| Cow::Borrowed(line)) - .collect() + let phys_range = screen.stable_range(&lines); + ( + screen.phys_to_stable_row_index(phys_range.start), + screen + .lines + .iter() + .skip(phys_range.start) + .take(phys_range.end - phys_range.start) + .map(|line| Cow::Borrowed(line)) + .collect(), + ) } fn clean_dirty_lines(&mut self) { @@ -99,6 +117,8 @@ impl Renderable for Terminal { viewport_rows: screen.physical_rows, scrollback_rows: screen.lines.len(), viewport_offset: self.get_viewport_offset(), + physical_top: screen.visible_row_to_stable_row(0), + scrollback_top: screen.phys_to_stable_row_index(0), } } diff --git a/src/server/tab.rs b/src/server/tab.rs index b7659b8b5..86b27cbc2 100644 --- a/src/server/tab.rs +++ b/src/server/tab.rs @@ -24,7 +24,7 @@ use term::color::ColorPalette; use term::selection::SelectionRange; use term::{ Clipboard, CursorPosition, KeyCode, KeyModifiers, Line, MouseButton, MouseEvent, - MouseEventKind, ScrollbackOrVisibleRowIndex, TerminalHost, VisibleRowIndex, + MouseEventKind, StableRowIndex, TerminalHost, VisibleRowIndex, }; use termwiz::hyperlink::Hyperlink; use termwiz::input::KeyEvent; @@ -419,16 +419,19 @@ impl Renderable for RenderableState { } } - fn get_lines(&self, lines: Range) -> Vec> { + fn get_lines(&self, lines: Range) -> (StableRowIndex, Vec>) { let inner = self.inner.borrow_mut(); - inner - .surface - .screen_lines() - .into_iter() - .skip(lines.start.try_into().unwrap()) - .take((lines.end - lines.start).try_into().unwrap()) - .map(|line| Cow::Owned(line.into_owned())) - .collect() + ( + lines.start, + inner + .surface + .screen_lines() + .into_iter() + .skip(lines.start.try_into().unwrap()) + .take((lines.end - lines.start).try_into().unwrap()) + .map(|line| Cow::Owned(line.into_owned())) + .collect(), + ) } fn get_dirty_lines(&self) -> Vec<(usize, Cow, Range)> { @@ -497,6 +500,8 @@ impl Renderable for RenderableState { cols, scrollback_rows: 0, viewport_offset: 0, + physical_top: 0, + scrollback_top: 0, } } diff --git a/src/termwiztermtab.rs b/src/termwiztermtab.rs index c4bfce2a5..8f8d6a850 100644 --- a/src/termwiztermtab.rs +++ b/src/termwiztermtab.rs @@ -27,8 +27,8 @@ use std::time::Duration; use term::color::ColorPalette; use term::selection::SelectionRange; use term::{ - CursorPosition, KeyCode, KeyModifiers, Line, MouseEvent, ScrollbackOrVisibleRowIndex, - TerminalHost, VisibleRowIndex, + CursorPosition, KeyCode, KeyModifiers, Line, MouseEvent, StableRowIndex, TerminalHost, + VisibleRowIndex, }; use termwiz::hyperlink::Hyperlink; use termwiz::input::{InputEvent, KeyEvent}; @@ -78,16 +78,19 @@ impl Renderable for RenderableState { } } - fn get_lines(&self, lines: Range) -> Vec> { + fn get_lines(&self, lines: Range) -> (StableRowIndex, Vec>) { let inner = self.inner.borrow_mut(); - inner - .surface - .screen_lines() - .into_iter() - .skip(lines.start.try_into().unwrap()) - .take((lines.end - lines.start).try_into().unwrap()) - .map(|line| Cow::Owned(line.into_owned())) - .collect() + ( + lines.start, + inner + .surface + .screen_lines() + .into_iter() + .skip(lines.start.try_into().unwrap()) + .take((lines.end - lines.start).try_into().unwrap()) + .map(|line| Cow::Owned(line.into_owned())) + .collect(), + ) } fn get_dirty_lines(&self) -> Vec<(usize, Cow, Range)> { @@ -161,6 +164,8 @@ impl Renderable for RenderableState { cols, scrollback_rows: 0, viewport_offset: 0, + physical_top: 0, + scrollback_top: 0, } } diff --git a/term/src/screen.rs b/term/src/screen.rs index 714106061..200ab9c89 100644 --- a/term/src/screen.rs +++ b/term/src/screen.rs @@ -196,6 +196,31 @@ impl Screen { self.scrollback_or_visible_row(range.start)..self.scrollback_or_visible_row(range.end) } + /// Converts a StableRowIndex range to the current effective + /// physical row index range. If the StableRowIndex goes off the top + /// of the scrollback, we'll return the top n rows, but if it goes off + /// the bottom we'll return the bottom n rows. + pub fn stable_range(&self, range: &Range) -> Range { + let range_len = (range.end - range.start) as usize; + + let first = match self.stable_row_to_phys(range.start) { + Some(first) => first, + None => { + return 0..range_len.min(self.lines.len()); + } + }; + + let last = match self.stable_row_to_phys(range.end - 1) { + Some(last) => last, + None => { + let last = self.lines.len() - 1; + return last - range_len..last + 1; + } + }; + + first..last + 1 + } + /// Translate a range of VisibleRowIndex to a range of PhysRowIndex. /// The resultant range will be invalidated by inserting or removing rows! #[inline]