From 156e0a807087b02a735ab14b6c363c64cecf5849 Mon Sep 17 00:00:00 2001 From: ocornut Date: Mon, 2 Jan 2023 18:01:57 +0100 Subject: [PATCH] Demo: amend Selectable() + BeginPopupContextItem() demo to maintain and update selection state when popup is open. (#6032) --- imgui_demo.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 560b086f..435c3684 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -3438,11 +3438,14 @@ static void ShowDemoWindowPopups() // and BeginPopupContextItem() will use the last item ID as the popup ID. { const char* names[5] = { "Label1", "Label2", "Label3", "Label4", "Label5" }; + static int selected = -1; for (int n = 0; n < 5; n++) { - ImGui::Selectable(names[n]); + if (ImGui::Selectable(names[n], selected == n)) + selected = n; if (ImGui::BeginPopupContextItem()) // <-- use last item id as popup id { + selected = n; ImGui::Text("This a popup for \"%s\"!", names[n]); if (ImGui::Button("Close")) ImGui::CloseCurrentPopup();