Skip to content

Commit d5a9c22

Browse files
committed
fix(windows): sync FLUTTERVIEW directly and defer post-DWM resize on fullscreen
1 parent ea05fcc commit d5a9c22

2 files changed

Lines changed: 112 additions & 5 deletions

File tree

‎src/platform/windows/window_windows.cpp‎

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ static const wchar_t* kMouseInputPolicyProperty = L"NativeAPIMouseInputPolicy";
6363
// DwmGetWindowAttribute(DWMWA_NCRENDERING_ENABLED), which flips to TRUE the
6464
// instant WS_CAPTION is restored in user mode, this tracks dwm.exe's actual state.
6565
static const wchar_t* kDwmNcDisabledProperty = L"NativeAPIDwmNcDisabled";
66+
// Set when a fullscreen transition has scheduled a deferred client/surface sync
67+
// (after the caller's synchronous FFI/thread call returns) or awaits dwm.exe's
68+
// asynchronous WM_DWMNCRENDERINGCHANGED notification.
69+
static const wchar_t* kFullScreenSyncPendingProperty = L"NativeAPIFullScreenSyncPending";
70+
static const wchar_t* kFullScreenDwmSyncProperty = L"NativeAPIFullScreenDwmSync";
71+
72+
static UINT SyncFullScreenClientMessage() {
73+
static const UINT message = RegisterWindowMessageW(L"NativeAPISyncFullScreenClient");
74+
return message;
75+
}
76+
static void SyncFullScreenClientArea(HWND hwnd, bool schedule_deferred = false);
6677

6778
struct MouseInputPolicy {
6879
bool forward = false;
@@ -902,6 +913,8 @@ static LRESULT WindowLifetimeMessage(HWND hwnd, UINT message, WPARAM wp, LPARAM
902913
RemovePropW(hwnd, kBackgroundColorProperty);
903914
RemovePropW(hwnd, kTranslucentBackgroundProperty);
904915
RemovePropW(hwnd, kDwmNcDisabledProperty);
916+
RemovePropW(hwnd, kFullScreenSyncPendingProperty);
917+
RemovePropW(hwnd, kFullScreenDwmSyncProperty);
905918
g_full_screen_windows.erase(hwnd);
906919
const auto result = DefSubclassProc(hwnd, message, wp, lp);
907920
WindowRegistry::GetInstance().Remove(static_cast<WindowId>(reference));
@@ -934,6 +947,11 @@ static LRESULT WindowLifetimeMessage(HWND hwnd, UINT message, WPARAM wp, LPARAM
934947
if (RemovePropW(hwnd, kShowMaximizedProperty)) ShowWindow(hwnd, SW_SHOWMAXIMIZED);
935948
return 0;
936949
}
950+
if (message == SyncFullScreenClientMessage()) {
951+
if (RemovePropW(hwnd, kFullScreenSyncPendingProperty))
952+
SyncFullScreenClientArea(hwnd, /*schedule_deferred=*/false);
953+
return 0;
954+
}
937955
if (message == WM_WINDOWPOSCHANGED) {
938956
const auto* pos = reinterpret_cast<const WINDOWPOS*>(lp);
939957
const LRESULT result = DefSubclassProc(hwnd, message, wp, lp);
@@ -957,6 +975,10 @@ static LRESULT WindowLifetimeMessage(HWND hwnd, UINT message, WPARAM wp, LPARAM
957975
!GetPropW(hwnd, kFramelessClientProperty)) {
958976
ClearHiddenTitleBarNonClientBorder(hwnd);
959977
}
978+
if (RemovePropW(hwnd, kFullScreenDwmSyncProperty)) {
979+
SetPropW(hwnd, kFullScreenSyncPendingProperty, reinterpret_cast<HANDLE>(1));
980+
PostMessageW(hwnd, SyncFullScreenClientMessage(), 0, 0);
981+
}
960982
return result;
961983
}
962984
if (auto handled = HandleMaximizeButtonArea(hwnd, message, wp, lp)) return *handled;
@@ -1483,19 +1505,55 @@ bool Window::IsMinimized() const {
14831505
// resize their child surface to the final post-frame-change client rect and
14841506
// repaint immediately after entering or leaving fullscreen, without waiting
14851507
// for a focus change (Alt+Tab).
1486-
static void SyncFullScreenClientArea(HWND hwnd) {
1508+
static void SyncFullScreenClientArea(HWND hwnd, bool schedule_deferred) {
14871509
if (!hwnd || !IsWindow(hwnd) || IsIconic(hwnd))
14881510
return;
14891511
RECT client = {};
14901512
if (GetClientRect(hwnd, &client)) {
1513+
const int width = client.right - client.left;
1514+
const int height = client.bottom - client.top;
14911515
const WPARAM size_type = IsZoomed(hwnd) ? SIZE_MAXIMIZED : SIZE_RESTORED;
1492-
SendMessageW(hwnd, WM_SIZE, size_type,
1493-
MAKELPARAM(client.right - client.left, client.bottom - client.top));
1516+
const LPARAM size_param = MAKELPARAM(width, height);
1517+
SendMessageW(hwnd, WM_SIZE, size_type, size_param);
1518+
1519+
// Win32's MoveWindow/SetWindowPos skips WM_SIZE on a child whose HWND
1520+
// rect already matches (width, height), even if the child's swapchain or
1521+
// Flutter view missed or timed out an earlier resize while Dart was
1522+
// blocked inside the synchronous FFI call or while DWM was transitioning
1523+
// fullscreen state. Ensure any FLUTTERVIEW child both matches the final
1524+
// client rect and receives WM_SIZE directly.
1525+
HWND flutter_view = FindWindowExW(hwnd, nullptr, L"FLUTTERVIEW", nullptr);
1526+
if (flutter_view && IsWindow(flutter_view)) {
1527+
RECT view_rect = {};
1528+
if (!GetClientRect(flutter_view, &view_rect) ||
1529+
(view_rect.right - view_rect.left) != width ||
1530+
(view_rect.bottom - view_rect.top) != height) {
1531+
SetWindowPos(flutter_view, nullptr, client.left, client.top, width, height,
1532+
SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE);
1533+
}
1534+
SendMessageW(flutter_view, WM_SIZE, size_type, size_param);
1535+
RedrawWindow(flutter_view, nullptr, nullptr,
1536+
RDW_INVALIDATE | RDW_NOERASE | RDW_UPDATENOW);
1537+
if (GetForegroundWindow() == hwnd) {
1538+
HWND focused = GetFocus();
1539+
if (!focused || GetAncestor(focused, GA_ROOT) != hwnd)
1540+
SetFocus(flutter_view);
1541+
}
1542+
}
14941543
}
14951544
UINT flags = RDW_INVALIDATE | RDW_NOERASE | RDW_ALLCHILDREN | RDW_UPDATENOW;
14961545
if (!GetPropW(hwnd, kTitleBarHiddenProperty))
14971546
flags |= RDW_FRAME;
14981547
RedrawWindow(hwnd, nullptr, nullptr, flags);
1548+
1549+
if (schedule_deferred) {
1550+
// Re-sync on the next message-loop turn (after SetFullScreen returns and
1551+
// unblocks the caller's thread/isolate) and again once dwm.exe delivers
1552+
// its asynchronous WM_DWMNCRENDERINGCHANGED notification.
1553+
SetPropW(hwnd, kFullScreenSyncPendingProperty, reinterpret_cast<HANDLE>(1));
1554+
SetPropW(hwnd, kFullScreenDwmSyncProperty, reinterpret_cast<HANDLE>(1));
1555+
PostMessageW(hwnd, SyncFullScreenClientMessage(), 0, 0);
1556+
}
14991557
}
15001558

15011559
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)
15391597
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE |
15401598
SWP_NOCOPYBITS | SWP_FRAMECHANGED);
15411599
if (sync_client)
1542-
SyncFullScreenClientArea(hwnd);
1600+
SyncFullScreenClientArea(hwnd, /*schedule_deferred=*/true);
15431601
NotifyWindowFullScreenChanged(hwnd, false);
15441602
return true;
15451603
}
@@ -1613,7 +1671,7 @@ void Window::SetFullScreen(bool is_full_screen) {
16131671
monitor.rcMonitor.right - monitor.rcMonitor.left,
16141672
monitor.rcMonitor.bottom - monitor.rcMonitor.top,
16151673
SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_FRAMECHANGED);
1616-
SyncFullScreenClientArea(hwnd);
1674+
SyncFullScreenClientArea(hwnd, /*schedule_deferred=*/true);
16171675
NotifyWindowFullScreenChanged(hwnd, true);
16181676
return;
16191677
}

‎tests/window_fullscreen_hidden_titlebar_windows_test.cpp‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ HBRUSH g_green_brush = nullptr;
2323
int g_size_count = 0;
2424
int g_last_size_w = 0;
2525
int g_last_size_h = 0;
26+
int g_flutter_view_size_count = 0;
27+
int g_flutter_view_last_w = 0;
28+
int g_flutter_view_last_h = 0;
2629
bool g_watching_top_band = false;
2730
bool g_top_band_polluted = false;
2831

@@ -52,6 +55,16 @@ LRESULT CALLBACK OuterWatchProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp, U
5255
return r;
5356
}
5457

58+
LRESULT CALLBACK FlutterViewProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp) {
59+
if (message == WM_SIZE) {
60+
++g_flutter_view_size_count;
61+
g_flutter_view_last_w = LOWORD(lp);
62+
g_flutter_view_last_h = HIWORD(lp);
63+
return 0;
64+
}
65+
return DefWindowProcW(hwnd, message, wp, lp);
66+
}
67+
5568
LRESULT CALLBACK HostProc(HWND hwnd, UINT message, WPARAM wp, LPARAM lp) {
5669
if (message == WM_ERASEBKGND) {
5770
RECT rc = {};
@@ -241,6 +254,42 @@ int main() {
241254
(post_max_client.bottom - post_max_client.top) == max_h,
242255
"maximized -> fullscreen -> maximized restores child surface to work area");
243256

257+
// 4. Regression for libnativeapi/nativeapi#79: FLUTTERVIEW child must receive
258+
// a deferred WM_SIZE on the next message-loop turn (after SetFullScreen
259+
// returns) and again after WM_DWMNCRENDERINGCHANGED, even when its HWND
260+
// rect already matches the parent client area.
261+
ShowWindow(hwnd, SW_RESTORE);
262+
Pump();
263+
WNDCLASSW fv_cls = {};
264+
fv_cls.lpfnWndProc = FlutterViewProc;
265+
fv_cls.hInstance = cls.hInstance;
266+
fv_cls.lpszClassName = L"FLUTTERVIEW";
267+
RegisterClassW(&fv_cls);
268+
HWND flutter_view = CreateWindowW(L"FLUTTERVIEW", L"", WS_CHILD, 0, 0, 100, 100, hwnd, nullptr,
269+
cls.hInstance, nullptr);
270+
Check(flutter_view != nullptr, "created FLUTTERVIEW child window");
271+
272+
g_flutter_view_size_count = 0;
273+
window.SetFullScreen(true);
274+
const int fv_count_after_sync_enter = g_flutter_view_size_count;
275+
Pump();
276+
const int fv_count_after_deferred_enter = g_flutter_view_size_count;
277+
Check(fv_count_after_sync_enter > 0 && fv_count_after_deferred_enter > fv_count_after_sync_enter &&
278+
g_flutter_view_last_w == mon_w && g_flutter_view_last_h == mon_h &&
279+
ChildMatchesClient(hwnd, flutter_view),
280+
"enter fullscreen delivers both immediate and deferred WM_SIZE to FLUTTERVIEW");
281+
282+
window.SetFullScreen(false);
283+
const int fv_count_after_sync_exit = g_flutter_view_size_count;
284+
Pump();
285+
const int fv_count_after_deferred_exit = g_flutter_view_size_count;
286+
Check(fv_count_after_sync_exit > fv_count_after_deferred_enter &&
287+
fv_count_after_deferred_exit > fv_count_after_sync_exit &&
288+
g_flutter_view_last_w == initial_w && g_flutter_view_last_h == initial_h &&
289+
ChildMatchesClient(hwnd, flutter_view),
290+
"exit fullscreen delivers both immediate and deferred WM_SIZE to FLUTTERVIEW");
291+
292+
DestroyWindow(flutter_view);
244293
DestroyWindow(hwnd);
245294
DeleteObject(g_green_brush);
246295
return failures ? 1 : 0;

0 commit comments

Comments
 (0)