Skip to content

Commit

Permalink
Optimize Layout Measure (dotnet#495)
Browse files Browse the repository at this point in the history
  • Loading branch information
myroot committed Aug 24, 2022
1 parent 10484c5 commit 94930d4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
8 changes: 7 additions & 1 deletion src/Core/src/Handlers/Layout/LayoutHandler.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected override LayoutViewGroup CreatePlatformView()

public override Graphics.Size GetDesiredSize(double widthConstraint, double heightConstraint)
{
return VirtualView.CrossPlatformMeasure(widthConstraint, heightConstraint);
return PlatformView.InvokeCrossPlatformMeasure(widthConstraint, heightConstraint);
}

public override void SetVirtualView(IView view)
Expand Down Expand Up @@ -59,6 +59,7 @@ public void Add(IView child)

var targetIndex = VirtualView.GetLayoutHandlerIndex(child);
PlatformView.Children.Insert(targetIndex, child.ToPlatform(MauiContext));
PlatformView.SetNeedMeasureUpdate();
}

public void Remove(IView child)
Expand All @@ -71,6 +72,8 @@ public void Remove(IView child)
PlatformView.Children.Remove(childView);
thandler.Dispose();
}
PlatformView.MarkChanged();
PlatformView.SetNeedMeasureUpdate();
}

public void Clear()
Expand All @@ -84,6 +87,7 @@ public void Clear()
{
child.Dispose();
}
PlatformView.SetNeedMeasureUpdate();
}

public void Insert(int index, IView child)
Expand All @@ -94,6 +98,7 @@ public void Insert(int index, IView child)

var targetIndex = VirtualView.GetLayoutHandlerIndex(child);
PlatformView.Children.Insert(targetIndex, child.ToPlatform(MauiContext));
PlatformView.SetNeedMeasureUpdate();
}

public void Update(int index, IView child)
Expand All @@ -108,6 +113,7 @@ public void Update(int index, IView child)

var targetIndex = VirtualView.GetLayoutHandlerIndex(child);
PlatformView.Children.Insert(targetIndex, child.ToPlatform(MauiContext));
PlatformView.SetNeedMeasureUpdate();
}

public void UpdateZIndex(IView child)
Expand Down
10 changes: 1 addition & 9 deletions src/Core/src/Handlers/ScrollView/ScrollViewHandler.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Microsoft.Maui.Handlers
{
public partial class ScrollViewHandler : ViewHandler<IScrollView, ScrollView>
{

IPlatformViewHandler? _contentHandler;
double _cachedWidth;
double _cachedHeight;
Expand All @@ -19,15 +18,13 @@ protected override void ConnectHandler(ScrollView platformView)

platformView.Scrolling += OnScrolled;
platformView.ScrollAnimationEnded += ScrollAnimationEnded;
platformView.Relayout += OnRelayout;
}

protected override void DisconnectHandler(ScrollView platformView)
{
base.DisconnectHandler(platformView);
platformView.Scrolling -= OnScrolled;
platformView.ScrollAnimationEnded -= ScrollAnimationEnded;
platformView.Relayout -= OnRelayout;
}

public override Graphics.Size GetDesiredSize(double widthConstraint, double heightConstraint)
Expand Down Expand Up @@ -98,22 +95,17 @@ void UpdateContent(IPlatformViewHandler? content)
void OnContentLayoutUpdated(object? sender, Tizen.UIExtensions.Common.LayoutEventArgs e)
{
var platformGeometry = PlatformView.GetBounds().ToDP();

var measuredSize = VirtualView.CrossPlatformMeasure(platformGeometry.Width, platformGeometry.Height);
if (_measureCache != measuredSize)
{
platformGeometry.X = 0;
platformGeometry.Y = 0;
VirtualView.CrossPlatformArrange(platformGeometry);
UpdateContentSize();
}
_measureCache = measuredSize;
}

void OnRelayout(object? sender, EventArgs e)
{
UpdateContentSize();
}

public static void MapContent(IScrollViewHandler handler, IScrollView scrollView)
{
if (handler.MauiContext == null || scrollView.PresentedContent == null || handler is not ScrollViewHandler sHandler)
Expand Down
35 changes: 30 additions & 5 deletions src/Core/src/Platform/Tizen/LayoutViewGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class LayoutViewGroup : ViewGroup, IMeasurable
IView _virtualView;
Size _measureCache;

bool _needMeasureUpdate;

public LayoutViewGroup(IView view)
{
_virtualView = view;
Expand All @@ -22,9 +24,20 @@ public LayoutViewGroup(IView view)
public Func<double, double, Size>? CrossPlatformMeasure { get; set; }
public Func<Rect, Size>? CrossPlatformArrange { get; set; }

public void SetNeedMeasureUpdate()
{
_needMeasureUpdate = true;
MarkChanged();
}

public void ClearNeedMeasureUpdate()
{
_needMeasureUpdate = false;
}

public TSize Measure(double availableWidth, double availableHeight)
{
return CrossPlatformMeasure?.Invoke(availableWidth.ToScaledDP(), availableHeight.ToScaledDP()).ToPixel() ?? new TSize(0, 0);
return InvokeCrossPlatformMeasure(availableWidth.ToScaledDP(), availableHeight.ToScaledDP()).ToPixel();
}

public bool InputTransparent { get; set; } = false;
Expand All @@ -34,22 +47,34 @@ protected override bool HitTest(TTouch touch)
return !InputTransparent;
}

void OnLayoutUpdated(object? sender, LayoutEventArgs e)
public Size InvokeCrossPlatformMeasure(double availableWidth, double availableHeight)
{
var platformGeometry = this.GetBounds().ToDP();
if (CrossPlatformMeasure == null)
return Graphics.Size.Zero;

var measured = CrossPlatformMeasure!(platformGeometry.Width, platformGeometry.Height);
ClearNeedMeasureUpdate();
var measured = CrossPlatformMeasure(availableWidth, availableHeight);
if (measured != _measureCache && _virtualView?.Parent is IView parentView)
{
parentView?.InvalidateMeasure();
}
_measureCache = measured;
return measured;
}

void OnLayoutUpdated(object? sender, LayoutEventArgs e)
{
var platformGeometry = this.GetBounds().ToDP();

if (_needMeasureUpdate)
{
InvokeCrossPlatformMeasure(platformGeometry.Width, platformGeometry.Height);
}
if (platformGeometry.Width > 0 && platformGeometry.Height > 0)
{
platformGeometry.X = 0;
platformGeometry.Y = 0;
CrossPlatformArrange!(platformGeometry);
CrossPlatformArrange?.Invoke(platformGeometry);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Core/src/Platform/Tizen/ViewExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ public static void UpdateSemantics(this NView platformView, IView view)

public static void InvalidateMeasure(this NView platformView, IView view)
{
if (platformView is ViewGroup viewGroup)
if (platformView is LayoutViewGroup layoutViewGroup)
{
layoutViewGroup.SetNeedMeasureUpdate();
}
else if (platformView is ViewGroup viewGroup)
{
viewGroup.MarkChanged();
}
Expand Down

0 comments on commit 94930d4

Please sign in to comment.