Skip to content

Deactivating InputTextMultiline by activating previous widget (draworder) icw IsItemDeactivatedAfterEdit causes changes to be lost (not applied) #9308

Closed

Description

@geertbleyen

Version/Branch of Dear ImGui:

Version 1.92.6, Branch: docking (master/docking/etc.)

Back-ends:

imgui_impl_SDL3.cpp + imgui_impl_metal.cpp (don't think backends have an impact here)

Compiler, OS:

macOS + Apple clang version 17.0.0 (clang-1700.3.19.1

Full config/build information:

Dear ImGui 1.92.6 (19261)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201103
define: __APPLE__
define: __GNUC__=4
define: __clang_version__=17.0.0 (clang-1700.3.19.1)
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
IM_ASSERT: runs expression: OK. expand size: OK
--------------------------------
io.BackendPlatformName: imgui_impl_sdl3 (3.4.2; 3.4.2)
io.BackendRendererName: imgui_impl_metal
io.ConfigFlags: 0x00000483
 NavEnableKeyboard
 NavEnableGamepad
 DockingEnable
 ViewportsEnable
io.ConfigViewportsNoDecoration
io.ConfigViewportsNoDefaultParent
io.ConfigMacOSXBehaviors
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00002C1E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasParentViewport
 RendererHasVtxOffset
 RendererHasTextures
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128
io.Fonts->FontLoaderName: stb_truetype
io.DisplaySize: 1280.00,800.00
io.DisplayFramebufferScale: 2.00,2.00
--------------------------------
style.WindowPadding: 8.00,8.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,3.00
style.FrameRounding: 0.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 8.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

We've noticed a rather strange behavior when a multiline InputText (ImGui::InputTextMultiline) gets deactivate by activating (clicking) an input that was defined earlier in the frame, causes the changes to be lost when used in combination with IsItemDeactivatedAfterEdit.

I've made some minor changes to imgui_demo.cpp to replicate the issue.
imgui-imgui_demo.patch
The changes are:

  • use imgui_stdlib to work with std::string as buffer for InputTextMultiline
  • replaced the InputText widgets in the 'Filtered Text Input' section, to make replication our use-case more easily
  • only apply persistent change to static stored string via IsItemDeactivatedAfterEdit() check
  • updated makefile to include imgui_stdlib

I'm not 100% confident, but when the InputTextMultiline widget gets deactivated, its own internal IsItemDeactivatedAfterEdit() check fails and the apply_new_text variable isn't set to g.InputTextDeactivatedState.TextA.Data, causing the IsItemDeactivatedAfterEdit() on the outside to trigger, but the value contains the unmodified string because the result isn't copied to the user buffer.
On why sItemDeactivatedAfterEdit() returns false, it seems that IsItemDeactivated() returns false because g.DeactivatedItemData.ID == g.LastItemData.ID is false. Some more digging leads me to believe that this is false because ImGui::InputTextEx adds a Dummy widget when it is set to multline, which changes g.LastItemData.ID (but I must admit it's all rather difficult to follow for me).

Using Ctrl+Enter to apply the text changes does work as expected, or deactivating without activating some other item, does seem to work as expected. The issue only occurs by deactivating the multiline InputText by activating another item.

So the 'position' in the draworder of the activated item matters to trigger this bug or not.

I've made some screencaptures to demonstrate the problem. But if more information is needed I will gladly provide it.

Screenshots/Video:

Screen.Recording.2026-03-18.at.15.30.04.mov

Minimal, Complete and Verifiable Example code:

// Here's some code anyone can copy and paste to reproduce your issue

// when deactivating InputTextMultiline 'default' by clicking on InputTextMultiline 'decimar' will cause IsItemDeactivatedAfterEdit() true, and buff1 will what the user typed.
static std::string sbuff1 = "";
std::string buff1 = sbuff1;
ImGui::InputTextMultiline("default", &buff1);
if (ImGui::IsItemDeactivatedAfterEdit())
  sbuff1 = buff1; // Copy back modified buffer to our std::string after InputText() is done with it.

// when deactivating InputTextMultiline 'decimal' by clicking on InputTextMultiline 'default' will cause IsItemDeactivatedAfterEdit() true, but buff2 will be "" instead of what the user typed.
static std::string sbuff2 = "";
std::string buff2 = sbuff2;
ImGui::InputTextMultiline("decimal", &buff2, ImVec2(0, 0));
if (ImGui::IsItemDeactivatedAfterEdit())
  sbuff2 = buff2;

Activity

  1. added this to the v1.93 milestone on Mar 18, 2026
  2. ocornut commented on Mar 18, 2026

    @ocornut
    Owner

    I'm currently working on #701 since its absence is at the root of all those problems, but will investigate this separately.

    Also note that using Tab/Shift+Tab leads to the same issue.

  3. ocornut commented on Mar 18, 2026

    @ocornut
    Owner

    To clarify, this is not related to the use of std::string or imgui_stdlib.h, but rather related to the fact that no underlying buffer is preserved by the user and InputText() to user buffer write-back is not applied during the IsItemDeactivatedAfterEdit() frame.

    // when deactivating InputTextMultiline 'default' by clicking on InputTextMultiline 'decimar' will cause IsItemDeactivatedAfterEdit() true, and buff1 will what the user typed.
    {
        static std::string sbuff1 = "";
        char buff1[128];
        strcpy_s(buff1, sbuff1.c_str());
        bool ret = ImGui::InputTextMultiline("default", buff1, 128);
    
        bool deactivated = ImGui::IsItemDeactivated();
        bool deactivated_after_edit = ImGui::IsItemDeactivatedAfterEdit();
        if (ret || deactivated || deactivated_after_edit)
            IMGUI_DEBUG_LOG("InputTextMultiline1: ret %d, deac %d,%d, buf = '%s', user = '%s'\n", ret, deactivated, deactivated_after_edit, buff1, sbuff1.c_str());
        if (ImGui::IsItemDeactivatedAfterEdit())
            sbuff1 = buff1; // Copy back modified buffer to our std::string after InputText() is done with it.
    }
    
    // when deactivating InputTextMultiline 'decimal' by clicking on InputTextMultiline 'default' will cause IsItemDeactivatedAfterEdit() true, but buff2 will be "" instead of what the user typed.
    {
        static std::string sbuff2 = "";
        char buff2[128];
        strcpy_s(buff2, sbuff2.c_str());
        bool ret = ImGui::InputTextMultiline("decimal", buff2, 128);
        bool deactivated = ImGui::IsItemDeactivated();
        bool deactivated_after_edit = ImGui::IsItemDeactivatedAfterEdit();
        if (ret || deactivated || deactivated_after_edit)
            IMGUI_DEBUG_LOG("InputTextMultiline2: ret %d, deac %d,%d, buf = '%s', user = '%s'\n", ret, deactivated, deactivated_after_edit, buff2, sbuff2.c_str());
        if (ImGui::IsItemDeactivatedAfterEdit())
            sbuff2 = buff2;
    }
    [00671] InputTextMultiline1: ret 1, deac 0,0, buf = 'hello', user = ''
    TAB
    [00672] InputTextMultiline1: ret 0, deac 1,1, buf = '', user = ''
    
    [01990] InputTextMultiline2: ret 1, deac 0,0, buf = 'world', user = ''
    click away
    [01991] InputTextMultiline2: ret 0, deac 1,1, buf = '', user = ''
    
  4. geertbleyen commented on Mar 19, 2026

    @geertbleyen
    ContributorAuthor

    I didn't intend to suggest using std::string or imgui_stdlib was contributing to the observed issue. It was just easier to create a condensed example of the issue as I don't copy char buffers :).
    But thanks for already looking into it

  5. ocornut commented on Mar 19, 2026

    @ocornut
    Owner

    Linking to #8915, #8273 for related issues previously fixed for InputText() without multi-line.

    I suspect this idiom of not persisting the underlying never really worked for multi-line (for various reasons). I'm working on it.

  6. ocornut commented on Mar 20, 2026

    @ocornut
    Owner

    I have pushed 2d95715 + 04dfcd8 + 2315b9f related to this, the last one being actual fix for this issue but two previous commits are required for it for work.

  7. geertbleyen commented on Mar 20, 2026

    @geertbleyen
    ContributorAuthor

    Awesome, thanks! This proved to be well outside my level of understanding of the inner workings of imgui :).

  8. ocornut commented on Mar 20, 2026

    @ocornut
    Owner

    Awesome, thanks! This proved to be well outside my level of understanding of the inner workings of imgui :).

    InputTextMultiline() is doing a few odd stuff which are really not pretty to look at.
    I've been considering removing the child window at some point, it might simplify a few things.

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址