Fix the engine window frame on windows

Four things came out of testing on windows.

The window settings were hidden outside of wayland because the check used
the backend name, while the bar height matters wherever the engine draws
the frame. The window now reports that (hasEngineFrame: barEnabled on
wayland, engineFrame on glfw) and the display page asks for it. Only the
live resize switch stays wayland specific, since the glfw backend always
draws a fresh frame.

The simplified mode is gone: when the frame is active and the surface has
alpha, the shadow and the outer resize border are always there.

The bar and its controls are drawn even when the font is missing: the early
return used to make the whole bar invisible while its hit zones kept
working.

GLFW rewrites the window style on every mode change and dropped the thick
frame the native resize border and snapping need, so the frame is set up
again after such a change.

Windows runs a modal loop while a window border is dragged and the main
loop is blocked inside it, which froze the content during a resize. The
backend draws and presents a frame from the refresh callback now, and
skips the framerate limit there.
This commit is contained in:
alexei-zebra 2026-10-10 22:02:33 +03:00
parent 4bc81af726
commit 68d4214627
No known key found for this signature in database
GPG key ID: D19D450AC3D9A80E
11 changed files with 96 additions and 52 deletions

View file

@ -66,16 +66,16 @@ function on_open()
"</select>", app.get_setting("display.window-mode"))
)
-- these only affect the window frame the native wayland backend draws
if app.get_window_backend() == "wayland" then
-- the bar height matters wherever the engine draws the window frame
if app.has_engine_window_frame() then
create_checkbox(
"display.compact-window-bar", "Narrow Window Bar",
"display.compact-window-bar.tooltip"
)
create_checkbox(
"display.simple-window", "Simple Window",
"display.simple-window.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"

View file

@ -119,10 +119,8 @@ settings.Clouds quality=Качество облаков
settings.Acoustic effects=Акустические эффекты
settings.Microphone access=Доступ к микрофону
settings.Narrow Window Bar=Узкая шапка окна
settings.Simple Window=Простое окно
settings.Live Window Resize=Живой ресайз окна
settings.display.compact-window-bar.tooltip=Шапка окна высотой 24 px вместо 48 px
settings.display.simple-window.tooltip=Без тени и внешней рамки ресайза
settings.display.live-resize.tooltip=Перерисовывать содержимое окна при изменении размера (выключено — последний кадр растягивается)
# Управление

View file

@ -40,9 +40,6 @@ void draw_window_bar(Engine& engine) {
}
auto* batch = engine.getGUI().getBatch2D();
auto font = engine.requireAssets().getShared<Font>(BAR_FONT);
if (font == nullptr) {
return;
}
const auto size = window.getSize();
const float barHeight = static_cast<float>(height);
@ -81,30 +78,35 @@ void draw_window_bar(Engine& engine) {
titleRight = std::min(titleRight, item.x - 8.0f);
}
}
const std::wstring title = to_wide(window.getTitle());
const float available = std::max(0.0f, titleRight - titleLeft);
size_t titleLength = title.size();
while (titleLength > 0 && font->calcWidth(title, titleLength) > available) {
titleLength--;
}
if (titleLength > 0) {
const float textWidth = static_cast<float>(
font->calcWidth(title, titleLength)
);
const float centered = (static_cast<float>(size.x) - textWidth) * 0.5f;
const float titleX = std::clamp(
centered,
titleLeft,
std::max(titleLeft, titleRight - textWidth)
);
batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.85f));
font->draw(
*batch, std::wstring_view(title).substr(0, titleLength),
static_cast<int>(titleX),
(height - font->getLineHeight()) / 2,
nullptr, 0
);
batch->untexture();
// 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 float available = std::max(0.0f, titleRight - titleLeft);
size_t titleLength = title.size();
while (titleLength > 0 &&
font->calcWidth(title, titleLength) > available) {
titleLength--;
}
if (titleLength > 0) {
const float textWidth = static_cast<float>(
font->calcWidth(title, titleLength)
);
const float centered =
(static_cast<float>(size.x) - textWidth) * 0.5f;
const float titleX = std::clamp(
centered,
titleLeft,
std::max(titleLeft, titleRight - textWidth)
);
batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.85f));
font->draw(
*batch, std::wstring_view(title).substr(0, titleLength),
static_cast<int>(titleX),
(height - font->getLineHeight()) / 2,
nullptr, 0
);
batch->untexture();
}
}
for (size_t i = 0; i < buttons.size(); i++) {

View file

@ -66,6 +66,14 @@ 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.
window->setRefreshHandler([this] {
engine.renderFrame();
engine.getWindow().swapBuffers();
});
return Result {std::move(window), std::move(input)};
}

View file

@ -56,7 +56,6 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
builder.add("window-mode", &settings.display.windowMode);
builder.add("adaptive-menu-fps", &settings.display.adaptiveFpsInMenu);
builder.add("compact-window-bar", &settings.display.compactWindowBar);
builder.add("simple-window", &settings.display.simpleWindow);
builder.add("live-resize", &settings.display.liveResize);
builder.addSection("camera");

View file

@ -229,6 +229,11 @@ static int l_open_url(lua::State* L) {
}
/// @brief Whether the engine draws the window frame itself
static int l_has_engine_window_frame(lua::State* L) {
return lua::pushboolean(L, engine->getWindow().hasEngineFrame());
}
/// @brief Name of the windowing backend ("glfw" or "wayland")
static int l_get_window_backend(lua::State* L) {
return lua::pushstring(L, engine->getWindow().getBackendName());
@ -508,6 +513,7 @@ const luaL_Reg applib[] = {
{"open_url", lua::wrap<l_open_url>},
/// window
{"get_window_backend", lua::wrap<l_get_window_backend>},
{"has_engine_window_frame", lua::wrap<l_has_engine_window_frame>},
{"focus", lua::wrap<l_focus>},
{"set_title", lua::wrap<l_set_title>},
{"quit", lua::wrap<l_quit>},

View file

@ -40,8 +40,6 @@ struct DisplaySettings {
FlagSetting adaptiveFpsInMenu {false};
/// @brief Draw the narrow (24px) window bar instead of the 48px one
FlagSetting compactWindowBar {false};
/// @brief Draw the window without the shadow and the resize border
FlagSetting simpleWindow {false};
/// @brief Render the window content every frame while it is resized
FlagSetting liveResize {true};
};

View file

@ -3,6 +3,7 @@
#include <glm/glm.hpp>
#include <memory>
#include <stack>
#include <functional>
#include <string>
#include <vector>
@ -72,6 +73,29 @@ 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
/// main loop is blocked inside it, so the only way to keep the content
/// alive during such a resize is to draw a frame from the message loop.
using RefreshHandler = std::function<void()>;
void setRefreshHandler(RefreshHandler handler) {
refreshHandler = std::move(handler);
}
const RefreshHandler& getRefreshHandler() const {
return refreshHandler;
}
virtual bool hasEngineFrame() const {
return false;
}
virtual const char* getBackendName() const = 0;
virtual int getDecorationHeight() const {
@ -118,6 +142,7 @@ protected:
std::stack<glm::vec4> scissorStack;
glm::vec4 scissorArea {};
WindowMode mode = WindowMode::WINDOWED;
RefreshHandler refreshHandler;
};
namespace display {

View file

@ -307,7 +307,7 @@ public:
void swapBuffers() override {
glfwSwapBuffers(window);
resetScissor();
if (framerate > 0) {
if (framerate > 0 && !inRefreshCallback) {
auto elapsedTime = time() - prevSwap;
auto frameTime = 1.0 / framerate;
if (elapsedTime < frameTime) {
@ -361,6 +361,13 @@ public:
return hoveredButton;
}
bool hasEngineFrame() const override {
return engineFrame;
}
/// @brief Set while a frame is drawn from the refresh callback
bool inRefreshCallback = false;
bool ownsCursor() const override {
return frameCursorActive;
}
@ -739,8 +746,9 @@ private:
void applyDecoration() {
if (engineFrame) {
// frame_setup_native() owns the window style, GLFW would drop the
// thick frame the native resize border and snapping need
// 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);
return;
}
glfwSetWindowAttrib(window, GLFW_DECORATED, GLFW_TRUE);
@ -904,6 +912,12 @@ 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;
}
}
static void setup_callbacks(GLFWwindow* window) {

View file

@ -281,8 +281,7 @@ void WaylandWindow::setMaximized(bool enabled) {
}
int WaylandWindow::marginSize() const {
if (!barEnabled || !shadowEnabled ||
(settings != nullptr && settings->simpleWindow.get())) {
if (!barEnabled || !shadowEnabled) {
return 0;
}
if (fullscreen || maximized || Window::mode != WindowMode::WINDOWED) {
@ -307,14 +306,6 @@ void WaylandWindow::updateSettings() {
if (settings == nullptr) {
return;
}
const bool simple = settings->simpleWindow.get();
if (simple != simpleWindowSetting) {
simpleWindowSetting = simple;
// The surface size depends on whether the frame around the content is
// drawn at all, so the egl window and the toplevel geometry change.
resize(size.x, size.y);
setShouldRefresh();
}
const bool compactBar = settings->compactWindowBar.get();
if (compactBar != compactBarSetting) {
compactBarSetting = compactBar;

View file

@ -114,6 +114,10 @@ class WaylandWindow : public Window {
void focus() override;
void setTitle(const std::string& title) override;
const std::string& getTitle() const override;
bool hasEngineFrame() const override {
return barEnabled;
}
const char* getBackendName() const override {
return "wayland";
}
@ -133,7 +137,6 @@ class WaylandWindow : public Window {
static constexpr int SHADOW_MARGIN = 10;
static constexpr int RESIZE_GRAB_EXTRA = 2;
bool shadowEnabled = false;
bool simpleWindowSetting = false;
bool compactBarSetting = false;
GLuint shadowProgram = 0;