Can't change underlying number value when InputText has focus #2881

Open
opened 2019-11-05 17:18:31 +00:00 by kudaba · 11 comments
kudaba commented 2019-11-05 17:18:31 +00:00 (Migrated from github.com)

Version: ~1.72b (a116439)
Branch: Docking

Back-ends: imgui_impl_win32.cpp + imgui_impl_dx12.cpp
Operating System: win10

My Issue/Question:

While trying to implement custom interaction with number fields I hit an issue where I need to clear the focus of the control to change the underlying value. To easily repro the issue:

        static float s_value;
        ImGui::InputFloat("Changing Float", &s_value);
        s_value += ImGui::GetIO().DeltaTime;

Causes:

changing_float

Example of controls I was trying to setup:
https://gist.github.com/kudaba/01337c088a0bcef0c2e22a4e846f729c

Version: ~1.72b (a116439) Branch: Docking Back-ends: imgui_impl_win32.cpp + imgui_impl_dx12.cpp Operating System: win10 **My Issue/Question:** While trying to implement custom interaction with number fields I hit an issue where I need to clear the focus of the control to change the underlying value. To easily repro the issue: ```cpp static float s_value; ImGui::InputFloat("Changing Float", &s_value); s_value += ImGui::GetIO().DeltaTime; ``` Causes: ![changing_float](https://user-images.githubusercontent.com/22896740/68229478-35962180-ffac-11e9-8fb5-aa73ceab5857.gif) Example of controls I was trying to setup: https://gist.github.com/kudaba/01337c088a0bcef0c2e22a4e846f729c
ocornut commented 2019-11-05 18:20:36 +00:00 (Migrated from github.com)

Hello,

InputFloat/InputText/etc. need to own the master copy of the data when active for several reasons (not getting into details here but it's not an easy thing to change with the current code).

I don't know what your input scheme is for that widget, but one possible work-around is to set the ImGuiInputTextFlags_ReadOnly flag when using the other form of interaction. This will force InputText to retranslate the source input value into a text buffer (in some edge case this can affect the text cursor).

Hello, `InputFloat`/`InputText`/etc. need to own the master copy of the data when active for several reasons (not getting into details here but it's not an easy thing to change with the current code). I don't know what your input scheme is for that widget, but one possible work-around is to set the `ImGuiInputTextFlags_ReadOnly` flag when using the other form of interaction. This will force InputText to retranslate the source input value into a text buffer (in some edge case this can affect the text cursor).
ocornut commented 2019-11-05 18:20:36 +00:00 (Migrated from github.com)

Hello,

InputFloat/InputText/etc. need to own the master copy of the data when active for several reasons (not getting into details here but it's not an easy thing to change with the current code).

I don't know what your input scheme is for that widget, but one possible work-around is to set the ImGuiInputTextFlags_ReadOnly flag when using the other form of interaction. This will force InputText to retranslate the source input value into a text buffer (in some edge case this can affect the text cursor).

Hello, `InputFloat`/`InputText`/etc. need to own the master copy of the data when active for several reasons (not getting into details here but it's not an easy thing to change with the current code). I don't know what your input scheme is for that widget, but one possible work-around is to set the `ImGuiInputTextFlags_ReadOnly` flag when using the other form of interaction. This will force InputText to retranslate the source input value into a text buffer (in some edge case this can affect the text cursor).
kudaba commented 2019-11-05 18:32:17 +00:00 (Migrated from github.com)

I was using that, but it was a bit weird since I had to set that value before, but I was changing the value after since I need to check hover state, etc. I just tried locally adding a new ImGuiInputTextFlags_Dynamic to re-initialize the control if it changes, but floating point precision is making it a bit unstable. I think I'll just have to add a something to ImGuiInputTextState to force it to reset in a nice way. In my case it's for a custom control so that makes sense. I'm not sure if this is a feature we would want to be publicly facing anyway.

I was using that, but it was a bit weird since I had to set that value before, but I was changing the value after since I need to check hover state, etc. I just tried locally adding a new ImGuiInputTextFlags_Dynamic to re-initialize the control if it changes, but floating point precision is making it a bit unstable. I think I'll just have to add a something to ImGuiInputTextState to force it to reset in a nice way. In my case it's for a custom control so that makes sense. I'm not sure if this is a feature we would want to be publicly facing anyway.
kudaba commented 2019-11-05 18:32:17 +00:00 (Migrated from github.com)

I was using that, but it was a bit weird since I had to set that value before, but I was changing the value after since I need to check hover state, etc. I just tried locally adding a new ImGuiInputTextFlags_Dynamic to re-initialize the control if it changes, but floating point precision is making it a bit unstable. I think I'll just have to add a something to ImGuiInputTextState to force it to reset in a nice way. In my case it's for a custom control so that makes sense. I'm not sure if this is a feature we would want to be publicly facing anyway.

I was using that, but it was a bit weird since I had to set that value before, but I was changing the value after since I need to check hover state, etc. I just tried locally adding a new ImGuiInputTextFlags_Dynamic to re-initialize the control if it changes, but floating point precision is making it a bit unstable. I think I'll just have to add a something to ImGuiInputTextState to force it to reset in a nice way. In my case it's for a custom control so that makes sense. I'm not sure if this is a feature we would want to be publicly facing anyway.
ocornut commented 2019-11-05 18:42:15 +00:00 (Migrated from github.com)

I think it would be good to first add that feature as an imgui_internal.h api (calling a function or poking into the state structure), when we redesign InputText() I would definitively want to take this into account! (along with more important kind-of-opposite change such as #701 !)

I think it would be good to first add that feature as an imgui_internal.h api (calling a function or poking into the state structure), when we redesign `InputText()` I would definitively want to take this into account! (along with more important kind-of-opposite change such as #701 !)
ocornut commented 2019-11-05 18:42:15 +00:00 (Migrated from github.com)

I think it would be good to first add that feature as an imgui_internal.h api (calling a function or poking into the state structure), when we redesign InputText() I would definitively want to take this into account! (along with more important kind-of-opposite change such as #701 !)

I think it would be good to first add that feature as an imgui_internal.h api (calling a function or poking into the state structure), when we redesign `InputText()` I would definitively want to take this into account! (along with more important kind-of-opposite change such as #701 !)
kudaba commented 2019-11-05 19:15:35 +00:00 (Migrated from github.com)

This seems to do the job:
kudaba/imgui@53f90d7c55

Unfortunately I'm not in a good state to make a pr for it.

This seems to do the job: https://github.com/kudaba/imgui/commit/53f90d7c550aebe58940898b6526e4a35b7582cf Unfortunately I'm not in a good state to make a pr for it.
kudaba commented 2019-11-05 19:15:35 +00:00 (Migrated from github.com)

This seems to do the job:
kudaba/imgui@53f90d7c55

Unfortunately I'm not in a good state to make a pr for it.

This seems to do the job: https://github.com/kudaba/imgui/commit/53f90d7c550aebe58940898b6526e4a35b7582cf Unfortunately I'm not in a good state to make a pr for it.
JPGygax68 commented 2020-04-03 13:56:28 +00:00 (Migrated from github.com)

I have a related use case. I'm writing a file dialog and am using InputText to let the user type in the name of the next leg of the path. The user can also go back to the parent using backspace when the input field is empty.
But this of course requires re-initializing the text of the input field as the user moves down or up the path chain. I get odd behavior in those cases, and it now occurred to me that this must be due to the internal state of the InputText not being updated after I change the content of its buffer from outside
I'm attaching a screenshot just to illustrate what I'm doing.
Is there a proper way to change the content of the buffer from outside? Or should I try to use the Always callback?

grafik

EDIT: I made this work using the callback interface, though it was a bit convoluted.

I have a related use case. I'm writing a file dialog and am using `InputText` to let the user type in the name of the next leg of the path. The user can also go back to the parent using backspace when the input field is empty. But this of course requires re-initializing the text of the input field as the user moves down or up the path chain. I get odd behavior in those cases, and it now occurred to me that this must be due to the internal state of the `InputText` not being updated after I change the content of its buffer from outside I'm attaching a screenshot just to illustrate what I'm doing. Is there a proper way to change the content of the buffer from outside? Or should I try to use the `Always` callback? ![grafik](https://user-images.githubusercontent.com/795272/78367924-20590980-75c3-11ea-8994-e15175f759bc.png) EDIT: I made this work using the callback interface, though it was a bit convoluted.
JPGygax68 commented 2020-04-03 13:56:28 +00:00 (Migrated from github.com)

I have a related use case. I'm writing a file dialog and am using InputText to let the user type in the name of the next leg of the path. The user can also go back to the parent using backspace when the input field is empty.
But this of course requires re-initializing the text of the input field as the user moves down or up the path chain. I get odd behavior in those cases, and it now occurred to me that this must be due to the internal state of the InputText not being updated after I change the content of its buffer from outside
I'm attaching a screenshot just to illustrate what I'm doing.
Is there a proper way to change the content of the buffer from outside? Or should I try to use the Always callback?

grafik

EDIT: I made this work using the callback interface, though it was a bit convoluted.

I have a related use case. I'm writing a file dialog and am using `InputText` to let the user type in the name of the next leg of the path. The user can also go back to the parent using backspace when the input field is empty. But this of course requires re-initializing the text of the input field as the user moves down or up the path chain. I get odd behavior in those cases, and it now occurred to me that this must be due to the internal state of the `InputText` not being updated after I change the content of its buffer from outside I'm attaching a screenshot just to illustrate what I'm doing. Is there a proper way to change the content of the buffer from outside? Or should I try to use the `Always` callback? ![grafik](https://user-images.githubusercontent.com/795272/78367924-20590980-75c3-11ea-8994-e15175f759bc.png) EDIT: I made this work using the callback interface, though it was a bit convoluted.
ocornut commented 2026-04-03 16:13:28 +00:00 (Migrated from github.com)

I kept this open as I'm not satisfied of the solution yet and it's not a public API, but FYI in case you stumble on this issue, for years we had:

#include "imgui_internal.h"

    // Reload user buf (WIP #2890)
    // If you modify underlying user-passed const char* while active you need to call this (InputText V2 may lift this)
    //   strcpy(my_buf, "hello");
    //   if (ImGuiInputTextState* state = ImGui::GetInputTextState(id)) // id may be ImGui::GetItemID() is last item
    //       state->ReloadUserBufAndSelectAll();
    void        ReloadUserBufAndSelectAll();
    void        ReloadUserBufAndKeepSelection();
    void        ReloadUserBufAndMoveToEnd();
I kept this open as I'm not satisfied of the solution yet and it's not a public API, but FYI in case you stumble on this issue, for years we had: ```cpp #include "imgui_internal.h" // Reload user buf (WIP #2890) // If you modify underlying user-passed const char* while active you need to call this (InputText V2 may lift this) // strcpy(my_buf, "hello"); // if (ImGuiInputTextState* state = ImGui::GetInputTextState(id)) // id may be ImGui::GetItemID() is last item // state->ReloadUserBufAndSelectAll(); void ReloadUserBufAndSelectAll(); void ReloadUserBufAndKeepSelection(); void ReloadUserBufAndMoveToEnd(); ```
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#2881
No description provided.