Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancelling edit doesn't reset the input buffer #2321

Closed
relick opened this issue Jan 29, 2019 · 4 comments
Closed

Cancelling edit doesn't reset the input buffer #2321

relick opened this issue Jan 29, 2019 · 4 comments

Comments

@relick
Copy link

relick commented Jan 29, 2019

Version/Branch of Dear ImGui:

Version: 1.67 and master
Branch: master

Back-end/Renderer/Compiler/OS

Tested using:
Back-ends: imgui-SFML
Compiler: mingw-w64 gcc 8.2
Operating System: Windows

My Issue/Question:

When typing in an InputText or InputTextMultiline and cancelling the edit (typically through pressing escape), whilst the text box does lose focus as expected, the input text buffer doesn't revert to its original contents. Reverting used to be the behaviour in previous versions but now seems to be broken on both 1.67 and master. Looking at the code it seems this may have just been an oversight when InputText resize callbacks were added.
I was able to fix it locally by changing this line in InputTextEx:

// If the underlying buffer resize was denied or not carried to the next frame, apply_new_text_length+1 may be >= buf_size.
ImStrncpy(buf, edit_state.TempBuffer.Data, ImMin(apply_new_text_length + 1, buf_size));

to

ImStrncpy(buf, apply_new_text, ImMin(apply_new_text_length + 1, buf_size));

If you'd like me to submit a PR for this fix let me know, but I'll wait for now in case there's an aspect to fixing this that I have missed.

@ocornut
Copy link
Owner

ocornut commented Jan 30, 2019

Thank you for finding this @relick.

It somehow escaped naive detection because applying the zero-terminator means that text addition would appear to be successfully cancelled.

"Hello" -> "Hello world" (Escape) -> "Hello" (appears to work!)
But
"Hello world" -> "Hello w" (Escape) -> "Hello world" (fail)

Broken in
24ff259

Fixed now.

However I noticed there is another bug where if you enable Keyboard Navigation, in this situation the Nav code will consume the ESC event earlier and not leave a chance for InputText() to cancel the edit. Will fix this later today/

@ocornut
Copy link
Owner

ocornut commented Jan 30, 2019

(
Bonus:

I also added a basic test for test in my (work in progress) testing framework #435
image

// Insert
strcpy(buf, "Hello");
ctx->ItemClick("InputText");
ctx->KeyCharsInputAppend("World123");
IM_CHECK(strcmp(buf, "HelloWorld123") == 0);

// Delete
ctx->ItemClick("InputText");
ctx->KeyPressMap(ImGuiKey_End);
ctx->KeyPressMap(ImGuiKey_Backspace, 3);
ctx->KeyPressMap(ImGuiKey_Enter);
IM_CHECK(strcmp(buf, "HelloWorld") == 0);

// Insert, Cancel
ctx->ItemClick("InputText");
ctx->KeyPressMap(ImGuiKey_End);
ctx->KeyChars("XXXXX");
ctx->KeyPressMap(ImGuiKey_Escape);
IM_CHECK(strcmp(buf, "HelloWorld") == 0);

// Delete, Cancel
ctx->ItemClick("InputText");
ctx->KeyPressMap(ImGuiKey_End);
ctx->KeyPressMap(ImGuiKey_Backspace, 5);
ctx->KeyPressMap(ImGuiKey_Escape);
IM_CHECK(strcmp(buf, "HelloWorld") == 0);

)

@ocornut ocornut closed this as completed Jan 30, 2019
ocornut added a commit that referenced this issue Jan 30, 2019
@relick
Copy link
Author

relick commented Jan 30, 2019

Awesome, thanks!

ocornut added a commit that referenced this issue Sep 17, 2021
… boxes (fix bd6c9e9). made it possible to activate InputText with tweak gamepad button (why not, now that we can cancel) (#4552, #2321)
@benbrauns
Copy link

However I noticed there is another bug where if you enable Keyboard Navigation, in this situation the Nav code will consume the ESC event earlier and not leave a chance for InputText() to cancel the edit. Will fix this later today/

Is there a way to allow this behavior. I'm trying to prevent the cancel behavior from happening.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants