win32 gui: get correct window size even when maximized

This commit is contained in:
Jonathan Marler 2025-01-14 12:02:37 -07:00 committed by CJ van den Berg
parent dcddd373b5
commit 96c56c7124

View file

@ -1041,18 +1041,7 @@ fn WndProc(
state.bounds = null; state.bounds = null;
return 0; return 0;
}, },
win32.WM_SIZE, win32.WM_WINDOWPOSCHANGED => {
=> {
const do_sanity_check = true;
if (do_sanity_check) {
const lparam_size: XY(u16) = .{
.x = win32.loword(lparam),
.y = win32.hiword(lparam),
};
const client_size = getClientSize(u16, hwnd);
std.debug.assert(lparam_size.x == client_size.x);
std.debug.assert(lparam_size.y == client_size.y);
}
sendResize(hwnd); sendResize(hwnd);
return 0; return 0;
}, },
@ -1230,25 +1219,19 @@ fn getClientSize(comptime T: type, hwnd: win32.HWND) XY(T) {
fn calcWindowRect( fn calcWindowRect(
dpi: u32, dpi: u32,
bouding_rect: win32.RECT, bounding_rect: win32.RECT,
maybe_edge: ?win32.WPARAM, maybe_edge: ?win32.WPARAM,
cell_size: XY(i32), cell_size: XY(i32),
) win32.RECT { ) win32.RECT {
const window_size: XY(i32) = .{
.x = bouding_rect.right - bouding_rect.left,
.y = bouding_rect.bottom - bouding_rect.top,
};
const client_inset = getClientInset(dpi); const client_inset = getClientInset(dpi);
const client_size: XY(i32) = .{ const bounding_client_size: XY(i32) = .{
.x = window_size.x - client_inset.x, .x = (bounding_rect.right - bounding_rect.left) - client_inset.x,
.y = window_size.y - client_inset.y, .y = (bounding_rect.bottom - bounding_rect.top) - client_inset.y,
}; };
const trim: XY(i32) = .{
const diff: XY(i32) = .{ .x = @mod(bounding_client_size.x, cell_size.x),
.x = -@mod(client_size.x, cell_size.x), .y = @mod(bounding_client_size.y, cell_size.y),
.y = -@mod(client_size.y, cell_size.y),
}; };
const Adjustment = enum { low, high, both }; const Adjustment = enum { low, high, both };
const adjustments: XY(Adjustment) = if (maybe_edge) |edge| switch (edge) { const adjustments: XY(Adjustment) = if (maybe_edge) |edge| switch (edge) {
win32.WMSZ_LEFT => .{ .x = .low, .y = .both }, win32.WMSZ_LEFT => .{ .x = .low, .y = .both },
@ -1263,25 +1246,25 @@ fn calcWindowRect(
} else .{ .x = .both, .y = .both }; } else .{ .x = .both, .y = .both };
return .{ return .{
.left = bouding_rect.left - switch (adjustments.x) { .left = bounding_rect.left + switch (adjustments.x) {
.low => diff.x, .low => trim.x,
.high => 0, .high => 0,
.both => @divTrunc(diff.x, 2), .both => @divTrunc(trim.x, 2),
}, },
.top = bouding_rect.top - switch (adjustments.y) { .top = bounding_rect.top + switch (adjustments.y) {
.low => diff.y, .low => trim.y,
.high => 0, .high => 0,
.both => @divTrunc(diff.y, 2), .both => @divTrunc(trim.y, 2),
}, },
.right = bouding_rect.right + switch (adjustments.x) { .right = bounding_rect.right - switch (adjustments.x) {
.low => 0, .low => 0,
.high => diff.x, .high => trim.x,
.both => @divTrunc(diff.x + 1, 2), .both => @divTrunc(trim.x + 1, 2),
}, },
.bottom = bouding_rect.bottom + switch (adjustments.y) { .bottom = bounding_rect.bottom - switch (adjustments.y) {
.low => 0, .low => 0,
.high => diff.y, .high => trim.y,
.both => @divTrunc(diff.y + 1, 2), .both => @divTrunc(trim.y + 1, 2),
}, },
}; };
} }