Changed default Windows clipboard handler to expand LF to CRLF to fix clipboard corruption on some systems #4036
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
✨ CRLF: Because Teletype machines are still relevant in 2021 ✨
Background
This PR fixes the clipboard corruption bug described in #4029
In short: For whatever reason, that user's clipboard gets corrupted when copying text containing non-CRLF newlines. Unfortunately, even after configuring my system to use Russian I was unable to reproduce the issue locally so we're not 100% sure what causes this.
Regardless though, the Windows documentation does state that the
CF_UNICODETEXT
clipboard format is expected to use CRLF newlines, so using CRLF newlines is the technically correct thing to do:As a random anecdotal sample: SDL's
WIN_SetClipboardText
function expands LF to CRLF as well.Changes
The changes to the existing implementation are essentially:
The widening is implemented by iterating over the string backwards and copying it to the end of the buffer to avoid the need for allocating an extra buffer. It's also skipped if the copied text did not contain any newlines. (If that chubby for loop grosses you out too much, I can rewrite it to be simpler at the expense of double-allocating the clipboard buffer.)
Performance
Since this bug seems to be extremely niche I took some basic performance metrics to see how the changes affected things. Unsurprisingly, the performance impact is negligible for a function called so rarely (and usually with very little data.) The results on my machine are summarized below:
imgui.cpp
with CRLF line endings (566 KB)imgui.cpp
with LF line endings (555 KB)Since this isn't a proper microbenchmark, those results tend to be pretty noisy. Either way I don't think people are regularly copying the entirety of the complete works of William Shakespeare from a Dear ImGui app, so I don't think the performance regression will hurt anyone 😅
You can find these source of these quick-and-dirty benchmarks in my
GH-4029
branch in theexample_glfw_opengl3
project.GLFW
GLFW's
_glfwPlatformSetClipboardString
implementation also has this issue.As such, I've modified
imgui_impl_glfw.cpp
to use Dear ImGui's built-in clipboard handling when building for Windows. I did this mainly because our GLFW backend appears to prefer keeping backwards compatibility with older versions of GLFW and the GLFW clipboard implementation offers little value over our own.(If I get around to submitting a PR to GLFW we could put it behind a version gate on Windows once it's merged and released.)
Allegro
Allegro's
win_set_clipboard_text
implementation also has this issue. The linked comment suggests they might be resistant to changing this.As such, I disabled the use of Allegro's clipboard in
imgui_impl_allegro5.cpp
on Windows as well.