From 7ecc47d12e5f4c322af3d16615a08ba9bc47c4c2 Mon Sep 17 00:00:00 2001 From: Mario Nachbaur Date: Thu, 26 Dec 2024 20:12:53 +0100 Subject: [PATCH] Fix `IsWindowFocused()` on web. (#4640) --- src/platforms/rcore_web.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/platforms/rcore_web.c b/src/platforms/rcore_web.c index d28ed55c2d5d..996e7daf5553 100644 --- a/src/platforms/rcore_web.c +++ b/src/platforms/rcore_web.c @@ -133,6 +133,7 @@ static void CursorEnterCallback(GLFWwindow *window, int enter); static EM_BOOL EmscriptenFullscreenChangeCallback(int eventType, const EmscriptenFullscreenChangeEvent *event, void *userData); // static EM_BOOL EmscriptenWindowResizedCallback(int eventType, const EmscriptenUiEvent *event, void *userData); static EM_BOOL EmscriptenResizeCallback(int eventType, const EmscriptenUiEvent *event, void *userData); +static EM_BOOL EmscriptenFocusCallback(int eventType, const EmscriptenFocusEvent *focusEvent, void *userData); // Emscripten input callback events static EM_BOOL EmscriptenMouseMoveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData); @@ -1369,6 +1370,10 @@ int InitPlatform(void) // Support gamepad events (not provided by GLFW3 on emscripten) emscripten_set_gamepadconnected_callback(NULL, 1, EmscriptenGamepadCallback); emscripten_set_gamepaddisconnected_callback(NULL, 1, EmscriptenGamepadCallback); + + // Support focus events + emscripten_set_blur_callback("#canvas", platform.handle, 1, EmscriptenFocusCallback); + emscripten_set_focus_callback("#canvas", platform.handle, 1, EmscriptenFocusCallback); //---------------------------------------------------------------------------- // Initialize timing system @@ -1732,6 +1737,18 @@ static EM_BOOL EmscriptenGamepadCallback(int eventType, const EmscriptenGamepadE return 1; // The event was consumed by the callback handler } +static EM_BOOL EmscriptenFocusCallback(int eventType, const EmscriptenFocusEvent *focusEvent, void *userData) +{ + EM_BOOL consumed = 1; + switch (eventType) + { + case EMSCRIPTEN_EVENT_BLUR: WindowFocusCallback(userData, 0); break; + case EMSCRIPTEN_EVENT_FOCUS: WindowFocusCallback(userData, 1); break; + default: consumed = 0; break; + } + return consumed; +} + // Register touch input events static EM_BOOL EmscriptenTouchCallback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData) {