From ada35ff3db5e6ab4996366068db014547282fe12 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 12 Dec 2021 09:27:37 -0700 Subject: [PATCH] gui: improve handling of dpi changes especially during window creation This fixes up the Windows issue, but I wonder if it also helps with the mac issue? refs: https://github.com/wez/wezterm/issues/1039 refs: https://github.com/wez/wezterm/issues/1381 --- docs/changelog.md | 1 + wezterm-gui/src/termwindow/resize.rs | 48 +++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 08f3857dd..23952726c 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -37,6 +37,7 @@ As features stabilize some brief notes about them will accumulate here. * Image attachment geometry for imgcat and sixels could stretch the image across the rounded up number of cells that contained the image. [#1300](https://github.com/wez/wezterm/issues/1300), [#1270](https://github.com/wez/wezterm/issues/1270) * Closing a split pane created inside a `wezterm ssh` session wouldn't actually close the pane [#1197](https://github.com/wez/wezterm/issues/1197) * Clicking when unfocused could lead to unwanted text selection [#1140](https://github.com/wez/wezterm/issues/1140) [#1310](https://github.com/wez/wezterm/issues/1310) +* Changing font scaling on Windows no longer causes the initial terminal rows/cols to be under-sized [#1381](https://github.com/wez/wezterm/issues/1381) ### 20211205-192649-672c1cc1 diff --git a/wezterm-gui/src/termwindow/resize.rs b/wezterm-gui/src/termwindow/resize.rs index c5cbf685d..ee1a6fccb 100644 --- a/wezterm-gui/src/termwindow/resize.rs +++ b/wezterm-gui/src/termwindow/resize.rs @@ -247,6 +247,17 @@ impl super::TermWindow { dims, cell_dims ); + // Stash this size pre-emptively. Without this, on Windows, + // when the font scaling is changed we can end up not seeing + // these dimensions and the scaling_changed logic ends up + // comparing two dimensions that have the same DPI and recomputing + // an adjusted terminal size. + // eg: rather than a simple old-dpi -> new dpi transition, we'd + // see old-dpi -> new dpi, call set_inner_size, then see a + // new-dpi -> new-dpi adjustment with a slightly different + // pixel geometry which is considered to be a user-driven resize. + // Stashing the dimensions here avoids that misconception. + self.dimensions = dims; window.set_inner_size(dims.pixel_width, dims.pixel_height); } } @@ -265,20 +276,45 @@ impl super::TermWindow { n as f32 / dpi as f32 } + /// On Windows, scaling changes may adjust the pixel geometry by a few pixels, + /// so this function checks if we're in a close-enough ballpark. + fn close_enough(a: f32, b: f32) -> bool { + let diff = (a - b).abs(); + diff < 10. + } + // Distinguish between eg: dpi being detected as double the initial dpi (where // the pixel dimensions don't change), and the dpi change being detected, but // where the window manager also decides to tile/resize the window. // In the latter case, we don't want to preserve the terminal rows/cols. let simple_dpi_change = dimensions.dpi != self.dimensions.dpi - && dpi_adjusted(dimensions.pixel_height, dimensions.dpi) - == dpi_adjusted(self.dimensions.pixel_height, self.dimensions.dpi) - && dpi_adjusted(dimensions.pixel_width, dimensions.dpi) - == dpi_adjusted(self.dimensions.pixel_width, self.dimensions.dpi); + && (close_enough( + dpi_adjusted(dimensions.pixel_height, dimensions.dpi), + dpi_adjusted(self.dimensions.pixel_height, self.dimensions.dpi), + ) && close_enough( + dpi_adjusted(dimensions.pixel_width, dimensions.dpi), + dpi_adjusted(self.dimensions.pixel_width, self.dimensions.dpi), + )) + || (close_enough( + dimensions.pixel_width as f32, + self.dimensions.pixel_width as f32, + ) && close_enough( + dimensions.pixel_height as f32, + self.dimensions.pixel_height as f32, + )); let dpi_changed = dimensions.dpi != self.dimensions.dpi; let font_scale_changed = font_scale != self.fonts.get_font_scale(); let scale_changed = dpi_changed || font_scale_changed; + log::trace!( + "dpi_changed={}, font_scale_changed={} scale_changed={} simple_dpi_change={}", + dpi_changed, + font_scale_changed, + scale_changed, + simple_dpi_change + ); + let cell_dims = self.current_cell_dimensions(); if scale_changed { @@ -291,6 +327,10 @@ impl super::TermWindow { None }; + log::trace!( + "scaling_changed, follow with applying dimensions. scale_changed_cells={:?}", + scale_changed_cells + ); self.apply_dimensions(&dimensions, scale_changed_cells, window); }