From 78613ba4642bde71192e8bb77ea5ead79151f270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=8A=B9=EA=B7=BC/Common=20Platform=20Lab=28SR?= =?UTF-8?q?=29/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Tue, 7 Jun 2022 13:54:48 +0900 Subject: [PATCH] Optimize Layout Measure (#495) --- .../Handlers/Layout/LayoutHandler.Tizen.cs | 8 ++++- .../ScrollView/ScrollViewHandler.Tizen.cs | 10 +----- .../src/Platform/Tizen/LayoutViewGroup.cs | 35 ++++++++++++++++--- src/Core/src/Platform/Tizen/ViewExtensions.cs | 6 +++- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/Core/src/Handlers/Layout/LayoutHandler.Tizen.cs b/src/Core/src/Handlers/Layout/LayoutHandler.Tizen.cs index 3ab4dadafa80..088762fa9ec2 100644 --- a/src/Core/src/Handlers/Layout/LayoutHandler.Tizen.cs +++ b/src/Core/src/Handlers/Layout/LayoutHandler.Tizen.cs @@ -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) @@ -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) @@ -71,6 +72,8 @@ public void Remove(IView child) PlatformView.Children.Remove(childView); thandler.Dispose(); } + PlatformView.MarkChanged(); + PlatformView.SetNeedMeasureUpdate(); } public void Clear() @@ -84,6 +87,7 @@ public void Clear() { child.Dispose(); } + PlatformView.SetNeedMeasureUpdate(); } public void Insert(int index, IView child) @@ -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) @@ -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) diff --git a/src/Core/src/Handlers/ScrollView/ScrollViewHandler.Tizen.cs b/src/Core/src/Handlers/ScrollView/ScrollViewHandler.Tizen.cs index 71bc4659f98c..4f789388edae 100644 --- a/src/Core/src/Handlers/ScrollView/ScrollViewHandler.Tizen.cs +++ b/src/Core/src/Handlers/ScrollView/ScrollViewHandler.Tizen.cs @@ -5,7 +5,6 @@ namespace Microsoft.Maui.Handlers { public partial class ScrollViewHandler : ViewHandler { - IPlatformViewHandler? _contentHandler; double _cachedWidth; double _cachedHeight; @@ -19,7 +18,6 @@ protected override void ConnectHandler(ScrollView platformView) platformView.Scrolling += OnScrolled; platformView.ScrollAnimationEnded += ScrollAnimationEnded; - platformView.Relayout += OnRelayout; } protected override void DisconnectHandler(ScrollView platformView) @@ -27,7 +25,6 @@ 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) @@ -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) diff --git a/src/Core/src/Platform/Tizen/LayoutViewGroup.cs b/src/Core/src/Platform/Tizen/LayoutViewGroup.cs index 34c12e983c5f..dbe54cfba8ec 100644 --- a/src/Core/src/Platform/Tizen/LayoutViewGroup.cs +++ b/src/Core/src/Platform/Tizen/LayoutViewGroup.cs @@ -13,6 +13,8 @@ public class LayoutViewGroup : ViewGroup, IMeasurable IView _virtualView; Size _measureCache; + bool _needMeasureUpdate; + public LayoutViewGroup(IView view) { _virtualView = view; @@ -22,9 +24,20 @@ public LayoutViewGroup(IView view) public Func? CrossPlatformMeasure { get; set; } public Func? 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; @@ -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); } } } diff --git a/src/Core/src/Platform/Tizen/ViewExtensions.cs b/src/Core/src/Platform/Tizen/ViewExtensions.cs index cd020381031c..a0ffbde7a3a8 100644 --- a/src/Core/src/Platform/Tizen/ViewExtensions.cs +++ b/src/Core/src/Platform/Tizen/ViewExtensions.cs @@ -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(); }