Tables: Removed extra +1.0f pixels initially allocated to make right-most column visible, fix visible padding asymmetry. Tweaked debug code in demo.

Seems visible enough without. Whole thing is/was fishy, may return to it but right cleaning up seems viable.
This commit is contained in:
omar 2020-07-02 13:59:30 +02:00 committed by ocornut
parent 353bb68e90
commit 03c8bfaf23
2 changed files with 34 additions and 27 deletions

View File

@ -3416,22 +3416,27 @@ static void ShowDemoWindowTables()
for (int column = 0; column < 3; column++) for (int column = 0; column < 3; column++)
{ {
ImGui::TableSetColumnIndex(column); ImGui::TableSetColumnIndex(column);
char buf[32];
if (display_width) if (display_width)
{ {
// [DEBUG] Draw limits
ImVec2 p = ImGui::GetCursorScreenPos(); ImVec2 p = ImGui::GetCursorScreenPos();
ImDrawList* draw_list = ImGui::GetWindowDrawList(); float contents_x1 = p.x;
float x1 = p.x; float contents_x2 = ImGui::GetWindowPos().x + ImGui::GetContentRegionMax().x;
float x2 = ImGui::GetWindowPos().x + ImGui::GetContentRegionMax().x; float cliprect_x1 = ImGui::GetWindowDrawList()->GetClipRectMin().x;
float x3 = draw_list->GetClipRectMax().x; float cliprect_x2 = ImGui::GetWindowDrawList()->GetClipRectMax().x;
float y2 = p.y + ImGui::GetTextLineHeight(); float y1 = p.y;
draw_list->AddLine(ImVec2(x1, y2), ImVec2(x3, y2), IM_COL32(255, 255, 0, 255)); // Hard clipping limit float y2 = p.y + ImGui::GetTextLineHeight() + 2.0f;
draw_list->AddLine(ImVec2(x1, y2), ImVec2(x2, y2), IM_COL32(255, 0, 0, 255)); // Normal limit ImDrawList* fg_draw_list = ImGui::GetForegroundDrawList();
ImGui::Text("w=%.2f", x2 - x1); fg_draw_list->AddRect(ImVec2(contents_x1, y1 + 0.0f), ImVec2(contents_x2, y2 + 0.0f), IM_COL32(255, 0, 0, 255)); // Contents limit (e.g. Cell + Padding)
fg_draw_list->AddLine(ImVec2(cliprect_x1, y2 + 1.0f), ImVec2(cliprect_x2, y2 + 1.0f), IM_COL32(255, 255, 0, 255)); // Hard clipping limit
sprintf(buf, "w=%.2f", contents_x2 - contents_x1);
} }
else else
{ {
ImGui::Text("Hello %d,%d", row, column); sprintf(buf, "Hello %d,%d", row, column);
} }
ImGui::TextUnformatted(buf);
} }
} }
ImGui::EndTable(); ImGui::EndTable();
@ -3776,10 +3781,10 @@ static void ShowDemoWindowTables()
if (ImGui::TreeNode("Sizing policies, cell contents")) if (ImGui::TreeNode("Sizing policies, cell contents"))
{ {
HelpMarker("This section allows you to interact and see the effect of StretchX vs FixedX sizing policies depending on whether Scroll is enabled and the contents of your columns."); HelpMarker("This section allows you to interact and see the effect of StretchX vs FixedX sizing policies depending on whether Scroll is enabled and the contents of your columns.");
enum ContentsType { CT_ShortText, CT_LongText, CT_Button, CT_StretchButton, CT_InputText }; enum ContentsType { CT_ShortText, CT_LongText, CT_Button, CT_FillButton, CT_InputText };
static int contents_type = CT_StretchButton; static int contents_type = CT_FillButton;
ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12); ImGui::SetNextItemWidth(ImGui::GetFontSize() * 12);
ImGui::Combo("Contents", &contents_type, "Short Text\0Long Text\0Button\0Stretch Button\0InputText\0"); ImGui::Combo("Contents", &contents_type, "Short Text\0Long Text\0Button\0Fill Button\0InputText\0");
static ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_RowBg; static ImGuiTableFlags flags = ImGuiTableFlags_ScrollY | ImGuiTableFlags_BordersOuter | ImGuiTableFlags_RowBg;
ImGui::CheckboxFlags("ImGuiTableFlags_BordersHInner", (unsigned int*)&flags, ImGuiTableFlags_BordersHInner); ImGui::CheckboxFlags("ImGuiTableFlags_BordersHInner", (unsigned int*)&flags, ImGuiTableFlags_BordersHInner);
@ -3810,11 +3815,11 @@ static void ShowDemoWindowTables()
sprintf(label, "Hello %d,%d", row, column); sprintf(label, "Hello %d,%d", row, column);
switch (contents_type) switch (contents_type)
{ {
case CT_ShortText: ImGui::TextUnformatted(label); break; case CT_ShortText: ImGui::TextUnformatted(label); break;
case CT_LongText: ImGui::Text("Some longer text %d,%d\nOver two lines..", row, column); break; case CT_LongText: ImGui::Text("Some longer text %d,%d\nOver two lines..", row, column); break;
case CT_Button: ImGui::Button(label); break; case CT_Button: ImGui::Button(label); break;
case CT_StretchButton: ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f)); break; case CT_FillButton: ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f)); break;
case CT_InputText: ImGui::SetNextItemWidth(-FLT_MIN); ImGui::InputText("##", text_buf, IM_ARRAYSIZE(text_buf)); break; case CT_InputText: ImGui::SetNextItemWidth(-FLT_MIN); ImGui::InputText("##", text_buf, IM_ARRAYSIZE(text_buf)); break;
} }
} }
} }
@ -4163,9 +4168,9 @@ static void ShowDemoWindowTables()
| ImGuiTableFlags_SizingPolicyFixedX | ImGuiTableFlags_SizingPolicyFixedX
; ;
enum ContentsType { CT_Text, CT_Button, CT_SmallButton, CT_Selectable }; enum ContentsType { CT_Text, CT_Button, CT_SmallButton, CT_FillButton, CT_Selectable };
static int contents_type = CT_Button; static int contents_type = CT_FillButton;
const char* contents_type_names[] = { "Text", "Button", "SmallButton", "Selectable" }; const char* contents_type_names[] = { "Text", "Button", "SmallButton", "FillButton", "Selectable" };
static int items_count = IM_ARRAYSIZE(template_items_names); static int items_count = IM_ARRAYSIZE(template_items_names);
static ImVec2 outer_size_value = ImVec2(0, 250); static ImVec2 outer_size_value = ImVec2(0, 250);
@ -4354,6 +4359,8 @@ static void ShowDemoWindowTables()
ImGui::Button(label); ImGui::Button(label);
else if (contents_type == CT_SmallButton) else if (contents_type == CT_SmallButton)
ImGui::SmallButton(label); ImGui::SmallButton(label);
else if (contents_type == CT_FillButton)
ImGui::Button(label, ImVec2(-FLT_MIN, 0.0f));
else if (contents_type == CT_Selectable) else if (contents_type == CT_Selectable)
{ {
if (ImGui::Selectable(label, item_is_selected, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap, ImVec2(0, row_min_height))) if (ImGui::Selectable(label, item_is_selected, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap, ImVec2(0, row_min_height)))

View File

@ -70,10 +70,11 @@
// | - TableUpdateColumnsWeightFromWidth()- recompute columns weights (of weighted columns) from their respective width // | - TableUpdateColumnsWeightFromWidth()- recompute columns weights (of weighted columns) from their respective width
// - TableSetupColumn() user submit columns details (optional) // - TableSetupColumn() user submit columns details (optional)
// - TableAutoHeaders() or TableHeader() user submit a headers row (optional) // - TableAutoHeaders() or TableHeader() user submit a headers row (optional)
// | TableSortSpecsClickColumn() - when clicked: alter sort order and sort direction // | TableSortSpecsClickColumn() - when left-clicked: alter sort order and sort direction
// | TableOpenContextMenu() - when right-clicked: trigger opening of the default context menu
// - TableGetSortSpecs() user queries updated sort specs (optional, generally after submitting headers) // - TableGetSortSpecs() user queries updated sort specs (optional, generally after submitting headers)
// - TableNextRow() / TableNextCell() user begin into the first row, also automatically called by TableAutoHeaders() // - TableNextRow() / TableNextCell() user begin into the first row, also automatically called by TableAutoHeaders()
// | TableUpdateLayout() - lock all widths and columns positions! called by the FIRST call to TableNextRow()! // | TableUpdateLayout() - lock all widths, columns positions, clipping rectangles. called by the FIRST call to TableNextRow()!
// | - TableUpdateDrawChannels() - setup ImDrawList channels // | - TableUpdateDrawChannels() - setup ImDrawList channels
// | - TableUpdateBorders() - detect hovering columns for resize, ahead of contents submission // | - TableUpdateBorders() - detect hovering columns for resize, ahead of contents submission
// | - TableDrawContextMenu() - draw right-click context menu // | - TableDrawContextMenu() - draw right-click context menu
@ -650,13 +651,12 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
table->ColumnsAutoFitWidth += spacing_auto_x * (table->ColumnsVisibleCount - 1); table->ColumnsAutoFitWidth += spacing_auto_x * (table->ColumnsVisibleCount - 1);
// Layout // Layout
// Remove -1.0f to cancel out the +1.0f we are doing in EndTable() to make last column line visible
const float width_spacings = table->CellSpacingX * (table->ColumnsVisibleCount - 1); const float width_spacings = table->CellSpacingX * (table->ColumnsVisibleCount - 1);
float width_avail; float width_avail;
if ((table->Flags & ImGuiTableFlags_ScrollX) && table->InnerWidth == 0.0f) if ((table->Flags & ImGuiTableFlags_ScrollX) && table->InnerWidth == 0.0f)
width_avail = table->InnerClipRect.GetWidth() - width_spacings - 1.0f; width_avail = table->InnerClipRect.GetWidth() - width_spacings;
else else
width_avail = work_rect.GetWidth() - width_spacings - 1.0f; width_avail = work_rect.GetWidth() - width_spacings;
const float width_avail_for_stretched_columns = width_avail - sum_width_fixed_requests; const float width_avail_for_stretched_columns = width_avail - sum_width_fixed_requests;
float width_remaining_for_stretched_columns = width_avail_for_stretched_columns; float width_remaining_for_stretched_columns = width_avail_for_stretched_columns;
@ -1023,7 +1023,7 @@ void ImGui::EndTable()
// Add an extra 1 pixel so we can see the last column vertical line if it lies on the right-most edge. // Add an extra 1 pixel so we can see the last column vertical line if it lies on the right-most edge.
if (table->VisibleMaskByIndex & ((ImU64)1 << column_n)) if (table->VisibleMaskByIndex & ((ImU64)1 << column_n))
max_pos_x = ImMax(max_pos_x, column->MaxX + 1.0f); max_pos_x = ImMax(max_pos_x, column->MaxX);
} }
// Flatten channels and merge draw calls // Flatten channels and merge draw calls
@ -1079,7 +1079,7 @@ void ImGui::EndTable()
if (table->Flags & ImGuiTableFlags_ScrollX) if (table->Flags & ImGuiTableFlags_ScrollX)
{ {
inner_window->DC.CursorMaxPos.x = max_pos_x; // Set contents width for scrolling inner_window->DC.CursorMaxPos.x = max_pos_x; // Set contents width for scrolling
outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos_x, backup_outer_cursor_pos_x + table->ColumnsTotalWidth + 1.0f + inner_window->ScrollbarSizes.x); // For auto-fit outer_window->DC.CursorMaxPos.x = ImMax(backup_outer_max_pos_x, backup_outer_cursor_pos_x + table->ColumnsTotalWidth + inner_window->ScrollbarSizes.x); // For auto-fit
} }
else else
{ {