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

Focus loss when removing focused element #2129

Open
cranflavin opened this issue Oct 9, 2018 · 4 comments
Open

Focus loss when removing focused element #2129

cranflavin opened this issue Oct 9, 2018 · 4 comments
Labels
focus nav keyboard/gamepad navigation

Comments

@cranflavin
Copy link
Contributor

cranflavin commented Oct 9, 2018

(Click "Preview" to turn any http URL into a clickable link)


Version/Branch of Dear ImGui:

master: c12da2a

Back-end file/Renderer/OS:
imgui_impl_dx12.cpp + imgui_impl_win32.cpp
OS: Windows

My Issue/Question:

I'm using the controller navigation. I have a user window that shows all the users in the game. It's possible to remove a user via a Remove button. When I remove the user, focus appears to be lost in the window. If I press the Left controller button, focus re-appears on the next button in the window. I'd like to make this happen by default when the button with focus is removed. In the example below, clicking Button 1 will make Button 1 disappear, and I'd like for focus to appear on Button 2.

Standalone, minimal, complete and verifiable example:

	ImGui::Begin("Window");

	static bool showButton1 = true;
	if (showButton1 && ImGui::Button("Button 1"))
	{
		showButton1 = !showButton1;
	}

	static bool showButton2 = true;
	if (showButton2 && ImGui::Button("Button 2"))
	{
		showButton2 = !showButton2;
	}

	ImGui::End();

I tried some code like this in the handler of Button 1 with no success. But I'd also like avoid writing this kind of handling for removable items. This seems like something that should be handled in the framework itself.

        // If the remove button is pressed, and has focus, try to set focus to the
        // next focusable element
        if (ImGui::IsItemFocused())
        {
            ImGui::SetKeyboardFocusHere();
        }
@ocornut ocornut added the nav keyboard/gamepad navigation label Oct 9, 2018
@ocornut
Copy link
Owner

ocornut commented Oct 9, 2018

Right. At the moment there's no fallback but we could/should try to a "in-place/neighbor" nav query when losing the current nav item.

Depending what your button are for and if they are mutually exclusive, you may use the ### operator to have them use the same id:

if (blah)
  ImGui::Button("Play###PlayStop");
else
  ImGui::Button("Stop###PlayStop");

Those two buttons will have the same identifier.

@ocornut ocornut added the focus label Oct 9, 2018
@cranflavin
Copy link
Contributor Author

The buttons are mutually exclusive. Is there a recommended way to preemptively assign focus to the next element if I detect that the "Remove" button is pressed? Or manually do the nearest neighbor search?

@ocornut
Copy link
Owner

ocornut commented Oct 9, 2018

The buttons are mutually exclusive.

So the solution pointed above would work.

Is there a recommended way to preemptively assign focus to the next element if I detect that the "Remove" button is pressed? Or manually do the nearest neighbor search?

Not yet with the public API. I have a stash with some of those implemented that I need to rework, but the general case for "focus this item" requires things like scrolling to keep something in view, and needs to be designed around the fact that items may be clipped (so queries needs to run pre-clipping).

If you don't need this, you may include imgui_internal.h and use ImGui::SetNavID(ImGui::GetID("something", 0)) in the meanwhile.

Linking to #1074.

@cranflavin
Copy link
Contributor Author

Sorry, I misspoke about the mutual exclusivity. I meant to say they are not mutually exclusive.

I tried the SetNavId solution and that seems to be a reasonable workaround for now.

Thanks!

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

No branches or pull requests

2 participants