Skip to content

feat: save and restore the grid layout automatically through a storage provider (#411) - #525

Open
michaelstonis wants to merge 3 commits into
feat/354-layout-statefrom
feat/411-layout-autosave
Open

michaelstonis wants to merge 3 commits into
feat/354-layout-statefrom
feat/411-layout-autosave

Conversation

@michaelstonis

@michaelstonis michaelstonis commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #411.

Stacked on #505: until that PR merges, this one also shows its commits.

Changes

Setting LayoutStateKey on the MAUI or Uno grid restores the layout saved under that key once, when the grid is attached, and then saves it again after the user changes it. The logic is in Core (KumikoLayoutAutoSave, internal); the hosts add the properties, a default store, a timer and the way back onto the UI thread.

  • Restore. Runs when the grid attaches (MAUI: handler connected, before the surface draws; Uno: Loaded) and a key is set. A store that answers at once (the default stores) puts the layout back before the first frame; a later answer is applied on the UI thread when it arrives, and a newer key or an unload cancels it. Changes made before the restore finishes are not saved, and SaveLayoutStateAsync() does nothing then, so the defaults can never overwrite a saved layout. If the grid has no columns yet (generated from items) the restore waits for them (AutoGeneratedColumns and Columns changes) and applies one UI turn later. Applying the layout is not a change, saves nothing and makes no announcement (the hosts' Announce is muted while it runs). Restore happens once per key, not on every re-attach.
  • Change detection (no input or renderer hooks). Existing events only: SortColumnsChanged, FilterChanged, ColumnsChanged (visibility, freeze, order), ColumnOrderChanged, group descriptions, and, when LayoutStateOptions asks for the part, GroupExpandedChanged, the selection's SelectionChanged and CurrentCellChanged, ScrollState.ScrollChanged, and the hosts' summary-row collection handlers. Width has no public event, so KumikoSource raises a new internal ColumnWidthChanged from the existing column property hook (KumikoSource.Columns.cs).
  • Debounce. The first change arms one timer of LayoutSaveDelay; a change during the window only marks it busy, and a timer that finds the window busy arms again. Saving happens after a whole delay with no change (so a resize drag or a scroll writes once, and a continuous scroll allocates nothing per event). The save also runs when the grid is removed from the page, on key change (under the old key, then the new key loads), and from SaveLayoutStateAsync().
  • Never blocks the UI thread. The store is called on the UI thread and never waited for: a completed ValueTask is read at once, otherwise a continuation posts back to the UI thread. Writes are serialized and never overlap or reorder; while one is in flight only the newest later snapshot is kept.
  • Errors. A corrupt or foreign saved value (JsonException), a store that throws or faults, a layout that cannot be applied and a capture failure are caught and raised as LayoutStateError (KumikoLayoutStateErrorEventArgs: Operation Load/Restore/Save, Key, Exception) and traced. The grid keeps its layout; the next change overwrites a corrupt value. A throwing error handler is caught too.
  • Default stores.
    • MAUI: Preferences (Android, iOS, Mac Catalyst); on Windows a JSON file in FileSystem.AppDataDirectory, because a Windows settings value is limited to a few kilobytes. The file store (KumikoFileLayoutStore, internal, in Core) hashes the key into the file name and replaces the file atomically.
    • Uno: ApplicationData.Current.LocalSettings, with a file in ApplicationData.Current.LocalFolder for a layout over 3800 characters (the 8 KB per value limit), marked by a settings flag so a short layout is read synchronously. Both are Uno's own implementations of the API on every target (checked: net10.0-desktop, net10.0-browserwasm and net10.0 compile; the code was not run on a device or in a browser). Not available: an unpackaged WinAppSDK app, where ApplicationData.Current throws; the error is reported through LayoutStateError and the app sets LayoutStateStore. Documented in the README and on LayoutStateStore.
  • Samples. The Grouping page of both sample apps sets LayoutStateKey="Samples.Grouping".
  • Docs. README "Saving and restoring automatically" (usage, store interface, timing, error handling, default-store table with where each is unavailable), CHANGELOG entry under Unreleased.
  • Strict scope. No file under src/KumikoUI.Core/Input/ and no KumikoRenderer*.cs was touched.

Public API added (PublicAPI.Unshipped.txt)

  • Core: IKumikoLayoutStore (LoadAsync, SaveAsync), KumikoLayoutStateErrorEventArgs, KumikoLayoutStateOperation.
  • MAUI and Uno KumikoDataGrid: LayoutStateKey, LayoutStateStore, LayoutSaveDelay, LayoutStateOptions (bindable and dependency properties, each with its ...Property field), LayoutStateError event, SaveLayoutStateAsync().

Deviations from the issue text

  • LayoutChanged on KumikoSource is not added: the task forbids new input hooks and the existing events are enough, so the aggregation is internal to KumikoLayoutAutoSave. It could be exposed later.
  • Added LayoutStateOptions (which parts to save, from Layout state: save and restore expanded groups, selection, scroll position and summary rows #354), which the issue does not list.
  • The timing question is answered as "require a synchronous default store and accept a late restore for an async custom store" (no first-draw delay), documented in the README. Debounce is "a whole quiet delay" (the save lands between one and two delays after the last change), not a fixed delay from the last change; this keeps per-change work allocation-free.
  • Core tests are in the new tests/KumikoUI.Core.Tests/KumikoLayoutAutoSaveTests.cs rather than KumikoLayoutStateTests.cs; host tests are in tests/KumikoUI.Maui.Tests/KumikoDataGridLayoutAutoSaveTests.cs. TestDispatcher now records the timers it creates so a host test can fire the one a grid started.

Acceptance criteria

  • Behavior tests: LayoutChanged fires once per user change (see deviation: the debounced save fires once per burst, tested with a 13-step resize drag, single changes and a 200-change instant burst); debounced save; flush on unload; restore before user interaction (synchronous store applied on attach; async store applied when it answers, changes before that ignored); key change (old key saved, new loaded, stale timer saves nothing); corrupt JSON ignored and overwritten by the next save; store exceptions (synchronous, faulted task, throwing error handler, failing default store) do not crash. Tests are deterministic: fake clock-free timer, fake UI-thread queue, fake stores.
  • [~] Works on MAUI and Uno with the default stores and a custom IKumikoLayoutStore. Custom store: tested on MAUI host and Core. MAUI default store: compiled for net10.0-maccatalyst; its Preferences path is not runnable in the xUnit host (the test checks the failure is reported, not thrown). Uno: compiles on desktop, browserwasm and net10.0; the Uno host and UnoLayoutStore have no test (the Uno test project does not compile KumikoDataGrid), the shared Core logic is covered. Nothing was run on a device.
  • Sample app: the Grouping page of both apps uses LayoutStateKey (Uno sample builds; the MAUI sample does not build on this machine for an unrelated restore error, see Verification).
  • XML docs on every new public member.
  • PublicAPI.Unshipped.txt updated for Core, Maui and Uno (lines taken from the RS0016 diagnostics, as in Layout state: save and restore expanded groups, selection, scroll position and summary rows #354).
  • README "Saving and restoring the layout" updated.
  • CHANGELOG entry under Unreleased.
  • Keyboard and screen-reader behavior unchanged; restoring makes no announcements (host test: with AnnounceChanges on, an announcement throws on the test host, and a restored sorted layout does not).
  • No allocations added to the per-frame render path (no renderer, input controller or hit tester code touched; change handlers allocate nothing per event, one timer per delay window).

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 the existing Uno0001 ones in files this change does not touch. -f net10.0-browserwasm and -f net10.0 also build with 0 errors.
  • dotnet build samples/SampleApp.Uno -f net10.0-desktop: succeeded (same 2 existing warnings).
  • dotnet build samples/SampleApp.Maui -f net10.0-maccatalyst: fails at restore with NU1605 (package downgrade), unrelated; not built here.
  • dotnet test tests/KumikoUI.Core.Tests: 4421 passed, 5 skipped (37 new).
  • dotnet test tests/KumikoUI.Maui.Tests: 464 passed (13 new).
  • dotnet test tests/KumikoUI.Uno.Tests: 321 passed.
  • dotnet test tests/KumikoUI.SkiaSharp.Tests: 42 passed.
  • The README autosave snippet uses null for the store and Console.WriteLine for the error, so both documentation snippet projects build with 0 errors.

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.

1 participant