Skip to content

Commit feddcf3

Browse files
committed
Combo: amends for ImGuiComboFlags_WidthFitPreview. (ocornut#6881)
Amend 112d8fc
1 parent 112d8fc commit feddcf3

File tree

4 files changed

+9
-5
lines changed

4 files changed

+9
-5
lines changed

docs/CHANGELOG.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Other changes:
118118
be accepted by the widget when navigation highlight is visible. (#6802, #3092, #5759, #787)
119119
- BeginGroup(): Fixed a bug pushing line lower extent too far down when called after a call
120120
to SameLine() followed by manual cursor manipulation.
121+
- BeginCombo(): Added ImGuiComboFlags_WidthFitPreview flag. (#6881) [@mpv-enjoyer]
121122
- BeginListBox(): Fixed not consuming SetNextWindowXXX data when returning false.
122123
- Menus: Fixed a bug where activating an item in a child-menu and dragging mouse over the
123124
parent-menu would erroneously close the child-menu. (Regression from 1.88). (#6869)

imgui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,7 +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
1118+
ImGuiComboFlags_WidthFitPreview = 1 << 7, // Width dynamically calculated from preview contents
11191119
ImGuiComboFlags_HeightMask_ = ImGuiComboFlags_HeightSmall | ImGuiComboFlags_HeightRegular | ImGuiComboFlags_HeightLarge | ImGuiComboFlags_HeightLargest,
11201120
};
11211121

imgui_demo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,11 +1190,12 @@ 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);
11941193
if (ImGui::CheckboxFlags("ImGuiComboFlags_NoArrowButton", &flags, ImGuiComboFlags_NoArrowButton))
11951194
flags &= ~ImGuiComboFlags_NoPreview; // Clear the other flag, as we cannot combine both
11961195
if (ImGui::CheckboxFlags("ImGuiComboFlags_NoPreview", &flags, ImGuiComboFlags_NoPreview))
1197-
flags &= ~ImGuiComboFlags_NoArrowButton; // Clear the other flag, as we cannot combine both
1196+
flags &= ~(ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_WidthFitPreview); // Clear the other flag, as we cannot combine both
1197+
if (ImGui::CheckboxFlags("ImGuiComboFlags_WidthFitPreview", &flags, ImGuiComboFlags_WidthFitPreview))
1198+
flags &= ~ImGuiComboFlags_NoPreview;
11981199

11991200
// Using the generic BeginCombo() API, you have full control over how to display the combo contents.
12001201
// (your selection data could be an index, a pointer to the object, an id for the object, a flag intrusively

imgui_widgets.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,11 +1683,13 @@ bool ImGui::BeginCombo(const char* label, const char* preview_value, ImGuiComboF
16831683
const ImGuiStyle& style = g.Style;
16841684
const ImGuiID id = window->GetID(label);
16851685
IM_ASSERT((flags & (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)) != (ImGuiComboFlags_NoArrowButton | ImGuiComboFlags_NoPreview)); // Can't use both flags together
1686+
if (flags & ImGuiComboFlags_WidthFitPreview)
1687+
IM_ASSERT((flags & (ImGuiComboFlags_NoPreview | ImGuiComboFlags_CustomPreview)) == 0);
16861688

16871689
const float arrow_size = (flags & ImGuiComboFlags_NoArrowButton) ? 0.0f : GetFrameHeight();
16881690
const ImVec2 label_size = CalcTextSize(label, NULL, true);
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());
1691+
const float preview_width = ((flags & ImGuiComboFlags_WidthFitPreview) && (preview_value != NULL)) ? CalcTextSize(preview_value, NULL, true).x : 0.0f;
1692+
const float w = (flags & ImGuiComboFlags_NoPreview) ? arrow_size : ((flags & ImGuiComboFlags_WidthFitPreview) ? (arrow_size + preview_width + style.FramePadding.x * 2.0f) : CalcItemWidth());
16911693
const ImRect bb(window->DC.CursorPos, window->DC.CursorPos + ImVec2(w, label_size.y + style.FramePadding.y * 2.0f));
16921694
const ImRect total_bb(bb.Min, bb.Max + ImVec2(label_size.x > 0.0f ? style.ItemInnerSpacing.x + label_size.x : 0.0f, 0.0f));
16931695
ItemSize(total_bb, style.FramePadding.y);

0 commit comments

Comments
 (0)