Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetKeyboardFocusHere() only works within active focus scope #7226

Closed
bratpilz opened this issue Jan 15, 2024 · 3 comments
Closed

SetKeyboardFocusHere() only works within active focus scope #7226

bratpilz opened this issue Jan 15, 2024 · 3 comments
Labels
bug focus nav keyboard/gamepad navigation

Comments

@bratpilz
Copy link

Version/Branch of Dear ImGui:

Version 1.90.1, Branch: master

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_opengl3.cpp

Compiler, OS:

Linux + GCC

Full config/build information:

Dear ImGui 1.90.1 (19010)
--------------------------------
sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=201103
define: __linux__
define: __GNUC__=12
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------
io.BackendPlatformName: imgui_impl_glfw
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000443
 NavEnableKeyboard
 NavEnableGamepad
 DockingEnable
 ViewportsEnable
io.ConfigViewportsNoDecoration
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x0000140E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 RendererHasVtxOffset
 RendererHasViewports
--------------------------------
io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,64
io.DisplaySize: 1280.00,720.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:

My Issue/Question:

Hello,

I often use SetKeyboardFocusHere() to focus an InputText() after some event has happened.
Since commit 70f2aaf, this works only if the current focus scope contains the InputText() in question.
I understand that this is because SetKeyboardFocusHere() is treated as a tabbing request and starting from the mentioned commit, it is required to set ImGuiWindowFlags_NavFlattened to be able to tab between child windows.
I do not want to enable this flag however, but still want to be able to programmatically switch focus like before.
One workaround currently is to call SetWindowFocus() before SetKeyboardFocusHere(), see the attached code example.
Would it be possible to skip this check:
if (g.NavFocusScopeId != g.CurrentFocusScopeId)
in NavProcessItemForTabbingRequest() when the tabbing request came from SetKeyboardFocusHere()?

Kind regards.

Screenshots/Video:

imgui-focus.mp4

Minimal, Complete and Verifiable Example code:

	  ImGui::Begin("Test", 0, ImGuiWindowFlags_AlwaysAutoResize);
	  unsigned child_flags = ImGuiChildFlags_Border | ImGuiChildFlags_AlwaysAutoResize | ImGuiChildFlags_AutoResizeX | ImGuiChildFlags_AutoResizeY;
	  ImGui::BeginChild("Child1", ImVec2(), child_flags);
	  bool focus_InputText = false;
	  if (ImGui::Button("Focus InputText in other child"))
	    focus_InputText = true;
	  ImGui::EndChild();
	  ImGui::BeginChild("Child2", ImVec2(), child_flags);
	  if (ImGui::Button("Focus InputText in this child"))
	    focus_InputText = true;
	  char buf[32];
	  snprintf(buf, sizeof buf, "Some text");
	  if (focus_InputText)
	    {
	      // ImGui::SetWindowFocus(); // Un-comment this to allow the top Button() to focus the InputText().
	      ImGui::SetKeyboardFocusHere();
	    }
	  ImGui::InputText("##Text", buf, sizeof buf, ImGuiInputTextFlags_AutoSelectAll);
	  ImGui::EndChild();
	  ImGui::End();
@ocornut ocornut added bug nav keyboard/gamepad navigation focus labels Jan 15, 2024
ocornut added a commit that referenced this issue Jan 15, 2024
@ocornut
Copy link
Owner

ocornut commented Jan 15, 2024

Nice catch, and thanks for narrowing it down to the commit. Thank you!
This is fixed by dd0efdc

ocornut added a commit to ocornut/imgui_test_engine that referenced this issue Jan 15, 2024
@ocornut
Copy link
Owner

ocornut commented Jan 15, 2024

And added regression test in ocornut/imgui_test_engine@ffd6085

@ocornut ocornut closed this as completed Jan 15, 2024
ocornut added a commit to ocornut/imgui_test_engine that referenced this issue Jan 15, 2024
@bratpilz
Copy link
Author

Thank you very much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug focus nav keyboard/gamepad navigation
Projects
None yet
Development

No branches or pull requests

2 participants