From 12e71a594a1b03c545d0b31fee0c9e2828742a53 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 26 Oct 2019 23:58:39 -0700 Subject: [PATCH] teach the terminalstate about pixel sizes This allows more accurate slicing of images when processing iterm2 image sequences --- src/frontend/guicommon/localtab.rs | 9 ++++--- src/mux/domain.rs | 2 ++ src/ssh.rs | 2 ++ term/src/terminal.rs | 4 +++ term/src/terminalstate.rs | 41 ++++++++++++++++++------------ 5 files changed, 39 insertions(+), 19 deletions(-) diff --git a/src/frontend/guicommon/localtab.rs b/src/frontend/guicommon/localtab.rs index b262e536b..2a70bb50c 100644 --- a/src/frontend/guicommon/localtab.rs +++ b/src/frontend/guicommon/localtab.rs @@ -51,9 +51,12 @@ impl Tab for LocalTab { fn resize(&self, size: PtySize) -> Result<(), Error> { self.pty.borrow_mut().resize(size)?; - self.terminal - .borrow_mut() - .resize(size.rows as usize, size.cols as usize); + self.terminal.borrow_mut().resize( + size.rows as usize, + size.cols as usize, + size.pixel_width as usize, + size.pixel_height as usize, + ); Ok(()) } diff --git a/src/mux/domain.rs b/src/mux/domain.rs index 3cc85cf4f..37136c759 100644 --- a/src/mux/domain.rs +++ b/src/mux/domain.rs @@ -105,6 +105,8 @@ impl Domain for LocalDomain { let mut terminal = term::Terminal::new( size.rows as usize, size.cols as usize, + size.pixel_width as usize, + size.pixel_height as usize, self.config.scrollback_lines.unwrap_or(3500), self.config.hyperlink_rules.clone(), ); diff --git a/src/ssh.rs b/src/ssh.rs index 9d08976d1..cb143208f 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -265,6 +265,8 @@ impl Domain for RemoteSshDomain { let mut terminal = term::Terminal::new( size.rows as usize, size.cols as usize, + size.pixel_width as usize, + size.pixel_height as usize, self.config.scrollback_lines.unwrap_or(3500), self.config.hyperlink_rules.clone(), ); diff --git a/term/src/terminal.rs b/term/src/terminal.rs index 012935c81..c0bfd07c4 100644 --- a/term/src/terminal.rs +++ b/term/src/terminal.rs @@ -70,6 +70,8 @@ impl Terminal { pub fn new( physical_rows: usize, physical_cols: usize, + pixel_width: usize, + pixel_height: usize, scrollback_size: usize, hyperlink_rules: Vec, ) -> Terminal { @@ -77,6 +79,8 @@ impl Terminal { state: TerminalState::new( physical_rows, physical_cols, + pixel_height, + pixel_width, scrollback_size, hyperlink_rules, ), diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index 5ebc8aafb..ba2280b91 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -209,6 +209,9 @@ pub struct TerminalState { /// The terminal title string title: String, palette: ColorPalette, + + pixel_width: usize, + pixel_height: usize, } fn is_double_click_word(s: &str) -> bool { @@ -229,6 +232,8 @@ impl TerminalState { pub fn new( physical_rows: usize, physical_cols: usize, + pixel_width: usize, + pixel_height: usize, scrollback_size: usize, hyperlink_rules: Vec, ) -> TerminalState { @@ -259,6 +264,8 @@ impl TerminalState { hyperlink_rules, title: "wezterm".to_string(), palette: ColorPalette::default(), + pixel_height, + pixel_width, } } @@ -1023,9 +1030,17 @@ impl TerminalState { Ok(()) } - pub fn resize(&mut self, physical_rows: usize, physical_cols: usize) { + pub fn resize( + &mut self, + physical_rows: usize, + physical_cols: usize, + pixel_width: usize, + pixel_height: usize, + ) { self.screen.resize(physical_rows, physical_cols); self.scroll_region = 0..physical_rows as i64; + self.pixel_height = pixel_height; + self.pixel_width = pixel_width; self.tabs.resize(physical_cols); self.set_scroll_viewport(0); // Ensure that the cursor is within the new bounds of the screen @@ -1282,17 +1297,13 @@ impl TerminalState { }; // Figure out the dimensions. - // TODO: we need to understand pixels here, and we don't today, - // so "guess" using the values that I see in my setup. - let cell_pixel_width = 8 * 2; - let cell_pixel_height = 15 * 2; + let physical_cols = self.screen().physical_cols; + let physical_rows = self.screen().physical_rows; + let cell_pixel_width = self.pixel_width / physical_cols; + let cell_pixel_height = self.pixel_height / physical_rows; - let width = image - .width - .to_pixels(cell_pixel_width, self.screen().physical_cols); - let height = image - .height - .to_pixels(cell_pixel_height, self.screen().physical_rows); + let width = image.width.to_pixels(cell_pixel_width, physical_cols); + let height = image.height.to_pixels(cell_pixel_height, physical_rows); // Compute any Automatic dimensions let (width, height) = match (width, height) { @@ -1316,13 +1327,11 @@ impl TerminalState { let width_in_cells = width / cell_pixel_width; let height_in_cells = height / cell_pixel_height; + // TODO: defer this to the actual renderer /* let available_pixel_width = width_in_cells * cell_pixel_width; let available_pixel_height = height_in_cells * cell_pixel_height; - */ - // TODO: defer this to the actual renderer - /* let resized_image = if image.preserve_aspect_ratio { let resized = decoded_image.resize( available_pixel_width as u32, @@ -1347,8 +1356,8 @@ impl TerminalState { let mut ypos = NotNan::new(0.0).unwrap(); let cursor_x = self.cursor.x; - let x_delta = 1.0 / width_in_cells as f32; - let y_delta = 1.0 / height_in_cells as f32; + let x_delta = 1.0 / (width as f32 / (self.pixel_width as f32 / physical_cols as f32)); + let y_delta = 1.0 / (height as f32 / (self.pixel_height as f32 / physical_rows as f32)); debug!( "image is {}x{} cells, {}x{} pixels", width_in_cells, height_in_cells, width, height