Skip to content

Commit 5c4f173

Browse files
committed
Merge pull request #110219 from bruvzg/mac_emb_focus_3
[macOS] Make embedded window focus behavior more similar to Windows/X11.
2 parents bbcdc41 + b1e659c commit 5c4f173

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

platform/macos/editor/embedded_process_macos.mm

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
EmbeddedProcessBase() {
193193
layer_host = memnew(LayerHost(this));
194194
add_child(layer_host);
195+
set_focus_mode(FOCUS_NONE);
195196
layer_host->set_focus_mode(FOCUS_ALL);
196197
layer_host->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
197198
layer_host->set_custom_minimum_size(Size2(100, 100));
@@ -219,6 +220,9 @@
219220
if (script_debugger) {
220221
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_MOUSE_ENTER });
221222
}
223+
if (get_window()->has_focus()) {
224+
grab_focus();
225+
}
222226
} break;
223227
case NOTIFICATION_FOCUS_ENTER: {
224228
// Restore mouse capture, if necessary.
@@ -228,9 +232,11 @@
228232
ds->mouse_set_mode(process->get_mouse_mode());
229233
}
230234
if (!window_focused && script_debugger) {
235+
script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_IN });
231236
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_IN });
232237
window_focused = true;
233238
}
239+
process->queue_redraw();
234240
} break;
235241
case NOTIFICATION_MOUSE_EXIT: {
236242
DisplayServer *ds = DisplayServer::get_singleton();
@@ -249,8 +255,10 @@
249255
}
250256
if (window_focused && script_debugger) {
251257
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_OUT });
258+
script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_OUT });
252259
window_focused = false;
253260
}
261+
process->queue_redraw();
254262
} break;
255263
case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
256264
if (script_debugger && has_focus()) {
@@ -268,26 +276,20 @@
268276
}
269277
}
270278
} break;
279+
case NOTIFICATION_APPLICATION_FOCUS_IN:
271280
case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
272-
if (!window_focused && script_debugger) {
281+
if (has_focus() && !window_focused && script_debugger) {
282+
script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_IN });
273283
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_IN });
274284
window_focused = true;
275285
}
276286
} break;
287+
case NOTIFICATION_APPLICATION_FOCUS_OUT:
277288
case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
278-
if (window_focused && script_debugger) {
289+
if (has_focus() && window_focused && script_debugger) {
279290
script_debugger->send_message("embed:win_event", { DisplayServer::WINDOW_EVENT_FOCUS_OUT });
280-
window_focused = false;
281-
}
282-
} break;
283-
case NOTIFICATION_APPLICATION_FOCUS_IN: {
284-
if (script_debugger) {
285-
script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_IN });
286-
}
287-
} break;
288-
case NOTIFICATION_APPLICATION_FOCUS_OUT: {
289-
if (script_debugger) {
290291
script_debugger->send_message("embed:notification", { NOTIFICATION_APPLICATION_FOCUS_OUT });
292+
window_focused = false;
291293
}
292294
} break;
293295
}

0 commit comments

Comments
 (0)