From eb88fee052ce368ca8305a88494341e2e8dd422f Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 25 Dec 2020 16:10:12 +0100 Subject: [PATCH] Tables: internal tidying up, calculate WidthAuto in first loop of layout + TableSetupColumn() with unspecified stretch weight leaves it at -1.0f (unset) rather than overrding default. Amend 3b3503e. Both changes are intended to have no side-effects. Committed separately from upcoming commit for easier future bissecting. Small demo fix. --- imgui.h | 2 +- imgui_demo.cpp | 11 ++++++----- imgui_tables.cpp | 49 ++++++++++++++++++++++++------------------------ 3 files changed, 31 insertions(+), 31 deletions(-) diff --git a/imgui.h b/imgui.h index f18178ab6..e6e2e0c3a 100644 --- a/imgui.h +++ b/imgui.h @@ -664,7 +664,7 @@ namespace ImGui // - 1. Call BeginTable() // - 2. Optionally call TableSetupColumn() to submit column name/flags/defaults // - 3. Optionally call TableSetupScrollFreeze() to request scroll freezing of columns/rows - // - 4. Optionally call TableHeadersRow() to submit a header row (names will be pulled from data submitted to TableSetupColumns) + // - 4. Optionally call TableHeadersRow() to submit a header row. Names will be pulled from data provided TableSetupColumn() calls) // - 5. Populate contents // - In most situations you can use TableNextRow() + TableSetColumnIndex(N) to start appending into a column. // - If you are using tables as a sort of grid, where every columns is holding the same type of contents, diff --git a/imgui_demo.cpp b/imgui_demo.cpp index 456e0b4e5..5427dfb8c 100644 --- a/imgui_demo.cpp +++ b/imgui_demo.cpp @@ -4080,15 +4080,15 @@ static void ShowDemoWindowTables() static int column_count = 3; PushStyleCompact(); - ImGui::SetNextItemWidth(TEXT_BASE_WIDTH * 22); + ImGui::PushItemWidth(TEXT_BASE_WIDTH * 30); ImGui::Combo("Contents", &contents_type, "Short Text\0Long Text\0Button\0Fill Button\0InputText\0"); if (contents_type == CT_FillButton) { ImGui::SameLine(); HelpMarker("Be mindful that using right-alignment (e.g. size.x = -FLT_MIN) creates a feedback loop where contents width can feed into auto-column width can feed into contents width."); } - ImGui::SetNextItemWidth(TEXT_BASE_WIDTH * 22); ImGui::DragInt("Columns", &column_count, 0.1f, 1, 64, "%d", ImGuiSliderFlags_AlwaysClamp); + ImGui::PopItemWidth(); ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerH", &flags, ImGuiTableFlags_BordersInnerH); ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterH", &flags, ImGuiTableFlags_BordersOuterH); ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerV", &flags, ImGuiTableFlags_BordersInnerV); @@ -4181,13 +4181,14 @@ static void ShowDemoWindowTables() static float inner_width = 1000.0f; PushStyleCompact(); ImGui::PushID("flags3"); + ImGui::PushItemWidth(TEXT_BASE_WIDTH * 30); ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags3, ImGuiTableFlags_ScrollX); if (ImGui::CheckboxFlags("ImGuiTableFlags_SizingPolicyStretch", &flags3, ImGuiTableFlags_SizingPolicyStretch)) flags3 &= ~ImGuiTableFlags_SizingPolicyFixed; // Can't specify both sizing polices so we clear the other if (ImGui::CheckboxFlags("ImGuiTableFlags_SizingPolicyFixed", &flags3, ImGuiTableFlags_SizingPolicyFixed)) flags3 &= ~ImGuiTableFlags_SizingPolicyStretch; // Can't specify both sizing polices so we clear the other - ImGui::SetNextItemWidth(TEXT_BASE_WIDTH * 10.0f); ImGui::DragFloat("inner_width", &inner_width, 1.0f, 0.0f, FLT_MAX, "%.1f"); + ImGui::PopItemWidth(); ImGui::PopID(); PopStyleCompact(); if (ImGui::BeginTable("##table3", 7, flags3 | ImGuiTableFlags_SizingPolicyStretch | ImGuiTableFlags_ContextMenuInBody, outer_size, inner_width)) @@ -4853,8 +4854,8 @@ static void ShowDemoWindowTables() ImGui::TableSetupColumn("ID", ImGuiTableColumnFlags_DefaultSort | ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_NoHide, -1.0f, MyItemColumnID_ID); ImGui::TableSetupColumn("Name", ImGuiTableColumnFlags_WidthFixed, -1.0f, MyItemColumnID_Name); ImGui::TableSetupColumn("Action", ImGuiTableColumnFlags_NoSort | ImGuiTableColumnFlags_WidthFixed, -1.0f, MyItemColumnID_Action); - ImGui::TableSetupColumn("Quantity", ImGuiTableColumnFlags_PreferSortDescending, 1.0f, MyItemColumnID_Quantity); - ImGui::TableSetupColumn("Description", ImGuiTableColumnFlags_WidthStretch, 1.0f, MyItemColumnID_Description); + ImGui::TableSetupColumn("Quantity", ImGuiTableColumnFlags_PreferSortDescending, -1.0f, MyItemColumnID_Quantity); + ImGui::TableSetupColumn("Description", ImGuiTableColumnFlags_WidthStretch, -1.0f, MyItemColumnID_Description); ImGui::TableSetupColumn("Hidden", ImGuiTableColumnFlags_DefaultHide | ImGuiTableColumnFlags_NoSort); ImGui::TableSetupScrollFreeze(freeze_cols, freeze_rows); diff --git a/imgui_tables.cpp b/imgui_tables.cpp index 07ea39c1b..a942af2eb 100644 --- a/imgui_tables.cpp +++ b/imgui_tables.cpp @@ -644,7 +644,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) table->EnabledMaskByDisplayOrder = 0x00; table->MinColumnWidth = ImMax(1.0f, g.Style.FramePadding.x * 1.0f); // g.Style.ColumnsMinSpacing; // FIXME-TABLE - // [Part 1] Apply/lock Enabled and Order states. + // [Part 1] Apply/lock Enabled and Order states. Calculate auto/ideal width for columns. // Process columns in their visible orders as we are building the Prev/Next indices. int last_visible_column_idx = -1; bool want_auto_fit = false; @@ -687,29 +687,33 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) if (start_auto_fit) column->AutoFitQueue = column->CannotSkipItemsQueue = (1 << 3) - 1; // Fit for three frames - if (column->AutoFitQueue != 0x00) - want_auto_fit = true; - ImU64 index_mask = (ImU64)1 << column_n; ImU64 display_order_mask = (ImU64)1 << column->DisplayOrder; - if (column->IsEnabled) - { - // Mark as enabled and link to previous/next enabled column - column->PrevEnabledColumn = (ImGuiTableColumnIdx)last_visible_column_idx; - column->NextEnabledColumn = -1; - if (last_visible_column_idx != -1) - table->Columns[last_visible_column_idx].NextEnabledColumn = (ImGuiTableColumnIdx)column_n; - column->IndexWithinEnabledSet = table->ColumnsEnabledCount; - table->ColumnsEnabledCount++; - table->EnabledMaskByIndex |= index_mask; - table->EnabledMaskByDisplayOrder |= display_order_mask; - last_visible_column_idx = column_n; - } - else + if (!column->IsEnabled) { column->IndexWithinEnabledSet = -1; + continue; } + + // Mark as enabled and link to previous/next enabled column + column->PrevEnabledColumn = (ImGuiTableColumnIdx)last_visible_column_idx; + column->NextEnabledColumn = -1; + if (last_visible_column_idx != -1) + table->Columns[last_visible_column_idx].NextEnabledColumn = (ImGuiTableColumnIdx)column_n; + column->IndexWithinEnabledSet = table->ColumnsEnabledCount; + table->ColumnsEnabledCount++; + table->EnabledMaskByIndex |= index_mask; + table->EnabledMaskByDisplayOrder |= display_order_mask; + last_visible_column_idx = column_n; IM_ASSERT(column->IndexWithinEnabledSet <= column->DisplayOrder); + + // Calculate ideal/auto column width (that's the width required for all contents to be visible without clipping) + // Combine width from regular rows + width from headers unless requested not to. + if (!column->IsPreserveWidthAuto) + column->WidthAuto = TableGetColumnWidthAuto(table, column); + + if (column->AutoFitQueue != 0x00) + want_auto_fit = true; } if ((table->Flags & ImGuiTableFlags_Sortable) && table->SortSpecsCount == 0 && !(table->Flags & ImGuiTableFlags_SortTristate)) table->IsSortSpecsDirty = true; @@ -724,7 +728,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) if (want_auto_fit) table->IsSettingsDirty = true; - // [Part 3] Fix column flags. Calculate ideal width for columns. Count how many fixed/stretch columns we have and sum of weights. + // [Part 3] Fix column flags. Count how many fixed/stretch columns we have and sum of weights. int count_fixed = 0; // Number of columns that have fixed sizing policy (not stretched sizing policy) (this is NOT the opposite of count_resizable!) int count_resizable = 0; // Number of columns the user can resize (this is NOT the opposite of count_fixed!) float sum_weights_stretched = 0.0f; // Sum of all weights for weighted columns. @@ -741,11 +745,6 @@ void ImGui::TableUpdateLayout(ImGuiTable* table) if ((column->Flags & ImGuiTableColumnFlags_NoResize) == 0) count_resizable++; - // Calculate ideal/auto column width (that's the width required for all contents to be visible without clipping) - // Combine width from regular rows + width from headers unless requested not to. - if (!column->IsPreserveWidthAuto) - column->WidthAuto = TableGetColumnWidthAuto(table, column); - if (column->Flags & (ImGuiTableColumnFlags_WidthFixed | ImGuiTableColumnFlags_WidthAuto)) { // Non-resizable columns keep their requested width @@ -1313,7 +1312,7 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags, flo if ((flags & ImGuiTableColumnFlags_WidthFixed) && init_width_or_weight > 0.0f) column->WidthRequest = init_width_or_weight; if (flags & ImGuiTableColumnFlags_WidthStretch) - column->StretchWeight = (init_width_or_weight > 0.0f) ? init_width_or_weight : 1.0f; + column->StretchWeight = (init_width_or_weight > 0.0f) ? init_width_or_weight : -1.0f; // Disable auto-fit if an explicit width/weight has been specified if (init_width_or_weight > 0.0f)