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
This commit is contained in:
Wez Furlong committed 2023-08-05 20:48:59 -07:00
1 parent 085e8da478
commit f7f253ec28
3 files changed
+39 -6

No files matched your search

+3
View File
@@ -109,6 +109,9 @@ pub struct Config {
/// The DPI to assume
pub dpi: Option<f64>,
#[dynamic(default)]
pub dpi_by_screen: HashMap<String, f64>,
/// The baseline font to use
#[dynamic(default)]
pub font: TextStyle,
+1 -1
View File
@@ -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,
+35 -5
View File
@@ -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<f64> {
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