diff --git a/res/layouts/pages/settings_display.xml.lua b/res/layouts/pages/settings_display.xml.lua index 4525b92ed..b6eaa6fa4 100644 --- a/res/layouts/pages/settings_display.xml.lua +++ b/res/layouts/pages/settings_display.xml.lua @@ -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") diff --git a/res/texts/ru_RU.txt b/res/texts/ru_RU.txt index 38d4048c8..efcd0ab96 100644 --- a/res/texts/ru_RU.txt +++ b/res/texts/ru_RU.txt @@ -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=Перезагрузить Чанки diff --git a/src/window/detail/FrameOperations.cpp b/src/window/detail/FrameOperations.cpp index 76effe5f8..6c5ebff46 100644 --- a/src/window/detail/FrameOperations.cpp +++ b/src/window/detail/FrameOperations.cpp @@ -169,10 +169,31 @@ namespace { #define GLFW_EXPOSE_NATIVE_WIN32 #include +#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(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 } diff --git a/src/window/detail/FrameOperations.hpp b/src/window/detail/FrameOperations.hpp index 5871b0982..9cb8ecc0b 100644 --- a/src/window/detail/FrameOperations.hpp +++ b/src/window/detail/FrameOperations.hpp @@ -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 +); /// @} diff --git a/src/window/detail/GLFWWindow.cpp b/src/window/detail/GLFWWindow.cpp index 633af93b1..91e684dd6 100644 --- a/src/window/detail/GLFWWindow.cpp +++ b/src/window/detail/GLFWWindow.cpp @@ -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(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(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) {