Skip to content

Commit 3b3503e

Browse files
committed
Tables: decent support for auto-resize of stretch columns (trickier than it sounds)
Four cases: 1. visible columns are all stretch, resize all : "size all to default" reset to default weight 2. visible columns are all stretch, resize one: "size one to fit" set weight, reapply weight (todo: improve weight redistribution in case of >1 siblings) 3. visible columns are mixed, resize all: "size all to fit/default" reset stretchs to default weight, set fixed to auto width 4. visible columns are mixed, resize one: "size one to fit", redistribute weight the same way as a manual resize + TableSetupColumn() more consistently clear AutoFitQueue. + zero-clear RowCellData buffer.
1 parent c5dcf2f commit 3b3503e

File tree

3 files changed

+85
-50
lines changed

3 files changed

+85
-50
lines changed

imgui_demo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3514,7 +3514,7 @@ static void ShowDemoWindowTables()
35143514
{
35153515
// By default, if we don't enable ScrollX the sizing policy for each columns is "Stretch"
35163516
// Each columns maintain a sizing weight, and they will occupy all available width.
3517-
static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV;
3517+
static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody;
35183518
PushStyleCompact();
35193519
ImGui::CheckboxFlags("ImGuiTableFlags_Resizable", &flags, ImGuiTableFlags_Resizable);
35203520
ImGui::CheckboxFlags("ImGuiTableFlags_BordersV", &flags, ImGuiTableFlags_BordersV);
@@ -3549,7 +3549,7 @@ static void ShowDemoWindowTables()
35493549
"Using _Resizable + _SizingPolicyFixedX flags.\n"
35503550
"Fixed-width columns generally makes more sense if you want to use horizontal scrolling.\n\n"
35513551
"Double-click a column border to auto-fit the column to its contents.");
3552-
static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_SizingPolicyFixedX | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV;
3552+
static ImGuiTableFlags flags = ImGuiTableFlags_Resizable | ImGuiTableFlags_SizingPolicyFixedX | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_BordersV | ImGuiTableFlags_ContextMenuInBody;
35533553
//ImGui::CheckboxFlags("ImGuiTableFlags_ScrollX", &flags, ImGuiTableFlags_ScrollX); // FIXME-TABLE: Explain or fix the effect of enable Scroll on outer_size
35543554
if (ImGui::BeginTable("##table1", 3, flags))
35553555
{

imgui_internal.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ struct ImSpan
519519
inline void set(T* data, int size) { Data = data; DataEnd = data + size; }
520520
inline void set(T* data, T* data_end) { Data = data; DataEnd = data_end; }
521521
inline int size() const { return (int)(ptrdiff_t)(DataEnd - Data); }
522+
inline int size_in_bytes() const { return (int)(ptrdiff_t)(DataEnd - Data) * (int)sizeof(T); }
522523
inline T& operator[](int i) { T* p = Data + i; IM_ASSERT(p >= Data && p < DataEnd); return *p; }
523524
inline const T& operator[](int i) const { const T* p = Data + i; IM_ASSERT(p >= Data && p < DataEnd); return *p; }
524525

@@ -1941,7 +1942,6 @@ struct ImGuiTableColumn
19411942
PrevVisibleColumn = NextVisibleColumn = -1;
19421943
SortOrder = -1;
19431944
SortDirection = ImGuiSortDirection_None;
1944-
AutoFitQueue = CannotSkipItemsQueue = (1 << 3) - 1; // Skip for three frames
19451945
DrawChannelCurrent = DrawChannelFrozen = DrawChannelUnfrozen = (ImU8)-1;
19461946
}
19471947
};
@@ -1969,7 +1969,6 @@ struct ImGuiTable
19691969
int SettingsOffset; // Offset in g.SettingsTables
19701970
int LastFrameActive;
19711971
int ColumnsCount; // Number of columns declared in BeginTable()
1972-
int ColumnsVisibleCount; // Number of non-hidden columns (<= ColumnsCount)
19731972
int CurrentRow;
19741973
int CurrentColumn;
19751974
ImS16 InstanceCurrent; // Count of BeginTable() calls with same ID in the same frame (generally 0). This is a little bit similar to BeginCount for a window, but multiple table with same ID look are multiple tables, they are just synched.
@@ -2018,9 +2017,12 @@ struct ImGuiTable
20182017
ImVector<ImGuiTableSortSpecsColumn> SortSpecsData; // FIXME-OPT: Fixed-size array / small-vector pattern, optimize for single sort spec
20192018
ImGuiTableSortSpecs SortSpecs; // Public facing sorts specs, this is what we return in TableGetSortSpecs()
20202019
ImS8 SortSpecsCount;
2020+
ImS8 ColumnsVisibleCount; // Number of non-hidden columns (<= ColumnsCount)
2021+
ImS8 ColumnsVisibleFixedCount; // Number of non-hidden columns (<= ColumnsCount)
20212022
ImS8 DeclColumnsCount; // Count calls to TableSetupColumn()
20222023
ImS8 HoveredColumnBody; // Index of column whose visible region is being hovered. Important: == ColumnsCount when hovering empty region after the right-most column!
20232024
ImS8 HoveredColumnBorder; // Index of column whose right-border is being hovered (for resizing).
2025+
ImS8 AutoFitSingleStretchColumn; // Index of single stretch column requesting auto-fit.
20242026
ImS8 ResizedColumn; // Index of column being resized. Reset when InstanceCurrent==0.
20252027
ImS8 LastResizedColumn; // Index of column being resized from previous frame.
20262028
ImS8 HeldHeaderColumn; // Index of column header being held.
@@ -2287,7 +2289,8 @@ namespace ImGui
22872289
IMGUI_API ImRect TableGetCellBgRect(const ImGuiTable* table, int column_n);
22882290
IMGUI_API const char* TableGetColumnName(const ImGuiTable* table, int column_n);
22892291
IMGUI_API ImGuiID TableGetColumnResizeID(const ImGuiTable* table, int column_n, int instance_no = 0);
2290-
IMGUI_API void TableSetColumnAutofit(ImGuiTable* table, int column_n);
2292+
IMGUI_API void TableSetColumnWidthAutoSingle(ImGuiTable* table, int column_n);
2293+
IMGUI_API void TableSetColumnWidthAutoAll(ImGuiTable* table);
22912294
IMGUI_API void PushTableBackground();
22922295
IMGUI_API void PopTableBackground();
22932296
IMGUI_API void TableRemove(ImGuiTable* table);

imgui_tables.cpp

Lines changed: 77 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ ImGuiTable::ImGuiTable()
155155
ContextPopupColumn = -1;
156156
ReorderColumn = -1;
157157
ResizedColumn = -1;
158+
AutoFitSingleStretchColumn = -1;
158159
HoveredColumnBody = HoveredColumnBorder = -1;
159160
}
160161

@@ -216,10 +217,13 @@ static void TableBeginInitMemory(ImGuiTable* table, int columns_count)
216217
span_allocator.GetSpan(1, &table->DisplayOrderToIndex);
217218
span_allocator.GetSpan(2, &table->RowCellData);
218219

220+
memset(table->RowCellData.Data, 0, table->RowCellData.size_in_bytes());
219221
for (int n = 0; n < columns_count; n++)
220222
{
221-
table->Columns[n] = ImGuiTableColumn();
222-
table->Columns[n].DisplayOrder = table->DisplayOrderToIndex[n] = (ImS8)n;
223+
ImGuiTableColumn* column = &table->Columns[n];
224+
*column = ImGuiTableColumn();
225+
column->DisplayOrder = table->DisplayOrderToIndex[n] = (ImS8)n;
226+
column->AutoFitQueue = column->CannotSkipItemsQueue = (1 << 3) - 1; // Fit for three frames
223227
}
224228
}
225229

@@ -440,6 +444,15 @@ void ImGui::TableBeginUpdateColumns(ImGuiTable* table)
440444
table->LastResizedColumn = table->ResizedColumn;
441445
table->ResizedColumnNextWidth = FLT_MAX;
442446
table->ResizedColumn = -1;
447+
448+
// Process auto-fit for single stretch column, which is a special case
449+
// FIXME-TABLE: Would be nice to redistribute available stretch space accordingly to other weights, instead of giving it all to siblings.
450+
if (table->AutoFitSingleStretchColumn != -1)
451+
{
452+
table->Columns[table->AutoFitSingleStretchColumn].AutoFitQueue = 0x00;
453+
TableSetColumnWidth(table->AutoFitSingleStretchColumn, table->Columns[table->AutoFitSingleStretchColumn].WidthAuto);
454+
table->AutoFitSingleStretchColumn = -1;
455+
}
443456
}
444457

445458
// Handle reordering request
@@ -486,7 +499,7 @@ void ImGui::TableBeginUpdateColumns(ImGuiTable* table)
486499
table->IsSettingsDirty = true;
487500
}
488501

489-
// Setup and lock Visible state and order
502+
// Lock Visible state and Order
490503
table->ColumnsVisibleCount = 0;
491504
table->IsDefaultDisplayOrder = true;
492505
ImGuiTableColumn* last_visible_column = NULL;
@@ -522,7 +535,7 @@ void ImGui::TableBeginUpdateColumns(ImGuiTable* table)
522535
last_visible_column->NextVisibleColumn = (ImS8)column_n;
523536
column->PrevVisibleColumn = (ImS8)table->Columns.index_from_ptr(last_visible_column);
524537
}
525-
column->IndexWithinVisibleSet = (ImS8)table->ColumnsVisibleCount;
538+
column->IndexWithinVisibleSet = table->ColumnsVisibleCount;
526539
table->ColumnsVisibleCount++;
527540
table->VisibleMaskByIndex |= index_mask;
528541
table->VisibleMaskByDisplayOrder |= display_order_mask;
@@ -658,6 +671,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
658671

659672
if (column->Flags & (ImGuiTableColumnFlags_WidthAlwaysAutoResize | ImGuiTableColumnFlags_WidthFixed))
660673
{
674+
// Process auto-fit for non-stretched columns
661675
// Latch initial size for fixed columns and update it constantly for auto-resizing column (unless clipped!)
662676
if ((column->AutoFitQueue != 0x00) || ((column->Flags & ImGuiTableColumnFlags_WidthAlwaysAutoResize) && !column->IsClipped))
663677
column->WidthRequest = width_auto;
@@ -676,15 +690,16 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
676690
else
677691
{
678692
IM_ASSERT(column->Flags & ImGuiTableColumnFlags_WidthStretch);
679-
const int init_size = (column->StretchWeight < 0.0f);
680-
if (init_size)
681-
column->StretchWeight = 1.0f;
693+
const float default_weight = (column->InitStretchWeightOrWidth > 0.0f) ? column->InitStretchWeightOrWidth : 1.0f;
694+
if (column->AutoFitQueue != 0x00)
695+
column->StretchWeight = default_weight;
682696
sum_weights_stretched += column->StretchWeight;
683697
if (table->LeftMostStretchedColumnDisplayOrder == -1)
684698
table->LeftMostStretchedColumnDisplayOrder = (ImS8)column->DisplayOrder;
685699
}
686700
sum_width_fixed_requests += table->CellPaddingX * 2.0f;
687701
}
702+
table->ColumnsVisibleFixedCount = (ImS8)count_fixed;
688703

689704
// Layout
690705
const float width_spacings = (table->OuterPaddingX * 2.0f) + (table->CellSpacingX1 + table->CellSpacingX2) * (table->ColumnsVisibleCount - 1);
@@ -963,10 +978,9 @@ void ImGui::TableUpdateBorders(ImGuiTable* table)
963978

964979
bool hovered = false, held = false;
965980
bool pressed = ButtonBehavior(hit_rect, column_id, &hovered, &held, ImGuiButtonFlags_FlattenChildren | ImGuiButtonFlags_AllowItemOverlap | ImGuiButtonFlags_PressedOnClick | ImGuiButtonFlags_PressedOnDoubleClick);
966-
if (pressed && IsMouseDoubleClicked(0) && !(column->Flags & ImGuiTableColumnFlags_WidthStretch))
981+
if (pressed && IsMouseDoubleClicked(0))
967982
{
968-
// FIXME-TABLE: Double-clicking on column edge could auto-fit Stretch column?
969-
TableSetColumnAutofit(table, column_n);
983+
TableSetColumnWidthAutoSingle(table, column_n);
970984
ClearActiveID();
971985
held = hovered = false;
972986
}
@@ -1313,21 +1327,28 @@ void ImGui::TableSetColumnWidth(int column_n, float width)
13131327
}
13141328
else if (column_0->Flags & ImGuiTableColumnFlags_WidthStretch)
13151329
{
1316-
// [Resize Rule 2]
1317-
if (column_1 && (column_1->Flags & ImGuiTableColumnFlags_WidthFixed))
1330+
// We can also use previous column if there's no next one
1331+
if (column_1 == NULL)
1332+
column_1 = (column_0->PrevVisibleColumn != -1) ? &table->Columns[column_0->PrevVisibleColumn] : NULL;
1333+
if (column_1 == NULL)
1334+
return;
1335+
1336+
if (column_1->Flags & ImGuiTableColumnFlags_WidthFixed)
13181337
{
1338+
// [Resize Rule 2]
13191339
float off = (column_0->WidthGiven - column_0_width);
13201340
float column_1_width = column_1->WidthGiven + off;
13211341
column_1->WidthRequest = ImMax(min_width, column_1_width);
1322-
return;
13231342
}
1324-
1325-
// (old_a + old_b == new_a + new_b) --> (new_a == old_a + old_b - new_b)
1326-
float column_1_width = ImMax(column_1->WidthRequest - (column_0_width - column_0->WidthRequest), min_width);
1327-
column_0_width = column_0->WidthRequest + column_1->WidthRequest - column_1_width;
1328-
column_1->WidthRequest = column_1_width;
1329-
column_0->WidthRequest = column_0_width;
1330-
TableUpdateColumnsWeightFromWidth(table);
1343+
else
1344+
{
1345+
// (old_a + old_b == new_a + new_b) --> (new_a == old_a + old_b - new_b)
1346+
float column_1_width = ImMax(column_1->WidthRequest - (column_0_width - column_0->WidthRequest), min_width);
1347+
column_0_width = column_0->WidthRequest + column_1->WidthRequest - column_1_width;
1348+
column_1->WidthRequest = column_1_width;
1349+
column_0->WidthRequest = column_0_width;
1350+
TableUpdateColumnsWeightFromWidth(table);
1351+
}
13311352
}
13321353
}
13331354

@@ -1613,25 +1634,19 @@ void ImGui::TableSetupColumn(const char* label, ImGuiTableColumnFlags flags,
16131634

16141635
// Initialize defaults
16151636
if (flags & ImGuiTableColumnFlags_WidthStretch)
1616-
{
16171637
IM_ASSERT(init_width_or_weight != 0.0f && "Need to provide a valid weight!");
1618-
if (init_width_or_weight < 0.0f)
1619-
init_width_or_weight = 1.0f;
1620-
}
16211638
column->InitStretchWeightOrWidth = init_width_or_weight;
16221639
if (table->IsInitializing && column->WidthRequest < 0.0f && column->StretchWeight < 0.0f)
16231640
{
16241641
// Init width or weight
16251642
if ((flags & ImGuiTableColumnFlags_WidthFixed) && init_width_or_weight > 0.0f)
1626-
{
1627-
// Disable auto-fit if a default fixed width has been specified
16281643
column->WidthRequest = init_width_or_weight;
1629-
column->AutoFitQueue = 0x00;
1630-
}
16311644
if (flags & ImGuiTableColumnFlags_WidthStretch)
1632-
column->StretchWeight = init_width_or_weight;
1633-
else
1634-
column->StretchWeight = 1.0f;
1645+
column->StretchWeight = (init_width_or_weight > 0.0f) ? init_width_or_weight : 1.0f;
1646+
1647+
// Disable auto-fit if an explicit fixed width has been specified
1648+
if (init_width_or_weight > 0.0f)
1649+
column->AutoFitQueue = 0x00;
16351650
}
16361651
if (table->IsInitializing)
16371652
{
@@ -2041,13 +2056,30 @@ ImGuiID ImGui::TableGetColumnResizeID(const ImGuiTable* table, int column_n, int
20412056
return id;
20422057
}
20432058

2044-
void ImGui::TableSetColumnAutofit(ImGuiTable* table, int column_n)
2059+
// Disable clipping then auto-fit, will take 2 frames
2060+
// (we don't take a shortcut for unclipped columns to reduce inconsistencies when e.g. resizing multiple columns)
2061+
void ImGui::TableSetColumnWidthAutoSingle(ImGuiTable* table, int column_n)
20452062
{
2046-
// Disable clipping then auto-fit, will take 2 frames
2047-
// (we don't take a shortcut for unclipped columns to reduce inconsistencies when e.g. resizing multiple columns)
2063+
// Single auto width uses auto-fit
20482064
ImGuiTableColumn* column = &table->Columns[column_n];
2065+
if (!column->IsVisible)
2066+
return;
20492067
column->CannotSkipItemsQueue = (1 << 0);
20502068
column->AutoFitQueue = (1 << 1);
2069+
if (column->Flags & ImGuiTableColumnFlags_WidthStretch)
2070+
table->AutoFitSingleStretchColumn = (ImS8)column_n;
2071+
}
2072+
2073+
void ImGui::TableSetColumnWidthAutoAll(ImGuiTable* table)
2074+
{
2075+
for (int column_n = 0; column_n < table->ColumnsCount; column_n++)
2076+
{
2077+
ImGuiTableColumn* column = &table->Columns[column_n];
2078+
if (!column->IsVisible)
2079+
continue;
2080+
column->CannotSkipItemsQueue = (1 << 0);
2081+
column->AutoFitQueue = (1 << 1);
2082+
}
20512083
}
20522084

20532085
void ImGui::PushTableBackground()
@@ -2092,20 +2124,20 @@ void ImGui::TableDrawContextMenu(ImGuiTable* table)
20922124
{
20932125
if (column != NULL)
20942126
{
2095-
const bool can_resize = !(column->Flags & (ImGuiTableColumnFlags_NoResize | ImGuiTableColumnFlags_WidthStretch)) && column->IsVisible;
2127+
const bool can_resize = !(column->Flags & ImGuiTableColumnFlags_NoResize) && column->IsVisible;
20962128
if (MenuItem("Size column to fit", NULL, false, can_resize))
2097-
TableSetColumnAutofit(table, column_n);
2129+
TableSetColumnWidthAutoSingle(table, column_n);
20982130
}
20992131

2100-
if (MenuItem("Size all columns to fit", NULL))
2101-
{
2102-
for (int other_column_n = 0; other_column_n < table->ColumnsCount; other_column_n++)
2103-
{
2104-
ImGuiTableColumn* other_column = &table->Columns[other_column_n];
2105-
if (other_column->IsVisible)
2106-
TableSetColumnAutofit(table, other_column_n);
2107-
}
2108-
}
2132+
const char* size_all_desc;
2133+
if (table->ColumnsVisibleFixedCount == table->ColumnsVisibleCount)
2134+
size_all_desc = "Size all columns to fit"; // All fixed
2135+
else if (table->ColumnsVisibleFixedCount == 0)
2136+
size_all_desc = "Size all columns to default"; // All stretch
2137+
else
2138+
size_all_desc = "Size all columns to fit/default"; // Mixed
2139+
if (MenuItem(size_all_desc, NULL))
2140+
TableSetColumnWidthAutoAll(table);
21092141
want_separator = true;
21102142
}
21112143

0 commit comments

Comments
 (0)