From fbedfad478d89ed8f6a0ef76f39cea1d11692e14 Mon Sep 17 00:00:00 2001 From: alexei-zebra Date: Sun, 11 Oct 2026 20:06:11 +0300 Subject: [PATCH] Fix the issues found while reviewing the engine frame Use util::str2wstr_utf8 for the window title instead of the deprecated codecvt conversion, choose the bar control icon with a switch, and keep the frontend layout in sync when a frame is drawn from the refresh callback the platform calls while it blocks the main loop. The frame forgets its interaction state when the window mode changes or the cursor gets locked, a bar click that only moved the window is no longer a double click, and one frame per size is drawn per resize. The tooltips of the two window settings were only present in the Russian locale and under a key the settings page never asks for: they are named display..tooltip now and translated in both locales. The version label of the debug build moves below the bar that covers the menu screen. --- res/texts/en_US.txt | 2 + res/texts/ru_RU.txt | 4 +- src/engine/WindowBar.cpp | 43 +++--- src/engine/WindowControl.cpp | 6 +- src/frontend/menu.cpp | 6 +- src/frontend/screens/MenuScreen.hpp | 1 + src/window/Window.hpp | 13 +- src/window/detail/FrameLayout.hpp | 8 +- src/window/detail/FrameOperations.cpp | 211 +++++++++++++------------- src/window/detail/GLFWWindow.cpp | 32 +++- 10 files changed, 173 insertions(+), 153 deletions(-) diff --git a/res/texts/en_US.txt b/res/texts/en_US.txt index 294758a7e..4a566ba6b 100644 --- a/res/texts/en_US.txt +++ b/res/texts/en_US.txt @@ -25,6 +25,8 @@ graphics.backlight.tooltip=Backlight to prevent total darkness graphics.dense-render.tooltip=Enables transparency in blocks like leaves graphics.soft-lighting.tooltip=Enables blocks soft lighting graphics.advanced-render.tooltip=Use graphics pipeline supporting advanced effects like shadows, SSAO +display.compact-window-bar.tooltip=Window bar 24 px high instead of 48 px +display.live-resize.tooltip=Redraw the window while it is resized (off: show the last frame) # settings settings.Controls Search Mode=Search by attached button name diff --git a/res/texts/ru_RU.txt b/res/texts/ru_RU.txt index efcd0ab96..f1cc9fc41 100644 --- a/res/texts/ru_RU.txt +++ b/res/texts/ru_RU.txt @@ -120,8 +120,8 @@ settings.Acoustic effects=Акустические эффекты 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=Перерисовывать окно при изменении размера (выключено — показывать последний кадр) +display.compact-window-bar.tooltip=Шапка окна высотой 24 px вместо 48 px +display.live-resize.tooltip=Перерисовывать окно при изменении размера (выключено — показывать последний кадр) # Управление chunks.reload=Перезагрузить Чанки diff --git a/src/engine/WindowBar.cpp b/src/engine/WindowBar.cpp index 68fdeabd8..b6097a60c 100644 --- a/src/engine/WindowBar.cpp +++ b/src/engine/WindowBar.cpp @@ -11,27 +11,15 @@ #include "graphics/ui/GUI.hpp" #include "window/Window.hpp" #include "assets/Assets.hpp" +#include "util/stringutil.hpp" #include #include -#include -#include - -namespace { - std::wstring to_wide(const std::string& text) { - std::wstring_convert> converter; - return converter.from_bytes(text); - } - - const std::string BAR_FONT = FONT_DEFAULT; -} /// @brief Draws the window bar the backend cannot draw itself. /// -/// The bar lives in window coordinates while the GUI container is shifted -/// down by its height in game, so the GUI camera cannot be reused here: the -/// bar sets its own viewport and projection. It borrows the GUI batch only -/// because the ui shader it needs is the one the GUI already loads. +/// The bar lives in window coordinates, so it sets the shader and projection +/// itself instead of depending on the state the GUI left behind. void draw_window_bar(Engine& engine) { auto& window = engine.getWindow(); const int height = window.getDecorationHeight(); @@ -39,7 +27,7 @@ void draw_window_bar(Engine& engine) { return; } auto* batch = engine.getGUI().getBatch2D(); - auto font = engine.requireAssets().getShared(BAR_FONT); + auto font = engine.requireAssets().getShared(FONT_DEFAULT); const auto size = window.getSize(); const float barHeight = static_cast(height); @@ -80,7 +68,7 @@ void draw_window_bar(Engine& engine) { } // the bar and its controls do not need the font, only the title does if (font != nullptr) { - const std::wstring title = to_wide(window.getTitle()); + const std::wstring title = util::str2wstr_utf8(window.getTitle()); const float available = std::max(0.0f, titleRight - titleLeft); size_t titleLength = title.size(); while (titleLength > 0 && @@ -124,13 +112,20 @@ void draw_window_bar(Engine& engine) { } const float ink = isHovered ? 1.0f : 0.8f; batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, ink)); - const char* iconName = - item.button == DecorationButton::CLOSE - ? "gui/cross" - : item.button == DecorationButton::MINIMIZE - ? "gui/minimize_w" - : window.isMaximized() ? "gui/restore_w" - : "gui/maximize_w"; + const char* iconName = nullptr; + switch (item.button) { + case DecorationButton::CLOSE: + iconName = "gui/cross"; + break; + case DecorationButton::MINIMIZE: + iconName = "gui/minimize_w"; + break; + case DecorationButton::MAXIMIZE: + default: + iconName = window.isMaximized() ? "gui/restore_w" + : "gui/maximize_w"; + break; + } auto icon = engine.requireAssets().getShared(iconName); if (icon != nullptr) { const float iconSize = std::min( diff --git a/src/engine/WindowControl.cpp b/src/engine/WindowControl.cpp index add00ba1a..eb70b777b 100644 --- a/src/engine/WindowControl.cpp +++ b/src/engine/WindowControl.cpp @@ -66,10 +66,10 @@ WindowControl::Result WindowControl::initialize() { } } - // Windows blocks the main loop inside its own modal loop while the user - // drags a window border, and asks for a repaint instead: drawing a frame - // from there is what keeps the content alive during such a resize. + // The platform blocks the main loop while it resizes the window, so the + // frame is drawn from here, with the frontend layout updated first. window->setRefreshHandler([this] { + engine.updateFrontend(); engine.renderFrame(); engine.getWindow().swapBuffers(); }); diff --git a/src/frontend/menu.cpp b/src/frontend/menu.cpp index ab06b42dc..3e91551e0 100644 --- a/src/frontend/menu.cpp +++ b/src/frontend/menu.cpp @@ -17,6 +17,7 @@ #include "screens/MenuScreen.hpp" #include "settings.hpp" #include "util/stringutil.hpp" +#include "window/Window.hpp" #include "assets/assetload_funcs.hpp" #include "content/ContentPack.hpp" @@ -24,11 +25,12 @@ using namespace gui; void menus::create_version_label(gui::GUI& gui) { auto text = ENGINE_VERSION_STRING + " debug build"; + // the menu screen keeps no inset, the bar covers the top of it + const int top = gui.getEngine().getWindow().getDecorationHeight() + 4; gui.add(guiutil::create( gui, "" + "margin='4," + std::to_string(top) + ",4,4'>" + text + "" )); } diff --git a/src/frontend/screens/MenuScreen.hpp b/src/frontend/screens/MenuScreen.hpp index 7f8f02ad5..ba91f5b22 100644 --- a/src/frontend/screens/MenuScreen.hpp +++ b/src/frontend/screens/MenuScreen.hpp @@ -15,6 +15,7 @@ public: void onOpen() override; + // the bar covers the top getDecorationHeight() pixels, no widgets there int getContentInset(const Window&) const override { return 0; } diff --git a/src/window/Window.hpp b/src/window/Window.hpp index 7fac8dff3..3e8b61e0a 100644 --- a/src/window/Window.hpp +++ b/src/window/Window.hpp @@ -17,7 +17,7 @@ struct DisplaySettings; enum class DecorationButton { CLOSE, MINIMIZE, - MAXIMIZE, + MAXIMIZE }; struct DecorationButtonLayout { @@ -73,10 +73,6 @@ public: return empty; } - /// @brief Whether the engine draws the window frame itself here - /// - /// The bar height and its buttons only mean something when it does, and - /// the window settings are only shown where that is the case. /// @brief Handler the windowing system calls when it wants a repaint /// /// Windows runs a modal loop while the user drags a window border and the @@ -92,6 +88,10 @@ public: return refreshHandler; } + /// @brief Whether the engine draws the window frame itself here + /// + /// The bar height and its buttons only mean something when it does, and + /// the window settings are only shown where that is the case. virtual bool hasEngineFrame() const { return false; } @@ -106,7 +106,8 @@ public: return -1; } - virtual const std::vector& getDecorationButtons() const { + virtual const std::vector& getDecorationButtons() + const { static const std::vector empty; return empty; } diff --git a/src/window/detail/FrameLayout.hpp b/src/window/detail/FrameLayout.hpp index ae3394457..42542eac3 100644 --- a/src/window/detail/FrameLayout.hpp +++ b/src/window/detail/FrameLayout.hpp @@ -2,10 +2,10 @@ /// @brief Geometry of the window frame the engine draws itself. /// -/// Wayland has its own implementation because it also talks to the compositor, -/// this one is used by the backends that decorate their window by themselves. -/// It deliberately depends on nothing but the standard library, so the layout -/// and the hit testing can be checked without a window. +/// Wayland has its own implementation because it also talks to the compositor. +/// This one is used by the GLFW backends, on an undecorated window, and +/// depends on nothing but the standard library, so the layout and the hit +/// testing can be checked without a window. namespace frame { constexpr int BAR_HEIGHT = 48; constexpr int BAR_HEIGHT_COMPACT = 24; diff --git a/src/window/detail/FrameOperations.cpp b/src/window/detail/FrameOperations.cpp index 6c5ebff46..6eed04f7f 100644 --- a/src/window/detail/FrameOperations.cpp +++ b/src/window/detail/FrameOperations.cpp @@ -1,6 +1,107 @@ #include "window/detail/FrameOperations.hpp" -#if defined(VOXEL_X11_FRAME) +#if defined(_WIN32) +#define NOMINMAX +#include +#include + +#include + +#define GLFW_EXPOSE_NATIVE_WIN32 +#include + +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 + ) { + switch (message) { + case WM_NCCALCSIZE: + // the caption is gone, the whole window is the client area + if (wparam == TRUE) { + return 0; + } + break; + case WM_NCPAINT: + // the frame is not drawn by us + return 0; + 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; + } + POINT point {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)}; + ScreenToClient(hwnd, &point); + const frame::Hit hit = + hit_test_callback(hit_test_userdata, point.x, point.y); + if (hit.caption) { + return HTCAPTION; + } + switch (hit.edges) { + case frame::EDGE_TOP | frame::EDGE_LEFT: + return HTTOPLEFT; + case frame::EDGE_TOP: + return HTTOP; + case frame::EDGE_TOP | frame::EDGE_RIGHT: + return HTTOPRIGHT; + case frame::EDGE_RIGHT: + return HTRIGHT; + case frame::EDGE_BOTTOM | frame::EDGE_RIGHT: + return HTBOTTOMRIGHT; + case frame::EDGE_BOTTOM: + return HTBOTTOM; + case frame::EDGE_BOTTOM | frame::EDGE_LEFT: + return HTBOTTOMLEFT; + case frame::EDGE_LEFT: + return HTLEFT; + default: + return HTCLIENT; + } + } + } + return CallWindowProc( + original_window_proc, hwnd, message, wparam, lparam + ); + } +} + +#elif defined(VOXEL_X11_FRAME) #include #include #include @@ -158,111 +259,6 @@ namespace { return true; } } - -#elif defined(_WIN32) -#define NOMINMAX -#include -#include - -#include - -#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 - ) { - switch (message) { - case WM_NCCALCSIZE: - // the caption is gone, the whole window is the client area - if (wparam == TRUE) { - return 0; - } - break; - case WM_NCPAINT: - // the frame is not drawn by us - return 0; - 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; - } - POINT point {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)}; - ScreenToClient(hwnd, &point); - const frame::Hit hit = - hit_test_callback(hit_test_userdata, point.x, point.y); - if (hit.caption) { - return HTCAPTION; - } - switch (hit.edges) { - case frame::EDGE_TOP | frame::EDGE_LEFT: - return HTTOPLEFT; - case frame::EDGE_TOP: - return HTTOP; - case frame::EDGE_TOP | frame::EDGE_RIGHT: - return HTTOPRIGHT; - case frame::EDGE_RIGHT: - return HTRIGHT; - case frame::EDGE_BOTTOM | frame::EDGE_RIGHT: - return HTBOTTOMRIGHT; - case frame::EDGE_BOTTOM: - return HTBOTTOM; - case frame::EDGE_BOTTOM | frame::EDGE_LEFT: - return HTBOTTOMLEFT; - case frame::EDGE_LEFT: - return HTLEFT; - default: - return HTCLIENT; - } - } - } - return CallWindowProc( - original_window_proc, hwnd, message, wparam, lparam - ); - } -} #endif bool frame_has_native_operations() { @@ -321,9 +317,6 @@ 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, diff --git a/src/window/detail/GLFWWindow.cpp b/src/window/detail/GLFWWindow.cpp index 91e684dd6..db735d7b1 100644 --- a/src/window/detail/GLFWWindow.cpp +++ b/src/window/detail/GLFWWindow.cpp @@ -379,7 +379,7 @@ public: /// @brief Whether the window is redrawn while it is being resized bool isLiveResizeEnabled() const { - return settings == nullptr || settings->liveResize.get(); + return settings->liveResize.get(); } /// @brief Draws a frame for the windowing system @@ -388,14 +388,19 @@ public: /// 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()) { + if (inRefreshCallback || !isLiveResizeEnabled()) { return; } const auto& refresh = getRefreshHandler(); if (!refresh) { return; } + // the system may ask twice for one resize + if (size == lastRefreshSize) { + return; + } inRefreshCallback = true; + lastRefreshSize = size; refresh(); inRefreshCallback = false; } @@ -445,6 +450,7 @@ public: /// @brief Turns a pointer move into hovering, dragging or resizing void handleFrameMotion(double x, double y) { if (input.isCursorLocked() || barHeight() <= 0) { + resetFrameInteraction(); hoveredButton = -1; frameCursorActive = false; return; @@ -494,12 +500,19 @@ public: ); if (hit.caption) { const double now = glfwGetTime(); - if (lastBarClick > 0.0 && now - lastBarClick < DOUBLE_CLICK_TIME) { + int windowX = 0, windowY = 0; + glfwGetWindowPos(window, &windowX, &windowY); + // a window manager drag moves the window, and it must not leave + // the previous click able to turn the next one into a double one + if (lastBarClick > 0.0 && now - lastBarClick < DOUBLE_CLICK_TIME && + windowX == lastBarClickX && windowY == lastBarClickY) { lastBarClick = 0.0; toggleMaximize(); return true; } lastBarClick = now; + lastBarClickX = windowX; + lastBarClickY = windowY; if (frame_start_move(window)) { return true; } @@ -555,6 +568,7 @@ public: void setMode(WindowMode mode) override { Window::mode = mode; + resetFrameInteraction(); GLFWmonitor* monitor = get_window_monitor(window); const GLFWvidmode* glfwMode = glfwGetVideoMode(monitor); @@ -671,6 +685,14 @@ private: ); } + /// @brief Drops the interaction the frame can no longer keep + void resetFrameInteraction() { + dragging = false; + resizeEdges = 0; + pressedButton = -1; + frameMoved = false; + } + void updateFrameHover(double x, double y) { const int height = barHeight(); frame::Rect rects[frame::MAX_BUTTONS]; @@ -854,6 +876,8 @@ private: double grabScreenX = 0.0; double grabScreenY = 0.0; double lastBarClick = 0.0; + int lastBarClickX = 0; + int lastBarClickY = 0; static constexpr double DOUBLE_CLICK_TIME = 0.4; static constexpr double MOVE_THRESHOLD = 4.0; static constexpr int MIN_WIDTH = 320; @@ -864,6 +888,8 @@ private: int posX = 0; int posY = 0; bool shouldRefresh = true; + /// @brief Size of the last frame drawn from the refresh callback + glm::ivec2 lastRefreshSize {0, 0}; }; static_assert(!std::is_abstract());