Skip to content

Commit

Permalink
[Windows] Avoid Carousel changes the current position when window is …
Browse files Browse the repository at this point in the history
…resized (#22222)

* Added repro sample

* Added UI Test

* Fix the issue

* Added pending snapshot

* Run added test on all the platforms

* More changes

* Updated sample

* Added snapshots

* Updated test
  • Loading branch information
jsuarezruiz committed May 8, 2024
1 parent 1dfae6f commit 4a1cc39
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 5 deletions.
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue22000">
<Grid
RowDefinitions="Auto, *, Auto">
<Grid.Resources>

<DataTemplate x:Key="SampleItemTemplate">
<Grid
RowDefinitions="Auto, Auto"
BackgroundColor="{Binding Color, Mode=OneWay}">
<Label
TextColor="White"
Text="{Binding Title, Mode=OneWay}"
FontAttributes="Bold" />
<Label
Grid.Row="1"
TextColor="White"
Text="{Binding Description, Mode=OneWay}" />
</Grid>
</DataTemplate>

</Grid.Resources>
<Label
AutomationId="WaitForStubControl"
Text="Tap the Button several times to resize the CarouselView. If the CurrentItem not change, the test has passed."/>
<CarouselView
x:Name="TestCarouselView"
Grid.Row="1"
ItemTemplate="{StaticResource SampleItemTemplate}"
Loop="False"
HeightRequest="250"
HorizontalOptions="Center"/>
<Button
AutomationId="UpdateSizeButton"
Grid.Row="2"
Text="Update Size"
Clicked="OnUpdateSizeClicked"/>
</Grid>
</ContentPage>
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 22000, "[Windows] Carousel changes the current position when window is resized", PlatformAffected.UWP)]
public partial class Issue22000 : ContentPage
{
readonly Random _random;

public Issue22000()
{
InitializeComponent();

_random = new Random();

var exampleItems = new List<Issue22000Model>
{
new Issue22000Model( "First", "First CarouselView item", Colors.Red ),
new Issue22000Model( "Second", "Second CarouselView item", Colors.LightBlue ),
new Issue22000Model( "Third", "Third CarouselView item", Colors.Pink ),
new Issue22000Model( "Fourth", "Fourth CarouselView item", Colors.GreenYellow ),
new Issue22000Model( "Fifth", "Fifth CarouselView item", Colors.Purple ),
};

TestCarouselView.ItemsSource = exampleItems;

UpdateCarouselViewSize();
}

void OnUpdateSizeClicked(object sender, EventArgs e)
{
UpdateCarouselViewSize();
}

void UpdateCarouselViewSize()
{
var currentWidth = TestCarouselView.WidthRequest;

if (currentWidth == 400)
TestCarouselView.WidthRequest = 300;
else
TestCarouselView.WidthRequest = 200;
}
}

class Issue22000Model
{
public Issue22000Model(string title, string description, Color color)
{
Title = title;
Description = description;
Color = color;
}

public string Title { get; set; }
public string Description { get; set; }
public Color Color { get; set; }
}
}
Expand Up @@ -22,7 +22,8 @@ public partial class CarouselViewHandler : ItemsViewHandler<CarouselView>
ScrollViewer _scrollViewer;
WScrollBarVisibility? _horizontalScrollBarVisibilityWithoutLoop;
WScrollBarVisibility? _verticalScrollBarVisibilityWithoutLoop;
Size _currentSize;
Size _currentSize;
bool _isCarouselViewReady;
NotifyCollectionChangedEventHandler _collectionChanged;
readonly WeakNotifyCollectionChangedProxy _proxy = new();

Expand Down Expand Up @@ -548,10 +549,34 @@ void Resize(Size newSize)
{
_currentSize = newSize;

UpdateItemsSource();
UpdateSnapPointsType();
UpdateSnapPointsAlignment();
UpdateCarouselViewInitialPosition();
if (_isCarouselViewReady)
InvalidateItemSize();
else
InitialSetup();

_isCarouselViewReady = true;
}
}

void InitialSetup()
{
UpdateItemsSource();
UpdateSnapPointsType();
UpdateSnapPointsAlignment();
UpdateCarouselViewInitialPosition();
}

void InvalidateItemSize()
{
var itemHeight = GetItemHeight();
var itemWidth = GetItemWidth();

foreach (var item in ListViewBase.GetChildren<ItemContentControl>())
{
item.ItemHeight = itemHeight;
item.ItemWidth = itemWidth;

item.InvalidateMeasure();
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue22000.cs
@@ -0,0 +1,29 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue22000 : _IssuesUITest
{
public Issue22000(TestDevice device) : base(device) { }

public override string Issue => "[Windows] Carousel changes the current position when window is resized";

[Test]
[Category(UITestCategories.CarouselView)]
public async Task ResizeCarouselViewKeepsIndex()
{
App.WaitForElement("WaitForStubControl");

for (int i = 0; i < 10; i++)
{
App.Click("UpdateSizeButton");
}

await Task.Delay(500);

VerifyScreenshot();
}
}
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4a1cc39

Please sign in to comment.