Skip to content

Commit 7c11997

Browse files
committed
Selectable: Fixed honoring style.SelectableTextAlign with unspecified size. (ocornut#2347, ocornut#2601)
1 parent ac2247f commit 7c11997

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

docs/CHANGELOG.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Other Changes:
4545
when the menu is not open. (#3030)
4646
- InputText: Fixed password fields displaying ASCII spaces as blanks instead of using the '*'
4747
glyph. (#2149, #515)
48+
- Selectable: Fixed honoring style.SelectableTextAlign with unspecified size. (#2347, #2601)
4849
- Scrolling: Fixed scrolling centering API leading to non-integer scrolling values and initial
4950
cursor position. This would often get fixed after the fix item submission, but using the
5051
ImGuiListClipper as the first thing after Begin() could largely break size calculations. (#3073)

docs/TODO.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ It's mostly a bunch of personal notes, probably incomplete. Feel free to query i
223223
- menus: menu-bar: main menu-bar could affect clamping of windows position (~ akin to modifying DisplayMin)
224224
- menus: hovering from menu to menu on a menu-bar has 1 frame without any menu, which is a little annoying. ideally either 0 either longer.
225225
- menus: could merge draw call in most cases (how about storing an optional aabb in ImDrawCmd to move the burden of merging in a single spot).
226+
- menus: would be nice if the Selectable() supported horizontal alignment (must be given the equivalent of WorkRect.Max.x matching the position of the shortcut column)
226227

227228
- tree node: add treenode/treepush int variants? not there because (void*) cast from int warns on some platforms/settings?
228229
- tree node: try to apply scrolling at time of TreePop() if node was just opened and end of node is past scrolling limits?

imgui_widgets.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5583,31 +5583,33 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
55835583
float max_x = (flags & ImGuiSelectableFlags_SpanAllColumns) ? GetWindowContentRegionMax().x + window->Pos.x : GetContentRegionMaxAbs().x;
55845584
float w_draw = ImMax(label_size.x, max_x - window_padding.x - pos.x);
55855585
ImVec2 size_draw((size_arg.x != 0 && !(flags & ImGuiSelectableFlags_DrawFillAvailWidth)) ? size_arg.x : w_draw, size.y);
5586-
ImRect bb(pos, pos + size_draw);
5586+
5587+
ImRect bb_align(pos, pos + size_draw);
55875588
if (size_arg.x == 0.0f || (flags & ImGuiSelectableFlags_DrawFillAvailWidth))
5588-
bb.Max.x += window_padding.x;
5589+
bb_align.Max.x += window_padding.x;
55895590

55905591
// Selectables are meant to be tightly packed together with no click-gap, so we extend the box to cover spacing between selectable.
5592+
ImRect bb_enlarged = bb_align;
55915593
const float spacing_x = style.ItemSpacing.x;
55925594
const float spacing_y = style.ItemSpacing.y;
55935595
const float spacing_L = IM_FLOOR(spacing_x * 0.50f);
55945596
const float spacing_U = IM_FLOOR(spacing_y * 0.50f);
5595-
bb.Min.x -= spacing_L;
5596-
bb.Min.y -= spacing_U;
5597-
bb.Max.x += (spacing_x - spacing_L);
5598-
bb.Max.y += (spacing_y - spacing_U);
5597+
bb_enlarged.Min.x -= spacing_L;
5598+
bb_enlarged.Min.y -= spacing_U;
5599+
bb_enlarged.Max.x += (spacing_x - spacing_L);
5600+
bb_enlarged.Max.y += (spacing_y - spacing_U);
55995601

56005602
bool item_add;
56015603
if (flags & ImGuiSelectableFlags_Disabled)
56025604
{
56035605
ImGuiItemFlags backup_item_flags = window->DC.ItemFlags;
56045606
window->DC.ItemFlags |= ImGuiItemFlags_Disabled | ImGuiItemFlags_NoNavDefaultFocus;
5605-
item_add = ItemAdd(bb, id);
5607+
item_add = ItemAdd(bb_enlarged, id);
56065608
window->DC.ItemFlags = backup_item_flags;
56075609
}
56085610
else
56095611
{
5610-
item_add = ItemAdd(bb, id);
5612+
item_add = ItemAdd(bb_enlarged, id);
56115613
}
56125614
if (!item_add)
56135615
{
@@ -5630,7 +5632,7 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
56305632

56315633
const bool was_selected = selected;
56325634
bool hovered, held;
5633-
bool pressed = ButtonBehavior(bb, id, &hovered, &held, button_flags);
5635+
bool pressed = ButtonBehavior(bb_enlarged, id, &hovered, &held, button_flags);
56345636

56355637
// Update NavId when clicking or when Hovering (this doesn't happen on most widgets), so navigation can be resumed with gamepad/keyboard
56365638
if (pressed || (hovered && (flags & ImGuiSelectableFlags_SetNavIdOnHover)))
@@ -5657,21 +5659,22 @@ bool ImGui::Selectable(const char* label, bool selected, ImGuiSelectableFlags fl
56575659
if (hovered || selected)
56585660
{
56595661
const ImU32 col = GetColorU32((held && hovered) ? ImGuiCol_HeaderActive : hovered ? ImGuiCol_HeaderHovered : ImGuiCol_Header);
5660-
RenderFrame(bb.Min, bb.Max, col, false, 0.0f);
5661-
RenderNavHighlight(bb, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding);
5662+
RenderFrame(bb_enlarged.Min, bb_enlarged.Max, col, false, 0.0f);
5663+
RenderNavHighlight(bb_enlarged, id, ImGuiNavHighlightFlags_TypeThin | ImGuiNavHighlightFlags_NoRounding);
56625664
}
56635665

56645666
if ((flags & ImGuiSelectableFlags_SpanAllColumns) && window->DC.CurrentColumns)
56655667
PopColumnsBackground();
56665668

56675669
if (flags & ImGuiSelectableFlags_Disabled) PushStyleColor(ImGuiCol_Text, style.Colors[ImGuiCol_TextDisabled]);
5668-
RenderTextClipped(bb_inner.Min, bb_inner.Max, label, NULL, &label_size, style.SelectableTextAlign, &bb);
5670+
RenderTextClipped(bb_align.Min, bb_align.Max, label, NULL, &label_size, style.SelectableTextAlign, &bb_enlarged);
56695671
if (flags & ImGuiSelectableFlags_Disabled) PopStyleColor();
56705672

56715673
// Automatically close popups
56725674
if (pressed && (window->Flags & ImGuiWindowFlags_Popup) && !(flags & ImGuiSelectableFlags_DontClosePopups) && !(window->DC.ItemFlags & ImGuiItemFlags_SelectableDontClosePopup))
56735675
CloseCurrentPopup();
56745676

5677+
//if (g.IO.KeyCtrl) { window->DrawList->AddRect(bb_align.Min, bb_align.Max, IM_COL32(0, 255, 0, 255)); }
56755678
IMGUI_TEST_ENGINE_ITEM_INFO(id, label, window->DC.ItemFlags);
56765679
return pressed;
56775680
}

0 commit comments

Comments
 (0)