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

Fixes unnecessary initial resize on Windows #3581

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/window.rs
Expand Up @@ -320,6 +320,7 @@ impl ApplicationHandler<UserEvent> for Application {

match event {
WindowEvent::Resized(size) => {
info!("Window={window_id:?} resized to {size:?}");
window.resize(size);
},
WindowEvent::Focused(focused) => {
Expand Down
36 changes: 28 additions & 8 deletions src/platform_impl/windows/window.rs
Expand Up @@ -45,6 +45,7 @@ use windows_sys::Win32::UI::WindowsAndMessaging::{
};

use tracing::warn;
use windows_sys::Win32::UI::WindowsAndMessaging::AdjustWindowRectEx;

use crate::cursor::Cursor;
use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
Expand Down Expand Up @@ -1256,6 +1257,7 @@ impl<'a> InitData<'a> {
win.set_taskbar_icon(self.attributes.platform_specific.taskbar_icon.clone());

let attributes = self.attributes.clone();
let clamped_size = calculate_clamped_window_size(&attributes, win.scale_factor());

if attributes.content_protected {
win.set_content_protected(true);
Expand All @@ -1269,12 +1271,6 @@ impl<'a> InitData<'a> {

win.set_enabled_buttons(attributes.enabled_buttons);

let size = attributes.inner_size.unwrap_or_else(|| PhysicalSize::new(800, 600).into());
let max_size = attributes
.max_inner_size
.unwrap_or_else(|| PhysicalSize::new(f64::MAX, f64::MAX).into());
let min_size = attributes.min_inner_size.unwrap_or_else(|| PhysicalSize::new(0, 0).into());
let clamped_size = Size::clamp(size, min_size, max_size, win.scale_factor());
win.request_inner_size(clamped_size);

// let margins = MARGINS {
Expand Down Expand Up @@ -1365,9 +1361,25 @@ unsafe fn init(
let menu = attributes.platform_specific.menu;
let fullscreen = attributes.fullscreen.clone();
let maximized = attributes.maximized;

let default_scale_factor =
event_loop.primary_monitor().map(|monitor| monitor.scale_factor()).unwrap_or(1.0);
let clamped_size = calculate_clamped_window_size(&attributes, default_scale_factor);

let mut initdata = InitData { event_loop, attributes, window_flags, window: None };

let (style, ex_style) = window_flags.to_window_styles();

// Best effort: try to create the window with the requested inner size
let adjusted_size = {
let [w, h]: [i32; 2] = clamped_size.to_physical::<u32>(default_scale_factor).into();
let mut rect = RECT { left: 0, top: 0, right: w, bottom: h };
unsafe {
AdjustWindowRectEx(&mut rect, style, menu.is_some().into(), ex_style);
}
(rect.right - rect.left, rect.bottom - rect.top)
};

let handle = unsafe {
CreateWindowExW(
ex_style,
Expand All @@ -1376,8 +1388,8 @@ unsafe fn init(
style,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
adjusted_size.0,
adjusted_size.1,
parent.unwrap_or(0),
menu.unwrap_or(0),
util::get_instance_handle(),
Expand Down Expand Up @@ -1434,6 +1446,14 @@ unsafe fn register_window_class(class_name: &[u16]) {
unsafe { RegisterClassExW(&class) };
}

fn calculate_clamped_window_size(attributes: &WindowAttributes, scale_factor: f64) -> Size {
let size = attributes.inner_size.unwrap_or_else(|| PhysicalSize::new(800, 600).into());
let max_size =
attributes.max_inner_size.unwrap_or_else(|| PhysicalSize::new(f64::MAX, f64::MAX).into());
let min_size = attributes.min_inner_size.unwrap_or_else(|| PhysicalSize::new(0, 0).into());
Size::clamp(size, min_size, max_size, scale_factor)
}

struct ComInitialized(#[allow(dead_code)] *mut ());
impl Drop for ComInitialized {
fn drop(&mut self) {
Expand Down