From ce5f7ab951b66f7ed2e90f553a8f6eec0efa514b Mon Sep 17 00:00:00 2001 From: ocornut Date: Thu, 5 Sep 2024 21:28:48 +0200 Subject: [PATCH] InputText: amends: fixes overwrite mode handling. (7925) Note that there is a bug (not new) when undoing restores the cursor at a wrong position. --- imstb_textedit.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/imstb_textedit.h b/imstb_textedit.h index b7a761c85381..a845db2a92c2 100644 --- a/imstb_textedit.h +++ b/imstb_textedit.h @@ -747,8 +747,11 @@ static void stb_textedit_text(IMSTB_TEXTEDIT_STRING* str, STB_TexteditState* sta return; if (state->insert_mode && !STB_TEXT_HAS_SELECTION(state) && state->cursor < STB_TEXTEDIT_STRINGLEN(str)) { - stb_text_makeundo_replace(str, state, state->cursor, 1, 1); - STB_TEXTEDIT_DELETECHARS(str, state->cursor, 1); + // [DEAR IMGUI] This assume that text contains a single character. + // [DEAR IMGUI] FIXME: When undoing the restored cursor position is wrong, but it's not a new bug. + int prev_char_len = IMSTB_TEXTEDIT_GETNEXTCHARINDEX(str, state->cursor) - state->cursor; + stb_text_makeundo_replace(str, state, state->cursor, prev_char_len, text_len); + STB_TEXTEDIT_DELETECHARS(str, state->cursor, prev_char_len); if (STB_TEXTEDIT_INSERTCHARS(str, state->cursor, text, text_len)) { state->cursor += text_len; state->has_preferred_x = 0;