Skip to content

InputInt and IsItemDeactivatedAfterEdit don't work as expected when pressing Escape #8915

Description

@tom-seddon

Version/Branch of Dear ImGui:

v1.92.2b-docking (1f7f1f5)

Back-ends:

probably all, but tested with win32_directx9

Compiler, OS:

probably all, but using Windows 10 (Version 22H2 (OS Build 19045.6216)) and VS2022 (Microsoft (R) C/C++ Optimizing Compiler Version 19.44.35215 for x64)

Full config/build information:

Dear ImGui 1.92.2b (19222)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: _WIN32
define: _WIN64
define: _MSC_VER=1944
define: _MSVC_LANG=201402
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_dx9
io.ConfigFlags: 0x00000483
 NavEnableKeyboard
 NavEnableGamepad
 DockingEnable
 ViewportsEnable
io.ConfigDpiScaleFonts
io.ConfigDpiScaleViewports
io.ConfigViewportsNoDecoration
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00001C1F
 HasGamepad
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 RendererHasVtxOffset
 RendererHasTextures
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128
io.Fonts->FontLoaderName: stb_truetype
io.DisplaySize: 1264.00,761.00
io.DisplayFramebufferScale: 1.00,1.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:

I'm trying to make InputInt work when there's no direct access to the actual value - just by-value getters/setters. (It'd be inconvenient to change this.)

The code I'd expect to be able to use would be along these lines:

int tmp=GetValue();
if(ImGui::InputInt("label",&tmp)) {
    if(ImGui::IsItemDeactivatedAfterEdit()) {
        SetValue(tmp);
    }
}

And this does work somewhat ok, in that SetValue is called for pressing Return, but not for intermediate edits, as desired - but, after pressing Escape, IsItemDeactivatedAfterEdit( returns true (which is fine), and tmp is the edited value rather than the original one (which is a bit surprising).

I don't mind if SetValue gets called again with the original value, because the setter can always check and do nothing if it's the same value. But it feels odd that pressing Escape doesn't undo the edits! The behaviour of the input int widget in the Widgets/Basic/InputText section of the demo is much more as I'd expect: Return accepts the edited value, and Escape reverts its it to the pre-edit value. It operates directly on a static int though which isn't appropriate in this case.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

You can clone my test code from a branch of my fork:

git clone https://github.com/tom-seddon/imgui
cd imgui
git checkout tom/InputInt_problem

The changes are to imgui_demo.cpp (diff here: tom-seddon@b6273fe) - so it should hopefully apply to any example with any backends with any OS/compiler combination, but I've been building and running example_win32_directx9 from the VS2022 IDE on Windows 10 (exact versions as above)

Repro steps:

  1. Build and run demo
  2. Expand the Widgets/Basic section in the demo. There should be a row labelled input int (2) visible - this is the test I've added
  3. Click on the input int row, current value 123. Type in 124 and press Return. The value changes to 124
  4. Click on the input int row, current value 124. Type in 125 and press Escape. The value changes back to 124
  5. Click on the input int (2) row, current value 124. Type in 125 and press Return. The value changes to 125
  6. Click on the input int (2) row, current value 125. Type in 126 and press Escape. The value changes to 126 rather than reverting to 125

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions