From df145bab8c85c5cf9d52a957b559791cc5d37fc4 Mon Sep 17 00:00:00 2001 From: Isaiah Inuwa Date: Fri, 21 Nov 2025 21:33:53 -0600 Subject: [PATCH] Convert physical to logical pixels --- .../windows_plugin_authenticator/Cargo.toml | 1 + .../windows_plugin_authenticator/src/util.rs | 26 ++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/apps/desktop/desktop_native/windows_plugin_authenticator/Cargo.toml b/apps/desktop/desktop_native/windows_plugin_authenticator/Cargo.toml index 7155e728519..4de416bb4f1 100644 --- a/apps/desktop/desktop_native/windows_plugin_authenticator/Cargo.toml +++ b/apps/desktop/desktop_native/windows_plugin_authenticator/Cargo.toml @@ -13,6 +13,7 @@ windows = { workspace = true, features = [ "Win32_Security", "Win32_System_Com", "Win32_System_LibraryLoader", + "Win32_UI_HiDpi", ] } windows-core = { workspace = true } serde_json = { workspace = true } diff --git a/apps/desktop/desktop_native/windows_plugin_authenticator/src/util.rs b/apps/desktop/desktop_native/windows_plugin_authenticator/src/util.rs index 99d7622d8cf..5675c5ac23a 100644 --- a/apps/desktop/desktop_native/windows_plugin_authenticator/src/util.rs +++ b/apps/desktop/desktop_native/windows_plugin_authenticator/src/util.rs @@ -3,6 +3,7 @@ use std::io::Write; use std::path::Path; use std::time::{SystemTime, UNIX_EPOCH}; +use windows::Win32::UI::HiDpi::GetDpiForWindow; use windows::{ core::PCSTR, Win32::{Foundation::*, System::LibraryLoader::*, UI::WindowsAndMessaging::GetWindowRect}, @@ -10,6 +11,8 @@ use windows::{ use crate::com_buffer::ComBuffer; +const BASE_DPI: u32 = 96; + pub trait HwndExt { fn center_position(&self) -> windows::core::Result<(i32, i32)>; } @@ -19,11 +22,26 @@ impl HwndExt for HWND { let mut window: RECT = RECT::default(); unsafe { GetWindowRect(*self, &mut window)?; + + // Calculate center in physical pixels + let center = ( + (window.right + window.left) / 2, + (window.bottom + window.top) / 2, + ); + + // Convert from physical to logical pixels + let dpi = GetDpiForWindow(*self); + if dpi == BASE_DPI { + return Ok(center); + } + let scaling_factor: f64 = dpi as f64 / 96.0; + let scaled_center = ( + center.0 as f64 / scaling_factor, + center.1 as f64 / scaling_factor, + ); + + Ok((scaled_center.0 as i32, scaled_center.1 as i32)) } - // TODO: We may need to adjust for scaling. - let center_x = (window.right + window.left) / 2; - let center_y = (window.bottom + window.top) / 2; - Ok((center_x, center_y)) } }