Skip to content
Merged
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
66 changes: 66 additions & 0 deletions core/window/window_backend.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "core/window/window_backend.h"
#include "core/platform/native_bridge.h"
#include "core/window/x11_moveresize.h"

#include <algorithm>
#include <cmath>
Expand Down Expand Up @@ -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<SDL_Window*>(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<std::uint64_t>(info.info.x11.window), direction);
#else
(void)direction;
return false;
#endif
}
#endif


Expand Down Expand Up @@ -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<int>(edge));
}

} // namespace core::window

#else
Expand All @@ -544,6 +574,11 @@ void setImeCursorRect(Handle window, float x, float y, float width, float height
#endif
#include <GLFW/glfw3.h>

#if defined(__linux__) && !defined(__ANDROID__)
#define GLFW_EXPOSE_NATIVE_X11
#include <GLFW/glfw3native.h>
#endif

#include "core/platform/ime_bridge.h"

namespace core::window {
Expand All @@ -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<GLFWwindow*>(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<std::uint64_t>(glfwGetX11Window(glfwWindow)),
direction);
#else
(void)window;
(void)direction;
return false;
#endif
}

} // namespace

Handle createWindow(const WindowCreateRequest& request) {
Expand Down Expand Up @@ -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<GLFWwindow*>(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<int>(edge));
}

} // namespace core::window

#endif
7 changes: 7 additions & 0 deletions core/window/window_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions core/window/window_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
112 changes: 112 additions & 0 deletions core/window/x11_moveresize.h
Original file line number Diff line number Diff line change
@@ -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 <cstdint>

// 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 <dlfcn.h>
#include <X11/Xlib.h>

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<Atom (*)(Display*, const char*, Bool)>(
dlsym(result.library, "XInternAtom"));
result.queryPointer =
reinterpret_cast<Bool (*)(Display*, Window, Window*, Window*, int*, int*, int*, int*,
unsigned int*)>(dlsym(result.library, "XQueryPointer"));
result.ungrabPointer =
reinterpret_cast<int (*)(Display*, Time)>(dlsym(result.library, "XUngrabPointer"));
result.sendEvent = reinterpret_cast<Status (*)(Display*, Window, Bool, long, XEvent*)>(
dlsym(result.library, "XSendEvent"));
result.flush = reinterpret_cast<int (*)(Display*)>(dlsym(result.library, "XFlush"));
return result;
}();
return api;
}

inline bool sendX11MoveResize(void* displayHandle, std::uint64_t windowId, int direction) {
Display* display = static_cast<Display*>(displayHandle);
const Window window = static_cast<Window>(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<long>(rootX);
event.xclient.data.l[1] = static_cast<long>(rootY);
event.xclient.data.l[2] = static_cast<long>(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
26 changes: 26 additions & 0 deletions docs/平台能力.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,不需要额外运行时工具。
Expand Down
8 changes: 8 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 @@ -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
24 changes: 24 additions & 0 deletions tests/fixtures/window_controls_viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
Loading