feat: Support Copy/Paste on WASM build outside the application border (#1542)

### Problem description
WASM build does not support copy/paste beyond the application. Meaning,
there's no practical way of sending text back and forth across the
application border.

There are lengthy threads why this is a technical challenge in
WASM/Browser world, e.g:
- https://github.com/pthom/hello_imgui/issues/3
- https://github.com/emscripten-core/emscripten/pull/19510

### Implementation description
Implements a workaround solution as Header only C++ library, as proposed
and implemented at:
https://github.com/Armchair-Software/emscripten-browser-clipboard

Maybe there are cleaner ways of achieving the functionality. Definitely
would like to have some discussion around this. 👀

ℹ️ The proposed PR "works for me" on Windows, using CTRL-C/V shortcuts
to copy text from and to the application. On MacOS the system shortcut
to Paste is different from what ImHex has defined. This results in
system Paste shortcut of command-V triggering the browser callback to
synchronise the application clipboard, but no actual Paste takes place
within ImHex.

If there would be a clean way to trigger the paste command, that would
be wonderful (or get the context and references to write the data to the
cursor, but I was unable to find a clean solution). The only proposed
solutions in the referenced threads were about triggering paste event
internally via Key events. This seemed wonky 🙃 , so is not currently
implemented. At the moment the paste on MacOS is command+V followed by
control+V.

### Additional things
This is definitely a stopgap solution before the ImGui and Emscripten
take a more proper solution in enabling Copy/Paste outside the
application borders. However, I feel like this is a must have capability
to make the WASM build more useful, not just for trying out ImHex.

Cheers! 🍻

---------

Co-authored-by: Nik <werwolv98@gmail.com>
This commit is contained in:
Sten Feldman
2024-02-13 21:30:18 +02:00
committed by GitHub
parent 56b2e09b01
commit d70f7422b7
2 changed files with 125 additions and 0 deletions

View File

@@ -107,6 +107,11 @@
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
// IMHEX PATCH BEGIN
#include <emscripten_browser_clipboard.h>
static std::string clipboardContent;
// IMHEX PATCH END
#endif
// We gather version tests as define in order to easily see which features are version-dependent.
@@ -198,12 +203,24 @@ static void ImGui_ImplGlfw_ShutdownPlatformInterface();
// Functions
static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data)
{
// IMHEX PATCH BEGIN
#ifdef __EMSCRIPTEN__
return clipboardContent.c_str();
#else
return glfwGetClipboardString((GLFWwindow*)user_data);
#endif
// IMHEX PATCH END
}
static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text)
{
glfwSetClipboardString((GLFWwindow*)user_data, text);
// IMHEX PATCH BEGIN
#ifdef __EMSCRIPTEN__
clipboardContent = text;
emscripten_browser_clipboard::copy(clipboardContent);
#endif
// IMHEX PATCH END
}
static ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int key)
@@ -594,6 +611,15 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw
bd->Time = 0.0;
bd->WantUpdateMonitors = true;
// IMHEX PATCH BEGIN
#ifdef __EMSCRIPTEN__
// Callback to handle clipboard paste from browser
emscripten_browser_clipboard::paste([](std::string const &paste_data, void *callback_data [[maybe_unused]]) {
clipboardContent = std::move(paste_data);
});
#endif
// IMHEX PATCH END
io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
io.ClipboardUserData = bd->Window;