Emscripten: Handling "external" clipboard #7851

Closed
opened 2024-08-01 18:08:24 +00:00 by ypujante · 0 comments
ypujante commented 2024-08-01 18:08:24 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:

Version 1.91.0

Back-ends:

imgui_impl_glfw.cpp

Compiler, OS:

emscripten / browser

Full config/build information:

No response

Details:

With the release of ImGui 1.91.0 and the support for emscripten-glfw, as you know, the clipboard is now supported in the browser environment. For example, you can now copy the build configuration (which can try for yourself with the demo I pushed with the PR)

If you copy to the clipboard by calling glfwSetClipboardString, then I store the string locally and push it to the external clipboard (which is why you can paste it outside the browser).
If you then paste from the clipboard by calling glfwGetClipboardString, then the clipboard string that was stored previously is returned.

In other words, copy/paste from within the application works great.

The issue comes with pasting from the "external" clipboard: the Javascript API, which is the API that I need to use is asynchronous. For this purpose, in the latest version of emscripten-glfw, I added a new API which reflects this API:

typedef void (* emscripten_glfw_clipboard_string_fun)(void *userData, char const *clipboardString, char const *error);
void emscripten_glfw_get_clipboard_string(emscripten_glfw_clipboard_string_fun callback, void *userData);

I am using this API in my recently released WebGPU Shader Toy tool for the code editor class (TextEditor) because I am in control of this logic: when I detect a "Paste" event (CTRL+V), I call the asynchronous API and when I receive the result, I do the actual Paste action.

Of course this does not work with ImGui::InputText and I wanted to ask you if you could think of a way, or giving me pointers of whether this would be possible to do in ImGui (I tried to look at Shortcut routing but I don't know if that is even the right place to start...). I would be more than happy to do a PR for the work if this is something that is possible at all. Otherwise I could implement an entirely new control for my project.

Thank you

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

### Version/Branch of Dear ImGui: Version 1.91.0 ### Back-ends: imgui_impl_glfw.cpp ### Compiler, OS: emscripten / browser ### Full config/build information: _No response_ ### Details: With the release of ImGui 1.91.0 and the support for `emscripten-glfw`, as you know, the clipboard is now supported in the browser environment. For example, you can now copy the build configuration (which can [try for yourself](https://pongasoft.github.io/imgui/pr-7647/use_port_contrib_glfw3/) with the demo I pushed with the PR) If you copy to the clipboard by calling `glfwSetClipboardString`, then I store the string locally and push it to the external clipboard (which is why you can paste it outside the browser). If you then paste from the clipboard by calling `glfwGetClipboardString`, then the clipboard string that was stored previously is returned. In other words, copy/paste from within the application works great. The issue comes with pasting **from** the "external" clipboard: the Javascript API, which is the API that I need to use is asynchronous. For this purpose, in the latest version of `emscripten-glfw`, I added a new API which reflects this API: ```c typedef void (* emscripten_glfw_clipboard_string_fun)(void *userData, char const *clipboardString, char const *error); void emscripten_glfw_get_clipboard_string(emscripten_glfw_clipboard_string_fun callback, void *userData); ``` I am using this API in my recently released [WebGPU Shader Toy](https://pongasoft.github.io/webgpu-shader-toy/) tool for the code editor class (`TextEditor`) because I am in control of this logic: when I detect a "Paste" event (CTRL+V), I call the asynchronous API and when I receive the result, I do the actual Paste action. Of course this does not work with `ImGui::InputText` and I wanted to ask you if you could think of a way, or giving me pointers of whether this would be possible to do in ImGui (I tried to look at Shortcut routing but I don't know if that is even the right place to start...). I would be more than happy to do a PR for the work if this is something that is possible at all. Otherwise I could implement an entirely new control for my project. Thank you ### Screenshots/Video: _No response_ ### Minimal, Complete and Verifiable Example code: _No response_
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#7851
No description provided.