Return null from GlfwBackend.GetClipboardText when the clipboard holds no text

glfwGetClipboardString returns NULL when the clipboard cannot be converted to text (GLFW_FORMAT_UNAVAILABLE), and Glfw.Clipboard is declared string? for it. Passing that null to Utf8.GetByteCount threw ArgumentNullException inside the native callback, which ended the process on Ctrl+V. ImGui treats a null clipboard as nothing to paste, as with the C++ imgui_impl_glfw.

Fixes #8

Signed-off-by: Laurens de Voogd <laurens@devoogd.be>
This commit is contained in:
Laurens de Voogd 2026-10-15 09:59:41 +02:00
commit 66b9705837

View file

@ -277,6 +277,9 @@ public partial class GlfwBackend : IPlatformBackend
private unsafe byte* GetClipboardText(ImGuiContextPtr context)
{
var str = Glfw.Clipboard;
if (str is null)
return null;
var byteCount = Utf8.GetByteCount(str);
_cachedClipboardText.Count = byteCount + 1;
Utf8.FromUtf16(str, _cachedClipboardText.AsSpan(), out _, out _);