Skip to content

Commit

Permalink
Demo: Combos: demonstrate a very simple way to add a filter to a comb…
Browse files Browse the repository at this point in the history
…o. (#718)
  • Loading branch information
ocornut committed Feb 10, 2025
1 parent e8ad60c commit 2206e31
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Other changes:
which amusingly made it disappear when using very big font/frame size.
- Tables: fixed calling SetNextWindowScroll() on clipped scrolling table
to not leak the value into a subsequent window. (#8196)
- Demo: Combos: demonstrate a very simple way to add a filter to a combo,
by showing the filter inside the combo contents. (#718)
- Backends: Metal: Fixed a crash on application resources. (#8367, #7419) [@anszom]
- Backends: WebGPU: Fix for DAWN API rename WGPUProgrammableStageDescriptor -> WGPUComputeState.
[@PhantomCloak] (#8369)
Expand Down
32 changes: 27 additions & 5 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,6 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data)

// 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_selected_idx];

if (ImGui::BeginCombo("combo 1", combo_preview_value, flags))
{
for (int n = 0; n < IM_ARRAYSIZE(items); n++)
Expand All @@ -1451,23 +1450,46 @@ static void ShowDemoWindowWidgets(ImGuiDemoWindowData* demo_data)
ImGui::EndCombo();
}

// Show case embedding a filter using a simple trick: displaying the filter inside combo contents.
// See https://github.com/ocornut/imgui/issues/718 for advanced/esoteric alternatives.
if (ImGui::BeginCombo("combo 2 (w/ filter)", combo_preview_value, flags))
{
static ImGuiTextFilter filter;
if (ImGui::IsWindowAppearing())
{
ImGui::SetKeyboardFocusHere();
filter.Clear();
}
ImGui::SetNextItemShortcut(ImGuiMod_Ctrl | ImGuiKey_F);
filter.Draw("##Filter", -FLT_MIN);

for (int n = 0; n < IM_ARRAYSIZE(items); n++)
{
const bool is_selected = (item_selected_idx == n);
if (filter.PassFilter(items[n]))
if (ImGui::Selectable(items[n], is_selected))
item_selected_idx = n;
}
ImGui::EndCombo();
}

ImGui::Spacing();
ImGui::SeparatorText("One-liner variants");
HelpMarker("Flags above don't apply to this section.");
HelpMarker("The Combo() function is not greatly useful apart from cases were you want to embed all options in a single strings.\nFlags above don't apply to this section.");

// Simplified one-liner Combo() API, using values packed in a single constant string
// This is a convenience for when the selection set is small and known at compile-time.
static int item_current_2 = 0;
ImGui::Combo("combo 2 (one-liner)", &item_current_2, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0");
ImGui::Combo("combo 3 (one-liner)", &item_current_2, "aaaa\0bbbb\0cccc\0dddd\0eeee\0\0");

// Simplified one-liner Combo() using an array of const char*
// This is not very useful (may obsolete): prefer using BeginCombo()/EndCombo() for full control.
static int item_current_3 = -1; // If the selection isn't within 0..count, Combo won't display a preview
ImGui::Combo("combo 3 (array)", &item_current_3, items, IM_ARRAYSIZE(items));
ImGui::Combo("combo 4 (array)", &item_current_3, items, IM_ARRAYSIZE(items));

// Simplified one-liner Combo() using an accessor function
static int item_current_4 = 0;
ImGui::Combo("combo 4 (function)", &item_current_4, [](void* data, int n) { return ((const char**)data)[n]; }, items, IM_ARRAYSIZE(items));
ImGui::Combo("combo 5 (function)", &item_current_4, [](void* data, int n) { return ((const char**)data)[n]; }, items, IM_ARRAYSIZE(items));

ImGui::TreePop();
}
Expand Down

0 comments on commit 2206e31

Please # to comment.