Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
Copy Markdown
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 = 800;
else
TestCarouselView.WidthRequest = 400;
}
}

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,31 @@ 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()
{
foreach (var item in ListViewBase.GetChildren<ItemContentControl>())
{
item.ItemHeight = GetItemHeight();
Comment thread
jsuarezruiz marked this conversation as resolved.
Outdated
item.ItemWidth = GetItemWidth();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's beyond the scope of this PR, but I wonder why this logic isn't in the layout/measure pass. You'd think that item width + height is just calculated based on horizontal/vertical modes


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 void ResizeCarouselViewKeepsIndex()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Android, TestDevice.iOS, TestDevice.Mac });
Comment thread
jsuarezruiz marked this conversation as resolved.
Outdated

App.WaitForElement("WaitForStubControl");

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

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