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

Permit to highlight selectable programmatically #7820

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,7 @@ enum ImGuiSelectableFlags_
ImGuiSelectableFlags_AllowDoubleClick = 1 << 2, // Generate press events on double clicks too
ImGuiSelectableFlags_Disabled = 1 << 3, // Cannot be selected, display grayed out text
ImGuiSelectableFlags_AllowOverlap = 1 << 4, // (WIP) Hit testing to allow subsequent widgets to overlap this one
ImGuiSelectableFlags_Highlight = 1 << 5, // Make the item be displayed as if it is hovered

#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
ImGuiSelectableFlags_DontClosePopups = ImGuiSelectableFlags_NoAutoClosePopups, // Renamed in 1.91.0
Expand Down
25 changes: 15 additions & 10 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1357,18 +1357,18 @@ static void ShowDemoWindowWidgets(DemoWindowData* demo_data)
// (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively
// stored in the object itself, etc.)
const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" };
static int item_current_idx = 0; // Here we store our selection data as an index.
static int item_selected_idx = 0; // Here we store our selection data as an index.

// Pass in the preview value visible before opening the combo (it could technically be different contents or not pulled from items[])
const char* combo_preview_value = items[item_current_idx];
const char* combo_preview_value = items[item_selected_idx];

if (ImGui::BeginCombo("combo 1", combo_preview_value, flags))
{
for (int n = 0; n < IM_ARRAYSIZE(items); n++)
{
const bool is_selected = (item_current_idx == n);
const bool is_selected = (item_selected_idx == n);
if (ImGui::Selectable(items[n], is_selected))
item_current_idx = n;
item_selected_idx = n;

// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
Expand Down Expand Up @@ -1410,14 +1410,18 @@ static void ShowDemoWindowWidgets(DemoWindowData* demo_data)
// (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively
// stored in the object itself, etc.)
const char* items[] = { "AAAA", "BBBB", "CCCC", "DDDD", "EEEE", "FFFF", "GGGG", "HHHH", "IIII", "JJJJ", "KKKK", "LLLLLLL", "MMMM", "OOOOOOO" };
static int item_current_idx = 0; // Here we store our selection data as an index.
static int item_selected_idx = 0; // Here we store our selected data as an index.
static int item_highlighted_idx = -1; // Here we store our highlighted data as an index.
if (ImGui::BeginListBox("listbox 1"))
{
for (int n = 0; n < IM_ARRAYSIZE(items); n++)
{
const bool is_selected = (item_current_idx == n);
const bool is_selected = (item_selected_idx == n);
if (ImGui::Selectable(items[n], is_selected))
item_current_idx = n;
item_selected_idx = n;

if (ImGui::IsItemHovered())
item_highlighted_idx = n;

// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
Expand All @@ -1433,9 +1437,10 @@ static void ShowDemoWindowWidgets(DemoWindowData* demo_data)
{
for (int n = 0; n < IM_ARRAYSIZE(items); n++)
{
const bool is_selected = (item_current_idx == n);
if (ImGui::Selectable(items[n], is_selected))
item_current_idx = n;
const bool is_selected = (item_selected_idx == n);
const ImGuiSelectableFlags flags = (item_highlighted_idx == n)?ImGuiSelectableFlags_Highlight: 0;
if (ImGui::Selectable(items[n], is_selected, flags))
item_selected_idx = n;

// Set the initial focus when opening the combo (scrolling + keyboard navigation focus)
if (is_selected)
Expand Down
8 changes: 5 additions & 3 deletions imgui_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6857,6 +6857,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
bool hovered, held;
bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags);

const bool highlighted = hovered || (flags & ImGuiSelectableFlags_Highlight);

// Multi-selection support (footer)
if (is_multi_select)
{
Expand Down Expand Up @@ -6894,14 +6896,14 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
// Render
if (is_visible)
{
if (hovered || selected)
if (highlighted || selected)
{
// FIXME-MULTISELECT: Styling: Color for 'selected' elements? ImGuiCol_HeaderSelected
ImU32 col;
if (selected && !hovered)
if (selected && !highlighted)
col = GetColorU32(ImLerp(GetStyleColorVec4(ImGuiCol_Header), GetStyleColorVec4(ImGuiCol_HeaderHovered), 0.5f));
else
col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
col = GetColorU32((held && highlighted) ? ImGuiCol_HeaderActive : highlighted ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
RenderFrame(bb.Min, bb.Max, col, false, 0.0f);
}
if (g.NavId == id)
Expand Down