From 6eeec9b403a6d32788a00029d5c3e7a4a17f4629 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=B0=8F=E7=99=BD?= <364772080@qq.com> Date: Fri, 12 Jul 2024 00:54:59 +0800 Subject: [PATCH] windows: Create window with correct size (#14218) The `Bounds` we use to create a window represents the size of the drawable area. ### Before: https://github.com/zed-industries/zed/assets/14981363/52f0d196-b113-4b64-a0d1-407972674990 ### After https://github.com/zed-industries/zed/assets/14981363/83298b6c-5e5f-4a47-b051-35b4a02404ac Release Notes: - N/A --- crates/gpui/src/platform/windows/window.rs | 37 +++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/crates/gpui/src/platform/windows/window.rs b/crates/gpui/src/platform/windows/window.rs index 22a6a74c2e65e86f6cd0cd0763fbc3f76ad6c81a..e701fb1b1c05d715839f37a031c55a58c182f147 100644 --- a/crates/gpui/src/platform/windows/window.rs +++ b/crates/gpui/src/platform/windows/window.rs @@ -323,10 +323,7 @@ impl WindowsWindow { display.default_bounds() }; let bounds = bounds.to_device_pixels(wnd.0.state.borrow().scale_factor); - placement.rcNormalPosition.left = bounds.left().0; - placement.rcNormalPosition.right = bounds.right().0; - placement.rcNormalPosition.top = bounds.top().0; - placement.rcNormalPosition.bottom = bounds.bottom().0; + placement.rcNormalPosition = calcualte_window_position(bounds, raw_hwnd).unwrap(); SetWindowPlacement(raw_hwnd, &placement).log_err(); } unsafe { ShowWindow(raw_hwnd, SW_SHOW).ok().log_err() }; @@ -958,6 +955,38 @@ fn register_drag_drop(state_ptr: Rc) { }; } +fn calcualte_window_position(bounds: Bounds, hwnd: HWND) -> anyhow::Result { + let mut rect = RECT { + left: bounds.left().0, + top: bounds.top().0, + right: bounds.right().0, + bottom: bounds.bottom().0, + }; + let window_rect = unsafe { + let mut rect = std::mem::zeroed(); + GetWindowRect(hwnd, &mut rect)?; + rect + }; + let client_rect = unsafe { + let mut rect = std::mem::zeroed(); + GetClientRect(hwnd, &mut rect)?; + rect + }; + let width_offset = + (window_rect.right - window_rect.left) - (client_rect.right - client_rect.left); + let height_offset = + (window_rect.bottom - window_rect.top) - (client_rect.bottom - client_rect.top); + let left_offset = width_offset / 2; + let top_offset = height_offset / 2; + let right_offset = width_offset - left_offset; + let bottom_offet = height_offset - top_offset; + rect.left -= left_offset; + rect.top -= top_offset; + rect.right += right_offset; + rect.bottom += bottom_offet; + Ok(rect) +} + // https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-dragqueryfilew const DRAGDROP_GET_FILES_COUNT: u32 = 0xFFFFFFFF;