Skip to content

feat: save and restore expanded groups, selection, scroll position and summary rows in the layout state (#354) - #505

Open
michaelstonis wants to merge 2 commits into
mainfrom
feat/354-layout-state
Open

michaelstonis wants to merge 2 commits into
mainfrom
feat/354-layout-state

Conversation

@michaelstonis

Copy link
Copy Markdown
Contributor

Closes #354.

Changes

KumikoLayoutState (format version 2) can now also hold the expanded groups, the selection, the scroll position and the summary rows. The parts are opt-in, so existing callers and version 1 documents behave as before.

  • Parts and options. KumikoLayoutParts (Columns, Grouping, GroupExpansion, Selection, Scroll, Summaries, Default = Columns | Grouping, All) and KumikoLayoutCaptureOptions (Parts, ItemKeySelector). A part is applied only when the options ask for it and the layout holds it (the new properties are null when not captured).
  • Group expansion. ExpandedGroups (each group that differs from the default, named by the keys of its path: KumikoGroupExpansionLayoutState, KumikoGroupKeyLayoutState; key values reuse the filter value encoding, now shared through an internal KumikoLayoutValue) and DefaultGroupExpanded. Restore runs in the same BeginUpdate as the grouping, so there is one rebuild; background rebuilds snapshot the restored state. Saved keys are matched to the live keys by their invariant text (one pass over the items, each distinct key matched once, early exit), so int, enum, date, bool and text keys come back as the real key objects and unknown keys are ignored.
  • Selection. Selection (KumikoSelectionLayoutState: selected keys, current key, current column key), named by ItemKeySelector. Without a selector the part is skipped with one trace warning. Restore uses SetSelectedItems and SetCurrentCell; while a background rebuild is pending it waits for the view to be published (internal KumikoLayoutRestore, driven by ViewUpdatePendingChanged), and a newer restore replaces a waiting one.
  • Scroll. Scroll (KumikoScrollLayoutState: offsets and, with a selector, the first visible data row's key). KumikoScrollLayoutState.ApplyTo(ScrollState, KumikoSource, KumikoStyle, options) goes through KumikoScroller (RefreshContentExtents, ScrollToItem(..., Start, expandGroups: false), then ScrollTo), falling back to the offsets and clamping to the content. No input-controller or renderer code is involved.
  • Summaries. TableSummaryRows / GroupSummaryRows (KumikoSummaryRowLayoutState, KumikoSummaryColumnLayoutState). Rows are replaced; code that cannot be saved (CustomAggregate, CustomCellRenderer) is carried over from the grid's row of the same name with the same property and type, and a Custom aggregate with no such row is left out.
  • Hosts (MAUI and Uno, same overloads). GetLayoutState(KumikoLayoutCaptureOptions?) and ApplyLayoutState(KumikoLayoutState, KumikoLayoutCaptureOptions?); the existing overloads delegate with null options. TableSummaryRows and GroupSummaryRows follow a restore (guarded against the collection-changed handlers rebuilding twice), as GroupDescriptions already did.
  • Samples. The Events page of both sample apps saves and restores the full state (new Group toggle and a salary total row so expansion and summaries have something to restore); ShowcaseData.LayoutOptions is shared.
  • Docs. README "Saving and restoring the layout" (parts table, what is still not saved), CHANGELOG entry under Unreleased.

Public API added (PublicAPI.Unshipped.txt)

  • Core: KumikoLayoutParts, KumikoLayoutCaptureOptions, KumikoGroupExpansionLayoutState, KumikoGroupKeyLayoutState, KumikoSelectionLayoutState, KumikoScrollLayoutState (with ApplyTo), KumikoSummaryRowLayoutState, KumikoSummaryColumnLayoutState; on KumikoLayoutState: LatestVersion, ExpandedGroups, DefaultGroupExpanded, Selection, Scroll, TableSummaryRows, GroupSummaryRows, Capture(source, options), Capture(source, selection, scroll, style, options), ApplyTo(source, options), ApplyTo(source, selection, options).
  • MAUI and Uno KumikoDataGrid: GetLayoutState(options), ApplyLayoutState(state, options).

Deviation from the issue text

The issue proposes CurrentVersion = 2. KumikoLayoutState.CurrentVersion is a shipped constant (= 1), and the frozen 1.0.0 API cannot change it, so it stays 1 and a new LatestVersion = 2 is what Capture writes and what Version defaults to.

Acceptance criteria

  • Version 1 JSON still loads; default capture output is unchanged apart from Version (tests: Version1Json_*, DefaultCapture_IsTheOldShape_PlusVersion2, the existing JSON-shape tests).
  • Each part round-trips: nested expanded and collapsed groups (also int and enum keys, collapsed default with expanded exceptions), selected items and current cell by key, scroll offsets and anchor, table and group summary rows.
  • Missing keys, removed columns and changed data restore gracefully with one rebuild (missing groups, missing selection keys, missing current column, filtered-out items, missing scroll anchor, changed sort order; a view-changed count test for the expansion restore).
  • Works with background rebuilds above BackgroundRebuildThreshold (BackgroundRebuild_SelectionIsRestoredWhenTheViewIsPublished_ExpansionBuildsStraightIn).
  • Behavior tests in tests/KumikoUI.Core.Tests/KumikoLayoutStateTests.cs (plus two host tests in KumikoDataGridMappingTests.cs).
  • Works on MAUI and Uno with the same overloads.
  • Sample page saves and restores the full state on both sample apps (Events page). The Uno sample builds; the MAUI sample could not be built on this machine (see Verification), its change mirrors the Uno one.
  • XML docs on every new public member; PublicAPI.Unshipped.txt updated for Core, Maui and Uno.
  • README "Saving and restoring the layout" updated; CHANGELOG entry under Unreleased.
  • No allocations added to the per-frame render path (no renderer, input controller or hit tester code touched).

Verification

  • dotnet build src/KumikoUI.Core --no-incremental: 0 warnings, 0 errors.
  • dotnet build src/KumikoUI.SkiaSharp --no-incremental: 0 warnings, 0 errors.
  • dotnet build src/KumikoUI.Maui -f net10.0-maccatalyst --no-incremental: 0 warnings, 0 errors.
  • dotnet build src/KumikoUI.Uno -f net10.0-desktop --no-incremental: 0 errors; the only 2 warnings are existing Uno0001 ones in files this change does not touch.
  • dotnet build samples/SampleApp.Uno -f net10.0-desktop: succeeded, 0 warnings.
  • dotnet build samples/SampleApp.Maui -f net10.0-maccatalyst: fails at restore with NU1605 (Microsoft.Maui.Controls 10.0.41 vs 10.0.110 package downgrade), unrelated to this change; not built here.
  • dotnet test tests/KumikoUI.Core.Tests: 4384 passed, 5 skipped (the 41 layout-state tests included, run several times for the background test).
  • dotnet test tests/KumikoUI.Maui.Tests: 451 passed.
  • dotnet test tests/KumikoUI.Uno.Tests: 321 passed.
  • dotnet test tests/KumikoUI.SkiaSharp.Tests: 42 passed.
  • eng/update-public-api.sh did not write anything on this machine (the RS0016 code fix of dotnet format analyzers made no changes), so the PublicAPI.Unshipped.txt lines were taken from the RS0016 build diagnostics and merged in sorted order; the Core, Maui and Uno builds are clean against them.
  • The README layout-parts snippet uses Employee and declares layoutPath, so both documentation snippet projects build with 0 errors.

Comment on lines +187 to +192
foreach (int row in selection.SelectedRows.OrderBy(r => r))
{
if (source.IsNonDataRow(row)) continue;
string? key = selector(source.GetItem(row));
if (key != null) state.SelectedKeys.Add(key);
}
Comment on lines +84 to +115
foreach (var entry in saved)
{
if (entry?.Path is not { Count: > 0 } path || path.Count > _groupDescriptions.Count) continue;
var node = root;
bool valid = true;
for (int level = 0; level < path.Count && valid; level++)
{
var segment = path[level];
valid = segment != null
&& string.Equals(segment.PropertyName, _groupDescriptions[level].PropertyName, StringComparison.Ordinal);
if (!valid) break;

GroupPathNode? child = null;
foreach (var existing in node.Children)
{
if (existing.Kind == segment!.ValueKind && string.Equals(existing.Text, segment.Value, StringComparison.Ordinal))
{
child = existing.Node;
break;
}
}
if (child == null)
{
child = new GroupPathNode();
node.Children.Add((segment!.ValueKind, segment.Value, child));
}
node = child;
}
if (!valid) continue;
if (node.Expanded == null) wanted++;
node.Expanded = entry.IsExpanded;
}

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.

Layout state: save and restore expanded groups, selection, scroll position and summary rows

2 participants