Skip to content
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

[Windows] Avoid Carousel changes the current position when window is resized #22222

Merged
merged 10 commits into from
May 8, 2024
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
@@ -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">
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue-22000-test

<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>
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading