Skip to content

Commit 72c3afd

Browse files
committed
wip (halp)
1 parent fc79de6 commit 72c3afd

5 files changed

Lines changed: 50 additions & 7 deletions

File tree

‎src/platform/win/error.rs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
use crate::HandlerError;
2+
use dpi::PhysicalSize;
23
use std::fmt::Display;
34

45
pub type Result<T> = std::result::Result<T, PlatformError>;
56

67
#[derive(Debug)]
78
pub enum PlatformError {
89
Win32(windows_core::Error),
9-
ResizeFailed,
10+
UnexpectedResizeResult { new_actual_size: PhysicalSize<u32> },
11+
InvalidScaleFactor,
1012
Handler(HandlerError),
1113
}
1214

@@ -27,7 +29,14 @@ impl Display for PlatformError {
2729
match self {
2830
PlatformError::Win32(e) => Display::fmt(e, f),
2931
PlatformError::Handler(e) => Display::fmt(e, f),
30-
PlatformError::ResizeFailed => f.write_str("Window resize request failed."),
32+
PlatformError::UnexpectedResizeResult { new_actual_size } => {
33+
write!(
34+
f,
35+
"Resize operation did not result in expected size, got {:?} instead.",
36+
new_actual_size
37+
)
38+
}
39+
PlatformError::InvalidScaleFactor => f.write_str("Invalid window scale factor."),
3140
}
3241
}
3342
}

‎src/platform/win/window.rs‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ impl WindowHandle {
6969
}
7070

7171
pub fn resize(&self, new_size: Size) -> Result<()> {
72+
crate::debug!("Resizing to {:?}", new_size);
7273
let new_size = self.state.sizing_strategy.adjust_size(new_size, self.size()).physical;
74+
crate::debug!("Size adjusted to {:?}", new_size);
7375

7476
if new_size == self.state.current_size.get() {
7577
return Ok(());
@@ -86,12 +88,15 @@ impl WindowHandle {
8688
let _guard = self.state.originate_host_resize();
8789
let dpi_ctx =
8890
DpiAwarenessGuard::new(&self.state.user32, self.state.dpi_scaling_strategy.get())?;
91+
crate::debug!("DPI: {:?}", hwnd.get_dpi(&self.state.user32));
8992
hwnd.resize_and_activate(new_size, self.state.current_dpi.get(), &dpi_ctx)?;
9093

9194
if self.state.current_size.get() == new_size {
9295
Ok(())
9396
} else {
94-
Err(PlatformError::ResizeFailed)
97+
Err(PlatformError::UnexpectedResizeResult {
98+
new_actual_size: self.state.current_size.get(),
99+
})
95100
}
96101
}
97102

@@ -100,6 +105,9 @@ impl WindowHandle {
100105
}
101106

102107
pub fn suggest_scale_factor(&self, new_host_scale_factor: f64) -> Result<()> {
108+
let new_dpi = Dpi::from_scale_factor(new_host_scale_factor)
109+
.ok_or(PlatformError::InvalidScaleFactor)?;
110+
103111
let current_scale_factor = self.state.scale_factor();
104112
self.state.fallback_scale_factor.set(Some(new_host_scale_factor));
105113

@@ -109,6 +117,8 @@ impl WindowHandle {
109117
return Ok(());
110118
}
111119

120+
self.state.current_dpi.set(Some(new_dpi));
121+
112122
let Some(hwnd) = self.hwnd.get() else { return Ok(()) };
113123

114124
let current_size = self.state.current_size.get();
@@ -127,12 +137,14 @@ impl WindowHandle {
127137
let _guard = self.state.originate_host_resize();
128138
let dpi_ctx = DpiAwarenessGuard::new(&self.state.user32, dpi_scaling_strategy)?;
129139

130-
hwnd.resize_and_activate(new_size, None, &dpi_ctx)?;
140+
hwnd.resize_and_activate(new_size, Some(new_dpi), &dpi_ctx)?;
131141

132142
if self.state.current_size.get() == new_size {
133143
Ok(())
134144
} else {
135-
Err(PlatformError::ResizeFailed)
145+
Err(PlatformError::UnexpectedResizeResult {
146+
new_actual_size: self.state.current_size.get(),
147+
})
136148
}
137149
}
138150

@@ -620,6 +632,8 @@ unsafe fn wnd_proc_inner(
620632
let new_size = PhysicalSize { width, height };
621633
let current_size = window_state.shared.current_size.get();
622634

635+
crate::debug!("WM_SIZE: new {new_size:?}, old {current_size:?}");
636+
623637
// Only send the event if anything changed
624638
if current_size == new_size {
625639
return None;

‎src/platform/win/window_state.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl WindowSharedState {
175175
Self {
176176
parented: (settings.parent.is_some() || settings.wait_for_parent).into(),
177177
is_alive: true.into(),
178-
current_dpi: None.into(),
178+
current_dpi: Some(Dpi::default()).into(),
179179
current_size: settings.size.to_physical(1.0).into(),
180180
fallback_scale_factor: settings.fallback_scale_factor.into(),
181181
resize_host_originated: false.into(),
@@ -216,6 +216,10 @@ impl WindowSharedState {
216216
}
217217

218218
pub fn scale_factor(&self) -> f64 {
219+
if self.dpi_scaling_strategy.get().assume_96_dpi {
220+
return 1.0;
221+
}
222+
219223
if let Some(dpi) = self.current_dpi.get() {
220224
dpi.scale_factor()
221225
} else {

‎src/wrappers/win32/dpi.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::platform::DpiScalingStrategy;
33
use crate::wrappers::win32::user32::ExtendedUser32;
44
use crate::wrappers::win32::DpiAwarenessContextType::*;
55
use std::ffi::c_void;
6-
use std::fmt::Debug;
6+
use std::fmt::{Debug, Formatter};
77
use std::num::NonZeroU32;
88
use std::ptr::NonNull;
99
use windows_core::{Error, Result};
@@ -20,6 +20,10 @@ impl Dpi {
2020
self.0.get() as f64 / USER_DEFAULT_SCREEN_DPI as f64
2121
}
2222

23+
pub fn from_scale_factor(scale_factor: f64) -> Option<Dpi> {
24+
Some(Dpi(NonZeroU32::new((scale_factor * USER_DEFAULT_SCREEN_DPI as f64) as u32)?))
25+
}
26+
2327
/// Windows 10, version 1607.
2428
pub fn get_system(user32: &ExtendedUser32) -> Option<Self> {
2529
if let Some(get_dpi_for_system) = user32.get_dpi_for_system {
@@ -333,6 +337,14 @@ impl<'a> DpiAwarenessGuard<'a> {
333337
}
334338
}
335339

340+
impl Debug for DpiAwarenessGuard<'_> {
341+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
342+
f.debug_struct("DpiAwarenessGuard")
343+
.field("context_type", &self.inner.map(|(c, u)| c.get_type(u)))
344+
.finish()
345+
}
346+
}
347+
336348
impl Drop for DpiAwarenessGuard<'_> {
337349
fn drop(&mut self) {
338350
if let Some(inner) = self.inner {

‎src/wrappers/win32/window/handle.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,12 @@ impl HWnd {
184184
let style = self.get_style()?;
185185

186186
let rect = Rect::from(client_size);
187+
crate::debug!("Resize_and_activate rect: {rect:?} (dpi: {window_dpi:?}");
187188
let rect = dpi_ctx.client_area_to_nc_area(rect, style, window_dpi)?;
188189

190+
crate::debug!("Resize_and_activate rect after adjust: {rect:?}");
191+
crate::debug!("CTX: {dpi_ctx:?}");
192+
189193
self.resize_nc_and_activate(rect.size())
190194
}
191195

0 commit comments

Comments
 (0)