InputText: do not require anymore that CursorPos be clamped by user code. (#9029)

Add clamping outside of callback code + simplify logic. The previous logic checking for difference was because old code e.g. 21d03edcb0 required a ImTextCountCharsFromUtf8() which is not required since #7925.
This commit is contained in:
ocornut
2025-10-23 18:54:32 +02:00
parent e571ccf3f4
commit 750c5d2a61
2 changed files with 8 additions and 5 deletions

View File

@@ -49,6 +49,8 @@ Other Changes:
EndTable() was mistakenly restoring a wrong current table. EndTable() was mistakenly restoring a wrong current table.
- InputText: avoid continuously overwriting ownership of ImGuiKey_Enter/_KeypadEnter - InputText: avoid continuously overwriting ownership of ImGuiKey_Enter/_KeypadEnter
keys in order to allow e.g. external Shortcut override behavior. (#9004) keys in order to allow e.g. external Shortcut override behavior. (#9004)
- InputText: when using a callback to reduce/manipulate the value of BufTextLen,
we do not require anymore that CursorPos be clamped by user code. (#9029)
- InputTextMultiline: fixed a crash when using ImGuiInputTextFlags_WordWrap and - InputTextMultiline: fixed a crash when using ImGuiInputTextFlags_WordWrap and
resizing the parent window while keeping the multi-line field active (which is resizing the parent window while keeping the multi-line field active (which is
most typically achieved when resizing programmatically or via a docking layout most typically achieved when resizing programmatically or via a docking layout

View File

@@ -5274,11 +5274,12 @@ bool ImGui::InputTextEx(const char* label, const char* hint, char* buf, int buf_
IM_ASSERT(callback_data.Buf == callback_buf); // Invalid to modify those fields IM_ASSERT(callback_data.Buf == callback_buf); // Invalid to modify those fields
IM_ASSERT(callback_data.BufSize == state->BufCapacity); IM_ASSERT(callback_data.BufSize == state->BufCapacity);
IM_ASSERT(callback_data.Flags == flags); IM_ASSERT(callback_data.Flags == flags);
const bool buf_dirty = callback_data.BufDirty; if (callback_data.BufDirty || callback_data.CursorPos != state->Stb->cursor)
if (buf_dirty || callback_data.CursorPos != state->Stb->cursor) { state->Stb->cursor = callback_data.CursorPos; state->CursorFollow = true; } state->CursorFollow = true;
if (buf_dirty || callback_data.SelectionStart != state->Stb->select_start) { state->Stb->select_start = (callback_data.SelectionStart == callback_data.CursorPos) ? state->Stb->cursor : callback_data.SelectionStart; } state->Stb->cursor = ImClamp(callback_data.CursorPos, 0, callback_data.BufTextLen);
if (buf_dirty || callback_data.SelectionEnd != state->Stb->select_end) { state->Stb->select_end = (callback_data.SelectionEnd == callback_data.SelectionStart) ? state->Stb->select_start : callback_data.SelectionEnd; } state->Stb->select_start = ImClamp(callback_data.SelectionStart, 0, callback_data.BufTextLen);
if (buf_dirty) state->Stb->select_end = ImClamp(callback_data.SelectionEnd, 0, callback_data.BufTextLen);
if (callback_data.BufDirty)
{ {
// Callback may update buffer and thus set buf_dirty even in read-only mode. // Callback may update buffer and thus set buf_dirty even in read-only mode.
IM_ASSERT(callback_data.BufTextLen == (int)ImStrlen(callback_data.Buf)); // You need to maintain BufTextLen if you change the text! IM_ASSERT(callback_data.BufTextLen == (int)ImStrlen(callback_data.Buf)); // You need to maintain BufTextLen if you change the text!