Skip to content

Commit 2026e3d

Browse files
committed
Menus: fixed BeginMenu() child popup position when used inside a line with a baseline offset.
1 parent 3ff8c46 commit 2026e3d

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

docs/CHANGELOG.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ Other Changes:
5151
- Textures:
5252
- Fixed a building issue when ImTextureID is defined as a struct.
5353
- Fixed displaying texture # in Metrics/Debugger window.
54-
- Menus: fixed MenuItem() label baseline when using inside a line with an offset.
54+
- Menus:
55+
- Fixed MenuItem() label position and BeginMenu() arrow/icon/popup positions,
56+
when used inside a line with a baseline offset.
5557
- Scrollbar: fixed a codepath leading to a divide-by-zero (which would not be
5658
noticeable by user but detected by sanitizers). (#9089) [@judicaelclair]
5759
- Backends:

imgui_widgets.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9192,7 +9192,8 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled)
91929192
// The reference position stored in popup_pos will be used by Begin() to find a suitable position for the child menu,
91939193
// However the final position is going to be different! It is chosen by FindBestWindowPosForPopup().
91949194
// e.g. Menus tend to overlap each other horizontally to amplify relative Z-ordering.
9195-
ImVec2 popup_pos, pos = window->DC.CursorPos;
9195+
ImVec2 popup_pos;
9196+
ImVec2 pos = window->DC.CursorPos;
91969197
PushID(label);
91979198
if (!enabled)
91989199
BeginDisabled();
@@ -9206,34 +9207,34 @@ bool ImGui::BeginMenuEx(const char* label, const char* icon, bool enabled)
92069207
// Menu inside a horizontal menu bar
92079208
// Selectable extend their highlight by half ItemSpacing in each direction.
92089209
// For ChildMenu, the popup position will be overwritten by the call to FindBestWindowPosForPopup() in Begin()
9209-
popup_pos = ImVec2(pos.x - 1.0f - IM_TRUNC(style.ItemSpacing.x * 0.5f), pos.y - style.FramePadding.y + window->MenuBarHeight);
92109210
window->DC.CursorPos.x += IM_TRUNC(style.ItemSpacing.x * 0.5f);
92119211
PushStyleVarX(ImGuiStyleVar_ItemSpacing, style.ItemSpacing.x * 2.0f);
92129212
float w = label_size.x;
9213-
ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
9213+
ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, pos.y + window->DC.CurrLineTextBaseOffset);
92149214
pressed = Selectable("", menu_is_open, selectable_flags, ImVec2(w, label_size.y));
92159215
LogSetNextTextDecoration("[", "]");
92169216
RenderText(text_pos, label);
92179217
PopStyleVar();
92189218
window->DC.CursorPos.x += IM_TRUNC(style.ItemSpacing.x * (-1.0f + 0.5f)); // -1 spacing to compensate the spacing added when Selectable() did a SameLine(). It would also work to call SameLine() ourselves after the PopStyleVar().
9219+
popup_pos = ImVec2(pos.x - 1.0f - IM_TRUNC(style.ItemSpacing.x * 0.5f), text_pos.y - style.FramePadding.y + window->MenuBarHeight);
92199220
}
92209221
else
92219222
{
92229223
// Menu inside a regular/vertical menu
92239224
// (In a typical menu window where all items are BeginMenu() or MenuItem() calls, extra_w will always be 0.0f.
92249225
// Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system.)
9225-
popup_pos = ImVec2(pos.x, pos.y - style.WindowPadding.y);
92269226
float icon_w = (icon && icon[0]) ? CalcTextSize(icon, NULL).x : 0.0f;
92279227
float checkmark_w = IM_TRUNC(g.FontSize * 1.20f);
92289228
float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, 0.0f, checkmark_w); // Feedback to next frame
92299229
float extra_w = ImMax(0.0f, GetContentRegionAvail().x - min_w);
9230-
ImVec2 text_pos(window->DC.CursorPos.x, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
9230+
ImVec2 text_pos(window->DC.CursorPos.x, pos.y + window->DC.CurrLineTextBaseOffset);
92319231
pressed = Selectable("", menu_is_open, selectable_flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, label_size.y));
92329232
LogSetNextTextDecoration("", ">");
92339233
RenderText(ImVec2(text_pos.x + offsets->OffsetLabel, text_pos.y), label);
92349234
if (icon_w > 0.0f)
92359235
RenderText(ImVec2(text_pos.x + offsets->OffsetIcon, text_pos.y), icon);
92369236
RenderArrow(window->DrawList, ImVec2(text_pos.x + offsets->OffsetMark + extra_w + g.FontSize * 0.30f, text_pos.y), GetColorU32(ImGuiCol_Text), ImGuiDir_Right);
9237+
popup_pos = ImVec2(pos.x, text_pos.y - style.WindowPadding.y);
92379238
}
92389239
if (!enabled)
92399240
EndDisabled();

0 commit comments

Comments
 (0)