Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public override void ScrollTo(ScrollToRequestEventArgs args)
// And at the same time the user is requesting we go to a particular item
if (position == -1)
{
_gotoPosition = -1;
if (Carousel.Loop)
_carouselViewLoopManager.AddPendingScrollTo(args);

Expand All @@ -193,6 +194,8 @@ public override void ScrollTo(ScrollToRequestEventArgs args)

if (args.IsAnimated)
{
if (_gotoPosition == -1)
_gotoPosition = args.Index;
ScrollHelper.AnimateScrollToPosition(position, args.ScrollToPosition);
}
else
Expand Down Expand Up @@ -506,8 +509,6 @@ void IMauiCarouselRecyclerView.UpdateFromCurrentItem()
_gotoPosition = currentItemPosition;
ItemsView.ScrollTo(currentItemPosition, position: Microsoft.Maui.Controls.ScrollToPosition.Center, animate: Carousel.AnimateCurrentItemChanges);
}

_gotoPosition = -1;
}

void IMauiCarouselRecyclerView.UpdateFromPosition()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Maui.Controls.Platform;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
Expand All @@ -25,6 +26,7 @@ public partial class CarouselViewHandler : ItemsViewHandler<CarouselView>
WScrollBarVisibility? _verticalScrollBarVisibilityWithoutLoop;
Size _currentSize;
bool _isCarouselViewReady;
int _gotoPosition = -1;
NotifyCollectionChangedEventHandler _collectionChanged;
readonly WeakNotifyCollectionChangedProxy _proxy = new();

Expand Down Expand Up @@ -190,6 +192,28 @@ public static void MapCurrentItem(CarouselViewHandler handler, CarouselView caro
handler.UpdateCurrentItem();
}

protected override async Task ScrollTo(ScrollToRequestEventArgs args)
{
if (args.IsAnimated && args.Mode == ScrollToMode.Position)
{
_gotoPosition = args.Index;

// Commit position before animation so PreviousPosition/PreviousItem are correct immediately.
SetCarouselViewPosition(_gotoPosition);
}

try
{
await base.ScrollTo(args);
}
finally
{
// Conditional reset guards against a concurrent ScrollTo replacing the target.
if (_gotoPosition == args.Index)
_gotoPosition = -1;
}
}

public static void MapPosition(CarouselViewHandler handler, CarouselView carouselView)
{
// If the initial position hasn't been set, we have a UpdateInitialPosition call on CarouselViewHandler
Expand Down Expand Up @@ -385,7 +409,14 @@ void UpdateCurrentItem()
var currentItemPosition = GetItemPositionInCarousel(ItemsView.CurrentItem);

if (currentItemPosition < 0 || currentItemPosition >= ItemCount)
{
return;
}

if (_gotoPosition != -1)
{
return;
}

ItemsView.ScrollTo(currentItemPosition, position: ScrollToPosition.Center, animate: ItemsView.AnimateCurrentItemChanges);
}
Expand Down Expand Up @@ -491,6 +522,12 @@ void UpdateScrollBarVisibilityForLoop()

void CarouselScrolled(object sender, ItemsViewScrolledEventArgs e)
{
// Ignore ViewChanged events fired before the initial position is established.
if (!InitialPositionSet)
{
return;
}

var position = e.CenterItemIndex;

if (position == -1)
Expand All @@ -503,6 +540,12 @@ void CarouselScrolled(object sender, ItemsViewScrolledEventArgs e)
return;
}

// Suppress intermediate scroll events during a programmatic animated scroll.
if (_gotoPosition != -1)
{
return;
}

SetCarouselViewPosition(position);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#nullable enable
~override Microsoft.Maui.Controls.Handlers.Items.CarouselViewHandler.ScrollTo(Microsoft.Maui.Controls.ScrollToRequestEventArgs args) -> System.Threading.Tasks.Task
override Microsoft.Maui.Controls.Shapes.Shape.OnPropertyChanged(string? propertyName = null) -> void
152 changes: 152 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29544.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 29544,
"PreviousItem and PreviousPosition not updating correctly on ScrollTo or Position set",
PlatformAffected.Android | PlatformAffected.UWP)]
public class Issue29544 : ContentPage
{
readonly CarouselView _carouselView;
readonly ObservableCollection<string> _items;
readonly Label _previousItemLabel;
readonly Label _previousPositionLabel;
readonly Label _currentItemLabel;
readonly Label _currentPositionLabel;

public Issue29544()
{
_items = new ObservableCollection<string>
{
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5"
};

_currentItemLabel = new Label
{
AutomationId = "CurrentItemLabel",
Text = "Current Item: Item 1"
};

_previousItemLabel = new Label
{
AutomationId = "PreviousItemLabel",
Text = "Previous Item: none"
};

_currentPositionLabel = new Label
{
AutomationId = "CurrentPositionLabel",
Text = "Current Position: 0"
};

_previousPositionLabel = new Label
{
AutomationId = "PreviousPositionLabel",
Text = "Previous Position: none"
};

_carouselView = new CarouselView
{
AutomationId = "CarouselView",
Loop = false,
ItemsSource = _items,
ItemTemplate = new DataTemplate(() =>
{
var label = new Label
{
FontSize = 24,
BackgroundColor = Colors.LightGray,
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.Center,
HorizontalTextAlignment = TextAlignment.Center
};
label.SetBinding(Label.TextProperty, ".");
return label;
})
};

_carouselView.CurrentItemChanged += OnCurrentItemChanged;
_carouselView.PositionChanged += OnPositionChanged;

var scrollTo3Button = new Button
{
AutomationId = "ScrollTo3Button",
Text = "Scroll To 3"
};
scrollTo3Button.Clicked += (s, e) => _carouselView.ScrollTo(3);

var scrollTo1Button = new Button
{
AutomationId = "ScrollTo1Button",
Text = "Scroll To 1"
};
scrollTo1Button.Clicked += (s, e) => _carouselView.ScrollTo(1);

var setPosition3Button = new Button
{
AutomationId = "SetPosition3Button",
Text = "Set Position 3"
};
setPosition3Button.Clicked += (s, e) => _carouselView.Position = 3;

var setPosition0Button = new Button
{
AutomationId = "SetPosition0Button",
Text = "Set Position 0"
};
setPosition0Button.Clicked += (s, e) => _carouselView.Position = 0;

Content = new Grid
{
Padding = new Thickness(10),
RowDefinitions =
{
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Star }
},
Children =
{
new VerticalStackLayout
{
Spacing = 6,
Children =
{
_currentItemLabel,
_previousItemLabel,
_currentPositionLabel,
_previousPositionLabel,
new HorizontalStackLayout
{
Spacing = 8,
Children = { scrollTo3Button, scrollTo1Button }
},
new HorizontalStackLayout
{
Spacing = 8,
Children = { setPosition3Button, setPosition0Button }
}
}
},
_carouselView
}
};

Grid.SetRow(_carouselView, 1);
}

void OnCurrentItemChanged(object sender, CurrentItemChangedEventArgs e)
{
_currentItemLabel.Text = $"Current Item: {e.CurrentItem}";
_previousItemLabel.Text = $"Previous Item: {e.PreviousItem ?? "none"}";
}

void OnPositionChanged(object sender, PositionChangedEventArgs e)
{
_currentPositionLabel.Text = $"Current Position: {e.CurrentPosition}";
_previousPositionLabel.Text = $"Previous Position: {e.PreviousPosition}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue29544 : _IssuesUITest
{
public override string Issue =>
"PreviousItem and PreviousPosition not updating correctly on ScrollTo or Position set";

public Issue29544(TestDevice device) : base(device) { }

[Test]
[Category(UITestCategories.CarouselView)]
public void PreviousPositionUpdatesCorrectlyOnScrollTo()
{
App.WaitForElement("ScrollTo3Button");
App.Tap("ScrollTo3Button");

App.RetryAssert(() =>
{
var currentPos = App.FindElement("CurrentPositionLabel").GetText();
Assert.That(currentPos, Does.Contain("3"),
"CarouselView should reach position 3 after ScrollTo");
});

var previousPositionText = App.FindElement("PreviousPositionLabel").GetText();
Assert.That(previousPositionText, Does.Contain("0"),
"After scrolling from position 0 to 3, PreviousPosition should be 0");

var previousItemText = App.FindElement("PreviousItemLabel").GetText();
Assert.That(previousItemText, Does.Contain("Item 1"),
"After scrolling from position 0 to 3, PreviousItem should be 'Item 1'");
Comment thread
praveenkumarkarunanithi marked this conversation as resolved.

App.Tap("ScrollTo1Button");

App.RetryAssert(() =>
{
var currentPos = App.FindElement("CurrentPositionLabel").GetText();
Assert.That(currentPos, Does.Contain("1"),
"CarouselView should reach position 1 after second ScrollTo");
});

var secondPreviousPosition = App.FindElement("PreviousPositionLabel").GetText();
Assert.That(secondPreviousPosition, Does.Contain("3"),
"After scrolling from position 3 to 1, PreviousPosition should be 3");

var secondPreviousItem = App.FindElement("PreviousItemLabel").GetText();
Assert.That(secondPreviousItem, Does.Contain("Item 4"),
"After scrolling from position 3 to 1, PreviousItem should be 'Item 4'");
Comment thread
praveenkumarkarunanithi marked this conversation as resolved.
}

[Test]
[Category(UITestCategories.CarouselView)]
public void PreviousPositionUpdatesCorrectlyOnSetPosition()
{
App.WaitForElement("SetPosition0Button");
App.Tap("SetPosition0Button");

App.RetryAssert(() =>
{
var pos = App.FindElement("CurrentPositionLabel").GetText();
Assert.That(pos, Does.Contain("0"), "CarouselView should be at position 0");
});

App.Tap("SetPosition3Button");

App.RetryAssert(() =>
{
var currentPos = App.FindElement("CurrentPositionLabel").GetText();
Assert.That(currentPos, Does.Contain("3"),
"CarouselView should reach position 3 after setting Position = 3");
});

var previousPositionText = App.FindElement("PreviousPositionLabel").GetText();
Assert.That(previousPositionText, Does.Contain("0"),
"After setting Position from 0 to 3, PreviousPosition should be 0");

var previousItemText = App.FindElement("PreviousItemLabel").GetText();
Assert.That(previousItemText, Does.Contain("Item 1"),
"After setting Position from 0 to 3, PreviousItem should be 'Item 1'");
Comment thread
praveenkumarkarunanithi marked this conversation as resolved.
}
}
Loading