Skip to content

Commit 168ef39

Browse files
committed
Demo: moved menu bar code to its own function.
1 parent 57eea67 commit 168ef39

File tree

1 file changed

+70
-60
lines changed

1 file changed

+70
-60
lines changed

imgui_demo.cpp

+70-60
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Index of this file:
7272
// [SECTION] Helpers
7373
// [SECTION] Helpers: ExampleTreeNode, ExampleMemberInfo (for use by Property Editor & Multi-Select demos)
7474
// [SECTION] Demo Window / ShowDemoWindow()
75+
// [SECTION] ShowDemoWindowMenuBar()
7576
// [SECTION] ShowDemoWindowWidgets()
7677
// [SECTION] ShowDemoWindowMultiSelect()
7778
// [SECTION] ShowDemoWindowLayout()
@@ -217,6 +218,7 @@ static void ShowExampleMenuFile();
217218
// We split the contents of the big ShowDemoWindow() function into smaller functions
218219
// (because the link time of very large functions tends to grow non-linearly)
219220
struct DemoWindowData;
221+
static void ShowDemoWindowMenuBar(DemoWindowData* demo_data);
220222
static void ShowDemoWindowWidgets(DemoWindowData* demo_data);
221223
static void ShowDemoWindowMultiSelect(DemoWindowData* demo_data);
222224
static void ShowDemoWindowLayout();
@@ -443,68 +445,11 @@ void ImGui::ShowDemoWindow(bool* p_open)
443445
}
444446

445447
// Most "big" widgets share a common width settings by default. See 'Demo->Layout->Widgets Width' for details.
446-
// e.g. Use 2/3 of the space for widgets and 1/3 for labels (right align)
447-
//ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f);
448-
// e.g. Leave a fixed amount of width for labels (by passing a negative value), the rest goes to widgets.
449-
ImGui::PushItemWidth(ImGui::GetFontSize() * -12);
448+
ImGui::PushItemWidth(ImGui::GetFontSize() * -12); // e.g. Leave a fixed amount of width for labels (by passing a negative value), the rest goes to widgets.
449+
//ImGui::PushItemWidth(-ImGui::GetWindowWidth() * 0.35f); // e.g. Use 2/3 of the space for widgets and 1/3 for labels (right align)
450450

451451
// Menu Bar
452-
if (ImGui::BeginMenuBar())
453-
{
454-
if (ImGui::BeginMenu("Menu"))
455-
{
456-
IMGUI_DEMO_MARKER("Menu/File");
457-
ShowExampleMenuFile();
458-
ImGui::EndMenu();
459-
}
460-
if (ImGui::BeginMenu("Examples"))
461-
{
462-
IMGUI_DEMO_MARKER("Menu/Examples");
463-
ImGui::MenuItem("Main menu bar", NULL, &demo.ShowMainMenuBar);
464-
465-
ImGui::SeparatorText("Mini apps");
466-
ImGui::MenuItem("Assets Browser", NULL, &demo.ShowAppAssetsBrowser);
467-
ImGui::MenuItem("Console", NULL, &demo.ShowAppConsole);
468-
ImGui::MenuItem("Custom rendering", NULL, &demo.ShowAppCustomRendering);
469-
ImGui::MenuItem("Documents", NULL, &demo.ShowAppDocuments);
470-
ImGui::MenuItem("Log", NULL, &demo.ShowAppLog);
471-
ImGui::MenuItem("Property editor", NULL, &demo.ShowAppPropertyEditor);
472-
ImGui::MenuItem("Simple layout", NULL, &demo.ShowAppLayout);
473-
ImGui::MenuItem("Simple overlay", NULL, &demo.ShowAppSimpleOverlay);
474-
475-
ImGui::SeparatorText("Concepts");
476-
ImGui::MenuItem("Auto-resizing window", NULL, &demo.ShowAppAutoResize);
477-
ImGui::MenuItem("Constrained-resizing window", NULL, &demo.ShowAppConstrainedResize);
478-
ImGui::MenuItem("Fullscreen window", NULL, &demo.ShowAppFullscreen);
479-
ImGui::MenuItem("Long text display", NULL, &demo.ShowAppLongText);
480-
ImGui::MenuItem("Manipulating window titles", NULL, &demo.ShowAppWindowTitles);
481-
482-
ImGui::EndMenu();
483-
}
484-
//if (ImGui::MenuItem("MenuItem")) {} // You can also use MenuItem() inside a menu bar!
485-
if (ImGui::BeginMenu("Tools"))
486-
{
487-
IMGUI_DEMO_MARKER("Menu/Tools");
488-
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
489-
const bool has_debug_tools = true;
490-
#else
491-
const bool has_debug_tools = false;
492-
#endif
493-
ImGui::MenuItem("Metrics/Debugger", NULL, &demo.ShowMetrics, has_debug_tools);
494-
ImGui::MenuItem("Debug Log", NULL, &demo.ShowDebugLog, has_debug_tools);
495-
ImGui::MenuItem("ID Stack Tool", NULL, &demo.ShowIDStackTool, has_debug_tools);
496-
ImGui::MenuItem("Style Editor", NULL, &demo.ShowStyleEditor);
497-
bool is_debugger_present = ImGui::GetIO().ConfigDebugIsDebuggerPresent;
498-
if (ImGui::MenuItem("Item Picker", NULL, false, has_debug_tools && is_debugger_present))
499-
ImGui::DebugStartItemPicker();
500-
if (!is_debugger_present)
501-
ImGui::SetItemTooltip("Requires io.ConfigDebugIsDebuggerPresent=true to be set.\n\nWe otherwise disable the menu option to avoid casual users crashing the application.\n\nYou can however always access the Item Picker in Metrics->Tools.");
502-
ImGui::Separator();
503-
ImGui::MenuItem("About Dear ImGui", NULL, &demo.ShowAbout);
504-
ImGui::EndMenu();
505-
}
506-
ImGui::EndMenuBar();
507-
}
452+
ShowDemoWindowMenuBar(&demo);
508453

509454
ImGui::Text("dear imgui says hello! (%s) (%d)", IMGUI_VERSION, IMGUI_VERSION_NUM);
510455
ImGui::Spacing();
@@ -684,6 +629,71 @@ void ImGui::ShowDemoWindow(bool* p_open)
684629
ImGui::End();
685630
}
686631

632+
//-----------------------------------------------------------------------------
633+
// [SECTION] ShowDemoWindowMenuBar()
634+
//-----------------------------------------------------------------------------
635+
636+
static void ShowDemoWindowMenuBar(DemoWindowData* demo_data)
637+
{
638+
IMGUI_DEMO_MARKER("Menu");
639+
if (ImGui::BeginMenuBar())
640+
{
641+
if (ImGui::BeginMenu("Menu"))
642+
{
643+
IMGUI_DEMO_MARKER("Menu/File");
644+
ShowExampleMenuFile();
645+
ImGui::EndMenu();
646+
}
647+
if (ImGui::BeginMenu("Examples"))
648+
{
649+
IMGUI_DEMO_MARKER("Menu/Examples");
650+
ImGui::MenuItem("Main menu bar", NULL, &demo_data->ShowMainMenuBar);
651+
652+
ImGui::SeparatorText("Mini apps");
653+
ImGui::MenuItem("Assets Browser", NULL, &demo_data->ShowAppAssetsBrowser);
654+
ImGui::MenuItem("Console", NULL, &demo_data->ShowAppConsole);
655+
ImGui::MenuItem("Custom rendering", NULL, &demo_data->ShowAppCustomRendering);
656+
ImGui::MenuItem("Documents", NULL, &demo_data->ShowAppDocuments);
657+
ImGui::MenuItem("Log", NULL, &demo_data->ShowAppLog);
658+
ImGui::MenuItem("Property editor", NULL, &demo_data->ShowAppPropertyEditor);
659+
ImGui::MenuItem("Simple layout", NULL, &demo_data->ShowAppLayout);
660+
ImGui::MenuItem("Simple overlay", NULL, &demo_data->ShowAppSimpleOverlay);
661+
662+
ImGui::SeparatorText("Concepts");
663+
ImGui::MenuItem("Auto-resizing window", NULL, &demo_data->ShowAppAutoResize);
664+
ImGui::MenuItem("Constrained-resizing window", NULL, &demo_data->ShowAppConstrainedResize);
665+
ImGui::MenuItem("Fullscreen window", NULL, &demo_data->ShowAppFullscreen);
666+
ImGui::MenuItem("Long text display", NULL, &demo_data->ShowAppLongText);
667+
ImGui::MenuItem("Manipulating window titles", NULL, &demo_data->ShowAppWindowTitles);
668+
669+
ImGui::EndMenu();
670+
}
671+
//if (ImGui::MenuItem("MenuItem")) {} // You can also use MenuItem() inside a menu bar!
672+
if (ImGui::BeginMenu("Tools"))
673+
{
674+
IMGUI_DEMO_MARKER("Menu/Tools");
675+
#ifndef IMGUI_DISABLE_DEBUG_TOOLS
676+
const bool has_debug_tools = true;
677+
#else
678+
const bool has_debug_tools = false;
679+
#endif
680+
ImGui::MenuItem("Metrics/Debugger", NULL, &demo_data->ShowMetrics, has_debug_tools);
681+
ImGui::MenuItem("Debug Log", NULL, &demo_data->ShowDebugLog, has_debug_tools);
682+
ImGui::MenuItem("ID Stack Tool", NULL, &demo_data->ShowIDStackTool, has_debug_tools);
683+
ImGui::MenuItem("Style Editor", NULL, &demo_data->ShowStyleEditor);
684+
bool is_debugger_present = ImGui::GetIO().ConfigDebugIsDebuggerPresent;
685+
if (ImGui::MenuItem("Item Picker", NULL, false, has_debug_tools && is_debugger_present))
686+
ImGui::DebugStartItemPicker();
687+
if (!is_debugger_present)
688+
ImGui::SetItemTooltip("Requires io.ConfigDebugIsDebuggerPresent=true to be set.\n\nWe otherwise disable the menu option to avoid casual users crashing the application.\n\nYou can however always access the Item Picker in Metrics->Tools.");
689+
ImGui::Separator();
690+
ImGui::MenuItem("About Dear ImGui", NULL, &demo_data->ShowAbout);
691+
ImGui::EndMenu();
692+
}
693+
ImGui::EndMenuBar();
694+
}
695+
}
696+
687697
//-----------------------------------------------------------------------------
688698
// [SECTION] ShowDemoWindowWidgets()
689699
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)