Demo: amend Selectable() + BeginPopupContextItem() demo to maintain and update selection state when popup is open. (#6032)

This commit is contained in:
ocornut 2023-01-02 18:01:57 +01:00
parent e5d51866c6
commit 156e0a8070
1 changed files with 4 additions and 1 deletions

View File

@ -3438,11 +3438,14 @@ static void ShowDemoWindowPopups()
// and BeginPopupContextItem() will use the last item ID as the popup ID. // and BeginPopupContextItem() will use the last item ID as the popup ID.
{ {
const char* names[5] = { "Label1", "Label2", "Label3", "Label4", "Label5" }; const char* names[5] = { "Label1", "Label2", "Label3", "Label4", "Label5" };
static int selected = -1;
for (int n = 0; n < 5; n++) 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 if (ImGui::BeginPopupContextItem()) // <-- use last item id as popup id
{ {
selected = n;
ImGui::Text("This a popup for \"%s\"!", names[n]); ImGui::Text("This a popup for \"%s\"!", names[n]);
if (ImGui::Button("Close")) if (ImGui::Button("Close"))
ImGui::CloseCurrentPopup(); ImGui::CloseCurrentPopup();