-
Notifications
You must be signed in to change notification settings - Fork 853
feat: ItemCollectionTransition
#22960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MartinZikmund
wants to merge
5
commits into
master
Choose a base branch
from
dev/mazi/repeater-collection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
88abebf
feat: `ItemCollectionTransition`
MartinZikmund 6e3e06e
test: `ItemCollectionTransition`
MartinZikmund 3567bbe
chore: Improve port accuracy
MartinZikmund a171406
feat: Improve WinUI port skill
MartinZikmund bd888db
chore: Disposable
MartinZikmund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
235 changes: 235 additions & 0 deletions
235
...I.RuntimeTests/Tests/Microsoft_UI_Xaml_Controls/Given_ItemCollectionTransitionProvider.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,235 @@ | ||
| // MUX Reference Repeater/APITests/ItemCollectionTransitionProviderTests.cs, tag winui3/release/1.6-stable | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.ObjectModel; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.UI.Xaml; | ||
| using Microsoft.UI.Xaml.Controls; | ||
| using Microsoft.UI.Xaml.Markup; | ||
| using Microsoft.UI.Xaml.Media; | ||
| using Private.Infrastructure; | ||
| using Windows.Foundation; | ||
|
|
||
| namespace Uno.UI.RuntimeTests.Tests.Microsoft_UI_Xaml_Controls; | ||
|
|
||
| [TestClass] | ||
| public partial class Given_ItemCollectionTransitionProvider | ||
| { | ||
| /// <summary> | ||
| /// Validates that the transition provider receives the expected add/remove/move calls | ||
| /// when items are inserted and removed from the repeater's data source. | ||
| /// Port of ValidateItemCollectionTransitionProvider from WinUI. | ||
| /// </summary> | ||
| [TestMethod] | ||
| [RunsOnUIThread] | ||
| public async Task When_Items_Changed_TransitionProvider_Receives_Correct_Calls() | ||
| { | ||
| var data = new ObservableCollection<string>(Enumerable.Range(0, 10).Select(i => $"Item #{i}")); | ||
|
|
||
| var elementFactory = new RecyclingElementFactory(); | ||
| elementFactory.RecyclePool = new RecyclePool(); | ||
| elementFactory.Templates["Item"] = (DataTemplate)XamlReader.Load( | ||
| @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> | ||
| <TextBlock Text='{Binding}' Height='50' /> | ||
| </DataTemplate>"); | ||
|
|
||
| var repeater = new ItemsRepeater() | ||
| { | ||
| ItemsSource = data, | ||
| ItemTemplate = elementFactory, | ||
| }; | ||
|
|
||
| TestServices.WindowHelper.WindowContent = new ItemsRepeaterScrollHost() | ||
| { | ||
| Width = 400, | ||
| Height = 800, | ||
| ScrollViewer = new ScrollViewer | ||
| { | ||
| Content = repeater | ||
| } | ||
| }; | ||
|
|
||
| await TestServices.WindowHelper.WaitForLoaded(repeater); | ||
| await TestServices.WindowHelper.WaitForIdle(); | ||
|
|
||
| var addCalls = new List<CallInfo>(); | ||
| var removeCalls = new List<CallInfo>(); | ||
| var moveCalls = new List<CallInfo>(); | ||
|
|
||
| var transitionProvider = new ItemCollectionTransitionProviderDerived | ||
| { | ||
| ShouldAnimateFunc = _ => true, | ||
| StartTransitionsFunc = transitions => | ||
| { | ||
| foreach (var transition in transitions) | ||
| { | ||
| var progress = transition.Start(); | ||
|
|
||
| switch (transition.Operation) | ||
| { | ||
| case ItemCollectionTransitionOperation.Add: | ||
| addCalls.Add(new CallInfo(repeater.GetElementIndex(progress.Element), transition)); | ||
| break; | ||
| case ItemCollectionTransitionOperation.Remove: | ||
| removeCalls.Add(new CallInfo(repeater.GetElementIndex(progress.Element), transition)); | ||
| break; | ||
| case ItemCollectionTransitionOperation.Move: | ||
| moveCalls.Add(new CallInfo(repeater.GetElementIndex(progress.Element), transition)); | ||
| break; | ||
| } | ||
|
|
||
| progress.Complete(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| repeater.ItemTransitionProvider = transitionProvider; | ||
|
|
||
| data.Insert(0, "new item"); | ||
| data.RemoveAt(2); | ||
|
|
||
| await TestServices.WindowHelper.WaitForIdle(); | ||
|
|
||
| Assert.AreEqual(1, addCalls.Count, "Expected 1 add call"); | ||
| var call = addCalls[0]; | ||
| Assert.AreEqual(0, call.Index, "Add call should be at index 0"); | ||
| Assert.AreEqual(ItemCollectionTransitionTriggers.CollectionChangeAdd, call.Transition.Triggers); | ||
|
|
||
| Assert.AreEqual(1, removeCalls.Count, "Expected 1 remove call"); | ||
| call = removeCalls[0]; | ||
| Assert.AreEqual(-1, call.Index, "Removed item should no longer be in the repeater"); | ||
| Assert.AreEqual(ItemCollectionTransitionTriggers.CollectionChangeRemove, call.Transition.Triggers); | ||
|
|
||
| Assert.AreEqual(1, moveCalls.Count, "Expected 1 move call"); | ||
| call = moveCalls[0]; | ||
| Assert.AreEqual(1, call.Index, "Moved item should be at index 1"); | ||
| Assert.AreEqual(ItemCollectionTransitionTriggers.CollectionChangeAdd | ItemCollectionTransitionTriggers.CollectionChangeRemove, call.Transition.Triggers); | ||
| Assert.AreEqual(0, call.Transition.OldBounds.Y, "Old Y bound should be 0"); | ||
| Assert.AreEqual(50, call.Transition.NewBounds.Y, "New Y bound should be 50 (one item height)"); | ||
|
|
||
| addCalls.Clear(); | ||
| removeCalls.Clear(); | ||
| moveCalls.Clear(); | ||
|
|
||
| // Validate filtering: only animate Add operations. | ||
| transitionProvider.ShouldAnimateFunc = t => t.Operation == ItemCollectionTransitionOperation.Add; | ||
|
|
||
| data.Insert(0, "new item"); | ||
| data.RemoveAt(2); | ||
|
|
||
| await TestServices.WindowHelper.WaitForIdle(); | ||
|
|
||
| Assert.AreEqual(1, addCalls.Count, "Expected 1 add call in filtered scenario"); | ||
| call = addCalls[0]; | ||
| Assert.AreEqual(0, call.Index); | ||
| Assert.AreEqual(ItemCollectionTransitionTriggers.CollectionChangeAdd, call.Transition.Triggers); | ||
|
|
||
| Assert.AreEqual(0, removeCalls.Count, "Remove should not be animated when filtered"); | ||
| Assert.AreEqual(0, moveCalls.Count, "Move should not be animated when filtered"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Validates HasStarted transitions through Start() and that TransitionCompleted fires on Complete(). | ||
| /// </summary> | ||
| [TestMethod] | ||
| [RunsOnUIThread] | ||
| public async Task When_Progress_Complete_TransitionCompleted_Is_Raised() | ||
| { | ||
| var data = new ObservableCollection<string>(Enumerable.Range(0, 3).Select(i => $"Item #{i}")); | ||
|
|
||
| var elementFactory = new RecyclingElementFactory(); | ||
| elementFactory.RecyclePool = new RecyclePool(); | ||
| elementFactory.Templates["Item"] = (DataTemplate)XamlReader.Load( | ||
| @"<DataTemplate xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'> | ||
| <TextBlock Text='{Binding}' Height='50' /> | ||
| </DataTemplate>"); | ||
|
|
||
| var repeater = new ItemsRepeater() | ||
| { | ||
| ItemsSource = data, | ||
| ItemTemplate = elementFactory, | ||
| }; | ||
|
|
||
| TestServices.WindowHelper.WindowContent = new ItemsRepeaterScrollHost() | ||
| { | ||
| Width = 400, | ||
| Height = 800, | ||
| ScrollViewer = new ScrollViewer { Content = repeater } | ||
| }; | ||
|
|
||
| await TestServices.WindowHelper.WaitForLoaded(repeater); | ||
| await TestServices.WindowHelper.WaitForIdle(); | ||
|
|
||
| ItemCollectionTransition? capturedTransition = null; | ||
| var completedArgs = new List<ItemCollectionTransitionCompletedEventArgs>(); | ||
|
|
||
| var transitionProvider = new ItemCollectionTransitionProviderDerived | ||
| { | ||
| ShouldAnimateFunc = _ => true, | ||
| StartTransitionsFunc = transitions => | ||
| { | ||
| foreach (var transition in transitions) | ||
| { | ||
| capturedTransition = transition; | ||
|
|
||
| Assert.IsFalse(transition.HasStarted, "HasStarted should be false before Start()"); | ||
| var progress = transition.Start(); | ||
| Assert.IsTrue(transition.HasStarted, "HasStarted should be true after Start()"); | ||
|
|
||
| // Repeated Start() should return the same object. | ||
| var progress2 = transition.Start(); | ||
| Assert.AreSame(progress, progress2, "Repeated Start() calls should return the same progress object"); | ||
|
|
||
| Assert.IsNotNull(progress.Element, "Progress.Element should not be null"); | ||
| Assert.AreSame(transition, progress.Transition, "Progress.Transition should match"); | ||
|
|
||
| progress.Complete(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| transitionProvider.TransitionCompleted += (s, e) => completedArgs.Add(e); | ||
| repeater.ItemTransitionProvider = transitionProvider; | ||
|
|
||
| data.Insert(0, "new item"); | ||
|
|
||
| await TestServices.WindowHelper.WaitForIdle(); | ||
|
|
||
| Assert.IsNotNull(capturedTransition, "At least one transition should have been started"); | ||
| Assert.IsTrue(completedArgs.Count > 0, "TransitionCompleted should have been raised"); | ||
| Assert.AreSame(capturedTransition, completedArgs[0].Transition, "CompletedEventArgs.Transition should match"); | ||
| Assert.IsNotNull(completedArgs[0].Element, "CompletedEventArgs.Element should not be null"); | ||
| } | ||
|
|
||
| private struct CallInfo | ||
| { | ||
| public CallInfo(int index, ItemCollectionTransition transition) | ||
| { | ||
| Index = index; | ||
| Transition = transition; | ||
| } | ||
|
|
||
| public int Index { get; set; } | ||
| public ItemCollectionTransition Transition { get; set; } | ||
|
|
||
| public override string ToString() => | ||
| $"Index: {Index} Operation: {Transition.Operation} Triggers: {Transition.Triggers} " + | ||
| $"OldBounds: {Transition.OldBounds} NewBounds: {Transition.NewBounds}"; | ||
| } | ||
|
|
||
| private class ItemCollectionTransitionProviderDerived : ItemCollectionTransitionProvider | ||
| { | ||
| public Func<ItemCollectionTransition, bool>? ShouldAnimateFunc { get; set; } | ||
| public Action<IList<ItemCollectionTransition>>? StartTransitionsFunc { get; set; } | ||
|
|
||
| protected override bool ShouldAnimateCore(ItemCollectionTransition transition) => | ||
| ShouldAnimateFunc?.Invoke(transition) ?? false; | ||
|
|
||
| protected override void StartTransitions(IList<ItemCollectionTransition> transitions) => | ||
| StartTransitionsFunc?.Invoke(transitions); | ||
| } | ||
| } | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.