Issue Pushing styles upon listbox hovered.
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:
Minimal, Complete and Verifiable Example code:
No response
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()->HoveredWindowthen see if thatwindowID/name matches the one you want. e.g.if (strcmp(window->Name, "##City") == 0.
This way you can perform the PushStyleXXX calls before BeginListBox().
thank you my dear. I will take the suggestions and play with it a bit.