From d5a9c227d2d3f7602c73fc7aac0bccd1fca5832e Mon Sep 17 00:00:00 2001 From: LiJianying Date: Thu, 3 Dec 2026 05:35:02 +0800 Subject: [PATCH] fix(windows): sync FLUTTERVIEW directly and defer post-DWM resize on fullscreen --- src/platform/windows/window_windows.cpp | 68 +++++++++++++++++-- ...ullscreen_hidden_titlebar_windows_test.cpp | 49 +++++++++++++ 2 files changed, 112 insertions(+), 5 deletions(-) diff --git a/src/platform/windows/window_windows.cpp b/src/platform/windows/window_windows.cpp index 375b600..b579b2e 100644 --- a/src/platform/windows/window_windows.cpp +++ b/src/platform/windows/window_windows.cpp @@ -63,6 +63,17 @@ static const wchar_t* kMouseInputPolicyProperty = L"NativeAPIMouseInputPolicy"; // DwmGetWindowAttribute(DWMWA_NCRENDERING_ENABLED), which flips to TRUE the // instant WS_CAPTION is restored in user mode, this tracks dwm.exe's actual state. static const wchar_t* kDwmNcDisabledProperty = L"NativeAPIDwmNcDisabled"; +// Set when a fullscreen transition has scheduled a deferred client/surface sync +// (after the caller's synchronous FFI/thread call returns) or awaits dwm.exe's +// asynchronous WM_DWMNCRENDERINGCHANGED notification. +static const wchar_t* kFullScreenSyncPendingProperty = L"NativeAPIFullScreenSyncPending"; +static const wchar_t* kFullScreenDwmSyncProperty = L"NativeAPIFullScreenDwmSync"; + +static UINT SyncFullScreenClientMessage() { + static const UINT message = RegisterWindowMessageW(L"NativeAPISyncFullScreenClient"); + return message; +} +static void SyncFullScreenClientArea(HWND hwnd, bool schedule_deferred = false); struct MouseInputPolicy { bool forward = false; @@ -902,6 +913,8 @@ static LRESULT WindowLifetimeMessage(HWND hwnd, UINT message, WPARAM wp, LPARAM RemovePropW(hwnd, kBackgroundColorProperty); RemovePropW(hwnd, kTranslucentBackgroundProperty); RemovePropW(hwnd, kDwmNcDisabledProperty); + RemovePropW(hwnd, kFullScreenSyncPendingProperty); + RemovePropW(hwnd, kFullScreenDwmSyncProperty); g_full_screen_windows.erase(hwnd); const auto result = DefSubclassProc(hwnd, message, wp, lp); WindowRegistry::GetInstance().Remove(static_cast(reference)); @@ -934,6 +947,11 @@ static LRESULT WindowLifetimeMessage(HWND hwnd, UINT message, WPARAM wp, LPARAM if (RemovePropW(hwnd, kShowMaximizedProperty)) ShowWindow(hwnd, SW_SHOWMAXIMIZED); return 0; } + if (message == SyncFullScreenClientMessage()) { + if (RemovePropW(hwnd, kFullScreenSyncPendingProperty)) + SyncFullScreenClientArea(hwnd, /*schedule_deferred=*/false); + return 0; + } if (message == WM_WINDOWPOSCHANGED) { const auto* pos = reinterpret_cast(lp); const LRESULT result = DefSubclassProc(hwnd, message, wp, lp); @@ -957,6 +975,10 @@ static LRESULT WindowLifetimeMessage(HWND hwnd, UINT message, WPARAM wp, LPARAM !GetPropW(hwnd, kFramelessClientProperty)) { ClearHiddenTitleBarNonClientBorder(hwnd); } + if (RemovePropW(hwnd, kFullScreenDwmSyncProperty)) { + SetPropW(hwnd, kFullScreenSyncPendingProperty, reinterpret_cast(1)); + PostMessageW(hwnd, SyncFullScreenClientMessage(), 0, 0); + } return result; } if (auto handled = HandleMaximizeButtonArea(hwnd, message, wp, lp)) return *handled; @@ -1483,19 +1505,55 @@ bool Window::IsMinimized() const { // resize their child surface to the final post-frame-change client rect and // repaint immediately after entering or leaving fullscreen, without waiting // for a focus change (Alt+Tab). -static void SyncFullScreenClientArea(HWND hwnd) { +static void SyncFullScreenClientArea(HWND hwnd, bool schedule_deferred) { if (!hwnd || !IsWindow(hwnd) || IsIconic(hwnd)) return; RECT client = {}; if (GetClientRect(hwnd, &client)) { + const int width = client.right - client.left; + const int height = client.bottom - client.top; const WPARAM size_type = IsZoomed(hwnd) ? SIZE_MAXIMIZED : SIZE_RESTORED; - SendMessageW(hwnd, WM_SIZE, size_type, - MAKELPARAM(client.right - client.left, client.bottom - client.top)); + const LPARAM size_param = MAKELPARAM(width, height); + SendMessageW(hwnd, WM_SIZE, size_type, size_param); + + // Win32's MoveWindow/SetWindowPos skips WM_SIZE on a child whose HWND + // rect already matches (width, height), even if the child's swapchain or + // Flutter view missed or timed out an earlier resize while Dart was + // blocked inside the synchronous FFI call or while DWM was transitioning + // fullscreen state. Ensure any FLUTTERVIEW child both matches the final + // client rect and receives WM_SIZE directly. + HWND flutter_view = FindWindowExW(hwnd, nullptr, L"FLUTTERVIEW", nullptr); + if (flutter_view && IsWindow(flutter_view)) { + RECT view_rect = {}; + if (!GetClientRect(flutter_view, &view_rect) || + (view_rect.right - view_rect.left) != width || + (view_rect.bottom - view_rect.top) != height) { + SetWindowPos(flutter_view, nullptr, client.left, client.top, width, height, + SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE); + } + SendMessageW(flutter_view, WM_SIZE, size_type, size_param); + RedrawWindow(flutter_view, nullptr, nullptr, + RDW_INVALIDATE | RDW_NOERASE | RDW_UPDATENOW); + if (GetForegroundWindow() == hwnd) { + HWND focused = GetFocus(); + if (!focused || GetAncestor(focused, GA_ROOT) != hwnd) + SetFocus(flutter_view); + } + } } UINT flags = RDW_INVALIDATE | RDW_NOERASE | RDW_ALLCHILDREN | RDW_UPDATENOW; if (!GetPropW(hwnd, kTitleBarHiddenProperty)) flags |= RDW_FRAME; RedrawWindow(hwnd, nullptr, nullptr, flags); + + if (schedule_deferred) { + // Re-sync on the next message-loop turn (after SetFullScreen returns and + // unblocks the caller's thread/isolate) and again once dwm.exe delivers + // its asynchronous WM_DWMNCRENDERINGCHANGED notification. + SetPropW(hwnd, kFullScreenSyncPendingProperty, reinterpret_cast(1)); + SetPropW(hwnd, kFullScreenDwmSyncProperty, reinterpret_cast(1)); + PostMessageW(hwnd, SyncFullScreenClientMessage(), 0, 0); + } } static bool ExitFullScreen(HWND hwnd, bool restore_maximized, bool sync_client) { @@ -1539,7 +1597,7 @@ static bool ExitFullScreen(HWND hwnd, bool restore_maximized, bool sync_client) SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_FRAMECHANGED); if (sync_client) - SyncFullScreenClientArea(hwnd); + SyncFullScreenClientArea(hwnd, /*schedule_deferred=*/true); NotifyWindowFullScreenChanged(hwnd, false); return true; } @@ -1613,7 +1671,7 @@ void Window::SetFullScreen(bool is_full_screen) { monitor.rcMonitor.right - monitor.rcMonitor.left, monitor.rcMonitor.bottom - monitor.rcMonitor.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_FRAMECHANGED); - SyncFullScreenClientArea(hwnd); + SyncFullScreenClientArea(hwnd, /*schedule_deferred=*/true); NotifyWindowFullScreenChanged(hwnd, true); return; } diff --git a/tests/window_fullscreen_hidden_titlebar_windows_test.cpp b/tests/window_fullscreen_hidden_titlebar_windows_test.cpp index b818463..aa9b8da 100644 --- a/tests/window_fullscreen_hidden_titlebar_windows_test.cpp +++ b/tests/window_fullscreen_hidden_titlebar_windows_test.cpp @@ -23,6 +23,9 @@ HBRUSH g_green_brush = nullptr; int g_size_count = 0; int g_last_size_w = 0; int g_last_size_h = 0; +int g_flutter_view_size_count = 0; +int g_flutter_view_last_w = 0; +int g_flutter_view_last_h = 0; bool g_watching_top_band = false; bool g_top_band_polluted = false; @@ -52,6 +55,16 @@ LRESULT CALLBACK OuterWatchProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp, U return r; } +LRESULT CALLBACK FlutterViewProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp) { + if (message == WM_SIZE) { + ++g_flutter_view_size_count; + g_flutter_view_last_w = LOWORD(lp); + g_flutter_view_last_h = HIWORD(lp); + return 0; + } + return DefWindowProcW(hwnd, message, wp, lp); +} + LRESULT CALLBACK HostProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp) { if (message == WM_ERASEBKGND) { RECT rc = {}; @@ -241,6 +254,42 @@ int main() { (post_max_client.bottom - post_max_client.top) == max_h, "maximized -> fullscreen -> maximized restores child surface to work area"); + // 4. Regression for libnativeapi/nativeapi#79: FLUTTERVIEW child must receive + // a deferred WM_SIZE on the next message-loop turn (after SetFullScreen + // returns) and again after WM_DWMNCRENDERINGCHANGED, even when its HWND + // rect already matches the parent client area. + ShowWindow(hwnd, SW_RESTORE); + Pump(); + WNDCLASSW fv_cls = {}; + fv_cls.lpfnWndProc = FlutterViewProc; + fv_cls.hInstance = cls.hInstance; + fv_cls.lpszClassName = L"FLUTTERVIEW"; + RegisterClassW(&fv_cls); + HWND flutter_view = CreateWindowW(L"FLUTTERVIEW", L"", WS_CHILD, 0, 0, 100, 100, hwnd, nullptr, + cls.hInstance, nullptr); + Check(flutter_view != nullptr, "created FLUTTERVIEW child window"); + + g_flutter_view_size_count = 0; + window.SetFullScreen(true); + const int fv_count_after_sync_enter = g_flutter_view_size_count; + Pump(); + const int fv_count_after_deferred_enter = g_flutter_view_size_count; + Check(fv_count_after_sync_enter > 0 && fv_count_after_deferred_enter > fv_count_after_sync_enter && + g_flutter_view_last_w == mon_w && g_flutter_view_last_h == mon_h && + ChildMatchesClient(hwnd, flutter_view), + "enter fullscreen delivers both immediate and deferred WM_SIZE to FLUTTERVIEW"); + + window.SetFullScreen(false); + const int fv_count_after_sync_exit = g_flutter_view_size_count; + Pump(); + const int fv_count_after_deferred_exit = g_flutter_view_size_count; + Check(fv_count_after_sync_exit > fv_count_after_deferred_enter && + fv_count_after_deferred_exit > fv_count_after_sync_exit && + g_flutter_view_last_w == initial_w && g_flutter_view_last_h == initial_h && + ChildMatchesClient(hwnd, flutter_view), + "exit fullscreen delivers both immediate and deferred WM_SIZE to FLUTTERVIEW"); + + DestroyWindow(flutter_view); DestroyWindow(hwnd); DeleteObject(g_green_brush); return failures ? 1 : 0;