Redraw the windows frame from WM_SIZE and keep its style

The refresh callback is triggered by WM_PAINT, but windows does not send it
while a border is dragged: the window class has neither CS_HREDRAW nor
CS_VREDRAW and nothing invalidates the client area. Only WM_SIZE arrives, so
nothing was drawn during such a resize. The frame is drawn from WM_SIZE now,
after forwarding the message to GLFW so that the new size is already in.

WM_NCHITTEST re-applies the style bits the engine frame needs and which GLFW
drops on every window mode change, taking the drag area with them. Every frame
setup logs whether the subclass had to be installed again, so a lost window
procedure is visible in the log.

The live resize switch is honoured by the GLFW backend as well: with it off the
frame from the message loop is skipped and the window keeps showing what the
system stretches.
This commit is contained in:
alexei-zebra 2026-10-11 01:05:47 +03:00
parent 4f9b125e17
commit 4d1b4a1638
No known key found for this signature in database
GPG key ID: D19D450AC3D9A80E
5 changed files with 100 additions and 25 deletions

View file

@ -73,14 +73,12 @@ function on_open()
"display.compact-window-bar.tooltip"
)
end
-- live rendering only has the cached frame as its alternative, which is
-- what the native wayland backend does
if app.get_window_backend() == "wayland" then
create_checkbox(
"display.live-resize", "Live Window Resize",
"display.live-resize.tooltip"
)
end
-- every backend has an alternative to redrawing during a resize: the
-- cached frame on wayland, the frame the system stretches elsewhere
create_checkbox(
"display.live-resize", "Live Window Resize",
"display.live-resize.tooltip"
)
create_checkbox("camera.shaking", "Camera Shaking")
create_checkbox("camera.inertia", "Camera Inertia")

View file

@ -121,7 +121,7 @@ settings.Microphone access=Доступ к микрофону
settings.Narrow Window Bar=Узкая шапка окна
settings.Live Window Resize=Живой ресайз окна
settings.display.compact-window-bar.tooltip=Шапка окна высотой 24 px вместо 48 px
settings.display.live-resize.tooltip=Перерисовывать содержимое окна при изменении размера (выключено — последний кадр растягивается)
settings.display.live-resize.tooltip=Перерисовывать окно при изменении размера (выключено — показывать последний кадр)
# Управление
chunks.reload=Перезагрузить Чанки

View file

@ -169,10 +169,31 @@ namespace {
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#include "debug/Logger.hpp"
static debug::Logger frame_logger("window-frame");
namespace {
WNDPROC original_window_proc = nullptr;
frame::Hit (*hit_test_callback)(void*, int, int) = nullptr;
void* hit_test_userdata = nullptr;
FrameRefresh refresh_callback = nullptr;
void* refresh_userdata = nullptr;
/// @brief Keeps the window style the engine frame relies on
///
/// GLFW rewrites the style on every window mode change, and a lost thick
/// frame takes the native resize border and the window snapping with it.
void apply_engine_style(HWND hwnd) {
const LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
const LONG_PTR wanted =
(style | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX |
WS_SYSMENU) &
~WS_CAPTION;
if (wanted != style) {
SetWindowLongPtr(hwnd, GWL_STYLE, wanted);
}
}
LRESULT CALLBACK frame_window_proc(
HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam
@ -190,7 +211,21 @@ namespace {
case WM_NCACTIVATE:
// and it is not repainted when the window is activated
return TRUE;
case WM_SIZE: {
// Windows runs its own modal loop while a border is dragged
// and never sends WM_PAINT there, so WM_SIZE is the only
// place to draw a frame from during such a resize.
const LRESULT result = CallWindowProc(
original_window_proc, hwnd, message, wparam, lparam
);
if (refresh_callback != nullptr) {
refresh_callback(refresh_userdata);
}
return result;
}
case WM_NCHITTEST: {
// the style may have been rewritten since the last setup
apply_engine_style(hwnd);
if (hit_test_callback == nullptr) {
break;
}
@ -261,7 +296,9 @@ bool frame_start_resize(GLFWwindow* window, int edges) {
void frame_setup_native(
GLFWwindow* window,
frame::Hit (*hitTest)(void*, int, int),
void* userdata
void* hitTestData,
FrameRefresh refresh,
void* refreshData
) {
#if defined(_WIN32)
HWND hwnd = glfwGetWin32Window(window);
@ -269,14 +306,13 @@ void frame_setup_native(
return;
}
hit_test_callback = hitTest;
hit_test_userdata = userdata;
hit_test_userdata = hitTestData;
refresh_callback = refresh;
refresh_userdata = refreshData;
// The native frame stays: its thick frame is what gives the window the
// resize border, the shadow, the animations and the snap layouts. Only
// the caption is hidden, and the client area covers the whole window.
LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
style |= WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU;
style &= ~WS_CAPTION;
SetWindowLongPtr(hwnd, GWL_STYLE, style);
apply_engine_style(hwnd);
// Only subclass once: storing our own procedure as the original one
// would send every unhandled message back into it, forever.
const LONG_PTR current = GetWindowLongPtr(hwnd, GWLP_WNDPROC);
@ -285,6 +321,9 @@ void frame_setup_native(
SetWindowLongPtr(
hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(frame_window_proc)
);
frame_logger.info() << "window frame: subclass installed";
} else {
frame_logger.info() << "window frame: subclass was ours";
}
SetWindowPos(
hwnd,
@ -298,6 +337,8 @@ void frame_setup_native(
#else
(void)window;
(void)hitTest;
(void)userdata;
(void)hitTestData;
(void)refresh;
(void)refreshData;
#endif
}

View file

@ -21,8 +21,18 @@ bool frame_start_move(GLFWwindow* window);
/// @brief Ask the window manager to start resizing the window
bool frame_start_resize(GLFWwindow* window, int edges);
/// @brief Called when the windowing system wants the content redrawn
using FrameRefresh = void (*)(void*);
/// @brief Hide the caption of the native frame, keeping the rest of it
/// @param hitTest tells what the pointer is over
void frame_setup_native(GLFWwindow* window, frame::Hit (*hitTest)(void*, int, int), void* userdata);
/// @param refresh draws a frame when the system blocks the main loop
void frame_setup_native(
GLFWwindow* window,
frame::Hit (*hitTest)(void*, int, int),
void* hitTestData,
FrameRefresh refresh,
void* refreshData
);
/// @}

View file

@ -297,7 +297,9 @@ public:
window(window),
engineFrame(engineFrame) {
if (engineFrame) {
frame_setup_native(window, &frame_hit_test, this);
frame_setup_native(
window, &frame_hit_test, this, &frame_refresh_thunk, this
);
}
}
@ -375,6 +377,33 @@ public:
return engineFrame;
}
/// @brief Whether the window is redrawn while it is being resized
bool isLiveResizeEnabled() const {
return settings == nullptr || settings->liveResize.get();
}
/// @brief Draws a frame for the windowing system
///
/// Windows runs a modal loop while a border is dragged and blocks the main
/// loop inside it, so the only frames drawn during such a resize are the
/// ones asked for from the message loop.
void drawRefreshFrame() {
if (!isLiveResizeEnabled()) {
return;
}
const auto& refresh = getRefreshHandler();
if (!refresh) {
return;
}
inRefreshCallback = true;
refresh();
inRefreshCallback = false;
}
static void frame_refresh_thunk(void* userdata) {
static_cast<GLFWWindow*>(userdata)->drawRefreshFrame();
}
/// @brief Set while a frame is drawn from the refresh callback
bool inRefreshCallback = false;
@ -758,7 +787,9 @@ private:
if (engineFrame) {
// GLFW rewrites the window style on every mode change and drops
// the thick frame the native resize border and snapping need
frame_setup_native(window, &frame_hit_test, this);
frame_setup_native(
window, &frame_hit_test, this, &frame_refresh_thunk, this
);
return;
}
glfwSetWindowAttrib(window, GLFW_DECORATED, GLFW_TRUE);
@ -922,12 +953,7 @@ static void create_standard_cursors() {
static void refresh_callback(GLFWwindow* window) {
auto handler = static_cast<GLFWWindow*>(glfwGetWindowUserPointer(window));
handler->setShouldRefresh();
const auto& refresh = handler->getRefreshHandler();
if (refresh) {
handler->inRefreshCallback = true;
refresh();
handler->inRefreshCallback = false;
}
handler->drawRefreshFrame();
}
static void setup_callbacks(GLFWwindow* window) {