Repository navigation
feat: add a bindable KumikoViewState that the grid applies and writes back (#393) - #517
Open
michaelstonis wants to merge 2 commits into
Open
michaelstonis wants to merge 2 commits into
michaelstonis wants to merge 2 commits into
Conversation
| var (source, binder) = CreateDisconnectedGrid(state); | ||
| for (int i = 0; i < 3; i++) | ||
| { | ||
| GC.Collect(); |
Comment on lines
+151
to
+158
| foreach (var sort in Sorts) | ||
| { | ||
| if (sort is null || string.IsNullOrEmpty(sort.ColumnKey) || sort.Direction == KumikoSortDirection.None) continue; | ||
| var entry = Entry(sort.ColumnKey); | ||
| if (entry.SortDirection != KumikoSortDirection.None) continue; // the first entry for a column wins | ||
| entry.SortDirection = sort.Direction; | ||
| entry.SortOrder = order++; | ||
| } |
Comment on lines
+159
to
+164
| foreach (var filter in Filters) | ||
| { | ||
| if (filter is null || string.IsNullOrEmpty(filter.ColumnKey)) continue; | ||
| var entry = Entry(filter.ColumnKey); | ||
| entry.Filter ??= Copy(filter.Filter); | ||
| } |
Comment on lines
+165
to
+174
| foreach (var group in Groups) | ||
| { | ||
| if (group is null || string.IsNullOrEmpty(group.PropertyName)) continue; | ||
| layout.Groups!.Add(new KumikoGroupLayoutState | ||
| { | ||
| PropertyName = group.PropertyName, | ||
| Header = group.Header, | ||
| SortDirection = group.GroupSortDirection, | ||
| }); | ||
| } |
Comment on lines
+156
to
+161
| foreach (var sort in state.Sorts) | ||
| { | ||
| if (sort is null || sort.Direction == KumikoSortDirection.None || string.IsNullOrEmpty(sort.ColumnKey)) continue; | ||
| if (!byKey.TryGetValue(sort.ColumnKey, out var column) || !seen.Add(column)) continue; | ||
| desired.Add((column, sort.Direction)); | ||
| } |
Comment on lines
+178
to
+182
| foreach (var filter in state.Filters) | ||
| { | ||
| if (filter is null || string.IsNullOrEmpty(filter.ColumnKey)) continue; | ||
| if (byKey.TryGetValue(filter.ColumnKey, out var column)) desired.TryAdd(column, filter.Filter); | ||
| } |
Comment on lines
+226
to
+230
| foreach (var sort in state.Sorts) | ||
| { | ||
| if (sort is not null && !string.IsNullOrEmpty(sort.ColumnKey) && !byKey.ContainsKey(sort.ColumnKey)) | ||
| sorts.Add(sort); // a column the grid does not have (yet) | ||
| } |
Comment on lines
+244
to
+254
| foreach (var filter in state.Filters) | ||
| { | ||
| if (filter is null || string.IsNullOrEmpty(filter.ColumnKey) || written.Contains(filter.ColumnKey)) continue; | ||
| // Kept: a column the grid does not have, or one that does not allow filtering (the user could | ||
| // not have removed its filter). | ||
| if (!byKey.TryGetValue(filter.ColumnKey, out var column) || !column.AllowFiltering) | ||
| { | ||
| written.Add(filter.ColumnKey); | ||
| filters.Add(filter); | ||
| } | ||
| } |
Comment on lines
+293
to
+297
| foreach (var column in source.Columns) | ||
| { | ||
| if (KumikoFilterLayoutState.From(column.ActiveFilter) is { } filter) | ||
| snapshot.Filters.Add((column, filter)); | ||
| } |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #393.
Changes
A view model can now own the grid's sort, column filters, filter predicate and grouping as a
KumikoViewState, bound to a newViewStateproperty on both hosts. The grid applies the state, and it writes the user's changes back into it. Renderer, input controller and hit tester are not touched.Core (
KumikoUI.Core.Models)KumikoViewState(INotifyPropertyChanged, used from the UI thread only):Sorts(ObservableCollection<KumikoSortState>, in priority order)Filters(ObservableCollection<KumikoColumnFilterState>)Groups(ObservableCollection<GroupDescription>)PredicateVersionandChanged: raised once per change, or once perDeferChanges()batchClear()ToLayoutState(),static FromLayoutState(layout)andLoadLayoutState(layout)(in place, as one change)KumikoSortState(ColumnKey, Direction): a record.KumikoColumnFilterState(ColumnKey, Filter): the column-free twin ofFilterDescription. It reusesKumikoFilterLayoutState, so filters keep the shape and value encoding that saved layouts use.FromValues(key, values)andFromCondition(key, condition1, condition2?, combination)build one without hand-encoding values.Key, elsePropertyName, the internalLayoutKeyrule thatKumikoLayoutStateuses. No reflection is involved.KumikoViewStateBinderdoes the two-way sync. The hosts reach it throughInternalsVisibleTo.BeginUpdate/EndUpdatebatch, so one rebuild (in the background aboveBackgroundRebuildThreshold). This happens on bind, on everyChanged, on reconnect, and when the source's columns are replaced. Only the parts that differ are touched, so a state that already matches costs no rebuild. Sorts go throughClearSortandSetSort, filters throughSetColumnFilter, the predicate throughSetFilter, and groups throughSetGroupDescriptions, so the existing events still fire. This mirrors whatKumikoLayoutState.ApplyTodoes.ViewChangedwith kindRebuild, the binder compares a snapshot of the source with the one taken after its last apply or write. If they differ, it writes the source's sort, filters, groups and predicate into the state as oneDeferChangesbatch, so oneChanged. Entries for unknown columns are kept, and so are filters on columns that do not allow filtering. Filter entries that did not change stay the same objects. A canceledSortColumnsChangingdoes not rebuild, so nothing is written. Programmatic changes (includingApplyLayoutState) are written back the same way. Write-back runs once per rebuild, which coalesces a source batch into one write.Changedand records the newVersionas already shown.GroupDescriptionwith a new direction) is detected through the snapshot, so the other grid rebuilds too.Disconnect()unsubscribes from the state, so a long-lived view model does not keep the grid alive.Reconnect()subscribes again and re-applies.KumikoSource:KumikoSource.ViewState.cs:internal FilterPredicateandinternal event ColumnsReplaced.SetColumnsnow runs inside its ownBeginUpdate/EndUpdateand raisesColumnsReplacedinside that batch. A bound state is re-applied to the new column objects, for example when columns arrive after the state is bound or are generated later, and it costs no second rebuild.KumikoFilterLayoutState.ValueEqualsandKumikoFilterConditionLayoutState.ValueEquals: new internal helpers.Hosts (MAUI and Uno, same names)
ViewStateproperty:BindablePropertywithBindingMode.TwoWaydefault on MAUI,DependencyPropertyon Uno (docs sayMode=TwoWay).Cleanup/Unloadeddisconnect the binder.Reconnect/Loadedreconnect it after the bound collections are re-synced, so a state that changed while the page was away wins.GroupDescriptions: with Grouping: GroupDescriptions keeps a group the user removed in the group panel #418 already merged, the host collection mirrors every source grouping change. While a state is bound,GroupDescriptionstherefore mirrorsViewState.Groups, and a change to either reaches the other. It was not made a view overViewState.Groups, so behavior without a state is unchanged.Samples and docs
ViewStateto a shared state that outlives the page, so sort and filters survive navigation. The page also has a "Top salaries" button (aDeferChangesbatch from the view model), "Reset view", and a line showing the state as the view model sees it. That line is subscribed only while the page is shown.ViewStaterow in the host properties tablePublic API added (
PublicAPI.Unshipped.txt)KumikoViewState(ctor,Sorts,Filters,Groups,Predicate,Version,Changed,PropertyChanged,DeferChanges,Clear,ToLayoutState,FromLayoutState,LoadLayoutState);KumikoSortState(record members);KumikoColumnFilterState(ctor,ColumnKey,Filter,FromValues,FromCondition).KumikoDataGrid.ViewState,ViewStateProperty. Uno:KumikoDataGrid.ViewState,ViewStateProperty.Relation to #359 (
KumikoQuery) and #354 (layout parts)KumikoQueryversusKumikoViewState.KumikoQueryis an immutable, read-only snapshot for sending a query to a server.KumikoViewStateis the mutable, bindable object a view model owns, and the grid both reads it and writes it. They do not overlap in API.KumikoQuerykeys a sort byPropertyName(elseKey), while this issue keys byKey(elsePropertyName), the saved-layout rule the issue asks for. For a column with both set and different, that difference matters when converging.ViewChanged(Rebuild)toKumikoSource.QueryChanged. That event has exactly the semantics the binder emulates: once per batch, nothing for a canceled change or a no-op, withKumikoQueryChangesflags that let the binder skip unchanged parts.KumikoViewStategains aToQuery()(or the query is built from the source as today), so a view model that owns the view can also send it to a remote source.RemoteSource, the same state then drives the server query with no extra code. The binder already goes throughSetSort,SetColumnFilterandSetGroupDescriptions, which Data: define a remote view contract that pushes sort, filter, grouping and row windows to the server #359 routes to the server. One caveat: Data: define a remote view contract that pushes sort, filter, grouping and row windows to the server #359 throws onSetFilter(predicate)under a filtering remote source, so a predicate in the state must stay null there.KumikoLayoutStatecapture and apply are not changed here.ToLayoutState()writes only the column entries for sorted or filtered columns, plusGroups. AKumikoLayoutParts-aware version could later write just theDefaultparts. Layout: optionally save and restore the grid layout automatically through a storage provider #411 (automatic persistence) can save aKumikoViewStatethroughToLayoutState().ToJson().Acceptance criteria
ViewStateapplies sort, filters, predicate and grouping in one rebuild; changing it later re-applies; tests intests/KumikoUI.Core.Tests. Covered by the newKumikoViewStateTests.cs(19 tests), which countsViewChangedrebuilds: binding is one rebuild, each change or batch is one, and a matching state is zero. It also covers background rebuild. The tests went into a new file instead ofKumikoLayoutStateTests/ViewChangedTestsso the binder's behavior stays in one place.ToggleSort,SetColumnFilter,RemoveGroupDescription(what the chip calls),ClearAllFilters, a source batch giving one write, a canceled sort writing nothing, andApplyLayoutState.Changedand one rebuild per grid for each change, from either grid or from the view model. A group reversed in place in one grid also rebuilds the other.GroupDescriptionscollection reflects group panel chip removals. Already true since Grouping: GroupDescriptions keeps a group the user removed in the group panel #418 / PR fix: keep the bound GroupDescriptions in step with grouping changed in the group panel (#418) #419. The new MAUI host test also checks it with aViewStatebound, in both directions.GroupDescriptions,ApplyLayoutState, null, and the default binding mode.KumikoUI.Uno.Testscannot construct the Uno grid on its plain test host, so Uno's behavior is covered by the shared Core binder tests and by the Uno sample build. Neither sample app was run interactively.PublicAPI.Unshipped.txtupdated witheng/update-public-api.sh. The docs are in place. On this machineeng/update-public-api.sh Coreemptied the file instead of filling it (the same issue the Layout state: save and restore expanded groups, selection, scroll position and summary rows #354 and Data: define a remote view contract that pushes sort, filter, grouping and row windows to the server #359 sessions hit), so the change was reverted and the entries were added from the RS0016 diagnostics, sorted. Core, Maui and Uno build clean against them.KumikoColumn.LayoutKeystrings, with no reflection. The Core Release build has 0 warnings with the trimming and AOT analyzers on.eng/AotSmokewas not run.Verification
dotnet build src/KumikoUI.Core(Debug and Release): succeeded, 0 warnings.dotnet build src/KumikoUI.Maui -f net10.0-maccatalyst: succeeded, 0 warnings.dotnet build src/KumikoUI.Uno -f net10.0-desktop: succeeded. The only warnings are the existing 2Uno0001ones inEditing.csandInput.cs.dotnet build samples/SampleApp.Uno -f net10.0-desktop: succeeded.dotnet build samples/SampleApp.Maui -f net10.0-maccatalyst: restore fails with NU1605 on this machine's MAUI workload (Microsoft.Maui.Controls 10.0.41 against the 10.0.110 that SkiaSharp.Views.Maui needs). That is unrelated to this change. With-p:NoWarn=NU1605(local check only, nothing committed) it builds, with no warnings in the changed page.dotnet test tests/KumikoUI.Core.Tests: 4,380 passed, 5 skipped, 0 failed (19 new tests).dotnet test tests/KumikoUI.SkiaSharp.Tests: 42 passed.dotnet test tests/KumikoUI.Maui.Tests: 456 passed (6 new tests, 7 cases).dotnet test tests/KumikoUI.Uno.Tests: 321 passed.dotnet run --project eng/DocsSnippets -c Release, thendotnet build tests/KumikoUI.Docs.Snippets.Uno -c Releaseanddotnet build tests/KumikoUI.Docs.Snippets.Maui -c Release: both succeeded, new README snippets included.dotnet run --project eng/TypeNameCheck -c Release -- src/KumikoUI.Core/bin/Release/net10.0/KumikoUI.Core.dll: no public type name collides.