From f7f253ec280c5625370e4e399226c3bb63454fed Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 5 Aug 2023 20:48:59 -0700 Subject: [PATCH] macos: add config.dpi_by_screen config option Allows specifying the precise dpi to use on a per-screen basis: ```lua return { dpi_by_screen = { ["Built-in Retina Display"] = 144, }, } ``` The screen names are the same as those returned from `wezterm.gui.screens()`. Changing either `dpi` or `dpi_by_screen` in the config will now cause the window to be immediately resized/adjusted to the changed dpi override. Ultimately, I'd like to deprecate `dpi` in favor of `dpi_by_screen`, but can't do that until this functionality is ported to windows, x11 and wayland. refs: #4096 --- config/src/config.rs | 3 +++ window/src/os/macos/connection.rs | 2 +- window/src/os/macos/window.rs | 40 +++++++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/config/src/config.rs b/config/src/config.rs index 0b2e0d680..ff7d53adc 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -109,6 +109,9 @@ pub struct Config { /// The DPI to assume pub dpi: Option, + #[dynamic(default)] + pub dpi_by_screen: HashMap, + /// The baseline font to use #[dynamic(default)] pub font: TextStyle, diff --git a/window/src/os/macos/connection.rs b/window/src/os/macos/connection.rs index 3c6f42c8a..5c5fd5e35 100644 --- a/window/src/os/macos/connection.rs +++ b/window/src/os/macos/connection.rs @@ -198,7 +198,7 @@ fn screen_backing_frame(screen: *mut Object) -> NSRect { } } -fn nsscreen_to_screen_info(screen: *mut Object) -> ScreenInfo { +pub fn nsscreen_to_screen_info(screen: *mut Object) -> ScreenInfo { let frame = screen_backing_frame(screen); let rect = euclid::rect( frame.origin.x as isize, diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index a97a118fc..54b902328 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -605,6 +605,10 @@ impl Window { let width = backing_frame.size.width; let height = backing_frame.size.height; + let dpi = dpi_for_window_screen(*window, &config) + .unwrap_or(crate::DEFAULT_DPI * (backing_frame.size.width / frame.size.width)) + as usize; + let weak_window = window.weak(); let window_handle = Window { id: window_id, @@ -635,8 +639,7 @@ impl Window { dimensions: Dimensions { pixel_width: width as usize, pixel_height: height as usize, - dpi: (crate::DEFAULT_DPI * (backing_frame.size.width / frame.size.width)) - as usize, + dpi, }, window_state: WindowState::default(), live_resizing: false, @@ -1226,9 +1229,16 @@ impl WindowInner { } fn config_did_change(&mut self, config: &ConfigHandle) { + let dpi_changed = + self.config.dpi != config.dpi || self.config.dpi_by_screen != config.dpi_by_screen; + self.config = config.clone(); if let Some(window_view) = WindowView::get_this(unsafe { &**self.view }) { - window_view.inner.borrow_mut().config = config.clone(); + let mut inner = window_view.inner.borrow_mut(); + inner.config = config.clone(); + if dpi_changed { + inner.screen_changed = true; + } } self.update_window_shadow(); self.update_window_background_blur(); @@ -1632,6 +1642,17 @@ pub fn superclass(this: &Object) -> &'static Class { } } +fn dpi_for_window_screen(ns_window: *mut Object, config: &ConfigHandle) -> Option { + if config.dpi_by_screen.is_empty() { + return config.dpi; + } + + let screen = unsafe { msg_send![ns_window, screen] }; + let info = crate::os::macos::connection::nsscreen_to_screen_info(screen); + + config.dpi_by_screen.get(&info.name).copied() +} + #[allow(clippy::identity_op)] fn decode_mouse_buttons(mask: u64) -> MouseButtons { let mut buttons = MouseButtons::NONE; @@ -2741,12 +2762,21 @@ impl WindowView { unsafe { msg_send![*window, isZoomed] } }); + let dpi = inner + .window + .as_ref() + .and_then(|window| { + let window = window.load(); + dpi_for_window_screen(*window, &inner.config) + }) + .unwrap_or(crate::DEFAULT_DPI * (backing_frame.size.width / frame.size.width)) + as usize; + inner.events.dispatch(WindowEvent::Resized { dimensions: Dimensions { pixel_width: width as usize, pixel_height: height as usize, - dpi: (crate::DEFAULT_DPI * (backing_frame.size.width / frame.size.width)) - as usize, + dpi, }, window_state: if is_full_screen { WindowState::FULL_SCREEN