imgui icon indicating copy to clipboard operation
imgui copied to clipboard

Issue Pushing styles upon listbox hovered.

Open devhaocui opened this issue 11 months ago • 1 comments

Version/Branch of Dear ImGui:

master

Back-ends:

using glfw + opengl3

Compiler, OS:

macOS clang 12

Full config/build information:

No response

Details:

I'm trying to add a hover border setup. The photo example is a stationary one. The code below is my attempt at trying to get it to only create this style when the user cursor or the mouse is hovered onto the specified listbox. I'm not having any luck so far. Any tips? The isListBoxHovered is working as I assumed but I think I'm not up to speed with how PushStyle and PopStyle works.

  if (ImGui::BeginListBox("##City", listBoxSize)) {
    bool isListBoxHovered = ImGui::IsWindowHovered(ImGuiHoveredFlags_ChildWindows);
    ImGui::Text("Hovered: %i", isListBoxHovered);
    if (isListBoxHovered) {
      ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(10.0f, 10.0f));
      ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 2.5f);
      ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(0.0f, 1.0f, 0.0f, 1.0f));
      ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 9.0f);
    }
    ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), "City Destination");
    ImGui::SeparatorText("");
    for (int n = 0; n < cityList.size(); n++) {
      const bool is_selected = (item_current_id_1 == n);
      if (ImGui::Selectable(cityList[n].c_str(), is_selected))
        item_current_id_1 = n;
      if (is_selected)
        ImGui::SetItemDefaultFocus();
    }
    ImGui::EndListBox();
    if (isListBoxHovered) {
      ImGui::PopStyleColor();
      ImGui::PopStyleVar(3);
    }
    //  ImGui::PopStyleColor();
    //  ImGui::PopStyleVar(3);
  }

Screenshots/Video:

Image

Minimal, Complete and Verifiable Example code:

No response

devhaocui avatar Mar 23 '25 20:03 devhaocui

Your problem is that you are modifying style values after BeginListBox() (which calls Begin()) and those values are solely used by Begin() so the modification happens too late.

You can either:

  • Store the hovered list box and use it in the next frame.
  • Get into internal and use ImGuiWindow* window = GetCurrentContext()->HoveredWindow then see if that window ID/name matches the one you want. e.g. if (strcmp(window->Name, "##City") == 0.

This way you can perform the PushStyleXXX calls before BeginListBox().

ocornut avatar Mar 26 '25 13:03 ocornut

thank you my dear. I will take the suggestions and play with it a bit.

devhaocui avatar Apr 10 '25 08:04 devhaocui