mirror of
https://github.com/Drezil/imgui.git
synced 2025-07-06 04:58:47 +02:00
InputText: Fix using a combination of _CallbackResize + _EnterReturnsTrue + lack of persisting user storage. (#3009)
Amend 24ff25981
(#2006, #1443, #1008)
This commit is contained in:
@ -3857,7 +3857,8 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
|
||||
}
|
||||
|
||||
// When using 'ImGuiInputTextFlags_EnterReturnsTrue' as a special case we reapply the live buffer back to the input buffer before clearing ActiveId, even though strictly speaking it wasn't modified on this frame.
|
||||
// If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail. Also this allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage.
|
||||
// If we didn't do that, code like InputInt() with ImGuiInputTextFlags_EnterReturnsTrue would fail.
|
||||
// This also allows the user to use InputText() with ImGuiInputTextFlags_EnterReturnsTrue without maintaining any user-side storage (please note that if you use this property along ImGuiInputTextFlags_CallbackResize you can end up with your temporary string object unnecessarily allocating once a frame, either store your string data, either if you don't then don't use ImGuiInputTextFlags_CallbackResize).
|
||||
bool apply_edit_back_to_user_buffer = !cancel_edit || (enter_pressed && (flags & ImGuiInputTextFlags_EnterReturnsTrue) != 0);
|
||||
if (apply_edit_back_to_user_buffer)
|
||||
{
|
||||
@ -3951,8 +3952,11 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
|
||||
// Copy result to user buffer
|
||||
if (apply_new_text)
|
||||
{
|
||||
// We cannot test for 'backup_current_text_length != apply_new_text_length' here because we have no guarantee that the size
|
||||
// of our owned buffer matches the size of the string object held by the user, and by design we allow InputText() to be used
|
||||
// without any storage on user's side.
|
||||
IM_ASSERT(apply_new_text_length >= 0);
|
||||
if (backup_current_text_length != apply_new_text_length && is_resizable)
|
||||
if (is_resizable)
|
||||
{
|
||||
ImGuiInputTextCallbackData callback_data;
|
||||
callback_data.EventFlag = ImGuiInputTextFlags_CallbackResize;
|
||||
@ -3967,6 +3971,7 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
|
||||
apply_new_text_length = ImMin(callback_data.BufTextLen, buf_size - 1);
|
||||
IM_ASSERT(apply_new_text_length <= buf_size);
|
||||
}
|
||||
//IMGUI_DEBUG_LOG("InputText(\"%s\"): apply_new_text length %d\n", label, apply_new_text_length);
|
||||
|
||||
// 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, apply_new_text, ImMin(apply_new_text_length + 1, buf_size));
|
||||
|
Reference in New Issue
Block a user