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: Fix incorrect titlebar behavior with a scale factor greater than 1.0 #9438

Closed
wants to merge 5 commits into from
Closed
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 18 additions & 14 deletions crates/gpui/src/platform/windows/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl WindowsWindowInner {
}

fn get_titlebar_rect(&self) -> anyhow::Result<RECT> {
let top_and_bottom_borders = 2;
const TOP_BOTTOM_BORDERS: i32 = 2;
let theme = unsafe { OpenThemeData(self.hwnd, w!("WINDOW")) };
let title_bar_size = unsafe {
GetThemePartSize(
Expand All @@ -139,12 +139,15 @@ impl WindowsWindowInner {
}?;
unsafe { CloseThemeData(theme) }?;

let mut height =
(title_bar_size.cy as f32 * self.scale_factor).round() as i32 + top_and_bottom_borders;
// let mut height =
// (title_bar_size.cy as f32 * self.scale_factor).round() as i32 + TOP_BOTTOM_BORDERS;
let mut height = title_bar_size.cy + TOP_BOTTOM_BORDERS;

if self.is_maximized() {
let dpi = unsafe { GetDpiForWindow(self.hwnd) };
height += unsafe { (GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi) * 2) as i32 };
// let dpi = unsafe { GetDpiForWindow(self.hwnd) };
height += unsafe {
(GetSystemMetricsForDpi(SM_CXPADDEDBORDER, DEFAULT_DPI_VALUE) * 2) as i32
};
}

let mut rect = RECT::default();
Expand Down Expand Up @@ -751,11 +754,10 @@ impl WindowsWindowInner {
return unsafe { DefWindowProcW(self.hwnd, msg, wparam, lparam) };
}

let dpi = unsafe { GetDpiForWindow(self.hwnd) };

let frame_x = unsafe { GetSystemMetricsForDpi(SM_CXFRAME, dpi) };
let frame_y = unsafe { GetSystemMetricsForDpi(SM_CYFRAME, dpi) };
let padding = unsafe { GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi) };
// let dpi = unsafe { GetDpiForWindow(self.hwnd) };
let frame_x = unsafe { GetSystemMetricsForDpi(SM_CXFRAME, DEFAULT_DPI_VALUE) };
let frame_y = unsafe { GetSystemMetricsForDpi(SM_CYFRAME, DEFAULT_DPI_VALUE) };
let padding = unsafe { GetSystemMetricsForDpi(SM_CXPADDEDBORDER, DEFAULT_DPI_VALUE) };

// wparam is TRUE so lparam points to an NCCALCSIZE_PARAMS structure
let mut params = lparam.0 as *mut NCCALCSIZE_PARAMS;
Expand Down Expand Up @@ -825,9 +827,9 @@ impl WindowsWindowInner {
return hit;
}

let dpi = unsafe { GetDpiForWindow(self.hwnd) };
let frame_y = unsafe { GetSystemMetricsForDpi(SM_CYFRAME, dpi) };
let padding = unsafe { GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi) };
// let dpi = unsafe { GetDpiForWindow(self.hwnd) };
let frame_y = unsafe { GetSystemMetricsForDpi(SM_CYFRAME, DEFAULT_DPI_VALUE) };
let padding = unsafe { GetSystemMetricsForDpi(SM_CXPADDEDBORDER, DEFAULT_DPI_VALUE) };

let mut cursor_point = POINT {
x: lparam.signed_loword().into(),
Expand All @@ -841,7 +843,8 @@ impl WindowsWindowInner {
let titlebar_rect = self.get_titlebar_rect();
if let Ok(titlebar_rect) = titlebar_rect {
if cursor_point.y < titlebar_rect.bottom {
let caption_btn_width = unsafe { GetSystemMetricsForDpi(SM_CXSIZE, dpi) };
let caption_btn_width =
unsafe { GetSystemMetricsForDpi(SM_CXSIZE, DEFAULT_DPI_VALUE) };
if cursor_point.x >= titlebar_rect.right - caption_btn_width {
return LRESULT(HTCLOSE as _);
} else if cursor_point.x >= titlebar_rect.right - caption_btn_width * 2 {
Expand Down Expand Up @@ -1593,3 +1596,4 @@ fn oemkey_vkcode_to_string(code: u16) -> Option<String> {

// https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-dragqueryfilew
const DRAGDROP_GET_FILES_COUNT: u32 = 0xFFFFFFFF;
const DEFAULT_DPI_VALUE: u32 = 96;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use USER_DEFAULT_SCREEN_DPI

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Loading