Refactor layout update code
authorSeungkeun Lee <sngn.lee@samsung.com>
Wed, 12 Apr 2017 05:30:25 +0000 (14:30 +0900)
committerKangho Hur <kangho.hur@samsung.com>
Mon, 10 Jul 2017 02:11:22 +0000 (11:11 +0900)
 - A small refactoring
 - In OnBatchCommitted, Only update the Element itself location, not update children
   Because it is enough
 - Prevent duplicate update
 - Globally applied IgnoreBatchCommitted was changed to locally and re-named
 - Fixed rotation issue

Change-Id: I32b57317169d81d7ad6e45f0b52eb2a8f1d861fa

Xamarin.Forms.Platform.Tizen/Native/Box.cs
Xamarin.Forms.Platform.Tizen/Native/LayoutEventArgs.cs
Xamarin.Forms.Platform.Tizen/Renderers/VisualElementRenderer.cs

index 9bd7bad..2f55190 100644 (file)
@@ -52,10 +52,7 @@ namespace Xamarin.Forms.Platform.Tizen.Native
                                LayoutUpdated(this, new LayoutEventArgs()
                                        {
                                                HasChanged = g != _previousGeometry,
-                                               X = g.X,
-                                               Y = g.Y,
-                                               Width = g.Width,
-                                               Height = g.Height,
+                                               Geometry = g,
                                        }
                                );
                        }
index 6668f8d..060a121 100644 (file)
@@ -1,4 +1,5 @@
 using System;
+using ElmSharp;
 
 namespace Xamarin.Forms.Platform.Tizen.Native
 {
@@ -17,39 +18,12 @@ namespace Xamarin.Forms.Platform.Tizen.Native
                }
 
                /// <summary>
-               /// X coordinate of the layout area, relative to the main window.
+               /// Geometry of the layout area, absolute coordinate
                /// </summary>
-               public int X
+               public Rect Geometry
                {
                        get;
-                       internal set;
-               }
-
-               /// <summary>
-               /// Y coordinate of the layout area, relative to the main window.
-               /// </summary>
-               public int Y
-               {
-                       get;
-                       internal set;
-               }
-
-               /// <summary>
-               /// Width of the layout area.
-               /// </summary>
-               public int Width
-               {
-                       get;
-                       internal set;
-               }
-
-               /// <summary>
-               /// Height of the layout area.
-               /// </summary>
-               public int Height
-               {
-                       get;
-                       internal set;
+                       set;
                }
        }
 }
index 73e8c64..37e2a03 100644 (file)
@@ -44,6 +44,8 @@ namespace Xamarin.Forms.Platform.Tizen
 
                HashSet<string> _batchedProperties = new HashSet<string>();
 
+               int _layoutCallback = 0;
+
                /// <summary>
                /// Default constructor.
                /// </summary>
@@ -209,10 +211,13 @@ namespace Xamarin.Forms.Platform.Tizen
 
                public void UpdateNativeGeometry()
                {
-                       var x = ComputeAbsoluteX(Element);
-                       var y = ComputeAbsoluteY(Element);
-                       NativeView.Geometry = new Rectangle(x, y, Element.Width, Element.Height).ToPixel();
-                       ApplyTransformation();
+                       var updatedGeometry = new Rectangle(ComputeAbsolutePoint(Element), new Size(Element.Width, Element.Height)).ToPixel();
+
+                       if (NativeView.Geometry != updatedGeometry)
+                       {
+                               NativeView.Geometry = updatedGeometry;
+                               ApplyTransformation();
+                       }
                }
 
                void IVisualElementRenderer.SetElement(VisualElement element)
@@ -250,14 +255,15 @@ namespace Xamarin.Forms.Platform.Tizen
                protected virtual void UpdateLayout()
                {
                        // we're updating the coordinates of native control only if they were modified
-                       // via Xamarin (Settings.IgnoreBatchCommitted is set to false);
+                       // via Xamarin (IsNativeLayouting() returns false);
                        // otherwise native control is already in the right place
-                       if (!Settings.IgnoreBatchCommitted && null != NativeView)
+                       if (!IsNativeLayouting() && null != NativeView)
                        {
                                UpdateNativeGeometry();
                        }
 
                        // we're updating just immediate children
+                       // To update the relative postion of children
                        var logicalChildren = (Element as IElementController).LogicalChildren;
                        foreach (var child in logicalChildren)
                        {
@@ -291,9 +297,7 @@ namespace Xamarin.Forms.Platform.Tizen
 
                                        Element.FocusChangeRequested -= OnFocusChangeRequested;
 
-                                       Settings.StartIgnoringBatchCommitted();
                                        Element.Layout(new Rectangle(0, 0, -1, -1));
-                                       Settings.StopIgnoringBatchCommitted();
 
                                        var logicalChildren = (Element as IElementController).LogicalChildren;
                                        foreach (var child in logicalChildren)
@@ -334,9 +338,7 @@ namespace Xamarin.Forms.Platform.Tizen
 
                                e.OldElement.FocusChangeRequested -= OnFocusChangeRequested;
 
-                               Settings.StartIgnoringBatchCommitted();
                                Element.Layout(new Rectangle(0, 0, -1, -1));
-                               Settings.StopIgnoringBatchCommitted();
 
                                var controller = e.OldElement as IElementController;
                                if (controller != null && controller.EffectControlProvider == this)
@@ -460,9 +462,9 @@ namespace Xamarin.Forms.Platform.Tizen
                {
                        if (_flags.HasFlag(VisualElementRendererFlags.NeedsLayout))
                        {
-                               if (!Settings.IgnoreBatchCommitted)
+                               if (!IsNativeLayouting())
                                {
-                                       UpdateLayout();
+                                       UpdateNativeGeometry();
                                        // UpdateLayout already updates transformation, clear NeedsTranformation flag then
                                        _flags &= ~VisualElementRendererFlags.NeedsTransformation;
                                }
@@ -548,19 +550,21 @@ namespace Xamarin.Forms.Platform.Tizen
 
                protected void DoLayout(Native.LayoutEventArgs e)
                {
-                       Settings.StartIgnoringBatchCommitted();
-
-                       Element.Layout(new Rectangle(Element.X, Element.Y, Forms.ConvertToScaledDP(e.Width), Forms.ConvertToScaledDP(e.Height)));
+                       EnterNativeLayoutCallback();
                        if (e.HasChanged)
                        {
+                               var bound = e.Geometry.ToDP();
+                               bound.X = Element.X;
+                               bound.Y = Element.Y;
+                               Element.Layout(bound);
                                UpdateLayout();
                        }
-                       Settings.StopIgnoringBatchCommitted();
+                       LeaveNativeLayoutCallback();
                }
 
                protected virtual Size MinimumSize()
                {
-                       return new Size();
+                       return new ESize(NativeView.MinimumWidth, NativeView.MinimumHeight).ToDP();
                }
 
                /// <summary>
@@ -606,6 +610,11 @@ namespace Xamarin.Forms.Platform.Tizen
                        return e.Y + (e.RealParent is VisualElement ? Forms.ConvertToScaledDP(Platform.GetRenderer(e.RealParent).NativeView.Geometry.Y) : 0.0);
                }
 
+               static Point ComputeAbsolutePoint(VisualElement e)
+               {
+                       return new Point(ComputeAbsoluteX(e), ComputeAbsoluteY(e));
+               }
+
                /// <summary>
                /// Handles focus events.
                /// </summary>
@@ -995,7 +1004,6 @@ namespace Xamarin.Forms.Platform.Tizen
                                NativeView.EvasMap = map;
                        }
                }
-
                EFocusDirection ConvertToNativeFocusDirection(string direction) {
                        if (direction == XFocusDirection.Back) return EFocusDirection.Previous;
                        if (direction == XFocusDirection.Forward) return EFocusDirection.Next;
@@ -1006,29 +1014,18 @@ namespace Xamarin.Forms.Platform.Tizen
 
                        return EFocusDirection.Next;
                }
-       }
 
-       internal static class Settings
-       {
-               static int s_ignoreCount = 0;
-
-               public static bool IgnoreBatchCommitted
+               void EnterNativeLayoutCallback()
                {
-                       get
-                       {
-                               return s_ignoreCount != 0;
-                       }
+                       _layoutCallback++;
                }
-
-               public static void StartIgnoringBatchCommitted()
+               void LeaveNativeLayoutCallback()
                {
-                       ++s_ignoreCount;
+                       _layoutCallback--;
                }
-
-               public static void StopIgnoringBatchCommitted()
+               bool IsNativeLayouting()
                {
-                       Debug.Assert(s_ignoreCount > 0);
-                       --s_ignoreCount;
+                       return _layoutCallback > 0;
                }
        }
 }