windows: Create window with correct size (#14218)
张小白
created 2 years ago
The `Bounds<DevicePixels>` 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
Change summary
crates/gpui/src/platform/windows/window.rs | 37 +++++++++++++++++++++--
1 file changed, 33 insertions(+), 4 deletions(-)
Detailed changes
@@ -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<WindowsWindowStatePtr>) {
};
}
+fn calcualte_window_position(bounds: Bounds<DevicePixels>, hwnd: HWND) -> anyhow::Result<RECT> {
+ 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;