Skip to content

Commit

Permalink
Merge pull request godotengine#80439 from bruvzg/macos_mouse_enter_exit
Browse files Browse the repository at this point in the history
[macOS] Fix missing mouse exit events on window close.
  • Loading branch information
akien-mga committed Aug 9, 2023
2 parents 821579e + 78caaf3 commit 11ea4dc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions platform/macos/display_server_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class DisplayServerMacOS : public DisplayServer {
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;
Expand Down Expand Up @@ -240,6 +241,8 @@ class DisplayServerMacOS : public DisplayServer {
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);
Expand Down
23 changes: 20 additions & 3 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,25 @@
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<InputEvent> &p_event) {
((DisplayServerMacOS *)(get_singleton()))->_dispatch_input_event(p_event);
}
Expand Down Expand Up @@ -2069,9 +2088,7 @@

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) {
Expand Down
6 changes: 2 additions & 4 deletions platform/macos/godot_content_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,8 @@ - (void)mouseExited:(NSEvent *)event {
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);
}
}

Expand All @@ -517,9 +516,8 @@ - (void)mouseEntered:(NSEvent *)event {
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();
Expand Down
1 change: 1 addition & 0 deletions platform/macos/godot_window_delegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ - (void)windowWillClose:(NSNotification *)notification {
ds->window_set_transient(window_id, DisplayServerMacOS::INVALID_WINDOW_ID);
}

ds->mouse_exit_window(window_id);
ds->window_destroy(window_id);
}

Expand Down

0 comments on commit 11ea4dc

Please sign in to comment.