Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement DisplayServer.window_start_resize. #100532

Merged
merged 1 commit into from
Jan 7, 2025
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
40 changes: 38 additions & 2 deletions doc/classes/DisplayServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1836,8 +1836,17 @@
<return type="void" />
<param index="0" name="window_id" type="int" default="0" />
<description>
Starts a drag operation on the window with the given [param window_id], using the current mouse position. Call this method when handling a mouse button being pressed to simulate a pressed event on the window's title bar. Using this method allows the window to participate in space switching, tiling, and other system features.
[b]Note:[/b] This method is implemented on Linux(X11/Wayland), macOS, and Windows.
Starts an interactive drag operation on the window with the given [param window_id], using the current mouse position. Call this method when handling a mouse button being pressed to simulate a pressed event on the window's title bar. Using this method allows the window to participate in space switching, tiling, and other system features.
[b]Note:[/b] This method is implemented on Linux (X11/Wayland), macOS, and Windows.
</description>
</method>
<method name="window_start_resize">
<return type="void" />
<param index="0" name="edge" type="int" enum="DisplayServer.WindowResizeEdge" />
<param index="1" name="window_id" type="int" default="0" />
<description>
Starts an interactive resize operation on the window with the given [param window_id], using the current mouse position. Call this method when handling a mouse button being pressed to simulate a pressed event on the window's edge.
[b]Note:[/b] This method is implemented on Linux (X11/Wayland), macOS, and Windows.
</description>
</method>
</methods>
Expand Down Expand Up @@ -2183,6 +2192,33 @@
Sent when the window title bar decoration is changed (e.g. [constant WINDOW_FLAG_EXTEND_TO_TITLE] is set or window entered/exited full screen mode).
[b]Note:[/b] This flag is implemented only on macOS.
</constant>
<constant name="WINDOW_EDGE_TOP_LEFT" value="0" enum="WindowResizeEdge">
Top-left edge of a window.
</constant>
<constant name="WINDOW_EDGE_TOP" value="1" enum="WindowResizeEdge">
Top edge of a window.
</constant>
<constant name="WINDOW_EDGE_TOP_RIGHT" value="2" enum="WindowResizeEdge">
Top-right edge of a window.
</constant>
<constant name="WINDOW_EDGE_LEFT" value="3" enum="WindowResizeEdge">
Left edge of a window.
</constant>
<constant name="WINDOW_EDGE_RIGHT" value="4" enum="WindowResizeEdge">
Right edge of a window.
</constant>
<constant name="WINDOW_EDGE_BOTTOM_LEFT" value="5" enum="WindowResizeEdge">
Bottom-left edge of a window.
</constant>
<constant name="WINDOW_EDGE_BOTTOM" value="6" enum="WindowResizeEdge">
Bottom edge of a window.
</constant>
<constant name="WINDOW_EDGE_BOTTOM_RIGHT" value="7" enum="WindowResizeEdge">
Bottom-right edge of a window.
</constant>
<constant name="WINDOW_EDGE_MAX" value="8" enum="WindowResizeEdge">
Represents the size of the [enum WindowResizeEdge] enum.
</constant>
<constant name="VSYNC_DISABLED" value="0" enum="VSyncMode">
No vertical synchronization, which means the engine will display frames as fast as possible (tearing may be visible). Framerate is unlimited (regardless of [member Engine.max_fps]).
</constant>
Expand Down
7 changes: 7 additions & 0 deletions platform/linuxbsd/wayland/display_server_wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,13 @@ void DisplayServerWayland::window_start_drag(WindowID p_window) {
wayland_thread.window_start_drag(p_window);
}

void DisplayServerWayland::window_start_resize(WindowResizeEdge p_edge, WindowID p_window) {
MutexLock mutex_lock(wayland_thread.mutex);

ERR_FAIL_INDEX(int(p_edge), WINDOW_EDGE_MAX);
wayland_thread.window_start_resize(p_edge, p_window);
}

void DisplayServerWayland::cursor_set_shape(CursorShape p_shape) {
ERR_FAIL_INDEX(p_shape, CURSOR_MAX);

Expand Down
1 change: 1 addition & 0 deletions platform/linuxbsd/wayland/display_server_wayland.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ class DisplayServerWayland : public DisplayServer {
virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_window_id) const override;

virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_start_resize(WindowResizeEdge p_edge, WindowID p_window) override;

virtual void cursor_set_shape(CursorShape p_shape) override;
virtual CursorShape cursor_get_shape() const override;
Expand Down
74 changes: 74 additions & 0 deletions platform/linuxbsd/wayland/wayland_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3354,6 +3354,80 @@ void WaylandThread::window_start_drag(DisplayServer::WindowID p_window_id) {
#endif
}

void WaylandThread::window_start_resize(DisplayServer::WindowResizeEdge p_edge, DisplayServer::WindowID p_window) {
// TODO: Use window IDs for multiwindow support.
WindowState &ws = main_window;
SeatState *ss = wl_seat_get_seat_state(wl_seat_current);

if (ss && ws.xdg_toplevel) {
xdg_toplevel_resize_edge edge = XDG_TOPLEVEL_RESIZE_EDGE_NONE;
switch (p_edge) {
case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT;
} break;
case DisplayServer::WINDOW_EDGE_TOP: {
edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP;
} break;
case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
edge = XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT;
} break;
case DisplayServer::WINDOW_EDGE_LEFT: {
edge = XDG_TOPLEVEL_RESIZE_EDGE_LEFT;
} break;
case DisplayServer::WINDOW_EDGE_RIGHT: {
edge = XDG_TOPLEVEL_RESIZE_EDGE_RIGHT;
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT;
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM: {
edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM;
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
edge = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT;
} break;
default:
break;
}
xdg_toplevel_resize(ws.xdg_toplevel, ss->wl_seat, ss->pointer_data.button_serial, edge);
}

#ifdef LIBDECOR_ENABLED
if (ws.libdecor_frame) {
libdecor_resize_edge edge = LIBDECOR_RESIZE_EDGE_NONE;
switch (p_edge) {
case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
edge = LIBDECOR_RESIZE_EDGE_TOP_LEFT;
} break;
case DisplayServer::WINDOW_EDGE_TOP: {
edge = LIBDECOR_RESIZE_EDGE_TOP;
} break;
case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
edge = LIBDECOR_RESIZE_EDGE_TOP_RIGHT;
} break;
case DisplayServer::WINDOW_EDGE_LEFT: {
edge = LIBDECOR_RESIZE_EDGE_LEFT;
} break;
case DisplayServer::WINDOW_EDGE_RIGHT: {
edge = LIBDECOR_RESIZE_EDGE_RIGHT;
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
edge = LIBDECOR_RESIZE_EDGE_BOTTOM_LEFT;
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM: {
edge = LIBDECOR_RESIZE_EDGE_BOTTOM;
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
edge = LIBDECOR_RESIZE_EDGE_BOTTOM_RIGHT;
} break;
default:
break;
}
libdecor_frame_resize(ws.libdecor_frame, ss->wl_seat, ss->pointer_data.button_serial, edge);
}
#endif
}

void WaylandThread::window_set_max_size(DisplayServer::WindowID p_window_id, const Size2i &p_size) {
// TODO: Use window IDs for multiwindow support.
WindowState &ws = main_window;
Expand Down
2 changes: 2 additions & 0 deletions platform/linuxbsd/wayland/wayland_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,8 @@ class WaylandThread {

struct wl_surface *window_get_wl_surface(DisplayServer::WindowID p_window_id) const;

void window_start_resize(DisplayServer::WindowResizeEdge p_edge, DisplayServer::WindowID p_window);

void window_set_max_size(DisplayServer::WindowID p_window_id, const Size2i &p_size);
void window_set_min_size(DisplayServer::WindowID p_window_id, const Size2i &p_size);

Expand Down
72 changes: 72 additions & 0 deletions platform/linuxbsd/x11/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
#define _NET_WM_STATE_REMOVE 0L // remove/unset property
#define _NET_WM_STATE_ADD 1L // add/set property

#define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0L
#define _NET_WM_MOVERESIZE_SIZE_TOP 1L
#define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2L
#define _NET_WM_MOVERESIZE_SIZE_RIGHT 3L
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4L
#define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5L
#define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6L
#define _NET_WM_MOVERESIZE_SIZE_LEFT 7L
#define _NET_WM_MOVERESIZE_MOVE 8L

// 2.2 is the first release with multitouch
Expand Down Expand Up @@ -5516,6 +5524,70 @@ void DisplayServerX11::window_start_drag(WindowID p_window) {
XSync(x11_display, 0);
}

void DisplayServerX11::window_start_resize(WindowResizeEdge p_edge, WindowID p_window) {
_THREAD_SAFE_METHOD_

ERR_FAIL_INDEX(int(p_edge), WINDOW_EDGE_MAX);

ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];

XClientMessageEvent m;
memset(&m, 0, sizeof(m));

XUngrabPointer(x11_display, CurrentTime);

Window root_return, child_return;
int root_x, root_y, win_x, win_y;
unsigned int mask_return;

Bool xquerypointer_result = XQueryPointer(x11_display, wd.x11_window, &root_return, &child_return, &root_x, &root_y, &win_x, &win_y, &mask_return);

m.type = ClientMessage;
m.window = wd.x11_window;
m.message_type = XInternAtom(x11_display, "_NET_WM_MOVERESIZE", True);
m.format = 32;
if (xquerypointer_result) {
m.data.l[0] = root_x;
m.data.l[1] = root_y;
m.data.l[3] = Button1;
}

switch (p_edge) {
case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_TOPLEFT;
} break;
case DisplayServer::WINDOW_EDGE_TOP: {
m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_TOP;
} break;
case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT;
} break;
case DisplayServer::WINDOW_EDGE_LEFT: {
m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_LEFT;
} break;
case DisplayServer::WINDOW_EDGE_RIGHT: {
m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_RIGHT;
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT;
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM: {
m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_BOTTOM;
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
m.data.l[2] = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT;
} break;
default:
break;
}
m.data.l[4] = 1; // Source - normal application.

XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&m);

XSync(x11_display, 0);
}

pid_t get_window_pid(Display *p_display, Window p_window) {
Atom atom = XInternAtom(p_display, "_NET_WM_PID", False);
Atom actualType;
Expand Down
1 change: 1 addition & 0 deletions platform/linuxbsd/x11/display_server_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,7 @@ class DisplayServerX11 : public DisplayServer {
virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;

virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_start_resize(WindowResizeEdge p_edge, WindowID p_window) override;

virtual Error embed_process(WindowID p_window, OS::ProcessID p_pid, const Rect2i &p_rect, bool p_visible, bool p_grab_focus) override;
virtual Error remove_embedded_process(OS::ProcessID p_pid) override;
Expand Down
2 changes: 2 additions & 0 deletions platform/macos/display_server_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class DisplayServerMacOS : public DisplayServer {
Vector<Vector2> mpath;

Point2i mouse_pos;
WindowResizeEdge edge = WINDOW_EDGE_MAX;

Size2i min_size;
Size2i max_size;
Expand Down Expand Up @@ -409,6 +410,7 @@ class DisplayServerMacOS : public DisplayServer {
virtual bool window_minimize_on_title_dbl_click() const override;

virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) override;
virtual void window_start_resize(WindowResizeEdge p_edge, WindowID p_window = MAIN_WINDOW_ID) override;

virtual void window_set_window_buttons_offset(const Vector2i &p_offset, WindowID p_window = MAIN_WINDOW_ID) override;
virtual Vector3i window_get_safe_title_margins(WindowID p_window = MAIN_WINDOW_ID) const override;
Expand Down
10 changes: 10 additions & 0 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2440,6 +2440,16 @@
[wd.window_object performWindowDragWithEvent:event];
}

void DisplayServerMacOS::window_start_resize(WindowResizeEdge p_edge, WindowID p_window) {
_THREAD_SAFE_METHOD_

ERR_FAIL_INDEX(int(p_edge), WINDOW_EDGE_MAX);
ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];

wd.edge = p_edge;
}

void DisplayServerMacOS::window_set_window_buttons_offset(const Vector2i &p_offset, WindowID p_window) {
_THREAD_SAFE_METHOD_

Expand Down
60 changes: 60 additions & 0 deletions platform/macos/godot_content_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,11 @@ - (void)processMouseEvent:(NSEvent *)event index:(MouseButton)index pressed:(boo
}

- (void)mouseDown:(NSEvent *)event {
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
if (ds && ds->has_window(window_id)) {
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
wd.edge = DisplayServer::WINDOW_EDGE_MAX;
}
if (([event modifierFlags] & NSEventModifierFlagControl)) {
mouse_down_control = true;
[self processMouseEvent:event index:MouseButton::RIGHT pressed:true outofstream:false];
Expand All @@ -404,10 +409,65 @@ - (void)mouseDown:(NSEvent *)event {
}

- (void)mouseDragged:(NSEvent *)event {
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
if (ds && ds->has_window(window_id)) {
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
if (wd.edge != DisplayServer::WINDOW_EDGE_MAX) {
Size2i max_size = wd.max_size / ds->screen_get_max_scale();
Size2i min_size = wd.min_size / ds->screen_get_max_scale();
NSRect frame = [wd.window_object frame];
switch (wd.edge) {
case DisplayServer::WINDOW_EDGE_TOP_LEFT: {
int clamped_dx = CLAMP(frame.size.width - event.deltaX, min_size.x, max_size.x) - frame.size.width;
int clamped_dy = CLAMP(frame.size.height - event.deltaY, min_size.y, max_size.y) - frame.size.height;
[wd.window_object setFrame:NSMakeRect(frame.origin.x - clamped_dx, frame.origin.y, frame.size.width + clamped_dx, frame.size.height + clamped_dy) display:YES];
} break;
case DisplayServer::WINDOW_EDGE_TOP: {
int clamped_dy = CLAMP(frame.size.height - event.deltaY, min_size.y, max_size.y) - frame.size.height;
[wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height + clamped_dy) display:YES];
} break;
case DisplayServer::WINDOW_EDGE_TOP_RIGHT: {
int clamped_dx = CLAMP(frame.size.width + event.deltaX, min_size.x, max_size.x) - frame.size.width;
int clamped_dy = CLAMP(frame.size.height - event.deltaY, min_size.y, max_size.y) - frame.size.height;
[wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y, frame.size.width + clamped_dx, frame.size.height + clamped_dy) display:YES];
} break;
case DisplayServer::WINDOW_EDGE_LEFT: {
int clamped_dx = CLAMP(frame.size.width - event.deltaX, min_size.x, max_size.x) - frame.size.width;
[wd.window_object setFrame:NSMakeRect(frame.origin.x - clamped_dx, frame.origin.y, frame.size.width + clamped_dx, frame.size.height) display:YES];
} break;
case DisplayServer::WINDOW_EDGE_RIGHT: {
int clamped_dx = CLAMP(frame.size.width + event.deltaX, min_size.x, max_size.x) - frame.size.width;
[wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y, frame.size.width + clamped_dx, frame.size.height) display:YES];
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM_LEFT: {
int clamped_dx = CLAMP(frame.size.width - event.deltaX, min_size.x, max_size.x) - frame.size.width;
int clamped_dy = CLAMP(frame.size.height + event.deltaY, min_size.y, max_size.y) - frame.size.height;
[wd.window_object setFrame:NSMakeRect(frame.origin.x - clamped_dx, frame.origin.y - clamped_dy, frame.size.width + clamped_dx, frame.size.height + clamped_dy) display:YES];
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM: {
int clamped_dy = CLAMP(frame.size.height + event.deltaY, min_size.y, max_size.y) - frame.size.height;
[wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y - clamped_dy, frame.size.width, frame.size.height + clamped_dy) display:YES];
} break;
case DisplayServer::WINDOW_EDGE_BOTTOM_RIGHT: {
int clamped_dx = CLAMP(frame.size.width + event.deltaX, min_size.x, max_size.x) - frame.size.width;
int clamped_dy = CLAMP(frame.size.height + event.deltaY, min_size.y, max_size.y) - frame.size.height;
[wd.window_object setFrame:NSMakeRect(frame.origin.x, frame.origin.y - clamped_dy, frame.size.width + clamped_dx, frame.size.height + clamped_dy) display:YES];
} break;
default:
break;
}
return;
}
}
[self mouseMoved:event];
}

- (void)mouseUp:(NSEvent *)event {
DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
if (ds && ds->has_window(window_id)) {
DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
wd.edge = DisplayServer::WINDOW_EDGE_MAX;
}
if (mouse_down_control) {
[self processMouseEvent:event index:MouseButton::RIGHT pressed:false outofstream:false];
} else {
Expand Down
Loading