Skip to content

IsItemDeactivatedAfterEdit doesn't play nicely with ConfigInputTextEnterKeepActive #9001

@Pix3lP3rf3ct

Description

@Pix3lP3rf3ct

Version/Branch of Dear ImGui:

Version v1.92.4 WIP, Branch: Docking

Back-ends:

Custom

Compiler, OS:

MacOS

Full config/build information:

Dear ImGui 1.92.4 WIP (19234)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=202002
define: IMGUI_DISABLE_DEFAULT_FORMAT_FUNCTIONS
define: IMGUI_DISABLE_DEFAULT_FILE_FUNCTIONS
define: IMGUI_DISABLE_FILE_FUNCTIONS
define: IMGUI_DISABLE_DEFAULT_ALLOCATORS
define: __APPLE__
define: __GNUC__=4
define: __clang_version__=16.0.0 (clang-1600.0.26.6)
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: NULL
io.BackendRendererName: imgui_impl_TGCEngine
io.ConfigFlags: 0x00000081
 NavEnableKeyboard
 DockingEnable
io.ConfigViewportsNoDecoration
io.ConfigViewportsNoDefaultParent
io.ConfigMacOSXBehaviors
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigWindowsMoveFromTitleBarOnly
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00000C08
 PlatformHasViewports
 RendererHasVtxOffset
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 2048,2048
io.Fonts->FontLoaderName: stb_truetype
io.DisplaySize: 2611.00,1578.00
io.DisplayFramebufferScale: 2.00,2.00
--------------------------------
style.WindowPadding: 6.00,6.00
style.WindowBorderSize: 1.00
style.FramePadding: 4.00,4.00
style.FrameRounding: 2.00
style.FrameBorderSize: 1.00
style.ItemSpacing: 6.00,4.00
style.ItemInnerSpacing: 4.00,4.00

Details:

My goal is that when the user presses the Enter key in a input text item that the focus remains in the input text box and the text is selected.

In previous projects we wrote a custom solution for this but I see that ImGui provides a beta feature flag called ConfigInputTextEnterKeepActive. Using this flag acts almost perfectly with one draw back. ImGui::IsItemDeactivatedAfterEdit() doesn't work anymore causing us to have to implement our own solution to get around this problem. We use that function to know when to finalize the undo event. Our edit system will merge events that are similar if the previous event hasn't been finalized. With that flag enabled, this never gets called because the item is never deactivated. So, I made a new function to help out but it might be nice that it's handled on the ImGui side. Here is my code:

inline bool IsItemEnterAfterEdit()
{
	ImGuiContext& g = *GImGui;
	return ImGui::IsItemActive() && ImGui::IsKeyPressed( ImGuiKey_Enter ) && g.ActiveIdHasBeenEditedBefore;
}
  	const bool edit = ImGui::DragScalarN( "###value", imGuiDataType, &value, NUM_CHANNELS, speed, minPtr, maxPtr, format.c_str(), ImGuiSliderFlags_AlwaysClamp );
	const bool done = ImGui::IsItemDeactivatedAfterEdit() || TgcImGui::IsItemEnterAfterEdit();

	if ( edit || done )
	{
		std::string title;

		if constexpr( std::is_same< DATA_TYPE, uint8_t >::value || std::is_same< DATA_TYPE, int8_t>::value )
			title = EVENT_TITLE( read, static_cast< int32_t >( value ) );
		else
			title = EVENT_TITLE( read, value );

		editManager.StartRecording( title.c_str(), done );
		read.SetValue< DATA_TYPE >( value );
		editManager.StopRecording();
	}

If you look at the done variable there is now an added call to know if the enter key was pressed so I get the same results as before. This clearly isn't much work on my part but it adds to the complexities of ImGui. One possible solution is to deactivate the item and then reactivate so the same code path works.

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

No response

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions