BeginComboPreview(): fixed issues with nesting. Previous comments were wrong. (#1658, #4168)

This commit is contained in:
ocornut
2026-09-08 21:52:23 +02:00
parent db5b735a5d
commit ea6d21687b
2 changed files with 8 additions and 4 deletions

View File

@@ -189,6 +189,7 @@ struct ImGuiTypingSelectRequest; // Storage for GetTypingSelectRequest() (aim
struct ImGuiWindow; // Storage for one window
struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame, in practice we currently keep it for each window)
struct ImGuiWindowSettings; // Storage for a window .ini settings (we keep one of those even if the actual window wasn't instanced during this session)
struct ImGuiWindowStackData; // Storage for each window pushed into the stack
// Enumerations
// Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists.
@@ -1459,7 +1460,7 @@ struct IMGUI_API ImGuiErrorRecoveryState
ImGuiErrorRecoveryState() { memset((void*)this, 0, sizeof(*this)); }
};
// Data saved for each window pushed into the stack
// Storage for each window pushed into the stack.
struct ImGuiWindowStackData
{
ImGuiWindow* Window;
@@ -1467,6 +1468,7 @@ struct ImGuiWindowStackData
ImGuiErrorRecoveryState StackSizesInBegin; // Store size of various stacks for asserting
bool DisabledOverrideReenable; // Non-child window override disabled flag
float DisabledOverrideReenableAlphaBackup;
ImRect ParentLastComboPreviewRect;
};
struct ImGuiShrinkWidthItem
@@ -3538,7 +3540,7 @@ namespace ImGui
// Combos
IMGUI_API bool BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags flags);
IMGUI_API bool BeginComboPreview(); // Submit preview contents for the *last* BeginCombo() call, to display contents that's more than just a text label.
IMGUI_API bool BeginComboPreview(); // Submit preview contents a combo. Call this after EndCombo() to display contents that's more than just a text label.
IMGUI_API void EndComboPreview();
// Keyboard/Gamepad Navigation

View File

@@ -2074,6 +2074,7 @@ bool ImGui::BeginComboPopup(ImGuiID popup_id, const ImRect& bb, ImGuiComboFlags
return false;
}
g.BeginComboDepth++;
g.CurrentWindowStack.back().ParentLastComboPreviewRect = g.ComboPreviewData.PreviewRect;
return true;
}
@@ -2081,6 +2082,7 @@ void ImGui::EndCombo()
{
ImGuiContext& g = *GImGui;
g.BeginComboDepth--;
g.ComboPreviewData.PreviewRect = g.CurrentWindowStack.back().ParentLastComboPreviewRect;
char name[16];
ImFormatString(name, IM_COUNTOF(name), "##Combo_%02d", g.BeginComboDepth); // FIXME: Move those to helpers?
if (strcmp(g.CurrentWindow->Name, name) != 0)
@@ -2088,10 +2090,10 @@ void ImGui::EndCombo()
EndPopup();
}
// Submit preview contents for the *last* BeginCombo() call, to display contents that's more than just a text label.
// Submit preview contents for BeginCombo()/EndCombo(), to display contents that's more than just a text label.
// - [BETA] See GitHub issues: #1658, #4168.
// - Make sure you call this after EndCombo().
// - The preview is designed to only host non-interactive elements.
// - If you use nested combos, make sure you call this right after BeginCombo() and not after EndCombo(), in order to target the correct one.
// - Not compatible with ImGuiComboFlags_WidthFitPreview.
// - 2026-09-08 (1.93.0): removed ImGuiComboFlags_CustomPreview. You can use BeginComboPreview()/EndComboPreview() without an extra flag.
bool ImGui::BeginComboPreview()