Tables: Context-menu: added "Reset" sub-menu and "Reset Visibility" option.

This commit is contained in:
ocornut
2026-06-11 18:39:53 +02:00
parent b10137f92f
commit e5ff2d07d7
6 changed files with 42 additions and 18 deletions

View File

@@ -704,7 +704,7 @@ void ImGui::TableBeginApplyRequests(ImGuiTable* table)
// table->ReorderColumn = -1;
}
// Handle display order reset request
// Handle display order / visibility reset requests
if (table->IsResetDisplayOrderRequest)
{
for (int n = 0; n < table->ColumnsCount; n++)
@@ -712,6 +712,13 @@ void ImGui::TableBeginApplyRequests(ImGuiTable* table)
table->IsResetDisplayOrderRequest = false;
table->IsSettingsDirty = true;
}
if (table->IsResetVisibilityRequest)
{
for (ImGuiTableColumn& column : table->Columns)
column.IsUserEnabled = column.IsUserEnabledNextFrame = (column.Flags & ImGuiTableColumnFlags_DefaultHide) ? 0 : 1;
table->IsResetVisibilityRequest = false;
table->IsSettingsDirty = true;
}
}
// Apply immediately. See TableQueueSetColumnDisplayOrder() for additional checks/constraints.
@@ -853,7 +860,7 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
table->RefScale = new_ref_scale_unit;
const ImGuiTableFlags table_sizing_policy = (table->Flags & ImGuiTableFlags_SizingMask_);
table->IsDefaultDisplayOrder = true;
table->IsDefaultDisplayOrder = table->IsDefaultVisibility = true;
table->ColumnsEnabledCount = 0;
ImBitArrayClearAllBits(table->EnabledMaskByIndex, columns_count);
ImBitArrayClearAllBits(table->EnabledMaskByDisplayOrder, columns_count);
@@ -872,8 +879,6 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
for (int order_n = 0; order_n < columns_count; order_n++)
{
const int column_n = table->DisplayOrderToIndex[order_n];
if (column_n != order_n)
table->IsDefaultDisplayOrder = false;
ImGuiTableColumn* column = &table->Columns[column_n];
// Clear column setup if not submitted by user. Currently we make it mandatory to call TableSetupColumn() every frame.
@@ -897,6 +902,11 @@ void ImGui::TableUpdateLayout(ImGuiTable* table)
}
column->IsEnabled = column->IsUserEnabled && (column->Flags & ImGuiTableColumnFlags_Disabled) == 0;
if (column->IsEnabled != ((column->Flags & ImGuiTableColumnFlags_DefaultHide) ? 0 : 1))
table->IsDefaultVisibility = false;
if (column_n != order_n)
table->IsDefaultDisplayOrder = false;
if (column->SortOrder != -1 && !column->IsEnabled)
table->IsSortSpecsDirty = true;
if (column->SortOrder > 0 && !(table->Flags & ImGuiTableFlags_SortMulti))
@@ -3598,17 +3608,20 @@ void ImGui::TableDrawDefaultContextMenu(ImGuiTable* table, ImGuiTableFlags flags
want_separator = true;
}
// Ordering
if (flags_for_section_to_display & ImGuiTableFlags_Reorderable)
{
if (MenuItem(LocalizeGetMsg(ImGuiLocKey_TableResetOrder), NULL, false, !table->IsDefaultDisplayOrder))
table->IsResetDisplayOrderRequest = true;
want_separator = true;
}
// Reset all (should work but seems unnecessary/noisy to expose?)
//if (MenuItem("Reset all"))
// table->IsResetAllRequest = true;
// Reset Order/Visibility etc.
if (flags_for_section_to_display & (ImGuiTableFlags_Reorderable | ImGuiTableFlags_Hideable))
if (BeginMenu(LocalizeGetMsg(ImGuiLocKey_TableReset)))
{
//if (MenuItem(LocalizeGetMsg(ImGuiLocKey_TableResetAll))) // FIXME: Hiding because altering Sort Order + requesting auto-fit simultaneously has a tendency to exhibit a sizing glitch.
// table->IsResetAllRequest = true;
if (flags_for_section_to_display & ImGuiTableFlags_Reorderable)
if (MenuItem(LocalizeGetMsg(ImGuiLocKey_TableResetOrder), NULL, false, !table->IsDefaultDisplayOrder)) // PS: cannot be hidden because it would mess with drag reordering.
table->IsResetDisplayOrderRequest = true;
if (flags_for_section_to_display & ImGuiTableFlags_Hideable)
if (MenuItem(LocalizeGetMsg(ImGuiLocKey_TableResetVisibility), NULL, false, !table->IsDefaultVisibility))
table->IsResetVisibilityRequest = true;
EndMenu();
}
// Sorting
// (modify TableOpenContextMenu() to add _Sortable flag if enabling this)