Skip to content

feat: add AutoFitColumns and AutoFitColumnsAsync to fit every column in one call (#380) - #515

Open
michaelstonis wants to merge 4 commits into
mainfrom
feat/380-autofit-all-columns
Open

michaelstonis wants to merge 4 commits into
mainfrom
feat/380-autofit-all-columns

Conversation

@michaelstonis

Copy link
Copy Markdown
Contributor

Closes #380.

Changes

Adds a grid-wide auto-fit: one call sizes every visible column (or the ones passed) to its header and content, makes the widths explicit and switches Star/Auto columns to Fixed, exactly as a resize-grip double-click does for one column.

  • Core, KumikoLayoutEngine: new CalculateAutoFitWidth(column, source, ctx, style, firstVisibleRow, lastVisibleRow, KumikoAutoFitScope scope). AllRows walks every row once per column, skips group header and summary rows (IsNonDataRow), reads display text through KumikoSource.GetCellDisplayText (the cell text cache), reuses one measuring paint. Boolean, image, progress bar and selection columns and columns with a custom cell renderer fit their header only. Internal chunk helpers (MeasureAllRowsText, FinishAutoFitWidth) serve the async path.
  • Core, KumikoRenderer.AutoFit.cs (new partial file, delegates to the layout engine; KumikoRenderer.cs is untouched): AutoFitColumns(...) and AutoFitColumnsAsync(...). The async variant measures about 2,000 cells per turn, yields with Task.Yield() (so the host's UI context keeps the source on its own thread), applies all widths at the end, throws OperationCanceledException leaving every width as it was, and restarts once if the view is rebuilt while measuring (second change returns false).
  • Hosts (MAUI and Uno, same names and defaults): AutoFitColumns(scope = Sampled, columns = null) returns false before the grid has a size; AutoFitColumnsAsync(scope = AllRows, columns = null, cancellationToken = default). The measuring-surface creation is now one private AutoFitMeasurer helper per host, shared with the existing OnAutoFitColumnRequested (grip double-click path unchanged in behavior; the input controller is not touched).
  • Samples: an "Auto-fit all" button on LargeDataPage in both sample apps (AutoFitColumnsAsync(AllRows)).
  • Docs: README "Column sizing" section, CHANGELOG entry.

Public API added (recorded in PublicAPI.Unshipped.txt for Core, Maui, Uno):

  • KumikoUI.Core.Models.KumikoAutoFitScope (Sampled, AllRows)
  • KumikoRenderer.AutoFitColumns(IReadOnlyList<KumikoColumn>, KumikoSource, IDrawingContext, KumikoStyle, KumikoAutoFitScope) -> int
  • KumikoRenderer.AutoFitColumnsAsync(..., KumikoAutoFitScope, CancellationToken) -> Task<bool>
  • KumikoLayoutEngine.CalculateAutoFitWidth(..., int, int, KumikoAutoFitScope) -> float
  • MAUI and Uno KumikoDataGrid.AutoFitColumns and AutoFitColumnsAsync

Left for follow-up (issue marks them optional or open questions): the "Auto-fit all columns" header-menu item (needs a KumikoInputController.ColumnMenu change, which is off-limits for this batch), a ColumnsAutoFitted event, and ICellRenderer.Measure for template columns.

Acceptance criteria

  • AutoFitColumns fits every visible column (or the given ones) in one redraw, with Sampled matching today's double-click result (test compares per-column AutoFitColumn with AutoFitColumns(Sampled)).
  • AllRows measures every data row; AutoFitColumnsAsync yields between 2,000-cell chunks (a test asserts the host queue is serviced between every chunk on 80,000 cells) and honors cancellation without leaving partial widths. A literal 100K-row responsiveness run on a real UI thread was not done; the guarantee is the chunk-and-yield design above.
  • Widths become explicit and survive KumikoLayoutState.Capture/ApplyTo (tested in Core).
  • [~] Behavior tests added in tests/KumikoUI.Core.Tests/Rendering/AutoFitColumnsTests.cs (a new file next to ColumnSizingTests/AutoFitTimingTests, which are unchanged) and MAUI host tests for sync, async and cancellation. No Uno host tests: the Uno grid cannot be constructed in the test project; the Uno host is a line-for-line mirror and is covered by the Core tests and a successful build.
  • Works on MAUI and Uno with the same names and defaults.
  • LargeDataPage on both sample apps has an "Auto-fit all" button.
  • XML docs on every new public member; PublicAPI.Unshipped.txt updated (by hand from the analyzer's RS0016 list, because eng/update-public-api.sh emptied the Core file without refilling it in this environment; the diff is only the six/two added lines per project).
  • README "Column sizing" section updated; CHANGELOG entry under Unreleased.
  • No allocations added to the per-frame render path (nothing in the render loop changed; _lastFirstRow/_lastLastRow are only read).

Verification

  • dotnet build src/KumikoUI.Core, src/KumikoUI.SkiaSharp: 0 warnings, 0 errors.
  • dotnet build src/KumikoUI.Maui -f net10.0-maccatalyst: 0 warnings, 0 errors.
  • dotnet build src/KumikoUI.Uno -f net10.0-desktop: 0 errors (two existing Uno0001 "not implemented in Uno" warnings in untouched files).
  • dotnet build samples/SampleApp.Uno -f net10.0-desktop: 0 errors, 0 warnings.
  • dotnet build samples/SampleApp.Maui -f net10.0-maccatalyst: fails at restore with NU1605 (Microsoft.Maui.Controls 10.0.110 vs 10.0.41 downgrade), a package-version issue in the sample project that this change does not touch; the MAUI sample edit is a mirror of the Uno one and could not be compiled here.
  • dotnet test tests/KumikoUI.Core.Tests: 4373 passed, 5 skipped, 0 failed (12 new tests; the async ones ran 5 times in a row without a failure).
  • dotnet test tests/KumikoUI.Maui.Tests: 455 passed (6 new).
  • dotnet test tests/KumikoUI.Uno.Tests: 321 passed. dotnet test tests/KumikoUI.SkiaSharp.Tests: 42 passed.

Note for Foundations (#446)

The new AutoFitColumns / AutoFitColumnsAsync entry points live in a separate partial file, src/KumikoUI.Core/KumikoRenderer.AutoFit.cs, next to the shipped AutoFitColumn. They use the renderer's last-drawn visible row range, as AutoFitColumn does, and contain no drawing code; KumikoRenderer.cs is unchanged. The painter split should place them alongside AutoFitColumn.

  • The README auto-fit snippet uses the README's kumiko and employees and declares the token source, so both documentation snippet projects build with 0 errors.

public void AllRows_FindsARowOutsideTheSample()
{
var grid = Grid(rows: 1000);
var items = (ObservableCollection<Row>)grid.ItemsSource!;
Comment on lines +71 to +72
foreach (var column in shown)
if (column.IsVisible && (wanted is null || wanted.Contains(column))) targets.Add(column);
Comment on lines +71 to +72
foreach (var column in shown)
if (column.IsVisible && (wanted is null || wanted.Contains(column))) targets.Add(column);

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Columns: add AutoFitAllColumns to fit every column to its content in one call

2 participants