From 8d4a4f2f3d2d889ccc49e269a125e790d44a3dd6 Mon Sep 17 00:00:00 2001 From: LiJianying Date: Tue, 1 Dec 2026 08:59:17 +0800 Subject: [PATCH] Restore Windows frame before geometry changes and preserve DWM frame on normal shadowless windows --- src/platform/windows/window_windows.cpp | 160 +++++++--- tests/CMakeLists.txt | 8 +- ...indow_geometry_and_shadow_windows_test.cpp | 273 ++++++++++++++++++ 3 files changed, 396 insertions(+), 45 deletions(-) create mode 100644 tests/window_geometry_and_shadow_windows_test.cpp diff --git a/src/platform/windows/window_windows.cpp b/src/platform/windows/window_windows.cpp index 5a15cf2..375b600 100644 --- a/src/platform/windows/window_windows.cpp +++ b/src/platform/windows/window_windows.cpp @@ -757,9 +757,11 @@ static void DropThemeRegion(HWND hwnd) { // Who draws a window's frame, decided from its flags in this one place. The compositor // draws it, with its shadow (and on Windows 11 the rounded corners and the thin border), -// unless the window has no shadow, or has a hidden title bar and core draws the shadow: -// a custom shadow, or a shape, which the compositor's frame would not follow. A window -// with a hidden title bar and no frame from the compositor has no frame at all +// unless the window has a hidden title bar and either has no shadow or core draws the +// shadow: a custom shadow, or a shape, which the compositor's frame would not follow. +// System-decorated windows always keep the compositor's frame, because disabling DWM +// non-client rendering degrades the native title bar and borders to the classic theme. +// A window with a hidden title bar and no frame from the compositor has no frame at all // (kFramelessClientProperty): the sizing frame would otherwise be painted in the classic // theme. `keep_content` keeps the client area where it is on screen when the frame comes or // goes, so neither the content nor the shape coordinates move. @@ -768,11 +770,11 @@ static void UpdateFrameRendering(HWND hwnd, bool keep_content) { const bool shadow = GetPropW(hwnd, kNoShadowProperty) == nullptr; #ifdef NATIVEAPI_ENABLE_WINUI3 // WinUI 3 owns the frame of a window whose title bar it hides. - const bool compositor = shadow && !hidden; + const bool compositor = !hidden; #else - const bool compositor = shadow && !(hidden && (GetPropW(hwnd, kExplicitFramelessProperty) || - GetPropW(hwnd, kShapedProperty) || - GetPropW(hwnd, shape_shadow::kConfig))); + const bool compositor = !hidden || (shadow && !(GetPropW(hwnd, kExplicitFramelessProperty) || + GetPropW(hwnd, kShapedProperty) || + GetPropW(hwnd, shape_shadow::kConfig))); #endif DWMNCRENDERINGPOLICY policy = compositor ? DWMNCRP_USEWINDOWSTYLE : DWMNCRP_DISABLED; DwmSetWindowAttribute(hwnd, DWMWA_NCRENDERING_POLICY, &policy, sizeof(policy)); @@ -1496,6 +1498,83 @@ static void SyncFullScreenClientArea(HWND hwnd) { RedrawWindow(hwnd, nullptr, nullptr, flags); } +static bool ExitFullScreen(HWND hwnd, bool restore_maximized, bool sync_client) { + const auto saved = g_full_screen_windows.find(hwnd); + if (saved == g_full_screen_windows.end()) + return false; + + const FullScreenState state = saved->second; + g_full_screen_windows.erase(saved); + // Put back the bits that were taken away, rather than the whole style word: the + // window may have been reshaped while it was full screen (the title bar hidden, say) + // and that is not this method's to undo. + LONG_PTR restored_style = + GetWindowLongPtrW(hwnd, GWL_STYLE) | (state.style & kFullScreenRemovedStyle); + if (!restore_maximized) + restored_style &= ~WS_MAXIMIZE; + SetWindowLongPtrW(hwnd, GWL_STYLE, restored_style); + SetWindowLongPtrW( + hwnd, GWL_EXSTYLE, + GetWindowLongPtrW(hwnd, GWL_EXSTYLE) | (state.ex_style & kFullScreenRemovedExStyle)); + WINDOWPLACEMENT placement = state.placement; + if (!IsWindowVisible(hwnd)) { + placement.showCmd = SW_HIDE; + } else if (!restore_maximized && + (placement.showCmd == SW_SHOWMAXIMIZED || placement.showCmd == SW_MAXIMIZE || + placement.showCmd == SW_SHOWMINIMIZED || placement.showCmd == SW_MINIMIZE)) { + placement.showCmd = (GetWindowLongPtrW(hwnd, GWL_EXSTYLE) & WS_EX_NOACTIVATE) + ? SW_SHOWNOACTIVATE + : SW_SHOWNORMAL; + } + // Restore window placement BEFORE sending SWP_FRAMECHANGED: while the window + // still covers monitor.rcMonitor, DWM keeps DWMWA_NCRENDERING_ENABLED == FALSE, + // so an early SWP_FRAMECHANGED at fullscreen bounds would force non-client + // repainting before leaving fullscreen. + SetWindowPlacement(hwnd, &placement); + if (auto* policy = ControlPolicy(hwnd, false)) + ApplyControlPolicy(hwnd, *policy, /*notify_frame_changed=*/false); + UpdateFrameExtent(hwnd); + UpdateFrameRendering(hwnd, false); + SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, + SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE | + SWP_NOCOPYBITS | SWP_FRAMECHANGED); + if (sync_client) + SyncFullScreenClientArea(hwnd); + NotifyWindowFullScreenChanged(hwnd, false); + return true; +} + +// Restores a full screen or maximized window to a normal framed state before +// applying an explicit position or size, so SetWindowPos does not resize a +// borderless fullscreen HWND or leave WS_MAXIMIZE and off-screen maximized +// border offsets active until the user drags the window. +static bool RestoreWindowForGeometryChange(Window& window, HWND hwnd) { + if (!hwnd || !::IsWindow(hwnd)) + return false; + bool exited_full_screen = false; + if (g_full_screen_windows.find(hwnd) != g_full_screen_windows.end()) { + detail::WindowPropertyScope property_scope(window); + exited_full_screen = ExitFullScreen(hwnd, /*restore_maximized=*/false, /*sync_client=*/false); + } + if (IsZoomed(hwnd)) { + if (IsWindowVisible(hwnd)) { + const bool no_activate = (GetWindowLongPtrW(hwnd, GWL_EXSTYLE) & WS_EX_NOACTIVATE) != 0; + ShowWindow(hwnd, no_activate ? SW_SHOWNOACTIVATE : SW_RESTORE); + } else { + WINDOWPLACEMENT wp = {sizeof(wp)}; + if (GetWindowPlacement(hwnd, &wp)) { + wp.showCmd = SW_HIDE; + SetWindowLongPtrW(hwnd, GWL_STYLE, GetWindowLongPtrW(hwnd, GWL_STYLE) & ~WS_MAXIMIZE); + SetWindowPlacement(hwnd, &wp); + SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, + SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE | + SWP_FRAMECHANGED); + } + } + } + return exited_full_screen; +} + void Window::SetFullScreen(bool is_full_screen) { HWND owner_window = pimpl_->hwnd_; if (owner_window && GetWindowThreadProcessId(owner_window, nullptr) != GetCurrentThreadId()) { @@ -1539,33 +1618,7 @@ void Window::SetFullScreen(bool is_full_screen) { return; } - if (saved == g_full_screen_windows.end()) - return; - - const FullScreenState state = saved->second; - g_full_screen_windows.erase(saved); - // Put back the bits that were taken away, rather than the whole style word: the - // window may have been reshaped while it was full screen (the title bar hidden, say) - // and that is not this method's to undo. - SetWindowLongPtrW(hwnd, GWL_STYLE, - GetWindowLongPtrW(hwnd, GWL_STYLE) | (state.style & kFullScreenRemovedStyle)); - SetWindowLongPtrW( - hwnd, GWL_EXSTYLE, - GetWindowLongPtrW(hwnd, GWL_EXSTYLE) | (state.ex_style & kFullScreenRemovedExStyle)); - // Restore window placement BEFORE sending SWP_FRAMECHANGED: while the window - // still covers monitor.rcMonitor, DWM keeps DWMWA_NCRENDERING_ENABLED == FALSE, - // so an early SWP_FRAMECHANGED at fullscreen bounds would force non-client - // repainting before leaving fullscreen. - SetWindowPlacement(hwnd, &state.placement); - if (auto* policy = ControlPolicy(hwnd, false)) - ApplyControlPolicy(hwnd, *policy, /*notify_frame_changed=*/false); - UpdateFrameExtent(hwnd); - UpdateFrameRendering(hwnd, false); - SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, - SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE | - SWP_NOCOPYBITS | SWP_FRAMECHANGED); - SyncFullScreenClientArea(hwnd); - NotifyWindowFullScreenChanged(hwnd, false); + ExitFullScreen(hwnd, /*restore_maximized=*/true, /*sync_client=*/true); } bool Window::IsFullScreen() const { @@ -1598,7 +1651,11 @@ static void MoveWindowTo(HWND hwnd, const RECT& target) { void Window::SetBounds(Rectangle bounds) { if (pimpl_->hwnd_) { - MoveWindowTo(pimpl_->hwnd_, LogicalToPhysicalRect(bounds)); + HWND hwnd = pimpl_->hwnd_; + const bool sync_client = RestoreWindowForGeometryChange(*this, hwnd); + MoveWindowTo(hwnd, LogicalToPhysicalRect(bounds)); + if (sync_client) + SyncFullScreenClientArea(hwnd); } } @@ -1611,15 +1668,19 @@ Rectangle Window::GetBounds() const { void Window::SetSize(Size size, bool animate) { if (pimpl_->hwnd_) { + HWND hwnd = pimpl_->hwnd_; + const bool sync_client = RestoreWindowForGeometryChange(*this, hwnd); // Windows doesn't have built-in animation for window resizing // Animation would require custom implementation - double scale = GetScaleFactorForWindow(pimpl_->hwnd_); + double scale = GetScaleFactorForWindow(hwnd); if (scale <= 0.0) scale = 1.0; - SetWindowPos(pimpl_->hwnd_, nullptr, 0, 0, + SetWindowPos(hwnd, nullptr, 0, 0, static_cast(std::lround(size.width * scale)), static_cast(std::lround(size.height * scale)), SWP_NOMOVE | SWP_NOZORDER); + if (sync_client) + SyncFullScreenClientArea(hwnd); } } @@ -1639,21 +1700,25 @@ Size Window::GetSize() const { void Window::SetContentSize(Size size) { if (pimpl_->hwnd_) { + HWND hwnd = pimpl_->hwnd_; + const bool sync_client = RestoreWindowForGeometryChange(*this, hwnd); RECT windowRect, clientRect; - GetWindowRect(pimpl_->hwnd_, &windowRect); - GetClientRect(pimpl_->hwnd_, &clientRect); + GetWindowRect(hwnd, &windowRect); + GetClientRect(hwnd, &clientRect); // Calculate the difference between window and client area int borderWidth = (windowRect.right - windowRect.left) - clientRect.right; int borderHeight = (windowRect.bottom - windowRect.top) - clientRect.bottom; - double scale = GetScaleFactorForWindow(pimpl_->hwnd_); + double scale = GetScaleFactorForWindow(hwnd); if (scale <= 0.0) scale = 1.0; - SetWindowPos(pimpl_->hwnd_, nullptr, 0, 0, + SetWindowPos(hwnd, nullptr, 0, 0, static_cast(std::lround(size.width * scale)) + borderWidth, static_cast(std::lround(size.height * scale)) + borderHeight, SWP_NOMOVE | SWP_NOZORDER); + if (sync_client) + SyncFullScreenClientArea(hwnd); } } @@ -1675,6 +1740,7 @@ void Window::SetContentBounds(Rectangle bounds) { if (!pimpl_->hwnd_) return; HWND hwnd = pimpl_->hwnd_; + const bool sync_client = RestoreWindowForGeometryChange(*this, hwnd); const RECT content = LogicalToPhysicalRect(bounds); // The frame around the content changes with the DPI, so after a move onto a // monitor with another factor measure it again and correct. @@ -1683,11 +1749,11 @@ void Window::SetContentBounds(Rectangle bounds) { POINT client_origin = {0, 0}; if (!GetWindowRect(hwnd, &window_rect) || !GetClientRect(hwnd, &client_rect) || !ClientToScreen(hwnd, &client_origin)) - return; + break; if (attempt > 0 && client_origin.x == content.left && client_origin.y == content.top && client_rect.right == content.right - content.left && client_rect.bottom == content.bottom - content.top) - return; + break; const LONG left_inset = client_origin.x - window_rect.left; const LONG top_inset = client_origin.y - window_rect.top; const LONG right_inset = window_rect.right - (client_origin.x + client_rect.right); @@ -1696,6 +1762,8 @@ void Window::SetContentBounds(Rectangle bounds) { (content.right - content.left) + left_inset + right_inset, (content.bottom - content.top) + top_inset + bottom_inset, SWP_NOZORDER); } + if (sync_client) + SyncFullScreenClientArea(hwnd); } Rectangle Window::GetContentBounds() const { @@ -2236,10 +2304,14 @@ bool Window::IsNonActivating() const { void Window::SetPosition(Point point) { if (pimpl_->hwnd_) { + HWND hwnd = pimpl_->hwnd_; + const bool sync_client = RestoreWindowForGeometryChange(*this, hwnd); // The logical size stays; on a monitor with another factor that is another // physical size. const Size size = GetSize(); - MoveWindowTo(pimpl_->hwnd_, LogicalToPhysicalRect({point.x, point.y, size.width, size.height})); + MoveWindowTo(hwnd, LogicalToPhysicalRect({point.x, point.y, size.width, size.height})); + if (sync_client) + SyncFullScreenClientArea(hwnd); } } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a286aab..89baab6 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -710,4 +710,10 @@ if(WIN32) add_test(NAME window_fullscreen_hidden_titlebar_windows_test COMMAND window_fullscreen_hidden_titlebar_windows_test) set_tests_properties(window_fullscreen_hidden_titlebar_windows_test PROPERTIES TIMEOUT 20) -endif() \ No newline at end of file + add_executable(window_geometry_and_shadow_windows_test + window_geometry_and_shadow_windows_test.cpp) + target_link_libraries(window_geometry_and_shadow_windows_test PRIVATE nativeapi) + add_test(NAME window_geometry_and_shadow_windows_test + COMMAND window_geometry_and_shadow_windows_test) + set_tests_properties(window_geometry_and_shadow_windows_test PROPERTIES TIMEOUT 30) +endif() diff --git a/tests/window_geometry_and_shadow_windows_test.cpp b/tests/window_geometry_and_shadow_windows_test.cpp new file mode 100644 index 0000000..295292a --- /dev/null +++ b/tests/window_geometry_and_shadow_windows_test.cpp @@ -0,0 +1,273 @@ +// Regression test for Windows window geometry setters in FullScreen / Maximized +// states and SetHasShadow(false) on system-decorated (TitleBarStyle::Normal) +// windows: +// 1. SetHasShadow(false) on TitleBarStyle::Normal must NOT disable DWM +// non-client rendering (which degrades the window to a Windows 7 Aero Basic +// frame), while still recording HasShadow() == false so switching to +// TitleBarStyle::Hidden drops the frame and shadow as requested. +// 2. SetSize / SetContentSize / SetBounds / SetContentBounds / SetPosition +// while FullScreen or Maximized must exit fullscreen and/or unmaximize +// first so the window restores normal frame styles, DWM non-client borders, +// and on-screen placement without requiring a manual drag to recover. +#include +#include +#include +#include +#include "nativeapi.h" + +#pragma comment(lib, "dwmapi.lib") + +namespace { + +int failures = 0; + +void Check(bool ok, const char* label) { + std::cout << (ok ? "PASS " : "FAIL ") << label << std::endl; + if (!ok) + ++failures; +} + +void Pump(int ms = 120) { + const ULONGLONG deadline = GetTickCount64() + static_cast(ms); + MSG msg; + do { + while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessageW(&msg); + } + Sleep(10); + } while (GetTickCount64() < deadline); +} + +BOOL IsDwmNcEnabled(HWND hwnd) { + BOOL enabled = FALSE; + DwmGetWindowAttribute(hwnd, DWMWA_NCRENDERING_ENABLED, &enabled, sizeof(enabled)); + return enabled; +} + +struct HostState { + HWND child = nullptr; +}; + +LRESULT CALLBACK HostProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp) { + auto* state = reinterpret_cast(GetWindowLongPtrW(hwnd, GWLP_USERDATA)); + if (message == WM_SIZE && state && state->child) { + MoveWindow(state->child, 0, 0, LOWORD(lp), HIWORD(lp), TRUE); + return 0; + } + return DefWindowProcW(hwnd, message, wp, lp); +} + +HWND CreateHostWindow(HostState* state) { + static bool registered = false; + HINSTANCE instance = GetModuleHandleW(nullptr); + if (!registered) { + WNDCLASSEXW wc = {}; + wc.cbSize = sizeof(wc); + wc.lpfnWndProc = HostProc; + wc.hInstance = instance; + wc.hbrBackground = static_cast(GetStockObject(BLACK_BRUSH)); + wc.lpszClassName = L"NativeApiGeometryAndShadowHostTest"; + RegisterClassExW(&wc); + registered = true; + } + + HWND hwnd = CreateWindowExW( + 0, L"NativeApiGeometryAndShadowHostTest", L"Geometry & Shadow Test", + WS_OVERLAPPEDWINDOW, 180, 140, 820, 560, nullptr, nullptr, instance, nullptr); + SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast(state)); + + RECT client = {}; + GetClientRect(hwnd, &client); + state->child = CreateWindowExW( + 0, L"STATIC", L"", WS_CHILD | WS_VISIBLE, + 0, 0, client.right - client.left, client.bottom - client.top, + hwnd, nullptr, instance, nullptr); + return hwnd; +} + +bool Near(double a, double b, double tol = 2.0) { + return std::abs(a - b) <= tol; +} + +} // namespace + +int main() { + SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + nativeapi::Application::GetInstance(); + + HostState host = {}; + HWND hwnd = CreateHostWindow(&host); + nativeapi::Window window(hwnd); + + int exited_fullscreen_events = 0; + window.AddListener( + [&](const nativeapi::WindowExitedFullScreenEvent&) { ++exited_fullscreen_events; }); + + ShowWindow(hwnd, SW_SHOWNOACTIVATE); + UpdateWindow(hwnd); + Pump(120); + + BOOL composition_enabled = FALSE; + DwmIsCompositionEnabled(&composition_enabled); + + // 1. SetHasShadow(false) on TitleBarStyle::Normal keeps DWM frame active. + { + RECT win_before = {}, client_before = {}; + GetWindowRect(hwnd, &win_before); + GetClientRect(hwnd, &client_before); + Check(window.GetTitleBarStyle() == nativeapi::TitleBarStyle::Normal, + "window starts with TitleBarStyle::Normal"); + Check(window.HasShadow(), "window starts with HasShadow() == true"); + if (composition_enabled) { + Check(IsDwmNcEnabled(hwnd) == TRUE, "DWM NC rendering starts enabled on Normal title bar"); + } + + window.SetHasShadow(false); + Pump(80); + RECT win_after = {}, client_after = {}; + GetWindowRect(hwnd, &win_after); + GetClientRect(hwnd, &client_after); + Check(!window.HasShadow(), "SetHasShadow(false) records disabled shadow policy"); + if (composition_enabled) { + Check(IsDwmNcEnabled(hwnd) == TRUE, + "SetHasShadow(false) on Normal title bar preserves DWM NC rendering (no Win7 frame)"); + } + Check(EqualRect(&win_before, &win_after) && EqualRect(&client_before, &client_after), + "SetHasShadow(false) on Normal title bar preserves outer and client geometry"); + + // Switching to Hidden applies the recorded shadowless frameless policy. + window.SetTitleBarStyle(nativeapi::TitleBarStyle::Hidden); + Pump(80); + RECT hidden_win = {}, hidden_client = {}; + GetWindowRect(hwnd, &hidden_win); + GetClientRect(hwnd, &hidden_client); + Check(!window.HasShadow(), "switching to Hidden preserves HasShadow() == false"); + Check(IsDwmNcEnabled(hwnd) == FALSE, + "Hidden title bar with HasShadow() == false disables DWM NC frame"); + Check(hidden_client.right == (hidden_win.right - hidden_win.left) && + hidden_client.bottom == (hidden_win.bottom - hidden_win.top), + "Hidden title bar with HasShadow() == false fills full window rect"); + + // Switching back to Normal re-enables DWM NC rendering even while HasShadow() == false. + window.SetTitleBarStyle(nativeapi::TitleBarStyle::Normal); + Pump(80); + if (composition_enabled) { + Check(IsDwmNcEnabled(hwnd) == TRUE, + "switching back to Normal re-enables DWM NC rendering"); + } + window.SetHasShadow(true); + Pump(60); + Check(window.HasShadow(), "SetHasShadow(true) restores HasShadow() == true"); + } + + // 2. Resizing / moving while FullScreen (Normal & Hidden, plus Maximize -> FullScreen). + for (auto style : {nativeapi::TitleBarStyle::Normal, nativeapi::TitleBarStyle::Hidden}) { + window.SetTitleBarStyle(style); + window.SetBounds({180, 140, 760, 520}); + Pump(100); + + exited_fullscreen_events = 0; + window.SetFullScreen(true); + Pump(150); + Check(window.IsFullScreen(), "entered fullscreen before SetSize"); + + window.SetSize({640, 480}, false); + Pump(150); + const LONG_PTR gwl_style = GetWindowLongPtrW(hwnd, GWL_STYLE); + RECT win_rect = {}, client_rect = {}, child_rect = {}; + GetWindowRect(hwnd, &win_rect); + GetClientRect(hwnd, &client_rect); + GetClientRect(host.child, &child_rect); + const auto sz = window.GetSize(); + + Check(!window.IsFullScreen() && !window.IsMaximized() && !IsZoomed(hwnd), + "SetSize from fullscreen exits fullscreen and is not maximized"); + Check(exited_fullscreen_events == 1, + "SetSize from fullscreen emits WindowExitedFullScreenEvent once"); + Check((gwl_style & (WS_CAPTION | WS_THICKFRAME)) == (WS_CAPTION | WS_THICKFRAME), + "SetSize from fullscreen restores WS_CAPTION | WS_THICKFRAME"); + Check(Near(sz.width, 640) && Near(sz.height, 480), + "SetSize from fullscreen applies requested outer size"); + Check(win_rect.left >= 100 && win_rect.top >= 80, + "SetSize from fullscreen restores normal on-screen origin"); + Check(child_rect.right == client_rect.right && child_rect.bottom == client_rect.bottom, + "SetSize from fullscreen syncs host child HWND size immediately"); + if (composition_enabled) { + Check(IsDwmNcEnabled(hwnd) == TRUE, + "SetSize from fullscreen restores DWM non-client rendering"); + } + + // Maximize -> FullScreen -> SetContentSize: must clear BOTH fullscreen and maximized! + window.Maximize(); + Pump(100); + Check(window.IsMaximized() && IsZoomed(hwnd), "maximized before entering fullscreen"); + window.SetFullScreen(true); + Pump(150); + Check(window.IsFullScreen(), "entered fullscreen from maximized"); + + window.SetContentSize({600, 420}); + Pump(150); + const auto csz = window.GetContentSize(); + GetWindowRect(hwnd, &win_rect); + Check(!window.IsFullScreen() && !window.IsMaximized() && !IsZoomed(hwnd), + "SetContentSize after Maximize->FullScreen clears both fullscreen and maximized"); + Check(Near(csz.width, 600) && Near(csz.height, 420), + "SetContentSize after Maximize->FullScreen applies exact content size"); + Check(win_rect.left >= 100 && win_rect.top >= 80, + "SetContentSize after Maximize->FullScreen restores on-screen origin (not -11,-11)"); + + // FullScreen -> SetPosition + window.SetFullScreen(true); + Pump(120); + window.SetPosition({210, 170}); + Pump(120); + const auto pos = window.GetPosition(); + Check(!window.IsFullScreen() && !IsZoomed(hwnd) && Near(pos.x, 210) && Near(pos.y, 170), + "SetPosition from fullscreen exits fullscreen and moves restored window"); + } + + // 3. Resizing while Maximized (Normal & Hidden) without FullScreen. + for (auto style : {nativeapi::TitleBarStyle::Normal, nativeapi::TitleBarStyle::Hidden}) { + window.SetTitleBarStyle(style); + window.SetBounds({190, 150, 740, 500}); + Pump(80); + window.Maximize(); + Pump(100); + Check(window.IsMaximized() && IsZoomed(hwnd), "window maximized before SetSize"); + + window.SetSize({620, 460}, false); + Pump(100); + RECT win_rect = {}, client_rect = {}; + GetWindowRect(hwnd, &win_rect); + GetClientRect(hwnd, &client_rect); + const auto sz = window.GetSize(); + Check(!window.IsMaximized() && !IsZoomed(hwnd), + "SetSize from maximized clears WS_MAXIMIZE / IsZoomed"); + Check(win_rect.left >= 100 && win_rect.top >= 80, + "SetSize from maximized restores normal origin instead of (-padded_border)"); + Check(Near(sz.width, 620) && Near(sz.height, 460), + "SetSize from maximized applies requested size"); + Check(client_rect.right <= (win_rect.right - win_rect.left) && + client_rect.bottom <= (win_rect.bottom - win_rect.top), + "SetSize from maximized keeps client area inside outer window bounds"); + } + + DestroyWindow(hwnd); + Pump(40); + + // 4. Pre-show Maximize() + SetSize() keeps deferred startup maximize intact. + { + HostState unshown_host = {}; + HWND unshown = CreateHostWindow(&unshown_host); + nativeapi::Window w(unshown); + w.Maximize(); + w.SetSize({640, 480}, false); + Check(!IsWindowVisible(unshown) && w.IsMaximized(), + "SetSize before first show keeps hidden window and deferred startup maximize"); + DestroyWindow(unshown); + } + + std::cout << (failures ? "FAILED" : "OK") << std::endl; + return failures ? 1 : 0; +}