Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/app/glfw_app_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ int eui_app_run() {
windowRequest.resizable = app::windowResizable();
windowRequest.highDpi = app::windowHighDpi();
windowRequest.decorated = app::windowDecorated();
windowRequest.transparent = app::windowTransparent();
windowRequest.alwaysOnTop = app::windowAlwaysOnTop();
windowRequest.maximized = app::windowMaximized();
windowRequest.title = app::windowTitle();
Expand Down Expand Up @@ -451,7 +452,7 @@ int eui_app_run() {
glfwTerminate();
};

auto renderBackend = core::render::createRenderBackend(window);
auto renderBackend = core::render::createRenderBackend(window, nullptr, app::windowTransparent());
if (!renderBackend) {
cleanupMainWindow();
return -1;
Expand Down
3 changes: 2 additions & 1 deletion core/app/sdl2_app_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ int eui_app_run() {
windowRequest.resizable = app::windowResizable();
windowRequest.highDpi = app::windowHighDpi();
windowRequest.decorated = app::windowDecorated();
windowRequest.transparent = app::windowTransparent();
windowRequest.alwaysOnTop = app::windowAlwaysOnTop();
windowRequest.maximized = app::windowMaximized();
windowRequest.title = app::windowTitle();
Expand All @@ -624,7 +625,7 @@ int eui_app_run() {
return -1;
}

auto renderBackend = core::render::createRenderBackend(window);
auto renderBackend = core::render::createRenderBackend(window, nullptr, app::windowTransparent());
if (!renderBackend) {
core::window::destroyWindow(window);
SDL_Quit();
Expand Down
6 changes: 4 additions & 2 deletions core/render/render_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ core::window::RenderApi windowRenderApi() {
#endif
}

std::unique_ptr<RenderBackend> createRenderBackend(core::window::Handle window, RenderBackend* shareBackend) {
std::unique_ptr<RenderBackend> createRenderBackend(core::window::Handle window, RenderBackend* shareBackend, bool transparent) {
#if defined(EUI_RENDER_BACKEND_OPENGL)
(void)transparent; // GL composites through the framebuffer alpha channel as-is.
return std::make_unique<opengl::OpenGLRenderBackend>(window, shareBackend);
#elif defined(EUI_RENDER_BACKEND_VULKAN)
return std::make_unique<vulkan::VulkanRenderBackend>(window, shareBackend);
return std::make_unique<vulkan::VulkanRenderBackend>(window, shareBackend, transparent);
#else
(void)window;
(void)shareBackend;
(void)transparent;
return {};
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion core/render/render_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class RenderBackend {
const std::uint64_t deviceIdentity_ = nextDeviceIdentity_.fetch_add(1, std::memory_order_relaxed);
};

std::unique_ptr<RenderBackend> createRenderBackend(core::window::Handle window, RenderBackend* shareBackend = nullptr);
std::unique_ptr<RenderBackend> createRenderBackend(core::window::Handle window, RenderBackend* shareBackend = nullptr, bool transparent = false);
core::window::RenderApi windowRenderApi();
void initializeRenderBackendLoader();

Expand Down
14 changes: 12 additions & 2 deletions core/render/vulkan/vulkan_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ bool VulkanRenderBackend::applyDrawViewportAndScissor(int windowWidth, int windo
return true;
}

VulkanRenderBackend::VulkanRenderBackend(core::window::Handle window, RenderBackend*) : window_(window) {}
VulkanRenderBackend::VulkanRenderBackend(core::window::Handle window, RenderBackend*, bool transparent)
: window_(window), transparent_(transparent) {}

VulkanRenderBackend::~VulkanRenderBackend() {
destroy();
Expand Down Expand Up @@ -670,7 +671,16 @@ bool VulkanRenderBackend::recreateSwapchain(const RenderSurface& surface) {
}
swapchainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
swapchainInfo.preTransform = capabilities.currentTransform;
swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
// Transparent windows need a composite mode that honors the framebuffer
// alpha; the renderer's content is premultiplied, so prefer
// POST_MULTIPLIED and degrade gracefully instead of failing creation.
if (transparent_ && (capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR)) {
swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR;
} else if (transparent_ && (capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)) {
swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR;
} else {
swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
}
swapchainInfo.presentMode = presentMode;
swapchainInfo.clipped = VK_TRUE;
// 保留旧交换链至新链创建调用结束,使 WSI 能复用呈现资源;调用失败也会使旧链退役。
Expand Down
3 changes: 2 additions & 1 deletion core/render/vulkan/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace core::render::vulkan {

class VulkanRenderBackend final : public RenderBackend {
public:
explicit VulkanRenderBackend(core::window::Handle window, RenderBackend* shareBackend = nullptr);
explicit VulkanRenderBackend(core::window::Handle window, RenderBackend* shareBackend = nullptr, bool transparent = false);
~VulkanRenderBackend() override;

VulkanRenderBackend(const VulkanRenderBackend&) = delete;
Expand Down Expand Up @@ -304,6 +304,7 @@ class VulkanRenderBackend final : public RenderBackend {
core::Rect scissorRect_{};
core::Rect cacheRenderArea_{};
core::Color clearColor_{0.0f, 0.0f, 0.0f, 1.0f};
bool transparent_ = false;

VkDescriptorSetLayout roundedRectDescriptorSetLayout_ = VK_NULL_HANDLE;
VkDescriptorPool roundedRectDescriptorPool_ = VK_NULL_HANDLE;
Expand Down
14 changes: 14 additions & 0 deletions core/window/window_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,12 @@ void setImeCursorRect(Handle window, float x, float y, float width, float height
#endif
}

bool framebufferTransparent(Handle) {
// SDL2 has no transparent-window flag (it arrived in SDL3), so the
// request.transparent hint cannot be honored on this backend.
return false;
}

} // namespace core::window

#else
Expand Down Expand Up @@ -576,6 +582,7 @@ Handle createWindow(const WindowCreateRequest& request) {
}
glfwWindowHint(GLFW_RESIZABLE, request.resizable ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_DECORATED, request.decorated ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, request.transparent ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_FLOATING, request.alwaysOnTop ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_MAXIMIZED, request.maximized ? GLFW_TRUE : GLFW_FALSE);
#if defined(GLFW_WAYLAND_APP_ID) || defined(GLFW_X11_CLASS_NAME)
Expand Down Expand Up @@ -680,6 +687,13 @@ void setImeCursorRect(Handle window, float x, float y, float width, float height
eui_ime_set_cursor_rect_with_font(static_cast<GLFWwindow*>(window), x, y, width, height, height);
}

bool framebufferTransparent(Handle window) {
// Reports what the platform actually granted: without a compositor an
// X11 window cannot honor the transparent hint.
return window != nullptr &&
glfwGetWindowAttrib(static_cast<GLFWwindow*>(window), GLFW_TRANSPARENT_FRAMEBUFFER) == GLFW_TRUE;
}

} // namespace core::window

#endif
3 changes: 3 additions & 0 deletions core/window/window_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ void destroyCursor(CursorHandle cursor);

void setWindowIcon(Handle window, int width, int height, unsigned char* pixels);
void setImeCursorRect(Handle window, float x, float y, float width, float height);
// Reports whether the created framebuffer actually carries alpha (GLFW
// honors the transparent request; SDL2 and compositor-less X11 do not).
bool framebufferTransparent(Handle window);
void installInputCallbacks(Handle window);
void uninstallInputCallbacks(Handle window);
bool queryImeComposition(Handle window, std::string& text, bool& composing);
Expand Down
1 change: 1 addition & 0 deletions core/window/window_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct WindowCreateRequest {
bool resizable = true;
bool highDpi = true;
bool decorated = true;
bool transparent = false;
bool alwaysOnTop = false;
bool maximized = false;
bool modal = false;
Expand Down
5 changes: 4 additions & 1 deletion docs/DSL.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ static const DslAppConfig config = DslAppConfig{}
.resizable(true)
.highDpi(true)
.decorated(true)
.transparent(false)
.alwaysOnTop(false)
.maximized(false)
.debugTitleInterval(1.0)
Expand All @@ -91,7 +92,9 @@ static const DslAppConfig config = DslAppConfig{}
});
```

`minWindowSize` 和 `maxWindowSize` 中的 `0` 表示对应方向不限制;窗口尺寸约束由 GLFW/SDL2 后端执行。`centerWindow()` 会清除显式位置并恢复居中。`highDpi` 在 SDL2 中控制 `SDL_WINDOW_ALLOW_HIGHDPI`;GLFW 的 DPI 感知由其初始化阶段按平台设置,是进程级行为,不能安全地按单个窗口关闭。全屏、透明窗口和 VSync 不属于 `DslAppConfig`,它们会改变平台窗口或渲染后端生命周期,应通过专用平台/渲染配置处理。
`minWindowSize` 和 `maxWindowSize` 中的 `0` 表示对应方向不限制;窗口尺寸约束由 GLFW/SDL2 后端执行。`centerWindow()` 会清除显式位置并恢复居中。`highDpi` 在 SDL2 中控制 `SDL_WINDOW_ALLOW_HIGHDPI`;GLFW 的 DPI 感知由其初始化阶段按平台设置,是进程级行为,不能安全地按单个窗口关闭。全屏和 VSync 不属于 `DslAppConfig`,它们会改变平台窗口或渲染后端生命周期,应通过专用平台/渲染配置处理。

`.transparent(true)` 与 `decorated` 同属创建期窗口属性:GLFW 后端以 `GLFW_TRANSPARENT_FRAMEBUFFER` 请求带 alpha 的 framebuffer,Vulkan 后端据此选择受支持的 composite-alpha 模式,用于无边框自绘圆角窗口。平台是否真的授予以 `eui::window::framebufferTransparent(window)` 为准(SDL2 后端不支持;X11 无合成器时会拒绝)。启用后应用应把 `clearColor` 的 alpha 设为 0,并自绘圆角根表面;未启用时行为与从前逐像素一致。

Debug 配置只控制诊断输出,不参与业务状态。`showDebugStatsInTitle` 控制窗口标题中的 FPS、CPU/GPU 和渲染统计;`debugTitleInterval` 控制标题统计刷新间隔(秒)。`showDebugOverlay` 与 `onDebugOverlay` 用于注入布局边界、性能标记等调试框,回调在每次页面 compose 后执行;未设置回调时不会绘制任何额外内容。Debug 构建默认开启标题统计和覆盖层开关,Release 构建默认关闭。

Expand Down
1 change: 1 addition & 0 deletions include/eui/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ int maximumWindowHeight();
bool windowResizable();
bool windowHighDpi();
bool windowDecorated();
bool windowTransparent();
bool windowAlwaysOnTop();
bool windowMaximized();
float uiScale();
Expand Down
4 changes: 4 additions & 0 deletions include/eui/detail/dsl_app_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ bool windowDecorated() {
return dslAppConfig().decoratedValue;
}

bool windowTransparent() {
return dslAppConfig().transparentValue;
}

bool windowAlwaysOnTop() {
return dslAppConfig().alwaysOnTopValue;
}
Expand Down
3 changes: 3 additions & 0 deletions include/eui/dsl_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct DslAppConfig {
bool resizableValue = true;
bool highDpiValue = true;
bool decoratedValue = true;
bool transparentValue = false;
bool alwaysOnTopValue = false;
bool maximizedValue = false;
float uiScaleValue = 1.0f;
Expand Down Expand Up @@ -85,6 +86,8 @@ struct DslAppConfig {
DslAppConfig& resizable(bool value = true) { resizableValue = value; return *this; }
DslAppConfig& highDpi(bool value = true) { highDpiValue = value; return *this; }
DslAppConfig& decorated(bool value = true) { decoratedValue = value; return *this; }
/** @brief 请求带 alpha 的 framebuffer(自绘圆角窗口)。平台不支持时 framebufferTransparent 返回 false。 */
DslAppConfig& transparent(bool value = true) { transparentValue = value; return *this; }
DslAppConfig& alwaysOnTop(bool value = true) { alwaysOnTopValue = value; return *this; }
DslAppConfig& maximized(bool value = true) { maximizedValue = value; return *this; }
DslAppConfig& uiScale(float value) {
Expand Down
5 changes: 5 additions & 0 deletions include/eui/window.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "core/window/window_backend.h"
#include "core/window/window_types.h"

namespace eui::window {
Expand All @@ -9,4 +10,8 @@ using CursorHandle = core::window::CursorHandle;
using CursorType = core::window::CursorType;
using Handle = core::window::Handle;

// Reports whether the created framebuffer actually carries alpha (GLFW honors
// the transparent request; SDL2 and compositor-less X11 do not).
using core::window::framebufferTransparent;

} // namespace eui::window
73 changes: 73 additions & 0 deletions tests/fixtures/transparent_window_viewer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "eui_neo.h"

#if defined(EUI_WINDOW_BACKEND_SDL2)
#include <SDL.h>
#else
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#endif

namespace app {
namespace {

#if defined(EUI_WINDOW_BACKEND_SDL2)
SDL_Window* window = nullptr;
#else
GLFWwindow* window = nullptr;
#endif

void capture() {
if (window) return;
#if defined(EUI_WINDOW_BACKEND_SDL2)
window = SDL_GetKeyboardFocus();
#else
// 通过当前 OpenGL context 获取应用窗口,不替换框架事件回调。
window = glfwGetCurrentContext();
#endif
}

} // namespace

const DslAppConfig& dslAppConfig() {
#if defined(EUI_WINDOW_BACKEND_SDL2)
constexpr const char* title = "EUI Transparent Window Test - SDL2";
#else
constexpr const char* title = "EUI Transparent Window Test - GLFW";
#endif
static const auto config = DslAppConfig{}.title(title).windowSize(760, 480)
.decorated(false).transparent(true).clearColor({0.0f, 0.0f, 0.0f, 0.0f});
return config;
}

void compose(eui::Ui& ui, const eui::Screen& screen) {
capture();
const bool granted = eui::window::framebufferTransparent(window);
const float margin = 36.f;
const float inset = 40.f;
ui.stack("root").size(screen.width, screen.height).content([&] {
ui.rect("panel")
.position(margin, margin)
.size(screen.width - margin * 2.f, screen.height - margin * 2.f)
.radius(28.f)
.color("#0F172A")
.build();
ui.column("content")
.position(margin + inset, margin + 36.f)
.size(screen.width - (margin + inset) * 2.f, screen.height - (margin + 36.f) * 2.f)
.gap(12.f)
.content([&] {
ui.text("headline").size(520.f, 34.f).fontSize(24.f).color("#E2E8F0")
.text("Transparent window").build();
ui.text("status").size(620.f, 24.f).fontSize(15.f)
.color(granted ? "#4ADE80" : "#F87171")
.text(granted
? "framebufferTransparent: true - desktop shows through corners and margins"
: "framebufferTransparent: false - platform did not grant an alpha surface")
.build();
ui.text("hint").size(620.f, 24.f).fontSize(14.f).color("#94A3B8")
.text("Rounded corners and the outer margin are fully transparent.").build();
}).build();
}).build();
}

} // namespace app
Loading