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

ImGui::SetKeyboardFocusHere doesn't highlight selected item #7370

Closed
Woitek1993 opened this issue Mar 5, 2024 · 4 comments
Closed

ImGui::SetKeyboardFocusHere doesn't highlight selected item #7370

Woitek1993 opened this issue Mar 5, 2024 · 4 comments
Labels
focus nav keyboard/gamepad navigation

Comments

@Woitek1993
Copy link

Version/Branch of Dear ImGui:

Version 1.90.0, Branch: docking

Back-ends:

imgui_impl_glfw.cpp + imgui_impl_opengl2.cpp

Compiler, OS:

Windows 10

Full config/build information:

No response

Details:

Hello,
I am trying to make a programmable change of the focus of the keyboard on the item using SetKeyboardFocusHere();; I've noticed that the highlight is not applied while doing so. I would like to get a highlight on this item in the same way as when the down/up navigation keys are pressed. The object in this case does not lose the highlight when the mouse is moved away.

Example code:

    ImGui::Selectable(std::format("Name: \"{0}\"", (t->m_name.c_str()) ? t->m_name.c_str() : "").c_str());
    if (ImGui::IsItemClicked())
    {
        SetKeyboardFocusHere();
    }
    ImGui::Selectable(std::format("Mask Name: \"{0}\"", (t->m_mask_name.c_str()) ? t->m_mask_name.c_str() : "").c_str());
    if (ImGui::IsItemClicked())
    {
        SetKeyboardFocusHere();
    }
    ImGui::Selectable(std::format("Width: {0}", t->m_width).c_str());
    if (ImGui::IsItemClicked())
    {
        SetKeyboardFocusHere();
    }

Screenshots/Video:

Got:
got
Expected:
expected

Minimal, Complete and Verifiable Example code:

No response

@ocornut ocornut added nav keyboard/gamepad navigation focus labels Mar 6, 2024
@PathogenDavid
Copy link
Contributor

I've noticed that the highlight is not applied while doing so. I would like to get a highlight on this item in the same way as when the down/up navigation keys are pressed.

The nav highlight is intentionally hidden when the mouse is used since the highlight is only intended only for keyboard and gamepad navigation, so you're fighting against the internal design of the nav system.

It would be helpful to know exactly what you're trying to accomplish UI-wise from a user perspective as this feels very XY problem.

Are you wanting the nav highlight to always be visible when the user interacts with all items or just for these selectables?

If the latter, why? And could the built-in selectable highlighting suitable for your use-case?


As an aside, ImGui::SetKeyboardFocusHere(); without any arguments focuses the next item, you need to use ImGui::SetKeyboardFocusHere(-1);

@Woitek1993
Copy link
Author

I've noticed that the highlight is not applied while doing so. I would like to get a highlight on this item in the same way as when the down/up navigation keys are pressed.

The nav highlight is intentionally hidden when the mouse is used since the highlight is only intended only for keyboard and gamepad navigation, so you're fighting against the internal design of the nav system.

It would be helpful to know exactly what you're trying to accomplish UI-wise from a user perspective as this feels very XY problem.

Are you wanting the nav highlight to always be visible when the user interacts with all items or just for these selectables?

If the latter, why? And could the built-in selectable highlighting suitable for your use-case?

As an aside, ImGui::SetKeyboardFocusHere(); without any arguments focuses the next item, you need to use ImGui::SetKeyboardFocusHere(-1);

Hello,

The goal I want to achieve is when I press the mouse left click button on an item or use the UP/DOWN keys, it is selected in TreeNodeEX. When I move through the navigation, the item is selected there all the time, so I thought that when I change the focus of the keyboard, the selection automatically appears.

My code at the moment works like this:
Every time I press the Up/Down key, I change the state of the variable:
m_keyboard_emulate_mouse_click to true, and when the button is released, to false.

int main(int argc, char* argv[])
{
...
glfwSetKeyCallback(g_Window, Application::KeyHandlerCallback);
...
}

void Application::KeyHandlerCallback(GLFWwindow* window, s32 key, s32 scancode, s32 action, s32 mods)
{
ImGui_ImplGlfw_KeyCallback(window, key, scancode, action, mods);

if (action == GLFW_PRESS)
{
...
    if ((key == GLFW_KEY_UP || key == GLFW_KEY_DOWN))
    {
         g_App->m_keyboard_emulate_mouse_click = true;
    }
...
}
else if (action == GLFW_RELEASE)
{
    if ((key == GLFW_KEY_UP || key == GLFW_KEY_DOWN))
    {
        g_App->m_keyboard_emulate_mouse_click = false;
    }
}

}

For each item in the tree, I check:

bool Application::IsItemClicked(ImGuiMouseButton mouse_button)
{
return (ImGui::IsMouseClicked(mouse_button) || m_keyboard_emulate_mouse_click) && ImGui::IsItemHovered(ImGuiHoveredFlags_None);
}

whether a button was pressed, or an up or down button was pressed, and whether we hovered the mouse over an item.
I use this function for each check if we have clicked something in the tree.

The operation of the program looks like this using this code:

https://youtu.be/cX2P973gR_0

@Woitek1993
Copy link
Author

I can make it work if I set the ImGuiNavHighlightFlags_AlwaysDraw flag that seems to be unused in the ImGui::RenderNavHighlight function. Changing RenderFrame, so it triggers g.NavId == id as well would also be nice to implement. I think it would be nice to set some flag that forces widgets to be rendered this way. In other guis it seems to be a thing. This way highlights for the currently focused item will be persistent between the mouse clicking and nagivation movement.

Virtual TreeView Example: https://youtu.be/TgXnG67DGko
Example with the mentioned changes in ImGui code: https://youtu.be/WTFlaJYAktg

ocornut added a commit that referenced this issue Oct 18, 2024
ocornut added a commit that referenced this issue Oct 18, 2024
…(), ImGuiNavHighlightFlags to ImGuiNavRenderCursorFlags. (#1074, #2048, #7237, #8059, #1712, #7370, #787)

+ referenced in #8057, #3882, #3411, #2155, #3351, #4722, #1658, #4050.
@ocornut
Copy link
Owner

ocornut commented Oct 18, 2024

I have added a few things related to those requests:

  • Nav: added io.ConfigNavCursorVisibleAuto and io.ConfigNavCursorVisibleAlways config options (ab9ce2a).
  • Nav: added SetNavCursorVisible() to override current state (634a7ed).

Technically I think the config options are sufficient for most people.

Other tangentially related options:

  • Nav: added io.ConfigNavEscapeClearFocusItem (7a56b41).
  • Nav: added io.ConfigNavEscapeClearFocusWindow (b001038).
  • And a couple of related fixes for all the above.

Technically this # also pertain to use SetKeyboardFocusHere() and other functions which are still be redesigned/improved.
My expectation is that the future replacement SetKeyboardFocusHere() will have options to configure how nav cursor visibility is made visible or not.

I'm closing this and other related issues as normally solved, but feel free to comment or open new issues if you have question or problem related to those.

(

Also note, if you have mods using internals, that those two fields have been renamed internally:

  • Terminology has been changed to call this the "nav cursor" more consistently.
  • bool NavDisableHighlight -> became bool NavCursorVisible (with opposite value).
  • bool NavDisableMouseHover -> became bool NavDisableMouseHover

)

@ocornut ocornut closed this as completed Oct 18, 2024
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

3 participants