Skip to content

Commit 6816789

Browse files
ypujanteocornut
authored andcommitted
Backends: GLFW+Emscripten: (Breaking) Renamed ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback() to ImGui_ImplGlfw_InstallEmscriptenCallbacks(), added GLFWwindow* parameter. (#7647, #7600)
+ Fixed Emscripten warning when using mouse wheel on some setups.
1 parent 9504068 commit 6816789

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

backends/imgui_impl_glfw.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
// CHANGELOG
2222
// (minor and older changes stripped away, please see git history for details)
23+
// 2024-07-08: *BREAKING* Renamed ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback to ImGui_ImplGlfw_InstallEmscriptenCallbacks(), added GLFWWindow* parameter.
2324
// 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions.
2425
// 2023-12-19: Emscripten: Added ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback() to register canvas selector and auto-resize GLFW window.
2526
// 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys.
@@ -611,12 +612,6 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw
611612
// Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
612613
if (install_callbacks)
613614
ImGui_ImplGlfw_InstallCallbacks(window);
614-
// Register Emscripten Wheel callback to workaround issue in Emscripten GLFW Emulation (#6096)
615-
// We intentionally do not check 'if (install_callbacks)' here, as some users may set it to false and call GLFW callback themselves.
616-
// FIXME: May break chaining in case user registered their own Emscripten callback?
617-
#ifdef __EMSCRIPTEN__
618-
emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, false, ImGui_ImplEmscripten_WheelCallback);
619-
#endif
620615

621616
// Set platform dependent data in viewport
622617
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
@@ -664,7 +659,8 @@ void ImGui_ImplGlfw_Shutdown()
664659
if (bd->InstalledCallbacks)
665660
ImGui_ImplGlfw_RestoreCallbacks(bd->Window);
666661
#ifdef __EMSCRIPTEN__
667-
emscripten_set_wheel_callback(EMSCRIPTEN_EVENT_TARGET_DOCUMENT, nullptr, false, nullptr);
662+
if (bd->CanvasSelector)
663+
emscripten_set_wheel_callback(bd->CanvasSelector, nullptr, false, nullptr);
668664
#endif
669665

670666
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
@@ -844,7 +840,7 @@ static EM_BOOL ImGui_ImplEmscripten_FullscreenChangeCallback(int event_type, con
844840

845841
// 'canvas_selector' is a CSS selector. The event listener is applied to the first element that matches the query.
846842
// STRING MUST PERSIST FOR THE APPLICATION DURATION. PLEASE USE A STRING LITERAL OR ENSURE POINTER WILL STAY VALID.
847-
void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector)
843+
void ImGui_ImplGlfw_InstallEmscriptenCallbacks(GLFWwindow*, const char* canvas_selector)
848844
{
849845
IM_ASSERT(canvas_selector != nullptr);
850846
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
@@ -856,6 +852,11 @@ void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_sel
856852

857853
// Change the size of the GLFW window according to the size of the canvas
858854
ImGui_ImplGlfw_OnCanvasSizeChange(EMSCRIPTEN_EVENT_RESIZE, {}, bd);
855+
856+
// Register Emscripten Wheel callback to workaround issue in Emscripten GLFW Emulation (#6096)
857+
// We intentionally do not check 'if (install_callbacks)' here, as some users may set it to false and call GLFW callback themselves.
858+
// FIXME: May break chaining in case user registered their own Emscripten callback?
859+
emscripten_set_wheel_callback(bd->CanvasSelector, nullptr, false, ImGui_ImplEmscripten_WheelCallback);
859860
}
860861
#endif
861862

backends/imgui_impl_glfw.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@ IMGUI_IMPL_API bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool ins
3030
IMGUI_IMPL_API void ImGui_ImplGlfw_Shutdown();
3131
IMGUI_IMPL_API void ImGui_ImplGlfw_NewFrame();
3232

33-
// Emscripten related initialization phase methods
33+
// Emscripten related initialization phase methods (call after ImGui_ImplGlfw_InitForOpenGL)
3434
#ifdef __EMSCRIPTEN__
35-
IMGUI_IMPL_API void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector);
35+
IMGUI_IMPL_API void ImGui_ImplGlfw_InstallEmscriptenCallbacks(GLFWwindow* window, const char* canvas_selector);
36+
//static inline void ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback(const char* canvas_selector) { ImGui_ImplGlfw_InstallEmscriptenCallbacks(nullptr, canvas_selector); } } // Renamed in 1.91.0
3637
#endif
3738

3839
// GLFW callbacks install

docs/CHANGELOG.txt

+4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ Breaking changes:
4949
- Commented out obsolete ImGuiModFlags (renamed to ImGuiKeyChord in 1.89). (#4921, #456)
5050
- Commented out obsolete ImGuiModFlags_XXX values (renamed to ImGuiMod_XXX in 1.89). (#4921, #456)
5151
- ImGuiModFlags_Ctrl -> ImGuiMod_Ctrl, ImGuiModFlags_Shift -> ImGuiMod_Shift etc.
52+
- Backends: GLFW+Emscripten: Renamed ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback() to
53+
ImGui_ImplGlfw_InstallEmscriptenCallbacks(), with additional GLFWWindow* parameter. (#7647) [@ypujante]
5254

5355
Other changes:
5456

@@ -66,6 +68,8 @@ Other changes:
6668
- Backends: SDL2,SDL3,OSX: Update for io.SetPlatformImeDataFn() -> io.PlatformSetImeDataFn() rename.
6769
- Backends: GLFW,SDL2: Added io.PlatformOpenInShellFn handler for web/Emscripten versions. (#7660)
6870
[@ypujante, @ocornut]
71+
- Backends: GLFW+Emscripten: Fixed Emscripten warning when using mouse wheel on some setups
72+
"Unable to preventDefault inside passive event listener". (#7647, #7600) [@ypujante]
6973

7074

7175
-----------------------------------------------------------------------

examples/example_glfw_opengl3/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int main(int, char**)
8585
// Setup Platform/Renderer backends
8686
ImGui_ImplGlfw_InitForOpenGL(window, true);
8787
#ifdef __EMSCRIPTEN__
88-
ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback("#canvas");
88+
ImGui_ImplGlfw_InstallEmscriptenCallbacks(window, "#canvas");
8989
#endif
9090
ImGui_ImplOpenGL3_Init(glsl_version);
9191

examples/example_glfw_wgpu/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ int main(int, char**)
101101
// Setup Platform/Renderer backends
102102
ImGui_ImplGlfw_InitForOther(window, true);
103103
#ifdef __EMSCRIPTEN__
104-
ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback("#canvas");
104+
ImGui_ImplGlfw_InstallEmscriptenCallbacks(window, "#canvas");
105105
#endif
106106
ImGui_ImplWGPU_InitInfo init_info;
107107
init_info.Device = wgpu_device;

0 commit comments

Comments
 (0)