From cee61517486bf84ea01affe4534fba5f68b94f2b Mon Sep 17 00:00:00 2001 From: Lars Berger Date: Sat, 5 Oct 2024 02:56:25 +0800 Subject: [PATCH] feat: add separate field for monitor scale factor; fix incorrect dpi field --- .../wm/src/common/platform/native_monitor.rs | 16 ++++++++++++---- .../src/containers/traits/tiling_size_getters.rs | 2 +- packages/wm/src/monitors/monitor.rs | 7 +++++-- packages/wm/src/workspaces/workspace.rs | 2 +- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/packages/wm/src/common/platform/native_monitor.rs b/packages/wm/src/common/platform/native_monitor.rs index 3077e9475..306a29c0e 100644 --- a/packages/wm/src/common/platform/native_monitor.rs +++ b/packages/wm/src/common/platform/native_monitor.rs @@ -31,7 +31,8 @@ struct MonitorInfo { hardware_id: Option, rect: Rect, working_rect: Rect, - dpi: f32, + dpi: u32, + scale_factor: f32, } impl NativeMonitor { @@ -62,10 +63,14 @@ impl NativeMonitor { self.monitor_info().map(|info| &info.working_rect) } - pub fn dpi(&self) -> anyhow::Result { + pub fn dpi(&self) -> anyhow::Result { self.monitor_info().map(|info| info.dpi) } + pub fn scale_factor(&self) -> anyhow::Result { + self.monitor_info().map(|info| info.scale_factor) + } + fn monitor_info(&self) -> anyhow::Result<&MonitorInfo> { self.info.get_or_try_init(|| { let mut monitor_info = MONITORINFOEXW::default(); @@ -124,6 +129,7 @@ impl NativeMonitor { let device_name = String::from_utf16_lossy(&monitor_info.szDevice); let dpi = monitor_dpi(self.handle)?; + let scale_factor = dpi as f32 / 96.0; let rc_monitor = monitor_info.monitorInfo.rcMonitor; let rect = Rect::from_ltrb( @@ -148,6 +154,7 @@ impl NativeMonitor { rect, working_rect, dpi, + scale_factor, }) }) } @@ -209,7 +216,7 @@ pub fn nearest_monitor(window_handle: isize) -> NativeMonitor { NativeMonitor::new(handle.0) } -fn monitor_dpi(handle: isize) -> anyhow::Result { +fn monitor_dpi(handle: isize) -> anyhow::Result { let mut dpi_x = u32::default(); let mut dpi_y = u32::default(); @@ -222,5 +229,6 @@ fn monitor_dpi(handle: isize) -> anyhow::Result { ) }?; - Ok(dpi_y as f32 / 96.0) + // Arbitrarily choose the Y DPI. + Ok(dpi_y) } diff --git a/packages/wm/src/containers/traits/tiling_size_getters.rs b/packages/wm/src/containers/traits/tiling_size_getters.rs index 4406e986a..ffdbe04ca 100644 --- a/packages/wm/src/containers/traits/tiling_size_getters.rs +++ b/packages/wm/src/containers/traits/tiling_size_getters.rs @@ -29,7 +29,7 @@ pub trait TilingSizeGetters: CommonGetters { let gaps_config = self.gaps_config(); let scale_factor = match gaps_config.scale_with_dpi { - true => monitor.native().dpi()?, + true => monitor.native().scale_factor()?, false => 1., }; diff --git a/packages/wm/src/monitors/monitor.rs b/packages/wm/src/monitors/monitor.rs index 8d8eb2ec9..f970cd45f 100644 --- a/packages/wm/src/monitors/monitor.rs +++ b/packages/wm/src/monitors/monitor.rs @@ -45,7 +45,8 @@ pub struct MonitorDto { height: i32, x: i32, y: i32, - dpi: f32, + dpi: u32, + scale_factor: f32, handle: isize, device_name: String, device_path: Option, @@ -96,12 +97,13 @@ impl Monitor { other: &Container, ) -> anyhow::Result { let dpi = self.native().dpi()?; + let other_dpi = other .monitor() .and_then(|monitor| monitor.native().dpi().ok()) .context("Failed to get DPI of other monitor.")?; - Ok((dpi - other_dpi).abs() < f32::EPSILON) + Ok(dpi != other_dpi) } pub fn to_dto(&self) -> anyhow::Result { @@ -123,6 +125,7 @@ impl Monitor { x: rect.x(), y: rect.y(), dpi: self.native().dpi()?, + scale_factor: self.native().scale_factor()?, handle: self.native().handle, device_name: self.native().device_name()?.clone(), device_path: self.native().device_path()?.cloned(), diff --git a/packages/wm/src/workspaces/workspace.rs b/packages/wm/src/workspaces/workspace.rs index 29b323e86..8f3f0f19b 100644 --- a/packages/wm/src/workspaces/workspace.rs +++ b/packages/wm/src/workspaces/workspace.rs @@ -136,7 +136,7 @@ impl PositionGetters for Workspace { let gaps_config = &self.0.borrow().gaps_config; let scale_factor = match &gaps_config.scale_with_dpi { - true => monitor.native().dpi()?, + true => monitor.native().scale_factor()?, false => 1., };