imgui icon indicating copy to clipboard operation
imgui copied to clipboard

InputText CTRL+V paste fails if text is too long

Open Infiziert90 opened this issue 1 year ago • 4 comments

Version/Branch of Dear ImGui:

1.90.2 and before

Back-ends:

Win32 DirectX11

Compiler, OS:

MSVC, Windows 10

Full config/build information:

No response

Details:

Trying to paste a text with < 500 characters into a InputText(label, string, 500) will straight up fail, resulting in nothing be added.

The behaviour of other programs is more of a cut-off at exactly 500 letters, and i wanted to recreate this. Is it possible to achieve something similar with ImGui?

So:

  • User tries to paste
  • Check text for length
  • Cut if above X
  • Paste into the actual InputText

Looked over the Callback events, there doesn't seem to be one handled through the InputText directly

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

Infiziert90 avatar May 16 '24 13:05 Infiziert90

The behaviour of other programs is more of a cut-off at exactly 500 letters

I don't think many other programs have limitations? More often a real application would wrap InputText() to use std::string or their own string type, as shown e.g. https://github.com/ocornut/imgui/blob/master/misc/cpp/imgui_stdlib.h Are you sure you simply don't want to do that?

Or you want to actually benefit from the clamped pasting?

ocornut avatar May 16 '24 14:05 ocornut

My case requires a hard limit of 500 characters, so for me clamping is the bigger benefit

User can still paste their text without the text box just staying blank the moment it goes above 500

Infiziert90 avatar May 16 '24 14:05 Infiziert90

The ideal fix would requires reworking how calls to STB_TEXTEDIT_INSERTCHARS() are handled:

  • main meaningful one in stb_textedit_paste_internal() in imstb_textedit.h.
  • one in stb_textedit_replace() in imgui_widgets.cpp which is in theory cannot fail in normal condition (it would fail if user change underlying static buffer between activation and revert). But it probably means breaking stb_textedit.h API.

Another strategy which seems simpler if we perform a clamped call to stb_textedit_paste() directly in our handler, and only in the case where the buffer is not resizable.

ocornut avatar May 16 '24 14:05 ocornut

One thing that makes me uneasy about clamping the pasted contents is if you paste from beginning or middle of an existing thing, then your pasted content would be cut off but the cut off is likely to be less visible.

ocornut avatar May 16 '24 14:05 ocornut