How to Make Menus Persist After Clicking a MenuItem

Hi,

I’m trying to make a view menu where you can check and uncheck the MenuItems to display/hide certain features for rendering. I would like it to be such that when you click a MenuItem to select/unselect, it doesn’t de-focus the menu right away so that you can keep on displaying/hiding other things without having to go back up and re-open the view menu again.

Here’s an example of what I want to happen using Solidworks’ view menu:

SolidworksMenuPersist

Here’s what currently happens with my code:

ImguiMenuNoPersist

Additionally, here’s the code I have so far:

if (ImGui::BeginMenu("View")) {
	if (ImGui::BeginMenu("Menu")) {
		static bool variableA = false;
		static bool variableB = false;
		static bool variableC = false;
		static bool variableD = false;
		static bool variableE = false;
		static bool variableF = false;
		static bool variableG = false;

		ImGui::MenuItem("A", "", &variableA);
		ImGui::MenuItem("B", "", &variableB);
		ImGui::MenuItem("C", "", &variableC);
		ImGui::MenuItem("D", "", &variableD);
		ImGui::MenuItem("E", "", &variableE);
		ImGui::MenuItem("F", "", &variableF);
		ImGui::MenuItem("G", "", &variableG);
		ImGui::EndMenu();
	}

	ImGui::EndMenu();
}

Is there any way to do what I want to do without having to hack in my own custom MenuItem function?

Thank you,
Chris Gomez

It isn’t exposed in the imgui.h public API but at the moment you can use imgui_internal.h and do:

ImGui::PushItemFlag(ImGuiItemFlags_SelectableDontClosePopup, true);
[...] your MenuItem calls here [...]
ImGui::PopItemFlag();

Thank you, that ended up working!