Tables: fixed "resize to default" of multiple stretch column (added 3b3503e, broken 7a61f340).

Fixed a warning.
Storing RightMostStretchedColumn column for resizing code.
Avoid clearing RightMostEnabledColumn in BeginTable() so resizing code can potentially use it.
(Added regression tests for resize all to default imgui_dev)
This commit is contained in:
ocornut 2020-12-14 11:59:01 +01:00
parent f2df804fcc
commit bd899efbd0
3 changed files with 21 additions and 15 deletions

View File

@ -4458,7 +4458,7 @@ static void ShowDemoWindowTables()
for (int column = 0; column < COLUMNS_COUNT; column++)
{
ImGui::TableSetColumnIndex(column);
ImGui::Text("Cell %d,%d", 0, row);
ImGui::Text("Cell %d,%d", column, row);
}
}
ImGui::EndTable();

View File

@ -2047,8 +2047,9 @@ struct ImGuiTable
ImGuiTableColumnIdx HeldHeaderColumn; // Index of column header being held.
ImGuiTableColumnIdx ReorderColumn; // Index of column being reordered. (not cleared)
ImGuiTableColumnIdx ReorderColumnDir; // -1 or +1
ImGuiTableColumnIdx LeftMostStretchedColumn; // Index of left-most stretched column.
ImGuiTableColumnIdx RightMostStretchedColumn; // Index of right-most stretched column.
ImGuiTableColumnIdx RightMostEnabledColumn; // Index of right-most non-hidden column.
ImGuiTableColumnIdx LeftMostStretchedColumnDisplayOrder; // Display order of left-most stretched column.
ImGuiTableColumnIdx ContextPopupColumn; // Column right-clicked on, of -1 if opening context menu from a neutral/empty spot
ImGuiTableColumnIdx FreezeRowsRequest; // Requested frozen rows count
ImGuiTableColumnIdx FreezeRowsCount; // Actual frozen row count (== FreezeRowsRequest, or == 0 when no scrolling offset)

View File

@ -369,7 +369,6 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG
table->FreezeColumnsRequest = table->FreezeColumnsCount = 0;
table->IsUnfrozen = true;
table->DeclColumnsCount = 0;
table->RightMostEnabledColumn = -1;
// Using opaque colors facilitate overlapping elements of the grid
table->BorderColorStrong = GetColorU32(ImGuiCol_TableBorderStrong);
@ -709,7 +708,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
float sum_weights_stretched = 0.0f; // Sum of all weights for weighted columns.
float sum_width_fixed_requests = 0.0f; // Sum of all width for fixed and auto-resize columns, excluding width contributed by Stretch columns.
float max_width_auto = 0.0f; // Largest auto-width (used for SameWidths feature)
table->LeftMostStretchedColumnDisplayOrder = -1;
table->LeftMostStretchedColumn = table->RightMostStretchedColumn = -1;
for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
{
if (!(table->EnabledMaskByIndex & ((ImU64)1 << column_n)))
@ -760,11 +759,16 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
else
{
IM_ASSERT(column->Flags & ImGuiTableColumnFlags_WidthStretch);
if (column->StretchWeight < 0.0f)
column->StretchWeight = 1.0f;
// Revert or initialize weight (when column->StretchWeight < 0.0f normally it means there has been no init value so it'll always default to 1.0f)
if (column->AutoFitQueue != 0x00 || column->StretchWeight < 0.0f)
column->StretchWeight = (column->InitStretchWeightOrWidth > 0.0f) ? column->InitStretchWeightOrWidth : 1.0f;
sum_weights_stretched += column->StretchWeight;
if (table->LeftMostStretchedColumnDisplayOrder == -1 || table->LeftMostStretchedColumnDisplayOrder > column->DisplayOrder)
table->LeftMostStretchedColumnDisplayOrder = column->DisplayOrder;
if (table->LeftMostStretchedColumn == -1 || table->Columns[table->LeftMostStretchedColumn].DisplayOrder > column->DisplayOrder)
table->LeftMostStretchedColumn = (ImGuiTableColumnIdx)column_n;
if (table->RightMostStretchedColumn == -1 || table->Columns[table->RightMostStretchedColumn].DisplayOrder < column->DisplayOrder)
table->RightMostStretchedColumn = (ImGuiTableColumnIdx)column_n;
}
max_width_auto = ImMax(max_width_auto, column->WidthAuto);
sum_width_fixed_requests += table->CellPaddingX * 2.0f;
@ -831,8 +835,8 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
}
// [Resize Rule 1] The right-most Visible column is not resizable if there is at least one Stretch column
// (see comments in TableResizeColumn())
if (column->NextEnabledColumn == -1 && table->LeftMostStretchedColumnDisplayOrder != -1)
// See additional comments in TableSetColumnWidth().
if (column->NextEnabledColumn == -1 && table->LeftMostStretchedColumn != -1)
column->Flags |= ImGuiTableColumnFlags_NoDirectResize_;
// Assign final width, record width in case we will need to shrink
@ -1885,7 +1889,7 @@ void ImGui::TableSetColumnWidth(int column_n, float width)
// [Resize Rule 3] If we are are followed by a fixed column and we have a Stretch column before, we need to ensure
// that our left border won't move, which we can do by making sure column_a/column_b resizes cancels each others.
if (column_1 && (column_1->Flags & ImGuiTableColumnFlags_WidthFixed))
if (table->LeftMostStretchedColumnDisplayOrder != -1 && table->LeftMostStretchedColumnDisplayOrder < column_0->DisplayOrder)
if (table->LeftMostStretchedColumn != -1 && table->Columns[table->LeftMostStretchedColumn].DisplayOrder < column_0->DisplayOrder)
{
// (old_a + old_b == new_a + new_b) --> (new_a == old_a + old_b - new_b)
float column_1_width = ImMax(column_1->WidthRequest - (column_0_width - column_0->WidthRequest), min_width);
@ -1933,9 +1937,10 @@ void ImGui::TableSetColumnWidthAutoSingle(ImGuiTable* table, int column_n)
if (!column->IsEnabled)
return;
column->CannotSkipItemsQueue = (1 << 0);
column->AutoFitQueue = (1 << 1);
if (column->Flags & ImGuiTableColumnFlags_WidthStretch)
table->AutoFitSingleStretchColumn = (ImGuiTableColumnIdx)column_n;
else
column->AutoFitQueue = (1 << 1);
}
void ImGui::TableSetColumnWidthAutoAll(ImGuiTable* table)
@ -1952,7 +1957,7 @@ void ImGui::TableSetColumnWidthAutoAll(ImGuiTable* table)
void ImGui::TableUpdateColumnsWeightFromWidth(ImGuiTable* table)
{
IM_ASSERT(table->LeftMostStretchedColumnDisplayOrder != -1);
IM_ASSERT(table->LeftMostStretchedColumn != -1 && table->RightMostStretchedColumn != -1);
// Measure existing quantity
float visible_weight = 0.0f;
@ -2506,7 +2511,7 @@ void ImGui::TableSortSpecsSanitize(ImGuiTable* table)
{
sort_order_count = 1;
column->SortOrder = 0;
column->SortDirection = TableGetColumnAvailSortDirection(column, 0);
column->SortDirection = (ImU8)TableGetColumnAvailSortDirection(column, 0);
break;
}
}
@ -2792,7 +2797,7 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table)
if (column != NULL)
{
const bool can_resize = !(column->Flags & ImGuiTableColumnFlags_NoResize) && column->IsEnabled;
if (MenuItem("Size column to fit", NULL, false, can_resize))
if (MenuItem("Size column to fit###SizeOne", NULL, false, can_resize))
TableSetColumnWidthAutoSingle(table, column_n);
}