BeginComboPreview: set/restore WorkRect.Max.x. (#1658, #4168)

This commit is contained in:
ocornut 2026-09-08 17:33:42 +02:00
parent 148d128f6e
commit 32c3f6095a
2 changed files with 12 additions and 3 deletions

View File

@ -1192,6 +1192,8 @@ struct IMGUI_API ImGuiComboPreviewData
ImVec2 BackupCursorMaxPos; ImVec2 BackupCursorMaxPos;
ImVec2 BackupCursorPosPrevLine; ImVec2 BackupCursorPosPrevLine;
float BackupPrevLineTextBaseOffset; float BackupPrevLineTextBaseOffset;
float BackupWorkRectMaxX;
float BackupContentRectMaxX;
ImGuiLayoutType BackupLayout; ImGuiLayoutType BackupLayout;
ImGuiComboPreviewData() { memset((void*)this, 0, sizeof(*this)); } ImGuiComboPreviewData() { memset((void*)this, 0, sizeof(*this)); }

View File

@ -2093,8 +2093,10 @@ void ImGui::EndCombo()
EndPopup(); EndPopup();
} }
// Call directly after the BeginCombo/EndCombo block. The preview is designed to only host non-interactive elements // Call directly after the BeginCombo() call. If you use nested combo, make sure you end wait for EndCombo() to call this!
// (Experimental, see GitHub issues: #1658, #4168) // The preview is designed to only host non-interactive elements.
// - [BETA] See GitHub issues: #1658, #4168.
// - Not compatible with ImGuiComboFlags_WidthFitPreview.
bool ImGui::BeginComboPreview() bool ImGui::BeginComboPreview()
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
@ -2113,6 +2115,9 @@ bool ImGui::BeginComboPreview()
preview_data->BackupCursorPosPrevLine = window->DC.CursorPosPrevLine; preview_data->BackupCursorPosPrevLine = window->DC.CursorPosPrevLine;
preview_data->BackupPrevLineTextBaseOffset = window->DC.PrevLineTextBaseOffset; preview_data->BackupPrevLineTextBaseOffset = window->DC.PrevLineTextBaseOffset;
preview_data->BackupLayout = window->DC.LayoutType; preview_data->BackupLayout = window->DC.LayoutType;
preview_data->BackupWorkRectMaxX = window->WorkRect.Max.x;
preview_data->BackupContentRectMaxX = window->ContentRegionRect.Max.x;
window->WorkRect.Max.x = window->ContentRegionRect.Max.x = preview_data->PreviewRect.Max.x - g.Style.FramePadding.x;
window->DC.CursorPos = preview_data->PreviewRect.Min + g.Style.FramePadding; window->DC.CursorPos = preview_data->PreviewRect.Min + g.Style.FramePadding;
window->DC.CursorMaxPos = window->DC.CursorPos; window->DC.CursorMaxPos = window->DC.CursorPos;
window->DC.LayoutType = ImGuiLayoutType_Horizontal; window->DC.LayoutType = ImGuiLayoutType_Horizontal;
@ -2138,9 +2143,11 @@ void ImGui::EndComboPreview()
} }
PopClipRect(); PopClipRect();
window->DC.CursorPos = preview_data->BackupCursorPos; window->DC.CursorPos = preview_data->BackupCursorPos;
window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, preview_data->BackupCursorMaxPos); window->DC.CursorMaxPos = ImMax(window->DC.CursorMaxPos, preview_data->BackupCursorMaxPos); // No need to do the same with IdealMaxPos
window->DC.CursorPosPrevLine = preview_data->BackupCursorPosPrevLine; window->DC.CursorPosPrevLine = preview_data->BackupCursorPosPrevLine;
window->DC.PrevLineTextBaseOffset = preview_data->BackupPrevLineTextBaseOffset; window->DC.PrevLineTextBaseOffset = preview_data->BackupPrevLineTextBaseOffset;
window->WorkRect.Max.x = preview_data->BackupWorkRectMaxX;
window->ContentRegionRect.Max.x = preview_data->BackupContentRectMaxX;
window->DC.LayoutType = preview_data->BackupLayout; window->DC.LayoutType = preview_data->BackupLayout;
window->DC.IsSameLine = false; window->DC.IsSameLine = false;
preview_data->PreviewRect = ImRect(); preview_data->PreviewRect = ImRect();