From 699bd7aac1b73f28de4a000e1f39fb362dd0987e Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 26 Feb 2019 21:34:31 -0800 Subject: [PATCH] batch resize/dpi changes in glium windows This is recommended in the docs and allows deferring of the size and scale to happen at the same time. --- src/gliumwindows.rs | 64 +++++++++++++++++++---------------------- src/guicommon/window.rs | 3 ++ 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index f54118977..e15fe67e9 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -79,6 +79,7 @@ pub struct GliumTerminalWindow { last_modifiers: KeyModifiers, allow_received_character: bool, tabs: Tabs, + have_pending_resize_check: bool, } impl TerminalWindow for GliumTerminalWindow { @@ -160,6 +161,28 @@ impl TerminalWindow for GliumTerminalWindow { Ok(false) } } + fn check_for_resize(&mut self) -> Result<(), Error> { + self.have_pending_resize_check = false; + let old_dpi_scale = self.fonts.get_dpi_scale(); + let size = self + .host + .display + .gl_window() + .get_inner_size() + .ok_or_else(|| format_err!("failed to get inner window size"))?; + let dpi_scale = self.host.display.gl_window().get_hidpi_factor(); + let (width, height): (u32, u32) = size.to_physical(dpi_scale).into(); + eprintln!( + "resize {}x{}@{} -> {}x{}@{}", + self.width, self.height, old_dpi_scale, width, height, dpi_scale + ); + if (old_dpi_scale - dpi_scale).abs() >= std::f64::EPSILON { + self.scaling_changed(None, Some(dpi_scale), width as u16, height as u16)?; + } else { + self.resize_surfaces(width as u16, height as u16, false)?; + } + Ok(()) + } } impl GliumTerminalWindow { @@ -234,6 +257,7 @@ impl GliumTerminalWindow { last_modifiers: Default::default(), allow_received_character: false, tabs: Tabs::new(tab), + have_pending_resize_check: false, }) } @@ -241,14 +265,6 @@ impl GliumTerminalWindow { self.host.display.gl_window().id() } - fn resize_surfaces_logical(&mut self, size: LogicalSize) -> Result { - let dpi_scale = self.host.display.gl_window().get_hidpi_factor(); - let (width, height): (u32, u32) = size.to_physical(dpi_scale).into(); - let width = width as u16; - let height = height as u16; - self.resize_surfaces(width, height, false) - } - fn decode_modifiers(state: glium::glutin::ModifiersState) -> term::KeyModifiers { let mut mods = Default::default(); if state.shift { @@ -652,36 +668,16 @@ impl GliumTerminalWindow { return Err(SessionTerminated::WindowClosed.into()); } Event::WindowEvent { - event: WindowEvent::HiDpiFactorChanged(factor), + event: WindowEvent::HiDpiFactorChanged(_), .. - } => { - // Assuming that this is dragging a window between hidpi and - // normal dpi displays. Treat this as a resize event of sorts - eprintln!("HiDpiFactorChanged {}", factor); - self.scaling_changed(None, Some(factor), self.width, self.height)?; } - Event::WindowEvent { - event: WindowEvent::Resized(size), + | Event::WindowEvent { + event: WindowEvent::Resized(_), .. } => { - // On Linux we may get here when HiDpiFactorChanged occurs, but - // without ever receiving a HiDpiFactorChanged event. - // We need to synthesize that event here by checking what we - // think we know, otherwise we will use the wrong font size. - let old_dpi_scale = self.fonts.get_dpi_scale(); - let dpi_scale = self.host.display.gl_window().get_hidpi_factor(); - if cfg!(all(unix, not(target_os = "macos"))) - && (old_dpi_scale - dpi_scale).abs() < std::f64::EPSILON - { - let (width, height): (u32, u32) = size.to_physical(dpi_scale).into(); - eprintln!( - "Synthesize HiDpiFactorChanged {} -> {} current {}x{} -> {}x{}", - old_dpi_scale, dpi_scale, self.width, self.height, width, height - ); - eprintln!("Generate scaling_changed with {}x{}", width, height); - self.scaling_changed(None, Some(dpi_scale), self.width, self.height)?; - } else { - self.resize_surfaces_logical(size)?; + if !self.have_pending_resize_check { + self.have_pending_resize_check = true; + self.host.with_window(|win| win.check_for_resize()); } } Event::WindowEvent { diff --git a/src/guicommon/window.rs b/src/guicommon/window.rs index 3a539c0fa..de5c5e16c 100644 --- a/src/guicommon/window.rs +++ b/src/guicommon/window.rs @@ -43,6 +43,9 @@ pub trait TerminalWindow { fn fonts(&self) -> &Rc; fn get_dimensions(&self) -> Dimensions; fn resize_if_not_full_screen(&mut self, width: u16, height: u16) -> Result; + fn check_for_resize(&mut self) -> Result<(), Error> { + Ok(()) + } fn activate_tab(&mut self, tab_idx: usize) -> Result<(), Error> { let max = self.get_tabs().len();