Skip to content

Commit bd1f7ee

Browse files
Desktop: Fix resize helper window Z-order and attach to parent console on Windows (#3572)
* Desktop: Fix helper window zorder on Windows * Attach to parent console if launched from a terminal * Workaround for a Windows-specific exception that occurs when `app` is dropped
1 parent fafc687 commit bd1f7ee

4 files changed

Lines changed: 25 additions & 9 deletions

File tree

desktop/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ windows = { version = "0.58.0", features = [
5555
"Win32_Graphics_Gdi",
5656
"Win32_System_LibraryLoader",
5757
"Win32_System_Com",
58+
"Win32_System_Console",
5859
"Win32_UI_Controls",
5960
"Win32_UI_WindowsAndMessaging",
6061
"Win32_UI_HiDpi",

desktop/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ pub fn start() {
9393
let mut app = App::new(Box::new(cef_context), cef_view_info_sender, wgpu_context, app_event_receiver, app_event_scheduler, cli.files);
9494

9595
event_loop.run_app(&mut app).unwrap();
96+
97+
// Workaround for a Windows-specific exception that occurs when `app` is dropped.
98+
// The issue causes the window to hang for a few seconds before closing.
99+
// Appears to be related to CEF object destruction order.
100+
// Calling `exit` bypasses rust teardown and lets Windows perform process cleanup.
101+
// TODO: Identify and fix the underlying CEF shutdown issue so this workaround can be removed.
102+
#[cfg(target_os = "windows")]
103+
exit(0);
96104
}
97105

98106
pub fn start_helper() {

desktop/src/window/win.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use windows::Win32::System::Com::{COINIT_APARTMENTTHREADED, CoInitializeEx};
2+
use windows::Win32::System::Console::{ATTACH_PARENT_PROCESS, AttachConsole};
23
use windows::Win32::UI::Shell::SetCurrentProcessExplicitAppUserModelID;
34
use windows::core::HSTRING;
45
use winit::event_loop::ActiveEventLoop;
@@ -13,6 +14,12 @@ pub(super) struct NativeWindowImpl {
1314

1415
impl super::NativeWindow for NativeWindowImpl {
1516
fn init() {
17+
// Attach to parent console if launched from a terminal (no-op otherwise)
18+
unsafe {
19+
let _ = AttachConsole(ATTACH_PARENT_PROCESS);
20+
}
21+
22+
// Set stable app ID
1623
let app_id = HSTRING::from(APP_ID);
1724
unsafe {
1825
let _ = CoInitializeEx(None, COINIT_APARTMENTTHREADED).ok();

desktop/src/window/win/native_handle.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl NativeWindowHandle {
6161
0,
6262
0,
6363
0,
64-
None,
64+
main,
6565
None,
6666
HINSTANCE(std::ptr::null_mut()),
6767
// Pass the main window's HWND to WM_NCCREATE so the helper can store it.
@@ -118,7 +118,7 @@ impl NativeWindowHandle {
118118
}
119119

120120
// Force window update
121-
let _ = unsafe { SetWindowPos(main, None, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER) };
121+
let _ = unsafe { SetWindowPos(main, None, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE) };
122122

123123
native_handle
124124
}
@@ -325,20 +325,20 @@ unsafe fn position_helper(main: HWND, helper: HWND) {
325325
let w = (r.right - r.left) + RESIZE_BAND_THICKNESS * 2;
326326
let h = (r.bottom - r.top) + RESIZE_BAND_THICKNESS * 2;
327327

328-
let _ = unsafe { SetWindowPos(helper, main, x, y, w, h, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOSENDCHANGING) };
328+
let _ = unsafe { SetWindowPos(helper, main, x, y, w, h, SWP_NOACTIVATE | SWP_NOSENDCHANGING) };
329329
}
330330

331331
unsafe fn calculate_hit(helper: HWND, lparam: LPARAM) -> u32 {
332-
let x = (lparam.0 & 0xFFFF) as i16 as u32;
333-
let y = ((lparam.0 >> 16) & 0xFFFF) as i16 as u32;
332+
let x = (lparam.0 & 0xFFFF) as i16 as i32;
333+
let y = ((lparam.0 >> 16) & 0xFFFF) as i16 as i32;
334334

335335
let mut r = RECT::default();
336336
let _ = unsafe { GetWindowRect(helper, &mut r) };
337337

338-
let on_top = y < (r.top + RESIZE_BAND_THICKNESS) as u32;
339-
let on_right = x >= (r.right - RESIZE_BAND_THICKNESS) as u32;
340-
let on_bottom = y >= (r.bottom - RESIZE_BAND_THICKNESS) as u32;
341-
let on_left = x < (r.left + RESIZE_BAND_THICKNESS) as u32;
338+
let on_top = y < (r.top + RESIZE_BAND_THICKNESS) as i32;
339+
let on_right = x >= (r.right - RESIZE_BAND_THICKNESS) as i32;
340+
let on_bottom = y >= (r.bottom - RESIZE_BAND_THICKNESS) as i32;
341+
let on_left = x < (r.left + RESIZE_BAND_THICKNESS) as i32;
342342

343343
match (on_top, on_right, on_bottom, on_left) {
344344
(true, _, _, true) => HTTOPLEFT,

0 commit comments

Comments
 (0)