Skip to content

Commit 112d8fc

Browse files
mpv-enjoyerocornut
authored andcommitted
Combo: added ImGuiComboFlags_WidthFitPreview. (ocornut#6881)
1 parent 001f102 commit 112d8fc

File tree

3 files changed

+4
-1
lines changed

3 files changed

+4
-1
lines changed

imgui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ enum ImGuiComboFlags_
11151115
ImGuiComboFlags_HeightLargest = 1 << 4, // As many fitting items as possible
11161116
ImGuiComboFlags_NoArrowButton = 1 << 5, // Display on the preview box without the square arrow button
11171117
ImGuiComboFlags_NoPreview = 1 << 6, // Display only a square arrow button
1118+
ImGuiComboFlags_WidthFitPreview = 1 << 7, // Dynamic width depending on current selected element
11181119
ImGuiComboFlags_HeightMask_ = ImGuiComboFlags_HeightSmall | ImGuiComboFlags_HeightRegular | ImGuiComboFlags_HeightLarge | ImGuiComboFlags_HeightLargest,
11191120
};
11201121

imgui_demo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,7 @@ static void ShowDemoWindowWidgets()
11901190
static ImGuiComboFlags flags = 0;
11911191
ImGui::CheckboxFlags("ImGuiComboFlags_PopupAlignLeft", &flags, ImGuiComboFlags_PopupAlignLeft);
11921192
ImGui::SameLine(); HelpMarker("Only makes a difference if the popup is larger than the combo");
1193+
ImGui::CheckboxFlags("ImGuiComboFlags_WidthFitPreview", &flags, ImGuiComboFlags_WidthFitPreview);
11931194
if (ImGui::CheckboxFlags("ImGuiComboFlags_NoArrowButton", &flags, ImGuiComboFlags_NoArrowButton))
11941195
flags &= ~ImGuiComboFlags_NoPreview; // Clear the other flag, as we cannot combine both
11951196
if (ImGui::CheckboxFlags("ImGuiComboFlags_NoPreview", &flags, ImGuiComboFlags_NoPreview))

imgui_widgets.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1686,7 +1686,8 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF
16861686

16871687
const float arrow_size = (flags & ImGuiComboFlags_NoArrowButton) ? 0.0f : GetFrameHeight();
16881688
const ImVec2 label_size = CalcTextSize(label, NULL, true);
1689-
const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : CalcItemWidth();
1689+
const float preview_width = (preview_value != nullptr) ? CalcTextSize(preview_value, NULL, true).x : 0.0f;
1690+
const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : ((flags & ImGuiComboFlags_WidthFitPreview) ? arrow_size + preview_width + style.ItemInnerSpacing.x * 2.0f : CalcItemWidth());
16901691
const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f));
16911692
const ImRect total_bb(bb.Min, bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
16921693
ItemSize(total_bb, style.FramePadding.y);

0 commit comments

Comments
 (0)