Skip to content

Commit c5dcf2f

Browse files
committed
Tables: rework keep-visible/max-width code to be less incorrect, but right-most column may effectively has few pixels less of visible cliprect width.
See table_width_distrib and table_width_keep_visible tests. + fix minor left-side clipping on post-frozen column + made TableHeader() use reliable column->MaxX
1 parent 197e9c0 commit c5dcf2f

File tree

2 files changed

+42
-18
lines changed

2 files changed

+42
-18
lines changed

imgui_demo.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3690,10 +3690,20 @@ static void ShowDemoWindowTables()
36903690
ImGui::SameLine(); HelpMarker("Disable inner padding between columns (double inner padding if BordersOuterV is on, single inner padding if BordersOuterV is off)");
36913691
ImGui::CheckboxFlags("ImGuiTableFlags_BordersOuterV", &flags, ImGuiTableFlags_BordersOuterV);
36923692
ImGui::CheckboxFlags("ImGuiTableFlags_BordersInnerV", &flags, ImGuiTableFlags_BordersInnerV);
3693+
static bool show_headers = false;
3694+
ImGui::Checkbox("show_headers", &show_headers);
36933695
PopStyleCompact();
36943696

36953697
if (ImGui::BeginTable("##table1", 3, flags))
36963698
{
3699+
if (show_headers)
3700+
{
3701+
ImGui::TableSetupColumn("One");
3702+
ImGui::TableSetupColumn("Two");
3703+
ImGui::TableSetupColumn("Three");
3704+
ImGui::TableHeadersRow();
3705+
}
3706+
36973707
for (int row = 0; row < 5; row++)
36983708
{
36993709
ImGui::TableNextRow();
@@ -3710,8 +3720,8 @@ static void ShowDemoWindowTables()
37103720
sprintf(buf, "Hello %d,%d", row, column);
37113721
ImGui::Button(buf, ImVec2(-FLT_MIN, 0.0f));
37123722
}
3713-
if (ImGui::TableGetHoveredColumn() == column)
3714-
ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, IM_COL32(0, 100, 0, 255));
3723+
//if (ImGui::TableGetHoveredColumn() == column)
3724+
// ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, IM_COL32(0, 100, 0, 255));
37153725
}
37163726
}
37173727
ImGui::EndTable();

imgui_tables.cpp

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
static const int TABLE_DRAW_CHANNEL_BG0 = 0;
9797
static const int TABLE_DRAW_CHANNEL_BG1_FROZEN = 1;
9898
static const int TABLE_DRAW_CHANNEL_UNCLIPPED = 2; // When using ImGuiTableFlags_NoClip
99-
static const float TABLE_BORDER_SIZE = 1.0f; // FIXME-TABLE: Currently hard-coded.
99+
static const float TABLE_BORDER_SIZE = 1.0f; // FIXME-TABLE: Currently hard-coded because of clipping assumptions with outer borders rendering.
100100
static const float TABLE_RESIZE_SEPARATOR_HALF_THICKNESS = 4.0f; // Extend outside inner borders.
101101
static const float TABLE_RESIZE_SEPARATOR_FEEDBACK_TIMER = 0.06f; // Delay/timer before making the hover feedback (color+cursor) visible because tables/columns tends to be more cramped.
102102

@@ -616,7 +616,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
616616
// (can't make auto padding larger than what WorkRect knows about so right-alignment matches)
617617
const ImRect work_rect = table->WorkRect;
618618
const float min_column_width = TableGetMinColumnWidth();
619-
const float min_column_distance = min_column_width + table->CellPaddingX * 2.0f;
619+
const float min_column_distance = min_column_width + table->CellPaddingX * 2.0f + table->CellSpacingX1 + table->CellSpacingX2;
620620

621621
int count_fixed = 0;
622622
float sum_weights_stretched = 0.0f; // Sum of all weights for weighted columns.
@@ -668,7 +668,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
668668
// This is merely making the side-effect less extreme, but doesn't properly fixes it.
669669
// FIXME: Move this to ->WidthGiven to avoid temporary lossyless?
670670
if (column->AutoFitQueue > 0x01 && table->IsInitializing)
671-
column->WidthRequest = ImMax(column->WidthRequest, min_column_width * 4.0f);
671+
column->WidthRequest = ImMax(column->WidthRequest, min_column_width * 4.0f); // FIXME-TABLE: Another constant/scale?
672672

673673
count_fixed += 1;
674674
sum_width_fixed_requests += column->WidthRequest;
@@ -763,6 +763,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
763763
offset_x += table->OuterPaddingX;
764764
offset_x -= table->CellSpacingX1;
765765
ImRect host_clip_rect = table->InnerClipRect;
766+
//host_clip_rect.Max.x += table->CellPaddingX + table->CellSpacingX2;
766767
for (int order_n = 0; order_n < table->ColumnsCount; order_n++)
767768
{
768769
const int column_n = table->DisplayOrderToIndex[order_n];
@@ -786,31 +787,46 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
786787
continue;
787788
}
788789

789-
float max_x = FLT_MAX;
790+
// Maximum width
791+
float max_width = FLT_MAX;
790792
if (table->Flags & ImGuiTableFlags_ScrollX)
791793
{
792794
// Frozen columns can't reach beyond visible width else scrolling will naturally break.
793795
if (order_n < table->FreezeColumnsRequest)
794-
max_x = table->InnerClipRect.Max.x - (table->FreezeColumnsRequest - order_n) * min_column_distance - table->OuterPaddingX;
796+
{
797+
max_width = (table->InnerClipRect.Max.x - (table->FreezeColumnsRequest - order_n) * min_column_distance) - offset_x;
798+
max_width = max_width - table->OuterPaddingX - table->CellPaddingX - table->CellSpacingX2;
799+
}
795800
}
796801
else if ((table->Flags & ImGuiTableFlags_NoKeepColumnsVisible) == 0)
797802
{
798803
// If horizontal scrolling if disabled, we apply a final lossless shrinking of columns in order to make
799804
// sure they are all visible. Because of this we also know that all of the columns will always fit in
800805
// table->WorkRect and therefore in table->InnerRect (because ScrollX is off)
801-
max_x = table->WorkRect.Max.x - (table->ColumnsVisibleCount - (column->IndexWithinVisibleSet + 1)) * min_column_distance - table->OuterPaddingX;
806+
// FIXME-TABLE: This is solved incorrectly but also quite a difficult problem to fix as we also want ClipRect width to match.
807+
// See "table_width_distrib" and "table_width_keep_visible" tests
808+
max_width = table->WorkRect.Max.x - (table->ColumnsVisibleCount - column->IndexWithinVisibleSet - 1) * min_column_distance - offset_x;
809+
//max_width -= table->CellSpacingX1;
810+
max_width -= table->CellSpacingX2;
811+
max_width -= table->CellPaddingX * 2.0f;
812+
max_width -= table->OuterPaddingX;
802813
}
803-
if (offset_x + column->WidthGiven + table->CellPaddingX * 2.0f + table->CellSpacingX1 > max_x)
804-
column->WidthGiven = ImMax(max_x - offset_x - table->CellPaddingX * 2.0f - table->CellSpacingX1, min_column_width);
814+
column->WidthGiven = ImMin(column->WidthGiven, max_width);
815+
816+
// Minimum width
817+
column->WidthGiven = ImMax(column->WidthGiven, ImMin(column->WidthRequest, min_column_width));
805818

806819
// Lock all our positions
820+
// - ClipRect.Min.x: Because merging draw commands doesn't compare min boundaries, we make ClipRect.Min.x match left bounds to be consistent regardless of merging.
821+
// - ClipRect.Max.x: using WorkMaxX instead of MaxX (aka including padding) is detrimental to visibility in very-small column.
822+
// - FIXME-TABLE: We want equal width columns to have equal (ClipRect.Max.x - WorkMinX) width, which means ClipRect.max.x cannot stray off host_clip_rect.Max.x else right-most column may appear shorter.
807823
column->MinX = offset_x;
808824
column->MaxX = offset_x + column->WidthGiven + table->CellSpacingX1 + table->CellSpacingX2 + table->CellPaddingX * 2.0f;
809825
column->WorkMinX = column->MinX + table->CellPaddingX + table->CellSpacingX1;
810826
column->WorkMaxX = column->MaxX - table->CellPaddingX - table->CellSpacingX2; // Expected max
811827
column->ClipRect.Min.x = column->MinX;
812828
column->ClipRect.Min.y = work_rect.Min.y;
813-
column->ClipRect.Max.x = column->MaxX;
829+
column->ClipRect.Max.x = column->MaxX; // column->WorkMaxX;
814830
column->ClipRect.Max.y = FLT_MAX;
815831
column->ClipRect.ClipWithFull(host_clip_rect);
816832

@@ -845,7 +861,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
845861
}
846862

847863
if (visible_n < table->FreezeColumnsCount)
848-
host_clip_rect.Min.x = ImMax(host_clip_rect.Min.x, column->MaxX + 2.0f);
864+
host_clip_rect.Min.x = ImMax(host_clip_rect.Min.x, column->MaxX + TABLE_BORDER_SIZE);
849865

850866
offset_x += column->WidthGiven + table->CellSpacingX1 + table->CellSpacingX2 + table->CellPaddingX * 2.0f;
851867
visible_n++;
@@ -1031,7 +1047,7 @@ void ImGui::EndTable()
10311047
#if 0
10321048
// Strip out dummy channel draw calls
10331049
// We have no way to prevent user submitting direct ImDrawList calls into a hidden column (but ImGui:: calls will be clipped out)
1034-
// FIXME-TABLE: The problem with this approach is we are effectively making it harder for users watching metrics to spot wasted vertices.
1050+
// (The problem with this approach is we are effectively making it harder for users watching metrics to spot wasted vertices)
10351051
if (table->DummyDrawChannel != (ImU8)-1)
10361052
{
10371053
ImDrawChannel* dummy_channel = &table->DrawSplitter._Channels[table->DummyDrawChannel];
@@ -1169,6 +1185,7 @@ void ImGui::TableDrawBorders(ImGuiTable* table)
11691185
}
11701186

11711187
// Draw outer border
1188+
// FIXME-TABLE: could use AddRect or explicit VLine/HLine helper?
11721189
if (table->Flags & ImGuiTableFlags_BordersOuter)
11731190
{
11741191
// Display outer border offset by 1 which is a simple way to display it without adding an extra draw call
@@ -1186,7 +1203,6 @@ void ImGui::TableDrawBorders(ImGuiTable* table)
11861203
}
11871204
else if (table->Flags & ImGuiTableFlags_BordersOuterV)
11881205
{
1189-
// FIXME-TABLE: could use AddRect or explicit VLine/HLine helper?
11901206
outer_drawlist->AddLine(outer_border.Min, ImVec2(outer_border.Min.x, outer_border.Max.y), outer_col, border_size);
11911207
outer_drawlist->AddLine(ImVec2(outer_border.Max.x, outer_border.Min.y), outer_border.Max, outer_col, border_size);
11921208
}
@@ -1199,7 +1215,6 @@ void ImGui::TableDrawBorders(ImGuiTable* table)
11991215
if ((table->Flags & ImGuiTableFlags_BordersInnerH) && table->RowPosY2 < table->OuterRect.Max.y)
12001216
{
12011217
// Draw bottom-most row border
1202-
// FIXME-TABLE: could use AddRect or explicit VLine/HLine helper?
12031218
const float border_y = table->RowPosY2;
12041219
if (border_y >= table->BgClipRect.Min.y && border_y < table->BgClipRect.Max.y)
12051220
inner_drawlist->AddLine(ImVec2(table->BorderX1, border_y), ImVec2(table->BorderX2, border_y), table->BorderColorLight, border_size);
@@ -1790,7 +1805,7 @@ void ImGui::TableEndRow(ImGuiTable* table)
17901805
ImRect cell_bg_rect = TableGetCellBgRect(table, cell_data->Column);
17911806
cell_bg_rect.ClipWith(table->BgClipRect);
17921807
cell_bg_rect.Min.x = ImMax(cell_bg_rect.Min.x, column->ClipRect.Min.x); // So that first column after frozen one gets clipped
1793-
cell_bg_rect.Max.x = ImMin(cell_bg_rect.Max.x, column->ClipRect.Max.x);
1808+
cell_bg_rect.Max.x = ImMin(cell_bg_rect.Max.x, column->MaxX);
17941809
window->DrawList->AddRectFilled(cell_bg_rect.Min, cell_bg_rect.Max, cell_data->BgColor);
17951810
}
17961811
}
@@ -1911,7 +1926,6 @@ void ImGui::TableEndCell(ImGuiTable* table)
19111926
}
19121927

19131928
// Append into the next column/cell
1914-
// FIXME-TABLE: Wrapping to next row could be optional?
19151929
bool ImGui::TableNextColumn()
19161930
{
19171931
ImGuiContext& g = *GImGui;
@@ -2365,7 +2379,7 @@ void ImGui::TableHeader(const char* label)
23652379

23662380
// We feed our unclipped width to the column without writing on CursorMaxPos, so that column is still considering for merging.
23672381
float max_pos_x = label_pos.x + label_size.x + w_sort_text + w_arrow;
2368-
column->ContentMaxXHeadersUsed = ImMax(column->ContentMaxXHeadersUsed, cell_r.Max.x);
2382+
column->ContentMaxXHeadersUsed = ImMax(column->ContentMaxXHeadersUsed, column->WorkMaxX);
23692383
column->ContentMaxXHeadersIdeal = ImMax(column->ContentMaxXHeadersIdeal, max_pos_x);
23702384

23712385
// We don't use BeginPopupContextItem() because we want the popup to stay up even after the column is hidden

0 commit comments

Comments
 (0)