Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion src/platform/windows/window_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,11 @@ static std::optional<LRESULT> HandleHiddenTitleBarFrame(HWND hwnd, UINT message,
}
// The maximized frame extends beyond the work area by its resize borders.
// Fit the captionless client to the work area, excluding those borders.
// During WM_NCCALCSIZE the window still has its previous rect (the
// minimized placeholder at (-32000, -32000) when restoring from minimized),
// so query the monitor for the proposed rect it is moving to.
MONITORINFO monitor = {sizeof(monitor)};
if (GetMonitorInfoW(MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST), &monitor))
if (GetMonitorInfoW(MonitorFromRect(&proposed, MONITOR_DEFAULTTONEAREST), &monitor))
params->rgrc[0] = monitor.rcWork;
return result;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ if(WIN32)
target_link_libraries(window_geometry_coalesce_windows_test PRIVATE nativeapi comctl32)
add_test(NAME window_geometry_coalesce_windows_test COMMAND window_geometry_coalesce_windows_test)
set_tests_properties(window_geometry_coalesce_windows_test PROPERTIES TIMEOUT 20)
add_executable(window_hidden_titlebar_nccalcsize_windows_test window_hidden_titlebar_nccalcsize_windows_test.cpp)
target_link_libraries(window_hidden_titlebar_nccalcsize_windows_test PRIVATE nativeapi comctl32)
add_test(NAME window_hidden_titlebar_nccalcsize_windows_test COMMAND window_hidden_titlebar_nccalcsize_windows_test)
set_tests_properties(window_hidden_titlebar_nccalcsize_windows_test PROPERTIES TIMEOUT 20)
endif()

# window_manager#569 (#77): WS_POPUP windows and their caption.
Expand Down
153 changes: 153 additions & 0 deletions tests/window_hidden_titlebar_nccalcsize_windows_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// Regression test for libnativeapi/nativeapi#53:
// With TitleBarStyle::Hidden, WM_NCCALCSIZE for a maximized window must query
// the monitor for the incoming proposed rect (MonitorFromRect(&proposed))
// rather than MonitorFromWindow(hwnd), which still points at the minimized
// placeholder (-32000, -32000) while restoring from minimized on a secondary
// monitor.

#include <windows.h>

#include <cstdio>
#include <vector>

#include "../src/window.h"

namespace {

int g_failures = 0;

void Check(bool ok, const char* what) {
if (!ok) {
std::fprintf(stderr, "FAIL: %s\n", what);
++g_failures;
}
}

void PumpFor(DWORD ms) {
const ULONGLONG end = GetTickCount64() + ms;
MSG msg = {};
while (true) {
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
if (GetTickCount64() >= end) {
break;
}
Sleep(5);
}
}

RECT GetClientRectInScreen(HWND hwnd) {
RECT client = {};
GetClientRect(hwnd, &client);
POINT tl = {client.left, client.top};
POINT br = {client.right, client.bottom};
ClientToScreen(hwnd, &tl);
ClientToScreen(hwnd, &br);
return {tl.x, tl.y, br.x, br.y};
}

bool RectsEqual(const RECT& a, const RECT& b) {
return a.left == b.left && a.top == b.top && a.right == b.right && a.bottom == b.bottom;
}

LRESULT CALLBACK TestWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) {
return DefWindowProcW(hwnd, msg, wp, lp);
}

} // namespace

int main() {
SetProcessDPIAware();

WNDCLASSW wc = {};
wc.lpfnWndProc = TestWndProc;
wc.hInstance = GetModuleHandleW(nullptr);
wc.lpszClassName = L"NativeAPIIssue53TestWindow";
RegisterClassW(&wc);

std::vector<MONITORINFO> monitors;
EnumDisplayMonitors(
nullptr, nullptr,
[](HMONITOR hmon, HDC, LPRECT, LPARAM data) -> BOOL {
auto* list = reinterpret_cast<std::vector<MONITORINFO>*>(data);
MONITORINFO info = {sizeof(info)};
if (GetMonitorInfoW(hmon, &info)) {
list->push_back(info);
}
return TRUE;
},
reinterpret_cast<LPARAM>(&monitors));
Check(!monitors.empty(), "at least one monitor present");
if (monitors.empty()) {
return 1;
}

// Pick a secondary monitor if available; otherwise use the primary monitor.
MONITORINFO target_monitor = monitors.front();
bool has_secondary = false;
for (const auto& m : monitors) {
if (!(m.dwFlags & MONITORINFOF_PRIMARY)) {
target_monitor = m;
has_secondary = true;
break;
}
}

HWND hwnd = CreateWindowExW(0, wc.lpszClassName, L"Issue53 Test", WS_OVERLAPPEDWINDOW,
target_monitor.rcWork.left + 40, target_monitor.rcWork.top + 40,
640, 480, nullptr, nullptr, wc.hInstance, nullptr);
Check(hwnd != nullptr, "create test window");
if (!hwnd) {
return 1;
}

nativeapi::Window window(hwnd);
window.SetTitleBarStyle(nativeapi::TitleBarStyle::Hidden);
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
PumpFor(80);

const RECT before_client = GetClientRectInScreen(hwnd);
Check(RectsEqual(before_client, target_monitor.rcWork),
"maximized hidden-title-bar client rect matches target monitor work area");

// Direct WM_NCCALCSIZE check with a proposed rect on each monitor while the
// window is zoomed: rgrc[0] must resolve to the proposed rect's monitor.
for (const auto& m : monitors) {
NCCALCSIZE_PARAMS params = {};
params.rgrc[0] = {m.rcWork.left - 8, m.rcWork.top - 8, m.rcWork.right + 8,
m.rcWork.bottom + 8};
SendMessageW(hwnd, WM_NCCALCSIZE, TRUE, reinterpret_cast<LPARAM>(&params));
Check(RectsEqual(params.rgrc[0], m.rcWork),
"WM_NCCALCSIZE resolves monitor from proposed rect");
}

// Minimize and restore: client rect and hit testing must stay on target_monitor.
ShowWindow(hwnd, SW_MINIMIZE);
PumpFor(80);
ShowWindow(hwnd, SW_RESTORE);
PumpFor(120);

Check(IsZoomed(hwnd) != FALSE, "window returns to maximized state after restore");
const RECT after_client = GetClientRectInScreen(hwnd);
Check(RectsEqual(after_client, target_monitor.rcWork),
has_secondary
? "restored maximized window on secondary monitor keeps secondary work area"
: "restored maximized window keeps monitor work area");

const int mid_x = (target_monitor.rcWork.left + target_monitor.rcWork.right) / 2;
const int mid_y = (target_monitor.rcWork.top + target_monitor.rcWork.bottom) / 2;
const LRESULT hit = SendMessageW(hwnd, WM_NCHITTEST, 0, MAKELPARAM(mid_x, mid_y));
Check(hit == HTCLIENT, "WM_NCHITTEST at center of restored window returns HTCLIENT");

DestroyWindow(hwnd);
PumpFor(20);
UnregisterClassW(wc.lpszClassName, wc.hInstance);

if (g_failures == 0) {
std::printf("PASS: window_hidden_titlebar_nccalcsize_windows_test (monitors=%zu)\n",
monitors.size());
}
return g_failures == 0 ? 0 : 1;
}
Loading