Menus: Some renaming, comments, add to demo. Amend 0342a3c. (#1207)

This commit is contained in:
ocornut 2020-02-28 16:42:24 +01:00
parent 0342a3c548
commit 24bd33ace8
5 changed files with 50 additions and 15 deletions

View File

@ -3623,7 +3623,7 @@ void ImGui::NewFrame()
g.FrameCount += 1; g.FrameCount += 1;
g.TooltipOverrideCount = 0; g.TooltipOverrideCount = 0;
g.WindowsActiveCount = 0; g.WindowsActiveCount = 0;
g.RenderedMenusId.resize(0); g.MenusIdSubmittedThisFrame.resize(0);
// Setup current font and draw list shared data // Setup current font and draw list shared data
g.IO.Fonts->Locked = true; g.IO.Fonts->Locked = true;
@ -3915,6 +3915,7 @@ void ImGui::Shutdown(ImGuiContext* context)
g.ShrinkWidthBuffer.clear(); g.ShrinkWidthBuffer.clear();
g.PrivateClipboard.clear(); g.PrivateClipboard.clear();
g.MenusIdSubmittedThisFrame.clear();
g.InputTextState.ClearFreeMemory(); g.InputTextState.ClearFreeMemory();
g.SettingsWindows.clear(); g.SettingsWindows.clear();

View File

@ -559,7 +559,8 @@ namespace ImGui
// Widgets: Menus // Widgets: Menus
// - Use BeginMenuBar() on a window ImGuiWindowFlags_MenuBar to append to its menu bar. // - Use BeginMenuBar() on a window ImGuiWindowFlags_MenuBar to append to its menu bar.
// - Use BeginMainMenuBar() to create a menu bar at the top of the screen. // - Use BeginMainMenuBar() to create a menu bar at the top of the screen and append to it.
// - Use BeginMenu() to create a menu. You can call BeginMenu() multiple time with the same identifier to append more items to it.
IMGUI_API bool BeginMenuBar(); // append to menu-bar of current window (requires ImGuiWindowFlags_MenuBar flag set on parent window). IMGUI_API bool BeginMenuBar(); // append to menu-bar of current window (requires ImGuiWindowFlags_MenuBar flag set on parent window).
IMGUI_API void EndMenuBar(); // only call EndMenuBar() if BeginMenuBar() returns true! IMGUI_API void EndMenuBar(); // only call EndMenuBar() if BeginMenuBar() returns true!
IMGUI_API bool BeginMainMenuBar(); // create and append to a full screen menu-bar. IMGUI_API bool BeginMainMenuBar(); // create and append to a full screen menu-bar.

View File

@ -288,6 +288,27 @@ void ImGui::ShowDemoWindow(bool* p_open)
// Menu Bar // Menu Bar
if (ImGui::BeginMenuBar()) if (ImGui::BeginMenuBar())
{ {
if (ImGui::BeginMenu("Foo"))
{
if (ImGui::BeginMenu("AAA"))
{
ImGui::EndMenu();
}
if (ImGui::BeginMenu("BBB"))
{
ImGui::EndMenu();
}
if (ImGui::BeginMenu("AAA"))
{
ImGui::EndMenu();
}
if (ImGui::BeginMenu("BBB"))
{
ImGui::EndMenu();
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Menu")) if (ImGui::BeginMenu("Menu"))
{ {
ShowExampleMenuFile(); ShowExampleMenuFile();
@ -3580,6 +3601,7 @@ static void ShowExampleMenuFile()
} }
if (ImGui::MenuItem("Save", "Ctrl+S")) {} if (ImGui::MenuItem("Save", "Ctrl+S")) {}
if (ImGui::MenuItem("Save As..")) {} if (ImGui::MenuItem("Save As..")) {}
ImGui::Separator(); ImGui::Separator();
if (ImGui::BeginMenu("Options")) if (ImGui::BeginMenu("Options"))
{ {
@ -3591,13 +3613,12 @@ static void ShowExampleMenuFile()
ImGui::EndChild(); ImGui::EndChild();
static float f = 0.5f; static float f = 0.5f;
static int n = 0; static int n = 0;
static bool b = true;
ImGui::SliderFloat("Value", &f, 0.0f, 1.0f); ImGui::SliderFloat("Value", &f, 0.0f, 1.0f);
ImGui::InputFloat("Input", &f, 0.1f); ImGui::InputFloat("Input", &f, 0.1f);
ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0"); ImGui::Combo("Combo", &n, "Yes\0No\0Maybe\0\0");
ImGui::Checkbox("Check", &b);
ImGui::EndMenu(); ImGui::EndMenu();
} }
if (ImGui::BeginMenu("Colors")) if (ImGui::BeginMenu("Colors"))
{ {
float sz = ImGui::GetTextLineHeight(); float sz = ImGui::GetTextLineHeight();
@ -3612,6 +3633,17 @@ static void ShowExampleMenuFile()
} }
ImGui::EndMenu(); ImGui::EndMenu();
} }
// Here we demonstrate appending again to the "Options" menu (which we already created above)
// Of course in this demo it is a little bit silly that this function calls BeginMenu("Options") twice.
// In a real code-base using it would make senses to use this feature from very different code locations.
if (ImGui::BeginMenu("Options")) // <-- Append!
{
static bool b = true;
ImGui::Checkbox("SomeOption", &b);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Disabled", false)) // Disabled if (ImGui::BeginMenu("Disabled", false)) // Disabled
{ {
IM_ASSERT(0); IM_ASSERT(0);

View File

@ -1159,7 +1159,7 @@ struct ImGuiContext
float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage?
int TooltipOverrideCount; int TooltipOverrideCount;
ImVector<char> PrivateClipboard; // If no custom clipboard handler is defined ImVector<char> PrivateClipboard; // If no custom clipboard handler is defined
ImVector<ImGuiID> RenderedMenusId; // A list of menu IDs that were rendered at least once ImVector<ImGuiID> MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once
// Platform support // Platform support
ImVec2 PlatformImePos; // Cursor position request & last passed to the OS Input Method Editor ImVec2 PlatformImePos; // Cursor position request & last passed to the OS Input Method Editor

View File

@ -6223,25 +6223,26 @@ bool ImGui::BeginMenu(const char* label, bool enabled)
const ImGuiStyle& style = g.Style; const ImGuiStyle& style = g.Style;
const ImGuiID id = window->GetID(label); const ImGuiID id = window->GetID(label);
bool menu_is_open = IsPopupOpen(id); bool menu_is_open = IsPopupOpen(id);
ImGuiWindowFlags flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus;
// Sub-menus are ChildWindow so that mouse can be hovering across them (otherwise top-most popup menu would steal focus and not allow hovering on parent menu) // Sub-menus are ChildWindow so that mouse can be hovering across them (otherwise top-most popup menu would steal focus and not allow hovering on parent menu)
if (window->Flags & (ImGuiWindowFlags_Popup|ImGuiWindowFlags_ChildMenu)) ImGuiWindowFlags flags = ImGuiWindowFlags_ChildMenu | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoNavFocus;
if (window->Flags & (ImGuiWindowFlags_Popup | ImGuiWindowFlags_ChildMenu))
flags |= ImGuiWindowFlags_ChildWindow; flags |= ImGuiWindowFlags_ChildWindow;
if (g.RenderedMenusId.contains(id)) // If a menu with same the ID was already submitted, we will append to it, matching the behavior of Begin().
// We are relying on a O(N) search - so O(N log N) over the frame - which seems like the most efficient for the expected small amount of BeginMenu() calls per frame.
// If somehow this is ever becoming a problem we can switch to use e.g. a ImGuiStorager mapping key to last frame used.
if (g.MenusIdSubmittedThisFrame.contains(id))
{ {
// Menu with same ID was already created - append to it.
if (menu_is_open) if (menu_is_open)
menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display) menu_is_open = BeginPopupEx(id, flags); // menu_is_open can be 'false' when the popup is completely clipped (e.g. zero size display)
if (!menu_is_open) else
g.NextWindowData.ClearFlags(); // We behave like Begin() and need to consume those values g.NextWindowData.ClearFlags(); // we behave like Begin() and need to consume those values
return menu_is_open; return menu_is_open;
} }
else
{ // Tag menu as used. Next time BeginMenu() with same ID is called it will append to existing menu
g.RenderedMenusId.push_back(id); // Tag menu as used. Next time BeginMenu() with same ID is called it will append to existing menu. g.MenusIdSubmittedThisFrame.push_back(id);
}
ImVec2 label_size = CalcTextSize(label, NULL, true); ImVec2 label_size = CalcTextSize(label, NULL, true);
bool pressed; bool pressed;