mirror of
				https://github.com/Drezil/imgui.git
				synced 2025-11-03 22:51:06 +01:00 
			
		
		
		
	MenuItem(): Tweaks to mimic exact spacing of BeginMenu() when inside a menu bar, which is a little misleading imho but may be useful. (#1387)
This commit is contained in:
		
							
								
								
									
										24
									
								
								imgui.cpp
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								imgui.cpp
									
									
									
									
									
								
							@@ -9077,24 +9077,38 @@ bool ImGui::MenuItem(const char* label, const char* shortcut, bool selected, boo
 | 
				
			|||||||
        return false;
 | 
					        return false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ImGuiContext& g = *GImGui;
 | 
					    ImGuiContext& g = *GImGui;
 | 
				
			||||||
 | 
					    ImGuiStyle& style = g.Style;
 | 
				
			||||||
    ImVec2 pos = window->DC.CursorPos;
 | 
					    ImVec2 pos = window->DC.CursorPos;
 | 
				
			||||||
    ImVec2 label_size = CalcTextSize(label, NULL, true);
 | 
					    ImVec2 label_size = CalcTextSize(label, NULL, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ImGuiSelectableFlags flags = ImGuiSelectableFlags_MenuItem | (enabled ? 0 : ImGuiSelectableFlags_Disabled);
 | 
				
			||||||
 | 
					    bool pressed;
 | 
				
			||||||
 | 
					    if (window->DC.LayoutType == ImGuiLayoutType_Horizontal)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        // Mimic the exact layout spacing of BeginMenu() to allow MenuItem() inside a menu bar, which is a little misleading but may be useful
 | 
				
			||||||
 | 
					        // Note that in this situation we render neither the shortcut neither the selected tick mark
 | 
				
			||||||
 | 
					        float w = label_size.x;
 | 
				
			||||||
 | 
					        window->DC.CursorPos.x += (float)(int)(style.ItemSpacing.x * 0.5f);
 | 
				
			||||||
 | 
					        PushStyleVar(ImGuiStyleVar_ItemSpacing, style.ItemSpacing * 2.0f);
 | 
				
			||||||
 | 
					        pressed = Selectable(label, false, flags, ImVec2(w, 0.0f));
 | 
				
			||||||
 | 
					        PopStyleVar();
 | 
				
			||||||
 | 
					        window->DC.CursorPos.x += (float)(int)(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().
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
        ImVec2 shortcut_size = shortcut ? CalcTextSize(shortcut, NULL) : ImVec2(0.0f, 0.0f);
 | 
					        ImVec2 shortcut_size = shortcut ? CalcTextSize(shortcut, NULL) : ImVec2(0.0f, 0.0f);
 | 
				
			||||||
        float w = window->MenuColumns.DeclColumns(label_size.x, shortcut_size.x, (float)(int)(g.FontSize * 1.20f)); // Feedback for next frame
 | 
					        float w = window->MenuColumns.DeclColumns(label_size.x, shortcut_size.x, (float)(int)(g.FontSize * 1.20f)); // Feedback for next frame
 | 
				
			||||||
        float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w);
 | 
					        float extra_w = ImMax(0.0f, GetContentRegionAvail().x - w);
 | 
				
			||||||
 | 
					        pressed = Selectable(label, false, flags | ImGuiSelectableFlags_DrawFillAvailWidth, ImVec2(w, 0.0f));
 | 
				
			||||||
    ImGuiSelectableFlags flags = ImGuiSelectableFlags_MenuItem | (enabled ? 0 : ImGuiSelectableFlags_Disabled) | (window->DC.LayoutType == ImGuiLayoutType_Vertical ? ImGuiSelectableFlags_DrawFillAvailWidth : 0);
 | 
					 | 
				
			||||||
    bool pressed = Selectable(label, false, flags, ImVec2(w, 0.0f));
 | 
					 | 
				
			||||||
        if (shortcut_size.x > 0.0f)
 | 
					        if (shortcut_size.x > 0.0f)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);
 | 
					            PushStyleColor(ImGuiCol_Text, g.Style.Colors[ImGuiCol_TextDisabled]);
 | 
				
			||||||
            RenderText(pos + ImVec2(window->MenuColumns.Pos[1] + extra_w, 0.0f), shortcut, NULL, false);
 | 
					            RenderText(pos + ImVec2(window->MenuColumns.Pos[1] + extra_w, 0.0f), shortcut, NULL, false);
 | 
				
			||||||
            PopStyleColor();
 | 
					            PopStyleColor();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (selected)
 | 
					        if (selected)
 | 
				
			||||||
            RenderCheckMark(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * (0.20f+0.200f), g.FontSize * 0.134f * 0.5f), GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled), g.FontSize  * 0.866f);
 | 
					            RenderCheckMark(pos + ImVec2(window->MenuColumns.Pos[2] + extra_w + g.FontSize * (0.20f+0.200f), g.FontSize * 0.134f * 0.5f), GetColorU32(enabled ? ImGuiCol_Text : ImGuiCol_TextDisabled), g.FontSize  * 0.866f);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
    return pressed;
 | 
					    return pressed;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user