MCPcopy Create free account
hub / github.com/KebsCS/KBotExt / MenuItemEx

Method MenuItemEx

KBotExt/imgui/imgui_widgets.cpp:7357–7431  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7355}
7356
7357bool ImGui::MenuItemEx(const char* label, const char* icon, const char* shortcut, bool selected, bool enabled)
7358{
7359 ImGuiWindow* window = GetCurrentWindow();
7360 if (window->SkipItems)
7361 return false;
7362
7363 ImGuiContext& g = *GImGui;
7364 ImGuiStyle& style = g.Style;
7365 ImVec2 pos = window->DC.CursorPos;
7366 ImVec2 label_size = CalcTextSize(label, NULL, true);
7367
7368 // See BeginMenuEx() for comments about this.
7369 const bool menuset_is_open = IsRootOfOpenMenuSet();
7370 if (menuset_is_open)
7371 PushItemFlag(ImGuiItemFlags_NoWindowHoverableCheck, true);
7372
7373 // We've been using the equivalent of ImGuiSelectableFlags_SetNavIdOnHover on all Selectable() since early Nav system days (commit 43ee5d73),
7374 // but I am unsure whether this should be kept at all. For now moved it to be an opt-in feature used by menus only.
7375 bool pressed;
7376 PushID(label);
7377 if (!enabled)
7378 BeginDisabled();
7379
7380 // We use ImGuiSelectableFlags_NoSetKeyOwner to allow down on one menu item, move, up on another.
7381 const ImGuiSelectableFlags selectable_flags = ImGuiSelectableFlags_SelectOnRelease | ImGuiSelectableFlags_NoSetKeyOwner | ImGuiSelectableFlags_SetNavIdOnHover;
7382 const ImGuiMenuColumns* offsets = &window->DC.MenuColumns;
7383 if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)
7384 {
7385 // Mimic the exact layout spacing of BeginMenu() to allow MenuItem() inside a menu bar, which is a little misleading but may be useful
7386 // Note that in this situation: we don't render the shortcut, we render a highlight instead of the selected tick mark.
7387 float w = label_size.x;
7388 window->DC.CursorPos.x += IM_FLOOR(style.ItemSpacing.x * 0.5f);
7389 ImVec2 text_pos(window->DC.CursorPos.x + offsets->OffsetLabel, window->DC.CursorPos.y + window->DC.CurrLineTextBaseOffset);
7390 PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(style.ItemSpacing.x * 2.0f, style.ItemSpacing.y));
7391 pressed = Selectable("", selected, selectable_flags, ImVec2(w, 0.0f));
7392 PopStyleVar();
7393 if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Visible)
7394 RenderText(text_pos, label);
7395 window->DC.CursorPos.x += IM_FLOOR(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().
7396 }
7397 else
7398 {
7399 // Menu item inside a vertical menu
7400 // (In a typical menu window where all items are BeginMenu() or MenuItem() calls, extra_w will always be 0.0f.
7401 // Only when they are other items sticking out we're going to add spacing, yet only register minimum width into the layout system.
7402 float icon_w = (icon && icon[0]) ? CalcTextSize(icon, NULL).x : 0.0f;
7403 float shortcut_w = (shortcut && shortcut[0]) ? CalcTextSize(shortcut, NULL).x : 0.0f;
7404 float checkmark_w = IM_FLOOR(g.FontSize * 1.20f);
7405 float min_w = window->DC.MenuColumns.DeclColumns(icon_w, label_size.x, shortcut_w, checkmark_w); // Feedback for next frame
7406 float stretch_w = ImMax(0.0f, GetContentRegionAvail().x - min_w);
7407 pressed = Selectable("", false, selectable_flags | ImGuiSelectableFlags_SpanAvailWidth, ImVec2(min_w, label_size.y));
7408 if (g.LastItemData.StatusFlags & ImGuiItemStatusFlags_Visible)
7409 {
7410 RenderText(pos + ImVec2(offsets->OffsetLabel, 0.0f), label);
7411 if (icon_w > 0.0f)
7412 RenderText(pos + ImVec2(offsets->OffsetIcon, 0.0f), icon);
7413 if (shortcut_w > 0.0f)
7414 {

Callers

nothing calls this directly

Calls 5

GetCurrentWindowFunction · 0.85
IsRootOfOpenMenuSetFunction · 0.85
ImVec2Function · 0.85
ImMaxFunction · 0.85
DeclColumnsMethod · 0.80

Tested by

no test coverage detected