Skip to content

Commit 5be5add

Browse files
committed
Selectable: Removed extraneous WindowPadding.x worth of width when auto-sized selectable label goes off available width (would not be noticeable) + Renamed ImGuiSelectableFlags_DrawFillAvailWidth to ImGuiSelectableFlags_SpanAvailWidth.
1 parent b4d1287 commit 5be5add

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

imgui_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ enum ImGuiSelectableFlagsPrivate_
494494
ImGuiSelectableFlags_NoHoldingActiveID = 1 << 20,
495495
ImGuiSelectableFlags_SelectOnClick = 1 << 21, // Override button behavior to react on Click (default is Click+Release)
496496
ImGuiSelectableFlags_SelectOnRelease = 1 << 22, // Override button behavior to react on Release (default is Click+Release)
497-
ImGuiSelectableFlags_DrawFillAvailWidth = 1 << 23, // Draw over all avail width even if we declared less for layout. FIXME: We may be able to remove this (added in 6251d379, 2bcafc86 for menus)
497+
ImGuiSelectableFlags_SpanAvailWidth = 1 << 23, // Span all avail width even if we declared less for layout purpose. FIXME: We may be able to remove this (added in 6251d379, 2bcafc86 for menus)
498498
ImGuiSelectableFlags_DrawHoveredWhenHeld= 1 << 24, // Always show active when held, even is not hovered. This concept could probably be renamed/formalized somehow.
499499
ImGuiSelectableFlags_SetNavIdOnHover = 1 << 25
500500
};

imgui_widgets.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5570,21 +5570,21 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
55705570
if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns) // FIXME-OPT: Avoid if vertically clipped.
55715571
PushColumnsBackground();
55725572

5573+
// Submit label or explicit size to ItemSize(), whereas ItemAdd() will submit a larger/spanning rectangle.
55735574
ImGuiID id = window->GetID(label);
55745575
ImVec2 label_size = CalcTextSize(label, NULL, true);
55755576
ImVec2 size(size_arg.x != 0.0f ? size_arg.x : label_size.x, size_arg.y != 0.0f ? size_arg.y : label_size.y);
55765577
ImVec2 pos = window->DC.CursorPos;
55775578
pos.y += window->DC.CurrLineTextBaseOffset;
5578-
ImRect bb_inner(pos, pos + size);
55795579
ItemSize(size, 0.0f);
55805580

55815581
// Fill horizontal space
55825582
const float max_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? GetWindowContentRegionMax().x + window->Pos.x : GetContentRegionMaxAbs().x;
5583-
if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_DrawFillAvailWidth))
5584-
size.x = ImMax(label_size.x, max_x - window->WindowPadding.x - pos.x) + window->WindowPadding.x;
5583+
if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_SpanAvailWidth))
5584+
size.x = ImMax(label_size.x, max_x - pos.x);
55855585
ImRect bb_align(pos, pos + size);
55865586

5587-
// Selectables are meant to be tightly packed together with no click-gap, so we extend the box to cover spacing between selectable.
5587+
// Selectables are meant to be tightly packed together with no click-gap, so we extend their box to cover spacing between selectable.
55885588
ImRect bb_enlarged = bb_align;
55895589
const float spacing_x = style.ItemSpacing.x;
55905590
const float spacing_y = style.ItemSpacing.y;
@@ -5594,6 +5594,8 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
55945594
bb_enlarged.Min.y -= spacing_U;
55955595
bb_enlarged.Max.x += (spacing_x - spacing_L);
55965596
bb_enlarged.Max.y += (spacing_y - spacing_U);
5597+
//if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb_align.Min, bb_align.Max, IM_COL32(255, 0, 0, 255)); }
5598+
//if (g.IO.KeyCtrl) { GetForegroundDrawList()->AddRect(bb_enlarged.Min, bb_enlarged.Max, IM_COL32(0, 255, 0, 255)); }
55975599

55985600
bool item_add;
55995601
if (flags & ImGuiSelectableFlags_Disabled)
@@ -5670,7 +5672,6 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
56705672
if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(window->DC.ItemFlags & ImGuiItemFlags_SelectableDontClosePopup))
56715673
CloseCurrentPopup();
56725674

5673-
//if (g.IO.KeyCtrl) { window->DrawList->AddRect(bb_align.Min, bb_align.Max, IM_COL32(0, 255, 0, 255)); }
56745675
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags);
56755676
return pressed;
56765677
}
@@ -6229,7 +6230,7 @@ bool ImGui::BeginMenu(const char* label, bool enabled)
62296230
popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y);
62306231
float min_w = window->DC.MenuColumns.DeclColumns(label_size.x, 0.0f, IM_FLOOR(g.FontSize * 1.20f)); // Feedback to next frame
62316232
float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w);
6232-
pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_DrawFillAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(min_w, 0.0f));
6233+
pressed = Selectable(label, menu_is_open, ImGuiSelectableFlags_NoHoldingActiveID | ImGuiSelectableFlags_SelectOnClick | ImGuiSelectableFlags_DontClosePopups | ImGuiSelectableFlags_SpanAvailWidth | (!enabled ? ImGuiSelectableFlags_Disabled : 0), ImVec2(min_w, 0.0f));
62336234
ImU32 text_col = GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled);
62346235
RenderArrow(window->DrawList, pos + ImVec2(window->DC.MenuColumns.Pos[2] + extra_w + g.FontSize * 0.30f, 0.0f), text_col, ImGuiDir_Right);
62356236
}
@@ -6380,7 +6381,7 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, boo
63806381
float shortcut_w = shortcut ? CalcTextSize(shortcut, NULL).x : 0.0f;
63816382
float min_w = window->DC.MenuColumns.DeclColumns(label_size.x, shortcut_w, IM_FLOOR(g.FontSize * 1.20f)); // Feedback for next frame
63826383
float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w);
6383-
pressed = Selectable(label, false, flags | ImGuiSelectableFlags_DrawFillAvailWidth, ImVec2(min_w, 0.0f));
6384+
pressed = Selectable(label, false, flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, 0.0f));
63846385
if (shortcut_w > 0.0f)
63856386
{
63866387
PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);

0 commit comments

Comments
 (0)