draw window decorations with the engine GUI

The bar used to be drawn by the Wayland backend with its own shader,
bitmap font and textures, straight into the engine's GL frame. That
leaked GL state (the batch caches bound textures, so the engine kept
sampling the backend texture and all text turned into solid white
rectangles) and it duplicated fonts and styling.

Now the backend only owns the behaviour: hit zones, cursor shapes,
click interception and the compositor requests (move, resize,
minimize, maximize/restore, close). The bar itself is drawn by the
engine in WindowBar.cpp through the regular Batch2D/Font path, so it
looks like the rest of the UI.

Window gained getTitle(), getDecorationHeight() and
getDecorationHoveredButton(); GLFW keeps the defaults, so X11 and
Windows are unaffected. The backend's decoration drawing (bitmap font,
shader, quad batches) is removed: WaylandBar.cpp went from 630 to 140
lines.

Fixes found while debugging:
- GlStateGuard was defined but never used after the file split, so the
  stretched frame left its texture bound and broke text rendering after
  a window mode change; it is now applied around the cache capture and
  the stretched frame;
- takeScreenshot() now reads the front buffer and restores the read
  buffer afterwards.
This commit is contained in:
alexei-zebra 2026-10-09 01:18:13 +03:00
parent b934e033b3
commit 3b337241f5
No known key found for this signature in database
GPG key ID: D19D450AC3D9A80E
11 changed files with 890 additions and 1187 deletions

88
src/engine/WindowBar.cpp Normal file
View file

@ -0,0 +1,88 @@
#include "WindowBar.hpp"
#include "Engine.hpp"
#include "graphics/core/Batch2D.hpp"
#include "graphics/core/Font.hpp"
#include "graphics/ui/GUI.hpp"
#include "window/Window.hpp"
#include "assets/Assets.hpp"
#include <codecvt>
#include <locale>
namespace {
std::wstring to_wide(const std::string& text) {
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
return converter.from_bytes(text);
}
constexpr const char* BAR_FONT = "normal";
}
void draw_window_bar(Engine& engine) {
auto& window = engine.getWindow();
const int height = window.getDecorationHeight();
if (height <= 0) {
return;
}
auto* batch = engine.getGUI().getBatch2D();
auto font = engine.requireAssets().getShared<Font>(BAR_FONT);
if (batch == nullptr || font == nullptr) {
return;
}
const auto size = window.getSize();
const float barHeight = static_cast<float>(height);
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 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);
}
const float ink = hovered == i ? 1.0f : 0.8f;
const float thickness = 2.0f;
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);
}
} else if (i == 1 && !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) {
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);
batch->rect(cx - 7.0f, cy + 5.0f, 11.0f, thickness);
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();
}