Skip to content

Commit

Permalink
feat: add separate field for monitor scale factor; fix incorrect dpi …
Browse files Browse the repository at this point in the history
…field (#769)
  • Loading branch information
lars-berger authored Oct 4, 2024
1 parent 829bf0a commit cc4952e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
16 changes: 12 additions & 4 deletions packages/wm/src/common/platform/native_monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ struct MonitorInfo {
hardware_id: Option<String>,
rect: Rect,
working_rect: Rect,
dpi: f32,
dpi: u32,
scale_factor: f32,
}

impl NativeMonitor {
Expand Down Expand Up @@ -62,10 +63,14 @@ impl NativeMonitor {
self.monitor_info().map(|info| &info.working_rect)
}

pub fn dpi(&self) -> anyhow::Result<f32> {
pub fn dpi(&self) -> anyhow::Result<u32> {
self.monitor_info().map(|info| info.dpi)
}

pub fn scale_factor(&self) -> anyhow::Result<f32> {
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();
Expand Down Expand Up @@ -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(
Expand All @@ -148,6 +154,7 @@ impl NativeMonitor {
rect,
working_rect,
dpi,
scale_factor,
})
})
}
Expand Down Expand Up @@ -209,7 +216,7 @@ pub fn nearest_monitor(window_handle: isize) -> NativeMonitor {
NativeMonitor::new(handle.0)
}

fn monitor_dpi(handle: isize) -> anyhow::Result<f32> {
fn monitor_dpi(handle: isize) -> anyhow::Result<u32> {
let mut dpi_x = u32::default();
let mut dpi_y = u32::default();

Expand All @@ -222,5 +229,6 @@ fn monitor_dpi(handle: isize) -> anyhow::Result<f32> {
)
}?;

Ok(dpi_y as f32 / 96.0)
// Arbitrarily choose the Y DPI.
Ok(dpi_y)
}
2 changes: 1 addition & 1 deletion packages/wm/src/containers/traits/tiling_size_getters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.,
};

Expand Down
7 changes: 5 additions & 2 deletions packages/wm/src/monitors/monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand Down Expand Up @@ -96,12 +97,13 @@ impl Monitor {
other: &Container,
) -> anyhow::Result<bool> {
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<ContainerDto> {
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion packages/wm/src/workspaces/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.,
};

Expand Down

0 comments on commit cc4952e

Please sign in to comment.