ocornut / imgui

Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
MIT License
61.45k stars 10.34k forks source link

How to set the clipboard functions to default ImGui implementation? #7455

Closed paul-akl closed 8 months ago

paul-akl commented 8 months ago

Version/Branch of Dear ImGui:

Version v1.89.9, Branch: docking

Back-ends:

imgui_impl_opengl3.cpp + imgui_impl_sdl2.cpp

Compiler, OS:

Windows 10 + MSVC 2022

Full config/build information:

No response

Details:

Question: how to manually set the default implementation for clipboard functions?

My issue: application freezes for a short time when using CTRL+C. SDL2 implementation of ImGui uses _SDL_SetClipboardText as a io.SetClipboardTextFn_ function for setting the clipboard text. However, when multithreading and running ImGui calls on a different thread (non-main thread that created the SDL context), setting the clipboard (i.e. CTRL+C) freezes for around 10 seconds.

Disabling multithreading or commenting out this code in _imgui_impl_sdl2.cpp_ fixes the issue:

//io.SetClipboardTextFn = ImGui_ImplSDL2_SetClipboardText;
//io.GetClipboardTextFn = ImGui_ImplSDL2_GetClipboardText;
//io.ClipboardUserData = nullptr;

What would be the best way to manually set the clipboard functions to default implementation, i.e.:

g.IO.GetClipboardTextFn = GetClipboardTextFn_DefaultImpl;    // Platform dependent default implementations
g.IO.SetClipboardTextFn = SetClipboardTextFn_DefaultImpl;
g.IO.ClipboardUserData = (void*)&g;

without editing any of the ImGui implementation files. The _XXXClipboardTextFnDefaultImpl functions are declared in the imgui.cpp files, so I cannot see a simple way of accessing them.

By the way, if it's of any interest to anyone, the application hangs at this call, in _SDLwindowsclipboard.c file:

int WIN_SetClipboardText(_THIS, const char *text)
{
// ...
EmptyClipboard(); // <- hangs here
// ...
}

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

PathogenDavid commented 8 months ago

The easiest route would be to save and restore the clipboard functions before/after the backend initialization. IE:

auto defaultSetClipboardTextFn = io.SetClipboardTextFn;
auto defaultGetClipboardTextFn = io.GetClipboardTextFn;
auto defaultClipboardUserData = io.ClipboardUserData;
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
io.SetClipboardTextFn = defaultSetClipboardTextFn;
io.GetClipboardTextFn = defaultGetClipboardTextFn;
io.ClipboardUserData = defaultClipboardUserData;
paul-akl commented 8 months ago

The easiest route would be to save and restore the clipboard functions before/after the backend initialization. IE:

auto defaultSetClipboardTextFn = io.SetClipboardTextFn;
auto defaultGetClipboardTextFn = io.GetClipboardTextFn;
ImGui_ImplSDL2_InitForOpenGL(window, gl_context);
io.SetClipboardTextFn = defaultSetClipboardTextFn;
io.GetClipboardTextFn = defaultGetClipboardTextFn;

That is actually a clever solution, how didn't I think of that. Thank you!

I'll keep the issue open for a bit, in case someone comments with a direct way to get the default clipboard implementation.

EDIT: For this solution to work correctly, one needs to set the ClipboardUserData as well, since it is set to nullptr in the SDL2 implementation, thus ImGui throws an exception when reading the clipboard by trying to clear the nullptr.

Amended solution that works for me:

auto defaultSetClipboardTextFn = io.SetClipboardTextFn;
auto defaultGetClipboardTextFn = io.GetClipboardTextFn;
auto defaultClipboardUserData = io.ClipboardUserData;

ImGui_ImplSDL2_InitForOpenGL(window, gl_context);

io.SetClipboardTextFn = defaultSetClipboardTextFn;
io.GetClipboardTextFn = defaultGetClipboardTextFn;
io.ClipboardUserData = defaultClipboardUserData;
PathogenDavid commented 8 months ago

ClipboardUserData

Oops yeah, that's my bad. Sorry about that! 😅

ocornut commented 8 months ago

Also make sure that what you perceive as hanging isn’t some kind of symbol loading performed by the debugger (in which case you may set it up to lazily load symbols on crash only).

paul-akl commented 8 months ago

Also make sure that what you perceive as hanging isn’t some kind of symbol loading performed by the debugger (in which case you may set it up to lazily load symbols on crash only).

Thanks for your input, ocornut!

It wasn't a debuggers fault, as the same hanging is happening when manually running the binary compiled in release mode. In addition, the freezing happens every time the clipboard is accessed, not just the first time after running the program.

Assuming there is no intended way to explicitly access the default clipboard implementation and there is a work-around that I'm happy with, I'll close the issue as resolved 👍