From c269d6efb315a351db6e00f42cc416038b4f5916 Mon Sep 17 00:00:00 2001 From: alexei-zebra Date: Fri, 9 Oct 2026 22:27:18 +0300 Subject: [PATCH] Give the Wayland window an Adwaita-like frame The engine-drawn title bar grew to 48 px with 24x24 window controls, 12 px away from the edge and from each other, all sharing one hover highlight. The controls use textures now (gui/cross, gui/minimize_w, gui/maximize_w and gui/restore_w when maximized) with the vector icons kept as a fallback, and the title is centred in the bar, clamped to the free space between the button groups. The bar now sits inside invisible margins that carry a drop shadow, so the window is resizable from outside the visible frame: the surface is content plus twice SHADOW_MARGIN and xdg_surface.set_window_geometry tells the compositor which part is the window. The shadow follows Adwaita's window shadow: a Gaussian blur of 7 px (sigma 3.5) expanded by 1 px, rgba(0,0,0,0.5) focused and 0.3 in the backdrop, plus a 1 px hairline, symmetric on all four sides - a 16 px wide frame would show a fatter tail than the reference. Margins exist only while the window is decorated by us and not maximized or fullscreen. The engine composes the frame itself: it renders exactly as before, then the backend copies the frame into a texture, clears the surface, draws the shadow and puts the content in place with alpha forced to 1, which keeps the frame opaque without touching a single render pass. Only frames the engine actually drew are cached, and the EGL window is resized before its surface size is asked for, so no frame is composed from a buffer the compositor just replaced - that was the one frame blink after a resize. Resize cursors match GNOME: directional shapes for edges and corners (w/e/n/s/ne/nw/se/sw) instead of the double-headed diagonals, with the extra CursorShape values appended so the existing GLFW indices stay. Measured: window 1123x751 opens as saved, fullscreen round trip returns the same size with the settings untouched, the shadow alpha profile is 0.20/0.10/0.03 at 1/4/7 px from the edge against Adwaita's 0.21/0.095/ 0.055, all four sides equal, menu layout baseline unchanged (706), no errors in the log. --- res/textures/gui/maximize_w.png | Bin 0 -> 186 bytes res/textures/gui/minimize_w.png | Bin 0 -> 178 bytes res/textures/gui/restore_w.png | Bin 0 -> 182 bytes src/engine/WindowBar.cpp | 41 +++- src/graphics/core/commons.hpp | 17 ++ src/window/wayland/WaylandDecorations.cpp | 80 ++++++-- src/window/wayland/WaylandInit.cpp | 53 +++-- src/window/wayland/WaylandInput.hpp | 16 ++ src/window/wayland/WaylandListeners.cpp | 8 +- src/window/wayland/WaylandWindow.cpp | 238 +++++++++++++++++++++- src/window/wayland/WaylandWindow.hpp | 39 +++- 11 files changed, 434 insertions(+), 58 deletions(-) create mode 100644 res/textures/gui/maximize_w.png create mode 100644 res/textures/gui/minimize_w.png create mode 100644 res/textures/gui/restore_w.png diff --git a/res/textures/gui/maximize_w.png b/res/textures/gui/maximize_w.png new file mode 100644 index 0000000000000000000000000000000000000000..c710bf8145156dda07a3813ff1ae500b1cbd82f5 GIT binary patch literal 186 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jPK-BC>eK@{Ea{HEjtmSN z`?>!lvI6-E$sR$z3=CCj3=9n|3=F@3LJcn%7)lKo7+xhXFj&oCU=S~uvn$XBC}H90 z;uvCaIypfi;{ZqUpa1{=+j~k$0)d2t#0-s6MsB^yg^VTBgWfa+GP5-^>a;wX`Akou af`Oq#nn$9ID`pkY3eK@{Ea{HEjtmSN z`?>!lvI6-E$sR$z3=CCj3=9n|3=F@3LJcn%7)lKo7+xhXFj&oCU=S~uvn$XBC}H5~ z;uvCaIypgtb#a1-0SFwD_zwjBliYxS4G7d$q)p0gdzpCfnba;OhCT1OnB~3&?F8y& N@O1TaS?83{1OR>UF$n+w literal 0 HcmV?d00001 diff --git a/res/textures/gui/restore_w.png b/res/textures/gui/restore_w.png new file mode 100644 index 0000000000000000000000000000000000000000..fc245c1a614695e500ac553ce992aef0170753af GIT binary patch literal 182 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`jKx9jPK-BC>eK@{Ea{HEjtmSN z`?>!lvI6-E$sR$z3=CCj3=9n|3=F@3LJcn%7)lKo7+xhXFj&oCU=S~uvn$XBC}HC1 z;uvCaIypgtwdV!Xe<0`&XI573l2h}FJ>ay)cSnrdMsBuy40944G|W~!YYIGYfPuj> Wm`C8)oaG%r{S2P2elF{r5}E*?elv&w literal 0 HcmV?d00001 diff --git a/src/engine/WindowBar.cpp b/src/engine/WindowBar.cpp index ff0f33ecd..618df3a1e 100644 --- a/src/engine/WindowBar.cpp +++ b/src/engine/WindowBar.cpp @@ -7,6 +7,7 @@ #include #include "graphics/core/Shader.hpp" #include "graphics/core/Font.hpp" +#include "graphics/core/Texture.hpp" #include "graphics/ui/GUI.hpp" #include "window/Window.hpp" #include "assets/Assets.hpp" @@ -78,10 +79,19 @@ void draw_window_bar(Engine& engine) { titleLength--; } if (titleLength > 0) { + const float textWidth = static_cast( + font->calcWidth(title, titleLength) + ); + const float centered = (static_cast(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(titleLeft), + static_cast(titleX), (height - font->getLineHeight()) / 2, nullptr, 0 ); @@ -96,15 +106,34 @@ void draw_window_bar(Engine& engine) { const float cy = barHeight * 0.5f; const bool isHovered = hovered == static_cast(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->setColor(glm::vec4(1.0f, 1.0f, 1.0f, 0.16f)); + const float button = item.width; + batch->rect( + item.x, (barHeight - button) * 0.5f, item.width, button ); - 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)); + const char* iconName = + item.button == DecorationButton::CLOSE + ? "gui/cross" + : item.button == DecorationButton::MINIMIZE + ? "gui/minimize_w" + : window.isMaximized() ? "gui/restore_w" + : "gui/maximize_w"; + auto icon = engine.requireAssets().getShared(iconName); + if (icon != nullptr) { + const float iconSize = std::min( + 16.0f, static_cast(icon->getWidth()) + ); + batch->texture(icon.get()); + batch->rect( + cx - iconSize * 0.5f, cy - iconSize * 0.5f, iconSize, + iconSize, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, ink + ); + batch->untexture(); + continue; + } if (item.button == DecorationButton::CLOSE) { for (int step = 0; step < 8; step++) { batch->rect(cx - 5.0f + step, cy - 5.0f + step, thickness, thickness); diff --git a/src/graphics/core/commons.hpp b/src/graphics/core/commons.hpp index 0ca5a6470..6c09d5b43 100644 --- a/src/graphics/core/commons.hpp +++ b/src/graphics/core/commons.hpp @@ -46,6 +46,15 @@ enum class CursorShape { ALL_RESIZE, /// @brief Operation not allowed NOT_ALLOWED, + /// @brief Corner resize arrows + N_RESIZE, + S_RESIZE, + E_RESIZE, + W_RESIZE, + NE_RESIZE, + NW_RESIZE, + SE_RESIZE, + SW_RESIZE, LAST=NOT_ALLOWED }; @@ -74,6 +83,14 @@ VC_ENUM_METADATA(CursorShape) {"nesw-resize", CursorShape::NESW_RESIZE}, {"all-resize", CursorShape::ALL_RESIZE}, {"not-allowed", CursorShape::NOT_ALLOWED}, + {"n-resize", CursorShape::N_RESIZE}, + {"s-resize", CursorShape::S_RESIZE}, + {"e-resize", CursorShape::E_RESIZE}, + {"w-resize", CursorShape::W_RESIZE}, + {"ne-resize", CursorShape::NE_RESIZE}, + {"nw-resize", CursorShape::NW_RESIZE}, + {"se-resize", CursorShape::SE_RESIZE}, + {"sw-resize", CursorShape::SW_RESIZE}, VC_ENUM_END class Flushable { diff --git a/src/window/wayland/WaylandDecorations.cpp b/src/window/wayland/WaylandDecorations.cpp index d72544f11..ead71b136 100644 --- a/src/window/wayland/WaylandDecorations.cpp +++ b/src/window/wayland/WaylandDecorations.cpp @@ -147,16 +147,18 @@ void WaylandWindow::updateButtonLayout() { void WaylandWindow::updateBarGeometry() { barButtons.clear(); const float width = static_cast(size.x); + constexpr float pitch = BAR_BUTTON + BAR_GAP; for (size_t i = 0; i < leftButtons.size(); i++) { - const float x = static_cast(i) * BAR_BUTTON; + const float x = BAR_PADDING + static_cast(i) * pitch; if (x + BAR_BUTTON > width) { break; } barButtons.push_back({leftButtons[i], x, BAR_BUTTON}); } for (size_t i = 0; i < rightButtons.size(); i++) { - const float x = width - - static_cast(rightButtons.size() - i) * BAR_BUTTON; + const float x = width - BAR_PADDING - + static_cast(rightButtons.size() - i) * pitch + + BAR_GAP; if (x < 0.0f) { break; } @@ -193,7 +195,8 @@ bool WaylandWindow::barVisible() const { } int WaylandWindow::barButtonAt(double x, double y) const { - if (!barVisible() || y < 0.0 || y >= BAR_HEIGHT) { + constexpr int top = (BAR_HEIGHT - BAR_BUTTON) / 2; + if (!barVisible() || y < top || y >= top + BAR_BUTTON) { return -1; } for (size_t i = 0; i < barButtons.size(); i++) { @@ -232,21 +235,56 @@ CursorShape WaylandWindow::edgeCursor(uint32_t edges) { const bool right = edges & XDG_TOPLEVEL_RESIZE_EDGE_RIGHT; const bool top = edges & XDG_TOPLEVEL_RESIZE_EDGE_TOP; const bool bottom = edges & XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM; - if ((left && top) || (right && bottom)) { - return CursorShape::NWSE_RESIZE; + if (left && top) { + return CursorShape::NW_RESIZE; } - if ((right && top) || (left && bottom)) { - return CursorShape::NESW_RESIZE; + if (right && top) { + return CursorShape::NE_RESIZE; } - if (left || right) { - return CursorShape::EW_RESIZE; + if (left && bottom) { + return CursorShape::SW_RESIZE; } - if (top || bottom) { - return CursorShape::NS_RESIZE; + if (right && bottom) { + return CursorShape::SE_RESIZE; + } + if (left) { + return CursorShape::W_RESIZE; + } + if (right) { + return CursorShape::E_RESIZE; + } + if (top) { + return CursorShape::N_RESIZE; + } + if (bottom) { + return CursorShape::S_RESIZE; } return CursorShape::ARROW; } +uint32_t WaylandWindow::pointerEdges() const { + const int margin = marginSize(); + uint32_t edges = 0; + if (margin > 0) { + const auto surface = surfaceSize(); + const int edge = margin + RESIZE_GRAB_EXTRA; + if (pointerX < edge) { + edges |= XDG_TOPLEVEL_RESIZE_EDGE_LEFT; + } else if (pointerX >= surface.x - edge) { + edges |= XDG_TOPLEVEL_RESIZE_EDGE_RIGHT; + } + if (pointerY < edge) { + edges |= XDG_TOPLEVEL_RESIZE_EDGE_TOP; + } else if (pointerY >= surface.y - edge) { + edges |= XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM; + } + } + if (edges == 0 && margin == 0) { + edges = barEdgeAt(contentX(pointerX), contentY(pointerY)); + } + return edges; +} + bool WaylandWindow::handleBarMotion(double x, double y) { pointerX = x; pointerY = y; @@ -257,20 +295,22 @@ bool WaylandWindow::handleBarMotion(double x, double y) { hoveredButton = -1; xdg_toplevel_move(state.toplevel, state.seat, barPressSerial); } - const uint32_t edges = barEdgeAt(x, y); + const uint32_t edges = pointerEdges(); if (edges != 0) { hoveredButton = -1; pointerInBar = false; input.setOverrideCursor(edgeCursor(edges)); return true; } - const bool inBar = barVisible() && y >= 0.0 && y < BAR_HEIGHT; + const double contentY = this->contentY(y); + const bool inBar = barVisible() && contentY >= 0.0 && contentY < BAR_HEIGHT; if (inBar && !pointerInBar) { lastLayoutCheck = 0.0; } pointerInBar = inBar; if (inBar) { - hoveredButton = barDragging ? -1 : barButtonAt(x, y); + hoveredButton = + barDragging ? -1 : barButtonAt(contentX(x), contentY); input.setOverrideCursor( hoveredButton >= 0 ? CursorShape::POINTER : CursorShape::ARROW ); @@ -282,9 +322,9 @@ bool WaylandWindow::handleBarMotion(double x, double y) { } bool WaylandWindow::handleBarButton(uint32_t serial, int button, bool pressed) { - const double x = pointerX; - const double y = pointerY; - const uint32_t edges = barEdgeAt(x, y); + const double x = contentX(pointerX); + const double y = contentY(pointerY); + const uint32_t edges = pointerEdges(); const bool inBar = barVisible() && y >= 0.0 && y < BAR_HEIGHT; if (button != 0) { return inBar || edges != 0; @@ -345,8 +385,8 @@ bool WaylandWindow::handleBarButton(uint32_t serial, int button, bool pressed) { } bool WaylandWindow::handleBarScroll() const { - return (barVisible() && pointerY >= 0.0 && pointerY < BAR_HEIGHT) || - barEdgeAt(pointerX, pointerY) != 0; + const double y = contentY(pointerY); + return (barVisible() && y >= 0.0 && y < BAR_HEIGHT) || pointerEdges() != 0; } void WaylandWindow::onPointerLeave() { diff --git a/src/window/wayland/WaylandInit.cpp b/src/window/wayland/WaylandInit.cpp index 667a71523..d8904a487 100644 --- a/src/window/wayland/WaylandInit.cpp +++ b/src/window/wayland/WaylandInit.cpp @@ -120,33 +120,46 @@ wayland_window_initialize(DisplaySettings* settings, std::string title) { eglBindAPI(EGL_OPENGL_API); EGLint samples = settings->samples.get(); + const EGLint requestedSamples = samples > 0 ? samples : 0; EGLConfig config = nullptr; EGLint configCount = 0; - const EGLint configAttributes[] { - EGL_SURFACE_TYPE, EGL_WINDOW_BIT, - EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, - EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, - EGL_ALPHA_SIZE, 0, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, - EGL_SAMPLES, samples > 0 ? samples : 0, - EGL_NONE - }; - eglChooseConfig(window->eglDisplay, configAttributes, &config, 1, &configCount); - if (samples > 0 && configCount == 0) { - waylandLogger.warning() << "multisampling is not available"; - const EGLint fallbackAttributes[] { + const auto chooseConfig = [&] (EGLint alpha, EGLint sampleCount) { + const EGLint attributes[] { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, - EGL_ALPHA_SIZE, 0, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, + EGL_ALPHA_SIZE, alpha, EGL_DEPTH_SIZE, 24, EGL_STENCIL_SIZE, 8, + EGL_SAMPLES, sampleCount, EGL_NONE }; + config = nullptr; + configCount = 0; eglChooseConfig( - window->eglDisplay, fallbackAttributes, &config, 1, &configCount + window->eglDisplay, attributes, &config, 1, &configCount ); - } - if (configCount == 0) { - waylandLogger.error() << "no suitable EGL config"; - return {nullptr, nullptr}; + if (configCount == 0) { + return false; + } + EGLint value = 0; + eglGetConfigAttrib( + window->eglDisplay, config, EGL_ALPHA_SIZE, &value + ); + window->shadowEnabled = value >= 8; + return true; + }; + if (!chooseConfig(8, requestedSamples)) { + if (requestedSamples > 0) { + waylandLogger.warning() << "multisampling is not available"; + } + if (!chooseConfig(8, 0)) { + waylandLogger.warning() + << "no EGL config with alpha, the drop shadow is off"; + if (!chooseConfig(0, requestedSamples) && !chooseConfig(0, 0)) { + waylandLogger.error() << "no suitable EGL config"; + return {nullptr, nullptr}; + } + window->shadowEnabled = false; + } } const EGLint contextAttributes[] { @@ -157,8 +170,9 @@ wayland_window_initialize(DisplaySettings* settings, std::string title) { window->eglContext = eglCreateContext( window->eglDisplay, config, EGL_NO_CONTEXT, contextAttributes ); + const auto surfaceSize = window->surfaceSize(); window->eglWindow = wl_egl_window_create( - state.surface, window->getSize().x, window->getSize().y + state.surface, surfaceSize.x, surfaceSize.y ); window->eglSurface = eglCreateWindowSurface( window->eglDisplay, config, @@ -184,6 +198,7 @@ wayland_window_initialize(DisplaySettings* settings, std::string title) { } window->initStretchRenderer(); + window->initShadowRenderer(); glViewport(0, 0, window->getSize().x, window->getSize().y); glClearColor(0.0f, 0.0f, 0.0f, 1); glEnable(GL_BLEND); diff --git a/src/window/wayland/WaylandInput.hpp b/src/window/wayland/WaylandInput.hpp index b04bde572..a83e52d84 100644 --- a/src/window/wayland/WaylandInput.hpp +++ b/src/window/wayland/WaylandInput.hpp @@ -217,6 +217,22 @@ private: return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NESW_RESIZE; case CursorShape::ALL_RESIZE: return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_ALL_RESIZE; + case CursorShape::N_RESIZE: + return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_N_RESIZE; + case CursorShape::S_RESIZE: + return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_S_RESIZE; + case CursorShape::E_RESIZE: + return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_E_RESIZE; + case CursorShape::W_RESIZE: + return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_W_RESIZE; + case CursorShape::NE_RESIZE: + return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NE_RESIZE; + case CursorShape::NW_RESIZE: + return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NW_RESIZE; + case CursorShape::SE_RESIZE: + return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SE_RESIZE; + case CursorShape::SW_RESIZE: + return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_SW_RESIZE; case CursorShape::NOT_ALLOWED: return WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_NOT_ALLOWED; } diff --git a/src/window/wayland/WaylandListeners.cpp b/src/window/wayland/WaylandListeners.cpp index d92393678..ed2efe249 100644 --- a/src/window/wayland/WaylandListeners.cpp +++ b/src/window/wayland/WaylandListeners.cpp @@ -15,7 +15,9 @@ static void pointer_enter( return; } if (input) { - input->setCursorPosition(x, y); + input->setCursorPosition( + window ? window->contentX(x) : x, window ? window->contentY(y) : y + ); if (state.entered) { input->applyCursor(); } @@ -39,7 +41,9 @@ static void pointer_motion( return; } if (input) { - input->setCursorPosition(x, y); + input->setCursorPosition( + window ? window->contentX(x) : x, window ? window->contentY(y) : y + ); input->refreshWindow(); } } diff --git a/src/window/wayland/WaylandWindow.cpp b/src/window/wayland/WaylandWindow.cpp index 3c4748d8d..aa4eb1474 100644 --- a/src/window/wayland/WaylandWindow.cpp +++ b/src/window/wayland/WaylandWindow.cpp @@ -45,6 +45,9 @@ WaylandWindow::~WaylandWindow() { if (stretchProgram) { glDeleteProgram(stretchProgram); } + if (shadowProgram) { + glDeleteProgram(shadowProgram); + } if (stretchVbo) { glDeleteBuffers(1, &stretchVbo); } @@ -96,14 +99,21 @@ void WaylandWindow::swapBuffers() { if (renderPending) { cacheContent(); renderPending = false; - } else { + } else if (marginSize() == 0) { drawStretched(); } - } else { + } else if (frameRendered) { cacheContent(); } + frameRendered = false; + composeFrame(); requestFrame(); eglSwapBuffers(eglDisplay, eglSurface); + if (sizeChanged) { + sizeChanged = false; + composeFrame(); + eglSwapBuffers(eglDisplay, eglSurface); + } resetScissor(); frameDone = false; presentPending = false; @@ -119,7 +129,7 @@ void WaylandWindow::swapBuffers() { 1.0 / 30.0 ); } -} + } lastPresent = current; nextFrameTime = current + frameInterval; lastSwapEnd = now(); @@ -160,7 +170,11 @@ bool WaylandWindow::isIconified() const { } bool WaylandWindow::isFrameRequired() const { - return !resizing || renderPending; + const bool required = !resizing || renderPending; + if (required) { + frameRendered = true; + } + return required; } bool WaylandWindow::isShouldClose() const { @@ -234,19 +248,47 @@ void WaylandWindow::setMaximized(bool enabled) { } } +int WaylandWindow::marginSize() const { + if (!barEnabled || !shadowEnabled) { + return 0; + } + if (fullscreen || maximized || Window::mode != WindowMode::WINDOWED) { + return 0; + } + return SHADOW_MARGIN; +} + +glm::ivec2 WaylandWindow::surfaceSize() const { + const int margin = marginSize() * 2; + return {size.x + margin, size.y + margin}; +} + +void WaylandWindow::updateWindowGeometry() { + if (state.xdgSurface == nullptr) { + return; + } + const int margin = marginSize(); + xdg_surface_set_window_geometry( + state.xdgSurface, margin, margin, size.x, size.y + ); +} + void WaylandWindow::resize(int width, int height) { if (width <= 0 || height <= 0) { return; } if (size.x != width || size.y != height) { markResizing(); + sizeChanged = true; } presentPending = true; + size = {width, height}; if (eglWindow) { - wl_egl_window_resize(eglWindow, width, height, 0, 0); + const auto surface = surfaceSize(); + wl_egl_window_resize(eglWindow, surface.x, surface.y, 0, 0); } glViewport(0, 0, width, height); - size = {width, height}; + updateWindowGeometry(); if (Window::mode == WindowMode::WINDOWED && !maximized) { settings->width.set(width); settings->height.set(height); @@ -281,11 +323,12 @@ void WaylandWindow::applyConfigure() { maximized = stateMaximized; suspended = stateSuspended; if (pendingWidth > 0 && pendingHeight > 0) { - if (!configured && Window::mode == WindowMode::WINDOWED && !maximized && - initialWidth > 0 && initialHeight > 0) { + if (!initialSizeApplied && Window::mode == WindowMode::WINDOWED && + !maximized && initialWidth > 0 && initialHeight > 0) { pendingWidth = initialWidth; pendingHeight = initialHeight; } + initialSizeApplied = true; resize(pendingWidth, pendingHeight); } if (stateActivated && !activated) { @@ -358,9 +401,18 @@ std::unique_ptr WaylandWindow::takeScreenshot() { GLint readBuffer = 0; glGetIntegerv(GL_READ_BUFFER, &readBuffer); glReadBuffer(GL_FRONT); + const int margin = marginSize(); auto data = std::make_unique(size.x * size.y * 3); glPixelStorei(GL_PACK_ALIGNMENT, 1); - glReadPixels(0, 0, size.x, size.y, GL_RGB, GL_UNSIGNED_BYTE, data.get()); + glReadPixels( + margin, + margin, + size.x, + size.y, + GL_RGB, + GL_UNSIGNED_BYTE, + data.get() + ); glReadBuffer(static_cast(readBuffer)); return std::make_unique( ImageFormat::RGB888, size.x, size.y, data.release() @@ -416,6 +468,174 @@ void WaylandWindow::initStretchRenderer() { glGenTextures(1, &cacheTexture); } +void WaylandWindow::initShadowRenderer() { + if (!shadowEnabled) { + return; + } + static const char* vertexSource = + "#version 330 core\n" + "layout(location = 0) in vec2 aPos;\n" + "out vec2 vUV;\n" + "void main() {\n" + " vUV = aPos * 0.5 + 0.5;\n" + " gl_Position = vec4(aPos, 0.0, 1.0);\n" + "}\n"; + static const char* fragmentSource = + "#version 330 core\n" + "in vec2 vUV;\n" + "out vec4 color;\n" + "uniform vec2 uSurface;\n" + "uniform vec4 uRect;\n" + "uniform float uBlur;\n" + "uniform float uStrength;\n" + "uniform float uOffset;\n" + "uniform float uSpread;\n" + "uniform float uBorder;\n" + "uniform float uRadius;\n" + "float erfcApprox(float x) {\n" + " float t = 1.0 / (1.0 + 0.5 * abs(x));\n" + " float tau = t * exp(-x * x - 1.26551223 +\n" + " t * (1.00002368 +\n" + " t * (0.37409196 +\n" + " t * (0.09678418 +\n" + " t * (-0.18628806 +\n" + " t * (0.27886807 +\n" + " t * (-1.13520398 +\n" + " t * (1.48851587 +\n" + " t * (-0.82215223 +\n" + " t * 0.17087277)))))))));\n" + " return x >= 0.0 ? tau : 2.0 - tau;\n" + "}\n" + "float boxDistance(vec2 p, vec2 center, vec2 halfSize, float radius) {\n" + " vec2 q = abs(p - center) - (halfSize - vec2(radius));\n" + " return length(max(q, 0.0)) + min(max(q.x, q.y), 0.0) - radius;\n" + "}\n" + "void main() {\n" + " vec2 p = vUV * uSurface;\n" + " vec2 halfSize = uRect.zw * 0.5;\n" + " vec2 center = uRect.xy + halfSize;\n" + " float shadow = boxDistance(\n" + " p, center + vec2(0.0, -uOffset), halfSize + uSpread,\n" + " uRadius + uSpread\n" + " );\n" + " float sigma = max(uBlur * 0.5, 0.001);\n" + " float alpha = uStrength * 0.5 * erfcApprox(shadow / (sigma * 1.41421356));\n" + " float side = boxDistance(p, center, halfSize, uRadius);\n" + " float border = side > 0.0 && side <= 1.0 ? uBorder : 0.0;\n" + " color = vec4(0.0, 0.0, 0.0, max(alpha, border));\n" + "}\n"; + shadowProgram = compile_program(vertexSource, fragmentSource); + GLint linked = 0; + glGetProgramiv(shadowProgram, GL_LINK_STATUS, &linked); + if (shadowProgram == 0 || linked == 0) { + char log[1024] = {}; + glGetProgramInfoLog(shadowProgram, sizeof(log), nullptr, log); + for (char* c = log; *c != '\0'; c++) { + if (*c == '\n') { + *c = ' '; + } + } + waylandLogger.error() << "failed to build the shadow shader: " << log; + shadowEnabled = false; + return; + } + const char* names[] { + "uSurface", "uRect", "uBlur", "uStrength", "uOffset", "uSpread", + "uBorder", "uRadius", nullptr + }; + for (int i = 0; names[i] != nullptr; i++) { + shadowUniforms[i] = glGetUniformLocation(shadowProgram, names[i]); + } + +} + +void WaylandWindow::drawShadow() { + const int margin = marginSize(); + if (margin <= 0 || shadowProgram == 0) { + return; + } + const auto surface = surfaceSize(); + glViewport(0, 0, surface.x, surface.y); + glEnable(GL_BLEND); + glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + glDisable(GL_DEPTH_TEST); + glDisable(GL_CULL_FACE); + glDisable(GL_SCISSOR_TEST); + glUseProgram(shadowProgram); + glUniform2f( + shadowUniforms[0], + static_cast(surface.x), + static_cast(surface.y) + ); + glUniform4f( + shadowUniforms[1], + static_cast(margin), + static_cast(margin), + static_cast(size.x), + static_cast(size.y) + ); + glUniform1f(shadowUniforms[2], 7.0f); + glUniform1f(shadowUniforms[3], activated ? 0.5f : 0.3f); + glUniform1f(shadowUniforms[4], 0.0f); + glUniform1f(shadowUniforms[5], 1.0f); + glUniform1f(shadowUniforms[6], activated ? 0.1f : 0.05f); + glUniform1f(shadowUniforms[7], 0.0f); + glBindVertexArray(stretchVao); + glDrawArrays(GL_TRIANGLES, 0, 3); + glBindVertexArray(0); + glUseProgram(0); +} + +void WaylandWindow::makeContentOpaque(int x, int y, int width, int height) { + glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE); + glEnable(GL_SCISSOR_TEST); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glScissor(x, y, width, height); + glClear(GL_COLOR_BUFFER_BIT); + glDisable(GL_SCISSOR_TEST); + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); +} + +void WaylandWindow::composeFrame() { + const int margin = marginSize(); + GlStateGuard state; + glBindFramebuffer(GL_FRAMEBUFFER, 0); + if (margin <= 0) { + makeContentOpaque(0, 0, size.x, size.y); + return; + } + const auto surface = surfaceSize(); + glViewport(0, 0, surface.x, surface.y); + glDisable(GL_SCISSOR_TEST); + glDisable(GL_BLEND); + glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + glClear(GL_COLOR_BUFFER_BIT); + drawShadow(); + if (stretchProgram == 0 || cacheWidth == 0) { + glEnable(GL_SCISSOR_TEST); + glScissor(margin, margin, size.x, size.y); + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + return; + } + glViewport(margin, margin, size.x, size.y); + glDisable(GL_DEPTH_TEST); + glDisable(GL_BLEND); + glDisable(GL_CULL_FACE); + glDisable(GL_SCISSOR_TEST); + glUseProgram(stretchProgram); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, cacheTexture); + glUniform1i(glGetUniformLocation(stretchProgram, "uTexture"), 0); + glBindVertexArray(stretchVao); + glDrawArrays(GL_TRIANGLES, 0, 3); + glBindVertexArray(0); + glUseProgram(0); + makeContentOpaque(margin, margin, size.x, size.y); +} + void WaylandWindow::resizeCache(int width, int height) { if (cacheWidth == width && cacheHeight == height) { return; diff --git a/src/window/wayland/WaylandWindow.hpp b/src/window/wayland/WaylandWindow.hpp index 6186b9433..38a822992 100644 --- a/src/window/wayland/WaylandWindow.hpp +++ b/src/window/wayland/WaylandWindow.hpp @@ -55,6 +55,9 @@ double barPressX = 0.0; double barPressY = 0.0; uint32_t barPressSerial = 0; bool barPressed = false; +bool initialSizeApplied = false; +mutable bool frameRendered = false; +bool sizeChanged = false; bool barDragging = false; int barPressedButton = -1; int initialWidth = 0; @@ -113,9 +116,40 @@ glm::vec4 scissorArea {}; - static constexpr int BAR_HEIGHT = 30; - static constexpr int BAR_BUTTON = 30; + static constexpr int BAR_HEIGHT = 48; + static constexpr int BAR_BUTTON = 24; + static constexpr int BAR_PADDING = 12; + static constexpr int BAR_GAP = 12; static constexpr int BAR_EDGE = 8; + static constexpr int SHADOW_MARGIN = 10; + static constexpr int RESIZE_GRAB_EXTRA = 2; + bool shadowEnabled = false; + GLuint shadowProgram = 0; + + GLint shadowUniforms[7] {}; + + int marginSize() const; + + double contentX(double x) const { + return x - marginSize(); + } + + double contentY(double y) const { + return y - marginSize(); + } + + glm::ivec2 surfaceSize() const; + + void updateWindowGeometry(); + + void initShadowRenderer(); + + void drawShadow(); + + void composeFrame(); + + void makeContentOpaque(int x, int y, int width, int height); + int decorationOffset() const { return barVisible() ? BAR_HEIGHT : 0; @@ -152,6 +186,7 @@ glm::vec4 scissorArea {}; int barButtonAt(double x, double y) const; uint32_t barEdgeAt(double x, double y) const; static CursorShape edgeCursor(uint32_t edges); + uint32_t pointerEdges() const; bool handleBarMotion(double x, double y); bool handleBarButton(uint32_t serial, int button, bool pressed); bool handleBarScroll() const;