Skip to content

Commit

Permalink
improved macios rendering performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Dobrynin committed Aug 17, 2024
1 parent 9c225fc commit 80134d7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
2 changes: 0 additions & 2 deletions MPowerKit.VirtualizeListView/CellHolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ public class CellHolder : Grid

public CellHolder()
{
VerticalOptions = HorizontalOptions = LayoutOptions.Start;

this.SizeChanged += CellHolder_SizeChanged;
}

Expand Down
33 changes: 24 additions & 9 deletions MPowerKit.VirtualizeListView/Layouts/LinearItemsLayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,24 @@ protected override Size MeasureItem(IReadOnlyList<VirtualizeListViewItem> items,

var iview = (item.Cell as IView)!;

return IsOrientation(ScrollOrientation.Vertical)
? iview.Measure(availableSpace.Width, double.PositiveInfinity)
: iview.Measure(double.PositiveInfinity, availableSpace.Height);
Size measure;

if (IsOrientation(ScrollOrientation.Vertical))
{
measure = iview.Measure(availableSpace.Width, double.PositiveInfinity);

item.CellBounds = new Rect(item.CellBounds.X, item.CellBounds.Y, availableSpace.Width, measure.Height);
item.Bounds = new Rect(item.Bounds.X, item.Bounds.Y, availableSpace.Width, measure.Height + item.Margin.VerticalThickness);
}
else
{
measure = iview.Measure(double.PositiveInfinity, availableSpace.Height);

item.CellBounds = new Rect(item.CellBounds.X, item.CellBounds.Y, measure.Width, availableSpace.Height);
item.Bounds = new Rect(item.Bounds.X, item.Bounds.Y, measure.Width + item.Margin.HorizontalThickness, availableSpace.Height);
}

return measure;
}

protected override void ArrangeItem(IReadOnlyList<VirtualizeListViewItem> items, VirtualizeListViewItem item, Size availableSpace)
Expand All @@ -71,21 +86,21 @@ protected override void ArrangeItem(IReadOnlyList<VirtualizeListViewItem> items,

var newAvailableSpace = new Size(availableSpace.Width, availableSpace.Height);

var request = MeasureItem(items, item, newAvailableSpace);
item.CellBounds = new Rect(margin.Left, top + margin.Top, 0d, 0d);
item.Bounds = new Rect(0d, top, 0d, 0d);

item.CellBounds = new Rect(margin.Left, top + margin.Top, newAvailableSpace.Width, request.Height);
item.Bounds = new Rect(0d, top, newAvailableSpace.Width, request.Height + margin.VerticalThickness);
MeasureItem(items, item, newAvailableSpace);
}
else
{
var left = prevItemBounds.Right;

var newAvailableSpace = new Size(availableSpace.Width, availableSpace.Height);

var request = MeasureItem(items, item, newAvailableSpace);
item.CellBounds = new Rect(left + margin.Left, margin.Top, 0d, 0d);
item.Bounds = new Rect(left, 0d, 0d, 0d);

item.CellBounds = new Rect(left + margin.Left, margin.Top, request.Width, newAvailableSpace.Height);
item.Bounds = new Rect(left, 0d, request.Width + margin.HorizontalThickness, newAvailableSpace.Height);
MeasureItem(items, item, newAvailableSpace);
}
}

Expand Down
50 changes: 20 additions & 30 deletions MPowerKit.VirtualizeListView/Layouts/VirtualizeItemsLayoutManger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,21 @@ protected virtual bool DrawAndTriggerResize()
return TriggerResizeLayout();
}

protected virtual void DrawItem(IReadOnlyList<VirtualizeListViewItem> items, VirtualizeListViewItem item)
{
if (items.Count == 0 || item.Position < 0 || item.Cell is null) return;

var view = item.Cell;
#if !ANDROID
if (view.TranslationX != view.Item.CellBounds.X ||
view.TranslationY != view.Item.CellBounds.Y)
{
view.TranslationX = view.Item.CellBounds.X;
view.TranslationY = view.Item.CellBounds.Y;
}
#endif
}

protected virtual bool TriggerResizeLayout()
{
Size desiredSize = new();
Expand Down Expand Up @@ -747,34 +762,12 @@ protected virtual VirtualizeListViewItem CreateDummyItem(DataTemplate template,
Position = 0
};

var size =
#if MACIOS
//macios needs this to properly draw pooled cached cell
(cell as IView).Measure(double.PositiveInfinity, double.PositiveInfinity);
#else
GetEstimatedItemSize(item);
#endif

var size = GetEstimatedItemSize(item);
item.CellBounds = item.Bounds = new(0d, 0d, size.Width, size.Height);

return item;
}

protected virtual void DrawItem(IReadOnlyList<VirtualizeListViewItem> items, VirtualizeListViewItem item)
{
if (items.Count == 0 || item.Position < 0 || item.Cell is null) return;

var view = item.Cell;
#if !ANDROID
if (view.TranslationX != view.Item.CellBounds.X ||
view.TranslationY != view.Item.CellBounds.Y)
{
view.TranslationX = view.Item.CellBounds.X;
view.TranslationY = view.Item.CellBounds.Y;
}
#endif
}

protected virtual Thickness GetItemMargin(IReadOnlyList<VirtualizeListViewItem> items, VirtualizeListViewItem item)
{
return new Thickness();
Expand All @@ -798,8 +791,6 @@ protected virtual bool IsOrientation(ScrollOrientation orientation)
#region ILayoutManager
public virtual Size Measure(double widthConstraint, double heightConstraint)
{
var desiredSize = GetDesiredLayoutSize(widthConstraint, heightConstraint);

var items = CollectionsMarshal.AsSpan((this as IBindableLayout).Children as List<IView>);
var length = items.Length;

Expand All @@ -813,9 +804,11 @@ public virtual Size Measure(double widthConstraint, double heightConstraint)
if (view.IsCached || !view.Item.IsAttached) continue;

// this triggers item size change when needed
var measure = MeasureItem(LaidOutItems, view.Item, availableSpace);
MeasureItem(LaidOutItems, view.Item, availableSpace);
}

var desiredSize = GetDesiredLayoutSize(widthConstraint, heightConstraint);

return desiredSize;
}

Expand All @@ -834,18 +827,15 @@ public virtual Size ArrangeChildren(Rect bounds)
var child = items[n];
var view = child as CellHolder;

#if !MACIOS
if (view.IsCached || !view.Item.IsAttached) continue;
#endif

var (x, y) =
#if ANDROID
(view.Item.CellBounds.X, view.Item.CellBounds.Y);
#else
(0d, 0d);
#endif

var newBounds = new Rect(x, y, view.DesiredSize.Width, view.DesiredSize.Height);
var newBounds = new Rect(x, y, view.Item.CellBounds.Width, view.Item.CellBounds.Height);

#if MACIOS
if (view.Bounds == newBounds) continue;
Expand Down

0 comments on commit 80134d7

Please sign in to comment.