Skip to content

Commit e8568f3

Browse files
rokupsocornut
authored andcommitted
Menus: Fix appending to main menubar (ocornut#3068).
1 parent 16a9488 commit e8568f3

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

imgui_widgets.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6150,27 +6150,34 @@ bool ImGui::BeginMainMenuBar()
61506150
{
61516151
ImGuiContext& g = *GImGui;
61526152
ImGuiViewportP* viewport = g.Viewports[0];
6153+
ImGuiWindow* menu_bar_window = FindWindowByName("##MainMenuBar");
61536154

61546155
// For the main menu bar, which cannot be moved, we honor g.Style.DisplaySafeAreaPadding to ensure text can be visible on a TV set.
61556156
g.NextWindowData.MenuBarOffsetMinVal = ImVec2(g.Style.DisplaySafeAreaPadding.x, ImMax(g.Style.DisplaySafeAreaPadding.y - g.Style.FramePadding.y, 0.0f));
61566157

6157-
// Get our rectangle in the work area, and report the size we need for next frame.
6158-
// We don't attempt to calculate our height ahead, as it depends on the per-viewport font size. However menu-bar will affect the minimum window size so we'll get the right height.
6159-
ImVec2 menu_bar_pos = viewport->Pos + viewport->CurrWorkOffsetMin;
6160-
ImVec2 menu_bar_size = ImVec2(viewport->Size.x - viewport->CurrWorkOffsetMin.x + viewport->CurrWorkOffsetMax.x, 1.0f);
6158+
// Get our rectangle at the top of the work area
6159+
if (menu_bar_window == NULL || menu_bar_window->BeginCount == 0)
6160+
{
6161+
// Set window position
6162+
// We don't attempt to calculate our height ahead, as it depends on the per-viewport font size. However menu-bar will affect the minimum window size so we'll get the right height.
6163+
ImVec2 menu_bar_pos = viewport->Pos + viewport->CurrWorkOffsetMin;
6164+
ImVec2 menu_bar_size = ImVec2(viewport->Size.x - viewport->CurrWorkOffsetMin.x + viewport->CurrWorkOffsetMax.x, 1.0f);
6165+
SetNextWindowPos(menu_bar_pos);
6166+
SetNextWindowSize(menu_bar_size);
6167+
}
61616168

61626169
// Create window
6163-
SetNextWindowPos(menu_bar_pos);
6164-
SetNextWindowSize(menu_bar_size);
61656170
SetNextWindowViewport(viewport->ID); // Enforce viewport so we don't create our own viewport when ImGuiConfigFlags_ViewportsNoMerge is set.
61666171
PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
61676172
PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(0, 0)); // Lift normal size constraint, however the presence of a menu-bar will give us the minimum height we want.
61686173
ImGuiWindowFlags window_flags = ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_MenuBar;
61696174
bool is_open = Begin("##MainMenuBar", NULL, window_flags) && BeginMenuBar();
61706175
PopStyleVar(2);
61716176

6172-
// Feed back into work area using actual window size
6173-
viewport->CurrWorkOffsetMin.y += GetCurrentWindow()->Size.y;
6177+
// Report our size into work area (for next frame) using actual window size
6178+
menu_bar_window = GetCurrentWindow();
6179+
if (menu_bar_window->BeginCount == 1)
6180+
viewport->CurrWorkOffsetMin.y += menu_bar_window->Size.y;
61746181

61756182
g.NextWindowData.MenuBarOffsetMinVal = ImVec2(0.0f, 0.0f);
61766183
if (!is_open)

0 commit comments

Comments
 (0)