Skip to content

Commit

Permalink
Merge pull request godotengine#99642 from bruvzg/mac_win_tile
Browse files Browse the repository at this point in the history
[macOS] Use native window drag for the custom editor title bars.
  • Loading branch information
akien-mga committed Dec 2, 2024
2 parents 5836a24 + b248d66 commit bb09a6f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
11 changes: 11 additions & 0 deletions doc/classes/DisplayServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1815,6 +1815,14 @@
[b]Warning:[/b] Advanced users only! Adding such a callback to a [Window] node will override its default implementation, which can introduce bugs.
</description>
</method>
<method name="window_start_drag">
<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 only on macOS.
</description>
</method>
</methods>
<constants>
<constant name="FEATURE_GLOBAL_MENU" value="0" enum="Feature" deprecated="Use [NativeMenu] or [PopupMenu] instead.">
Expand Down Expand Up @@ -1895,6 +1903,9 @@
<constant name="FEATURE_NATIVE_DIALOG_FILE_EXTRA" value="26" enum="Feature">
The display server supports all features of [constant FEATURE_NATIVE_DIALOG_FILE], with the added functionality of Options and native dialog file access to [code]res://[/code] and [code]user://[/code] paths. See [method file_dialog_show] and [method file_dialog_with_options_show]. [b]Windows, macOS, Linux (X11/Wayland)[/b]
</constant>
<constant name="FEATURE_WINDOW_DRAG" value="27" enum="Feature">
The display server supports initiating window drag operation on demand. See [method window_start_drag].
</constant>
<constant name="MOUSE_MODE_VISIBLE" value="0" enum="MouseMode">
Makes the mouse cursor visible if it is hidden.
</constant>
Expand Down
8 changes: 6 additions & 2 deletions editor/gui/editor_title_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ void EditorTitleBar::gui_input(const Ref<InputEvent> &p_event) {
if (w) {
if (mb->get_button_index() == MouseButton::LEFT) {
if (mb->is_pressed()) {
click_pos = DisplayServer::get_singleton()->mouse_get_position() - w->get_position();
moving = true;
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_WINDOW_DRAG)) {
DisplayServer::get_singleton()->window_start_drag(w->get_window_id());
} else {
click_pos = DisplayServer::get_singleton()->mouse_get_position() - w->get_position();
moving = true;
}
} else {
moving = false;
}
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 @@ -402,6 +402,8 @@ class DisplayServerMacOS : public DisplayServer {
virtual bool window_maximize_on_title_dbl_click() const override;
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_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
11 changes: 11 additions & 0 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,7 @@
case FEATURE_SCREEN_CAPTURE:
case FEATURE_STATUS_INDICATOR:
case FEATURE_NATIVE_HELP:
case FEATURE_WINDOW_DRAG:
return true;
default: {
}
Expand Down Expand Up @@ -2342,6 +2343,16 @@
return false;
}

void DisplayServerMacOS::window_start_drag(WindowID p_window) {
_THREAD_SAFE_METHOD_

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

NSEvent *event = [NSEvent mouseEventWithType:NSEventTypeLeftMouseDown location:((NSWindow *)wd.window_object).mouseLocationOutsideOfEventStream modifierFlags:0 timestamp:[[NSProcessInfo processInfo] systemUptime] windowNumber:((NSWindow *)wd.window_object).windowNumber context:nil eventNumber:0 clickCount:1 pressure:1.0f];
[wd.window_object performWindowDragWithEvent:event];
}

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

Expand Down
3 changes: 3 additions & 0 deletions servers/display_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,8 @@ void DisplayServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("window_maximize_on_title_dbl_click"), &DisplayServer::window_maximize_on_title_dbl_click);
ClassDB::bind_method(D_METHOD("window_minimize_on_title_dbl_click"), &DisplayServer::window_minimize_on_title_dbl_click);

ClassDB::bind_method(D_METHOD("window_start_drag", "window_id"), &DisplayServer::window_start_drag, DEFVAL(MAIN_WINDOW_ID));

ClassDB::bind_method(D_METHOD("ime_get_selection"), &DisplayServer::ime_get_selection);
ClassDB::bind_method(D_METHOD("ime_get_text"), &DisplayServer::ime_get_text);

Expand Down Expand Up @@ -1057,6 +1059,7 @@ void DisplayServer::_bind_methods() {
BIND_ENUM_CONSTANT(FEATURE_NATIVE_DIALOG_INPUT);
BIND_ENUM_CONSTANT(FEATURE_NATIVE_DIALOG_FILE);
BIND_ENUM_CONSTANT(FEATURE_NATIVE_DIALOG_FILE_EXTRA);
BIND_ENUM_CONSTANT(FEATURE_WINDOW_DRAG);

BIND_ENUM_CONSTANT(MOUSE_MODE_VISIBLE);
BIND_ENUM_CONSTANT(MOUSE_MODE_HIDDEN);
Expand Down
3 changes: 3 additions & 0 deletions servers/display_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class DisplayServer : public Object {
FEATURE_NATIVE_DIALOG_INPUT,
FEATURE_NATIVE_DIALOG_FILE,
FEATURE_NATIVE_DIALOG_FILE_EXTRA,
FEATURE_WINDOW_DRAG,
};

virtual bool has_feature(Feature p_feature) const = 0;
Expand Down Expand Up @@ -490,6 +491,8 @@ class DisplayServer : public Object {
virtual bool window_maximize_on_title_dbl_click() const { return false; }
virtual bool window_minimize_on_title_dbl_click() const { return false; }

virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) {}

// necessary for GL focus, may be able to use one of the existing functions for this, not sure yet
virtual void gl_window_make_current(DisplayServer::WindowID p_window_id);

Expand Down

0 comments on commit bb09a6f

Please sign in to comment.