Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add separate field for monitor scale factor; fix incorrect dpi field #769

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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