From 78caaf37171437c5d03f793c6a590f5b6ae0a2b9 Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Wed, 9 Aug 2023 10:23:57 +0300 Subject: [PATCH] [macOS] Fix missing mouse exit events on window close. --- platform/macos/display_server_macos.h | 3 +++ platform/macos/display_server_macos.mm | 23 ++++++++++++++++++++--- platform/macos/godot_content_view.mm | 6 ++---- platform/macos/godot_window_delegate.mm | 1 + 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/platform/macos/display_server_macos.h b/platform/macos/display_server_macos.h index e5e0e53bfb9..69f60080435 100644 --- a/platform/macos/display_server_macos.h +++ b/platform/macos/display_server_macos.h @@ -171,6 +171,7 @@ private: int current_layout = 0; bool keyboard_layout_dirty = true; + WindowID window_mouseover_id = INVALID_WINDOW_ID; WindowID last_focused_window = INVALID_WINDOW_ID; WindowID window_id_counter = MAIN_WINDOW_ID; float display_max_scale = 1.f; @@ -240,6 +241,8 @@ public: bool get_is_resizing() const; void reparent_check(WindowID p_window); WindowID _get_focused_window_or_popup() const; + void mouse_enter_window(WindowID p_window); + void mouse_exit_window(WindowID p_window); void window_update(WindowID p_window); void window_destroy(WindowID p_window); diff --git a/platform/macos/display_server_macos.mm b/platform/macos/display_server_macos.mm index de191827f58..e79d6acc3f8 100644 --- a/platform/macos/display_server_macos.mm +++ b/platform/macos/display_server_macos.mm @@ -366,6 +366,25 @@ DisplayServer::WindowID DisplayServerMacOS::_get_focused_window_or_popup() const return last_focused_window; } +void DisplayServerMacOS::mouse_enter_window(WindowID p_window) { + if (window_mouseover_id != p_window) { + if (window_mouseover_id != INVALID_WINDOW_ID) { + send_window_event(windows[window_mouseover_id], WINDOW_EVENT_MOUSE_EXIT); + } + window_mouseover_id = p_window; + if (p_window != INVALID_WINDOW_ID) { + send_window_event(windows[p_window], WINDOW_EVENT_MOUSE_ENTER); + } + } +} + +void DisplayServerMacOS::mouse_exit_window(WindowID p_window) { + if (window_mouseover_id == p_window && p_window != INVALID_WINDOW_ID) { + send_window_event(windows[p_window], WINDOW_EVENT_MOUSE_EXIT); + } + window_mouseover_id = INVALID_WINDOW_ID; +} + void DisplayServerMacOS::_dispatch_input_events(const Ref &p_event) { ((DisplayServerMacOS *)(get_singleton()))->_dispatch_input_event(p_event); } @@ -2069,9 +2088,7 @@ void DisplayServerMacOS::mouse_set_mode(MouseMode p_mode) { if (show_cursor && !previously_shown) { window_id = get_window_at_screen_position(mouse_get_position()); - if (window_id != INVALID_WINDOW_ID) { - send_window_event(windows[window_id], WINDOW_EVENT_MOUSE_ENTER); - } + mouse_enter_window(window_id); } if (p_mode == MOUSE_MODE_CAPTURED) { diff --git a/platform/macos/godot_content_view.mm b/platform/macos/godot_content_view.mm index 3467bf90a1a..231be83a032 100644 --- a/platform/macos/godot_content_view.mm +++ b/platform/macos/godot_content_view.mm @@ -505,9 +505,8 @@ return; } - DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_CAPTURED) { - ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_MOUSE_EXIT); + ds->mouse_exit_window(window_id); } } @@ -517,9 +516,8 @@ return; } - DisplayServerMacOS::WindowData &wd = ds->get_window(window_id); if (ds->mouse_get_mode() != DisplayServer::MOUSE_MODE_CAPTURED) { - ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_MOUSE_ENTER); + ds->mouse_enter_window(window_id); } ds->cursor_update_shape(); diff --git a/platform/macos/godot_window_delegate.mm b/platform/macos/godot_window_delegate.mm index 1c6dbb1981a..46355b4ae8f 100644 --- a/platform/macos/godot_window_delegate.mm +++ b/platform/macos/godot_window_delegate.mm @@ -67,6 +67,7 @@ ds->window_set_transient(window_id, DisplayServerMacOS::INVALID_WINDOW_ID); } + ds->mouse_exit_window(window_id); ds->window_destroy(window_id); }