From 3f99890f40c999052e78490b26ce138f80c803b4 Mon Sep 17 00:00:00 2001 From: omar Date: Thu, 29 Aug 2019 14:46:02 +0200 Subject: [PATCH] TabBar: feed desired width (sum of unclipped tabs width) into layout system to allow for auto-resize. (#2768) Before 1.71 tab bars fed the sum of current width which created feedback loops in certain situations. Amend f95c77eeeae70383b3e278a33b511e0d63ee16ac. --- docs/CHANGELOG.txt | 2 ++ imgui_demo.cpp | 3 ++- imgui_internal.h | 3 ++- imgui_widgets.cpp | 13 ++++++++----- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 649e75ed..d20df9d9 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -39,6 +39,8 @@ Other Changes: when enabled will have small overlap glitches with (style.Alpha < 1.0). - TabBar: fixed ScrollToBar request creating bouncing loop when tab is larger than available space. - TabBar: fixed single-tab not shrinking their width down. +- TabBar: feed desired width (sum of unclipped tabs width) into layout system to allow for auto-resize. (#2768) + (before 1.71 tab bars fed the sum of current width which created feedback loops in certain situations). - TabBar: improved shrinking for large number of tabs to avoid leaving extraneous space on the right side. Individuals tabs are given integer-rounded width and remainder is spread between tabs left-to-right. - SliderScalar: Improved assert when using U32 or U64 types with a large v_max value. (#2765) [@loicmouton] diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 8485f7c9..d5bc96c5 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -4286,7 +4286,6 @@ static void ShowExampleAppWindowTitles(bool*) // Demonstrate using the low-level ImDrawList to draw custom shapes. static void ShowExampleAppCustomRendering(bool* p_open) { - ImGui::SetNextWindowSize(ImVec2(350, 560), ImGuiCond_FirstUseEver); if (!ImGui::Begin("Example: Custom rendering", p_open)) { ImGui::End(); @@ -4406,7 +4405,9 @@ static void ShowExampleAppCustomRendering(bool* p_open) static bool draw_bg = true; static bool draw_fg = true; ImGui::Checkbox("Draw in Background draw list", &draw_bg); + ImGui::SameLine(); HelpMarker("The Background draw list will be rendered below every Dear ImGui windows."); ImGui::Checkbox("Draw in Foreground draw list", &draw_fg); + ImGui::SameLine(); HelpMarker("The Foreground draw list will be rendered over every Dear ImGui windows."); ImVec2 window_pos = ImGui::GetWindowPos(); ImVec2 window_size = ImGui::GetWindowSize(); ImVec2 window_center = ImVec2(window_pos.x + window_size.x * 0.5f, window_pos.y + window_size.y * 0.5f); diff --git a/imgui_internal.h b/imgui_internal.h index 1018b1e9..e9139d60 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -1435,8 +1435,9 @@ struct ImGuiTabBar int CurrFrameVisible; int PrevFrameVisible; ImRect BarRect; - float ContentsHeight; + float LastTabContentHeight; // Record the height of contents submitted below the tab bar float OffsetMax; // Distance from BarRect.Min.x, locked during layout + float OffsetMaxIdeal; // Ideal offset if all tabs were visible and not clipped float OffsetNextTab; // Distance from BarRect.Min.x, incremented with each BeginTabItem() call, not used if ImGuiTabBarFlags_Reorderable if set. float ScrollingAnim; float ScrollingTarget; diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp index 42e13243..423a873b 100644 --- a/imgui_widgets.cpp +++ b/imgui_widgets.cpp @@ -6290,8 +6290,8 @@ ImGuiTabBar::ImGuiTabBar() ID = 0; SelectedTabId = NextSelectedTabId = VisibleTabId = 0; CurrFrameVisible = PrevFrameVisible = -1; - ContentsHeight = 0.0f; - OffsetMax = OffsetNextTab = 0.0f; + LastTabContentHeight = 0.0f; + OffsetMax = OffsetMaxIdeal = OffsetNextTab = 0.0f; ScrollingAnim = ScrollingTarget = ScrollingTargetDistToVisibility = ScrollingSpeed = 0.0f; Flags = ImGuiTabBarFlags_None; ReorderRequestTabId = 0; @@ -6373,7 +6373,7 @@ bool ImGui::BeginTabBarEx(ImGuiTabBar* tab_bar, const ImRect& tab_bar_bb, ImG tab_bar->FramePadding = g.Style.FramePadding; // Layout - ItemSize(ImVec2(0.0f /*tab_bar->OffsetMax*/, tab_bar->BarRect.GetHeight())); // Don't feed width back + ItemSize(ImVec2(tab_bar->OffsetMaxIdeal, tab_bar->BarRect.GetHeight())); window->DC.CursorPos.x = tab_bar->BarRect.Min.x; // Draw separator @@ -6406,9 +6406,9 @@ void ImGui::EndTabBar() // Restore the last visible height if no tab is visible, this reduce vertical flicker/movement when a tabs gets removed without calling SetTabItemClosed(). const bool tab_bar_appearing = (tab_bar->PrevFrameVisible + 1 < g.FrameCount); if (tab_bar->VisibleTabWasSubmitted || tab_bar->VisibleTabId == 0 || tab_bar_appearing) - tab_bar->ContentsHeight = ImMax(window->DC.CursorPos.y - tab_bar->BarRect.Max.y, 0.0f); + tab_bar->LastTabContentHeight = ImMax(window->DC.CursorPos.y - tab_bar->BarRect.Max.y, 0.0f); else - window->DC.CursorPos.y = tab_bar->BarRect.Max.y + tab_bar->ContentsHeight; + window->DC.CursorPos.y = tab_bar->BarRect.Max.y + tab_bar->LastTabContentHeight; if ((tab_bar->Flags & ImGuiTabBarFlags_DockNode) == 0) PopID(); @@ -6532,6 +6532,7 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) // Layout all active tabs float offset_x = initial_offset_x; + float offset_x_ideal = offset_x; tab_bar->OffsetNextTab = offset_x; // This is used by non-reorderable tab bar where the submission order is always honored. for (int tab_n = 0; tab_n < tab_bar->Tabs.Size; tab_n++) { @@ -6540,8 +6541,10 @@ static void ImGui::TabBarLayout(ImGuiTabBar* tab_bar) if (scroll_track_selected_tab_id == 0 && g.NavJustMovedToId == tab->ID) scroll_track_selected_tab_id = tab->ID; offset_x += tab->Width + g.Style.ItemInnerSpacing.x; + offset_x_ideal += tab->WidthContents + g.Style.ItemInnerSpacing.x; } tab_bar->OffsetMax = ImMax(offset_x - g.Style.ItemInnerSpacing.x, 0.0f); + tab_bar->OffsetMaxIdeal = ImMax(offset_x_ideal - g.Style.ItemInnerSpacing.x, 0.0f); // Horizontal scrolling buttons const bool scrolling_buttons = (tab_bar->OffsetMax > tab_bar->BarRect.GetWidth() && tab_bar->Tabs.Size > 1) && !(tab_bar->Flags & ImGuiTabBarFlags_NoTabListScrollingButtons) && (tab_bar->Flags & ImGuiTabBarFlags_FittingPolicyScroll);