Skip to content

Commit

Permalink
Merge the fixes on SR4 into main (#21782)
Browse files Browse the repository at this point in the history
### Description of Change

Upstream all the fixes!
  • Loading branch information
mattleibow authored Apr 11, 2024
2 parents 01e72e2 + c6ff4cd commit 0a562dc
Show file tree
Hide file tree
Showing 19 changed files with 576 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void ButtonClicked(object sender, EventArgs e)
#if UITEST
[Test]
[Compatibility.UITests.FailsOnMauiIOS]
[Compatibility.UITests.MovedToAppium]
public void CollectionViewItemsLayoutUpdate()
{
RunningApp.WaitForElement("CollectionView5354");
Expand Down
94 changes: 94 additions & 0 deletions src/Controls/samples/Controls.Sample.UITests/Issues/Issue21711.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Maui;
using Microsoft.Maui.Controls;

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 21711, "NullReferenceException from FlexLayout.InitItemProperties", PlatformAffected.iOS)]
public class Issue21711 : TestContentPage
{
protected override void Init()
{
FlexLayout flex = null!;
Content = new VerticalStackLayout
{
new Button
{
Text = "Add",
AutomationId = "Add",
Command = new Command(() =>
{
flex.Clear();
flex.Add(NewLabel(0));
flex.Add(NewLabel(1));
flex.Clear();
flex.Add(NewLabel(2));
flex.Add(NewLabel(3));
})
},
new Button
{
Text = "Insert",
AutomationId = "Insert",
Command = new Command(() =>
{
flex.Clear();
flex.Insert(0, NewLabel(1));
flex.Insert(0, NewLabel(0));
flex.Clear();
flex.Insert(0, NewLabel(3));
flex.Insert(0, NewLabel(2));
})
},
new Button
{
Text = "Update",
AutomationId = "Update",
Command = new Command(() =>
{
flex.Clear();
flex.Add(NewLabel(0));
flex[0] = NewLabel(1);
flex.Clear();
flex.Add(NewLabel(2));
flex[0] = NewLabel(3);
})
},
new Button
{
Text = "Remove",
AutomationId = "Remove",
Command = new Command(() =>
{
flex.Clear();
var label = NewLabel(0);
flex.Add(label);
flex.Remove(label);
flex.Clear();
label = NewLabel(1);
flex.Add(label);
flex.Remove(label);
flex.Add(NewLabel(2));
})
},
(flex = new FlexLayout { }),
};
}

Label NewLabel(int count) =>
new Label
{
Text = $"Item{count}",
AutomationId = $"Item{count}",
Background = Brush.Yellow,
TextType = TextType.Html
};
}
}
33 changes: 33 additions & 0 deletions src/Controls/samples/Controls.Sample.UITests/Issues/Issue5354.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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.Issue5354">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>

<StackLayout Orientation="Vertical" Spacing="5" Grid.Row="0" VerticalOptions="Center">
<Label x:Name="Label" LineBreakMode="WordWrap" Text="Switch between linear and grid layouts. If layouts appear as expected with proper spacing between items, the test passes." HorizontalTextAlignment="Center" VerticalTextAlignment="Center"/>
<Button AutomationId="Button5354" Text="Switch to grid layout" HorizontalOptions="Center" VerticalOptions="Center" Clicked="ButtonClicked"/>
</StackLayout>

<CollectionView AutomationId="CollectionView5354" Grid.Row="1" ItemsSource="{Binding Items}">
<CollectionView.ItemsLayout>
<LinearItemsLayout Orientation="Vertical" ItemSpacing="5"/>
</CollectionView.ItemsLayout>

<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout Orientation="Vertical" Spacing="10" BackgroundColor="Beige" Padding="10">
<Image Source="{Binding Source}" HeightRequest="100"/>
<Label Text="{Binding Text}" HorizontalTextAlignment="Center" AutomationId="{Binding AutomationId}"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>

</CollectionView>
</Grid>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Controls;


namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.None, 5354, "[CollectionView] Updating the ItemsLayout type should refresh the layout", PlatformAffected.All)]
public partial class Issue5354 : ContentPage
{
int count = 0;

public Issue5354()
{
InitializeComponent();

BindingContext = new ViewModel5354();
}

void ButtonClicked(object sender, EventArgs e)
{
var button = sender as Button;
var stackLayout = button.Parent as StackLayout;
var grid = stackLayout.Parent as Grid;
var collectionView = grid.Children[1] as CollectionView;

if (count % 2 == 0)
{
collectionView.ItemsLayout = new GridItemsLayout(ItemsLayoutOrientation.Vertical)
{
Span = 2,
HorizontalItemSpacing = 5,
VerticalItemSpacing = 5
};

button.Text = "Switch to linear layout";
}
else
{
collectionView.ItemsLayout = new LinearItemsLayout(ItemsLayoutOrientation.Vertical)
{
ItemSpacing = 5
};

button.Text = "Switch to grid layout";
}

++count;
}
}

[Preserve(AllMembers = true)]
public class ViewModel5354
{
public ObservableCollection<Model5354> Items { get; set; }

public ViewModel5354()
{
var collection = new ObservableCollection<Model5354>();
var pageSize = 50;

for (var i = 0; i < pageSize; i++)
{
collection.Add(new Model5354
{
Text = "Image" + i,
Source = i % 2 == 0 ?
"groceries.png" :
"dotnet_bot.png",
AutomationId = "Image" + i
});
}

Items = collection;
}
}

[Preserve(AllMembers = true)]
public class Model5354
{
public string Text { get; set; }

public string Source { get; set; }

public string AutomationId { get; set; }

public Model5354()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#if ANDROID || (IOS && !MACCATALYST)
// This behavior isn't lit up for WinUI because it's never been supported on WinUI, event in Xamarin.Forms
// The primary purpose of this API is for XF migration purposes.
// Ideally users would use behavior that's more accessible forward and consistent with platform expectations.
#if ANDROID || IOS
using System;
using System.Collections.Generic;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ bool FeatureEnabled
}
}

#if !(ANDROID || (IOS && !MACCATALYST))
#if !(ANDROID || IOS)
internal void UpdateFocusForView(InputView iv)
{

Expand Down
28 changes: 12 additions & 16 deletions src/Controls/src/Core/Layout/FlexLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ void InitItemProperties(IView view, Flex.Item item)

internal bool InMeasureMode { get; set; }

void AddFlexItem(IView child)
void AddFlexItem(int index, IView child)
{
if (_root == null)
return;
Expand Down Expand Up @@ -518,7 +518,7 @@ void AddFlexItem(IView child)
};
}

_root.InsertAt(Children.IndexOf(child), item);
_root.InsertAt(index, item);
SetFlexItem(child, item);
}

Expand Down Expand Up @@ -546,14 +546,8 @@ protected override ILayoutManager CreateLayoutManager()
return new FlexLayoutManager(this);
}

public Graphics.Rect GetFlexFrame(IView view)
{
return view switch
{
BindableObject bo => ((Flex.Item)bo.GetValue(FlexItemProperty)).GetFrame(),
_ => _viewInfo[view].FlexItem.GetFrame(),
};
}
public Graphics.Rect GetFlexFrame(IView view) =>
GetFlexItem(view).GetFrame();

void EnsureFlexItemPropertiesUpdated()
{
Expand Down Expand Up @@ -601,8 +595,10 @@ protected override void OnParentSet()
void PopulateLayout()
{
InitLayoutProperties(_root = new Flex.Item());
foreach (var child in Children)
AddFlexItem(child);
for (var i = 0; i < Children.Count; i++)
{
AddFlexItem(i, Children[i]);
}
}

void ClearLayout()
Expand All @@ -623,21 +619,21 @@ void InitLayoutProperties(Flex.Item item)

protected override void OnAdd(int index, IView view)
{
AddFlexItem(index, view);
base.OnAdd(index, view);
AddFlexItem(view);
}

protected override void OnInsert(int index, IView view)
{
AddFlexItem(index, view);
base.OnInsert(index, view);
AddFlexItem(view);
}

protected override void OnUpdate(int index, IView view, IView oldView)
{
base.OnUpdate(index, view, oldView);
RemoveFlexItem(oldView);
AddFlexItem(view);
AddFlexItem(index, view);
base.OnUpdate(index, view, oldView);
}

protected override void OnRemove(int index, IView view)
Expand Down
Loading

0 comments on commit 0a562dc

Please sign in to comment.