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
160 changes: 116 additions & 44 deletions src/platform/windows/window_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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));
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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<int>(std::lround(size.width * scale)),
static_cast<int>(std::lround(size.height * scale)),
SWP_NOMOVE | SWP_NOZORDER);
if (sync_client)
SyncFullScreenClientArea(hwnd);
}
}

Expand All @@ -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<int>(std::lround(size.width * scale)) + borderWidth,
static_cast<int>(std::lround(size.height * scale)) + borderHeight,
SWP_NOMOVE | SWP_NOZORDER);
if (sync_client)
SyncFullScreenClientArea(hwnd);
}
}

Expand All @@ -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.
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
}

Expand Down
8 changes: 7 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
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()
Loading
Loading