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
172 changes: 150 additions & 22 deletions src/platform/windows/window_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ static const wchar_t* kHiddenFromTaskbarProperty = L"NativeAPIHiddenFromTaskbar"
static const wchar_t* kNonFocusableProperty = L"NativeAPINonFocusable";
static const wchar_t* kNonActivatingProperty = L"NativeAPINonActivating";
static const wchar_t* kMouseInputPolicyProperty = L"NativeAPIMouseInputPolicy";
// Set while DWM has detached non-client rendering (WM_DWMNCRENDERINGCHANGED with
// wParam == FALSE, such as during borderless fullscreen and until dwm.exe
// asynchronously re-attaches after leaving fullscreen). Unlike
// 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";

struct MouseInputPolicy {
bool forward = false;
Expand Down Expand Up @@ -329,15 +335,17 @@ static LONG_PTR CaptionStyle(HWND hwnd) {
return full == g_full_screen_windows.end() ? GetWindowLongPtrW(hwnd, GWL_STYLE)
: full->second.style;
}
static void SetCaptionStyle(HWND hwnd, LONG_PTR style) {
static void SetCaptionStyle(HWND hwnd, LONG_PTR style, bool notify_frame_changed = true) {
constexpr LONG_PTR buttons = WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
auto full = g_full_screen_windows.find(hwnd);
if (full != g_full_screen_windows.end())
full->second.style = (full->second.style & ~buttons) | (style & buttons);
const auto actual = GetWindowLongPtrW(hwnd, GWL_STYLE);
SetWindowLongPtrW(hwnd, GWL_STYLE, (actual & ~buttons) | (style & buttons));
SetWindowPos(hwnd, nullptr, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
if (notify_frame_changed) {
SetWindowPos(hwnd, nullptr, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
}
}
static bool NativeClosable(HWND hwnd) {
if (g_full_screen_windows.count(hwnd)) {
Expand All @@ -364,18 +372,23 @@ static ControlButtonPolicy* ControlPolicy(HWND hwnd, bool create) {
}
return policy;
}
static void ApplyControlPolicy(HWND hwnd, const ControlButtonPolicy& policy) {
static void ApplyControlPolicy(HWND hwnd, const ControlButtonPolicy& policy,
bool notify_frame_changed = true) {
auto style = CaptionStyle(hwnd);
// The app-drawn caption still needs the system menu and MIN/MAX styles for
// native commands and Windows 11 snap layouts while the title bar is hidden.
const bool system_menu = policy.visible || GetPropW(hwnd, kTitleBarHiddenProperty);
if (system_menu) style |= WS_SYSMENU;
else style &= ~WS_SYSMENU;
SetCaptionStyle(hwnd, style);
SetCaptionStyle(hwnd, style, notify_frame_changed);
if (HMENU menu = GetSystemMenu(hwnd, FALSE)) {
EnableMenuItem(menu, SC_CLOSE, MF_BYCOMMAND |
(policy.closable ? MF_ENABLED : (MF_DISABLED | MF_GRAYED)));
DrawMenuBar(hwnd);
// DrawMenuBar triggers classic non-client caption painting when DWM
// non-client rendering is temporarily off (e.g. leaving fullscreen); a
// hidden-title-bar window has no native caption bar to redraw.
if (!GetPropW(hwnd, kTitleBarHiddenProperty))
DrawMenuBar(hwnd);
}
}

Expand Down Expand Up @@ -579,6 +592,24 @@ static BOOL CALLBACK AttachTopEdgeChild(HWND child, LPARAM) {
return TRUE;
}

// Clears the non-client resize border (WindowRect - ClientRect) of a
// compositor-framed hidden-title-bar window to GDI black (BGRA 0x00000000,
// which DWM composites as transparent), removing any opaque Aero Basic border
// pixels painted while DWM non-client rendering was detached in fullscreen.
static void ClearHiddenTitleBarNonClientBorder(HWND hwnd) {
HDC wdc = GetWindowDC(hwnd);
if (!wdc) return;
RECT wr = {}, cr = {};
if (GetWindowRect(hwnd, &wr) && GetClientRect(hwnd, &cr)) {
MapWindowPoints(hwnd, nullptr, reinterpret_cast<POINT*>(&cr), 2);
OffsetRect(&cr, -wr.left, -wr.top);
OffsetRect(&wr, -wr.left, -wr.top);
ExcludeClipRect(wdc, cr.left, cr.top, cr.right, cr.bottom);
FillRect(wdc, &wr, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
}
ReleaseDC(hwnd, wdc);
}

static std::optional<LRESULT> HandleHiddenTitleBarFrame(HWND hwnd, UINT message, WPARAM wp,
LPARAM lp) {
if (message == WM_PARENTNOTIFY && LOWORD(wp) == WM_CREATE) {
Expand Down Expand Up @@ -626,13 +657,60 @@ static std::optional<LRESULT> HandleHiddenTitleBarFrame(HWND hwnd, UINT message,
const LRESULT edge = HiddenFrameResizeHit(hwnd, lp);
return edge != 0 ? edge : hit;
}
// Without the compositor's frame, DefWindowProc paints the classic frame and caption
// that the window style still asks for, straight over the content at the window's
// edges: on every step of a resize (WM_NCPAINT), on activation (WM_NCACTIVATE, and the
// undocumented WM_NCUAHDRAWCAPTION / WM_NCUAHDRAWFRAME the theme sends) and when the
// title or icon changes. The content repaints over it a moment later, so it flickers.
// There is no frame to paint, so none of that is let through.
if (!frameless) return std::nullopt;
// Never let legacy UxTheme paint a classic Win7-style caption or frame over a
// hidden-title-bar window (e.g. during style/placement transitions such as
// leaving borderless fullscreen before DWM re-composes the non-client area).
constexpr UINT kNcUahDrawCaption = 0x00AE;
constexpr UINT kNcUahDrawFrame = 0x00AF;
if (message == kNcUahDrawCaption || message == kNcUahDrawFrame)
return 0;
// When the compositor owns the frame (!frameless), never alter WM_NCPAINT's
// wParam (passing a custom HRGN disables DWM non-client rendering) and leave
// messages untouched whenever dwm.exe is actively compositing. Only while
// dwm.exe is detached (between WM_DWMNCRENDERINGCHANGED(FALSE) in fullscreen
// and WM_DWMNCRENDERINGCHANGED(TRUE) after leaving fullscreen), preserve the
// top client band across DefSubclassProc(WM_NCPAINT) and clear the resize
// borders back to transparent black so neither the top classic caption nor
// the left/right/bottom classic border ever appears.
if (!frameless) {
if (!GetPropW(hwnd, kDwmNcDisabledProperty))
return std::nullopt;
if (message == WM_NCPAINT) {
RECT wr = {}, cr = {};
GetWindowRect(hwnd, &wr);
GetClientRect(hwnd, &cr);
MapWindowPoints(hwnd, nullptr, reinterpret_cast<POINT*>(&cr), 2);
const int cw = cr.right - cr.left;
const int ch = cr.bottom - cr.top;
const int band_h = ch < 80 ? ch : 80;
const int ox = cr.left - wr.left;
const int oy = cr.top - wr.top;
HDC wdc = (cw > 0 && band_h > 0) ? GetWindowDC(hwnd) : nullptr;
HDC mem = wdc ? CreateCompatibleDC(wdc) : nullptr;
HBITMAP saved = (wdc && mem) ? CreateCompatibleBitmap(wdc, cw, band_h) : nullptr;
HGDIOBJ old = saved ? SelectObject(mem, saved) : nullptr;
if (saved)
BitBlt(mem, 0, 0, cw, band_h, wdc, ox, oy, SRCCOPY);
if (wdc)
ReleaseDC(hwnd, wdc);

const LRESULT r = DefSubclassProc(hwnd, message, wp, lp);

if (saved) {
wdc = GetWindowDC(hwnd);
if (wdc) {
BitBlt(wdc, ox, oy, cw, band_h, mem, 0, 0, SRCCOPY);
ReleaseDC(hwnd, wdc);
}
SelectObject(mem, old);
DeleteObject(saved);
}
if (mem)
DeleteDC(mem);
ClearHiddenTitleBarNonClientBorder(hwnd);
return r;
}
}
if (message == WM_WINDOWPOSCHANGING && lp) {
auto* pos = reinterpret_cast<WINDOWPOS*>(lp);
if (!(pos->flags & SWP_NOMOVE)) {
Expand All @@ -643,9 +721,7 @@ static std::optional<LRESULT> HandleHiddenTitleBarFrame(HWND hwnd, UINT message,
}
return std::nullopt;
}
constexpr UINT kNcUahDrawCaption = 0x00AE;
constexpr UINT kNcUahDrawFrame = 0x00AF;
if (message == WM_NCPAINT || message == kNcUahDrawCaption || message == kNcUahDrawFrame)
if (message == WM_NCPAINT)
return 0;
// lParam -1 keeps the activation handling but skips the repaint.
if (message == WM_NCACTIVATE) return DefSubclassProc(hwnd, message, wp, -1);
Expand Down Expand Up @@ -823,6 +899,7 @@ static LRESULT WindowLifetimeMessage(HWND hwnd, UINT message, WPARAM wp, LPARAM
RemovePropW(hwnd, kCornerPreferenceProperty);
RemovePropW(hwnd, kBackgroundColorProperty);
RemovePropW(hwnd, kTranslucentBackgroundProperty);
RemovePropW(hwnd, kDwmNcDisabledProperty);
g_full_screen_windows.erase(hwnd);
const auto result = DefSubclassProc(hwnd, message, wp, lp);
WindowRegistry::GetInstance().Remove(static_cast<WindowId>(reference));
Expand Down Expand Up @@ -867,13 +944,26 @@ static LRESULT WindowLifetimeMessage(HWND hwnd, UINT message, WPARAM wp, LPARAM
return result;
}
#ifndef NATIVEAPI_ENABLE_WINUI3
constexpr UINT kWmDwmNcRenderingChanged = 0x031F;
if (message == kWmDwmNcRenderingChanged) {
if (wp)
RemovePropW(hwnd, kDwmNcDisabledProperty);
else
SetPropW(hwnd, kDwmNcDisabledProperty, reinterpret_cast<HANDLE>(1));
const LRESULT result = DefSubclassProc(hwnd, message, wp, lp);
if (wp && GetPropW(hwnd, kTitleBarHiddenProperty) &&
!GetPropW(hwnd, kFramelessClientProperty)) {
ClearHiddenTitleBarNonClientBorder(hwnd);
}
return result;
}
if (auto handled = HandleMaximizeButtonArea(hwnd, message, wp, lp)) return *handled;
if (message == WM_PARENTNOTIFY && LOWORD(wp) == WM_CREATE &&
GetPropW(hwnd, kMaximizeButtonProperty))
AttachTopEdgeChild(reinterpret_cast<HWND>(lp), 0);
if (message == WM_NCCALCSIZE || message == WM_NCHITTEST || message == WM_PARENTNOTIFY ||
message == WM_NCACTIVATE || message == WM_NCPAINT || message == 0x00AE ||
message == 0x00AF || message == WM_SETTEXT || message == WM_SETICON) {
message == WM_WINDOWPOSCHANGING || message == WM_NCACTIVATE || message == WM_NCPAINT ||
message == 0x00AE || message == 0x00AF || message == WM_SETTEXT || message == WM_SETICON) {
if (auto handled = HandleHiddenTitleBarFrame(hwnd, message, wp, lp)) return *handled;
}
#endif
Expand Down Expand Up @@ -904,7 +994,16 @@ static bool TrackWindowLifetime(HWND hwnd, WindowId id) {
reinterpret_cast<UINT_PTR>(&WindowLifetimeProc), &existing);
const bool installed = SetWindowSubclass(
hwnd, WindowLifetimeProc, reinterpret_cast<UINT_PTR>(&WindowLifetimeProc), id) != FALSE;
return installed && !tracked;
if (installed && !tracked) {
BOOL nc_enabled = FALSE;
if (!SUCCEEDED(DwmGetWindowAttribute(hwnd, DWMWA_NCRENDERING_ENABLED, &nc_enabled,
sizeof(nc_enabled))) ||
!nc_enabled) {
SetPropW(hwnd, kDwmNcDisabledProperty, reinterpret_cast<HANDLE>(1));
}
return true;
}
return false;
}

// Forward declaration
Expand Down Expand Up @@ -1378,6 +1477,25 @@ bool Window::IsMinimized() const {
return IsIconic(pimpl_->hwnd_) != FALSE;
}

// Ensures host windows (such as Flutter's Win32Window hosting FLUTTERVIEW)
// 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) {
if (!hwnd || !IsWindow(hwnd) || IsIconic(hwnd))
return;
RECT client = {};
if (GetClientRect(hwnd, &client)) {
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));
}
UINT flags = RDW_INVALIDATE | RDW_NOERASE | RDW_ALLCHILDREN | RDW_UPDATENOW;
if (!GetPropW(hwnd, kTitleBarHiddenProperty))
flags |= RDW_FRAME;
RedrawWindow(hwnd, nullptr, nullptr, flags);
}

void Window::SetFullScreen(bool is_full_screen) {
HWND owner_window = pimpl_->hwnd_;
if (owner_window && GetWindowThreadProcessId(owner_window, nullptr) != GetCurrentThreadId()) {
Expand Down Expand Up @@ -1415,7 +1533,8 @@ void Window::SetFullScreen(bool is_full_screen) {
SetWindowPos(hwnd, nullptr, monitor.rcMonitor.left, monitor.rcMonitor.top,
monitor.rcMonitor.right - monitor.rcMonitor.left,
monitor.rcMonitor.bottom - monitor.rcMonitor.top,
SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_FRAMECHANGED);
SyncFullScreenClientArea(hwnd);
NotifyWindowFullScreenChanged(hwnd, true);
return;
}
Expand All @@ -1433,10 +1552,19 @@ void Window::SetFullScreen(bool is_full_screen) {
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_FRAMECHANGED);
if (auto* policy = ControlPolicy(hwnd, false)) ApplyControlPolicy(hwnd, *policy);
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOACTIVATE |
SWP_NOCOPYBITS | SWP_FRAMECHANGED);
SyncFullScreenClientArea(hwnd);
NotifyWindowFullScreenChanged(hwnd, false);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -690,3 +690,12 @@ if(APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL "iOS")
add_test(NAME message_dialog_macos_sheet_test COMMAND message_dialog_macos_sheet_test)
set_tests_properties(message_dialog_macos_sheet_test PROPERTIES TIMEOUT 15)
endif()

if(WIN32)
add_executable(window_fullscreen_hidden_titlebar_windows_test
window_fullscreen_hidden_titlebar_windows_test.cpp)
target_link_libraries(window_fullscreen_hidden_titlebar_windows_test PRIVATE nativeapi)
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()
Loading
Loading