Skip to content

Commit

Permalink
Set Views used for CarouselView to Match Parent (#21662)
Browse files Browse the repository at this point in the history
* Call NotifyDataSetChanged when CarV dims change

* - set layout  parameters on container view to match parent so it always tries to fill the container

* - remove test from winui

* - don't run test on windows
  • Loading branch information
PureWeen committed Apr 6, 2024
1 parent 6f5ca12 commit ec0deda
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 50 deletions.
62 changes: 62 additions & 0 deletions src/Controls/samples/Controls.Sample.UITests/Issues/Issue21609.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Maui.Controls;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 21609, "Changing the dimensions of the CarouselView doesn't update Item Dimensions")]
public class Issue21609 : TestContentPage
{
protected override void Init()
{
if (OperatingSystem.IsWindows())
{
Content = new Label() { Text = "This Test currently doesn't work on windows" };
return;
}

var carV = new CarouselView()
{
HeightRequest = 200,
WidthRequest = 200,
ItemTemplate = new DataTemplate(() =>
{
return new Image() { Source = "dotnet_bot.png", AutomationId = "DotnetBot" };
})
};

carV.ItemsSource = new[] { 1 };

var changeSize = new Button()
{
Text = "Click me and the dimensions of the CarouselView Should Change",
AutomationId = "ChangeCarouselViewDimensions",
Command = new Command(() =>
{
if (carV.HeightRequest == 200)
{
carV.WidthRequest = carV.HeightRequest = 100;
}
else
{
carV.WidthRequest = carV.HeightRequest = 200;
}
})
};

var innerVSL = new VerticalStackLayout()
{
carV
};

innerVSL.HeightRequest = 200;
Content = new VerticalStackLayout()
{
innerVSL,
changeSize
};
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#nullable disable
using System;
using System.Collections.Generic;
using System.Text;
using Android.Views;
using AndroidX.RecyclerView.Widget;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;

namespace Microsoft.Maui.Controls.Handlers.Items
{
Expand All @@ -18,7 +14,13 @@ public partial class CarouselViewHandler : ItemsViewHandler<CarouselView>

protected override ItemsViewAdapter<CarouselView, IItemsViewSource> CreateAdapter()
{
return new CarouselViewAdapter<CarouselView, IItemsViewSource>(VirtualView, (view, context) => new SizedItemContentView(Context, GetItemWidth, GetItemHeight));
return new CarouselViewAdapter<CarouselView, IItemsViewSource>(VirtualView, (view, context) =>
{
return new SizedItemContentView(Context, GetItemWidth, GetItemHeight)
{
LayoutParameters = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MatchParent, RecyclerView.LayoutParams.MatchParent)
};
});
}

protected override RecyclerView CreatePlatformView()
Expand Down
63 changes: 18 additions & 45 deletions src/Controls/src/Core/NumericExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,21 @@
//using System;
//using System.ComponentModel;
//using System.Runtime.CompilerServices;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

//namespace Microsoft.Maui.Controls.Internals
//{
// [EditorBrowsable(EditorBrowsableState.Never)]
// public static class NumericExtensions
// {
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static double Clamp(this double self, double min, double max)
// {
// if (max < min)
// {
// return max;
// }
// else if (self < min)
// {
// return min;
// }
// else if (self > max)
// {
// return max;
// }
namespace Microsoft.Maui.Controls
{
static class NumericExtensions
{
const double Tolerance = 0.001;

// return self;
// }
public static bool IsCloseTo(this double sizeA, double sizeB)
{
if (Math.Abs(sizeA - sizeB) > Tolerance)
{
return false;
}

// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public static int Clamp(this int self, int min, int max)
// {
// if (max < min)
// {
// return max;
// }
// else if (self < min)
// {
// return min;
// }
// else if (self > max)
// {
// return max;
// }

// return self;
// }
// }
//}
return true;
}
}
}
31 changes: 31 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue21609.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Drawing;
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues;

public class Issue21609 : _IssuesUITest
{
public Issue21609(TestDevice device) : base(device) { }

public override string Issue => "Changing the dimensions of the CarouselView doesn't update Item Dimensions";

[Test]
public void ChangingDimensionsOfCarouselViewDoesntUpdateItemDimensions()
{
// This test is currently not passing on windows
// the 3rd size doesn't match the first one
this.IgnoreIfPlatform(TestDevice.Windows);

App.WaitForElement("ChangeCarouselViewDimensions");
var imageInitial = App.WaitForElement("DotnetBot").GetRect();
App.Click("ChangeCarouselViewDimensions");
var imageAfterSizeChange = App.WaitForElement("DotnetBot").GetRect();
App.Click("ChangeCarouselViewDimensions");
var imageAfterSizeChangedBacktoInitial = App.WaitForElement("DotnetBot").GetRect();

Assert.AreEqual(imageInitial, imageAfterSizeChangedBacktoInitial);
Assert.AreNotEqual(imageInitial, imageAfterSizeChange);
}
}

0 comments on commit ec0deda

Please sign in to comment.