stack the elevation of elements (#6108)
authorShane Neuville <shane94@hotmail.com>
Fri, 10 May 2019 04:08:14 +0000 (21:08 -0700)
committerSamantha Houts <samhouts@users.noreply.github.com>
Fri, 10 May 2019 04:08:14 +0000 (21:08 -0700)
* stack the elevation of elements

* only layer implicit elevations

* check elevation of incoming elemelements

* fix api 19
fixes #2989
fixes #3543

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue2653.cs
Xamarin.Forms.Platform.Android/Elevation.cs
Xamarin.Forms.Platform.Android/VisualElementPackager.cs

index e501577..590a68b 100644 (file)
@@ -149,7 +149,7 @@ namespace Xamarin.Forms.Controls.Issues
                }
 
 // https://github.com/xamarin/Xamarin.Forms/issues/2989
-#if UITEST && !__ANDROID__
+#if UITEST
                [Test]
                public void ZIndexWhenInsertingChildren()
                {
index 85482a4..f12e513 100644 (file)
@@ -21,5 +21,28 @@ namespace Xamarin.Forms.Platform.Android
 
                        view.Elevation = elevation.Value;
                }
+
+               internal static float? GetElevation(global::Android.Views.View view)
+               {
+                       if (view == null || !Forms.IsLollipopOrNewer)
+                       {
+                               return null;
+                       }
+
+                       return view.Elevation;
+               }
+
+               internal static float? GetElevation(VisualElement element)
+               {
+                       if (element == null || !Forms.IsLollipopOrNewer)
+                       {
+                               return null;
+                       }
+
+                       var iec = element as IElementConfiguration<VisualElement>;
+                       var elevation = iec?.On<PlatformConfiguration.Android>().GetElevation();
+
+                       return elevation;
+               }
        }
 }
\ No newline at end of file
index 72e5ae9..01f1be9 100644 (file)
@@ -5,7 +5,6 @@ using Android.Content;
 using Xamarin.Forms.Internals;
 using Android.Views;
 using AView = Android.Views.View;
-using System.Linq;
 
 namespace Xamarin.Forms.Platform.Android
 {
@@ -144,6 +143,7 @@ namespace Xamarin.Forms.Platform.Android
                }
                void EnsureChildOrder()
                {
+                       float elevationToSet = 0;
                        for (var i = 0; i < ElementController.LogicalChildren.Count; i++)
                        {
                                Element child = ElementController.LogicalChildren[i];
@@ -152,7 +152,23 @@ namespace Xamarin.Forms.Platform.Android
                                {
                                        IVisualElementRenderer r = Platform.GetRenderer(element);
                                        if (r != null)
+                                       {
+                                               if (Forms.IsLollipopOrNewer)
+                                               {
+                                                       var elevation = ElevationHelper.GetElevation(r.View) ?? 0;
+                                                       var elementElevation = ElevationHelper.GetElevation(element);
+
+                                                       if (elementElevation == null)
+                                                       {
+                                                               if (elevation > elevationToSet)
+                                                                       elevationToSet = elevation;
+
+                                                               r.View.Elevation = elevationToSet;
+                                                       }
+                                               }
+
                                                (_renderer.View as ViewGroup)?.BringChildToFront(r.View);
+                                       }
                                }
                        }
                }
@@ -163,7 +179,34 @@ namespace Xamarin.Forms.Platform.Android
                        if (view != null)
                                AddChild(view);
 
-                       if (ElementController.LogicalChildren.LastOrDefault() != view)
+                       int itemCount = ElementController.LogicalChildren.Count;
+                       if (itemCount <= 1)
+                               return;
+
+
+                       Element lastChild = ElementController.LogicalChildren[itemCount - 1];
+
+                       if(lastChild != view)
+                       {
+                               EnsureChildOrder();
+                               return;
+                       }
+
+                       if (!Forms.IsLollipopOrNewer)
+                               return;
+
+                       Element previousChild = ElementController.LogicalChildren[itemCount - 2];
+
+                       IVisualElementRenderer lastRenderer = null;
+                       IVisualElementRenderer previousRenderer = null;
+
+                       if (lastChild is VisualElement last)
+                               lastRenderer = Platform.GetRenderer(last);
+
+                       if (previousChild is VisualElement previous)
+                               previousRenderer = Platform.GetRenderer(previous);
+
+                       if (ElevationHelper.GetElevation(lastRenderer?.View) < ElevationHelper.GetElevation(previousRenderer?.View))
                                EnsureChildOrder();
                }