From a0e6aa1766f161054efc7b421cfa8b3029cca1d1 Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 17 Jul 2020 23:23:04 +0200 Subject: [PATCH] Tables: Fix calculation of auto-fit (remove padding). Demo setting a width in columns setup + ImGuiTableFlags_NoKeepColumnsVisible. --- imgui.h | 2 +- imgui_demo.cpp | 28 ++++++++++++++++++++++++++++ imgui_tables.cpp | 9 ++++----- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/imgui.h b/imgui.h index f1e280f8..c72757ad 100644 --- a/imgui.h +++ b/imgui.h @@ -1041,7 +1041,7 @@ enum ImGuiTableFlags_ ImGuiTableFlags_SizingPolicyStretchX = 1 << 14, // Default if ScrollX is off. Columns will default to use _WidthStretch policy. Read description above for more details. ImGuiTableFlags_NoHeadersWidth = 1 << 15, // Disable header width contribution to automatic width calculation. ImGuiTableFlags_NoHostExtendY = 1 << 16, // (FIXME-TABLE: Reword as SizingPolicy?) Disable extending past the limit set by outer_size.y, only meaningful when neither of ScrollX|ScrollY are set (data below the limit will be clipped and not visible) - ImGuiTableFlags_NoKeepColumnsVisible = 1 << 17, // (FIXME-TABLE) Disable code that keeps column always minimally visible when table width gets too small. + ImGuiTableFlags_NoKeepColumnsVisible = 1 << 17, // (FIXME-TABLE) Disable code that keeps column always minimally visible when table width gets too small and horizontal scrolling is off. // Scrolling ImGuiTableFlags_ScrollX = 1 << 18, // Enable horizontal scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. Because this create a child window, ScrollY is currently generally recommended when using ScrollX. ImGuiTableFlags_ScrollY = 1 << 19, // Enable vertical scrolling. Require 'outer_size' parameter of BeginTable() to specify the container size. diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 391ec248..2d8fa1a0 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -3596,6 +3596,32 @@ static void ShowDemoWindowTables() ImGui::TreePop(); } + if (open_action != -1) + ImGui::SetNextItemOpen(open_action != 0); + if (ImGui::TreeNode("Explicit widths")) + { + static ImGuiTableFlags flags = ImGuiTableFlags_None; + ImGui::CheckboxFlags("ImGuiTableFlags_NoKeepColumnsVisible", (unsigned int*)&flags, ImGuiTableFlags_NoKeepColumnsVisible); + if (ImGui::BeginTable("##table1", 3, flags)) + { + // We could also set ImGuiTableFlags_SizingPolicyFixedX on the table and all columns will default to ImGuiTableColumnFlags_WidthFixed. + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 100.0f); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 200.0f); + ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 100.0f); + for (int row = 0; row < 5; row++) + { + ImGui::TableNextRow(); + for (int column = 0; column < 3; column++) + { + ImGui::TableSetColumnIndex(column); + ImGui::Text("Hello %d,%d", row, column); + } + } + ImGui::EndTable(); + } + ImGui::TreePop(); + } + if (open_action != -1) ImGui::SetNextItemOpen(open_action != 0); if (ImGui::TreeNode("Vertical scrolling, with clipping")) @@ -4223,6 +4249,8 @@ static void ShowDemoWindowTables() ImGui::SameLine(); HelpMarker("[Default if ScrollX is on]\nEnlarge as needed: enable scrollbar if ScrollX is enabled, otherwise extend parent window's contents rectangle. Only Fixed columns allowed. Stretched columns will calculate their width assuming no scrolling."); ImGui::CheckboxFlags("ImGuiTableFlags_NoHeadersWidth", (unsigned int*)&flags, ImGuiTableFlags_NoHeadersWidth); ImGui::CheckboxFlags("ImGuiTableFlags_NoHostExtendY", (unsigned int*)&flags, ImGuiTableFlags_NoHostExtendY); + ImGui::CheckboxFlags("ImGuiTableFlags_NoKeepColumnsVisible", (unsigned int*)&flags, ImGuiTableFlags_NoKeepColumnsVisible); + ImGui::SameLine(); HelpMarker("Only available if ScrollX is disabled."); ImGui::Unindent(); ImGui::BulletText("Scrolling:"); diff --git a/imgui_tables.cpp b/imgui_tables.cpp index ab5c1803..2ead3fd8 100644 --- a/imgui_tables.cpp +++ b/imgui_tables.cpp @@ -580,7 +580,6 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) // (can't make auto padding larger than what WorkRect knows about so right-alignment matches) const ImRect work_rect = table->WorkRect; const float padding_auto_x = table->CellPaddingX2; - const float spacing_auto_x = table->CellSpacingX * (1.0f + 2.0f); // CellSpacingX is >0.0f when there's no vertical border, in which case we add two extra CellSpacingX to make auto-fit look nice instead of cramped. We may want to expose this somehow. const float min_column_width = TableGetMinColumnWidth(); int count_fixed = 0; @@ -614,7 +613,11 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) if (!(table->Flags & ImGuiTableFlags_NoHeadersWidth) && !(column->Flags & ImGuiTableColumnFlags_NoHeaderWidth)) column_width_ideal = ImMax(column_width_ideal, column_content_width_headers); column_width_ideal = ImMax(column_width_ideal + padding_auto_x, min_column_width); + + // CellSpacingX is >0.0f when there's no vertical border table->ColumnsAutoFitWidth += column_width_ideal; + if (column->PrevVisibleColumn != -1) + table->ColumnsAutoFitWidth += table->CellSpacingX; if (column->Flags & (ImGuiTableColumnFlags_WidthAlwaysAutoResize | ImGuiTableColumnFlags_WidthFixed)) { @@ -647,10 +650,6 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) } } - // CellSpacingX is >0.0f when there's no vertical border, in which case we add two extra CellSpacingX to make auto-fit look nice instead of cramped. - // We may want to expose this somehow. - table->ColumnsAutoFitWidth += spacing_auto_x * (table->ColumnsVisibleCount - 1); - // Layout const float width_spacings = table->CellSpacingX * (table->ColumnsVisibleCount - 1); float width_avail;