Repository navigation
fix(treeview): Onboard TreeView to the WinUI port sync (2.7.0) - #26685
MartinZikmund wants to merge 8 commits into
Conversation
Ports the upstream changes since winui3/release/1.4.2 and closes the gaps a line-by-line review against 2.7.0 found. Upstream changes: - SelectedNodes and SelectedItems look an item up from the position of the last hit (IndexOf_OptimizedForRemove), and descendants are unselected back to front, so unselecting a large subtree is no longer quadratic. - The item to node map is gone, the node is found by walking the tree. Review gaps: - SelectedItems.Insert and its indexer keep SelectedNodes in sync. - The list control events are revoked when the template is reapplied. - Parameter checks that are asserts in WinUI no longer throw. - TreeViewNode reports its full type name when it has no content. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01TXMNu2q9JbwVarbJ7PDuaT
TreeViewDragItemsStartingEventArgs.Cancel was a plain property, so setting it never reached the ListView arguments and the drag started anyway. WinUI forwards it. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01TXMNu2q9JbwVarbJ7PDuaT
The finalizer only detached the chevron handler, from the finalizer thread. Both objects are unreachable by then, so it did nothing but keep every container alive for one more collection. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01TXMNu2q9JbwVarbJ7PDuaT
TreeViewItem re-applied its indentation and selection visual when it was loaded and when its template was applied, and looked its TreeView up through the owning ItemsControl while it was not in the visual tree yet. They covered containers the native list views prepared before they were attached. The WinUI path, TreeViewList.OnContainerContentChanging, is now enough: the TreeView runtime tests pass without them, and a recycling test only fails once that path is disabled as well. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01TXMNu2q9JbwVarbJ7PDuaT
Every deviation from WinUI that stays after the audit says why it is there. The reasons were checked against native WinUI and the runtime tests: #15214 and #25796 handling, the base prepare order and the selection pushed to the list each have tests failing without them. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01TXMNu2q9JbwVarbJ7PDuaT
Ports SelectedItemsTest, TreeViewBackgroundTest and VerifySettingSelectionDoesNotCrash, the TreeViewItemsSourceTestPage sample and the perf scenario of TreeViewPage. TreeViewSelectionChangedMultipleMode stays disabled, now with the actual cause: a ListView in SelectionMode.None keeps the other items selected when ListViewItem.IsSelected is set, WinUI unselects them. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01TXMNu2q9JbwVarbJ7PDuaT
- Canceling DragItemsStarting keeps the drag from starting. - Selected nodes removed by an ItemsSource reset leave the selection. - Recycled containers keep their selection state and indentation in multi-select mode. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01TXMNu2q9JbwVarbJ7PDuaT
Records TreeView at winui3/release/2.7.0. The three theme XAML files are verbatim copies of upstream and are tracked as mirrors. The headers of the area now name only their upstream files, the entry holds the version; the ported tests and samples gain the header they lacked. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_01TXMNu2q9JbwVarbJ7PDuaT
|
Claude finished @MartinZikmund's task in 21s —— View job PR review: TreeView onboarding to the WinUI port sync (2.7.0)Verdict: ship after minor fixes. I found 0 high, 1 medium and 1 low severity issues. They are posted inline. I reviewed the diff by inspection only. I did not build or run any tests. Cross-cutting notes
|
|
Tick the box to add this pull request to the merge queue (same as
|
|
|
||
| internal TreeViewNode GetAssociatedNode(object item) => GetAssociatedNodeImpl(item, m_rootNode); | ||
|
|
||
| private TreeViewNode GetAssociatedNodeImpl(object item, TreeViewNode node) |
There was a problem hiding this comment.
Performance (medium): GetAssociatedNode is now an O(N) recursive walk with Equals on every call (the dictionary was O(1)). It's reached from TreeViewList (ItemFromNode/container prep paths, line ~769) and the view model, so on large expanded trees each container preparation or selection change can become O(N), i.e. O(N²) overall. Upstream accepts this, but upstream compares by identity (cheap), while Equals can call user overrides. Consider a short-circuit via ReferenceEquals first, and note the perf trade-off in the PR; the "needs a decision" item in the description should also weigh this cost.
| #if HAS_UNO | ||
| // TODO Uno: Original C++ destructor cleanup. Uno does not support cleanup via finalizers. | ||
| // Original destructor logic (not executed): | ||
| // ~TreeViewItem() | ||
| // { | ||
| // // We only need to use safe_get in the deconstruction loop | ||
| // RecycleEvents(true /* useSafeGet */); | ||
| // } |
There was a problem hiding this comment.
Style (low): This is a commented-out copy of the removed finalizer. AGENTS.md says comments should never narrate code removal/history; the finalizer is gone, so drop this block (the PR description already records why).
|
|
||
| Assert.IsNotNull(checkBox, $"[{step}] {node.Content} has no selection check box"); | ||
| Assert.AreEqual(expected, checkBox.IsChecked, $"[{step}] {node.Content} check box"); | ||
| Assert.AreEqual(node.Depth * 16, container.TreeViewItemTemplateSettings.Indentation.Left, $"[{step}] {node.Content} indentation"); |
| { | ||
| var nodeContent = treeView.SelectedNodes[i].Content as TreeViewItemSource; | ||
| Verify.IsNotNull(nodeContent); | ||
| Verify.AreEqual(expected[i], nodeContent.Content, $"Verify TreeView SelectedNodes[{i}]"); |
| { | ||
| var selectedItem = treeView.SelectedItems[i] as TreeViewItemSource; | ||
| Verify.IsNotNull(selectedItem); | ||
| Verify.AreEqual(expected[i], selectedItem.Content, $"Verify TreeView SelectedItems[{i}]"); |
| var treeViewList = FindVisualChildByName(treeView, "ListControl") as TreeViewList; | ||
|
|
||
| // Make sure the TreeViewList does not already have the background color we are using for this test. | ||
| bool testCondition = treeViewList.Background is SolidColorBrush brush && brush.Color == Colors.Green; |
| if (node.Content is { } content) | ||
| { | ||
| // Uno specific: WinUI compares identities. A boxed value or a string is not the same instance in .NET, | ||
| // so items are compared with Equals. | ||
| if (Equals(content, item)) | ||
| { | ||
| return node; | ||
| } | ||
| } |
| if (_list.Count == 0) | ||
| { | ||
| startNextSearchAt = 0; | ||
| } | ||
| else | ||
| { | ||
| startNextSearchAt = _list.Count - 1; | ||
| } |
| if (index > 0) | ||
| { | ||
| // Next time we search start near this index. We assume that the remove is being processed from the end | ||
| // of the vector towards index 0, so the next item being removed is probably at "index - 1". Start the | ||
| // next search there. | ||
| startNextSearchAt = index - 1; | ||
| } | ||
| else | ||
| { | ||
| startNextSearchAt = index; | ||
| } |
| if (string.IsNullOrEmpty(prefix)) | ||
| { | ||
| name = $"{i}"; | ||
| } | ||
| else | ||
| { | ||
| name = $"{prefix}.{i}"; | ||
| } |
|
🤖 Your WebAssembly Sample App stage site is ready! Visit it here: |
|
The build 248714 found UI Test snapshots differences: Details
|
GitHub Issue: closes unoplatform/uno-private#2642
PR Type:
What changed? 🚀
Onboards
TreeViewto theWinUI Port Sync, recorded at WinUI 2.7.0 (1b5b1a0ccc). The port is caught up from 1.4.2, reviewed line by line against 2.7.0, and its Uno workarounds are audited.Mapping
controls/dev/TreeViewsrc/Uno.UI/UI/Xaml/Controls/TreeViewcontrols/dev/TreeView/APITests,TestUIRuntimeTests/.../Controls/TreeView(*.mux.cs),SamplesApp.Samples/.../Controls/TreeViewcontrols/dev/TreeView/Stringssrc/Uno.UI/UI/Xaml/Controls/TreeView/StringsTreeView.xaml,TreeViewItem.xaml,TreeView_themeresources.xamlFluentTheme/Resources/PriorityDefaultfiles, as mirrorsTwo corrections to the estimate in the issue: upstream does have a
Stringsfolder (86 locales, the same set as Uno's and already identical, so it is mapped and nothing changed), and theTestUIpages were already ported, exceptTreeViewItemsSourceTestPage. The theme XAML is byte-for-byte upstream's, hence mirrors.InteractionTestsare not mapped.Given_TreeView*.csare Uno-written and stay outside the mapping.Starting point
Headers: 22 files at
winui3/release/1.4.2, 3 automation peers at 1.8.4, 3 theme files at 2.5.1, andTreeViewSelectionMode.csat the WinUI 2 commit96244e6. Starting point is 1.4.2, the tag most files agree on.96244e6predates thecontrols/devlayout and cannot be used; the enum matches the IDL. The peers and theme files were compared with 2.7.0 directly. Catch-up size: 16 files, +615 / −227.Upstream changes 1.4.2 → 2.7.0
Ported
ViewModel.cpp/.h:IndexOf_OptimizedForRemove/GetCustomIndexOfFunctionon both selection vectors,InsertAtCore_NoContainsCheck,InsertAt_NoContainsCheck, descendants unselected back to front →SelectedTreeNodeVector.cs,SelectedItemsVector.cs,TreeViewViewModel.cs. The search itself comes fromCollections/Vector.h(outside the area, small) and is ported intoObservableVector<T>; the caller holds the search position so other vectors do not grow.ViewModel.cpp,TreeView.cpp/.h:m_itemToNodeMapremoved,GetAssociatedNodewalks the tree →TreeViewViewModel.cs,TreeView.cs.TreeViewItem.cpp/.h:m_expandContentTimeris a plain field →TreeViewItem.cs.TreeViewList.cpp: binding-awareIsExpandedsync when a container is prepared → already present, aligned to upstream's form.TreeViewItem.xaml:Foregroundon theContentPresenter→ already present.APITests/TreeViewTests.cs:SelectedItemsTestand helpers →TreeViewTests.mux.cs.TestUI:partialselectors, container-reuse buttons ofTreeViewItemsSourceTestPage(the page andTreeViewViewModel.csare ported as a whole), perf scenario ofTreeViewPage.Not applicable
TreeView.vcxitems(*Perf2026page types),static_cast<double>inUpdateIndentation, whitespace-only edits.InteractionTests/TreeViewTests.cs(+39): Uno cannot run interaction tests yet.[TestProperty("TestPass:MaxOSVer", …)]onVerifyVisualTree: the test is not ported, it needsVisualTreeTestHelperand its verification files.Skipped: none, so there is no skipped-items issue.
Line-by-line review against 2.7.0
TreeView.cpp/.h/.idlTreeView.cs,.Properties.cs,.Events.cs,TreeViewSelectionMode.cs+=);m_containerContentChangedSourceis typed forListViewBase(the handler cast its sender toTreeView). Marked: #15214 and #25796 handling,EqualsinGetAssociatedNodeImplTreeViewList.cpp/.hTreeViewList.csIsCustomReorder,IsGeneratedContainer,OnDropInternal,GetNamefor the protectedGetNameCoreTreeViewItem.cpp/.hTreeViewItem.cs,.Properties.csUpdateSelectionVisualchecks the list control for null; the check box handlers are detached on re-template (stands in for the two revokers, no per-item allocation); the finalizer is gone. Marked:RecycleEventsdetachesPointerPressed(upstream revokesPointerCanceledwith that token), null check box guardTreeViewNode.cpp/.hTreeViewNode.cs,.Properties.cs,.Events.cs,TreeViewNodeVector.cs,TreeNodeSelectionState.csToString()without content andICustomPropertyProvider.Typeuse the runtime type's full name (checked on WinUI);InsertAt/ReplaceAllassert like upstream instead of throwing. Marked:RemoveAtEnd(upstream passes the flag as the index)ViewModel.cpp/.hTreeViewViewModel.cs,.Events.cs,SelectedTreeNodeVector.cs,SelectedItemsVector.csSelectedItems.Insertand its indexer go throughInsertAt/SetAtand keepSelectedNodesin sync (checked on WinUI); the range andItemChangedchecks are asserts. Marked: selection pushed to the list without a container, pruning on reset, the enumerator (stands in forImplement_IIterator),GetMany/ReplaceAll(#if false, noIList<T>counterpart), the view model finalizerTreeView*EventArgs.cpp/.h(6)TreeViewDragItemsStartingEventArgs.Cancelforwards to the ListView arguments; it was an auto-property, so a drag could not be canceledTreeViewItemTemplateSettings.*TreeViewItemTemplateSettings.csTreeView*AutomationPeer.cpp/.h,TreeViewAutomationPeers.idlTreeView.xaml,TreeViewItem.xaml,TreeView_themeresources.xamlAPITests/TreeViewTests.cs(23 tests)TreeViewTests.mux.csVerifyVisualTree(see above). Disabled:TreeViewInheritanceTest(disabled upstream too),TreeViewSelectionChangedMultipleMode(see below)TestUI/*SamplesApp.Samples/.../TreeViewGetFrame,GetItemsCount, the navigation grid) are markedNot done here: the port keeps its monolithic files (no
.mux.cs/.h.mux.cssplit), and most public members ofTreeViewNode,TreeViewItemand the event args have no XML docs.Uno workarounds
Each one was switched off in turn and the TreeView runtime tests were rerun. Open parity questions were checked on native WinUI (WinAppSDK 2.5.1) with temporary probe tests, since
*.mux.cstests are not compiled on that head.TreeViewItem.OnLoadedWhen_MultiSelect_Recycled_Containers_Keep_Selection_And_Indentationpasses without it and fails once the upstream path (OnContainerContentChanging) is disabled as well. The UI test its comment cited is itself ignoredTreeViewItem.OnApplyTemplateTreeViewItem.AncestorTreeView,TreeViewList.AncestorTreeView~TreeViewItem()TreeViewList.PrepareContainerForItemOverride: base call firstWhen_SelectNode_DoesNotToggle_OtherNodesExpansiontimes out with upstream's orderTryTakeStaleUnselection,IsPreparingStaleSelection,ChangeSelectedItem)IsSelectedfrom a two-way bindingSelectedItemnodes=0, container not selected), so this is a deliberate improvementNotifyContainerOfSelectionChange: push to the list without a containerTreeView.SelectedItemin step with a node that has no containerSelectedItemnull and the list index at −1PruneDetachedSelectedNodesnodes=1 items=1, no event). Now covered byWhen_ItemsSource_Cleared_MultiSelect_Selection_Is_Dropped. The item-map half of this workaround is gone with the mapGetAssociatedNodeImpl:EqualsSelectedItem = 2or an equal fresh string selects nothing there. Uno already matched by value through the old dictionary, so this keeps today's behaviorUpdateMultipleSelection: null check boxMultiSelectCheckBoxIsGeneratedContainer,IsCustomReorderItemsControl.ClearContainerForItemOverrideandListViewBase.DragDrop; drag reorder is only covered by UI tests~TreeViewViewModel()ClearEventTokenVectorsTreeViewPage.xaml.cs:Bindings.UpdateDisabled tests:
TreeViewSelectionChangedMultipleModewas re-enabled and still fails, for a reason outside this area: inSelectionMode.None, WinUI'sListViewunselects the other containers whenListViewItem.IsSelectedis set, Uno's keeps them. Its[Ignore]now says so.Tests
MUXControlsTestApp.TreeViewTests, bothGiven_TreeViewclasses): 54 passed, 2 skipped, 0 failed.SelectedItemsTest,TreeViewBackgroundTest,VerifySettingSelectionDoesNotCrash(upstream),When_DragItemsStarting_Canceled_Drag_Does_Not_Start(fails without the fix),When_ItemsSource_Cleared_MultiSelect_Selection_Is_Dropped,When_MultiSelect_Recycled_Containers_Keep_Selection_And_Indentation.Uno.UIandUno.UI.RuntimeTests.Genericbuild in Release withoutUnoFastDevBuild.detect.ps1reports the area up to date;build/winui-sync/tests/run.ps1passes.PR Checklist ✅
Screenshots Compare Test Runresults.🤖 Generated with Claude Code
https://claude.ai/code/session_01TXMNu2q9JbwVarbJ7PDuaT