Draw the window title bar by the engine

The native Wayland backend had no window controls: no title, no close
button, no way to move or resize the window, and the fullscreen toggle
could throw the window to another monitor. GNOME does not implement
zxdg_decoration_manager_v1, so there is nobody to draw a frame for us:
the backend now falls back to its own decorations when the compositor
offers no server side ones, and the engine draws the visible part.

The bar itself is drawn by the engine (WindowBar.cpp) through the usual
batch and font, so it uses the same assets as the rest of the UI and the
backend only owns the behaviour: hit zones, cursor shapes, click
interception and the xdg-shell requests. Its geometry follows the
desktop's button layout, read and subscribed through
xdg-desktop-portal over sd-bus (libsystemd is an optional dependency,
gsettings is only a startup fallback).

Controls behave like they do elsewhere:
- pressing anywhere on the bar starts a move once the pointer leaves a
  5 px radius, so dragging works from the buttons as well;
- a button acts on release, only if the pointer stayed on it;
- a double click on the bar toggles maximize;
- the top edge and the top corners resize, the top 8 px of the sides,
  the sides and the bottom are 8 px wide resize strips;
- right and middle clicks and the scroll wheel over the bar never reach
  the game.

The in-game user interface is offset below the bar while menus keep the
whole window, so the translucent bar shows the menu background through
itself and never covers the HUD. The HUD lays its elements out against
the content height and the cursor is translated once, which fixes items
following the mouse one row above the pointer in game.

The window size is now written to the settings when the window is
resized in windowed mode and applied on the first configure, so the
window opens with the size it was left with, and leaving fullscreen or
borderless mode restores it instead of overwriting the settings with the
fullscreen size (an unconditional unmaximize made the compositor restore
the maximized geometry).

Measured: menu layout unchanged (706 bright pixels in the same place),
gui viewport 1123x751 in menus against 1123x721 in game, 1123x751 ->
2560x1080 -> 1123x751 round trip keeps the settings, a saved 1000x640
opens as 1000x640, no errors in the log.
This commit is contained in:
alexei-zebra 2026-10-09 20:56:55 +03:00
parent 4731ac0706
commit 6c2192b5d8
No known key found for this signature in database
GPG key ID: D19D450AC3D9A80E
14 changed files with 685 additions and 94 deletions

View file

@ -1,12 +1,18 @@
#include "WindowBar.hpp"
#include "Engine.hpp"
#include "debug/Logger.hpp"
#include "graphics/core/Batch2D.hpp"
#include <GL/glew.h>
#include <glm/gtc/matrix_transform.hpp>
#include "graphics/core/Shader.hpp"
#include "graphics/core/Font.hpp"
#include "graphics/ui/GUI.hpp"
#include "window/Window.hpp"
#include "assets/Assets.hpp"
#include <algorithm>
#include <string_view>
#include <codecvt>
#include <locale>
@ -33,46 +39,85 @@ void draw_window_bar(Engine& engine) {
const auto size = window.getSize();
const float barHeight = static_cast<float>(height);
glViewport(0, 0, static_cast<GLsizei>(size.x), static_cast<GLsizei>(size.y));
if (auto shader = engine.requireAssets().get<Shader>("ui"); shader != nullptr) {
shader->use();
shader->uniformMatrix(
"u_projview",
glm::ortho(
0.0f,
static_cast<float>(size.x),
static_cast<float>(size.y),
0.0f,
-1.0f,
1.0f
)
);
}
batch->begin();
batch->setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.82f));
batch->rect(0.0f, 0.0f, size.x, barHeight);
batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.10f));
batch->rect(0.0f, barHeight - 1.0f, size.x, 1.0f);
const std::wstring title = to_wide(window.getTitle());
const int textX = static_cast<int>(barHeight * 0.4f);
const int textY = (height - font->getLineHeight()) / 2;
batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.85f));
font->draw(*batch, title, textX, textY, nullptr, 0);
batch->untexture();
const float button = barHeight;
const auto& buttons = window.getDecorationButtons();
const int hovered = window.getDecorationHoveredButton();
for (int i = 0; i < 3; i++) {
const float x = size.x - button * (i + 1);
const float cx = x + button * 0.5f;
const float cy = barHeight * 0.5f;
if (hovered == i) {
batch->setColor(
i == 0 ? glm::vec4(0.62f, 0.13f, 0.09f, 0.9f)
: glm::vec4(1.0f, 1.0f, 1.0f, 0.16f)
);
batch->rect(x, 0.0f, button, barHeight - 1.0f);
float titleLeft = static_cast<float>(barHeight * 0.4f);
float titleRight = size.x - 8.0f;
for (const auto& item : buttons) {
if (item.x + item.width * 0.5f < size.x * 0.5f) {
titleLeft = std::max(titleLeft, item.x + item.width + 8.0f);
} else {
titleRight = std::min(titleRight, item.x - 8.0f);
}
const float ink = hovered == i ? 1.0f : 0.8f;
const float thickness = 2.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) {
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>(titleLeft),
(height - font->getLineHeight()) / 2,
nullptr, 0
);
batch->untexture();
}
const float thickness = 2.0f;
for (size_t i = 0; i < buttons.size(); i++) {
const auto& item = buttons[i];
const float x = item.x;
const float cx = x + item.width * 0.5f;
const float cy = barHeight * 0.5f;
const bool isHovered = hovered == static_cast<int>(i);
if (isHovered) {
batch->setColor(
item.button == DecorationButton::CLOSE
? glm::vec4(0.62f, 0.13f, 0.09f, 0.9f)
: glm::vec4(1.0f, 1.0f, 1.0f, 0.16f)
);
batch->rect(x, 0.0f, item.width, barHeight);
}
const float ink = isHovered ? 1.0f : 0.8f;
batch->setColor(glm::vec4(1.0f, 1.0f, 1.0f, ink));
if (i == 0) {
for (int s = 0; s < 8; s++) {
batch->rect(cx - 5.0f + s, cy - 5.0f + s, thickness, thickness);
batch->rect(cx + 3.0f - s, cy - 5.0f + s, thickness, thickness);
if (item.button == DecorationButton::CLOSE) {
for (int step = 0; step < 8; step++) {
batch->rect(cx - 5.0f + step, cy - 5.0f + step, thickness, thickness);
batch->rect(cx + 3.0f - step, cy - 5.0f + step, thickness, thickness);
}
} else if (i == 1 && !window.isMaximized()) {
} else if (item.button == DecorationButton::MINIMIZE) {
batch->rect(cx - 6.0f, cy - 1.0f, 12.0f, thickness);
} else if (!window.isMaximized()) {
batch->rect(cx - 6.0f, cy - 6.0f, 12.0f, thickness);
batch->rect(cx - 6.0f, cy + 4.0f, 12.0f, thickness);
batch->rect(cx - 6.0f, cy - 6.0f, thickness, 12.0f);
batch->rect(cx + 4.0f, cy - 6.0f, thickness, 12.0f);
} else if (i == 1) {
} else {
batch->rect(cx - 7.0f, cy + 1.0f, 11.0f, thickness);
batch->rect(cx - 7.0f, cy + 1.0f, thickness, 6.0f);
batch->rect(cx + 2.0f, cy + 1.0f, thickness, 6.0f);
@ -80,8 +125,6 @@ void draw_window_bar(Engine& engine) {
batch->rect(cx - 2.0f, cy - 5.0f, 9.0f, thickness);
batch->rect(cx - 2.0f, cy - 5.0f, thickness, 6.0f);
batch->rect(cx + 5.0f, cy - 5.0f, thickness, 6.0f);
} else {
batch->rect(cx - 6.0f, cy - 1.0f, 12.0f, thickness);
}
}
batch->flush();