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

PushStyleColor for Selectable inside For Loop only colors first item #4199

Closed
samygarcia opened this issue Jun 4, 2021 · 2 comments
Closed
Labels
label/id and id stack implicit identifiers, pushid(), id stack style

Comments

@samygarcia
Copy link

samygarcia commented Jun 4, 2021

Version/Branch of Dear ImGui:

Version: v1.75 WIP
Branch: master

Back-end/Renderer/Compiler/OS

Back-ends: Filament
Operating System: web, cross-compiled to WASM

My Issue/Question:

I'm making a console log, and would like the background of each log to be color-coded depending on their type. I have a for loop going through all of the logs and making a Selectable for each log. However it only colors the first log in the loop. I've tried putting each selectable in their own window, and calling PushStyleColor before the window and PopStyleColor after the window end (as well as trying to call it inside the window). However, nothing seems to work. Any ideas how this works? Thanks!

Screenshots/Video

image

Example Code:

ImGui::ListBoxHeader(
        "", (filter_log_) ? log_settings_[filter_by_].count : all_logs_.size());
    for (int i = 0; i < all_logs_.size(); ++i) {
      if (filter_log_ && all_logs_.at(i).output_kind != filter_by_) {
        continue;
      }
      ImGui::Image(
          log_settings_[all_logs_.at(i).output_kind].icon->GetTexture(),
          kLogImageSize);
      ImGui::SameLine();
      ImGui::PushStyleColor(
          ImGuiCol_Header,
          log_settings_[all_logs_.at(i).output_kind].background_color);
      ImGui::Selectable(all_logs_[i].message.c_str(),
                        (i == selected_item_index_));
      ImGui::PopStyleColor();
    }
    ImGui::ListBoxFooter();
@ocornut ocornut added the style label Jun 8, 2021
@PathogenDavid
Copy link
Contributor

ImGuiCol_Header is only used when the item is selected. (Also note that it also might use ImGuiCol_HeaderActive or ImGuiCol_HeaderHovered if the item is held/hovered.)

Unless selected or hovered, Selectable widgets do not have a background at all. (The background you do see is from the containing list box.)

@ocornut ocornut added the label/id and id stack implicit identifiers, pushid(), id stack label May 9, 2023
@ocornut
Copy link
Owner

ocornut commented May 9, 2023

In addition, you issue here was an ID conflict which would be solved with surrounding your loop body with PushID(i) / PopID().

See FAQ entry:
https://github.com/ocornut/imgui/blob/master/docs/FAQ.md#q-about-the-id-stack-system

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
label/id and id stack implicit identifiers, pushid(), id stack style
Projects
None yet
Development

No branches or pull requests

3 participants