diff --git a/core/window/window_backend.cpp b/core/window/window_backend.cpp index e1851d9a..1124ff6e 100644 --- a/core/window/window_backend.cpp +++ b/core/window/window_backend.cpp @@ -1,5 +1,6 @@ #include "core/window/window_backend.h" #include "core/platform/native_bridge.h" +#include "core/window/x11_moveresize.h" #include #include @@ -273,6 +274,27 @@ const X11ResourceApi& x11ResourceApi() { static const X11ResourceApi api = loadX11ResourceApi(); return api; } + +bool sendX11MoveResizeForWindow(Handle window, int direction) { + SDL_Window* sdlWindow = static_cast(window); + if (!sdlWindow) { + return false; + } +#if defined(__linux__) && !defined(__ANDROID__) && defined(SDL_VIDEO_DRIVER_X11) + SDL_SysWMinfo info{}; + SDL_VERSION(&info.version); + if (SDL_GetWindowWMInfo(sdlWindow, &info) != SDL_TRUE || + info.subsystem != SDL_SYSWM_X11 || + info.info.x11.display == nullptr) { + return false; + } + return detail::sendX11MoveResize(info.info.x11.display, + static_cast(info.info.x11.window), direction); +#else + (void)direction; + return false; +#endif +} #endif @@ -535,6 +557,14 @@ void setImeCursorRect(Handle window, float x, float y, float width, float height #endif } +bool beginWindowMove(Handle window) { + return sendX11MoveResizeForWindow(window, kNetWmMoveResizeMove); +} + +bool beginWindowResize(Handle window, WindowResizeEdge edge) { + return sendX11MoveResizeForWindow(window, static_cast(edge)); +} + } // namespace core::window #else @@ -544,6 +574,11 @@ void setImeCursorRect(Handle window, float x, float y, float width, float height #endif #include +#if defined(__linux__) && !defined(__ANDROID__) +#define GLFW_EXPOSE_NATIVE_X11 +#include +#endif + #include "core/platform/ime_bridge.h" namespace core::window { @@ -564,6 +599,29 @@ void configureOpenGLWindowHints() { glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); } +bool sendX11MoveResizeForWindow(Handle window, int direction) { +#if defined(__linux__) && !defined(__ANDROID__) + GLFWwindow* glfwWindow = static_cast(window); + if (!glfwWindow) { + return false; + } +#if defined(GLFW_PLATFORM_X11) + // Runtime platform selection (GLFW 3.4+): only X11 windows can be moved + // via the EWMH message. + if (glfwGetPlatform() != GLFW_PLATFORM_X11) { + return false; + } +#endif + return detail::sendX11MoveResize(glfwGetX11Display(), + static_cast(glfwGetX11Window(glfwWindow)), + direction); +#else + (void)window; + (void)direction; + return false; +#endif +} + } // namespace Handle createWindow(const WindowCreateRequest& request) { @@ -680,6 +738,14 @@ void setImeCursorRect(Handle window, float x, float y, float width, float height eui_ime_set_cursor_rect_with_font(static_cast(window), x, y, width, height, height); } +bool beginWindowMove(Handle window) { + return sendX11MoveResizeForWindow(window, kNetWmMoveResizeMove); +} + +bool beginWindowResize(Handle window, WindowResizeEdge edge) { + return sendX11MoveResizeForWindow(window, static_cast(edge)); +} + } // namespace core::window #endif diff --git a/core/window/window_backend.h b/core/window/window_backend.h index 958ece0e..616b92fe 100644 --- a/core/window/window_backend.h +++ b/core/window/window_backend.h @@ -29,6 +29,13 @@ 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); +// Hand an interactive move/resize to the window manager (EWMH +// _NET_WM_MOVERESIZE on X11). Per-frame programmatic moves arrive as +// ConfigureRequests and can be clamped back into the workarea, so undecorated +// windows should delegate title-bar drags this way. Returns false when the +// current platform/backend cannot delegate; callers keep their own fallback. +bool beginWindowMove(Handle window); +bool beginWindowResize(Handle window, WindowResizeEdge edge); void installInputCallbacks(Handle window); void uninstallInputCallbacks(Handle window); bool queryImeComposition(Handle window, std::string& text, bool& composing); diff --git a/core/window/window_types.h b/core/window/window_types.h index 764c5fa8..69bfb253 100644 --- a/core/window/window_types.h +++ b/core/window/window_types.h @@ -13,6 +13,19 @@ enum class CursorType { Hand }; +// Edge or corner that starts a compositor-mediated resize. The values match +// the EWMH _NET_WM_MOVERESIZE SIZE_* directions (0-7); move is 8. +enum class WindowResizeEdge { + TopLeft, + Top, + TopRight, + Right, + BottomRight, + Bottom, + BottomLeft, + Left +}; + enum class RenderApi { OpenGL, Vulkan diff --git a/core/window/x11_moveresize.h b/core/window/x11_moveresize.h new file mode 100644 index 00000000..0e3fb291 --- /dev/null +++ b/core/window/x11_moveresize.h @@ -0,0 +1,112 @@ +#pragma once + +// Compositor-mediated move/resize for X11 windows via the EWMH +// _NET_WM_MOVERESIZE ClientMessage. libX11 is loaded dynamically, matching the +// backends' other X11 lookups, so no build configuration gains a link-time X11 +// dependency; when libX11 or the protocol is unavailable the request is +// reported as failed and callers keep their programmatic fallback. + +#include + +// EWMH _NET_WM_MOVERESIZE direction value for moves; 0-7 are the +// WindowResizeEdge corners/edges. +inline constexpr int kNetWmMoveResizeMove = 8; + +#if defined(__linux__) && !defined(__ANDROID__) + +#include +#include + +namespace core::window::detail { + +struct X11MoveResizeApi { + void* library = nullptr; + Atom (*internAtom)(Display*, const char*, Bool) = nullptr; + Bool (*queryPointer)(Display*, Window, Window*, Window*, int*, int*, int*, int*, + unsigned int*) = nullptr; + int (*ungrabPointer)(Display*, Time) = nullptr; + Status (*sendEvent)(Display*, Window, Bool, long, XEvent*) = nullptr; + int (*flush)(Display*) = nullptr; + + bool valid() const { + return library && internAtom && queryPointer && ungrabPointer && sendEvent && flush; + } +}; + +inline const X11MoveResizeApi& x11MoveResizeApi() { + static const X11MoveResizeApi api = [] { + X11MoveResizeApi result; + result.library = dlopen("libX11.so.6", RTLD_LAZY | RTLD_LOCAL); + if (!result.library) { + result.library = dlopen("libX11.so", RTLD_LAZY | RTLD_LOCAL); + } + if (!result.library) { + return result; + } + result.internAtom = reinterpret_cast( + dlsym(result.library, "XInternAtom")); + result.queryPointer = + reinterpret_cast(dlsym(result.library, "XQueryPointer")); + result.ungrabPointer = + reinterpret_cast(dlsym(result.library, "XUngrabPointer")); + result.sendEvent = reinterpret_cast( + dlsym(result.library, "XSendEvent")); + result.flush = reinterpret_cast(dlsym(result.library, "XFlush")); + return result; + }(); + return api; +} + +inline bool sendX11MoveResize(void* displayHandle, std::uint64_t windowId, int direction) { + Display* display = static_cast(displayHandle); + const Window window = static_cast(windowId); + const X11MoveResizeApi& api = x11MoveResizeApi(); + if (!display || !window || !api.valid()) { + return false; + } + + const Window root = DefaultRootWindow(display); + Window rootReturn = None; + Window childReturn = None; + int rootX = 0, rootY = 0, winX = 0, winY = 0; + unsigned int mask = 0; + if (!api.queryPointer(display, root, &rootReturn, &childReturn, &rootX, &rootY, &winX, &winY, + &mask)) { + return false; + } + + XEvent event{}; + event.xclient.type = ClientMessage; + event.xclient.send_event = True; + event.xclient.display = display; + event.xclient.window = window; + event.xclient.message_type = api.internAtom(display, "_NET_WM_MOVERESIZE", False); + event.xclient.format = 32; + event.xclient.data.l[0] = static_cast(rootX); + event.xclient.data.l[1] = static_cast(rootY); + event.xclient.data.l[2] = static_cast(direction); + event.xclient.data.l[3] = Button1; + event.xclient.data.l[4] = 1; // source indication: application + // The implicit grab from the initiating press must be released before the + // window manager can take over the pointer. + api.ungrabPointer(display, CurrentTime); + const Bool sent = api.sendEvent(display, root, False, + SubstructureRedirectMask | SubstructureNotifyMask, &event); + api.flush(display); + return event.xclient.message_type != None && sent != False; +} + +} // namespace core::window::detail + +#else + +namespace core::window::detail { + +inline bool sendX11MoveResize(void*, std::uint64_t, int) { + return false; +} + +} // namespace core::window::detail + +#endif diff --git "a/docs/\345\271\263\345\217\260\350\203\275\345\212\233.md" "b/docs/\345\271\263\345\217\260\350\203\275\345\212\233.md" index 6c560e73..00755ebc 100644 --- "a/docs/\345\271\263\345\217\260\350\203\275\345\212\233.md" +++ "b/docs/\345\271\263\345\217\260\350\203\275\345\212\233.md" @@ -137,6 +137,32 @@ if (result.status == eui::platform::FileDialogStatus::Selected) { } ``` +## 交给窗口管理器的拖动移动 / 缩放 + +无边框窗口(`.decorated(false)`)自绘标题栏或边缘拖手时,不要用逐帧 `glfwSetWindowPos` / +`SDL_SetWindowPosition` 实现拖动:程序化移动以 ConfigureRequest 到达窗口管理器,X11 WM(如 +Mutter)会把它钳制回工作区,窗口无法拖出屏幕边缘;原生 Wayland 则完全不允许客户端定位。正确 +做法是在按下时把这次交互交给窗口管理器: + +```cpp +titlebar.onPress([] { + if (eui::window::beginWindowMove(window)) { + // compositor 已接管:停用自己的拖动循环 + } else { + // 平台不支持(返回 false),继续使用程序化回退 + } +}); + +resizeHandle.onPress([] { + eui::window::beginWindowResize(window, eui::window::WindowResizeEdge::BottomRight); +}); +``` + +- X11:发送 EWMH `_NET_WM_MOVERESIZE`,由 WM 执行交互式移动/缩放,允许拖出屏幕边缘并享受 + 原生贴边分屏。 +- Windows / macOS / SDL2 非 X11:当前返回 `false`,程序化回退保持原行为。 +- Wayland:需要后端提供 seat serial(`xdg_toplevel.move`),当前同样返回 `false`。 + ## 平台依赖 - Windows:使用系统 Common Dialog,不需要额外运行时工具。 diff --git a/include/eui/window.h b/include/eui/window.h index e570d133..a49651de 100644 --- a/include/eui/window.h +++ b/include/eui/window.h @@ -1,5 +1,6 @@ #pragma once +#include "core/window/window_backend.h" #include "core/window/window_types.h" namespace eui::window { @@ -8,5 +9,12 @@ using ContextKey = core::window::ContextKey; using CursorHandle = core::window::CursorHandle; using CursorType = core::window::CursorType; using Handle = core::window::Handle; +using WindowResizeEdge = core::window::WindowResizeEdge; + +// Hand an interactive move/resize to the window manager (EWMH +// _NET_WM_MOVERESIZE on X11). Returns false when the current +// platform/backend cannot delegate; callers keep their own fallback. +using core::window::beginWindowMove; +using core::window::beginWindowResize; } // namespace eui::window diff --git a/tests/fixtures/window_controls_viewer.cpp b/tests/fixtures/window_controls_viewer.cpp index f53a1ea7..afc66171 100644 --- a/tests/fixtures/window_controls_viewer.cpp +++ b/tests/fixtures/window_controls_viewer.cpp @@ -116,6 +116,22 @@ struct WindowControls { resize(1000, 720); } + void beginMove() { + if (!window || fullscreen) return; + status = eui::window::beginWindowMove(window) + ? "Compositor move started: drag the window past the screen edges." + : "Compositor move is unavailable on this platform/backend."; + requestUpdate(); + } + + void beginResize(eui::window::WindowResizeEdge edge, const char* name) { + if (!window || fullscreen) return; + status = eui::window::beginWindowResize(window, edge) + ? (std::string("Compositor resize started (") + name + "); move the pointer.") + : "Compositor resize is unavailable on this platform/backend."; + requestUpdate(); + } + void close() { if (!window) return; #if defined(EUI_WINDOW_BACKEND_SDL2) @@ -191,6 +207,14 @@ void compose(eui::Ui& ui, const eui::Screen& screen) { .disabled(controls.fullscreen).onClick([] { controls.toggleBorder(); }).build(); components::button(ui, "restore").size(160, 38).text("Restore (Esc)").onClick([] { controls.restore(); }).build(); }).build(); + ui.row("native").gap(12.f).content([&] { + components::button(ui, "native.move").size(170, 38).text("Compositor move") + .onClick([] { controls.beginMove(); }).build(); + components::button(ui, "native.resize.left").size(140, 38).text("Resize left") + .onClick([] { controls.beginResize(eui::window::WindowResizeEdge::Left, "left"); }).build(); + components::button(ui, "native.resize.bottomright").size(190, 38).text("Resize bottom right") + .onClick([] { controls.beginResize(eui::window::WindowResizeEdge::BottomRight, "bottom right"); }).build(); + }).build(); label(ui, "hint", "F11: toggle fullscreen. Esc: restore window and border.", 14.f); label(ui, "status", controls.window ? controls.status : "This fixture requires a current OpenGL window.", 14.f); components::button(ui, "close").size(160, 36).text("Close window").onClick([] { controls.close(); }).build();