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

Windows: Split window initialization across NCCREATE and CREATE #2062

Merged
merged 3 commits into from Nov 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 20 additions & 21 deletions src/platform_impl/windows/event_loop.rs
Expand Up @@ -38,7 +38,7 @@ use crate::{
monitor::MonitorHandle as RootMonitorHandle,
platform_impl::platform::{
dark_mode::try_theme,
dpi::{become_dpi_aware, dpi_to_scale_factor, enable_non_client_dpi_scaling},
dpi::{become_dpi_aware, dpi_to_scale_factor},
drop_handler::FileDropHandler,
event::{self, handle_extended_keys, process_key_params, vkey_to_winit_vkey},
monitor::{self, MonitorHandle},
Expand Down Expand Up @@ -789,9 +789,9 @@ fn update_modifiers<T>(window: HWND, userdata: &WindowData<T>) {
}

#[cfg(target_arch = "x86_64")]
type WindowLongPtr = LONG_PTR;
pub(crate) type WindowLongPtr = LONG_PTR;
#[cfg(target_arch = "x86")]
type WindowLongPtr = LONG;
pub(crate) type WindowLongPtr = LONG;

/// Any window whose callback is configured to this function will have its events propagated
/// through the events loop of the thread the window was created in.
Expand All @@ -813,23 +813,27 @@ pub(super) unsafe extern "system" fn public_window_callback<T: 'static>(
let initdata = createstruct.lpCreateParams as LONG_PTR;
let initdata = &mut *(initdata as *mut InitData<'_, T>);

let runner = initdata.event_loop.runner_shared.clone();
if let Some((win, userdata)) = runner.catch_unwind(|| (initdata.post_init)(window)) {
initdata.window = Some(win);
let userdata = Box::into_raw(Box::new(userdata));
winuser::SetWindowLongPtrW(
window,
winuser::GWL_USERDATA,
userdata as WindowLongPtr,
);
userdata
} else {
return -1;
}
let result = match initdata.on_nccreate(window) {
Some(userdata) => {
winuser::SetWindowLongPtrW(window, winuser::GWL_USERDATA, userdata as _);
winuser::DefWindowProcW(window, msg, wparam, lparam)
}
None => -1, // failed to create the window
};

return result;
}
// Getting here should quite frankly be impossible,
// but we'll make window creation fail here just in case.
(0, winuser::WM_CREATE) => return -1,
(_, winuser::WM_CREATE) => {
let createstruct = &mut *(lparam as *mut winuser::CREATESTRUCTW);
let initdata = createstruct.lpCreateParams as LONG_PTR;
let initdata = &mut *(initdata as *mut InitData<'_, T>);

initdata.on_create();
return winuser::DefWindowProcW(window, msg, wparam, lparam);
}
(0, _) => return winuser::DefWindowProcW(window, msg, wparam, lparam),
_ => userdata as *mut WindowData<T>,
};
Expand Down Expand Up @@ -889,11 +893,6 @@ unsafe fn public_window_callback_inner<T: 'static>(
0
}

winuser::WM_NCCREATE => {
enable_non_client_dpi_scaling(window);
winuser::DefWindowProcW(window, msg, wparam, lparam)
}

winuser::WM_NCLBUTTONDOWN => {
if wparam == winuser::HTCAPTION as _ {
winuser::PostMessageW(window, winuser::WM_MOUSEMOVE, 0, lparam);
Expand Down