Skip to content

Commit bd899ef

Browse files
committed
Tables: fixed "resize to default" of multiple stretch column (added 3b3503e, broken 7a61f34).
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)
1 parent f2df804 commit bd899ef

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

imgui_demo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4458,7 +4458,7 @@ static void ShowDemoWindowTables()
44584458
for (int column = 0; column < COLUMNS_COUNT; column++)
44594459
{
44604460
ImGui::TableSetColumnIndex(column);
4461-
ImGui::Text("Cell %d,%d", 0, row);
4461+
ImGui::Text("Cell %d,%d", column, row);
44624462
}
44634463
}
44644464
ImGui::EndTable();

imgui_internal.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2047,8 +2047,9 @@ struct ImGuiTable
20472047
ImGuiTableColumnIdx HeldHeaderColumn; // Index of column header being held.
20482048
ImGuiTableColumnIdx ReorderColumn; // Index of column being reordered. (not cleared)
20492049
ImGuiTableColumnIdx ReorderColumnDir; // -1 or +1
2050+
ImGuiTableColumnIdx LeftMostStretchedColumn; // Index of left-most stretched column.
2051+
ImGuiTableColumnIdx RightMostStretchedColumn; // Index of right-most stretched column.
20502052
ImGuiTableColumnIdx RightMostEnabledColumn; // Index of right-most non-hidden column.
2051-
ImGuiTableColumnIdx LeftMostStretchedColumnDisplayOrder; // Display order of left-most stretched column.
20522053
ImGuiTableColumnIdx ContextPopupColumn; // Column right-clicked on, of -1 if opening context menu from a neutral/empty spot
20532054
ImGuiTableColumnIdx FreezeRowsRequest; // Requested frozen rows count
20542055
ImGuiTableColumnIdx FreezeRowsCount; // Actual frozen row count (== FreezeRowsRequest, or == 0 when no scrolling offset)

imgui_tables.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ bool ImGui::BeginTableEx(const char* name, ImGuiID id, int columns_count, ImG
369369
table->FreezeColumnsRequest = table->FreezeColumnsCount = 0;
370370
table->IsUnfrozen = true;
371371
table->DeclColumnsCount = 0;
372-
table->RightMostEnabledColumn = -1;
373372

374373
// Using opaque colors facilitate overlapping elements of the grid
375374
table->BorderColorStrong = GetColorU32(ImGuiCol_TableBorderStrong);
@@ -709,7 +708,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
709708
float sum_weights_stretched = 0.0f; // Sum of all weights for weighted columns.
710709
float sum_width_fixed_requests = 0.0f; // Sum of all width for fixed and auto-resize columns, excluding width contributed by Stretch columns.
711710
float max_width_auto = 0.0f; // Largest auto-width (used for SameWidths feature)
712-
table->LeftMostStretchedColumnDisplayOrder = -1;
711+
table->LeftMostStretchedColumn = table->RightMostStretchedColumn = -1;
713712
for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
714713
{
715714
if (!(table->EnabledMaskByIndex & ((ImU64)1 << column_n)))
@@ -760,11 +759,16 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
760759
else
761760
{
762761
IM_ASSERT(column->Flags & ImGuiTableColumnFlags_WidthStretch);
763-
if (column->StretchWeight < 0.0f)
764-
column->StretchWeight = 1.0f;
762+
763+
// 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)
764+
if (column->AutoFitQueue != 0x00 || column->StretchWeight < 0.0f)
765+
column->StretchWeight = (column->InitStretchWeightOrWidth > 0.0f) ? column->InitStretchWeightOrWidth : 1.0f;
766+
765767
sum_weights_stretched += column->StretchWeight;
766-
if (table->LeftMostStretchedColumnDisplayOrder == -1 || table->LeftMostStretchedColumnDisplayOrder > column->DisplayOrder)
767-
table->LeftMostStretchedColumnDisplayOrder = column->DisplayOrder;
768+
if (table->LeftMostStretchedColumn == -1 || table->Columns[table->LeftMostStretchedColumn].DisplayOrder > column->DisplayOrder)
769+
table->LeftMostStretchedColumn = (ImGuiTableColumnIdx)column_n;
770+
if (table->RightMostStretchedColumn == -1 || table->Columns[table->RightMostStretchedColumn].DisplayOrder < column->DisplayOrder)
771+
table->RightMostStretchedColumn = (ImGuiTableColumnIdx)column_n;
768772
}
769773
max_width_auto = ImMax(max_width_auto, column->WidthAuto);
770774
sum_width_fixed_requests += table->CellPaddingX * 2.0f;
@@ -831,8 +835,8 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
831835
}
832836

833837
// [Resize Rule 1] The right-most Visible column is not resizable if there is at least one Stretch column
834-
// (see comments in TableResizeColumn())
835-
if (column->NextEnabledColumn == -1 && table->LeftMostStretchedColumnDisplayOrder != -1)
838+
// See additional comments in TableSetColumnWidth().
839+
if (column->NextEnabledColumn == -1 && table->LeftMostStretchedColumn != -1)
836840
column->Flags |= ImGuiTableColumnFlags_NoDirectResize_;
837841

838842
// Assign final width, record width in case we will need to shrink
@@ -1885,7 +1889,7 @@ void ImGui::TableSetColumnWidth(int column_n, float width)
18851889
// [Resize Rule 3] If we are are followed by a fixed column and we have a Stretch column before, we need to ensure
18861890
// that our left border won't move, which we can do by making sure column_a/column_b resizes cancels each others.
18871891
if (column_1 && (column_1->Flags & ImGuiTableColumnFlags_WidthFixed))
1888-
if (table->LeftMostStretchedColumnDisplayOrder != -1 && table->LeftMostStretchedColumnDisplayOrder < column_0->DisplayOrder)
1892+
if (table->LeftMostStretchedColumn != -1 && table->Columns[table->LeftMostStretchedColumn].DisplayOrder < column_0->DisplayOrder)
18891893
{
18901894
// (old_a + old_b == new_a + new_b) --> (new_a == old_a + old_b - new_b)
18911895
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)
19331937
if (!column->IsEnabled)
19341938
return;
19351939
column->CannotSkipItemsQueue = (1 << 0);
1936-
column->AutoFitQueue = (1 << 1);
19371940
if (column->Flags & ImGuiTableColumnFlags_WidthStretch)
19381941
table->AutoFitSingleStretchColumn = (ImGuiTableColumnIdx)column_n;
1942+
else
1943+
column->AutoFitQueue = (1 << 1);
19391944
}
19401945

19411946
void ImGui::TableSetColumnWidthAutoAll(ImGuiTable* table)
@@ -1952,7 +1957,7 @@ void ImGui::TableSetColumnWidthAutoAll(ImGuiTable* table)
19521957

19531958
void ImGui::TableUpdateColumnsWeightFromWidth(ImGuiTable* table)
19541959
{
1955-
IM_ASSERT(table->LeftMostStretchedColumnDisplayOrder != -1);
1960+
IM_ASSERT(table->LeftMostStretchedColumn != -1 && table->RightMostStretchedColumn != -1);
19561961

19571962
// Measure existing quantity
19581963
float visible_weight = 0.0f;
@@ -2506,7 +2511,7 @@ void ImGui::TableSortSpecsSanitize(ImGuiTable* table)
25062511
{
25072512
sort_order_count = 1;
25082513
column->SortOrder = 0;
2509-
column->SortDirection = TableGetColumnAvailSortDirection(column, 0);
2514+
column->SortDirection = (ImU8)TableGetColumnAvailSortDirection(column, 0);
25102515
break;
25112516
}
25122517
}
@@ -2792,7 +2797,7 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table)
27922797
if (column != NULL)
27932798
{
27942799
const bool can_resize = !(column->Flags & ImGuiTableColumnFlags_NoResize) && column->IsEnabled;
2795-
if (MenuItem("Size column to fit", NULL, false, can_resize))
2800+
if (MenuItem("Size column to fit###SizeOne", NULL, false, can_resize))
27962801
TableSetColumnWidthAutoSingle(table, column_n);
27972802
}
27982803

0 commit comments

Comments
 (0)