Browse Source

Merge branch 'master' into fixes/2501-command-isenabled

Steven Kirk 6 years ago
parent
commit
55519113e8
100 changed files with 5539 additions and 3243 deletions
  1. 0 3
      .gitmodules
  2. 0 1
      Avalonia.sln
  3. 1 0
      native/Avalonia.Native/inc/avalonia-native.h
  4. 8 2
      native/Avalonia.Native/src/OSX/cursor.h
  5. 4 1
      native/Avalonia.Native/src/OSX/cursor.mm
  6. 10 0
      native/Avalonia.Native/src/OSX/window.mm
  7. 2 2
      readme.md
  8. 1 0
      samples/ControlCatalog.NetCore/ControlCatalog.NetCore.csproj
  9. 5 1
      samples/ControlCatalog.NetCore/Program.cs
  10. 2 2
      samples/ControlCatalog/Pages/DataGridPage.xaml
  11. 4 0
      samples/ControlCatalog/Pages/TextBoxPage.xaml
  12. 3 0
      samples/RenderDemo/MainWindow.xaml
  13. 49 0
      samples/RenderDemo/Pages/RenderTargetBitmapPage.cs
  14. 119 0
      src/Avalonia.Base/Utilities/MathUtilities.cs
  15. 9 2
      src/Avalonia.Build.Tasks/Program.cs
  16. 32 0
      src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.Helpers.cs
  17. 6 4
      src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs
  18. 19 6
      src/Avalonia.Controls.DataGrid/DataGridRow.cs
  19. 5 2
      src/Avalonia.Controls/Button.cs
  20. 5 5
      src/Avalonia.Controls/ColumnDefinition.cs
  21. 4 3
      src/Avalonia.Controls/ColumnDefinitions.cs
  22. 720 12
      src/Avalonia.Controls/DefinitionBase.cs
  23. 60 0
      src/Avalonia.Controls/DefinitionList.cs
  24. 3127 355
      src/Avalonia.Controls/Grid.cs
  25. 2 2
      src/Avalonia.Controls/MenuItem.cs
  26. 69 0
      src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs
  27. 46 18
      src/Avalonia.Controls/Presenters/TextPresenter.cs
  28. 2 1
      src/Avalonia.Controls/Primitives/SelectingItemsControl.cs
  29. 8 8
      src/Avalonia.Controls/RowDefinition.cs
  30. 2 3
      src/Avalonia.Controls/RowDefinitions.cs
  31. 17 1
      src/Avalonia.Controls/TabControl.cs
  32. 30 8
      src/Avalonia.Controls/TextBox.cs
  33. 0 705
      src/Avalonia.Controls/Utils/GridLayout.cs
  34. 0 651
      src/Avalonia.Controls/Utils/SharedSizeScopeHost.cs
  35. 94 102
      src/Avalonia.Controls/WrapPanel.cs
  36. 2 4
      src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs
  37. 1 0
      src/Avalonia.Input/Cursors.cs
  38. 127 0
      src/Avalonia.Input/GestureRecognizers/GestureRecognizerCollection.cs
  39. 23 0
      src/Avalonia.Input/GestureRecognizers/IGestureRecognizer.cs
  40. 183 0
      src/Avalonia.Input/GestureRecognizers/ScrollGestureRecognizer.cs
  41. 8 0
      src/Avalonia.Input/Gestures.cs
  42. 39 0
      src/Avalonia.Input/InputElement.cs
  43. 51 68
      src/Avalonia.Input/MouseDevice.cs
  44. 30 20
      src/Avalonia.Input/Pointer.cs
  45. 24 7
      src/Avalonia.Input/PointerEventArgs.cs
  46. 3 2
      src/Avalonia.Input/PointerWheelEventArgs.cs
  47. 1 0
      src/Avalonia.Input/Properties/AssemblyInfo.cs
  48. 29 0
      src/Avalonia.Input/ScrollGestureEventArgs.cs
  49. 11 9
      src/Avalonia.Input/TouchDevice.cs
  50. 1 3
      src/Avalonia.Native/Avalonia.Native.csproj
  51. 18 0
      src/Avalonia.OpenGL/AngleOptions.cs
  52. 50 21
      src/Avalonia.OpenGL/EglDisplay.cs
  53. 27 6
      src/Avalonia.OpenGL/EglGlPlatformSurface.cs
  54. 42 1
      src/Avalonia.OpenGL/EglInterface.cs
  55. 6 1
      src/Avalonia.OpenGL/IGlPlatformSurfaceRenderTarget.cs
  56. 2 3
      src/Avalonia.ReactiveUI/AppBuilderExtensions.cs
  57. 55 0
      src/Avalonia.ReactiveUI/AutoDataTemplateBindingHook.cs
  58. 17 126
      src/Avalonia.ReactiveUI/RoutedViewHost.cs
  59. 75 0
      src/Avalonia.ReactiveUI/TransitioningContentControl.cs
  60. 80 0
      src/Avalonia.ReactiveUI/ViewModelViewHost.cs
  61. 7 5
      src/Avalonia.Themes.Default/Accents/BaseDark.xaml
  62. 7 5
      src/Avalonia.Themes.Default/Accents/BaseLight.xaml
  63. 3 3
      src/Avalonia.Themes.Default/Button.xaml
  64. 5 1
      src/Avalonia.Themes.Default/NotificationCard.xaml
  65. 9 2
      src/Avalonia.Themes.Default/ScrollViewer.xaml
  66. 6 1
      src/Avalonia.Themes.Default/TextBox.xaml
  67. 4 4
      src/Avalonia.Themes.Default/ToggleButton.xaml
  68. 4 4
      src/Avalonia.Visuals/Animation/CrossFade.cs
  69. 5 0
      src/Avalonia.Visuals/Platform/IRenderTarget.cs
  70. 5 0
      src/Avalonia.Visuals/Rendering/DeferredRenderer.cs
  71. 14 0
      src/Avalonia.Visuals/Rendering/ManagedDeferredRendererLock.cs
  72. 32 0
      src/Avalonia.Visuals/Rendering/UiThreadRenderTimer.cs
  73. 22 2
      src/Avalonia.X11/X11CursorFactory.cs
  74. 3 0
      src/Avalonia.X11/XLib.cs
  75. 2 1
      src/Gtk/Avalonia.Gtk3/CursorFactory.cs
  76. 1 0
      src/Gtk/Avalonia.Gtk3/GdkCursor.cs
  77. 2 19
      src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj
  78. 0 107
      src/Markup/Avalonia.Markup.Xaml/AvaloniaTypeConverters.cs
  79. 6 153
      src/Markup/Avalonia.Markup.Xaml/AvaloniaXamlLoader.cs
  80. 0 99
      src/Markup/Avalonia.Markup.Xaml/Converters/AvaloniaEventConverter.cs
  81. 0 1
      src/Markup/Avalonia.Markup.Xaml/Converters/AvaloniaPropertyTypeConverter.cs
  82. 1 2
      src/Markup/Avalonia.Markup.Xaml/Converters/BitmapTypeConverter.cs
  83. 0 1
      src/Markup/Avalonia.Markup.Xaml/Converters/FontFamilyTypeConverter.cs
  84. 0 1
      src/Markup/Avalonia.Markup.Xaml/Converters/IconTypeConverter.cs
  85. 0 89
      src/Markup/Avalonia.Markup.Xaml/Converters/NullableTypeConverter.cs
  86. 0 79
      src/Markup/Avalonia.Markup.Xaml/Converters/ParseTypeConverter.cs
  87. 0 27
      src/Markup/Avalonia.Markup.Xaml/Converters/SelectorTypeConverter.cs
  88. 0 49
      src/Markup/Avalonia.Markup.Xaml/Converters/SetterValueTypeConverter.cs
  89. 8 33
      src/Markup/Avalonia.Markup.Xaml/Extensions.cs
  90. 9 0
      src/Markup/Avalonia.Markup.Xaml/MarkupExtension.cs
  91. 2 12
      src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/BindingExtension.cs
  92. 2 7
      src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/DynamicResourceExtension.cs
  93. 3 4
      src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/RelativeSourceExtension.cs
  94. 2 10
      src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/ResourceInclude.cs
  95. 3 19
      src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs
  96. 2 7
      src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StyleIncludeExtension.cs
  97. 0 39
      src/Markup/Avalonia.Markup.Xaml/PortableXaml/AttributeExtensions.cs
  98. 0 83
      src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaMemberAttributeProvider.cs
  99. 0 56
      src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaNameScope.cs
  100. 0 147
      src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaRuntimeTypeProvider.cs

+ 0 - 3
.gitmodules

@@ -1,6 +1,3 @@
-[submodule "src/Markup/Avalonia.Markup.Xaml/PortableXaml/portable.xaml.github"]
-	path = src/Markup/Avalonia.Markup.Xaml/PortableXaml/portable.xaml.github
-	url = https://github.com/AvaloniaUI/Portable.Xaml.git
 [submodule "nukebuild/Numerge"]
 	path = nukebuild/Numerge
 	url = https://github.com/kekekeks/Numerge.git

+ 0 - 1
Avalonia.sln

@@ -146,7 +146,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Props", "Props", "{F3AC8BC1
 		build\Serilog.props = build\Serilog.props
 		build\SharpDX.props = build\SharpDX.props
 		build\SkiaSharp.props = build\SkiaSharp.props
-		build\Splat.props = build\Splat.props
 		build\System.Memory.props = build\System.Memory.props
 		build\XUnit.props = build\XUnit.props
 	EndProjectSection

+ 1 - 0
native/Avalonia.Native/inc/avalonia-native.h

@@ -144,6 +144,7 @@ enum AvnStandardCursorType
     CursorDragMove,
     CursorDragCopy,
     CursorDragLink,
+    CursorNone
 };
 
 enum AvnWindowEdge

+ 8 - 2
native/Avalonia.Native/src/OSX/cursor.h

@@ -11,18 +11,24 @@ class Cursor : public ComSingleObject<IAvnCursor, &IID_IAvnCursor>
 {
 private:
     NSCursor * _native;
-
+    bool _isHidden;
 public:
     FORWARD_IUNKNOWN()
-    Cursor(NSCursor * cursor)
+    Cursor(NSCursor * cursor, bool isHidden = false)
     {
         _native = cursor;
+        _isHidden = isHidden;
     }
 
     NSCursor* GetNative()
     {
         return _native;
     }
+    
+    bool IsHidden ()
+    {
+        return _isHidden;
+    }
 };
 
 extern std::map<AvnStandardCursorType, Cursor*> s_cursorMap;

+ 4 - 1
native/Avalonia.Native/src/OSX/cursor.mm

@@ -21,6 +21,7 @@ class CursorFactory : public ComSingleObject<IAvnCursorFactory, &IID_IAvnCursorF
     Cursor* resizeRightCursor = new Cursor([NSCursor resizeRightCursor]);
     Cursor* resizeWestEastCursor = new Cursor([NSCursor resizeLeftRightCursor]);
     Cursor* operationNotAllowedCursor = new Cursor([NSCursor operationNotAllowedCursor]);
+    Cursor* noCursor = new Cursor([NSCursor arrowCursor], true);
 
     std::map<AvnStandardCursorType, Cursor*> s_cursorMap =
     {
@@ -46,11 +47,13 @@ class CursorFactory : public ComSingleObject<IAvnCursorFactory, &IID_IAvnCursorF
         { CursorIbeam, IBeamCursor },
         { CursorLeftSide, resizeLeftCursor },
         { CursorRightSide, resizeRightCursor },
-        { CursorNo, operationNotAllowedCursor }
+        { CursorNo, operationNotAllowedCursor },
+        { CursorNone, noCursor }
     };
 
 public:
     FORWARD_IUNKNOWN()
+    
     virtual HRESULT GetCursor (AvnStandardCursorType cursorType, IAvnCursor** retOut) override
     {
         *retOut = s_cursorMap[cursorType];

+ 10 - 0
native/Avalonia.Native/src/OSX/window.mm

@@ -353,6 +353,16 @@ public:
             Cursor* avnCursor = dynamic_cast<Cursor*>(cursor);
             this->cursor = avnCursor->GetNative();
             UpdateCursor();
+            
+            if(avnCursor->IsHidden())
+            {
+                [NSCursor hide];
+            }
+            else
+            {
+                [NSCursor unhide];
+            }
+            
             return S_OK;
         }
     }

+ 2 - 2
readme.md

@@ -8,9 +8,9 @@
 
 ## About
 
-Avalonia is a WPF-inspired cross-platform XAML-based UI framework providing a flexible styling system and supporting a wide range of OSs: Windows (.NET Framework, .NET Core), Linux (GTK), MacOS, Android and iOS.
+Avalonia is a WPF/UWP-inspired cross-platform XAML-based UI framework providing a flexible styling system and supporting a wide range of OSs: Windows (.NET Framework, .NET Core), Linux (libX11), MacOS, Android (experimental) and iOS (exprerimental).
 
-**Avalonia is currently in beta** which means that the framework is generally usable for writing applications, but there may be some bugs and breaking changes as we continue development.
+**Avalonia is currently in beta** which means that the framework is generally usable for writing applications, but there may be some bugs and breaking changes as we continue development, for more details about the status see https://github.com/AvaloniaUI/Avalonia/issues/2239
 
 | Control catalog | Desktop platforms | Mobile platforms |
 |---|---|---|

+ 1 - 0
samples/ControlCatalog.NetCore/ControlCatalog.NetCore.csproj

@@ -11,6 +11,7 @@
     <ProjectReference Include="..\ControlCatalog\ControlCatalog.csproj" />
     <ProjectReference Include="..\..\src\Avalonia.Desktop\Avalonia.Desktop.csproj" />
     <ProjectReference Include="..\..\src\Avalonia.X11\Avalonia.X11.csproj" />
+    <PackageReference Include="Avalonia.Angle.Windows.Natives" Version="2.1.0.2019013001"/>
   </ItemGroup>
 
 

+ 5 - 1
samples/ControlCatalog.NetCore/Program.cs

@@ -47,7 +47,11 @@ namespace ControlCatalog.NetCore
             => AppBuilder.Configure<App>()
                 .UsePlatformDetect()
                 .With(new X11PlatformOptions {EnableMultiTouch = true})
-                .With(new Win32PlatformOptions {EnableMultitouch = true})
+                .With(new Win32PlatformOptions
+                {
+                    EnableMultitouch = true,
+                    AllowEglInitialization = true
+                })
                 .UseSkia()
                 .UseReactiveUI();
 

+ 2 - 2
samples/ControlCatalog/Pages/DataGridPage.xaml

@@ -11,7 +11,7 @@
       <Setter Property="Background" Value="{Binding Path=GDP, Mode=OneWay, Converter={StaticResource GDPConverter}}" />
     </Style>
   </UserControl.Styles>
-  <Grid RowDefinitions="Auto,Auto">
+  <Grid RowDefinitions="Auto,*">
     <StackPanel Orientation="Vertical" Spacing="4" Grid.Row="0">
       <TextBlock Classes="h1">DataGrid</TextBlock>
       <TextBlock Classes="h2">A control for displaying and interacting with a data source.</TextBlock>
@@ -52,4 +52,4 @@
       </TabItem>
     </TabControl>
   </Grid>
-</UserControl>
+</UserControl>

+ 4 - 0
samples/ControlCatalog/Pages/TextBoxPage.xaml

@@ -26,6 +26,10 @@
         <TextBox Width="200" Text="Left aligned text" TextAlignment="Left" />
         <TextBox Width="200" Text="Center aligned text" TextAlignment="Center" />
         <TextBox Width="200" Text="Right aligned text" TextAlignment="Right" />
+        <TextBox Width="200" Text="Custom selection brush"
+                  SelectionStart="5" SelectionEnd="22"
+                  SelectionBrush="Green" SelectionForegroundBrush="Yellow"/>
+        <TextBox Width="200" Text="Custom caret brush" CaretBrush="DarkOrange"/>
       </StackPanel>
 
       <StackPanel Orientation="Vertical" Spacing="8">

+ 3 - 0
samples/RenderDemo/MainWindow.xaml

@@ -38,6 +38,9 @@
       <TabItem Header="SkCanvas">
         <pages:CustomSkiaPage/>
       </TabItem>
+      <TabItem Header="RenderTargetBitmap">
+        <pages:RenderTargetBitmapPage/>
+      </TabItem>
     </TabControl>
   </DockPanel>
 </Window>

+ 49 - 0
samples/RenderDemo/Pages/RenderTargetBitmapPage.cs

@@ -0,0 +1,49 @@
+using System.Diagnostics;
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.LogicalTree;
+using Avalonia.Media;
+using Avalonia.Media.Imaging;
+using Avalonia.Threading;
+using Avalonia.Visuals.Media.Imaging;
+
+namespace RenderDemo.Pages
+{
+    public class RenderTargetBitmapPage : Control
+    {
+        private RenderTargetBitmap _bitmap;
+
+        protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
+        {
+            _bitmap = new RenderTargetBitmap(new PixelSize(200, 200), new Vector(96, 96));
+            base.OnAttachedToLogicalTree(e);
+        }
+
+        protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
+        {
+            _bitmap.Dispose();
+            _bitmap = null;
+            base.OnDetachedFromLogicalTree(e);
+        }
+
+        readonly Stopwatch _st = Stopwatch.StartNew();
+        public override void Render(DrawingContext context)
+        {
+            using (var ctxi = _bitmap.CreateDrawingContext(null))
+            using(var ctx = new DrawingContext(ctxi, false))
+            using (ctx.PushPostTransform(Matrix.CreateTranslation(-100, -100)
+                                         * Matrix.CreateRotation(_st.Elapsed.TotalSeconds)
+                                         * Matrix.CreateTranslation(100, 100)))
+            {
+                ctxi.Clear(default);
+                ctx.FillRectangle(Brushes.Fuchsia, new Rect(50, 50, 100, 100));
+            }
+
+            context.DrawImage(_bitmap, 1, 
+                new Rect(0, 0, 200, 200), 
+                new Rect(0, 0, 200, 200));
+            Dispatcher.UIThread.Post(InvalidateVisual, DispatcherPriority.Background);
+            base.Render(context);
+        }
+    }
+}

+ 119 - 0
src/Avalonia.Base/Utilities/MathUtilities.cs

@@ -1,6 +1,9 @@
 // Copyright (c) The Avalonia Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
+using System;
+using System.Runtime.InteropServices;
+
 namespace Avalonia.Utilities
 {
     /// <summary>
@@ -8,6 +11,89 @@ namespace Avalonia.Utilities
     /// </summary>
     public static class MathUtilities
     {
+        /// <summary>
+        /// AreClose - Returns whether or not two doubles are "close".  That is, whether or 
+        /// not they are within epsilon of each other.
+        /// </summary> 
+        /// <param name="value1"> The first double to compare. </param>
+        /// <param name="value2"> The second double to compare. </param>
+        public static bool AreClose(double value1, double value2)
+        {
+            //in case they are Infinities (then epsilon check does not work)
+            if (value1 == value2) return true;
+            double eps = (Math.Abs(value1) + Math.Abs(value2) + 10.0) * double.Epsilon;
+            double delta = value1 - value2;
+            return (-eps < delta) && (eps > delta);
+        }
+
+        /// <summary>
+        /// LessThan - Returns whether or not the first double is less than the second double.
+        /// That is, whether or not the first is strictly less than *and* not within epsilon of
+        /// the other number.
+        /// </summary>
+        /// <param name="value1"> The first double to compare. </param>
+        /// <param name="value2"> The second double to compare. </param>
+        public static bool LessThan(double value1, double value2)
+        {
+            return (value1 < value2) && !AreClose(value1, value2);
+        }
+
+        /// <summary>
+        /// GreaterThan - Returns whether or not the first double is greater than the second double.
+        /// That is, whether or not the first is strictly greater than *and* not within epsilon of
+        /// the other number.
+        /// </summary>
+        /// <param name="value1"> The first double to compare. </param>
+        /// <param name="value2"> The second double to compare. </param>
+        public static bool GreaterThan(double value1, double value2)
+        {
+            return (value1 > value2) && !AreClose(value1, value2);
+        }
+
+        /// <summary>
+        /// LessThanOrClose - Returns whether or not the first double is less than or close to
+        /// the second double.  That is, whether or not the first is strictly less than or within
+        /// epsilon of the other number.
+        /// </summary>
+        /// <param name="value1"> The first double to compare. </param>
+        /// <param name="value2"> The second double to compare. </param>
+        public static bool LessThanOrClose(double value1, double value2)
+        {
+            return (value1 < value2) || AreClose(value1, value2);
+        }
+
+        /// <summary>
+        /// GreaterThanOrClose - Returns whether or not the first double is greater than or close to
+        /// the second double.  That is, whether or not the first is strictly greater than or within
+        /// epsilon of the other number.
+        /// </summary>
+        /// <param name="value1"> The first double to compare. </param>
+        /// <param name="value2"> The second double to compare. </param>
+        public static bool GreaterThanOrClose(double value1, double value2)
+        {
+            return (value1 > value2) || AreClose(value1, value2);
+        }
+
+        /// <summary>
+        /// IsOne - Returns whether or not the double is "close" to 1.  Same as AreClose(double, 1),
+        /// but this is faster.
+        /// </summary>
+        /// <param name="value"> The double to compare to 1. </param>
+        public static bool IsOne(double value)
+        {
+            return Math.Abs(value - 1.0) < 10.0 * double.Epsilon;
+        }
+
+        /// <summary>
+        /// IsZero - Returns whether or not the double is "close" to 0.  Same as AreClose(double, 0),
+        /// but this is faster.
+        /// </summary>
+        /// <param name="value"> The double to compare to 0. </param>
+        public static bool IsZero(double value)
+        {
+            return Math.Abs(value) < 10.0 * double.Epsilon;
+        }
+
         /// <summary>
         /// Clamps a value between a minimum and maximum value.
         /// </summary>
@@ -31,6 +117,39 @@ namespace Avalonia.Utilities
             }
         }
 
+        /// <summary>
+        /// Calculates the value to be used for layout rounding at high DPI.
+        /// </summary>
+        /// <param name="value">Input value to be rounded.</param>
+        /// <param name="dpiScale">Ratio of screen's DPI to layout DPI</param>
+        /// <returns>Adjusted value that will produce layout rounding on screen at high dpi.</returns>
+        /// <remarks>This is a layout helper method. It takes DPI into account and also does not return
+        /// the rounded value if it is unacceptable for layout, e.g. Infinity or NaN. It's a helper associated with
+        /// UseLayoutRounding  property and should not be used as a general rounding utility.</remarks>
+        public static double RoundLayoutValue(double value, double dpiScale)
+        {
+            double newValue;
+
+            // If DPI == 1, don't use DPI-aware rounding.
+            if (!MathUtilities.AreClose(dpiScale, 1.0))
+            {
+                newValue = Math.Round(value * dpiScale) / dpiScale;
+                // If rounding produces a value unacceptable to layout (NaN, Infinity or MaxValue), use the original value.
+                if (double.IsNaN(newValue) ||
+                    double.IsInfinity(newValue) ||
+                    MathUtilities.AreClose(newValue, double.MaxValue))
+                {
+                    newValue = value;
+                }
+            }
+            else
+            {
+                newValue = Math.Round(value);
+            }
+
+            return newValue;
+        }
+
         /// <summary>
         /// Clamps a value between a minimum and maximum value.
         /// </summary>

+ 9 - 2
src/Avalonia.Build.Tasks/Program.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections;
 using System.IO;
+using System.Linq;
 using Microsoft.Build.Framework;
 
 namespace Avalonia.Build.Tasks
@@ -11,8 +12,14 @@ namespace Avalonia.Build.Tasks
         {
             if (args.Length != 3)
             {
-                Console.Error.WriteLine("input references output");
-                return 1;
+                if (args.Length == 1)
+                    args = new[] {"original.dll", "references", "out.dll"}
+                        .Select(x => Path.Combine(args[0], x)).ToArray();
+                else
+                {
+                    Console.Error.WriteLine("input references output");
+                    return 1;
+                }
             }
 
             return new CompileAvaloniaXamlTask()

+ 32 - 0
src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.Helpers.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using Avalonia.Utilities;
 using Mono.Cecil;
 using Mono.Cecil.Cil;
+using Mono.Collections.Generic;
 using XamlIl.TypeSystem;
 
 namespace Avalonia.Build.Tasks
@@ -144,6 +145,37 @@ namespace Avalonia.Build.Tasks
             });
 
         }
+        
+
+        private static bool MatchThisCall(Collection<Instruction> instructions, int idx)
+        {
+            var i = instructions[idx];
+            // A "normal" way of passing `this` to a static method:
+            
+            // ldarg.0
+            // call void [Avalonia.Markup.Xaml]Avalonia.Markup.Xaml.AvaloniaXamlLoader::Load(object)
+            
+            if (i.OpCode == OpCodes.Ldarg_0 || (i.OpCode == OpCodes.Ldarg && i.Operand?.Equals(0) == true))
+                return true;
+
+            /* F# way of using `this` in constructor emits a monstrosity like this:
+                IL_01c7: ldarg.0
+                IL_01c8: ldfld class [FSharp.Core]Microsoft.FSharp.Core.FSharpRef`1<class FVim.Cursor> FVim.Cursor::this
+                IL_01cd: call instance !0 class [FSharp.Core]Microsoft.FSharp.Core.FSharpRef`1<class FVim.Cursor>::get_contents()
+                IL_01d2: call !!0 [FSharp.Core]Microsoft.FSharp.Core.LanguagePrimitives/IntrinsicFunctions::CheckThis<class FVim.Cursor>(!!0)
+                IL_01d7: call void [Avalonia.Markup.Xaml]Avalonia.Markup.Xaml.AvaloniaXamlLoader::Load(object)
+                
+                We check for the previous call to be Microsoft.FSharp.Core.LanguagePrimitives/IntrinsicFunctions::CheckThis
+                since it actually returns `this`
+            */
+            if (i.OpCode == OpCodes.Call
+                && i.Operand is GenericInstanceMethod gim
+                && gim.Name == "CheckThis"
+                && gim.DeclaringType.FullName == "Microsoft.FSharp.Core.LanguagePrimitives/IntrinsicFunctions")
+                return true;
+
+            return false;
+        }
     }
  
 }

+ 6 - 4
src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs

@@ -234,8 +234,7 @@ namespace Avalonia.Build.Tasks
                                 var i = method.Body.Instructions;
                                 for (var c = 1; c < i.Count; c++)
                                 {
-                                    if (i[c - 1].OpCode == OpCodes.Ldarg_0
-                                        && i[c].OpCode == OpCodes.Call)
+                                    if (i[c].OpCode == OpCodes.Call)
                                     {
                                         var op = i[c].Operand as MethodReference;
                                         
@@ -254,8 +253,11 @@ namespace Avalonia.Build.Tasks
                                             && op.Parameters[0].ParameterType.FullName == "System.Object"
                                             && op.DeclaringType.FullName == "Avalonia.Markup.Xaml.AvaloniaXamlLoader")
                                         {
-                                            i[c].Operand = trampoline;
-                                            foundXamlLoader = true;
+                                            if (MatchThisCall(i, c - 1))
+                                            {
+                                                i[c].Operand = trampoline;
+                                                foundXamlLoader = true;
+                                            }
                                         }
                                     }
                                 }

+ 19 - 6
src/Avalonia.Controls.DataGrid/DataGridRow.cs

@@ -881,10 +881,10 @@ namespace Avalonia.Controls
                 && (double.IsNaN(_detailsContent.Height))
                 && (AreDetailsVisible)
                 && (!double.IsNaN(_detailsDesiredHeight))
-                && !DoubleUtil.AreClose(_detailsContent.Bounds.Height, _detailsDesiredHeight)
+                && !DoubleUtil.AreClose(_detailsContent.Bounds.Inflate(_detailsContent.Margin).Height, _detailsDesiredHeight)
                 && Slot != -1)
             {
-                _detailsDesiredHeight = _detailsContent.Bounds.Height;
+                _detailsDesiredHeight = _detailsContent.Bounds.Inflate(_detailsContent.Margin).Height;
 
                 if (true)
                 {
@@ -943,6 +943,16 @@ namespace Avalonia.Controls
                 _previousDetailsHeight = newValue.Height;
             }
         }
+        private void DetailsContent_BoundsChanged(Rect newValue)
+        {
+            if(_detailsContent != null)
+                DetailsContent_SizeChanged(newValue.Inflate(_detailsContent.Margin));
+        }
+        private void DetailsContent_MarginChanged(Thickness newValue)
+        {
+            if (_detailsContent != null)
+                DetailsContent_SizeChanged(_detailsContent.Bounds.Inflate(newValue));
+        }
 
         //TODO Animation
         // Sets AreDetailsVisible on the row and animates if necessary
@@ -997,7 +1007,7 @@ namespace Avalonia.Controls
                 }
             }
         }
-
+        
         internal void ApplyDetailsTemplate(bool initializeDetailsPreferredHeight)
         {
             if (_detailsElement != null && AreDetailsVisible)
@@ -1023,8 +1033,11 @@ namespace Avalonia.Controls
                     if (_detailsContent != null)
                     {
                         _detailsContentSizeSubscription =
-                            _detailsContent.GetObservable(BoundsProperty)
-                                           .Subscribe(DetailsContent_SizeChanged);
+                            System.Reactive.Disposables.StableCompositeDisposable.Create(
+                                _detailsContent.GetObservable(BoundsProperty)
+                                               .Subscribe(DetailsContent_BoundsChanged),
+                                _detailsContent.GetObservable(MarginProperty)
+                                               .Subscribe(DetailsContent_MarginChanged));
                         _detailsElement.Children.Add(_detailsContent);
                     }
                 }
@@ -1053,4 +1066,4 @@ namespace Avalonia.Controls
 
     //TODO Styles
 
-}
+}

+ 5 - 2
src/Avalonia.Controls/Button.cs

@@ -255,7 +255,6 @@ namespace Avalonia.Controls
 
             if (e.MouseButton == MouseButton.Left)
             {
-                e.Device.Capture(this);
                 IsPressed = true;
                 e.Handled = true;
 
@@ -273,7 +272,6 @@ namespace Avalonia.Controls
 
             if (IsPressed && e.MouseButton == MouseButton.Left)
             {
-                e.Device.Capture(null);
                 IsPressed = false;
                 e.Handled = true;
 
@@ -285,6 +283,11 @@ namespace Avalonia.Controls
             }
         }
 
+        protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
+        {
+            IsPressed = false;
+        }
+
         protected override void UpdateDataValidation(AvaloniaProperty property, BindingNotification status)
         {
             base.UpdateDataValidation(property, status);

+ 5 - 5
src/Avalonia.Controls/ColumnDefinition.cs

@@ -55,11 +55,7 @@ namespace Avalonia.Controls
         /// <summary>
         /// Gets the actual calculated width of the column.
         /// </summary>
-        public double ActualWidth
-        {
-            get;
-            internal set;
-        }
+        public double ActualWidth => Parent?.GetFinalColumnDefinitionWidth(Index) ?? 0d;
 
         /// <summary>
         /// Gets or sets the maximum width of the column in DIPs.
@@ -87,5 +83,9 @@ namespace Avalonia.Controls
             get { return GetValue(WidthProperty); }
             set { SetValue(WidthProperty, value); }
         }
+
+        internal override GridLength UserSizeValueCache => this.Width;
+        internal override double UserMinSizeValueCache => this.MinWidth;
+        internal override double UserMaxSizeValueCache => this.MaxWidth;
     }
 }

+ 4 - 3
src/Avalonia.Controls/ColumnDefinitions.cs

@@ -1,6 +1,8 @@
 // Copyright (c) The Avalonia Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
+using System;
+using System.Collections.Specialized;
 using System.Linq;
 using Avalonia.Collections;
 
@@ -9,14 +11,13 @@ namespace Avalonia.Controls
     /// <summary>
     /// A collection of <see cref="ColumnDefinition"/>s.
     /// </summary>
-    public class ColumnDefinitions : AvaloniaList<ColumnDefinition>
+    public class ColumnDefinitions : DefinitionList<ColumnDefinition>
     {
         /// <summary>
         /// Initializes a new instance of the <see cref="ColumnDefinitions"/> class.
         /// </summary>
-        public ColumnDefinitions()
+        public ColumnDefinitions() : base ()
         {
-            ResetBehavior = ResetBehavior.Remove;
         }
 
         /// <summary>

+ 720 - 12
src/Avalonia.Controls/DefinitionBase.cs

@@ -1,26 +1,734 @@
-// Copyright (c) The Avalonia Project. All rights reserved.
-// Licensed under the MIT license. See licence.md file in the project root for full license information.
+// This source file is adapted from the Windows Presentation Foundation project. 
+// (https://github.com/dotnet/wpf/) 
+// 
+// Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation.
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
+
+using Avalonia;
+using Avalonia.Collections;
+using Avalonia.Utilities;
 
 namespace Avalonia.Controls
 {
     /// <summary>
-    /// Base class for <see cref="ColumnDefinition"/> and <see cref="RowDefinition"/>.
+    /// DefinitionBase provides core functionality used internally by Grid
+    /// and ColumnDefinitionCollection / RowDefinitionCollection
     /// </summary>
-    public class DefinitionBase : AvaloniaObject
+    public abstract class DefinitionBase : AvaloniaObject
     {
         /// <summary>
-        /// Defines the <see cref="SharedSizeGroup"/> property.
+        /// SharedSizeGroup property.
         /// </summary>
-        public static readonly StyledProperty<string> SharedSizeGroupProperty =
-            AvaloniaProperty.Register<DefinitionBase, string>(nameof(SharedSizeGroup), inherits: true);
+        public string SharedSizeGroup
+        {
+            get { return (string)GetValue(SharedSizeGroupProperty); }
+            set { SetValue(SharedSizeGroupProperty, value); }
+        }
 
         /// <summary>
-        /// Gets or sets the name of the shared size group of the column or row.
+        /// Callback to notify about entering model tree.
         /// </summary>
-        public string SharedSizeGroup
+        internal void OnEnterParentTree()
         {
-            get { return GetValue(SharedSizeGroupProperty); }
-            set { SetValue(SharedSizeGroupProperty, value); }
+            this.InheritanceParent = Parent;
+            if (_sharedState == null)
+            {
+                //  start with getting SharedSizeGroup value. 
+                //  this property is NOT inhereted which should result in better overall perf.
+                string sharedSizeGroupId = SharedSizeGroup;
+                if (sharedSizeGroupId != null)
+                {
+                    SharedSizeScope privateSharedSizeScope = PrivateSharedSizeScope;
+                    if (privateSharedSizeScope != null)
+                    {
+                        _sharedState = privateSharedSizeScope.EnsureSharedState(sharedSizeGroupId);
+                        _sharedState.AddMember(this);
+                    }
+                }
+            }
+        }
+
+        /// <summary>
+        /// Callback to notify about exitting model tree.
+        /// </summary>
+        internal void OnExitParentTree()
+        {
+            _offset = 0;
+            if (_sharedState != null)
+            {
+                _sharedState.RemoveMember(this);
+                _sharedState = null;
+            }
+        }
+
+        /// <summary>
+        /// Performs action preparing definition to enter layout calculation mode.
+        /// </summary>
+        internal void OnBeforeLayout(Grid grid)
+        {
+            //  reset layout state.
+            _minSize = 0;
+            LayoutWasUpdated = true;
+
+            //  defer verification for shared definitions
+            if (_sharedState != null) { _sharedState.EnsureDeferredValidation(grid); }
+        }
+
+        /// <summary>
+        /// Updates min size.
+        /// </summary>
+        /// <param name="minSize">New size.</param>
+        internal void UpdateMinSize(double minSize)
+        {
+            _minSize = Math.Max(_minSize, minSize);
+        }
+
+        /// <summary>
+        /// Sets min size.
+        /// </summary>
+        /// <param name="minSize">New size.</param>
+        internal void SetMinSize(double minSize)
+        {
+            _minSize = minSize;
+        }
+
+        /// <remarks>
+        /// This method reflects Grid.SharedScopeProperty state by setting / clearing
+        /// dynamic property PrivateSharedSizeScopeProperty. Value of PrivateSharedSizeScopeProperty
+        /// is a collection of SharedSizeState objects for the scope.
+        /// </remarks>
+        internal static void OnIsSharedSizeScopePropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
+        {
+            if ((bool)e.NewValue)
+            {
+                SharedSizeScope sharedStatesCollection = new SharedSizeScope();
+                d.SetValue(PrivateSharedSizeScopeProperty, sharedStatesCollection);
+            }
+            else
+            {
+                d.ClearValue(PrivateSharedSizeScopeProperty);
+            }
+        }
+
+        /// <summary>
+        /// Returns <c>true</c> if this definition is a part of shared group.
+        /// </summary>
+        internal bool IsShared
+        {
+            get { return (_sharedState != null); }
+        }
+
+        /// <summary>
+        /// Internal accessor to user size field.
+        /// </summary>
+        internal GridLength UserSize
+        {
+            get { return (_sharedState != null ? _sharedState.UserSize : UserSizeValueCache); }
+        }
+
+        /// <summary>
+        /// Internal accessor to user min size field.
+        /// </summary>
+        internal double UserMinSize
+        {
+            get { return (UserMinSizeValueCache); }
+        }
+
+        /// <summary>
+        /// Internal accessor to user max size field.
+        /// </summary>
+        internal double UserMaxSize
+        {
+            get { return (UserMaxSizeValueCache); }
+        }
+
+        /// <summary>
+        /// DefinitionBase's index in the parents collection.
+        /// </summary>
+        internal int Index
+        {
+            get
+            {
+                return (_parentIndex);
+            }
+            set
+            {
+                Debug.Assert(value >= -1);
+                _parentIndex = value;
+            }
+        }
+
+        /// <summary>
+        /// Layout-time user size type.
+        /// </summary>
+        internal Grid.LayoutTimeSizeType SizeType
+        {
+            get { return (_sizeType); }
+            set { _sizeType = value; }
+        }
+
+        /// <summary>
+        /// Returns or sets measure size for the definition.
+        /// </summary>
+        internal double MeasureSize
+        {
+            get { return (_measureSize); }
+            set { _measureSize = value; }
+        }
+
+        /// <summary>
+        /// Returns definition's layout time type sensitive preferred size.
+        /// </summary>
+        /// <remarks>
+        /// Returned value is guaranteed to be true preferred size.
+        /// </remarks>
+        internal double PreferredSize
+        {
+            get
+            {
+                double preferredSize = MinSize;
+                if (_sizeType != Grid.LayoutTimeSizeType.Auto
+                    && preferredSize < _measureSize)
+                {
+                    preferredSize = _measureSize;
+                }
+                return (preferredSize);
+            }
+        }
+
+        /// <summary>
+        /// Returns or sets size cache for the definition.
+        /// </summary>
+        internal double SizeCache
+        {
+            get { return (_sizeCache); }
+            set { _sizeCache = value; }
+        }
+
+        /// <summary>
+        /// Returns min size.
+        /// </summary>
+        internal double MinSize
+        {
+            get
+            {
+                double minSize = _minSize;
+                if (UseSharedMinimum
+                    && _sharedState != null
+                    && minSize < _sharedState.MinSize)
+                {
+                    minSize = _sharedState.MinSize;
+                }
+                return (minSize);
+            }
+        }
+
+        /// <summary>
+        /// Returns min size, always taking into account shared state.
+        /// </summary>
+        internal double MinSizeForArrange
+        {
+            get
+            {
+                double minSize = _minSize;
+                if (_sharedState != null
+                    && (UseSharedMinimum || !LayoutWasUpdated)
+                    && minSize < _sharedState.MinSize)
+                {
+                    minSize = _sharedState.MinSize;
+                }
+                return (minSize);
+            }
+        }
+
+        /// <summary>
+        /// Offset.
+        /// </summary>
+        internal double FinalOffset
+        {
+            get { return _offset; }
+            set { _offset = value; }
+        }
+
+        /// <summary>
+        /// Internal helper to access up-to-date UserSize property value.
+        /// </summary>
+        internal abstract GridLength UserSizeValueCache { get; }
+
+        /// <summary>
+        /// Internal helper to access up-to-date UserMinSize property value.
+        /// </summary>
+        internal abstract double UserMinSizeValueCache { get; }
+
+        /// <summary>
+        /// Internal helper to access up-to-date UserMaxSize property value.
+        /// </summary>
+        internal abstract double UserMaxSizeValueCache { get; }
+
+        internal Grid Parent { get; set; }
+
+        /// <summary>
+        /// SetFlags is used to set or unset one or multiple
+        /// flags on the object.
+        /// </summary>
+        private void SetFlags(bool value, Flags flags)
+        {
+            _flags = value ? (_flags | flags) : (_flags & (~flags));
+        }
+
+        /// <summary>
+        /// CheckFlagsAnd returns <c>true</c> if all the flags in the
+        /// given bitmask are set on the object.
+        /// </summary>
+        private bool CheckFlagsAnd(Flags flags)
+        {
+            return ((_flags & flags) == flags);
+        }
+
+        private static void OnSharedSizeGroupPropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
+        {
+            DefinitionBase definition = (DefinitionBase)d;
+
+            if (definition.Parent != null)
+            {
+                string sharedSizeGroupId = (string)e.NewValue;
+
+                if (definition._sharedState != null)
+                {
+                    //  if definition is already registered AND shared size group id is changing,
+                    //  then un-register the definition from the current shared size state object.
+                    definition._sharedState.RemoveMember(definition);
+                    definition._sharedState = null;
+                }
+
+                if ((definition._sharedState == null) && (sharedSizeGroupId != null))
+                {
+                    SharedSizeScope privateSharedSizeScope = definition.PrivateSharedSizeScope;
+                    if (privateSharedSizeScope != null)
+                    {
+                        //  if definition is not registered and both: shared size group id AND private shared scope 
+                        //  are available, then register definition.
+                        definition._sharedState = privateSharedSizeScope.EnsureSharedState(sharedSizeGroupId);
+                        definition._sharedState.AddMember(definition);
+                    }
+                }
+            }
+        }
+
+        /// <remarks>
+        /// Verifies that Shared Size Group Property string
+        /// a) not empty.
+        /// b) contains only letters, digits and underscore ('_').
+        /// c) does not start with a digit.
+        /// </remarks>
+        private static string SharedSizeGroupPropertyValueValid(Control _, string value)
+        {
+            Contract.Requires<ArgumentNullException>(value != null);
+
+            string id = (string)value;
+
+            if (id != string.Empty)
+            {
+                int i = -1;
+                while (++i < id.Length)
+                {
+                    bool isDigit = Char.IsDigit(id[i]);
+
+                    if ((i == 0 && isDigit)
+                        || !(isDigit
+                            || Char.IsLetter(id[i])
+                            || '_' == id[i]))
+                    {
+                        break;
+                    }
+                }
+
+                if (i == id.Length)
+                {
+                    return value;
+                }
+            }
+
+            throw new ArgumentException("Invalid SharedSizeGroup string.");
+        }
+
+        /// <remark>
+        /// OnPrivateSharedSizeScopePropertyChanged is called when new scope enters or
+        /// existing scope just left. In both cases if the DefinitionBase object is already registered
+        /// in SharedSizeState, it should un-register and register itself in a new one.
+        /// </remark>
+        private static void OnPrivateSharedSizeScopePropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
+        {
+            DefinitionBase definition = (DefinitionBase)d;
+
+            if (definition.Parent != null)
+            {
+                SharedSizeScope privateSharedSizeScope = (SharedSizeScope)e.NewValue;
+
+                if (definition._sharedState != null)
+                {
+                    //  if definition is already registered And shared size scope is changing,
+                    //  then un-register the definition from the current shared size state object.
+                    definition._sharedState.RemoveMember(definition);
+                    definition._sharedState = null;
+                }
+
+                if ((definition._sharedState == null) && (privateSharedSizeScope != null))
+                {
+                    string sharedSizeGroup = definition.SharedSizeGroup;
+                    if (sharedSizeGroup != null)
+                    {
+                        //  if definition is not registered and both: shared size group id AND private shared scope 
+                        //  are available, then register definition.
+                        definition._sharedState = privateSharedSizeScope.EnsureSharedState(definition.SharedSizeGroup);
+                        definition._sharedState.AddMember(definition);
+                    }
+                }
+            }
+        }
+
+        /// <summary>
+        /// Private getter of shared state collection dynamic property.
+        /// </summary>
+        private SharedSizeScope PrivateSharedSizeScope
+        {
+            get { return (SharedSizeScope)GetValue(PrivateSharedSizeScopeProperty); }
+        }
+
+        /// <summary>
+        /// Convenience accessor to UseSharedMinimum flag
+        /// </summary>
+        private bool UseSharedMinimum
+        {
+            get { return (CheckFlagsAnd(Flags.UseSharedMinimum)); }
+            set { SetFlags(value, Flags.UseSharedMinimum); }
+        }
+
+        /// <summary>
+        /// Convenience accessor to LayoutWasUpdated flag
+        /// </summary>
+        private bool LayoutWasUpdated
+        {
+            get { return (CheckFlagsAnd(Flags.LayoutWasUpdated)); }
+            set { SetFlags(value, Flags.LayoutWasUpdated); }
+        }
+
+        private Flags _flags;                           //  flags reflecting various aspects of internal state
+        internal int _parentIndex = -1;                  //  this instance's index in parent's children collection
+
+        private Grid.LayoutTimeSizeType _sizeType;      //  layout-time user size type. it may differ from _userSizeValueCache.UnitType when calculating "to-content"
+
+        private double _minSize;                        //  used during measure to accumulate size for "Auto" and "Star" DefinitionBase's
+        private double _measureSize;                    //  size, calculated to be the input contstraint size for Child.Measure
+        private double _sizeCache;                      //  cache used for various purposes (sorting, caching, etc) during calculations
+        private double _offset;                         //  offset of the DefinitionBase from left / top corner (assuming LTR case)
+
+        private SharedSizeState _sharedState;           //  reference to shared state object this instance is registered with
+
+        [System.Flags]
+        private enum Flags : byte
+        {
+            //
+            //  bool flags
+            //
+            UseSharedMinimum = 0x00000020,     //  when "1", definition will take into account shared state's minimum
+            LayoutWasUpdated = 0x00000040,     //  set to "1" every time the parent grid is measured
+        }
+
+        /// <summary>
+        /// Collection of shared states objects for a single scope
+        /// </summary>
+        internal class SharedSizeScope
+        {
+            /// <summary>
+            /// Returns SharedSizeState object for a given group.
+            /// Creates a new StatedState object if necessary.
+            /// </summary>
+            internal SharedSizeState EnsureSharedState(string sharedSizeGroup)
+            {
+                //  check that sharedSizeGroup is not default
+                Debug.Assert(sharedSizeGroup != null);
+
+                SharedSizeState sharedState = _registry[sharedSizeGroup] as SharedSizeState;
+                if (sharedState == null)
+                {
+                    sharedState = new SharedSizeState(this, sharedSizeGroup);
+                    _registry[sharedSizeGroup] = sharedState;
+                }
+                return (sharedState);
+            }
+
+            /// <summary>
+            /// Removes an entry in the registry by the given key.
+            /// </summary>
+            internal void Remove(object key)
+            {
+                Debug.Assert(_registry.Contains(key));
+                _registry.Remove(key);
+            }
+
+            private Hashtable _registry = new Hashtable();  //  storage for shared state objects
+        }
+
+        /// <summary>
+        /// Implementation of per shared group state object
+        /// </summary>
+        internal class SharedSizeState
+        {
+            /// <summary>
+            /// Default ctor.
+            /// </summary>
+            internal SharedSizeState(SharedSizeScope sharedSizeScope, string sharedSizeGroupId)
+            {
+                Debug.Assert(sharedSizeScope != null && sharedSizeGroupId != null);
+                _sharedSizeScope = sharedSizeScope;
+                _sharedSizeGroupId = sharedSizeGroupId;
+                _registry = new List<DefinitionBase>();
+                _layoutUpdated = new EventHandler(OnLayoutUpdated);
+                _broadcastInvalidation = true;
+            }
+
+            /// <summary>
+            /// Adds / registers a definition instance.
+            /// </summary>
+            internal void AddMember(DefinitionBase member)
+            {
+                Debug.Assert(!_registry.Contains(member));
+                _registry.Add(member);
+                Invalidate();
+            }
+
+            /// <summary>
+            /// Removes / un-registers a definition instance.
+            /// </summary>
+            /// <remarks>
+            /// If the collection of registered definitions becomes empty
+            /// instantiates self removal from owner's collection.
+            /// </remarks>
+            internal void RemoveMember(DefinitionBase member)
+            {
+                Invalidate();
+                _registry.Remove(member);
+
+                if (_registry.Count == 0)
+                {
+                    _sharedSizeScope.Remove(_sharedSizeGroupId);
+                }
+            }
+
+            /// <summary>
+            /// Propogates invalidations for all registered definitions.
+            /// Resets its own state.
+            /// </summary>
+            internal void Invalidate()
+            {
+                _userSizeValid = false;
+
+                if (_broadcastInvalidation)
+                {
+                    for (int i = 0, count = _registry.Count; i < count; ++i)
+                    {
+                        Grid parentGrid = (Grid)(_registry[i].Parent);
+                        parentGrid.Invalidate();
+                    }
+                    _broadcastInvalidation = false;
+                }
+            }
+
+            /// <summary>
+            /// Makes sure that one and only one layout updated handler is registered for this shared state.
+            /// </summary>
+            internal void EnsureDeferredValidation(Control layoutUpdatedHost)
+            {
+                if (_layoutUpdatedHost == null)
+                {
+                    _layoutUpdatedHost = layoutUpdatedHost;
+                    _layoutUpdatedHost.LayoutUpdated += _layoutUpdated;
+                }
+            }
+
+            /// <summary>
+            /// DefinitionBase's specific code.
+            /// </summary>
+            internal double MinSize
+            {
+                get
+                {
+                    if (!_userSizeValid) { EnsureUserSizeValid(); }
+                    return (_minSize);
+                }
+            }
+
+            /// <summary>
+            /// DefinitionBase's specific code.
+            /// </summary>
+            internal GridLength UserSize
+            {
+                get
+                {
+                    if (!_userSizeValid) { EnsureUserSizeValid(); }
+                    return (_userSize);
+                }
+            }
+
+            private void EnsureUserSizeValid()
+            {
+                _userSize = new GridLength(1, GridUnitType.Auto);
+
+                for (int i = 0, count = _registry.Count; i < count; ++i)
+                {
+                    Debug.Assert(_userSize.GridUnitType == GridUnitType.Auto
+                                || _userSize.GridUnitType == GridUnitType.Pixel);
+
+                    GridLength currentGridLength = _registry[i].UserSizeValueCache;
+                    if (currentGridLength.GridUnitType == GridUnitType.Pixel)
+                    {
+                        if (_userSize.GridUnitType == GridUnitType.Auto)
+                        {
+                            _userSize = currentGridLength;
+                        }
+                        else if (_userSize.Value < currentGridLength.Value)
+                        {
+                            _userSize = currentGridLength;
+                        }
+                    }
+                }
+                //  taking maximum with user size effectively prevents squishy-ness.
+                //  this is a "solution" to avoid shared definitions from been sized to
+                //  different final size at arrange time, if / when different grids receive
+                //  different final sizes.
+                _minSize = _userSize.IsAbsolute ? _userSize.Value : 0.0;
+
+                _userSizeValid = true;
+            }
+
+            /// <summary>
+            /// OnLayoutUpdated handler. Validates that all participating definitions
+            /// have updated min size value. Forces another layout update cycle if needed.
+            /// </summary>
+            private void OnLayoutUpdated(object sender, EventArgs e)
+            {
+                double sharedMinSize = 0;
+
+                //  accumulate min size of all participating definitions
+                for (int i = 0, count = _registry.Count; i < count; ++i)
+                {
+                    sharedMinSize = Math.Max(sharedMinSize, _registry[i].MinSize);
+                }
+
+                bool sharedMinSizeChanged = !MathUtilities.AreClose(_minSize, sharedMinSize);
+
+                //  compare accumulated min size with min sizes of the individual definitions
+                for (int i = 0, count = _registry.Count; i < count; ++i)
+                {
+                    DefinitionBase definitionBase = _registry[i];
+
+                    if (sharedMinSizeChanged || definitionBase.LayoutWasUpdated)
+                    {
+                        //  if definition's min size is different, then need to re-measure
+                        if (!MathUtilities.AreClose(sharedMinSize, definitionBase.MinSize))
+                        {
+                            Grid parentGrid = (Grid)definitionBase.Parent;
+                            parentGrid.InvalidateMeasure();
+                            definitionBase.UseSharedMinimum = true;
+                        }
+                        else
+                        {
+                            definitionBase.UseSharedMinimum = false;
+
+                            //  if measure is valid then also need to check arrange.
+                            //  Note: definitionBase.SizeCache is volatile but at this point 
+                            //  it contains up-to-date final size
+                            if (!MathUtilities.AreClose(sharedMinSize, definitionBase.SizeCache))
+                            {
+                                Grid parentGrid = (Grid)definitionBase.Parent;
+                                parentGrid.InvalidateArrange();
+                            }
+                        }
+
+                        definitionBase.LayoutWasUpdated = false;
+                    }
+                }
+
+                _minSize = sharedMinSize;
+
+                _layoutUpdatedHost.LayoutUpdated -= _layoutUpdated;
+                _layoutUpdatedHost = null;
+
+                _broadcastInvalidation = true;
+            }
+
+            //  the scope this state belongs to
+            private readonly SharedSizeScope _sharedSizeScope;
+
+            //  Id of the shared size group this object is servicing
+            private readonly string _sharedSizeGroupId;
+
+            //  Registry of participating definitions
+            private readonly List<DefinitionBase> _registry;
+
+            //  Instance event handler for layout updated event
+            private readonly EventHandler _layoutUpdated;
+
+            //  Control for which layout updated event handler is registered
+            private Control _layoutUpdatedHost;
+
+            //  "true" when broadcasting of invalidation is needed
+            private bool _broadcastInvalidation;
+
+            //  "true" when _userSize is up to date        
+            private bool _userSizeValid;
+
+            //  shared state                 
+            private GridLength _userSize;
+
+            //  shared state            
+            private double _minSize;
+        }
+
+        /// <summary>
+        /// Private shared size scope property holds a collection of shared state objects for the a given shared size scope.
+        /// <see cref="OnIsSharedSizeScopePropertyChanged"/>
+        /// </summary>
+        internal static readonly AttachedProperty<SharedSizeScope> PrivateSharedSizeScopeProperty =
+            AvaloniaProperty.RegisterAttached<DefinitionBase, Control, SharedSizeScope>(
+                "PrivateSharedSizeScope",
+                defaultValue: null,
+                inherits: true);
+
+        /// <summary>
+        /// Shared size group property marks column / row definition as belonging to a group "Foo" or "Bar".
+        /// </summary>
+        /// <remarks>
+        /// Value of the Shared Size Group Property must satisfy the following rules:
+        /// <list type="bullet">
+        /// <item><description>
+        /// String must not be empty.
+        /// </description></item>
+        /// <item><description>
+        /// String must consist of letters, digits and underscore ('_') only.
+        /// </description></item>
+        /// <item><description>
+        /// String must not start with a digit.
+        /// </description></item>
+        /// </list>
+        /// </remarks> 
+        public static readonly AttachedProperty<string> SharedSizeGroupProperty =
+            AvaloniaProperty.RegisterAttached<DefinitionBase, Control, string>(
+                "SharedSizeGroup",
+                validate: SharedSizeGroupPropertyValueValid);
+
+        /// <summary>
+        /// Static ctor. Used for static registration of properties.
+        /// </summary>
+        static DefinitionBase()
+        {
+            SharedSizeGroupProperty.Changed.AddClassHandler<DefinitionBase>(OnSharedSizeGroupPropertyChanged);
+            PrivateSharedSizeScopeProperty.Changed.AddClassHandler<DefinitionBase>(OnPrivateSharedSizeScopePropertyChanged);
         }
     }
-}
+}

+ 60 - 0
src/Avalonia.Controls/DefinitionList.cs

@@ -0,0 +1,60 @@
+// Copyright (c) The Avalonia Project. All rights reserved.
+// Licensed under the MIT license. See licence.md file in the project root for full license information.
+
+using System;
+using System.Collections.Specialized;
+using System.Linq;
+using Avalonia.Collections;
+
+namespace Avalonia.Controls
+{
+    public abstract class DefinitionList<T> : AvaloniaList<T> where T : DefinitionBase
+    {
+        public DefinitionList()
+        {
+            ResetBehavior = ResetBehavior.Remove;
+            CollectionChanged += OnCollectionChanged;
+        }
+
+        internal bool IsDirty = true;
+        private Grid _parent;
+
+        internal Grid Parent
+        {
+            get => _parent;
+            set => SetParent(value);
+        }
+
+        private void SetParent(Grid value)
+        {
+            _parent = value;
+
+            foreach (var pair in this.Select((definitions, index) => (definitions, index)))
+            {
+                pair.definitions.Parent = value;
+                pair.definitions.Index = pair.index;
+            }
+        }
+
+        internal void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
+        {
+            foreach (var nI in this.Select((d, i) => (d, i)))
+                nI.d._parentIndex = nI.i;
+
+            foreach (var nD in e.NewItems?.Cast<DefinitionBase>()
+                            ?? Enumerable.Empty<DefinitionBase>())
+            {
+                nD.Parent = this.Parent;
+                nD.OnEnterParentTree();
+            }
+
+            foreach (var oD in e.OldItems?.Cast<DefinitionBase>()
+                            ?? Enumerable.Empty<DefinitionBase>())
+            {
+                oD.OnExitParentTree();
+            }
+
+            IsDirty = true;
+        }
+    }
+}

+ 3127 - 355
src/Avalonia.Controls/Grid.cs

@@ -1,601 +1,3373 @@
-// Copyright (c) The Avalonia Project. All rights reserved.
-// Licensed under the MIT license. See licence.md file in the project root for full license information.
+// This source file is adapted from the Windows Presentation Foundation project. 
+// (https://github.com/dotnet/wpf/) 
+// 
+// Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation.
 
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Diagnostics;
 using System.Linq;
-using System.Reactive.Linq;
-using System.Runtime.CompilerServices;
+using System.Threading;
+using Avalonia;
 using Avalonia.Collections;
-using Avalonia.Controls.Utils;
+using Avalonia.Media;
+using Avalonia.Utilities;
 using Avalonia.VisualTree;
-using JetBrains.Annotations;
 
 namespace Avalonia.Controls
 {
     /// <summary>
-    /// Lays out child controls according to a grid.
+    /// Grid
     /// </summary>
     public class Grid : Panel
     {
+        static Grid()
+        {
+            IsSharedSizeScopeProperty.Changed.AddClassHandler<Control>(DefinitionBase.OnIsSharedSizeScopePropertyChanged);
+            ShowGridLinesProperty.Changed.AddClassHandler<Grid>(OnShowGridLinesPropertyChanged);
+
+            ColumnProperty.Changed.AddClassHandler<Grid>(OnCellAttachedPropertyChanged);
+            ColumnSpanProperty.Changed.AddClassHandler<Grid>(OnCellAttachedPropertyChanged);
+            RowProperty.Changed.AddClassHandler<Grid>(OnCellAttachedPropertyChanged);
+            RowSpanProperty.Changed.AddClassHandler<Grid>(OnCellAttachedPropertyChanged);
+            AffectsParentMeasure<Grid>(ColumnProperty, ColumnSpanProperty, RowProperty, RowSpanProperty);
+        }
+
+        /// <summary>
+        /// Default constructor.
+        /// </summary>
+        public Grid()
+        {
+        }
+
+        /// <summary>
+        /// Helper for setting Column property on a Control.
+        /// </summary>
+        /// <param name="element">Control to set Column property on.</param>
+        /// <param name="value">Column property value.</param>
+        public static void SetColumn(Control element, int value)
+        {
+            Contract.Requires<ArgumentNullException>(element != null);
+            element.SetValue(ColumnProperty, value);
+        }
+
+        /// <summary>
+        /// Helper for reading Column property from a Control.
+        /// </summary>
+        /// <param name="element">Control to read Column property from.</param>
+        /// <returns>Column property value.</returns>
+        public static int GetColumn(Control element)
+        {
+            Contract.Requires<ArgumentNullException>(element != null);
+            return element.GetValue(ColumnProperty);
+        }
+
+        /// <summary>
+        /// Helper for setting Row property on a Control.
+        /// </summary>
+        /// <param name="element">Control to set Row property on.</param>
+        /// <param name="value">Row property value.</param>
+        public static void SetRow(Control element, int value)
+        {
+            Contract.Requires<ArgumentNullException>(element != null);
+            element.SetValue(RowProperty, value);
+        }
+
+        /// <summary>
+        /// Helper for reading Row property from a Control.
+        /// </summary>
+        /// <param name="element">Control to read Row property from.</param>
+        /// <returns>Row property value.</returns>
+        public static int GetRow(Control element)
+        {
+            Contract.Requires<ArgumentNullException>(element != null);
+            return element.GetValue(RowProperty);
+        }
+
+        /// <summary>
+        /// Helper for setting ColumnSpan property on a Control.
+        /// </summary>
+        /// <param name="element">Control to set ColumnSpan property on.</param>
+        /// <param name="value">ColumnSpan property value.</param>
+        public static void SetColumnSpan(Control element, int value)
+        {
+            Contract.Requires<ArgumentNullException>(element != null);
+            element.SetValue(ColumnSpanProperty, value);
+        }
+
+        /// <summary>
+        /// Helper for reading ColumnSpan property from a Control.
+        /// </summary>
+        /// <param name="element">Control to read ColumnSpan property from.</param>
+        /// <returns>ColumnSpan property value.</returns>
+        public static int GetColumnSpan(Control element)
+        {
+            Contract.Requires<ArgumentNullException>(element != null);
+            return element.GetValue(ColumnSpanProperty);
+        }
+
+        /// <summary>
+        /// Helper for setting RowSpan property on a Control.
+        /// </summary>
+        /// <param name="element">Control to set RowSpan property on.</param>
+        /// <param name="value">RowSpan property value.</param>
+        public static void SetRowSpan(Control element, int value)
+        {
+            Contract.Requires<ArgumentNullException>(element != null);
+            element.SetValue(RowSpanProperty, value);
+        }
+
+        /// <summary>
+        /// Helper for reading RowSpan property from a Control.
+        /// </summary>
+        /// <param name="element">Control to read RowSpan property from.</param>
+        /// <returns>RowSpan property value.</returns>
+        public static int GetRowSpan(Control element)
+        {
+            Contract.Requires<ArgumentNullException>(element != null);
+            return element.GetValue(RowSpanProperty);
+        }
+
+        /// <summary>
+        /// Helper for setting IsSharedSizeScope property on a Control.
+        /// </summary>
+        /// <param name="element">Control to set IsSharedSizeScope property on.</param>
+        /// <param name="value">IsSharedSizeScope property value.</param>
+        public static void SetIsSharedSizeScope(Control element, bool value)
+        {
+            Contract.Requires<ArgumentNullException>(element != null);
+            element.SetValue(IsSharedSizeScopeProperty, value);
+        }
+
+        /// <summary>
+        /// Helper for reading IsSharedSizeScope property from a Control.
+        /// </summary>
+        /// <param name="element">Control to read IsSharedSizeScope property from.</param>
+        /// <returns>IsSharedSizeScope property value.</returns>
+        public static bool GetIsSharedSizeScope(Control element)
+        {
+            Contract.Requires<ArgumentNullException>(element != null);
+            return element.GetValue(IsSharedSizeScopeProperty);
+        }
+
+        /// <summary>
+        /// ShowGridLines property.
+        /// </summary>
+        public bool ShowGridLines
+        {
+            get { return GetValue(ShowGridLinesProperty); }
+            set { SetValue(ShowGridLinesProperty, value); }
+        }
+
+        /// <summary>
+        /// Returns a ColumnDefinitions of column definitions.
+        /// </summary>
+        public ColumnDefinitions ColumnDefinitions
+        {
+            get
+            {
+                if (_data == null) { _data = new ExtendedData(); }
+                if (_data.ColumnDefinitions == null) { _data.ColumnDefinitions = new ColumnDefinitions() { Parent = this }; }
+
+                return (_data.ColumnDefinitions);
+            }
+            set
+            {
+                if (_data == null) { _data = new ExtendedData(); }
+                _data.ColumnDefinitions = value;
+                _data.ColumnDefinitions.Parent = this;
+            }
+        }
+
+        /// <summary>
+        /// Returns a RowDefinitions of row definitions.
+        /// </summary>
+        public RowDefinitions RowDefinitions
+        {
+            get
+            {
+                if (_data == null) { _data = new ExtendedData(); }
+                if (_data.RowDefinitions == null) { _data.RowDefinitions = new RowDefinitions() { Parent = this }; }
+
+                return (_data.RowDefinitions);
+            }
+            set
+            {
+                if (_data == null) { _data = new ExtendedData(); }
+                _data.RowDefinitions = value;
+                _data.RowDefinitions.Parent = this;
+            }
+        }
+
+        /// <summary>
+        /// Content measurement.
+        /// </summary>
+        /// <param name="constraint">Constraint</param>
+        /// <returns>Desired size</returns>
+        protected override Size MeasureOverride(Size constraint)
+        {
+            Size gridDesiredSize;
+            ExtendedData extData = ExtData;
+
+            try
+            {
+                ListenToNotifications = true;
+                MeasureOverrideInProgress = true;
+
+                if (extData == null)
+                {
+                    gridDesiredSize = new Size();
+                    var children = this.Children;
+
+                    for (int i = 0, count = children.Count; i < count; ++i)
+                    {
+                        var child = children[i];
+                        if (child != null)
+                        {
+                            child.Measure(constraint);
+                            gridDesiredSize = new Size(Math.Max(gridDesiredSize.Width, child.DesiredSize.Width),
+                                                       Math.Max(gridDesiredSize.Height, child.DesiredSize.Height));
+                        }
+                    }
+                }
+                else
+                {
+                    {
+                        bool sizeToContentU = double.IsPositiveInfinity(constraint.Width);
+                        bool sizeToContentV = double.IsPositiveInfinity(constraint.Height);
+
+                        // Clear index information and rounding errors
+                        if (RowDefinitionsDirty || ColumnDefinitionsDirty)
+                        {
+                            if (_definitionIndices != null)
+                            {
+                                Array.Clear(_definitionIndices, 0, _definitionIndices.Length);
+                                _definitionIndices = null;
+                            }
+
+                            if (UseLayoutRounding)
+                            {
+                                if (_roundingErrors != null)
+                                {
+                                    Array.Clear(_roundingErrors, 0, _roundingErrors.Length);
+                                    _roundingErrors = null;
+                                }
+                            }
+                        }
+
+                        ValidateDefinitionsUStructure();
+                        ValidateDefinitionsLayout(DefinitionsU, sizeToContentU);
+
+                        ValidateDefinitionsVStructure();
+                        ValidateDefinitionsLayout(DefinitionsV, sizeToContentV);
+
+                        CellsStructureDirty |= (SizeToContentU != sizeToContentU) || (SizeToContentV != sizeToContentV);
+
+                        SizeToContentU = sizeToContentU;
+                        SizeToContentV = sizeToContentV;
+                    }
+
+                    ValidateCells();
+
+                    Debug.Assert(DefinitionsU.Count > 0 && DefinitionsV.Count > 0);
+
+                    //  Grid classifies cells into four groups depending on
+                    //  the column / row type a cell belongs to (number corresponds to
+                    //  group number):
+                    //
+                    //                   Px      Auto     Star
+                    //               +--------+--------+--------+
+                    //               |        |        |        |
+                    //            Px |    1   |    1   |    3   |
+                    //               |        |        |        |
+                    //               +--------+--------+--------+
+                    //               |        |        |        |
+                    //          Auto |    1   |    1   |    3   |
+                    //               |        |        |        |
+                    //               +--------+--------+--------+
+                    //               |        |        |        |
+                    //          Star |    4   |    2   |    4   |
+                    //               |        |        |        |
+                    //               +--------+--------+--------+
+                    //
+                    //  The group number indicates the order in which cells are measured.
+                    //  Certain order is necessary to be able to dynamically resolve star
+                    //  columns / rows sizes which are used as input for measuring of
+                    //  the cells belonging to them.
+                    //
+                    //  However, there are cases when topology of a grid causes cyclical
+                    //  size dependences. For example:
+                    //
+                    //
+                    //                         column width="Auto"      column width="*"
+                    //                      +----------------------+----------------------+
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //  row height="Auto"   |                      |      cell 1 2        |
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //                      +----------------------+----------------------+
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //  row height="*"      |       cell 2 1       |                      |
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //                      |                      |                      |
+                    //                      +----------------------+----------------------+
+                    //
+                    //  In order to accurately calculate constraint width for "cell 1 2"
+                    //  (which is the remaining of grid's available width and calculated
+                    //  value of Auto column), "cell 2 1" needs to be calculated first,
+                    //  as it contributes to the Auto column's calculated value.
+                    //  At the same time in order to accurately calculate constraint
+                    //  height for "cell 2 1", "cell 1 2" needs to be calcualted first,
+                    //  as it contributes to Auto row height, which is used in the
+                    //  computation of Star row resolved height.
+                    //
+                    //  to "break" this cyclical dependency we are making (arbitrary)
+                    //  decision to treat cells like "cell 2 1" as if they appear in Auto
+                    //  rows. And then recalculate them one more time when star row
+                    //  heights are resolved.
+                    //
+                    //  (Or more strictly) the code below implement the following logic:
+                    //
+                    //                       +---------+
+                    //                       |  enter  |
+                    //                       +---------+
+                    //                            |
+                    //                            V
+                    //                    +----------------+
+                    //                    | Measure Group1 |
+                    //                    +----------------+
+                    //                            |
+                    //                            V
+                    //                          / - \
+                    //                        /       \
+                    //                  Y   /    Can    \    N
+                    //            +--------|   Resolve   |-----------+
+                    //            |         \  StarsV?  /            |
+                    //            |           \       /              |
+                    //            |             \ - /                |
+                    //            V                                  V
+                    //    +----------------+                       / - \
+                    //    | Resolve StarsV |                     /       \
+                    //    +----------------+               Y   /    Can    \    N
+                    //            |                      +----|   Resolve   |------+
+                    //            V                      |     \  StarsU?  /       |
+                    //    +----------------+             |       \       /         |
+                    //    | Measure Group2 |             |         \ - /           |
+                    //    +----------------+             |                         V
+                    //            |                      |                 +-----------------+
+                    //            V                      |                 | Measure Group2' |
+                    //    +----------------+             |                 +-----------------+
+                    //    | Resolve StarsU |             |                         |
+                    //    +----------------+             V                         V
+                    //            |              +----------------+        +----------------+
+                    //            V              | Resolve StarsU |        | Resolve StarsU |
+                    //    +----------------+     +----------------+        +----------------+
+                    //    | Measure Group3 |             |                         |
+                    //    +----------------+             V                         V
+                    //            |              +----------------+        +----------------+
+                    //            |              | Measure Group3 |        | Measure Group3 |
+                    //            |              +----------------+        +----------------+
+                    //            |                      |                         |
+                    //            |                      V                         V
+                    //            |              +----------------+        +----------------+
+                    //            |              | Resolve StarsV |        | Resolve StarsV |
+                    //            |              +----------------+        +----------------+
+                    //            |                      |                         |
+                    //            |                      |                         V
+                    //            |                      |                +------------------+
+                    //            |                      |                | Measure Group2'' |
+                    //            |                      |                +------------------+
+                    //            |                      |                         |
+                    //            +----------------------+-------------------------+
+                    //                                   |
+                    //                                   V
+                    //                           +----------------+
+                    //                           | Measure Group4 |
+                    //                           +----------------+
+                    //                                   |
+                    //                                   V
+                    //                               +--------+
+                    //                               |  exit  |
+                    //                               +--------+
+                    //
+                    //  where:
+                    //  *   all [Measure GroupN] - regular children measure process -
+                    //      each cell is measured given contraint size as an input
+                    //      and each cell's desired size is accumulated on the
+                    //      corresponding column / row;
+                    //  *   [Measure Group2'] - is when each cell is measured with
+                    //      infinit height as a constraint and a cell's desired
+                    //      height is ignored;
+                    //  *   [Measure Groups''] - is when each cell is measured (second
+                    //      time during single Grid.MeasureOverride) regularly but its
+                    //      returned width is ignored;
+                    //
+                    //  This algorithm is believed to be as close to ideal as possible.
+                    //  It has the following drawbacks:
+                    //  *   cells belonging to Group2 can be called to measure twice;
+                    //  *   iff during second measure a cell belonging to Group2 returns
+                    //      desired width greater than desired width returned the first
+                    //      time, such a cell is going to be clipped, even though it
+                    //      appears in Auto column.
+                    //
+
+                    MeasureCellsGroup(extData.CellGroup1, constraint, false, false);
+
+                    {
+                        //  after Group1 is measured,  only Group3 may have cells belonging to Auto rows.
+                        bool canResolveStarsV = !HasGroup3CellsInAutoRows;
+
+                        if (canResolveStarsV)
+                        {
+                            if (HasStarCellsV) { ResolveStar(DefinitionsV, constraint.Height); }
+                            MeasureCellsGroup(extData.CellGroup2, constraint, false, false);
+                            if (HasStarCellsU) { ResolveStar(DefinitionsU, constraint.Width); }
+                            MeasureCellsGroup(extData.CellGroup3, constraint, false, false);
+                        }
+                        else
+                        {
+                            //  if at least one cell exists in Group2, it must be measured before
+                            //  StarsU can be resolved.
+                            bool canResolveStarsU = extData.CellGroup2 > PrivateCells.Length;
+                            if (canResolveStarsU)
+                            {
+                                if (HasStarCellsU) { ResolveStar(DefinitionsU, constraint.Width); }
+                                MeasureCellsGroup(extData.CellGroup3, constraint, false, false);
+                                if (HasStarCellsV) { ResolveStar(DefinitionsV, constraint.Height); }
+                            }
+                            else
+                            {
+                                // This is a revision to the algorithm employed for the cyclic
+                                // dependency case described above. We now repeatedly
+                                // measure Group3 and Group2 until their sizes settle. We
+                                // also use a count heuristic to break a loop in case of one.
+
+                                bool hasDesiredSizeUChanged = false;
+                                int cnt = 0;
+
+                                // Cache Group2MinWidths & Group3MinHeights
+                                double[] group2MinSizes = CacheMinSizes(extData.CellGroup2, false);
+                                double[] group3MinSizes = CacheMinSizes(extData.CellGroup3, true);
+
+                                MeasureCellsGroup(extData.CellGroup2, constraint, false, true);
+
+                                do
+                                {
+                                    if (hasDesiredSizeUChanged)
+                                    {
+                                        // Reset cached Group3Heights
+                                        ApplyCachedMinSizes(group3MinSizes, true);
+                                    }
+
+                                    if (HasStarCellsU) { ResolveStar(DefinitionsU, constraint.Width); }
+                                    MeasureCellsGroup(extData.CellGroup3, constraint, false, false);
+
+                                    // Reset cached Group2Widths
+                                    ApplyCachedMinSizes(group2MinSizes, false);
+
+                                    if (HasStarCellsV) { ResolveStar(DefinitionsV, constraint.Height); }
+                                    MeasureCellsGroup(extData.CellGroup2, constraint, cnt == c_layoutLoopMaxCount, false, out hasDesiredSizeUChanged);
+                                }
+                                while (hasDesiredSizeUChanged && ++cnt <= c_layoutLoopMaxCount);
+                            }
+                        }
+                    }
+
+                    MeasureCellsGroup(extData.CellGroup4, constraint, false, false);
+
+                    gridDesiredSize = new Size(
+                            CalculateDesiredSize(DefinitionsU),
+                            CalculateDesiredSize(DefinitionsV));
+                }
+            }
+            finally
+            {
+                MeasureOverrideInProgress = false;
+            }
+
+            return (gridDesiredSize);
+        }
+
+        /// <summary>
+        /// Content arrangement.
+        /// </summary>
+        /// <param name="arrangeSize">Arrange size</param>
+        protected override Size ArrangeOverride(Size arrangeSize)
+        {
+            try
+            {
+                ArrangeOverrideInProgress = true;
+
+                if (_data == null)
+                {
+                    var children = this.Children;
+
+                    for (int i = 0, count = children.Count; i < count; ++i)
+                    {
+                        var child = children[i];
+                        if (child != null)
+                        {
+                            child.Arrange(new Rect(arrangeSize));
+                        }
+                    }
+                }
+                else
+                {
+                    Debug.Assert(DefinitionsU.Count > 0 && DefinitionsV.Count > 0);
+
+                    SetFinalSize(DefinitionsU, arrangeSize.Width, true);
+                    SetFinalSize(DefinitionsV, arrangeSize.Height, false);
+
+                    var children = this.Children;
+
+                    for (int currentCell = 0; currentCell < PrivateCells.Length; ++currentCell)
+                    {
+                        var cell = children[currentCell];
+                        if (cell == null)
+                        {
+                            continue;
+                        }
+
+                        int columnIndex = PrivateCells[currentCell].ColumnIndex;
+                        int rowIndex = PrivateCells[currentCell].RowIndex;
+                        int columnSpan = PrivateCells[currentCell].ColumnSpan;
+                        int rowSpan = PrivateCells[currentCell].RowSpan;
+
+                        Rect cellRect = new Rect(
+                            columnIndex == 0 ? 0.0 : DefinitionsU[columnIndex].FinalOffset,
+                            rowIndex == 0 ? 0.0 : DefinitionsV[rowIndex].FinalOffset,
+                            GetFinalSizeForRange(DefinitionsU, columnIndex, columnSpan),
+                            GetFinalSizeForRange(DefinitionsV, rowIndex, rowSpan));
+
+
+                        cell.Arrange(cellRect);
+
+                    }
+
+                    //  update render bound on grid lines renderer visual
+                    var gridLinesRenderer = EnsureGridLinesRenderer();
+                    gridLinesRenderer?.UpdateRenderBounds(arrangeSize);
+                }
+            }
+            finally
+            {
+                SetValid();
+                ArrangeOverrideInProgress = false;
+            }
+            return (arrangeSize);
+        }
+
+        /// <summary>
+        /// Invalidates grid caches and makes the grid dirty for measure.
+        /// </summary>
+        internal void Invalidate()
+        {
+            CellsStructureDirty = true;
+            InvalidateMeasure();
+        }
+
+        /// <summary>
+        /// Returns final width for a column.
+        /// </summary>
+        /// <remarks>
+        /// Used from public ColumnDefinition ActualWidth. Calculates final width using offset data.
+        /// </remarks>
+        internal double GetFinalColumnDefinitionWidth(int columnIndex)
+        {
+            double value = 0.0;
+
+            Contract.Requires<NullReferenceException>(_data != null);
+
+            //  actual value calculations require structure to be up-to-date
+            if (!ColumnDefinitionsDirty)
+            {
+                IReadOnlyList<DefinitionBase> definitions = DefinitionsU;
+                value = definitions[(columnIndex + 1) % definitions.Count].FinalOffset;
+                if (columnIndex != 0) { value -= definitions[columnIndex].FinalOffset; }
+            }
+            return (value);
+        }
+
+        /// <summary>
+        /// Returns final height for a row.
+        /// </summary>
+        /// <remarks>
+        /// Used from public RowDefinition ActualHeight. Calculates final height using offset data.
+        /// </remarks>
+        internal double GetFinalRowDefinitionHeight(int rowIndex)
+        {
+            double value = 0.0;
+
+            Contract.Requires<NullReferenceException>(_data != null);
+
+            //  actual value calculations require structure to be up-to-date
+            if (!RowDefinitionsDirty)
+            {
+                IReadOnlyList<DefinitionBase> definitions = DefinitionsV;
+                value = definitions[(rowIndex + 1) % definitions.Count].FinalOffset;
+                if (rowIndex != 0) { value -= definitions[rowIndex].FinalOffset; }
+            }
+            return (value);
+        }
+
+        /// <summary>
+        /// Convenience accessor to MeasureOverrideInProgress bit flag.
+        /// </summary>
+        internal bool MeasureOverrideInProgress
+        {
+            get { return (CheckFlagsAnd(Flags.MeasureOverrideInProgress)); }
+            set { SetFlags(value, Flags.MeasureOverrideInProgress); }
+        }
+
+        /// <summary>
+        /// Convenience accessor to ArrangeOverrideInProgress bit flag.
+        /// </summary>
+        internal bool ArrangeOverrideInProgress
+        {
+            get { return (CheckFlagsAnd(Flags.ArrangeOverrideInProgress)); }
+            set { SetFlags(value, Flags.ArrangeOverrideInProgress); }
+        }
+
+        /// <summary>
+        /// Convenience accessor to ValidDefinitionsUStructure bit flag.
+        /// </summary> 
+        internal bool ColumnDefinitionsDirty
+        {
+            get => ColumnDefinitions?.IsDirty ?? false;
+            set => ColumnDefinitions.IsDirty = value;
+        }
+
+        /// <summary>
+        /// Convenience accessor to ValidDefinitionsVStructure bit flag.
+        /// </summary>
+        internal bool RowDefinitionsDirty
+        {
+            get => RowDefinitions?.IsDirty ?? false;
+            set => RowDefinitions.IsDirty = value;
+        }
+
+        /// <summary>
+        /// Lays out cells according to rows and columns, and creates lookup grids.
+        /// </summary>
+        private void ValidateCells()
+        {
+            if (CellsStructureDirty)
+            {
+                ValidateCellsCore();
+                CellsStructureDirty = false;
+            }
+        }
+
+        /// <summary>
+        /// ValidateCellsCore
+        /// </summary>
+        private void ValidateCellsCore()
+        {
+            var children = this.Children;
+            ExtendedData extData = ExtData;
+
+            extData.CellCachesCollection = new CellCache[children.Count];
+            extData.CellGroup1 = int.MaxValue;
+            extData.CellGroup2 = int.MaxValue;
+            extData.CellGroup3 = int.MaxValue;
+            extData.CellGroup4 = int.MaxValue;
+
+            bool hasStarCellsU = false;
+            bool hasStarCellsV = false;
+            bool hasGroup3CellsInAutoRows = false;
+
+            for (int i = PrivateCells.Length - 1; i >= 0; --i)
+            {
+                var child = children[i];
+                if (child == null)
+                {
+                    continue;
+                }
+
+                CellCache cell = new CellCache();
+
+
+                //  Read indices from the corresponding properties:
+                //      clamp to value < number_of_columns
+                //      column >= 0 is guaranteed by property value validation callback
+                cell.ColumnIndex = Math.Min(GetColumn((Control)child), DefinitionsU.Count - 1);
+                //      clamp to value < number_of_rows
+                //      row >= 0 is guaranteed by property value validation callback
+                cell.RowIndex = Math.Min(GetRow((Control)child), DefinitionsV.Count - 1);
+
+                //  Read span properties:
+                //      clamp to not exceed beyond right side of the grid
+                //      column_span > 0 is guaranteed by property value validation callback
+                cell.ColumnSpan = Math.Min(GetColumnSpan((Control)child), DefinitionsU.Count - cell.ColumnIndex);
+
+                //      clamp to not exceed beyond bottom side of the grid
+                //      row_span > 0 is guaranteed by property value validation callback
+                cell.RowSpan = Math.Min(GetRowSpan((Control)child), DefinitionsV.Count - cell.RowIndex);
+
+                Debug.Assert(0 <= cell.ColumnIndex && cell.ColumnIndex < DefinitionsU.Count);
+                Debug.Assert(0 <= cell.RowIndex && cell.RowIndex < DefinitionsV.Count);
+
+                //  Calculate and cache length types for the child.
+
+                cell.SizeTypeU = GetLengthTypeForRange(DefinitionsU, cell.ColumnIndex, cell.ColumnSpan);
+                cell.SizeTypeV = GetLengthTypeForRange(DefinitionsV, cell.RowIndex, cell.RowSpan);
+
+                hasStarCellsU |= cell.IsStarU;
+                hasStarCellsV |= cell.IsStarV;
+
+                //  Distribute cells into four groups.
+
+                if (!cell.IsStarV)
+                {
+                    if (!cell.IsStarU)
+                    {
+                        cell.Next = extData.CellGroup1;
+                        extData.CellGroup1 = i;
+                    }
+                    else
+                    {
+                        cell.Next = extData.CellGroup3;
+                        extData.CellGroup3 = i;
+
+                        //  Remember if this cell belongs to auto row
+                        hasGroup3CellsInAutoRows |= cell.IsAutoV;
+                    }
+                }
+                else
+                {
+                    if (cell.IsAutoU
+                        //  Note below: if spans through Star column it is NOT Auto
+                        && !cell.IsStarU)
+                    {
+                        cell.Next = extData.CellGroup2;
+                        extData.CellGroup2 = i;
+                    }
+                    else
+                    {
+                        cell.Next = extData.CellGroup4;
+                        extData.CellGroup4 = i;
+                    }
+                }
+
+                PrivateCells[i] = cell;
+            }
+
+            HasStarCellsU = hasStarCellsU;
+            HasStarCellsV = hasStarCellsV;
+            HasGroup3CellsInAutoRows = hasGroup3CellsInAutoRows;
+        }
+
+        /// <summary>
+        /// Initializes DefinitionsU memeber either to user supplied ColumnDefinitions collection
+        /// or to a default single element collection. DefinitionsU gets trimmed to size.
+        /// </summary>
+        /// <remarks>
+        /// This is one of two methods, where ColumnDefinitions and DefinitionsU are directly accessed.
+        /// All the rest measure / arrange / render code must use DefinitionsU.
+        /// </remarks>
+        private void ValidateDefinitionsUStructure()
+        {
+            if (ColumnDefinitionsDirty)
+            {
+                ExtendedData extData = ExtData;
+
+                if (extData.ColumnDefinitions == null)
+                {
+                    if (extData.DefinitionsU == null)
+                    {
+                        extData.DefinitionsU = new DefinitionBase[] { new ColumnDefinition() { Parent = this } };
+                    }
+                }
+                else
+                {
+                    if (extData.ColumnDefinitions.Count == 0)
+                    {
+                        //  if column definitions collection is empty
+                        //  mockup array with one column
+                        extData.DefinitionsU = new DefinitionBase[] { new ColumnDefinition() { Parent = this } };
+                    }
+                    else
+                    {
+                        extData.DefinitionsU = extData.ColumnDefinitions;
+                    }
+                }
+
+                ColumnDefinitionsDirty = false;
+            }
+
+            Debug.Assert(ExtData.DefinitionsU != null && ExtData.DefinitionsU.Count > 0);
+        }
+
+        /// <summary>
+        /// Initializes DefinitionsV memeber either to user supplied RowDefinitions collection
+        /// or to a default single element collection. DefinitionsV gets trimmed to size.
+        /// </summary>
+        /// <remarks>
+        /// This is one of two methods, where RowDefinitions and DefinitionsV are directly accessed.
+        /// All the rest measure / arrange / render code must use DefinitionsV.
+        /// </remarks>
+        private void ValidateDefinitionsVStructure()
+        {
+            if (RowDefinitionsDirty)
+            {
+                ExtendedData extData = ExtData;
+
+                if (extData.RowDefinitions == null)
+                {
+                    if (extData.DefinitionsV == null)
+                    {
+                        extData.DefinitionsV = new DefinitionBase[] { new RowDefinition() { Parent = this } };
+                    }
+                }
+                else
+                {
+                    if (extData.RowDefinitions.Count == 0)
+                    {
+                        //  if row definitions collection is empty
+                        //  mockup array with one row
+                        extData.DefinitionsV = new DefinitionBase[] { new RowDefinition() { Parent = this } };
+                    }
+                    else
+                    {
+                        extData.DefinitionsV = extData.RowDefinitions;
+                    }
+                }
+
+                RowDefinitionsDirty = false;
+            }
+
+            Debug.Assert(ExtData.DefinitionsV != null && ExtData.DefinitionsV.Count > 0);
+        }
+
+        /// <summary>
+        /// Validates layout time size type information on given array of definitions.
+        /// Sets MinSize and MeasureSizes.
+        /// </summary>
+        /// <param name="definitions">Array of definitions to update.</param>
+        /// <param name="treatStarAsAuto">if "true" then star definitions are treated as Auto.</param>
+        private void ValidateDefinitionsLayout(
+            IReadOnlyList<DefinitionBase> definitions,
+            bool treatStarAsAuto)
+        {
+            for (int i = 0; i < definitions.Count; ++i)
+            {
+                definitions[i].OnBeforeLayout(this);
+
+                double userMinSize = definitions[i].UserMinSize;
+                double userMaxSize = definitions[i].UserMaxSize;
+                double userSize = 0;
+
+                switch (definitions[i].UserSize.GridUnitType)
+                {
+                    case (GridUnitType.Pixel):
+                        definitions[i].SizeType = LayoutTimeSizeType.Pixel;
+                        userSize = definitions[i].UserSize.Value;
+                        // this was brought with NewLayout and defeats squishy behavior
+                        userMinSize = Math.Max(userMinSize, Math.Min(userSize, userMaxSize));
+                        break;
+                    case (GridUnitType.Auto):
+                        definitions[i].SizeType = LayoutTimeSizeType.Auto;
+                        userSize = double.PositiveInfinity;
+                        break;
+                    case (GridUnitType.Star):
+                        if (treatStarAsAuto)
+                        {
+                            definitions[i].SizeType = LayoutTimeSizeType.Auto;
+                            userSize = double.PositiveInfinity;
+                        }
+                        else
+                        {
+                            definitions[i].SizeType = LayoutTimeSizeType.Star;
+                            userSize = double.PositiveInfinity;
+                        }
+                        break;
+                    default:
+                        Debug.Assert(false);
+                        break;
+                }
+
+                definitions[i].UpdateMinSize(userMinSize);
+                definitions[i].MeasureSize = Math.Max(userMinSize, Math.Min(userSize, userMaxSize));
+            }
+        }
+
+        private double[] CacheMinSizes(int cellsHead, bool isRows)
+        {
+            double[] minSizes = isRows ? new double[DefinitionsV.Count] : new double[DefinitionsU.Count];
+
+            for (int j = 0; j < minSizes.Length; j++)
+            {
+                minSizes[j] = -1;
+            }
+
+            int i = cellsHead;
+            do
+            {
+                if (isRows)
+                {
+                    minSizes[PrivateCells[i].RowIndex] = DefinitionsV[PrivateCells[i].RowIndex].MinSize;
+                }
+                else
+                {
+                    minSizes[PrivateCells[i].ColumnIndex] = DefinitionsU[PrivateCells[i].ColumnIndex].MinSize;
+                }
+
+                i = PrivateCells[i].Next;
+            } while (i < PrivateCells.Length);
+
+            return minSizes;
+        }
+
+        private void ApplyCachedMinSizes(double[] minSizes, bool isRows)
+        {
+            for (int i = 0; i < minSizes.Length; i++)
+            {
+                if (MathUtilities.GreaterThanOrClose(minSizes[i], 0))
+                {
+                    if (isRows)
+                    {
+                        DefinitionsV[i].SetMinSize(minSizes[i]);
+                    }
+                    else
+                    {
+                        DefinitionsU[i].SetMinSize(minSizes[i]);
+                    }
+                }
+            }
+        }
+
+        private void MeasureCellsGroup(
+            int cellsHead,
+            Size referenceSize,
+            bool ignoreDesiredSizeU,
+            bool forceInfinityV)
+        {
+            bool unusedHasDesiredSizeUChanged;
+            MeasureCellsGroup(cellsHead, referenceSize, ignoreDesiredSizeU, forceInfinityV, out unusedHasDesiredSizeUChanged);
+        }
+
+        /// <summary>
+        /// Measures one group of cells.
+        /// </summary>
+        /// <param name="cellsHead">Head index of the cells chain.</param>
+        /// <param name="referenceSize">Reference size for spanned cells
+        /// calculations.</param>
+        /// <param name="ignoreDesiredSizeU">When "true" cells' desired
+        /// width is not registered in columns.</param>
+        /// <param name="forceInfinityV">Passed through to MeasureCell.
+        /// When "true" cells' desired height is not registered in rows.</param>
+        private void MeasureCellsGroup(
+            int cellsHead,
+            Size referenceSize,
+            bool ignoreDesiredSizeU,
+            bool forceInfinityV,
+            out bool hasDesiredSizeUChanged)
+        {
+            hasDesiredSizeUChanged = false;
+
+            if (cellsHead >= PrivateCells.Length)
+            {
+                return;
+            }
+
+            var children = this.Children;
+            Hashtable spanStore = null;
+            bool ignoreDesiredSizeV = forceInfinityV;
+
+            int i = cellsHead;
+            do
+            {
+                double oldWidth = children[i].DesiredSize.Width;
+
+                MeasureCell(i, forceInfinityV);
+
+                hasDesiredSizeUChanged |= !MathUtilities.AreClose(oldWidth, children[i].DesiredSize.Width);
+
+                if (!ignoreDesiredSizeU)
+                {
+                    if (PrivateCells[i].ColumnSpan == 1)
+                    {
+                        DefinitionsU[PrivateCells[i].ColumnIndex].UpdateMinSize(Math.Min(children[i].DesiredSize.Width, DefinitionsU[PrivateCells[i].ColumnIndex].UserMaxSize));
+                    }
+                    else
+                    {
+                        RegisterSpan(
+                            ref spanStore,
+                            PrivateCells[i].ColumnIndex,
+                            PrivateCells[i].ColumnSpan,
+                            true,
+                            children[i].DesiredSize.Width);
+                    }
+                }
+
+                if (!ignoreDesiredSizeV)
+                {
+                    if (PrivateCells[i].RowSpan == 1)
+                    {
+                        DefinitionsV[PrivateCells[i].RowIndex].UpdateMinSize(Math.Min(children[i].DesiredSize.Height, DefinitionsV[PrivateCells[i].RowIndex].UserMaxSize));
+                    }
+                    else
+                    {
+                        RegisterSpan(
+                            ref spanStore,
+                            PrivateCells[i].RowIndex,
+                            PrivateCells[i].RowSpan,
+                            false,
+                            children[i].DesiredSize.Height);
+                    }
+                }
+
+                i = PrivateCells[i].Next;
+            } while (i < PrivateCells.Length);
+
+            if (spanStore != null)
+            {
+                foreach (DictionaryEntry e in spanStore)
+                {
+                    SpanKey key = (SpanKey)e.Key;
+                    double requestedSize = (double)e.Value;
+
+                    EnsureMinSizeInDefinitionRange(
+                        key.U ? DefinitionsU : DefinitionsV,
+                        key.Start,
+                        key.Count,
+                        requestedSize,
+                        key.U ? referenceSize.Width : referenceSize.Height);
+                }
+            }
+        }
+
+        /// <summary>
+        /// Helper method to register a span information for delayed processing.
+        /// </summary>
+        /// <param name="store">Reference to a hashtable object used as storage.</param>
+        /// <param name="start">Span starting index.</param>
+        /// <param name="count">Span count.</param>
+        /// <param name="u"><c>true</c> if this is a column span. <c>false</c> if this is a row span.</param>
+        /// <param name="value">Value to store. If an entry already exists the biggest value is stored.</param>
+        private static void RegisterSpan(
+            ref Hashtable store,
+            int start,
+            int count,
+            bool u,
+            double value)
+        {
+            if (store == null)
+            {
+                store = new Hashtable();
+            }
+
+            SpanKey key = new SpanKey(start, count, u);
+            object o = store[key];
+
+            if (o == null
+                || value > (double)o)
+            {
+                store[key] = value;
+            }
+        }
+
+        /// <summary>
+        /// Takes care of measuring a single cell.
+        /// </summary>
+        /// <param name="cell">Index of the cell to measure.</param>
+        /// <param name="forceInfinityV">If "true" then cell is always
+        /// calculated to infinite height.</param>
+        private void MeasureCell(
+            int cell,
+            bool forceInfinityV)
+        {
+
+
+            double cellMeasureWidth;
+            double cellMeasureHeight;
+
+            if (PrivateCells[cell].IsAutoU
+                && !PrivateCells[cell].IsStarU)
+            {
+                //  if cell belongs to at least one Auto column and not a single Star column
+                //  then it should be calculated "to content", thus it is possible to "shortcut"
+                //  calculations and simply assign PositiveInfinity here.
+                cellMeasureWidth = double.PositiveInfinity;
+            }
+            else
+            {
+                //  otherwise...
+                cellMeasureWidth = GetMeasureSizeForRange(
+                                        DefinitionsU,
+                                        PrivateCells[cell].ColumnIndex,
+                                        PrivateCells[cell].ColumnSpan);
+            }
+
+            if (forceInfinityV)
+            {
+                cellMeasureHeight = double.PositiveInfinity;
+            }
+            else if (PrivateCells[cell].IsAutoV
+                    && !PrivateCells[cell].IsStarV)
+            {
+                //  if cell belongs to at least one Auto row and not a single Star row
+                //  then it should be calculated "to content", thus it is possible to "shortcut"
+                //  calculations and simply assign PositiveInfinity here.
+                cellMeasureHeight = double.PositiveInfinity;
+            }
+            else
+            {
+                cellMeasureHeight = GetMeasureSizeForRange(
+                                        DefinitionsV,
+                                        PrivateCells[cell].RowIndex,
+                                        PrivateCells[cell].RowSpan);
+            }
+
+
+            var child = this.Children[cell];
+            if (child != null)
+            {
+                Size childConstraint = new Size(cellMeasureWidth, cellMeasureHeight);
+                child.Measure(childConstraint);
+            }
+
+
+
+        }
+
         /// <summary>
-        /// Defines the Column attached property.
+        /// Calculates one dimensional measure size for given definitions' range.
         /// </summary>
-        public static readonly AttachedProperty<int> ColumnProperty =
-            AvaloniaProperty.RegisterAttached<Grid, Control, int>(
-                "Column",
-                validate: ValidateColumn);
+        /// <param name="definitions">Source array of definitions to read values from.</param>
+        /// <param name="start">Starting index of the range.</param>
+        /// <param name="count">Number of definitions included in the range.</param>
+        /// <returns>Calculated measure size.</returns>
+        /// <remarks>
+        /// For "Auto" definitions MinWidth is used in place of PreferredSize.
+        /// </remarks>
+        private double GetMeasureSizeForRange(
+            IReadOnlyList<DefinitionBase> definitions,
+            int start,
+            int count)
+        {
+            Debug.Assert(0 < count && 0 <= start && (start + count) <= definitions.Count);
+
+            double measureSize = 0;
+            int i = start + count - 1;
+
+            do
+            {
+                measureSize += (definitions[i].SizeType == LayoutTimeSizeType.Auto)
+                    ? definitions[i].MinSize
+                    : definitions[i].MeasureSize;
+            } while (--i >= start);
+
+            return (measureSize);
+        }
 
         /// <summary>
-        /// Defines the ColumnSpan attached property.
+        /// Accumulates length type information for given definition's range.
         /// </summary>
-        public static readonly AttachedProperty<int> ColumnSpanProperty =
-            AvaloniaProperty.RegisterAttached<Grid, Control, int>("ColumnSpan", 1);
+        /// <param name="definitions">Source array of definitions to read values from.</param>
+        /// <param name="start">Starting index of the range.</param>
+        /// <param name="count">Number of definitions included in the range.</param>
+        /// <returns>Length type for given range.</returns>
+        private LayoutTimeSizeType GetLengthTypeForRange(
+            IReadOnlyList<DefinitionBase> definitions,
+            int start,
+            int count)
+        {
+            Debug.Assert(0 < count && 0 <= start && (start + count) <= definitions.Count);
+
+            LayoutTimeSizeType lengthType = LayoutTimeSizeType.None;
+            int i = start + count - 1;
+
+            do
+            {
+                lengthType |= definitions[i].SizeType;
+            } while (--i >= start);
+
+            return (lengthType);
+        }
 
         /// <summary>
-        /// Defines the Row attached property.
+        /// Distributes min size back to definition array's range.
         /// </summary>
-        public static readonly AttachedProperty<int> RowProperty =
-            AvaloniaProperty.RegisterAttached<Grid, Control, int>(
-                "Row",
-                validate: ValidateRow);
+        /// <param name="start">Start of the range.</param>
+        /// <param name="count">Number of items in the range.</param>
+        /// <param name="requestedSize">Minimum size that should "fit" into the definitions range.</param>
+        /// <param name="definitions">Definition array receiving distribution.</param>
+        /// <param name="percentReferenceSize">Size used to resolve percentages.</param>
+        private void EnsureMinSizeInDefinitionRange(
+            IReadOnlyList<DefinitionBase> definitions,
+            int start,
+            int count,
+            double requestedSize,
+            double percentReferenceSize)
+        {
+            Debug.Assert(1 < count && 0 <= start && (start + count) <= definitions.Count);
+
+            //  avoid processing when asked to distribute "0"
+            if (!_IsZero(requestedSize))
+            {
+                DefinitionBase[] tempDefinitions = TempDefinitions; //  temp array used to remember definitions for sorting
+                int end = start + count;
+                int autoDefinitionsCount = 0;
+                double rangeMinSize = 0;
+                double rangePreferredSize = 0;
+                double rangeMaxSize = 0;
+                double maxMaxSize = 0;                              //  maximum of maximum sizes
+
+                //  first accumulate the necessary information:
+                //  a) sum up the sizes in the range;
+                //  b) count the number of auto definitions in the range;
+                //  c) initialize temp array
+                //  d) cache the maximum size into SizeCache
+                //  e) accumulate max of max sizes
+                for (int i = start; i < end; ++i)
+                {
+                    double minSize = definitions[i].MinSize;
+                    double preferredSize = definitions[i].PreferredSize;
+                    double maxSize = Math.Max(definitions[i].UserMaxSize, minSize);
+
+                    rangeMinSize += minSize;
+                    rangePreferredSize += preferredSize;
+                    rangeMaxSize += maxSize;
+
+                    definitions[i].SizeCache = maxSize;
+
+                    //  sanity check: no matter what, but min size must always be the smaller;
+                    //  max size must be the biggest; and preferred should be in between
+                    Debug.Assert(minSize <= preferredSize
+                                && preferredSize <= maxSize
+                                && rangeMinSize <= rangePreferredSize
+                                && rangePreferredSize <= rangeMaxSize);
+
+                    if (maxMaxSize < maxSize) maxMaxSize = maxSize;
+                    if (definitions[i].UserSize.IsAuto) autoDefinitionsCount++;
+                    tempDefinitions[i - start] = definitions[i];
+                }
+
+                //  avoid processing if the range already big enough
+                if (requestedSize > rangeMinSize)
+                {
+                    if (requestedSize <= rangePreferredSize)
+                    {
+                        //
+                        //  requestedSize fits into preferred size of the range.
+                        //  distribute according to the following logic:
+                        //  * do not distribute into auto definitions - they should continue to stay "tight";
+                        //  * for all non-auto definitions distribute to equi-size min sizes, without exceeding preferred size.
+                        //
+                        //  in order to achieve that, definitions are sorted in a way that all auto definitions
+                        //  are first, then definitions follow ascending order with PreferredSize as the key of sorting.
+                        //
+                        double sizeToDistribute;
+                        int i;
+
+                        Array.Sort(tempDefinitions, 0, count, s_spanPreferredDistributionOrderComparer);
+                        for (i = 0, sizeToDistribute = requestedSize; i < autoDefinitionsCount; ++i)
+                        {
+                            //  sanity check: only auto definitions allowed in this loop
+                            Debug.Assert(tempDefinitions[i].UserSize.IsAuto);
+
+                            //  adjust sizeToDistribute value by subtracting auto definition min size
+                            sizeToDistribute -= (tempDefinitions[i].MinSize);
+                        }
+
+                        for (; i < count; ++i)
+                        {
+                            //  sanity check: no auto definitions allowed in this loop
+                            Debug.Assert(!tempDefinitions[i].UserSize.IsAuto);
+
+                            double newMinSize = Math.Min(sizeToDistribute / (count - i), tempDefinitions[i].PreferredSize);
+                            if (newMinSize > tempDefinitions[i].MinSize) { tempDefinitions[i].UpdateMinSize(newMinSize); }
+                            sizeToDistribute -= newMinSize;
+                        }
+
+                        //  sanity check: requested size must all be distributed
+                        Debug.Assert(_IsZero(sizeToDistribute));
+                    }
+                    else if (requestedSize <= rangeMaxSize)
+                    {
+                        //
+                        //  requestedSize bigger than preferred size, but fit into max size of the range.
+                        //  distribute according to the following logic:
+                        //  * do not distribute into auto definitions, if possible - they should continue to stay "tight";
+                        //  * for all non-auto definitions distribute to euqi-size min sizes, without exceeding max size.
+                        //
+                        //  in order to achieve that, definitions are sorted in a way that all non-auto definitions
+                        //  are last, then definitions follow ascending order with MaxSize as the key of sorting.
+                        //
+                        double sizeToDistribute;
+                        int i;
+
+                        Array.Sort(tempDefinitions, 0, count, s_spanMaxDistributionOrderComparer);
+                        for (i = 0, sizeToDistribute = requestedSize - rangePreferredSize; i < count - autoDefinitionsCount; ++i)
+                        {
+                            //  sanity check: no auto definitions allowed in this loop
+                            Debug.Assert(!tempDefinitions[i].UserSize.IsAuto);
+
+                            double preferredSize = tempDefinitions[i].PreferredSize;
+                            double newMinSize = preferredSize + sizeToDistribute / (count - autoDefinitionsCount - i);
+                            tempDefinitions[i].UpdateMinSize(Math.Min(newMinSize, tempDefinitions[i].SizeCache));
+                            sizeToDistribute -= (tempDefinitions[i].MinSize - preferredSize);
+                        }
+
+                        for (; i < count; ++i)
+                        {
+                            //  sanity check: only auto definitions allowed in this loop
+                            Debug.Assert(tempDefinitions[i].UserSize.IsAuto);
+
+                            double preferredSize = tempDefinitions[i].MinSize;
+                            double newMinSize = preferredSize + sizeToDistribute / (count - i);
+                            tempDefinitions[i].UpdateMinSize(Math.Min(newMinSize, tempDefinitions[i].SizeCache));
+                            sizeToDistribute -= (tempDefinitions[i].MinSize - preferredSize);
+                        }
+
+                        //  sanity check: requested size must all be distributed
+                        Debug.Assert(_IsZero(sizeToDistribute));
+                    }
+                    else
+                    {
+                        //
+                        //  requestedSize bigger than max size of the range.
+                        //  distribute according to the following logic:
+                        //  * for all definitions distribute to equi-size min sizes.
+                        //
+                        double equalSize = requestedSize / count;
+
+                        if (equalSize < maxMaxSize
+                            && !_AreClose(equalSize, maxMaxSize))
+                        {
+                            //  equi-size is less than maximum of maxSizes.
+                            //  in this case distribute so that smaller definitions grow faster than
+                            //  bigger ones.
+                            double totalRemainingSize = maxMaxSize * count - rangeMaxSize;
+                            double sizeToDistribute = requestedSize - rangeMaxSize;
+
+                            //  sanity check: totalRemainingSize and sizeToDistribute must be real positive numbers
+                            Debug.Assert(!double.IsInfinity(totalRemainingSize)
+                                        && !double.IsNaN(totalRemainingSize)
+                                        && totalRemainingSize > 0
+                                        && !double.IsInfinity(sizeToDistribute)
+                                        && !double.IsNaN(sizeToDistribute)
+                                        && sizeToDistribute > 0);
+
+                            for (int i = 0; i < count; ++i)
+                            {
+                                double deltaSize = (maxMaxSize - tempDefinitions[i].SizeCache) * sizeToDistribute / totalRemainingSize;
+                                tempDefinitions[i].UpdateMinSize(tempDefinitions[i].SizeCache + deltaSize);
+                            }
+                        }
+                        else
+                        {
+                            //
+                            //  equi-size is greater or equal to maximum of max sizes.
+                            //  all definitions receive equalSize as their mim sizes.
+                            //
+                            for (int i = 0; i < count; ++i)
+                            {
+                                tempDefinitions[i].UpdateMinSize(equalSize);
+                            }
+                        }
+                    }
+                }
+            }
+        }
 
         /// <summary>
-        /// Defines the RowSpan attached property.
+        /// Resolves Star's for given array of definitions.
         /// </summary>
-        public static readonly AttachedProperty<int> RowSpanProperty =
-            AvaloniaProperty.RegisterAttached<Grid, Control, int>("RowSpan", 1);
+        /// <param name="definitions">Array of definitions to resolve stars.</param>
+        /// <param name="availableSize">All available size.</param>
+        /// <remarks>
+        /// Must initialize LayoutSize for all Star entries in given array of definitions.
+        /// </remarks>
+        private void ResolveStar(
+            IReadOnlyList<DefinitionBase> definitions,
+            double availableSize)
+        {
+            ResolveStarMaxDiscrepancy(definitions, availableSize);
+        }
 
-        public static readonly AttachedProperty<bool> IsSharedSizeScopeProperty =
-            AvaloniaProperty.RegisterAttached<Grid, Control, bool>("IsSharedSizeScope", false);
+        // New implementation as of 4.7.  Several improvements:
+        // 1. Allocate to *-defs hitting their min or max constraints, before allocating
+        //      to other *-defs.  A def that hits its min uses more space than its
+        //      proportional share, reducing the space available to everyone else.
+        //      The legacy algorithm deducted this space only from defs processed
+        //      after the min;  the new algorithm deducts it proportionally from all
+        //      defs.   This avoids the "*-defs exceed available space" problem,
+        //      and other related problems where *-defs don't receive proportional
+        //      allocations even though no constraints are preventing it.
+        // 2. When multiple defs hit min or max, resolve the one with maximum
+        //      discrepancy (defined below).   This avoids discontinuities - small
+        //      change in available space resulting in large change to one def's allocation.
+        // 3. Correct handling of large *-values, including Infinity.
+        private void ResolveStarMaxDiscrepancy(
+            IReadOnlyList<DefinitionBase> definitions,
+            double availableSize)
+        {
+            int defCount = definitions.Count;
+            DefinitionBase[] tempDefinitions = TempDefinitions;
+            int minCount = 0, maxCount = 0;
+            double takenSize = 0;
+            double totalStarWeight = 0.0;
+            int starCount = 0;      // number of unresolved *-definitions
+            double scale = 1.0;     // scale factor applied to each *-weight;  negative means "Infinity is present"
+
+            // Phase 1.  Determine the maximum *-weight and prepare to adjust *-weights
+            double maxStar = 0.0;
+            for (int i = 0; i < defCount; ++i)
+            {
+                DefinitionBase def = definitions[i];
+
+                if (def.SizeType == LayoutTimeSizeType.Star)
+                {
+                    ++starCount;
+                    def.MeasureSize = 1.0;  // meaning "not yet resolved in phase 3"
+                    if (def.UserSize.Value > maxStar)
+                    {
+                        maxStar = def.UserSize.Value;
+                    }
+                }
+            }
+
+            if (Double.IsPositiveInfinity(maxStar))
+            {
+                // negative scale means one or more of the weights was Infinity
+                scale = -1.0;
+            }
+            else if (starCount > 0)
+            {
+                // if maxStar * starCount > Double.Max, summing all the weights could cause
+                // floating-point overflow.  To avoid that, scale the weights by a factor to keep
+                // the sum within limits.  Choose a power of 2, to preserve precision.
+                double power = Math.Floor(Math.Log(Double.MaxValue / maxStar / starCount, 2.0));
+                if (power < 0.0)
+                {
+                    scale = Math.Pow(2.0, power - 4.0); // -4 is just for paranoia
+                }
+            }
+
+            // normally Phases 2 and 3 execute only once.  But certain unusual combinations of weights
+            // and constraints can defeat the algorithm, in which case we repeat Phases 2 and 3.
+            // More explanation below...
+            for (bool runPhase2and3 = true; runPhase2and3;)
+            {
+                // Phase 2.   Compute total *-weight W and available space S.
+                // For *-items that have Min or Max constraints, compute the ratios used to decide
+                // whether proportional space is too big or too small and add the item to the
+                // corresponding list.  (The "min" list is in the first half of tempDefinitions,
+                // the "max" list in the second half.  TempDefinitions has capacity at least
+                // 2*defCount, so there's room for both lists.)
+                totalStarWeight = 0.0;
+                takenSize = 0.0;
+                minCount = maxCount = 0;
+
+                for (int i = 0; i < defCount; ++i)
+                {
+                    DefinitionBase def = definitions[i];
+
+                    switch (def.SizeType)
+                    {
+                        case (LayoutTimeSizeType.Auto):
+                            takenSize += definitions[i].MinSize;
+                            break;
+                        case (LayoutTimeSizeType.Pixel):
+                            takenSize += def.MeasureSize;
+                            break;
+                        case (LayoutTimeSizeType.Star):
+                            if (def.MeasureSize < 0.0)
+                            {
+                                takenSize += -def.MeasureSize;  // already resolved
+                            }
+                            else
+                            {
+                                double starWeight = StarWeight(def, scale);
+                                totalStarWeight += starWeight;
+
+                                if (def.MinSize > 0.0)
+                                {
+                                    // store ratio w/min in MeasureSize (for now)
+                                    tempDefinitions[minCount++] = def;
+                                    def.MeasureSize = starWeight / def.MinSize;
+                                }
+
+                                double effectiveMaxSize = Math.Max(def.MinSize, def.UserMaxSize);
+                                if (!Double.IsPositiveInfinity(effectiveMaxSize))
+                                {
+                                    // store ratio w/max in SizeCache (for now)
+                                    tempDefinitions[defCount + maxCount++] = def;
+                                    def.SizeCache = starWeight / effectiveMaxSize;
+                                }
+                            }
+                            break;
+                    }
+                }
+
+                // Phase 3.  Resolve *-items whose proportional sizes are too big or too small.
+                int minCountPhase2 = minCount, maxCountPhase2 = maxCount;
+                double takenStarWeight = 0.0;
+                double remainingAvailableSize = availableSize - takenSize;
+                double remainingStarWeight = totalStarWeight - takenStarWeight;
+                Array.Sort(tempDefinitions, 0, minCount, s_minRatioComparer);
+                Array.Sort(tempDefinitions, defCount, maxCount, s_maxRatioComparer);
+
+                while (minCount + maxCount > 0 && remainingAvailableSize > 0.0)
+                {
+                    // the calculation
+                    //            remainingStarWeight = totalStarWeight - takenStarWeight
+                    // is subject to catastrophic cancellation if the two terms are nearly equal,
+                    // which leads to meaningless results.   Check for that, and recompute from
+                    // the remaining definitions.   [This leads to quadratic behavior in really
+                    // pathological cases - but they'd never arise in practice.]
+                    const double starFactor = 1.0 / 256.0;      // lose more than 8 bits of precision -> recalculate
+                    if (remainingStarWeight < totalStarWeight * starFactor)
+                    {
+                        takenStarWeight = 0.0;
+                        totalStarWeight = 0.0;
+
+                        for (int i = 0; i < defCount; ++i)
+                        {
+                            DefinitionBase def = definitions[i];
+                            if (def.SizeType == LayoutTimeSizeType.Star && def.MeasureSize > 0.0)
+                            {
+                                totalStarWeight += StarWeight(def, scale);
+                            }
+                        }
+
+                        remainingStarWeight = totalStarWeight - takenStarWeight;
+                    }
+
+                    double minRatio = (minCount > 0) ? tempDefinitions[minCount - 1].MeasureSize : Double.PositiveInfinity;
+                    double maxRatio = (maxCount > 0) ? tempDefinitions[defCount + maxCount - 1].SizeCache : -1.0;
+
+                    // choose the def with larger ratio to the current proportion ("max discrepancy")
+                    double proportion = remainingStarWeight / remainingAvailableSize;
+                    bool? chooseMin = Choose(minRatio, maxRatio, proportion);
+
+                    // if no def was chosen, advance to phase 4;  the current proportion doesn't
+                    // conflict with any min or max values.
+                    if (!(chooseMin.HasValue))
+                    {
+                        break;
+                    }
+
+                    // get the chosen definition and its resolved size
+                    DefinitionBase resolvedDef;
+                    double resolvedSize;
+                    if (chooseMin == true)
+                    {
+                        resolvedDef = tempDefinitions[minCount - 1];
+                        resolvedSize = resolvedDef.MinSize;
+                        --minCount;
+                    }
+                    else
+                    {
+                        resolvedDef = tempDefinitions[defCount + maxCount - 1];
+                        resolvedSize = Math.Max(resolvedDef.MinSize, resolvedDef.UserMaxSize);
+                        --maxCount;
+                    }
+
+                    // resolve the chosen def, deduct its contributions from W and S.
+                    // Defs resolved in phase 3 are marked by storing the negative of their resolved
+                    // size in MeasureSize, to distinguish them from a pending def.
+                    takenSize += resolvedSize;
+                    resolvedDef.MeasureSize = -resolvedSize;
+                    takenStarWeight += StarWeight(resolvedDef, scale);
+                    --starCount;
+
+                    remainingAvailableSize = availableSize - takenSize;
+                    remainingStarWeight = totalStarWeight - takenStarWeight;
+
+                    // advance to the next candidate defs, removing ones that have been resolved.
+                    // Both counts are advanced, as a def might appear in both lists.
+                    while (minCount > 0 && tempDefinitions[minCount - 1].MeasureSize < 0.0)
+                    {
+                        --minCount;
+                        tempDefinitions[minCount] = null;
+                    }
+                    while (maxCount > 0 && tempDefinitions[defCount + maxCount - 1].MeasureSize < 0.0)
+                    {
+                        --maxCount;
+                        tempDefinitions[defCount + maxCount] = null;
+                    }
+                }
+
+                // decide whether to run Phase2 and Phase3 again.  There are 3 cases:
+                // 1. There is space available, and *-defs remaining.  This is the
+                //      normal case - move on to Phase 4 to allocate the remaining
+                //      space proportionally to the remaining *-defs.
+                // 2. There is space available, but no *-defs.  This implies at least one
+                //      def was resolved as 'max', taking less space than its proportion.
+                //      If there are also 'min' defs, reconsider them - we can give
+                //      them more space.   If not, all the *-defs are 'max', so there's
+                //      no way to use all the available space.
+                // 3. We allocated too much space.   This implies at least one def was
+                //      resolved as 'min'.  If there are also 'max' defs, reconsider
+                //      them, otherwise the over-allocation is an inevitable consequence
+                //      of the given min constraints.
+                // Note that if we return to Phase2, at least one *-def will have been
+                // resolved.  This guarantees we don't run Phase2+3 infinitely often.
+                runPhase2and3 = false;
+                if (starCount == 0 && takenSize < availableSize)
+                {
+                    // if no *-defs remain and we haven't allocated all the space, reconsider the defs
+                    // resolved as 'min'.   Their allocation can be increased to make up the gap.
+                    for (int i = minCount; i < minCountPhase2; ++i)
+                    {
+                        DefinitionBase def = tempDefinitions[i];
+                        if (def != null)
+                        {
+                            def.MeasureSize = 1.0;      // mark as 'not yet resolved'
+                            ++starCount;
+                            runPhase2and3 = true;       // found a candidate, so re-run Phases 2 and 3
+                        }
+                    }
+                }
+
+                if (takenSize > availableSize)
+                {
+                    // if we've allocated too much space, reconsider the defs
+                    // resolved as 'max'.   Their allocation can be decreased to make up the gap.
+                    for (int i = maxCount; i < maxCountPhase2; ++i)
+                    {
+                        DefinitionBase def = tempDefinitions[defCount + i];
+                        if (def != null)
+                        {
+                            def.MeasureSize = 1.0;      // mark as 'not yet resolved'
+                            ++starCount;
+                            runPhase2and3 = true;    // found a candidate, so re-run Phases 2 and 3
+                        }
+                    }
+                }
+            }
+
+            // Phase 4.  Resolve the remaining defs proportionally.
+            starCount = 0;
+            for (int i = 0; i < defCount; ++i)
+            {
+                DefinitionBase def = definitions[i];
+
+                if (def.SizeType == LayoutTimeSizeType.Star)
+                {
+                    if (def.MeasureSize < 0.0)
+                    {
+                        // this def was resolved in phase 3 - fix up its measure size
+                        def.MeasureSize = -def.MeasureSize;
+                    }
+                    else
+                    {
+                        // this def needs resolution, add it to the list, sorted by *-weight
+                        tempDefinitions[starCount++] = def;
+                        def.MeasureSize = StarWeight(def, scale);
+                    }
+                }
+            }
+
+            if (starCount > 0)
+            {
+                Array.Sort(tempDefinitions, 0, starCount, s_starWeightComparer);
+
+                // compute the partial sums of *-weight, in increasing order of weight
+                // for minimal loss of precision.
+                totalStarWeight = 0.0;
+                for (int i = 0; i < starCount; ++i)
+                {
+                    DefinitionBase def = tempDefinitions[i];
+                    totalStarWeight += def.MeasureSize;
+                    def.SizeCache = totalStarWeight;
+                }
+
+                // resolve the defs, in decreasing order of weight
+                for (int i = starCount - 1; i >= 0; --i)
+                {
+                    DefinitionBase def = tempDefinitions[i];
+                    double resolvedSize = (def.MeasureSize > 0.0) ? Math.Max(availableSize - takenSize, 0.0) * (def.MeasureSize / def.SizeCache) : 0.0;
+
+                    // min and max should have no effect by now, but just in case...
+                    resolvedSize = Math.Min(resolvedSize, def.UserMaxSize);
+                    resolvedSize = Math.Max(def.MinSize, resolvedSize);
+
+                    def.MeasureSize = resolvedSize;
+                    takenSize += resolvedSize;
+                }
+            }
+        }
+
+        /// <summary>
+        /// Calculates desired size for given array of definitions.
+        /// </summary>
+        /// <param name="definitions">Array of definitions to use for calculations.</param>
+        /// <returns>Desired size.</returns>
+        private double CalculateDesiredSize(
+            IReadOnlyList<DefinitionBase> definitions)
+        {
+            double desiredSize = 0;
+
+            for (int i = 0; i < definitions.Count; ++i)
+            {
+                desiredSize += definitions[i].MinSize;
+            }
+
+            return (desiredSize);
+        }
+
+        /// <summary>
+        /// Calculates and sets final size for all definitions in the given array.
+        /// </summary>
+        /// <param name="definitions">Array of definitions to process.</param>
+        /// <param name="finalSize">Final size to lay out to.</param>
+        /// <param name="columns">True if sizing column definitions, false for rows</param>
+        private void SetFinalSize(
+            IReadOnlyList<DefinitionBase> definitions,
+            double finalSize,
+            bool columns)
+        {
+            SetFinalSizeMaxDiscrepancy(definitions, finalSize, columns);
+        }
+
+        // new implementation, as of 4.7.  This incorporates the same algorithm
+        // as in ResolveStarMaxDiscrepancy.  It differs in the same way that SetFinalSizeLegacy
+        // differs from ResolveStarLegacy, namely (a) leaves results in def.SizeCache
+        // instead of def.MeasureSize, (b) implements LayoutRounding if requested,
+        // (c) stores intermediate results differently.
+        // The LayoutRounding logic is improved:
+        // 1. Use pre-rounded values during proportional allocation.  This avoids the
+        //      same kind of problems arising from interaction with min/max that
+        //      motivated the new algorithm in the first place.
+        // 2. Use correct "nudge" amount when distributing roundoff space.   This
+        //      comes into play at high DPI - greater than 134.
+        // 3. Applies rounding only to real pixel values (not to ratios)
+        private void SetFinalSizeMaxDiscrepancy(
+            IReadOnlyList<DefinitionBase> definitions,
+            double finalSize,
+            bool columns)
+        {
+            int defCount = definitions.Count;
+            int[] definitionIndices = DefinitionIndices;
+            int minCount = 0, maxCount = 0;
+            double takenSize = 0.0;
+            double totalStarWeight = 0.0;
+            int starCount = 0;      // number of unresolved *-definitions
+            double scale = 1.0;   // scale factor applied to each *-weight;  negative means "Infinity is present"
+
+            // Phase 1.  Determine the maximum *-weight and prepare to adjust *-weights
+            double maxStar = 0.0;
+            for (int i = 0; i < defCount; ++i)
+            {
+                DefinitionBase def = definitions[i];
+
+                if (def.UserSize.IsStar)
+                {
+                    ++starCount;
+                    def.MeasureSize = 1.0;  // meaning "not yet resolved in phase 3"
+                    if (def.UserSize.Value > maxStar)
+                    {
+                        maxStar = def.UserSize.Value;
+                    }
+                }
+            }
+
+            if (Double.IsPositiveInfinity(maxStar))
+            {
+                // negative scale means one or more of the weights was Infinity
+                scale = -1.0;
+            }
+            else if (starCount > 0)
+            {
+                // if maxStar * starCount > Double.Max, summing all the weights could cause
+                // floating-point overflow.  To avoid that, scale the weights by a factor to keep
+                // the sum within limits.  Choose a power of 2, to preserve precision.
+                double power = Math.Floor(Math.Log(Double.MaxValue / maxStar / starCount, 2.0));
+                if (power < 0.0)
+                {
+                    scale = Math.Pow(2.0, power - 4.0); // -4 is just for paranoia
+                }
+            }
+
+            // normally Phases 2 and 3 execute only once.  But certain unusual combinations of weights
+            // and constraints can defeat the algorithm, in which case we repeat Phases 2 and 3.
+            // More explanation below...
+            for (bool runPhase2and3 = true; runPhase2and3;)
+            {
+                // Phase 2.   Compute total *-weight W and available space S.
+                // For *-items that have Min or Max constraints, compute the ratios used to decide
+                // whether proportional space is too big or too small and add the item to the
+                // corresponding list.  (The "min" list is in the first half of definitionIndices,
+                // the "max" list in the second half.  DefinitionIndices has capacity at least
+                // 2*defCount, so there's room for both lists.)
+                totalStarWeight = 0.0;
+                takenSize = 0.0;
+                minCount = maxCount = 0;
+
+                for (int i = 0; i < defCount; ++i)
+                {
+                    DefinitionBase def = definitions[i];
+
+                    if (def.UserSize.IsStar)
+                    {
+                        Debug.Assert(!def.IsShared, "*-defs cannot be shared");
+
+                        if (def.MeasureSize < 0.0)
+                        {
+                            takenSize += -def.MeasureSize;  // already resolved
+                        }
+                        else
+                        {
+                            double starWeight = StarWeight(def, scale);
+                            totalStarWeight += starWeight;
+
+                            if (def.MinSizeForArrange > 0.0)
+                            {
+                                // store ratio w/min in MeasureSize (for now)
+                                definitionIndices[minCount++] = i;
+                                def.MeasureSize = starWeight / def.MinSizeForArrange;
+                            }
+
+                            double effectiveMaxSize = Math.Max(def.MinSizeForArrange, def.UserMaxSize);
+                            if (!Double.IsPositiveInfinity(effectiveMaxSize))
+                            {
+                                // store ratio w/max in SizeCache (for now)
+                                definitionIndices[defCount + maxCount++] = i;
+                                def.SizeCache = starWeight / effectiveMaxSize;
+                            }
+                        }
+                    }
+                    else
+                    {
+                        double userSize = 0;
+
+                        switch (def.UserSize.GridUnitType)
+                        {
+                            case (GridUnitType.Pixel):
+                                userSize = def.UserSize.Value;
+                                break;
+
+                            case (GridUnitType.Auto):
+                                userSize = def.MinSizeForArrange;
+                                break;
+                        }
+
+                        double userMaxSize;
+
+                        if (def.IsShared)
+                        {
+                            //  overriding userMaxSize effectively prevents squishy-ness.
+                            //  this is a "solution" to avoid shared definitions from been sized to
+                            //  different final size at arrange time, if / when different grids receive
+                            //  different final sizes.
+                            userMaxSize = userSize;
+                        }
+                        else
+                        {
+                            userMaxSize = def.UserMaxSize;
+                        }
+
+                        def.SizeCache = Math.Max(def.MinSizeForArrange, Math.Min(userSize, userMaxSize));
+                        takenSize += def.SizeCache;
+                    }
+                }
+
+                // Phase 3.  Resolve *-items whose proportional sizes are too big or too small.
+                int minCountPhase2 = minCount, maxCountPhase2 = maxCount;
+                double takenStarWeight = 0.0;
+                double remainingAvailableSize = finalSize - takenSize;
+                double remainingStarWeight = totalStarWeight - takenStarWeight;
+
+                MinRatioIndexComparer minRatioIndexComparer = new MinRatioIndexComparer(definitions);
+                Array.Sort(definitionIndices, 0, minCount, minRatioIndexComparer);
+                MaxRatioIndexComparer maxRatioIndexComparer = new MaxRatioIndexComparer(definitions);
+                Array.Sort(definitionIndices, defCount, maxCount, maxRatioIndexComparer);
+
+                while (minCount + maxCount > 0 && remainingAvailableSize > 0.0)
+                {
+                    // the calculation
+                    //            remainingStarWeight = totalStarWeight - takenStarWeight
+                    // is subject to catastrophic cancellation if the two terms are nearly equal,
+                    // which leads to meaningless results.   Check for that, and recompute from
+                    // the remaining definitions.   [This leads to quadratic behavior in really
+                    // pathological cases - but they'd never arise in practice.]
+                    const double starFactor = 1.0 / 256.0;      // lose more than 8 bits of precision -> recalculate
+                    if (remainingStarWeight < totalStarWeight * starFactor)
+                    {
+                        takenStarWeight = 0.0;
+                        totalStarWeight = 0.0;
+
+                        for (int i = 0; i < defCount; ++i)
+                        {
+                            DefinitionBase def = definitions[i];
+                            if (def.UserSize.IsStar && def.MeasureSize > 0.0)
+                            {
+                                totalStarWeight += StarWeight(def, scale);
+                            }
+                        }
+
+                        remainingStarWeight = totalStarWeight - takenStarWeight;
+                    }
+
+                    double minRatio = (minCount > 0) ? definitions[definitionIndices[minCount - 1]].MeasureSize : Double.PositiveInfinity;
+                    double maxRatio = (maxCount > 0) ? definitions[definitionIndices[defCount + maxCount - 1]].SizeCache : -1.0;
+
+                    // choose the def with larger ratio to the current proportion ("max discrepancy")
+                    double proportion = remainingStarWeight / remainingAvailableSize;
+                    bool? chooseMin = Choose(minRatio, maxRatio, proportion);
+
+                    // if no def was chosen, advance to phase 4;  the current proportion doesn't
+                    // conflict with any min or max values.
+                    if (!chooseMin.HasValue)
+                    {
+                        break;
+                    }
+
+                    // get the chosen definition and its resolved size
+                    int resolvedIndex;
+                    DefinitionBase resolvedDef;
+                    double resolvedSize;
+                    if (chooseMin == true)
+                    {
+                        resolvedIndex = definitionIndices[minCount - 1];
+                        resolvedDef = definitions[resolvedIndex];
+                        resolvedSize = resolvedDef.MinSizeForArrange;
+                        --minCount;
+                    }
+                    else
+                    {
+                        resolvedIndex = definitionIndices[defCount + maxCount - 1];
+                        resolvedDef = definitions[resolvedIndex];
+                        resolvedSize = Math.Max(resolvedDef.MinSizeForArrange, resolvedDef.UserMaxSize);
+                        --maxCount;
+                    }
+
+                    // resolve the chosen def, deduct its contributions from W and S.
+                    // Defs resolved in phase 3 are marked by storing the negative of their resolved
+                    // size in MeasureSize, to distinguish them from a pending def.
+                    takenSize += resolvedSize;
+                    resolvedDef.MeasureSize = -resolvedSize;
+                    takenStarWeight += StarWeight(resolvedDef, scale);
+                    --starCount;
+
+                    remainingAvailableSize = finalSize - takenSize;
+                    remainingStarWeight = totalStarWeight - takenStarWeight;
+
+                    // advance to the next candidate defs, removing ones that have been resolved.
+                    // Both counts are advanced, as a def might appear in both lists.
+                    while (minCount > 0 && definitions[definitionIndices[minCount - 1]].MeasureSize < 0.0)
+                    {
+                        --minCount;
+                        definitionIndices[minCount] = -1;
+                    }
+                    while (maxCount > 0 && definitions[definitionIndices[defCount + maxCount - 1]].MeasureSize < 0.0)
+                    {
+                        --maxCount;
+                        definitionIndices[defCount + maxCount] = -1;
+                    }
+                }
+
+                // decide whether to run Phase2 and Phase3 again.  There are 3 cases:
+                // 1. There is space available, and *-defs remaining.  This is the
+                //      normal case - move on to Phase 4 to allocate the remaining
+                //      space proportionally to the remaining *-defs.
+                // 2. There is space available, but no *-defs.  This implies at least one
+                //      def was resolved as 'max', taking less space than its proportion.
+                //      If there are also 'min' defs, reconsider them - we can give
+                //      them more space.   If not, all the *-defs are 'max', so there's
+                //      no way to use all the available space.
+                // 3. We allocated too much space.   This implies at least one def was
+                //      resolved as 'min'.  If there are also 'max' defs, reconsider
+                //      them, otherwise the over-allocation is an inevitable consequence
+                //      of the given min constraints.
+                // Note that if we return to Phase2, at least one *-def will have been
+                // resolved.  This guarantees we don't run Phase2+3 infinitely often.
+                runPhase2and3 = false;
+                if (starCount == 0 && takenSize < finalSize)
+                {
+                    // if no *-defs remain and we haven't allocated all the space, reconsider the defs
+                    // resolved as 'min'.   Their allocation can be increased to make up the gap.
+                    for (int i = minCount; i < minCountPhase2; ++i)
+                    {
+                        if (definitionIndices[i] >= 0)
+                        {
+                            DefinitionBase def = definitions[definitionIndices[i]];
+                            def.MeasureSize = 1.0;      // mark as 'not yet resolved'
+                            ++starCount;
+                            runPhase2and3 = true;       // found a candidate, so re-run Phases 2 and 3
+                        }
+                    }
+                }
+
+                if (takenSize > finalSize)
+                {
+                    // if we've allocated too much space, reconsider the defs
+                    // resolved as 'max'.   Their allocation can be decreased to make up the gap.
+                    for (int i = maxCount; i < maxCountPhase2; ++i)
+                    {
+                        if (definitionIndices[defCount + i] >= 0)
+                        {
+                            DefinitionBase def = definitions[definitionIndices[defCount + i]];
+                            def.MeasureSize = 1.0;      // mark as 'not yet resolved'
+                            ++starCount;
+                            runPhase2and3 = true;    // found a candidate, so re-run Phases 2 and 3
+                        }
+                    }
+                }
+            }
+
+            // Phase 4.  Resolve the remaining defs proportionally.
+            starCount = 0;
+            for (int i = 0; i < defCount; ++i)
+            {
+                DefinitionBase def = definitions[i];
+
+                if (def.UserSize.IsStar)
+                {
+                    if (def.MeasureSize < 0.0)
+                    {
+                        // this def was resolved in phase 3 - fix up its size
+                        def.SizeCache = -def.MeasureSize;
+                    }
+                    else
+                    {
+                        // this def needs resolution, add it to the list, sorted by *-weight
+                        definitionIndices[starCount++] = i;
+                        def.MeasureSize = StarWeight(def, scale);
+                    }
+                }
+            }
+
+            if (starCount > 0)
+            {
+                StarWeightIndexComparer starWeightIndexComparer = new StarWeightIndexComparer(definitions);
+                Array.Sort(definitionIndices, 0, starCount, starWeightIndexComparer);
+
+                // compute the partial sums of *-weight, in increasing order of weight
+                // for minimal loss of precision.
+                totalStarWeight = 0.0;
+                for (int i = 0; i < starCount; ++i)
+                {
+                    DefinitionBase def = definitions[definitionIndices[i]];
+                    totalStarWeight += def.MeasureSize;
+                    def.SizeCache = totalStarWeight;
+                }
+
+                // resolve the defs, in decreasing order of weight.
+                for (int i = starCount - 1; i >= 0; --i)
+                {
+                    DefinitionBase def = definitions[definitionIndices[i]];
+                    double resolvedSize = (def.MeasureSize > 0.0) ? Math.Max(finalSize - takenSize, 0.0) * (def.MeasureSize / def.SizeCache) : 0.0;
+
+                    // min and max should have no effect by now, but just in case...
+                    resolvedSize = Math.Min(resolvedSize, def.UserMaxSize);
+                    resolvedSize = Math.Max(def.MinSizeForArrange, resolvedSize);
+
+                    // Use the raw (unrounded) sizes to update takenSize, so that
+                    // proportions are computed in the same terms as in phase 3;
+                    // this avoids errors arising from min/max constraints.
+                    takenSize += resolvedSize;
+                    def.SizeCache = resolvedSize;
+                }
+            }
+
+            // Phase 5.  Apply layout rounding.  We do this after fully allocating
+            // unrounded sizes, to avoid breaking assumptions in the previous phases
+            if (UseLayoutRounding)
+            {
+                // DpiScale dpiScale = GetDpi();
+                // double dpi = columns ? dpiScale.DpiScaleX : dpiScale.DpiScaleY;
+                var dpi = (VisualRoot as Layout.ILayoutRoot)?.LayoutScaling ?? 1.0;
+                double[] roundingErrors = RoundingErrors;
+                double roundedTakenSize = 0.0;
+
+                // round each of the allocated sizes, keeping track of the deltas
+                for (int i = 0; i < definitions.Count; ++i)
+                {
+                    DefinitionBase def = definitions[i];
+                    double roundedSize = MathUtilities.RoundLayoutValue(def.SizeCache, dpi);
+                    roundingErrors[i] = (roundedSize - def.SizeCache);
+                    def.SizeCache = roundedSize;
+                    roundedTakenSize += roundedSize;
+                }
+
+                // The total allocation might differ from finalSize due to rounding
+                // effects.  Tweak the allocations accordingly.
+
+                // Theoretical and historical note.  The problem at hand - allocating
+                // space to columns (or rows) with *-weights, min and max constraints,
+                // and layout rounding - has a long history.  Especially the special
+                // case of 50 columns with min=1 and available space=435 - allocating
+                // seats in the U.S. House of Representatives to the 50 states in
+                // proportion to their population.  There are numerous algorithms
+                // and papers dating back to the 1700's, including the book:
+                // Balinski, M. and H. Young, Fair Representation, Yale University Press, New Haven, 1982.
+                //
+                // One surprising result of all this research is that *any* algorithm
+                // will suffer from one or more undesirable features such as the
+                // "population paradox" or the "Alabama paradox", where (to use our terminology)
+                // increasing the available space by one pixel might actually decrease
+                // the space allocated to a given column, or increasing the weight of
+                // a column might decrease its allocation.   This is worth knowing
+                // in case someone complains about this behavior;  it's not a bug so
+                // much as something inherent to the problem.  Cite the book mentioned
+                // above or one of the 100s of references, and resolve as WontFix.
+                //
+                // Fortunately, our scenarios tend to have a small number of columns (~10 or fewer)
+                // each being allocated a large number of pixels (~50 or greater), and
+                // people don't even notice the kind of 1-pixel anomolies that are
+                // theoretically inevitable, or don't care if they do.  At least they shouldn't
+                // care - no one should be using the results WPF's grid layout to make
+                // quantitative decisions; its job is to produce a reasonable display, not
+                // to allocate seats in Congress.
+                //
+                // Our algorithm is more susceptible to paradox than the one currently
+                // used for Congressional allocation ("Huntington-Hill" algorithm), but
+                // it is faster to run:  O(N log N) vs. O(S * N), where N=number of
+                // definitions, S = number of available pixels.  And it produces
+                // adequate results in practice, as mentioned above.
+                //
+                // To reiterate one point:  all this only applies when layout rounding
+                // is in effect.  When fractional sizes are allowed, the algorithm
+                // behaves as well as possible, subject to the min/max constraints
+                // and precision of floating-point computation.  (However, the resulting
+                // display is subject to anti-aliasing problems.   TANSTAAFL.)
+
+                if (!_AreClose(roundedTakenSize, finalSize))
+                {
+                    // Compute deltas
+                    for (int i = 0; i < definitions.Count; ++i)
+                    {
+                        definitionIndices[i] = i;
+                    }
+
+                    // Sort rounding errors
+                    RoundingErrorIndexComparer roundingErrorIndexComparer = new RoundingErrorIndexComparer(roundingErrors);
+                    Array.Sort(definitionIndices, 0, definitions.Count, roundingErrorIndexComparer);
+                    double adjustedSize = roundedTakenSize;
+                    double dpiIncrement = 1.0 / dpi;
+
+                    if (roundedTakenSize > finalSize)
+                    {
+                        int i = definitions.Count - 1;
+                        while ((adjustedSize > finalSize && !_AreClose(adjustedSize, finalSize)) && i >= 0)
+                        {
+                            DefinitionBase definition = definitions[definitionIndices[i]];
+                            double final = definition.SizeCache - dpiIncrement;
+                            final = Math.Max(final, definition.MinSizeForArrange);
+                            if (final < definition.SizeCache)
+                            {
+                                adjustedSize -= dpiIncrement;
+                            }
+                            definition.SizeCache = final;
+                            i--;
+                        }
+                    }
+                    else if (roundedTakenSize < finalSize)
+                    {
+                        int i = 0;
+                        while ((adjustedSize < finalSize && !_AreClose(adjustedSize, finalSize)) && i < definitions.Count)
+                        {
+                            DefinitionBase definition = definitions[definitionIndices[i]];
+                            double final = definition.SizeCache + dpiIncrement;
+                            final = Math.Max(final, definition.MinSizeForArrange);
+                            if (final > definition.SizeCache)
+                            {
+                                adjustedSize += dpiIncrement;
+                            }
+                            definition.SizeCache = final;
+                            i++;
+                        }
+                    }
+                }
+            }
+
+            // Phase 6.  Compute final offsets
+            definitions[0].FinalOffset = 0.0;
+            for (int i = 0; i < definitions.Count; ++i)
+            {
+                definitions[(i + 1) % definitions.Count].FinalOffset = definitions[i].FinalOffset + definitions[i].SizeCache;
+            }
+        }
+
+        /// <summary>
+        /// Choose the ratio with maximum discrepancy from the current proportion.
+        /// Returns:
+        ///     true    if proportion fails a min constraint but not a max, or
+        ///                 if the min constraint has higher discrepancy
+        ///     false   if proportion fails a max constraint but not a min, or
+        ///                 if the max constraint has higher discrepancy
+        ///     null    if proportion doesn't fail a min or max constraint
+        /// The discrepancy is the ratio of the proportion to the max- or min-ratio.
+        /// When both ratios hit the constraint,  minRatio &lt; proportion &lt; maxRatio,
+        /// and the minRatio has higher discrepancy if
+        ///         (proportion / minRatio) &gt; (maxRatio / proportion)
+        /// </summary>
+        private static bool? Choose(double minRatio, double maxRatio, double proportion)
+        {
+            if (minRatio < proportion)
+            {
+                if (maxRatio > proportion)
+                {
+                    // compare proportion/minRatio : maxRatio/proportion, but
+                    // do it carefully to avoid floating-point overflow or underflow
+                    // and divide-by-0.
+                    double minPower = Math.Floor(Math.Log(minRatio, 2.0));
+                    double maxPower = Math.Floor(Math.Log(maxRatio, 2.0));
+                    double f = Math.Pow(2.0, Math.Floor((minPower + maxPower) / 2.0));
+                    if ((proportion / f) * (proportion / f) > (minRatio / f) * (maxRatio / f))
+                    {
+                        return true;
+                    }
+                    else
+                    {
+                        return false;
+                    }
+                }
+                else
+                {
+                    return true;
+                }
+            }
+            else if (maxRatio > proportion)
+            {
+                return false;
+            }
+
+            return null;
+        }
+
+        /// <summary>
+        /// Sorts row/column indices by rounding error if layout rounding is applied.
+        /// </summary>
+        /// <param name="x">Index, rounding error pair</param>
+        /// <param name="y">Index, rounding error pair</param>
+        /// <returns>1 if x.Value > y.Value, 0 if equal, -1 otherwise</returns>
+        private static int CompareRoundingErrors(KeyValuePair<int, double> x, KeyValuePair<int, double> y)
+        {
+            if (x.Value < y.Value)
+            {
+                return -1;
+            }
+            else if (x.Value > y.Value)
+            {
+                return 1;
+            }
+            return 0;
+        }
+
+        /// <summary>
+        /// Calculates final (aka arrange) size for given range.
+        /// </summary>
+        /// <param name="definitions">Array of definitions to process.</param>
+        /// <param name="start">Start of the range.</param>
+        /// <param name="count">Number of items in the range.</param>
+        /// <returns>Final size.</returns>
+        private double GetFinalSizeForRange(
+            IReadOnlyList<DefinitionBase> definitions,
+            int start,
+            int count)
+        {
+            double size = 0;
+            int i = start + count - 1;
+
+            do
+            {
+                size += definitions[i].SizeCache;
+            } while (--i >= start);
+
+            return (size);
+        }
+
+        /// <summary>
+        /// Clears dirty state for the grid and its columns / rows
+        /// </summary>
+        private void SetValid()
+        {
+            ExtendedData extData = ExtData;
+            if (extData != null)
+            {
+                //                for (int i = 0; i < PrivateColumnCount; ++i) DefinitionsU[i].SetValid ();
+                //                for (int i = 0; i < PrivateRowCount; ++i) DefinitionsV[i].SetValid ();
+
+                if (extData.TempDefinitions != null)
+                {
+                    //  TempDefinitions has to be cleared to avoid "memory leaks"
+                    Array.Clear(extData.TempDefinitions, 0, Math.Max(DefinitionsU.Count, DefinitionsV.Count));
+                    extData.TempDefinitions = null;
+                }
+            }
+        }
+ 
+        /// <summary>
+        /// Synchronized ShowGridLines property with the state of the grid's visual collection
+        /// by adding / removing GridLinesRenderer visual.
+        /// Returns a reference to GridLinesRenderer visual or null.
+        /// </summary>
+        private GridLinesRenderer EnsureGridLinesRenderer()
+        {
+            //
+            //  synchronize the state
+            //
+            if (ShowGridLines && (_gridLinesRenderer == null))
+            {
+                _gridLinesRenderer = new GridLinesRenderer();
+                this.VisualChildren.Add(_gridLinesRenderer);
+            }
+
+            if ((!ShowGridLines) && (_gridLinesRenderer != null))
+            {
+                this.VisualChildren.Add(_gridLinesRenderer);
+                _gridLinesRenderer = null;
+            }
+
+            return (_gridLinesRenderer);
+        }
+
+        /// <summary>
+        /// SetFlags is used to set or unset one or multiple
+        /// flags on the object.
+        /// </summary>
+        private void SetFlags(bool value, Flags flags)
+        {
+            _flags = value ? (_flags | flags) : (_flags & (~flags));
+        }
+
+        /// <summary>
+        /// CheckFlagsAnd returns <c>true</c> if all the flags in the
+        /// given bitmask are set on the object.
+        /// </summary>
+        private bool CheckFlagsAnd(Flags flags)
+        {
+            return ((_flags & flags) == flags);
+        }
+
+        /// <summary>
+        /// CheckFlagsOr returns <c>true</c> if at least one flag in the
+        /// given bitmask is set.
+        /// </summary>
+        /// <remarks>
+        /// If no bits are set in the given bitmask, the method returns
+        /// <c>true</c>.
+        /// </remarks>
+        private bool CheckFlagsOr(Flags flags)
+        {
+            return (flags == 0 || (_flags & flags) != 0);
+        }
+
+        private static void OnShowGridLinesPropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
+        {
+            Grid grid = (Grid)d;
+
+            if (grid.ExtData != null    // trivial grid is 1 by 1. there is no grid lines anyway
+                && grid.ListenToNotifications)
+            {
+                grid.InvalidateVisual();
+            }
+
+            grid.SetFlags((bool)e.NewValue, Flags.ShowGridLinesPropertyValue);
+        }
 
-        protected override void OnMeasureInvalidated()
+        private static void OnCellAttachedPropertyChanged(AvaloniaObject d, AvaloniaPropertyChangedEventArgs e)
         {
-            base.OnMeasureInvalidated();
-            _sharedSizeHost?.InvalidateMeasure(this);
+            Visual child = d as Visual;
+
+            if (child != null)
+            {
+                Grid grid = child.GetVisualParent<Grid>();
+                if (grid != null
+                    && grid.ExtData != null
+                    && grid.ListenToNotifications)
+                {
+                    grid.CellsStructureDirty = true;
+                }
+            }
         }
 
-        private SharedSizeScopeHost _sharedSizeHost;
-
         /// <summary>
-        /// Defines the SharedSizeScopeHost private property. 
-        /// The ampersands are used to make accessing the property via xaml inconvenient.
+        /// Helper for Comparer methods.
         /// </summary>
-        internal static readonly AttachedProperty<SharedSizeScopeHost> s_sharedSizeScopeHostProperty =
-            AvaloniaProperty.RegisterAttached<Grid, Control, SharedSizeScopeHost>("&&SharedSizeScopeHost");
+        /// <returns>
+        /// true if one or both of x and y are null, in which case result holds
+        /// the relative sort order.
+        /// </returns>
+        private static bool CompareNullRefs(object x, object y, out int result)
+        {
+            result = 2;
 
-        private ColumnDefinitions _columnDefinitions;
+            if (x == null)
+            {
+                if (y == null)
+                {
+                    result = 0;
+                }
+                else
+                {
+                    result = -1;
+                }
+            }
+            else
+            {
+                if (y == null)
+                {
+                    result = 1;
+                }
+            }
 
-        private RowDefinitions _rowDefinitions;
+            return (result != 2);
+        }
 
-        static Grid()
+        /// <summary>
+        /// Private version returning array of column definitions.
+        /// </summary>
+        private IReadOnlyList<DefinitionBase> DefinitionsU
         {
-            AffectsParentMeasure<Grid>(ColumnProperty, ColumnSpanProperty, RowProperty, RowSpanProperty);
-            IsSharedSizeScopeProperty.Changed.AddClassHandler<Control>(IsSharedSizeScopeChanged);
+            get { return (ExtData.DefinitionsU); }
         }
 
-        public Grid()
+        /// <summary>
+        /// Private version returning array of row definitions.
+        /// </summary>
+        private IReadOnlyList<DefinitionBase> DefinitionsV
         {
-            this.AttachedToVisualTree += Grid_AttachedToVisualTree;
-            this.DetachedFromVisualTree += Grid_DetachedFromVisualTree;
+            get { return (ExtData.DefinitionsV); }
         }
 
         /// <summary>
-        /// Gets or sets the columns definitions for the grid.
+        /// Helper accessor to layout time array of definitions.
         /// </summary>
-        public ColumnDefinitions ColumnDefinitions
+        private DefinitionBase[] TempDefinitions
         {
             get
             {
-                if (_columnDefinitions == null)
+                ExtendedData extData = ExtData;
+                int requiredLength = Math.Max(DefinitionsU.Count, DefinitionsV.Count) * 2;
+
+                if (extData.TempDefinitions == null
+                    || extData.TempDefinitions.Length < requiredLength)
                 {
-                    ColumnDefinitions = new ColumnDefinitions();
+                    WeakReference tempDefinitionsWeakRef = (WeakReference)Thread.GetData(s_tempDefinitionsDataSlot);
+                    if (tempDefinitionsWeakRef == null)
+                    {
+                        extData.TempDefinitions = new DefinitionBase[requiredLength];
+                        Thread.SetData(s_tempDefinitionsDataSlot, new WeakReference(extData.TempDefinitions));
+                    }
+                    else
+                    {
+                        extData.TempDefinitions = (DefinitionBase[])tempDefinitionsWeakRef.Target;
+                        if (extData.TempDefinitions == null
+                            || extData.TempDefinitions.Length < requiredLength)
+                        {
+                            extData.TempDefinitions = new DefinitionBase[requiredLength];
+                            tempDefinitionsWeakRef.Target = extData.TempDefinitions;
+                        }
+                    }
                 }
-
-                return _columnDefinitions;
+                return (extData.TempDefinitions);
             }
+        }
 
-            set
+        /// <summary>
+        /// Helper accessor to definition indices.
+        /// </summary>
+        private int[] DefinitionIndices
+        {
+            get
             {
-                if (_columnDefinitions != null)
+                int requiredLength = Math.Max(Math.Max(DefinitionsU.Count, DefinitionsV.Count), 1) * 2;
+
+                if (_definitionIndices == null || _definitionIndices.Length < requiredLength)
                 {
-                    throw new NotSupportedException("Reassigning ColumnDefinitions not yet implemented.");
+                    _definitionIndices = new int[requiredLength];
                 }
 
-                _columnDefinitions = value;
-                _columnDefinitions.TrackItemPropertyChanged(_ => InvalidateMeasure());
-                _columnDefinitions.CollectionChanged += (_, __) => InvalidateMeasure();
+                return _definitionIndices;
             }
         }
 
         /// <summary>
-        /// Gets or sets the row definitions for the grid.
+        /// Helper accessor to rounding errors.
         /// </summary>
-        public RowDefinitions RowDefinitions
+        private double[] RoundingErrors
         {
             get
             {
-                if (_rowDefinitions == null)
+                int requiredLength = Math.Max(DefinitionsU.Count, DefinitionsV.Count);
+
+                if (_roundingErrors == null && requiredLength == 0)
                 {
-                    RowDefinitions = new RowDefinitions();
+                    _roundingErrors = new double[1];
                 }
-
-                return _rowDefinitions;
-            }
-
-            set
-            {
-                if (_rowDefinitions != null)
+                else if (_roundingErrors == null || _roundingErrors.Length < requiredLength)
                 {
-                    throw new NotSupportedException("Reassigning RowDefinitions not yet implemented.");
+                    _roundingErrors = new double[requiredLength];
                 }
-
-                _rowDefinitions = value;
-                _rowDefinitions.TrackItemPropertyChanged(_ => InvalidateMeasure());
-                _rowDefinitions.CollectionChanged += (_, __) => InvalidateMeasure();
+                return _roundingErrors;
             }
         }
 
         /// <summary>
-        /// Gets the value of the Column attached property for a control.
+        /// Private version returning array of cells.
         /// </summary>
-        /// <param name="element">The control.</param>
-        /// <returns>The control's column.</returns>
-        public static int GetColumn(AvaloniaObject element)
+        private CellCache[] PrivateCells
         {
-            return element.GetValue(ColumnProperty);
+            get { return (ExtData.CellCachesCollection); }
         }
 
         /// <summary>
-        /// Gets the value of the ColumnSpan attached property for a control.
+        /// Convenience accessor to ValidCellsStructure bit flag.
         /// </summary>
-        /// <param name="element">The control.</param>
-        /// <returns>The control's column span.</returns>
-        public static int GetColumnSpan(AvaloniaObject element)
+        private bool CellsStructureDirty
         {
-            return element.GetValue(ColumnSpanProperty);
+            get { return (!CheckFlagsAnd(Flags.ValidCellsStructure)); }
+            set { SetFlags(!value, Flags.ValidCellsStructure); }
         }
 
         /// <summary>
-        /// Gets the value of the Row attached property for a control.
+        /// Convenience accessor to ListenToNotifications bit flag.
         /// </summary>
-        /// <param name="element">The control.</param>
-        /// <returns>The control's row.</returns>
-        public static int GetRow(AvaloniaObject element)
+        private bool ListenToNotifications
         {
-            return element.GetValue(RowProperty);
+            get { return (CheckFlagsAnd(Flags.ListenToNotifications)); }
+            set { SetFlags(value, Flags.ListenToNotifications); }
         }
 
         /// <summary>
-        /// Gets the value of the RowSpan attached property for a control.
+        /// Convenience accessor to SizeToContentU bit flag.
         /// </summary>
-        /// <param name="element">The control.</param>
-        /// <returns>The control's row span.</returns>
-        public static int GetRowSpan(AvaloniaObject element)
+        private bool SizeToContentU
         {
-            return element.GetValue(RowSpanProperty);
+            get { return (CheckFlagsAnd(Flags.SizeToContentU)); }
+            set { SetFlags(value, Flags.SizeToContentU); }
         }
 
+        /// <summary>
+        /// Convenience accessor to SizeToContentV bit flag.
+        /// </summary>
+        private bool SizeToContentV
+        {
+            get { return (CheckFlagsAnd(Flags.SizeToContentV)); }
+            set { SetFlags(value, Flags.SizeToContentV); }
+        }
 
         /// <summary>
-        /// Gets the value of the IsSharedSizeScope attached property for a control.
+        /// Convenience accessor to HasStarCellsU bit flag.
         /// </summary>
-        /// <param name="element">The control.</param>
-        /// <returns>The control's IsSharedSizeScope value.</returns>
-        public static bool GetIsSharedSizeScope(AvaloniaObject element)
+        private bool HasStarCellsU
         {
-            return element.GetValue(IsSharedSizeScopeProperty);
+            get { return (CheckFlagsAnd(Flags.HasStarCellsU)); }
+            set { SetFlags(value, Flags.HasStarCellsU); }
         }
 
         /// <summary>
-        /// Sets the value of the Column attached property for a control.
+        /// Convenience accessor to HasStarCellsV bit flag.
         /// </summary>
-        /// <param name="element">The control.</param>
-        /// <param name="value">The column value.</param>
-        public static void SetColumn(AvaloniaObject element, int value)
+        private bool HasStarCellsV
         {
-            element.SetValue(ColumnProperty, value);
+            get { return (CheckFlagsAnd(Flags.HasStarCellsV)); }
+            set { SetFlags(value, Flags.HasStarCellsV); }
         }
 
         /// <summary>
-        /// Sets the value of the ColumnSpan attached property for a control.
+        /// Convenience accessor to HasGroup3CellsInAutoRows bit flag.
         /// </summary>
-        /// <param name="element">The control.</param>
-        /// <param name="value">The column span value.</param>
-        public static void SetColumnSpan(AvaloniaObject element, int value)
+        private bool HasGroup3CellsInAutoRows
         {
-            element.SetValue(ColumnSpanProperty, value);
+            get { return (CheckFlagsAnd(Flags.HasGroup3CellsInAutoRows)); }
+            set { SetFlags(value, Flags.HasGroup3CellsInAutoRows); }
         }
 
         /// <summary>
-        /// Sets the value of the Row attached property for a control.
+        /// fp version of <c>d == 0</c>.
         /// </summary>
-        /// <param name="element">The control.</param>
-        /// <param name="value">The row value.</param>
-        public static void SetRow(AvaloniaObject element, int value)
+        /// <param name="d">Value to check.</param>
+        /// <returns><c>true</c> if d == 0.</returns>
+        private static bool _IsZero(double d)
         {
-            element.SetValue(RowProperty, value);
+            return (Math.Abs(d) < double.Epsilon);
         }
 
         /// <summary>
-        /// Sets the value of the RowSpan attached property for a control.
+        /// fp version of <c>d1 == d2</c>
         /// </summary>
-        /// <param name="element">The control.</param>
-        /// <param name="value">The row span value.</param>
-        public static void SetRowSpan(AvaloniaObject element, int value)
+        /// <param name="d1">First value to compare</param>
+        /// <param name="d2">Second value to compare</param>
+        /// <returns><c>true</c> if d1 == d2</returns>
+        private static bool _AreClose(double d1, double d2)
         {
-            element.SetValue(RowSpanProperty, value);
+            return (Math.Abs(d1 - d2) < double.Epsilon);
         }
 
         /// <summary>
-        /// Sets the value of IsSharedSizeScope property for a control.
+        /// Returns reference to extended data bag.
         /// </summary>
-        /// <param name="element">The control.</param>
-        /// <param name="value">The IsSharedSizeScope value.</param>
-        public static void SetIsSharedSizeScope(AvaloniaObject element, bool value)
+        private ExtendedData ExtData
         {
-            element.SetValue(IsSharedSizeScopeProperty, value);
+            get { return (_data); }
+        }
+
+        /// <summary>
+        /// Returns *-weight, adjusted for scale computed during Phase 1
+        /// </summary>
+        static double StarWeight(DefinitionBase def, double scale)
+        {
+            if (scale < 0.0)
+            {
+                // if one of the *-weights is Infinity, adjust the weights by mapping
+                // Infinty to 1.0 and everything else to 0.0:  the infinite items share the
+                // available space equally, everyone else gets nothing.
+                return (Double.IsPositiveInfinity(def.UserSize.Value)) ? 1.0 : 0.0;
+            }
+            else
+            {
+                return def.UserSize.Value * scale;
+            }
+        }
+
+        // Extended data instantiated on demand, for non-trivial case handling only
+        private ExtendedData _data;
+
+        // Grid validity / property caches dirtiness flags
+        private Flags _flags;
+        private GridLinesRenderer _gridLinesRenderer;
+
+        // Keeps track of definition indices.
+        int[] _definitionIndices;
+
+        // Stores unrounded values and rounding errors during layout rounding.
+        double[] _roundingErrors;
+
+        // 5 is an arbitrary constant chosen to end the measure loop
+        private const int c_layoutLoopMaxCount = 5;
+
+        private static readonly LocalDataStoreSlot s_tempDefinitionsDataSlot = Thread.AllocateDataSlot();
+        private static readonly IComparer s_spanPreferredDistributionOrderComparer = new SpanPreferredDistributionOrderComparer();
+        private static readonly IComparer s_spanMaxDistributionOrderComparer = new SpanMaxDistributionOrderComparer();
+        private static readonly IComparer s_minRatioComparer = new MinRatioComparer();
+        private static readonly IComparer s_maxRatioComparer = new MaxRatioComparer();
+        private static readonly IComparer s_starWeightComparer = new StarWeightComparer();
+
+        /// <summary>
+        /// Extended data instantiated on demand, when grid handles non-trivial case.
+        /// </summary>
+        private class ExtendedData
+        {
+            internal ColumnDefinitions ColumnDefinitions;  //  collection of column definitions (logical tree support)
+            internal RowDefinitions RowDefinitions;        //  collection of row definitions (logical tree support)
+            internal IReadOnlyList<DefinitionBase> DefinitionsU;    //  collection of column definitions used during calc
+            internal IReadOnlyList<DefinitionBase> DefinitionsV;    //  collection of row definitions used during calc
+            internal CellCache[] CellCachesCollection;              //  backing store for logical children
+            internal int CellGroup1;                                //  index of the first cell in first cell group
+            internal int CellGroup2;                                //  index of the first cell in second cell group
+            internal int CellGroup3;                                //  index of the first cell in third cell group
+            internal int CellGroup4;                                //  index of the first cell in forth cell group
+            internal DefinitionBase[] TempDefinitions;              //  temporary array used during layout for various purposes
+                                                                    //  TempDefinitions.Length == Max(definitionsU.Length, definitionsV.Length)
         }
 
         /// <summary>
-        /// Gets the result of the last column measurement.
-        /// Use this result to reduce the arrange calculation.
+        /// Grid validity / property caches dirtiness flags
         /// </summary>
-        private GridLayout.MeasureResult _columnMeasureCache;
+        [System.Flags]
+        private enum Flags
+        {
+            //
+            //  the foolowing flags let grid tracking dirtiness in more granular manner:
+            //  * Valid???Structure flags indicate that elements were added or removed.
+            //  * Valid???Layout flags indicate that layout time portion of the information
+            //    stored on the objects should be updated.
+            //
+            ValidDefinitionsUStructure = 0x00000001,
+            ValidDefinitionsVStructure = 0x00000002,
+            ValidCellsStructure = 0x00000004,
+
+            //
+            //  boolean properties state
+            //
+            ShowGridLinesPropertyValue = 0x00000100,   //  show grid lines ?
+
+            //
+            //  boolean flags
+            //
+            ListenToNotifications = 0x00001000,   //  "0" when all notifications are ignored
+            SizeToContentU = 0x00002000,   //  "1" if calculating to content in U direction
+            SizeToContentV = 0x00004000,   //  "1" if calculating to content in V direction
+            HasStarCellsU = 0x00008000,   //  "1" if at least one cell belongs to a Star column
+            HasStarCellsV = 0x00010000,   //  "1" if at least one cell belongs to a Star row
+            HasGroup3CellsInAutoRows = 0x00020000,   //  "1" if at least one cell of group 3 belongs to an Auto row
+            MeasureOverrideInProgress = 0x00040000,   //  "1" while in the context of Grid.MeasureOverride
+            ArrangeOverrideInProgress = 0x00080000,   //  "1" while in the context of Grid.ArrangeOverride
+        }
 
         /// <summary>
-        /// Gets the result of the last row measurement.
-        /// Use this result to reduce the arrange calculation.
+        /// ShowGridLines property. This property is used mostly
+        /// for simplification of visual debuggig. When it is set
+        /// to <c>true</c> grid lines are drawn to visualize location
+        /// of grid lines.
         /// </summary>
-        private GridLayout.MeasureResult _rowMeasureCache;
+        public static readonly StyledProperty<bool> ShowGridLinesProperty =
+            AvaloniaProperty.Register<Grid, bool>(nameof(ShowGridLines));
 
         /// <summary>
-        /// Gets the row layout as of the last measure.
+        /// Column property. This is an attached property.
+        /// Grid defines Column property, so that it can be set
+        /// on any element treated as a cell. Column property
+        /// specifies child's position with respect to columns.
         /// </summary>
-        private GridLayout _rowLayoutCache;
+        /// <remarks>
+        /// <para> Columns are 0 - based. In order to appear in first column, element
+        /// should have Column property set to <c>0</c>. </para>
+        /// <para> Default value for the property is <c>0</c>. </para>
+        /// </remarks>
+        public static readonly AttachedProperty<int> ColumnProperty =
+            AvaloniaProperty.RegisterAttached<Grid, Control, int>(
+                "Column",
+                defaultValue: 0,
+                validate: (_, v) =>
+                {
+                    if (v >= 0) return v;
+                    else throw new ArgumentException("Invalid Grid.Column value.");
+                });
 
         /// <summary>
-        /// Gets the column layout as of the last measure.
+        /// Row property. This is an attached property.
+        /// Grid defines Row, so that it can be set
+        /// on any element treated as a cell. Row property
+        /// specifies child's position with respect to rows.
+        /// <remarks>
+        /// <para> Rows are 0 - based. In order to appear in first row, element
+        /// should have Row property set to <c>0</c>. </para>
+        /// <para> Default value for the property is <c>0</c>. </para>
+        /// </remarks>
         /// </summary>
-        private GridLayout _columnLayoutCache;
+        public static readonly AttachedProperty<int> RowProperty =
+            AvaloniaProperty.RegisterAttached<Grid, Control, int>(
+                "Row",
+                defaultValue: 0,
+                validate: (_, v) =>
+                {
+                    if (v >= 0) return v;
+                    else throw new ArgumentException("Invalid Grid.Row value.");
+                });
 
         /// <summary>
-        /// Measures the grid.
+        /// ColumnSpan property. This is an attached property.
+        /// Grid defines ColumnSpan, so that it can be set
+        /// on any element treated as a cell. ColumnSpan property
+        /// specifies child's width with respect to columns.
+        /// Example, ColumnSpan == 2 means that child will span across two columns.
         /// </summary>
-        /// <param name="constraint">The available size.</param>
-        /// <returns>The desired size of the control.</returns>
-        protected override Size MeasureOverride(Size constraint)
-        {
-            // Situation 1/2:
-            // If the grid doesn't have any column/row definitions, it behaves like a normal panel.
-            // GridLayout supports this situation but we handle this separately for performance.
+        /// <remarks>
+        /// Default value for the property is <c>1</c>.
+        /// </remarks>
+        public static readonly AttachedProperty<int> ColumnSpanProperty =
+            AvaloniaProperty.RegisterAttached<Grid, Control, int>(
+                "ColumnSpan",
+                defaultValue: 1,
+                validate: (_, v) =>
+                {
+                    if (v >= 1) return v;
+                    else throw new ArgumentException("Invalid Grid.ColumnSpan value.");
+                });
 
-            if (ColumnDefinitions.Count == 0 && RowDefinitions.Count == 0)
-            {
-                var maxWidth = 0.0;
-                var maxHeight = 0.0;
-                foreach (var child in Children.OfType<Control>())
+        /// <summary>
+        /// RowSpan property. This is an attached property.
+        /// Grid defines RowSpan, so that it can be set
+        /// on any element treated as a cell. RowSpan property
+        /// specifies child's height with respect to row grid lines.
+        /// Example, RowSpan == 3 means that child will span across three rows.
+        /// </summary>
+        /// <remarks>
+        /// Default value for the property is <c>1</c>.
+        /// </remarks>
+        public static readonly AttachedProperty<int> RowSpanProperty =
+            AvaloniaProperty.RegisterAttached<Grid, Control, int>(
+                "RowSpan",
+                defaultValue: 1,
+                validate: (_, v) =>
                 {
-                    child.Measure(constraint);
-                    maxWidth = Math.Max(maxWidth, child.DesiredSize.Width);
-                    maxHeight = Math.Max(maxHeight, child.DesiredSize.Height);
-                }
+                    if (v >= 1) return v;
+                    else throw new ArgumentException("Invalid Grid.RowSpan value.");
+                });
 
-                maxWidth = Math.Min(maxWidth, constraint.Width);
-                maxHeight = Math.Min(maxHeight, constraint.Height);
-                return new Size(maxWidth, maxHeight);
-            }
+        /// <summary>
+        /// IsSharedSizeScope property marks scoping element for shared size.
+        /// </summary>
+        public static readonly AttachedProperty<bool> IsSharedSizeScopeProperty =
+            AvaloniaProperty.RegisterAttached<Grid, Control, bool>(
+                "IsSharedSizeScope");
 
-            // Situation 2/2:
-            // If the grid defines some columns or rows.
-            // Debug Tip:
-            //     - GridLayout doesn't hold any state, so you can drag the debugger execution
-            //       arrow back to any statements and re-run them without any side-effect.
+        /// <summary>
+        /// LayoutTimeSizeType is used internally and reflects layout-time size type.
+        /// </summary>
+        [System.Flags]
+        internal enum LayoutTimeSizeType : byte
+        {
+            None = 0x00,
+            Pixel = 0x01,
+            Auto = 0x02,
+            Star = 0x04,
+        }
 
-            var measureCache = new Dictionary<Control, Size>();
-            var (safeColumns, safeRows) = GetSafeColumnRows();
-            var columnLayout = new GridLayout(ColumnDefinitions);
-            var rowLayout = new GridLayout(RowDefinitions);
-            // Note: If a child stays in a * or Auto column/row, use constraint to measure it.
-            columnLayout.AppendMeasureConventions(safeColumns, child => MeasureOnce(child, constraint).Width);
-            rowLayout.AppendMeasureConventions(safeRows, child => MeasureOnce(child, constraint).Height);
+        /// <summary>
+        /// CellCache stored calculated values of
+        /// 1. attached cell positioning properties;
+        /// 2. size type;
+        /// 3. index of a next cell in the group;
+        /// </summary>
+        private struct CellCache
+        {
+            internal int ColumnIndex;
+            internal int RowIndex;
+            internal int ColumnSpan;
+            internal int RowSpan;
+            internal LayoutTimeSizeType SizeTypeU;
+            internal LayoutTimeSizeType SizeTypeV;
+            internal int Next;
+            internal bool IsStarU { get { return ((SizeTypeU & LayoutTimeSizeType.Star) != 0); } }
+            internal bool IsAutoU { get { return ((SizeTypeU & LayoutTimeSizeType.Auto) != 0); } }
+            internal bool IsStarV { get { return ((SizeTypeV & LayoutTimeSizeType.Star) != 0); } }
+            internal bool IsAutoV { get { return ((SizeTypeV & LayoutTimeSizeType.Auto) != 0); } }
+        }
 
-            // Calculate measurement.
-            var columnResult = columnLayout.Measure(constraint.Width);
-            var rowResult = rowLayout.Measure(constraint.Height);
+        /// <summary>
+        /// Helper class for representing a key for a span in hashtable.
+        /// </summary>
+        private class SpanKey
+        {
+            /// <summary>
+            /// Constructor.
+            /// </summary>
+            /// <param name="start">Starting index of the span.</param>
+            /// <param name="count">Span count.</param>
+            /// <param name="u"><c>true</c> for columns; <c>false</c> for rows.</param>
+            internal SpanKey(int start, int count, bool u)
+            {
+                _start = start;
+                _count = count;
+                _u = u;
+            }
 
-            // Use the results of the measurement to measure the rest of the children.
-            foreach (var child in Children.OfType<Control>())
+            /// <summary>
+            /// <see cref="object.GetHashCode"/>
+            /// </summary>
+            public override int GetHashCode()
             {
-                var (column, columnSpan) = safeColumns[child];
-                var (row, rowSpan) = safeRows[child];
-                var width = Enumerable.Range(column, columnSpan).Select(x => columnResult.LengthList[x]).Sum();
-                var height = Enumerable.Range(row, rowSpan).Select(x => rowResult.LengthList[x]).Sum();
+                int hash = (_start ^ (_count << 2));
 
-                MeasureOnce(child, new Size(width, height));
-            }
+                if (_u) hash &= 0x7ffffff;
+                else hash |= 0x8000000;
 
-            // Cache the measure result and return the desired size.
-            _columnMeasureCache = columnResult;
-            _rowMeasureCache = rowResult;
-            _rowLayoutCache = rowLayout;
-            _columnLayoutCache = columnLayout;
+                return (hash);
+            }
 
-            if (_sharedSizeHost?.ParticipatesInScope(this) ?? false)
+            /// <summary>
+            /// <see cref="object.Equals(object)"/>
+            /// </summary>
+            public override bool Equals(object obj)
             {
-                _sharedSizeHost.UpdateMeasureStatus(this, rowResult, columnResult);
+                SpanKey sk = obj as SpanKey;
+                return (sk != null
+                        && sk._start == _start
+                        && sk._count == _count
+                        && sk._u == _u);
             }
 
-            return new Size(columnResult.DesiredLength, rowResult.DesiredLength);
+            /// <summary>
+            /// Returns start index of the span.
+            /// </summary>
+            internal int Start { get { return (_start); } }
+
+            /// <summary>
+            /// Returns span count.
+            /// </summary>
+            internal int Count { get { return (_count); } }
+
+            /// <summary>
+            /// Returns <c>true</c> if this is a column span.
+            /// <c>false</c> if this is a row span.
+            /// </summary>
+            internal bool U { get { return (_u); } }
+
+            private int _start;
+            private int _count;
+            private bool _u;
+        }
 
-            // Measure each child only once.
-            // If a child has been measured, it will just return the desired size.
-            Size MeasureOnce(Control child, Size size)
+        /// <summary>
+        /// SpanPreferredDistributionOrderComparer.
+        /// </summary>
+        private class SpanPreferredDistributionOrderComparer : IComparer
+        {
+            public int Compare(object x, object y)
             {
-                if (measureCache.TryGetValue(child, out var desiredSize))
+                DefinitionBase definitionX = x as DefinitionBase;
+                DefinitionBase definitionY = y as DefinitionBase;
+
+                int result;
+
+                if (!CompareNullRefs(definitionX, definitionY, out result))
                 {
-                    return desiredSize;
+                    if (definitionX.UserSize.IsAuto)
+                    {
+                        if (definitionY.UserSize.IsAuto)
+                        {
+                            result = definitionX.MinSize.CompareTo(definitionY.MinSize);
+                        }
+                        else
+                        {
+                            result = -1;
+                        }
+                    }
+                    else
+                    {
+                        if (definitionY.UserSize.IsAuto)
+                        {
+                            result = +1;
+                        }
+                        else
+                        {
+                            result = definitionX.PreferredSize.CompareTo(definitionY.PreferredSize);
+                        }
+                    }
                 }
 
-                child.Measure(size);
-                desiredSize = child.DesiredSize;
-                measureCache[child] = desiredSize;
-                return desiredSize;
+                return result;
             }
         }
 
         /// <summary>
-        /// Arranges the grid's children.
+        /// SpanMaxDistributionOrderComparer.
         /// </summary>
-        /// <param name="finalSize">The size allocated to the control.</param>
-        /// <returns>The space taken.</returns>
-        protected override Size ArrangeOverride(Size finalSize)
+        private class SpanMaxDistributionOrderComparer : IComparer
         {
-            // Situation 1/2:
-            // If the grid doesn't have any column/row definitions, it behaves like a normal panel.
-            // GridLayout supports this situation but we handle this separately for performance.
-
-            if (ColumnDefinitions.Count == 0 && RowDefinitions.Count == 0)
+            public int Compare(object x, object y)
             {
-                foreach (var child in Children.OfType<Control>())
+                DefinitionBase definitionX = x as DefinitionBase;
+                DefinitionBase definitionY = y as DefinitionBase;
+
+                int result;
+
+                if (!CompareNullRefs(definitionX, definitionY, out result))
                 {
-                    child.Arrange(new Rect(finalSize));
+                    if (definitionX.UserSize.IsAuto)
+                    {
+                        if (definitionY.UserSize.IsAuto)
+                        {
+                            result = definitionX.SizeCache.CompareTo(definitionY.SizeCache);
+                        }
+                        else
+                        {
+                            result = +1;
+                        }
+                    }
+                    else
+                    {
+                        if (definitionY.UserSize.IsAuto)
+                        {
+                            result = -1;
+                        }
+                        else
+                        {
+                            result = definitionX.SizeCache.CompareTo(definitionY.SizeCache);
+                        }
+                    }
                 }
 
-                return finalSize;
+                return result;
             }
+        }
 
-            // Situation 2/2:
-            // If the grid defines some columns or rows.
-            // Debug Tip:
-            //     - GridLayout doesn't hold any state, so you can drag the debugger execution
-            //       arrow back to any statements and re-run them without any side-effect.
-
-            var (safeColumns, safeRows) = GetSafeColumnRows();
-            var columnLayout = _columnLayoutCache;
-            var rowLayout = _rowLayoutCache;
-
-            var rowCache = _rowMeasureCache;
-            var columnCache = _columnMeasureCache;
+        /// <summary>
+        /// StarDistributionOrderIndexComparer.
+        /// </summary>
+        private class StarDistributionOrderIndexComparer : IComparer
+        {
+            private readonly IReadOnlyList<DefinitionBase> definitions;
 
-            if (_sharedSizeHost?.ParticipatesInScope(this) ?? false)
+            internal StarDistributionOrderIndexComparer(IReadOnlyList<DefinitionBase> definitions)
             {
-                (rowCache, columnCache) = _sharedSizeHost.HandleArrange(this, _rowMeasureCache, _columnMeasureCache);
-            
-                rowCache = rowLayout.Measure(finalSize.Height, rowCache.LeanLengthList);
-                columnCache = columnLayout.Measure(finalSize.Width, columnCache.LeanLengthList);
+                Contract.Requires<NullReferenceException>(definitions != null);
+                this.definitions = definitions;
             }
 
-            // Calculate for arrange result.
-            var columnResult = columnLayout.Arrange(finalSize.Width, columnCache);
-            var rowResult = rowLayout.Arrange(finalSize.Height, rowCache);
-            // Arrange the children.
-            foreach (var child in Children.OfType<Control>())
+            public int Compare(object x, object y)
             {
-                var (column, columnSpan) = safeColumns[child];
-                var (row, rowSpan) = safeRows[child];
-                var x = Enumerable.Range(0, column).Sum(c => columnResult.LengthList[c]);
-                var y = Enumerable.Range(0, row).Sum(r => rowResult.LengthList[r]);
-                var width = Enumerable.Range(column, columnSpan).Sum(c => columnResult.LengthList[c]);
-                var height = Enumerable.Range(row, rowSpan).Sum(r => rowResult.LengthList[r]);
-                child.Arrange(new Rect(x, y, width, height));
-            }
+                int? indexX = x as int?;
+                int? indexY = y as int?;
 
-            // Assign the actual width.
-            for (var i = 0; i < ColumnDefinitions.Count; i++)
-            {
-                ColumnDefinitions[i].ActualWidth = columnResult.LengthList[i];
-            }
+                DefinitionBase definitionX = null;
+                DefinitionBase definitionY = null;
 
-            // Assign the actual height.
-            for (var i = 0; i < RowDefinitions.Count; i++)
-            {
-                RowDefinitions[i].ActualHeight = rowResult.LengthList[i];
-            }
+                if (indexX != null)
+                {
+                    definitionX = definitions[indexX.Value];
+                }
+                if (indexY != null)
+                {
+                    definitionY = definitions[indexY.Value];
+                }
+
+                int result;
+
+                if (!CompareNullRefs(definitionX, definitionY, out result))
+                {
+                    result = definitionX.SizeCache.CompareTo(definitionY.SizeCache);
+                }
 
-            // Return the render size.
-            return finalSize;
+                return result;
+            }
         }
 
         /// <summary>
-        /// Tests whether this grid belongs to a shared size scope.
+        /// DistributionOrderComparer.
         /// </summary>
-        /// <returns>True if the grid is registered in a shared size scope.</returns>
-        internal bool HasSharedSizeScope()
+        private class DistributionOrderIndexComparer : IComparer
         {
-            return _sharedSizeHost != null;
+            private readonly IReadOnlyList<DefinitionBase> definitions;
+
+            internal DistributionOrderIndexComparer(IReadOnlyList<DefinitionBase> definitions)
+            {
+                Contract.Requires<NullReferenceException>(definitions != null);
+                this.definitions = definitions;
+            }
+
+            public int Compare(object x, object y)
+            {
+                int? indexX = x as int?;
+                int? indexY = y as int?;
+
+                DefinitionBase definitionX = null;
+                DefinitionBase definitionY = null;
+
+                if (indexX != null)
+                {
+                    definitionX = definitions[indexX.Value];
+                }
+                if (indexY != null)
+                {
+                    definitionY = definitions[indexY.Value];
+                }
+
+                int result;
+
+                if (!CompareNullRefs(definitionX, definitionY, out result))
+                {
+                    double xprime = definitionX.SizeCache - definitionX.MinSizeForArrange;
+                    double yprime = definitionY.SizeCache - definitionY.MinSizeForArrange;
+                    result = xprime.CompareTo(yprime);
+                }
+
+                return result;
+            }
         }
 
         /// <summary>
-        /// Called when the SharedSizeScope for a given grid has changed.
-        /// Unregisters the grid from it's current scope and finds a new one (if any) 
+        /// RoundingErrorIndexComparer.
         /// </summary>
-        /// <remarks>
-        /// This method, while not efficient, correctly handles nested scopes, with any order of scope changes.
-        /// </remarks>
-        internal void SharedScopeChanged()
+        private class RoundingErrorIndexComparer : IComparer
         {
-            _sharedSizeHost?.UnegisterGrid(this);
-
-            _sharedSizeHost = null;
-            var scope = this.GetVisualAncestors().OfType<Control>()
-                .FirstOrDefault(c => c.GetValue(IsSharedSizeScopeProperty));
+            private readonly double[] errors;
 
-            if (scope != null)
+            internal RoundingErrorIndexComparer(double[] errors)
             {
-                _sharedSizeHost = scope.GetValue(s_sharedSizeScopeHostProperty);
-                _sharedSizeHost.RegisterGrid(this);
+                Contract.Requires<NullReferenceException>(errors != null);
+                this.errors = errors;
             }
 
-            InvalidateMeasure();
+            public int Compare(object x, object y)
+            {
+                int? indexX = x as int?;
+                int? indexY = y as int?;
+
+                int result;
+
+                if (!CompareNullRefs(indexX, indexY, out result))
+                {
+                    double errorX = errors[indexX.Value];
+                    double errorY = errors[indexY.Value];
+                    result = errorX.CompareTo(errorY);
+                }
+
+                return result;
+            }
         }
 
         /// <summary>
-        /// Callback when a grid is attached to the visual tree. Finds the innermost SharedSizeScope and registers the grid 
-        /// in it.
+        /// MinRatioComparer.
+        /// Sort by w/min (stored in MeasureSize), descending.
+        /// We query the list from the back, i.e. in ascending order of w/min.
         /// </summary>
-        /// <param name="sender">The source of the event.</param>
-        /// <param name="e">The event arguments.</param>
-        private void Grid_AttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e)
+        private class MinRatioComparer : IComparer
         {
-            var scope =
-                new Control[] { this }.Concat(this.GetVisualAncestors().OfType<Control>())
-                    .FirstOrDefault(c => c.GetValue(IsSharedSizeScopeProperty));
+            public int Compare(object x, object y)
+            {
+                DefinitionBase definitionX = x as DefinitionBase;
+                DefinitionBase definitionY = y as DefinitionBase;
 
-            if (_sharedSizeHost != null)
-                throw new AvaloniaInternalException("Shared size scope already present when attaching to visual tree!");
+                int result;
 
-            if (scope != null)
-            {
-                _sharedSizeHost = scope.GetValue(s_sharedSizeScopeHostProperty);
-                _sharedSizeHost.RegisterGrid(this);
+                if (!CompareNullRefs(definitionY, definitionX, out result))
+                {
+                    result = definitionY.MeasureSize.CompareTo(definitionX.MeasureSize);
+                }
+
+                return result;
             }
         }
 
         /// <summary>
-        /// Callback when a grid is detached from the visual tree. Unregisters the grid from its SharedSizeScope if any.
+        /// MaxRatioComparer.
+        /// Sort by w/max (stored in SizeCache), ascending.
+        /// We query the list from the back, i.e. in descending order of w/max.
         /// </summary>
-        /// <param name="sender">The source of the event.</param>
-        /// <param name="e">The event arguments.</param>
-        private void Grid_DetachedFromVisualTree(object sender, VisualTreeAttachmentEventArgs e)
+        private class MaxRatioComparer : IComparer
         {
-            _sharedSizeHost?.UnegisterGrid(this);
-            _sharedSizeHost = null;
-        }
+            public int Compare(object x, object y)
+            {
+                DefinitionBase definitionX = x as DefinitionBase;
+                DefinitionBase definitionY = y as DefinitionBase;
+
+                int result;
+
+                if (!CompareNullRefs(definitionX, definitionY, out result))
+                {
+                    result = definitionX.SizeCache.CompareTo(definitionY.SizeCache);
+                }
 
+                return result;
+            }
+        }
 
         /// <summary>
-        /// Get the safe column/columnspan and safe row/rowspan.
-        /// This method ensures that none of the children has a column/row outside the bounds of the definitions.
+        /// StarWeightComparer.
+        /// Sort by *-weight (stored in MeasureSize), ascending.
         /// </summary>
-        [Pure]
-        private (Dictionary<Control, (int index, int span)> safeColumns,
-            Dictionary<Control, (int index, int span)> safeRows) GetSafeColumnRows()
+        private class StarWeightComparer : IComparer
         {
-            var columnCount = ColumnDefinitions.Count;
-            var rowCount = RowDefinitions.Count;
-            columnCount = columnCount == 0 ? 1 : columnCount;
-            rowCount = rowCount == 0 ? 1 : rowCount;
-            var safeColumns = Children.OfType<Control>().ToDictionary(child => child,
-                child => GetSafeSpan(columnCount, GetColumn(child), GetColumnSpan(child)));
-            var safeRows = Children.OfType<Control>().ToDictionary(child => child,
-                child => GetSafeSpan(rowCount, GetRow(child), GetRowSpan(child)));
-            return (safeColumns, safeRows);
+            public int Compare(object x, object y)
+            {
+                DefinitionBase definitionX = x as DefinitionBase;
+                DefinitionBase definitionY = y as DefinitionBase;
+
+                int result;
+
+                if (!CompareNullRefs(definitionX, definitionY, out result))
+                {
+                    result = definitionX.MeasureSize.CompareTo(definitionY.MeasureSize);
+                }
+
+                return result;
+            }
         }
 
         /// <summary>
-        /// Gets the safe row/column and rowspan/columnspan for a specified range.
-        /// The user may assign row/column properties outside the bounds of the row/column count, this method coerces them inside.
+        /// MinRatioIndexComparer.
         /// </summary>
-        /// <param name="length">The row or column count.</param>
-        /// <param name="userIndex">The row or column that the user assigned.</param>
-        /// <param name="userSpan">The rowspan or columnspan that the user assigned.</param>
-        /// <returns>The safe row/column and rowspan/columnspan.</returns>
-        [Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
-        private static (int index, int span) GetSafeSpan(int length, int userIndex, int userSpan)
+        private class MinRatioIndexComparer : IComparer
         {
-            var index = userIndex;
-            var span = userSpan;
+            private readonly IReadOnlyList<DefinitionBase> definitions;
 
-            if (index < 0)
+            internal MinRatioIndexComparer(IReadOnlyList<DefinitionBase> definitions)
             {
-                span = index + span;
-                index = 0;
+                Contract.Requires<NullReferenceException>(definitions != null);
+                this.definitions = definitions;
             }
 
-            if (span <= 0)
+            public int Compare(object x, object y)
             {
-                span = 1;
-            }
+                int? indexX = x as int?;
+                int? indexY = y as int?;
 
-            if (userIndex >= length)
-            {
-                index = length - 1;
-                span = 1;
-            }
-            else if (userIndex + userSpan > length)
-            {
-                span = length - userIndex;
-            }
+                DefinitionBase definitionX = null;
+                DefinitionBase definitionY = null;
+
+                if (indexX != null)
+                {
+                    definitionX = definitions[indexX.Value];
+                }
+                if (indexY != null)
+                {
+                    definitionY = definitions[indexY.Value];
+                }
 
-            return (index, span);
+                int result;
+
+                if (!CompareNullRefs(definitionY, definitionX, out result))
+                {
+                    result = definitionY.MeasureSize.CompareTo(definitionX.MeasureSize);
+                }
+
+                return result;
+            }
         }
 
-        private static int ValidateColumn(AvaloniaObject o, int value)
+        /// <summary>
+        /// MaxRatioIndexComparer.
+        /// </summary>
+        private class MaxRatioIndexComparer : IComparer
         {
-            if (value < 0)
+            private readonly IReadOnlyList<DefinitionBase> definitions;
+
+            internal MaxRatioIndexComparer(IReadOnlyList<DefinitionBase> definitions)
             {
-                throw new ArgumentException("Invalid Grid.Column value.");
+                Contract.Requires<NullReferenceException>(definitions != null);
+                this.definitions = definitions;
             }
 
-            return value;
+            public int Compare(object x, object y)
+            {
+                int? indexX = x as int?;
+                int? indexY = y as int?;
+
+                DefinitionBase definitionX = null;
+                DefinitionBase definitionY = null;
+
+                if (indexX != null)
+                {
+                    definitionX = definitions[indexX.Value];
+                }
+                if (indexY != null)
+                {
+                    definitionY = definitions[indexY.Value];
+                }
+
+                int result;
+
+                if (!CompareNullRefs(definitionX, definitionY, out result))
+                {
+                    result = definitionX.SizeCache.CompareTo(definitionY.SizeCache);
+                }
+
+                return result;
+            }
         }
 
-        private static int ValidateRow(AvaloniaObject o, int value)
+        /// <summary>
+        /// MaxRatioIndexComparer.
+        /// </summary>
+        private class StarWeightIndexComparer : IComparer
         {
-            if (value < 0)
+            private readonly IReadOnlyList<DefinitionBase> definitions;
+
+            internal StarWeightIndexComparer(IReadOnlyList<DefinitionBase> definitions)
             {
-                throw new ArgumentException("Invalid Grid.Row value.");
+                Contract.Requires<NullReferenceException>(definitions != null);
+                this.definitions = definitions;
             }
 
-            return value;
+            public int Compare(object x, object y)
+            {
+                int? indexX = x as int?;
+                int? indexY = y as int?;
+
+                DefinitionBase definitionX = null;
+                DefinitionBase definitionY = null;
+
+                if (indexX != null)
+                {
+                    definitionX = definitions[indexX.Value];
+                }
+                if (indexY != null)
+                {
+                    definitionY = definitions[indexY.Value];
+                }
+
+                int result;
+
+                if (!CompareNullRefs(definitionX, definitionY, out result))
+                {
+                    result = definitionX.MeasureSize.CompareTo(definitionY.MeasureSize);
+                }
+
+                return result;
+            }
         }
 
         /// <summary>
-        /// Called when the value of <see cref="Grid.IsSharedSizeScopeProperty"/> changes for a control.
+        /// Helper for rendering grid lines.
         /// </summary>
-        /// <param name="source">The control that triggered the change.</param>
-        /// <param name="arg2">Change arguments.</param>
-        private static void IsSharedSizeScopeChanged(Control source, AvaloniaPropertyChangedEventArgs arg2)
+        internal class GridLinesRenderer : Control
         {
-            var shouldDispose = (arg2.OldValue is bool d) && d;
-            if (shouldDispose)
+            /// <summary>
+            /// Static initialization
+            /// </summary>
+            static GridLinesRenderer()
             {
-                var host = source.GetValue(s_sharedSizeScopeHostProperty) as SharedSizeScopeHost;
-                if (host == null)
-                    throw new AvaloniaInternalException("SharedScopeHost wasn't set when IsSharedSizeScope was true!");
-                host.Dispose();
-                source.ClearValue(s_sharedSizeScopeHostProperty);
+                var dashArray = new List<double>() { _dashLength, _dashLength };
+
+                var ds1 = new DashStyle(dashArray, 0);
+                _oddDashPen = new Pen(Brushes.Blue,
+                                      _penWidth,
+                                      lineCap: PenLineCap.Flat,
+                                      dashStyle: ds1);
+
+                var ds2 = new DashStyle(dashArray, _dashLength);
+                _evenDashPen = new Pen(Brushes.Yellow,
+                                       _penWidth,
+                                       lineCap: PenLineCap.Flat,
+                                       dashStyle: ds2);
             }
 
-            var shouldAssign = (arg2.NewValue is bool a) && a;
-            if (shouldAssign)
+            /// <summary>
+            /// UpdateRenderBounds.
+            /// </summary>
+            public override void Render(DrawingContext drawingContext)
             {
-                if (source.GetValue(s_sharedSizeScopeHostProperty) != null)
-                    throw new AvaloniaInternalException("SharedScopeHost was already set when IsSharedSizeScope is only now being set to true!");
-                source.SetValue(s_sharedSizeScopeHostProperty, new SharedSizeScopeHost());
+                var grid = this.GetVisualParent<Grid>();
+
+                if (grid == null || !grid.ShowGridLines)
+                    return;
+
+                for (int i = 1; i < grid.ColumnDefinitions.Count; ++i)
+                {
+                    DrawGridLine(
+                        drawingContext,
+                        grid.ColumnDefinitions[i].FinalOffset, 0.0,
+                        grid.ColumnDefinitions[i].FinalOffset, _lastArrangeSize.Height);
+                }
+
+                for (int i = 1; i < grid.RowDefinitions.Count; ++i)
+                {
+                    DrawGridLine(
+                        drawingContext,
+                        0.0, grid.RowDefinitions[i].FinalOffset,
+                        _lastArrangeSize.Width, grid.RowDefinitions[i].FinalOffset);
+                }
             }
 
-            // if the scope has changed, notify the descendant grids that they need to update.
-            if (source.GetVisualRoot() != null && shouldAssign || shouldDispose)
+            /// <summary>
+            /// Draw single hi-contrast line.
+            /// </summary>
+            private static void DrawGridLine(
+                DrawingContext drawingContext,
+                double startX,
+                double startY,
+                double endX,
+                double endY)
             {
-                var participatingGrids = new[] { source }.Concat(source.GetVisualDescendants()).OfType<Grid>();
-
-                foreach (var grid in participatingGrids)
-                    grid.SharedScopeChanged();
+                var start = new Point(startX, startY);
+                var end = new Point(endX, endY);
+                drawingContext.DrawLine(_oddDashPen, start, end);
+                drawingContext.DrawLine(_evenDashPen, start, end);
+            }
 
+            internal void UpdateRenderBounds(Size arrangeSize)
+            {
+                _lastArrangeSize = arrangeSize;
+                this.InvalidateVisual();
             }
+
+            private static Size _lastArrangeSize;
+            private const double _dashLength = 4.0;    //
+            private const double _penWidth = 1.0;      //
+            private static readonly Pen _oddDashPen;   //  first pen to draw dash
+            private static readonly Pen _evenDashPen;  //  second pen to draw dash
         }
     }
 }

+ 2 - 2
src/Avalonia.Controls/MenuItem.cs

@@ -339,7 +339,7 @@ namespace Avalonia.Controls
 
             var point = e.GetPointerPoint(null);
             RaiseEvent(new PointerEventArgs(PointerEnterItemEvent, this, e.Pointer, this.VisualRoot, point.Position,
-                point.Properties, e.InputModifiers));
+                e.Timestamp, point.Properties, e.InputModifiers));
         }
 
         /// <inheritdoc/>
@@ -349,7 +349,7 @@ namespace Avalonia.Controls
 
             var point = e.GetPointerPoint(null);
             RaiseEvent(new PointerEventArgs(PointerLeaveItemEvent, this, e.Pointer, this.VisualRoot, point.Position,
-                point.Properties, e.InputModifiers));
+                e.Timestamp, point.Properties, e.InputModifiers));
         }
 
         /// <summary>

+ 69 - 0
src/Avalonia.Controls/Presenters/ScrollContentPresenter.cs

@@ -2,6 +2,7 @@
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
 using System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Reactive.Disposables;
 using System.Reactive.Linq;
@@ -64,6 +65,7 @@ namespace Avalonia.Controls.Presenters
         private Vector _offset;
         private IDisposable _logicalScrollSubscription;
         private Size _viewport;
+        private Dictionary<int, Vector> _activeLogicalGestureScrolls;
 
         /// <summary>
         /// Initializes static members of the <see cref="ScrollContentPresenter"/> class.
@@ -81,6 +83,7 @@ namespace Avalonia.Controls.Presenters
         public ScrollContentPresenter()
         {
             AddHandler(RequestBringIntoViewEvent, BringIntoViewRequested);
+            AddHandler(Gestures.ScrollGestureEvent, OnScrollGesture);
 
             this.GetObservable(ChildProperty).Subscribe(UpdateScrollableSubscription);
         }
@@ -227,6 +230,72 @@ namespace Avalonia.Controls.Presenters
             return finalSize;
         }
 
+        // Arbitrary chosen value, probably need to ask ILogicalScrollable
+        private const int LogicalScrollItemSize = 50;
+        private void OnScrollGesture(object sender, ScrollGestureEventArgs e)
+        {
+            if (Extent.Height > Viewport.Height || Extent.Width > Viewport.Width)
+            {
+                var scrollable = Child as ILogicalScrollable;
+                bool isLogical = scrollable?.IsLogicalScrollEnabled == true;
+
+                double x = Offset.X;
+                double y = Offset.Y;
+
+                Vector delta = default;
+                if (isLogical)
+                    _activeLogicalGestureScrolls?.TryGetValue(e.Id, out delta);
+                delta += e.Delta;
+                
+                if (Extent.Height > Viewport.Height)
+                {
+                    double dy;
+                    if (isLogical)
+                    {
+                        var logicalUnits = delta.Y / LogicalScrollItemSize;
+                        delta = delta.WithY(delta.Y - logicalUnits * LogicalScrollItemSize);
+                        dy = logicalUnits * scrollable.ScrollSize.Height;
+                    }
+                    else
+                        dy = delta.Y;
+
+
+                    y += dy;
+                    y = Math.Max(y, 0);
+                    y = Math.Min(y, Extent.Height - Viewport.Height);
+                }
+
+                if (Extent.Width > Viewport.Width)
+                {
+                    double dx;
+                    if (isLogical)
+                    {
+                        var logicalUnits = delta.X / LogicalScrollItemSize;
+                        delta = delta.WithX(delta.X - logicalUnits * LogicalScrollItemSize);
+                        dx = logicalUnits * scrollable.ScrollSize.Width;
+                    }
+                    else
+                        dx = delta.X;
+                    x += dx;
+                    x = Math.Max(x, 0);
+                    x = Math.Min(x, Extent.Width - Viewport.Width);
+                }
+
+                if (isLogical)
+                {
+                    if (_activeLogicalGestureScrolls == null)
+                        _activeLogicalGestureScrolls = new Dictionary<int, Vector>();
+                    _activeLogicalGestureScrolls[e.Id] = delta;
+                }
+
+                Offset = new Vector(x, y);
+                e.Handled = true;
+            }
+        }
+
+        private void OnScrollGestureEnded(object sender, ScrollGestureEndedEventArgs e) 
+            => _activeLogicalGestureScrolls?.Remove(e.Id);
+
         /// <inheritdoc/>
         protected override void OnPointerWheelChanged(PointerWheelEventArgs e)
         {

+ 46 - 18
src/Avalonia.Controls/Presenters/TextPresenter.cs

@@ -19,6 +19,15 @@ namespace Avalonia.Controls.Presenters
         public static readonly StyledProperty<char> PasswordCharProperty =
             AvaloniaProperty.Register<TextPresenter, char>(nameof(PasswordChar));
 
+        public static readonly StyledProperty<IBrush> SelectionBrushProperty =
+            AvaloniaProperty.Register<TextPresenter, IBrush>(nameof(SelectionBrushProperty));
+
+        public static readonly StyledProperty<IBrush> SelectionForegroundBrushProperty =
+            AvaloniaProperty.Register<TextPresenter, IBrush>(nameof(SelectionForegroundBrushProperty));
+
+        public static readonly StyledProperty<IBrush> CaretBrushProperty =
+            AvaloniaProperty.Register<TextPresenter, IBrush>(nameof(CaretBrushProperty));
+
         public static readonly DirectProperty<TextPresenter, int> SelectionStartProperty =
             TextBox.SelectionStartProperty.AddOwner<TextPresenter>(
                 o => o.SelectionStart,
@@ -34,11 +43,12 @@ namespace Avalonia.Controls.Presenters
         private int _selectionStart;
         private int _selectionEnd;
         private bool _caretBlink;
-        private IBrush _highlightBrush;
-        
+
         static TextPresenter()
         {
-            AffectsRender<TextPresenter>(PasswordCharProperty);
+            AffectsRender<TextPresenter>(PasswordCharProperty,
+                SelectionBrushProperty, SelectionForegroundBrushProperty,
+                SelectionStartProperty, SelectionEndProperty);
         }
 
         public TextPresenter()
@@ -79,6 +89,24 @@ namespace Avalonia.Controls.Presenters
             set => SetValue(PasswordCharProperty, value);
         }
 
+        public IBrush SelectionBrush
+        {
+            get => GetValue(SelectionBrushProperty);
+            set => SetValue(SelectionBrushProperty, value);
+        }
+
+        public IBrush SelectionForegroundBrush
+        {
+            get => GetValue(SelectionForegroundBrushProperty);
+            set => SetValue(SelectionForegroundBrushProperty, value);
+        }
+        
+        public IBrush CaretBrush
+        {
+            get => GetValue(CaretBrushProperty);
+            set => SetValue(CaretBrushProperty, value);
+        }
+
         public int SelectionStart
         {
             get
@@ -129,14 +157,9 @@ namespace Avalonia.Controls.Presenters
 
                 var rects = FormattedText.HitTestTextRange(start, length);
 
-                if (_highlightBrush == null)
-                {
-                    _highlightBrush = (IBrush)this.FindResource("HighlightBrush");
-                }
-
                 foreach (var rect in rects)
                 {
-                    context.FillRectangle(_highlightBrush, rect);
+                    context.FillRectangle(SelectionBrush, rect);
                 }
             }
 
@@ -144,16 +167,21 @@ namespace Avalonia.Controls.Presenters
 
             if (selectionStart == selectionEnd)
             {
-                var backgroundColor = (((Control)TemplatedParent).GetValue(BackgroundProperty) as SolidColorBrush)?.Color;
-                var caretBrush = Brushes.Black;
+                var caretBrush = CaretBrush;
 
-                if (backgroundColor.HasValue)
+                if (caretBrush is null)
                 {
-                    byte red = (byte)~(backgroundColor.Value.R);
-                    byte green = (byte)~(backgroundColor.Value.G);
-                    byte blue = (byte)~(backgroundColor.Value.B);
-
-                    caretBrush = new SolidColorBrush(Color.FromRgb(red, green, blue));
+                    var backgroundColor = (((Control)TemplatedParent).GetValue(BackgroundProperty) as SolidColorBrush)?.Color;
+                    if (backgroundColor.HasValue)
+                    {
+                        byte red = (byte)~(backgroundColor.Value.R);
+                        byte green = (byte)~(backgroundColor.Value.G);
+                        byte blue = (byte)~(backgroundColor.Value.B);
+
+                        caretBrush = new SolidColorBrush(Color.FromRgb(red, green, blue));
+                    }
+                    else
+                        caretBrush = Brushes.Black;
                 }
 
                 if (_caretBlink)
@@ -252,7 +280,7 @@ namespace Avalonia.Controls.Presenters
             {
                 result.Spans = new[]
                 {
-                    new FormattedTextStyleSpan(start, length, foregroundBrush: Brushes.White),
+                    new FormattedTextStyleSpan(start, length, SelectionForegroundBrush),
                 };
             }
 

+ 2 - 1
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@@ -54,7 +54,8 @@ namespace Avalonia.Controls.Primitives
                 nameof(SelectedIndex),
                 o => o.SelectedIndex,
                 (o, v) => o.SelectedIndex = v,
-                unsetValue: -1);
+                unsetValue: -1,
+                defaultBindingMode: BindingMode.TwoWay);
 
         /// <summary>
         /// Defines the <see cref="SelectedItem"/> property.

+ 8 - 8
src/Avalonia.Controls/RowDefinition.cs

@@ -29,7 +29,7 @@ namespace Avalonia.Controls
         /// <summary>
         /// Initializes a new instance of the <see cref="RowDefinition"/> class.
         /// </summary>
-        public RowDefinition()
+        public RowDefinition() 
         {
         }
 
@@ -38,7 +38,7 @@ namespace Avalonia.Controls
         /// </summary>
         /// <param name="value">The height of the row.</param>
         /// <param name="type">The height unit of the column.</param>
-        public RowDefinition(double value, GridUnitType type)
+        public RowDefinition(double value, GridUnitType type) 
         {
             Height = new GridLength(value, type);
         }
@@ -47,7 +47,7 @@ namespace Avalonia.Controls
         /// Initializes a new instance of the <see cref="RowDefinition"/> class.
         /// </summary>
         /// <param name="height">The height of the column.</param>
-        public RowDefinition(GridLength height)
+        public RowDefinition(GridLength height) 
         {
             Height = height;
         }
@@ -55,11 +55,7 @@ namespace Avalonia.Controls
         /// <summary>
         /// Gets the actual calculated height of the row.
         /// </summary>
-        public double ActualHeight
-        {
-            get;
-            internal set;
-        }
+        public double ActualHeight => Parent?.GetFinalRowDefinitionHeight(Index) ?? 0d;
 
         /// <summary>
         /// Gets or sets the maximum height of the row in DIPs.
@@ -87,5 +83,9 @@ namespace Avalonia.Controls
             get { return GetValue(HeightProperty); }
             set { SetValue(HeightProperty, value); }
         }
+
+        internal override GridLength UserSizeValueCache => this.Height;
+        internal override double UserMinSizeValueCache => this.MinHeight;
+        internal override double UserMaxSizeValueCache => this.MaxHeight;
     }
 }

+ 2 - 3
src/Avalonia.Controls/RowDefinitions.cs

@@ -9,14 +9,13 @@ namespace Avalonia.Controls
     /// <summary>
     /// A collection of <see cref="RowDefinition"/>s.
     /// </summary>
-    public class RowDefinitions : AvaloniaList<RowDefinition>
+    public class RowDefinitions : DefinitionList<RowDefinition>
     {
         /// <summary>
         /// Initializes a new instance of the <see cref="RowDefinitions"/> class.
         /// </summary>
-        public RowDefinitions()
+        public RowDefinitions() : base()
         {
-            ResetBehavior = ResetBehavior.Remove;
         }
 
         /// <summary>

+ 17 - 1
src/Avalonia.Controls/TabControl.cs

@@ -1,6 +1,7 @@
 // Copyright (c) The Avalonia Project. All rights reserved.
 // Licensed under the MIT license. See licence.md file in the project root for full license information.
 
+using System.Linq;
 using Avalonia.Controls.Generators;
 using Avalonia.Controls.Mixins;
 using Avalonia.Controls.Presenters;
@@ -8,6 +9,7 @@ using Avalonia.Controls.Primitives;
 using Avalonia.Controls.Templates;
 using Avalonia.Input;
 using Avalonia.Layout;
+using Avalonia.VisualTree;
 
 namespace Avalonia.Controls
 {
@@ -166,10 +168,24 @@ namespace Avalonia.Controls
         {
             base.OnPointerPressed(e);
 
-            if (e.MouseButton == MouseButton.Left)
+            if (e.MouseButton == MouseButton.Left && e.Pointer.Type == PointerType.Mouse)
             {
                 e.Handled = UpdateSelectionFromEventSource(e.Source);
             }
         }
+
+        protected override void OnPointerReleased(PointerReleasedEventArgs e)
+        {
+            if (e.MouseButton == MouseButton.Left && e.Pointer.Type != PointerType.Mouse)
+            {
+                var container = GetContainerFromEventSource(e.Source);
+                if (container != null
+                    && container.GetVisualsAt(e.GetPosition(container))
+                        .Any(c => container == c || container.IsVisualAncestorOf(c)))
+                {
+                    e.Handled = UpdateSelectionFromEventSource(e.Source);
+                }
+            }
+        }
     }
 }

+ 30 - 8
src/Avalonia.Controls/TextBox.cs

@@ -38,6 +38,15 @@ namespace Avalonia.Controls
         public static readonly StyledProperty<char> PasswordCharProperty =
             AvaloniaProperty.Register<TextBox, char>(nameof(PasswordChar));
 
+        public static readonly StyledProperty<IBrush> SelectionBrushProperty =
+            AvaloniaProperty.Register<TextBox, IBrush>(nameof(SelectionBrushProperty));
+
+        public static readonly StyledProperty<IBrush> SelectionForegroundBrushProperty =
+            AvaloniaProperty.Register<TextBox, IBrush>(nameof(SelectionForegroundBrushProperty));
+
+        public static readonly StyledProperty<IBrush> CaretBrushProperty =
+            AvaloniaProperty.Register<TextBox, IBrush>(nameof(CaretBrushProperty));
+
         public static readonly DirectProperty<TextBox, int> SelectionStartProperty =
             AvaloniaProperty.RegisterDirect<TextBox, int>(
                 nameof(SelectionStart),
@@ -169,6 +178,24 @@ namespace Avalonia.Controls
             set => SetValue(PasswordCharProperty, value);
         }
 
+        public IBrush SelectionBrush
+        {
+            get => GetValue(SelectionBrushProperty);
+            set => SetValue(SelectionBrushProperty, value);
+        }
+
+        public IBrush SelectionForegroundBrush
+        {
+            get => GetValue(SelectionForegroundBrushProperty);
+            set => SetValue(SelectionForegroundBrushProperty, value);
+        }
+
+        public IBrush CaretBrush
+        {
+            get => GetValue(CaretBrushProperty);
+            set => SetValue(CaretBrushProperty, value);
+        }
+
         public int SelectionStart
         {
             get
@@ -287,16 +314,11 @@ namespace Avalonia.Controls
             {
                 DecideCaretVisibility();
             }
-
-            e.Handled = true;
         }
 
         private void DecideCaretVisibility()
         {
-            if (!IsReadOnly)
-                _presenter?.ShowCaret();
-            else
-                _presenter?.HideCaret();
+            _presenter.ShowCaret();
         }
 
         protected override void OnLostFocus(RoutedEventArgs e)
@@ -456,7 +478,7 @@ namespace Avalonia.Controls
                 movement = true;
                 selection = false;
                 handled = true;
-                
+
             }
             else if (Match(keymap.MoveCursorToTheEndOfLine))
             {
@@ -485,7 +507,7 @@ namespace Avalonia.Controls
                 movement = true;
                 selection = true;
                 handled = true;
-                
+
             }
             else if (Match(keymap.MoveCursorToTheEndOfLineWithSelection))
             {

+ 0 - 705
src/Avalonia.Controls/Utils/GridLayout.cs

@@ -1,705 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Diagnostics;
-using System.Globalization;
-using System.Linq;
-using System.Runtime.CompilerServices;
-using Avalonia.Layout;
-using JetBrains.Annotations;
-
-namespace Avalonia.Controls.Utils
-{
-    /// <summary>
-    /// Contains algorithms that can help to measure and arrange a Grid.
-    /// </summary>
-    internal class GridLayout
-    {
-        /// <summary>
-        /// Initialize a new <see cref="GridLayout"/> instance from the column definitions.
-        /// The instance doesn't care about whether the definitions are rows or columns.
-        /// It will not calculate the column or row differently.
-        /// </summary>
-        internal GridLayout([NotNull] ColumnDefinitions columns)
-        {
-            if (columns == null) throw new ArgumentNullException(nameof(columns));
-            _conventions = columns.Count == 0
-                ? new List<LengthConvention> { new LengthConvention() }
-                : columns.Select(x => new LengthConvention(x.Width, x.MinWidth, x.MaxWidth)).ToList();
-        }
-
-        /// <summary>
-        /// Initialize a new <see cref="GridLayout"/> instance from the row definitions.
-        /// The instance doesn't care about whether the definitions are rows or columns.
-        /// It will not calculate the column or row differently.
-        /// </summary>
-        internal GridLayout([NotNull] RowDefinitions rows)
-        {
-            if (rows == null) throw new ArgumentNullException(nameof(rows));
-            _conventions = rows.Count == 0
-                ? new List<LengthConvention> { new LengthConvention() }
-                : rows.Select(x => new LengthConvention(x.Height, x.MinHeight, x.MaxHeight)).ToList();
-        }
-
-        /// <summary>
-        /// Gets the layout tolerance. If any length offset is less than this value, we will treat them the same.
-        /// </summary>
-        private const double LayoutTolerance = 1.0 / 256.0;
-
-        /// <summary>
-        /// Gets all the length conventions that come from column/row definitions.
-        /// These conventions provide cell limitations, such as the expected pixel length, the min/max pixel length and the * count.
-        /// </summary>
-        [NotNull]
-        private readonly List<LengthConvention> _conventions;
-
-        /// <summary>
-        /// Gets all the length conventions that come from the grid children.
-        /// </summary>
-        [NotNull]
-        private readonly List<AdditionalLengthConvention> _additionalConventions =
-            new List<AdditionalLengthConvention>();
-
-        /// <summary>
-        /// Appending these elements into the convention list helps lay them out according to their desired sizes.
-        /// <para/>
-        /// Some elements are not only in a single grid cell, they have one or more column/row spans,
-        /// and these elements may affect the grid layout especially the measuring procedure.<para/>
-        /// Append these elements into the convention list can help to layout them correctly through
-        /// their desired size. Only a small subset of children need to be measured before layout starts
-        /// and they will be called via the<paramref name="getDesiredLength"/> callback.
-        /// </summary>
-        /// <typeparam name="T">The grid children type.</typeparam>
-        /// <param name="source">
-        /// Contains the safe column/row index and its span.
-        /// Notice that we will not verify whether the range is in the column/row count,
-        /// so you should get the safe column/row info first.
-        /// </param>
-        /// <param name="getDesiredLength">
-        /// This callback will be called if the <see cref="GridLayout"/> thinks that a child should be
-        /// measured first. Usually, these are the children that have the * or Auto length.
-        /// </param>
-        internal void AppendMeasureConventions<T>([NotNull] IDictionary<T, (int index, int span)> source,
-            [NotNull] Func<T, double> getDesiredLength)
-        {
-            if (source == null) throw new ArgumentNullException(nameof(source));
-            if (getDesiredLength == null) throw new ArgumentNullException(nameof(getDesiredLength));
-
-            // M1/7. Find all the Auto and * length columns/rows. (M1/7 means the 1st procedure of measurement.)
-            // Only these columns/rows' layout can be affected by the child desired size.
-            // 
-            // Find all columns/rows that have Auto or * length. We'll measure the children in advance.
-            // Only these kind of columns/rows will affect the Grid layout.
-            // Please note:
-            // - If the column / row has Auto length, the Grid.DesiredSize and the column width
-            //   will be affected by the child's desired size.
-            // - If the column / row has* length, the Grid.DesiredSize will be affected by the
-            //   child's desired size but the column width not.
-
-            //               +-----------------------------------------------------------+
-            //               |  *  |  A  |  *  |  P  |  A  |  *  |  P  |     *     |  *  |
-            //               +-----------------------------------------------------------+
-            // _conventions: | min | max |     |           | min |     |  min max  | max |
-            // _additionalC:                   |<-   desired   ->|     |< desired >|
-            // _additionalC:       |< desired >|           |<-        desired          ->|
-
-            // 寻找所有行列范围中包含 Auto 和 * 的元素,使用全部可用尺寸提前测量。
-            // 因为只有这部分元素的布局才会被 Grid 的子元素尺寸影响。
-            // 请注意:
-            // - Auto 长度的行列必定会受到子元素布局影响,会影响到行列的布局长度和 Grid 本身的 DesiredSize;
-            // - 而对于 * 长度,只有 Grid.DesiredSize 会受到子元素布局影响,而行列长度不会受影响。
-
-            // Find all the Auto and * length columns/rows.
-            var found = new Dictionary<T, (int index, int span)>();
-            for (var i = 0; i < _conventions.Count; i++)
-            {
-                var index = i;
-                var convention = _conventions[index];
-                if (convention.Length.IsAuto || convention.Length.IsStar)
-                {
-                    foreach (var pair in source.Where(x =>
-                        x.Value.index <= index && index < x.Value.index + x.Value.span))
-                    {
-                        found[pair.Key] = pair.Value;
-                    }
-                }
-            }
-
-            // Append these layout into the additional convention list.
-            foreach (var pair in found)
-            {
-                var t = pair.Key;
-                var (index, span) = pair.Value;
-                var desiredLength = getDesiredLength(t);
-                if (Math.Abs(desiredLength) > LayoutTolerance)
-                {
-                    _additionalConventions.Add(new AdditionalLengthConvention(index, span, desiredLength));
-                }
-            }
-        }
-
-        /// <summary>
-        /// Run measure procedure according to the <paramref name="containerLength"/> and gets the <see cref="MeasureResult"/>.
-        /// </summary>
-        /// <param name="containerLength">
-        /// The container length. Usually, it is the constraint of the <see cref="Layoutable.MeasureOverride"/> method.
-        /// </param>
-        /// <param name="conventions">
-        /// Overriding conventions that allows the algorithm to handle external inputa 
-        /// </param>
-        /// <returns>
-        /// The measured result that containing the desired size and all the column/row lengths.
-        /// </returns>
-        [NotNull, Pure]
-        internal MeasureResult Measure(double containerLength, IReadOnlyList<LengthConvention> conventions = null)
-        {
-            // Prepare all the variables that this method needs to use.
-            conventions = conventions ?? _conventions.Select(x => x.Clone()).ToList();
-            var starCount = conventions.Where(x => x.Length.IsStar).Sum(x => x.Length.Value);
-            var aggregatedLength = 0.0;
-            double starUnitLength;
-
-            // M2/7. Aggregate all the pixel lengths. Then we can get the remaining length by `containerLength - aggregatedLength`.
-            // We mark the aggregated length as "fix" because we can completely determine their values. Same as below.
-            //
-            // +-----------------------------------------------------------+
-            // |  *  |  A  |  *  |  P  |  A  |  *  |  P  |     *     |  *  |
-            // +-----------------------------------------------------------+
-            //                   |#fix#|           |#fix#|
-            //
-            // 将全部的固定像素长度的行列长度累加。这样,containerLength - aggregatedLength 便能得到剩余长度。
-            // 我们会将所有能够确定下长度的行列标记为 fix。下同。
-            // 请注意:
-            // - 我们并没有直接从 containerLength 一直减下去,而是使用 aggregatedLength 进行累加,是因为无穷大相减得到的是 NaN,不利于后续计算。
-
-            aggregatedLength += conventions.Where(x => x.Length.IsAbsolute).Sum(x => x.Length.Value);
-
-            // M3/7. Fix all the * lengths that have reached the minimum.
-            //
-            // +-----------------------------------------------------------+
-            // |  *  |  A  |  *  |  P  |  A  |  *  |  P  |     *     |  *  |
-            // +-----------------------------------------------------------+
-            // | min | max |     |           | min |     |  min max  | max |
-            //                   | fix |     |#fix#| fix |
-
-            var shouldTestStarMin = true;
-            while (shouldTestStarMin)
-            {
-                // Calculate the unit * length to estimate the length of each column/row that has * length.
-                // Under this estimated length, check if there is a minimum value that has a length less than its constraint.
-                // If there is such a *, then fix the size of this cell, and then loop it again until there is no * that can be constrained by the minimum value.
-                //
-                // 计算单位 * 的长度,以便预估出每一个 * 行列的长度。
-                // 在此预估的长度下,从前往后寻找是否存在某个 * 长度已经小于其约束的最小值。
-                // 如果发现存在这样的 *,那么将此单元格的尺寸固定下来(Fix),然后循环重来,直至再也没有能被最小值约束的 *。
-                var @fixed = false;
-                starUnitLength = (containerLength - aggregatedLength) / starCount;
-                foreach (var convention in conventions.Where(x => x.Length.IsStar))
-                {
-                    var (star, min) = (convention.Length.Value, convention.MinLength);
-                    var starLength = star * starUnitLength;
-                    if (starLength < min)
-                    {
-                        convention.Fix(min);
-                        starLength = min;
-                        aggregatedLength += starLength;
-                        starCount -= star;
-                        @fixed = true;
-                        break;
-                    }
-                }
-
-                shouldTestStarMin = @fixed;
-            }
-
-            // M4/7. Determine the absolute pixel size of all columns/rows that have an Auto length.
-            //
-            // +-----------------------------------------------------------+
-            // |  *  |  A  |  *  |  P  |  A  |  *  |  P  |     *     |  *  |
-            // +-----------------------------------------------------------+
-            // | min | max |     |           | min |     |  min max  | max |
-            //       |#fix#|     | fix |#fix#| fix | fix |
-
-            var shouldTestAuto = true;
-            while (shouldTestAuto)
-            {
-                var @fixed = false;
-                starUnitLength = (containerLength - aggregatedLength) / starCount;
-                for (var i = 0; i < conventions.Count; i++)
-                {
-                    var convention = conventions[i];
-                    if (!convention.Length.IsAuto)
-                    {
-                        continue;
-                    }
-
-                    var more = ApplyAdditionalConventionsForAuto(conventions, i, starUnitLength);
-                    convention.Fix(more);
-                    aggregatedLength += more;
-                    @fixed = true;
-                    break;
-                }
-
-                shouldTestAuto = @fixed;
-            }
-
-            // M5/7. Expand the stars according to the additional conventions (usually the child desired length).
-            // We can't fix this kind of length, so we just mark them as desired (des).
-            //
-            // +-----------------------------------------------------------+
-            // |  *  |  A  |  *  |  P  |  A  |  *  |  P  |     *     |  *  |
-            // +-----------------------------------------------------------+
-            // | min | max |     |           | min |     |  min max  | max |
-            // |#des#| fix |#des#| fix | fix | fix | fix |   #des#   |#des#|
-
-            var (minLengths, desiredStarMin) = AggregateAdditionalConventionsForStars(conventions);
-            aggregatedLength += desiredStarMin;
-
-            // M6/7. Determine the desired length of the grid for current container length. Its value is stored in desiredLength.
-            // Assume if the container has infinite length, the grid desired length is stored in greedyDesiredLength.
-            //
-            // +-----------------------------------------------------------+
-            // |  *  |  A  |  *  |  P  |  A  |  *  |  P  |     *     |  *  |
-            // +-----------------------------------------------------------+
-            // | min | max |     |           | min |     |  min max  | max |
-            // |#des#| fix |#des#| fix | fix | fix | fix |   #des#   |#des#|
-            // Note: This table will be stored as the intermediate result into the MeasureResult and it will be reused by Arrange procedure.
-            // 
-            // desiredLength = Math.Max(0.0, des + fix + des + fix + fix + fix + fix + des + des)
-            // greedyDesiredLength = des + fix + des + fix + fix + fix + fix + des + des
-
-            var desiredLength = containerLength - aggregatedLength >= 0.0 ? aggregatedLength : containerLength;
-            var greedyDesiredLength = aggregatedLength;
-
-            // M7/7. Expand all the rest stars. These stars have no conventions or only have
-            // max value they can be expanded from zero to constraint.
-            //
-            // +-----------------------------------------------------------+
-            // |  *  |  A  |  *  |  P  |  A  |  *  |  P  |     *     |  *  |
-            // +-----------------------------------------------------------+
-            // | min | max |     |           | min |     |  min max  | max |
-            // |#fix#| fix |#fix#| fix | fix | fix | fix |   #fix#   |#fix#|
-            // Note: This table will be stored as the final result into the MeasureResult.
-
-            var dynamicConvention = ExpandStars(conventions, containerLength);
-            Clip(dynamicConvention, containerLength);
-
-            // Returns the measuring result.
-            return new MeasureResult(containerLength, desiredLength, greedyDesiredLength,
-                conventions, dynamicConvention, minLengths);
-        }
-
-        /// <summary>
-        /// Run arrange procedure according to the <paramref name="measure"/> and gets the <see cref="ArrangeResult"/>.
-        /// </summary>
-        /// <param name="finalLength">
-        /// The container length. Usually, it is the finalSize of the <see cref="Layoutable.ArrangeOverride"/> method.
-        /// </param>
-        /// <param name="measure">
-        /// The result that the measuring procedure returns. If it is null, a new measure procedure will run.
-        /// </param>
-        /// <returns>
-        /// The measured result that containing the desired size and all the column/row length.
-        /// </returns>
-        [NotNull, Pure]
-        public ArrangeResult Arrange(double finalLength, [CanBeNull] MeasureResult measure)
-        {
-            measure = measure ?? Measure(finalLength);
-
-            // If the arrange final length does not equal to the measure length, we should measure again.
-            if (finalLength - measure.ContainerLength > LayoutTolerance)
-            {
-                // If the final length is larger, we will rerun the whole measure.
-                measure = Measure(finalLength, measure.LeanLengthList);
-            }
-            else if (finalLength - measure.ContainerLength < -LayoutTolerance)
-            {
-                // If the final length is smaller, we measure the M6/6 procedure only.
-                var dynamicConvention = ExpandStars(measure.LeanLengthList, finalLength);
-                measure = new MeasureResult(finalLength, measure.DesiredLength, measure.GreedyDesiredLength,
-                    measure.LeanLengthList, dynamicConvention, measure.MinLengths);
-            }
-
-            return new ArrangeResult(measure.LengthList);
-        }
-
-        /// <summary>
-        /// Use the <see cref="_additionalConventions"/> to calculate the fixed length of the Auto column/row.
-        /// </summary>
-        /// <param name="conventions">The convention list that all the * with minimum length are fixed.</param>
-        /// <param name="index">The column/row index that should be fixed.</param>
-        /// <param name="starUnitLength">The unit * length for the current rest length.</param>
-        /// <returns>The final length of the Auto length column/row.</returns>
-        [Pure]
-        private double ApplyAdditionalConventionsForAuto(IReadOnlyList<LengthConvention> conventions,
-            int index, double starUnitLength)
-        {
-            // 1. Calculate all the * length with starUnitLength.
-            // 2. Exclude all the fixed length and all the * length.
-            // 3. Compare the rest of the desired length and the convention.
-            // +-----------------+
-            // |  *  |  A  |  *  |
-            // +-----------------+
-            // | exl |     | exl |
-            // |< desired >|
-            //       |< desired >|
-
-            var more = 0.0;
-            foreach (var additional in _additionalConventions)
-            {
-                // If the additional convention's last column/row contains the Auto column/row, try to determine the Auto column/row length.
-                if (index == additional.Index + additional.Span - 1)
-                {
-                    var min = Enumerable.Range(additional.Index, additional.Span)
-                        .Select(x =>
-                        {
-                            var c = conventions[x];
-                            if (c.Length.IsAbsolute) return c.Length.Value;
-                            if (c.Length.IsStar) return c.Length.Value * starUnitLength;
-                            return 0.0;
-                        }).Sum();
-                    more = Math.Max(additional.Min - min, more);
-                }
-            }
-
-            return Math.Min(conventions[index].MaxLength, more);
-        }
-
-        /// <summary>
-        /// Calculate the total desired length of all the * length.
-        /// Bug Warning:
-        /// - The behavior of this method is undefined! Different UI Frameworks have different behaviors.
-        /// - We ignore all the span columns/rows and just take single cells into consideration.
-        /// </summary>
-        /// <param name="conventions">All the conventions that have almost been fixed except the rest *.</param>
-        /// <returns>The total desired length of all the * length.</returns>
-        [Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
-        private (List<double>, double) AggregateAdditionalConventionsForStars(
-            IReadOnlyList<LengthConvention> conventions)
-        {
-            // 1. Determine all one-span column's desired widths or row's desired heights.
-            // 2. Order the multi-span conventions by its last index
-            //    (Notice that the sorted data is much smaller than the source.)
-            // 3. Determine each multi-span last index by calculating the maximum desired size.
-
-            // Before we determine the behavior of this method, we just aggregate the one-span * columns.
-
-            var fixedLength = conventions.Where(x => x.Length.IsAbsolute).Sum(x => x.Length.Value);
-
-            // Prepare a lengthList variable indicating the fixed length of each column/row.
-            var lengthList = conventions.Select(x => x.Length.IsAbsolute ? x.Length.Value : 0.0).ToList();
-            foreach (var group in _additionalConventions
-                .Where(x => x.Span == 1 && conventions[x.Index].Length.IsStar)
-                .ToLookup(x => x.Index))
-            {
-                lengthList[group.Key] = Math.Max(lengthList[group.Key], group.Max(x => x.Min));
-            }
-
-            // Now the lengthList is fixed by every one-span columns/rows.
-            // Then we should determine the multi-span column's/row's length.
-            foreach (var group in _additionalConventions
-                .Where(x => x.Span > 1)
-                .ToLookup(x => x.Index + x.Span - 1)
-                // Order the multi-span columns/rows by last index.
-                .OrderBy(x => x.Key))
-            {
-                var length = group.Max(x => x.Min - Enumerable.Range(x.Index, x.Span - 1).Sum(r => lengthList[r]));
-                lengthList[group.Key] = Math.Max(lengthList[group.Key], length > 0 ? length : 0);
-            }
-
-            return (lengthList, lengthList.Sum() - fixedLength);
-        }
-
-        /// <summary>
-        /// This method implements the last procedure (M7/7) of measure.
-        /// It expands all the * length to the fixed length according to the <paramref name="constraint"/>.
-        /// </summary>
-        /// <param name="conventions">All the conventions that have almost been fixed except the remaining *.</param>
-        /// <param name="constraint">The container length.</param>
-        /// <returns>The final pixel length list.</returns>
-        [Pure]
-        private static List<double> ExpandStars(IEnumerable<LengthConvention> conventions, double constraint)
-        {
-            // Initial.
-            var dynamicConvention = conventions.Select(x => x.Clone()).ToList();
-            constraint -= dynamicConvention.Where(x => x.Length.IsAbsolute).Sum(x => x.Length.Value);
-            var starUnitLength = 0.0;
-
-            // M6/6.
-            if (constraint >= 0)
-            {
-                var starCount = dynamicConvention.Where(x => x.Length.IsStar).Sum(x => x.Length.Value);
-
-                var shouldTestStarMax = true;
-                while (shouldTestStarMax)
-                {
-                    var @fixed = false;
-                    starUnitLength = constraint / starCount;
-                    foreach (var convention in dynamicConvention.Where(x =>
-                        x.Length.IsStar && !double.IsPositiveInfinity(x.MaxLength)))
-                    {
-                        var (star, max) = (convention.Length.Value, convention.MaxLength);
-                        var starLength = star * starUnitLength;
-                        if (starLength > max)
-                        {
-                            convention.Fix(max);
-                            starLength = max;
-                            constraint -= starLength;
-                            starCount -= star;
-                            @fixed = true;
-                            break;
-                        }
-                    }
-
-                    shouldTestStarMax = @fixed;
-                }
-            }
-
-            Debug.Assert(dynamicConvention.All(x => !x.Length.IsAuto));
-
-            var starUnit = starUnitLength;
-            var result = dynamicConvention.Select(x =>
-            {
-                if (x.Length.IsStar)
-                {
-                    return double.IsInfinity(starUnit) ? double.PositiveInfinity : starUnit * x.Length.Value;
-                }
-
-                return x.Length.Value;
-            }).ToList();
-
-            return result;
-        }
-
-        /// <summary>
-        /// If the container length is not infinity. It may be not enough to contain all the columns/rows.
-        /// We should clip the columns/rows that have been out of the container bounds.
-        /// Note: This method may change the items value of <paramref name="lengthList"/>.
-        /// </summary>
-        /// <param name="lengthList">A list of all the column widths and row heights with a fixed pixel length</param>
-        /// <param name="constraint">the container length. It can be positive infinity.</param>
-        private static void Clip([NotNull] IList<double> lengthList, double constraint)
-        {
-            if (double.IsInfinity(constraint))
-            {
-                return;
-            }
-
-            var measureLength = 0.0;
-            for (var i = 0; i < lengthList.Count; i++)
-            {
-                var length = lengthList[i];
-                if (constraint - measureLength > length)
-                {
-                    measureLength += length;
-                }
-                else
-                {
-                    lengthList[i] = constraint - measureLength;
-                    measureLength = constraint;
-                }
-            }
-        }
-
-        /// <summary>
-        /// Contains the convention of each column/row.
-        /// This is mostly the same as <see cref="RowDefinition"/> or <see cref="ColumnDefinition"/>.
-        /// We use this because we can treat the column and the row the same.
-        /// </summary>
-        [DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
-        internal class LengthConvention : ICloneable
-        {
-            /// <summary>
-            /// Initialize a new instance of <see cref="LengthConvention"/>.
-            /// </summary>
-            public LengthConvention()
-            {
-                Length = new GridLength(1.0, GridUnitType.Star);
-                MinLength = 0.0;
-                MaxLength = double.PositiveInfinity;
-            }
-
-            /// <summary>
-            /// Initialize a new instance of <see cref="LengthConvention"/>.
-            /// </summary>
-            public LengthConvention(GridLength length, double minLength, double maxLength)
-            {
-                Length = length;
-                MinLength = minLength;
-                MaxLength = maxLength;
-                if (length.IsAbsolute)
-                {
-                    _isFixed = true;
-                }
-            }
-
-            /// <summary>
-            /// Gets the <see cref="GridLength"/> of a column or a row.
-            /// </summary>
-            internal GridLength Length { get; private set; }
-
-            /// <summary>
-            /// Gets the minimum convention for a column or a row.
-            /// </summary>
-            internal double MinLength { get; }
-
-            /// <summary>
-            /// Gets the maximum convention for a column or a row.
-            /// </summary>
-            internal double MaxLength { get; }
-
-            /// <summary>
-            /// Fix the <see cref="LengthConvention"/>.
-            /// If all columns/rows are fixed, we can get the size of all columns/rows in pixels.
-            /// </summary>
-            /// <param name="pixel">
-            /// The pixel length that should be used to fix the convention.
-            /// </param>
-            /// <exception cref="InvalidOperationException">
-            /// If the convention is pixel length, this exception will throw.
-            /// </exception>
-            public void Fix(double pixel)
-            {
-                if (_isFixed)
-                {
-                    throw new InvalidOperationException("Cannot fix the length convention if it is fixed.");
-                }
-
-                Length = new GridLength(pixel);
-                _isFixed = true;
-            }
-
-            /// <summary>
-            /// Gets a value that indicates whether this convention is fixed.
-            /// </summary>
-            private bool _isFixed;
-
-            /// <summary>
-            /// Helps the debugger to display the intermediate column/row calculation result.
-            /// </summary>
-            private string DebuggerDisplay =>
-                $"{(_isFixed ? Length.Value.ToString(CultureInfo.InvariantCulture) : (Length.GridUnitType == GridUnitType.Auto ? "Auto" : $"{Length.Value}*"))}, ∈[{MinLength}, {MaxLength}]";
-
-            /// <inheritdoc />
-            object ICloneable.Clone() => Clone();
-
-            /// <summary>
-            /// Get a deep copy of this convention list.
-            /// We need this because we want to store some intermediate states.
-            /// </summary>
-            internal LengthConvention Clone() => new LengthConvention(Length, MinLength, MaxLength);
-        }
-
-        /// <summary>
-        /// Contains the convention that comes from the grid children.
-        /// Some children span multiple columns or rows, so even a simple column/row can have multiple conventions.
-        /// </summary>
-        [DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")]
-        internal struct AdditionalLengthConvention
-        {
-            /// <summary>
-            /// Initialize a new instance of <see cref="AdditionalLengthConvention"/>.
-            /// </summary>
-            public AdditionalLengthConvention(int index, int span, double min)
-            {
-                Index = index;
-                Span = span;
-                Min = min;
-            }
-
-            /// <summary>
-            /// Gets the start index of this additional convention.
-            /// </summary>
-            public int Index { get; }
-
-            /// <summary>
-            /// Gets the span of this additional convention.
-            /// </summary>
-            public int Span { get; }
-
-            /// <summary>
-            /// Gets the minimum length of this additional convention.
-            /// This value is usually provided by the child's desired length.
-            /// </summary>
-            public double Min { get; }
-
-            /// <summary>
-            /// Helps the debugger to display the intermediate column/row calculation result.
-            /// </summary>
-            private string DebuggerDisplay =>
-                $"{{{string.Join(",", Enumerable.Range(Index, Span))}}}, ∈[{Min},∞)";
-        }
-
-        /// <summary>
-        /// Stores the result of the measuring procedure.
-        /// This result can be used to measure children and assign the desired size.
-        /// Passing this result to <see cref="Arrange"/> can reduce calculation.
-        /// </summary>
-        [DebuggerDisplay("{" + nameof(LengthList) + ",nq}")]
-        internal class MeasureResult
-        {
-            /// <summary>
-            /// Initialize a new instance of <see cref="MeasureResult"/>.
-            /// </summary>
-            internal MeasureResult(double containerLength, double desiredLength, double greedyDesiredLength,
-                IReadOnlyList<LengthConvention> leanConventions, IReadOnlyList<double> expandedConventions, IReadOnlyList<double> minLengths)
-            {
-                ContainerLength = containerLength;
-                DesiredLength = desiredLength;
-                GreedyDesiredLength = greedyDesiredLength;
-                LeanLengthList = leanConventions;
-                LengthList = expandedConventions;
-                MinLengths = minLengths;
-            }
-
-            /// <summary>
-            /// Gets the container length for this result.
-            /// This property will be used by <see cref="Arrange"/> to determine whether to measure again or not.
-            /// </summary>
-            public double ContainerLength { get; }
-
-            /// <summary>
-            /// Gets the desired length of this result.
-            /// Just return this value as the desired size in <see cref="Layoutable.MeasureOverride"/>.
-            /// </summary>
-            public double DesiredLength { get; }
-
-            /// <summary>
-            /// Gets the desired length if the container has infinite length.
-            /// </summary>
-            public double GreedyDesiredLength { get; }
-
-            /// <summary>
-            /// Contains the column/row calculation intermediate result.
-            /// This value is used by <see cref="Arrange"/> for reducing repeat calculation.
-            /// </summary>
-            public IReadOnlyList<LengthConvention> LeanLengthList { get; }
-
-            /// <summary>
-            /// Gets the length list for each column/row.
-            /// </summary>
-            public IReadOnlyList<double> LengthList { get; }
-            public IReadOnlyList<double> MinLengths { get; }
-        }
-
-        /// <summary>
-        /// Stores the result of the measuring procedure.
-        /// This result can be used to arrange children and assign the render size.
-        /// </summary>
-        [DebuggerDisplay("{" + nameof(LengthList) + ",nq}")]
-        internal class ArrangeResult
-        {
-            /// <summary>
-            /// Initialize a new instance of <see cref="ArrangeResult"/>.
-            /// </summary>
-            internal ArrangeResult(IReadOnlyList<double> lengthList)
-            {
-                LengthList = lengthList;
-            }
-
-            /// <summary>
-            /// Gets the length list for each column/row.
-            /// </summary>
-            public IReadOnlyList<double> LengthList { get; }
-        }
-    }
-}

+ 0 - 651
src/Avalonia.Controls/Utils/SharedSizeScopeHost.cs

@@ -1,651 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Collections.Specialized;
-using System.ComponentModel;
-using System.Diagnostics;
-using System.Linq;
-using System.Reactive.Disposables;
-using System.Reactive.Subjects;
-using Avalonia.Collections;
-using Avalonia.Controls.Utils;
-using Avalonia.Layout;
-using Avalonia.VisualTree;
-
-namespace Avalonia.Controls
-{
-    /// <summary>
-    /// Shared size scope implementation.
-    /// Shares the size information between participating grids.
-    /// An instance of this class is attached to every <see cref="Control"/> that has its
-    /// IsSharedSizeScope property set to true.
-    /// </summary>
-    internal sealed class SharedSizeScopeHost : IDisposable
-    {
-        private enum MeasurementState
-        {
-            Invalidated,
-            Measuring,
-            Cached
-        }
-
-        /// <summary>
-        /// Class containing the measured rows/columns for a single grid.
-        /// Monitors changes to the row/column collections as well as the SharedSizeGroup changes
-        /// for the individual items in those collections.
-        /// Notifies the <see cref="SharedSizeScopeHost"/> of SharedSizeGroup changes.
-        /// </summary>
-        private sealed class MeasurementCache : IDisposable
-        {
-            readonly CompositeDisposable _subscriptions;
-            readonly Subject<(string, string, MeasurementResult)> _groupChanged = new Subject<(string, string, MeasurementResult)>();
-
-            public ISubject<(string oldName, string newName, MeasurementResult result)> GroupChanged => _groupChanged;
-
-            public MeasurementCache(Grid grid)
-            {
-                Grid = grid;
-                Results = grid.RowDefinitions.Cast<DefinitionBase>()
-                    .Concat(grid.ColumnDefinitions)
-                    .Select(d => new MeasurementResult(grid, d))
-                    .ToList();
-
-                grid.RowDefinitions.CollectionChanged += DefinitionsCollectionChanged;
-                grid.ColumnDefinitions.CollectionChanged += DefinitionsCollectionChanged;
-
-
-                _subscriptions = new CompositeDisposable(
-                    Disposable.Create(() => grid.RowDefinitions.CollectionChanged -= DefinitionsCollectionChanged),
-                    Disposable.Create(() => grid.ColumnDefinitions.CollectionChanged -= DefinitionsCollectionChanged),
-                    grid.RowDefinitions.TrackItemPropertyChanged(DefinitionPropertyChanged),
-                    grid.ColumnDefinitions.TrackItemPropertyChanged(DefinitionPropertyChanged));
-
-            }
-
-            // method to be hooked up once RowDefinitions/ColumnDefinitions collections can be replaced on a grid
-            private void DefinitionsChanged(object sender, AvaloniaPropertyChangedEventArgs e)
-            {
-                // route to collection changed as a Reset.
-                DefinitionsCollectionChanged(null, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
-            }
-
-            private void DefinitionPropertyChanged(Tuple<object, PropertyChangedEventArgs> propertyChanged)
-            {
-                if (propertyChanged.Item2.PropertyName == nameof(DefinitionBase.SharedSizeGroup))
-                {
-                    var result = Results.Single(mr => ReferenceEquals(mr.Definition, propertyChanged.Item1));
-                    var oldName = result.SizeGroup?.Name;
-                    var newName = (propertyChanged.Item1 as DefinitionBase).SharedSizeGroup;
-                    _groupChanged.OnNext((oldName, newName, result));
-                }
-            }
-
-            private void DefinitionsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
-            {
-                int offset = 0;
-                if (sender is ColumnDefinitions)
-                    offset = Grid.RowDefinitions.Count;
-
-                var newItems = e.NewItems?.OfType<DefinitionBase>().Select(db => new MeasurementResult(Grid, db)).ToList() ?? new List<MeasurementResult>();
-                var oldItems = e.OldStartingIndex >= 0 
-                                    ? Results.GetRange(e.OldStartingIndex + offset, e.OldItems.Count) 
-                                    : new List<MeasurementResult>();
-
-                void NotifyNewItems()
-                {
-                    foreach (var item in newItems)
-                    {
-                        if (string.IsNullOrEmpty(item.Definition.SharedSizeGroup))
-                            continue;
-
-                        _groupChanged.OnNext((null, item.Definition.SharedSizeGroup, item));
-                    }
-                }
-
-                void NotifyOldItems()
-                {
-                    foreach (var item in oldItems)
-                    {
-                        if (string.IsNullOrEmpty(item.Definition.SharedSizeGroup))
-                            continue;
-
-                        _groupChanged.OnNext((item.Definition.SharedSizeGroup, null, item));
-                    }
-                }
-
-                switch (e.Action)
-                {
-                    case NotifyCollectionChangedAction.Add:
-                        Results.InsertRange(e.NewStartingIndex + offset, newItems);
-                        NotifyNewItems();
-                        break;
-
-                    case NotifyCollectionChangedAction.Remove:
-                        Results.RemoveRange(e.OldStartingIndex + offset, oldItems.Count);
-                        NotifyOldItems();
-                        break;
-
-                    case NotifyCollectionChangedAction.Move:
-                        Results.RemoveRange(e.OldStartingIndex + offset, oldItems.Count);
-                        Results.InsertRange(e.NewStartingIndex + offset, oldItems);
-                        break;
-
-                    case NotifyCollectionChangedAction.Replace:
-                        Results.RemoveRange(e.OldStartingIndex + offset, oldItems.Count);
-                        Results.InsertRange(e.NewStartingIndex + offset, newItems);
-
-                        NotifyOldItems();
-                        NotifyNewItems();
-
-                        break;
-
-                    case NotifyCollectionChangedAction.Reset:
-                        oldItems = Results;
-                        newItems = Results = Grid.RowDefinitions.Cast<DefinitionBase>()
-                            .Concat(Grid.ColumnDefinitions)
-                            .Select(d => new MeasurementResult(Grid, d))
-                            .ToList();
-                        NotifyOldItems();
-                        NotifyNewItems();
-
-                        break;
-                }
-            }
-
-
-            /// <summary>
-            /// Updates the Results collection with Grid Measure results. 
-            /// </summary>
-            /// <param name="rowResult">Result of the GridLayout.Measure method for the RowDefinitions in the grid.</param>
-            /// <param name="columnResult">Result of the GridLayout.Measure method for the ColumnDefinitions in the grid.</param>
-            public void UpdateMeasureResult(GridLayout.MeasureResult rowResult, GridLayout.MeasureResult columnResult)
-            {
-                MeasurementState = MeasurementState.Cached;
-                for (int i = 0; i < Grid.RowDefinitions.Count; i++)
-                {
-                    Results[i].MeasuredResult = rowResult.LengthList[i];
-                    Results[i].MinLength = rowResult.MinLengths[i];
-                }
-
-                for (int i = 0; i < Grid.ColumnDefinitions.Count; i++)
-                {
-                    Results[i + Grid.RowDefinitions.Count].MeasuredResult = columnResult.LengthList[i];
-                    Results[i + Grid.RowDefinitions.Count].MinLength = columnResult.MinLengths[i];
-                }
-            }
-
-            /// <summary>
-            /// Clears the measurement cache, in preparation for the Measure pass.
-            /// </summary>
-            public void InvalidateMeasure()
-            {
-                var newItems = new List<MeasurementResult>();
-                var oldItems = new List<MeasurementResult>();
-
-                MeasurementState = MeasurementState.Invalidated;
-
-                Results.ForEach(r =>
-                {
-                    r.MeasuredResult = double.NaN;
-                    r.SizeGroup?.Reset();
-                });
-            }
-
-            /// <summary>
-            /// Clears the <see cref="IObservable{T}"/> subscriptions.
-            /// </summary>
-            public void Dispose()
-            {
-                _subscriptions.Dispose();
-                _groupChanged.OnCompleted();
-            }
-
-            /// <summary>
-            /// Gets the <see cref="Grid"/> for which this cache has been created.
-            /// </summary>
-            public Grid Grid { get; }
-            
-            /// <summary>
-            /// Gets the <see cref="MeasurementState"/> of this cache.
-            /// </summary>
-            public MeasurementState MeasurementState { get; private set; }
-
-            /// <summary>
-            /// Gets the list of <see cref="MeasurementResult"/> instances.
-            /// </summary>
-            /// <remarks>
-            /// The list is a 1-1 map of the concatenation of RowDefinitions and ColumnDefinitions
-            /// </remarks>
-            public List<MeasurementResult> Results { get; private set; }
-        }
-
-
-        /// <summary>
-        /// Class containing the Measure result for a single Row/Column in a grid.
-        /// </summary>
-        private class MeasurementResult
-        {
-            public MeasurementResult(Grid owningGrid, DefinitionBase definition)
-            {
-                OwningGrid = owningGrid;
-                Definition = definition;
-                MeasuredResult = double.NaN;
-            }
-
-            /// <summary>
-            /// Gets the <see cref="RowDefinition"/>/<see cref="ColumnDefinition"/> related to this <see cref="MeasurementResult"/>
-            /// </summary>
-            public DefinitionBase Definition { get; }
-            
-            /// <summary>
-            /// Gets or sets the actual result of the Measure operation for this column.
-            /// </summary>
-            public double MeasuredResult { get; set; }
-            
-            /// <summary>
-            /// Gets or sets the Minimum constraint for a Row/Column - relevant for star Rows/Columns in unconstrained grids.
-            /// </summary>
-            public double MinLength { get; set; }
-            
-            /// <summary>
-            /// Gets or sets the <see cref="Group"/> that this result belongs to.
-            /// </summary>
-            public Group SizeGroup { get; set; }
-            
-            /// <summary>
-            /// Gets the Grid that is the parent of the Row/Column
-            /// </summary>
-            public Grid OwningGrid { get; }
-
-            /// <summary>
-            /// Calculates the effective length that this Row/Column wishes to enforce in the SharedSizeGroup.
-            /// </summary>
-            /// <returns>A tuple of length and the priority in the shared size group.</returns>
-            public (double length, int priority) GetPriorityLength()
-            {
-                var length = (Definition as ColumnDefinition)?.Width ?? ((RowDefinition)Definition).Height;
-
-                if (length.IsAbsolute)
-                    return (MeasuredResult, 1);
-                if (length.IsAuto)
-                    return (MeasuredResult, 2);
-                if (MinLength > 0)
-                    return (MinLength, 3);
-                return (MeasuredResult, 4);
-            }
-        }
-
-        /// <summary>
-        /// Visitor class used to gather the final length for a given SharedSizeGroup.
-        /// </summary>
-        /// <remarks>
-        /// The values are applied according to priorities defined in <see cref="MeasurementResult.GetPriorityLength"/>.
-        /// </remarks>
-        private class LentgthGatherer
-        {
-            /// <summary>
-            /// Gets the final Length to be applied to every Row/Column in a SharedSizeGroup
-            /// </summary>
-            public double Length { get; private set; }
-            private int gatheredPriority = 6;
-
-            /// <summary>
-            /// Visits the <paramref name="result"/> applying the result of <see cref="MeasurementResult.GetPriorityLength"/> to its internal cache.
-            /// </summary>
-            /// <param name="result">The <see cref="MeasurementResult"/> instance to visit.</param>
-            public void Visit(MeasurementResult result)
-            {
-                var (length, priority) = result.GetPriorityLength();
-
-                if (gatheredPriority < priority)
-                    return;
-
-                gatheredPriority = priority;
-                if (gatheredPriority == priority)
-                {
-                    Length = Math.Max(length,Length);
-                }
-                else
-                {
-                    Length = length;
-                }
-            }
-        }
-
-        /// <summary>
-        /// Representation of a SharedSizeGroup, containing Rows/Columns with the same SharedSizeGroup property value.
-        /// </summary>
-        private class Group
-        {
-            private double? cachedResult;
-            private List<MeasurementResult> _results = new List<MeasurementResult>(); 
-
-            /// <summary>
-            /// Gets the name of the SharedSizeGroup.
-            /// </summary>
-            public string Name { get; }
-
-            public Group(string name)
-            {
-                Name = name;
-            }
-
-            /// <summary>
-            /// Gets the collection of the <see cref="MeasurementResult"/> instances.
-            /// </summary>
-            public IReadOnlyList<MeasurementResult> Results => _results;
-
-            /// <summary>
-            /// Gets the final, calculated length for all Rows/Columns in the SharedSizeGroup.
-            /// </summary>
-            public double CalculatedLength => (cachedResult ?? (cachedResult = Gather())).Value;
-
-            /// <summary>
-            /// Clears the previously cached result in preparation for measurement.
-            /// </summary>
-            public void Reset()
-            {
-                cachedResult = null;
-            }
-
-            /// <summary>
-            /// Ads a measurement result to this group and sets it's <see cref="MeasurementResult.SizeGroup"/> property
-            /// to this instance.
-            /// </summary>
-            /// <param name="result">The <see cref="MeasurementResult"/> to include in this group.</param>
-            public void Add(MeasurementResult result)
-            {
-                if (_results.Contains(result))
-                    throw new AvaloniaInternalException(
-                        $"SharedSizeScopeHost: Invalid call to Group.Add - The SharedSizeGroup {Name} already contains the passed result");
-
-                result.SizeGroup = this;
-                _results.Add(result);
-            }
-
-            /// <summary>
-            /// Removes the measurement result from this group and clears its <see cref="MeasurementResult.SizeGroup"/> value.
-            /// </summary>
-            /// <param name="result">The <see cref="MeasurementResult"/> to clear.</param>
-            public void Remove(MeasurementResult result)
-            {
-                if (!_results.Contains(result))
-                    throw new AvaloniaInternalException(
-                        $"SharedSizeScopeHost: Invalid call to Group.Remove - The SharedSizeGroup {Name} does not contain the passed result");
-                result.SizeGroup = null;
-                _results.Remove(result);
-            }
-
-
-            private double Gather()
-            {
-                var visitor = new LentgthGatherer();
-
-                _results.ForEach(visitor.Visit);
-
-                return visitor.Length;
-            }
-        }
-
-        private readonly AvaloniaList<MeasurementCache> _measurementCaches = new AvaloniaList<MeasurementCache>();
-        private readonly Dictionary<string, Group> _groups = new Dictionary<string, Group>();
-        private bool _invalidating;
-
-        /// <summary>
-        /// Removes the SharedSizeScope and notifies all affected grids of the change.
-        /// </summary>
-        public void Dispose()
-        {
-            while (_measurementCaches.Any())
-                _measurementCaches[0].Grid.SharedScopeChanged();
-        }
-
-        /// <summary>
-        /// Registers the grid in this SharedSizeScope, to be called when the grid is added to the visual tree. 
-        /// </summary>
-        /// <param name="toAdd">The <see cref="Grid"/> to add to this scope.</param>
-        internal void RegisterGrid(Grid toAdd)
-        {
-            if (_measurementCaches.Any(mc => ReferenceEquals(mc.Grid, toAdd)))
-                throw new AvaloniaInternalException("SharedSizeScopeHost: tried to register a grid twice!");
-
-            var cache = new MeasurementCache(toAdd);
-            _measurementCaches.Add(cache);
-            AddGridToScopes(cache);
-        }
-
-        /// <summary>
-        /// Removes the registration for a grid in this SharedSizeScope.
-        /// </summary>
-        /// <param name="toRemove">The <see cref="Grid"/> to remove.</param>
-        internal void UnegisterGrid(Grid toRemove)
-        {
-            var cache = _measurementCaches.FirstOrDefault(mc => ReferenceEquals(mc.Grid, toRemove));
-            if (cache == null)
-                throw new AvaloniaInternalException("SharedSizeScopeHost: tried to unregister a grid that wasn't registered before!");
-
-            _measurementCaches.Remove(cache);
-            RemoveGridFromScopes(cache);
-            cache.Dispose();
-        }
-
-        /// <summary>
-        /// Helper method to check if a grid needs to forward its Mesure results to, and requrest Arrange results from this scope.
-        /// </summary>
-        /// <param name="toCheck">The <see cref="Grid"/> that should be checked.</param>
-        /// <returns>True if the grid should forward its calls.</returns>
-        internal bool ParticipatesInScope(Grid toCheck)
-        {
-            return _measurementCaches.FirstOrDefault(mc => ReferenceEquals(mc.Grid, toCheck))
-                                    ?.Results.Any(r => r.SizeGroup != null) ?? false;
-        }
-
-        /// <summary>
-        /// Notifies the SharedSizeScope that a grid had requested its measurement to be invalidated.
-        /// Forwards the same call to all affected grids in this scope.
-        /// </summary>
-        /// <param name="grid">The <see cref="Grid"/> that had it's Measure invalidated.</param>
-        internal void InvalidateMeasure(Grid grid)
-        {
-            // prevent stack overflow
-            if (_invalidating)
-                return;
-            _invalidating = true;
-
-            InvalidateMeasureImpl(grid);
-
-            _invalidating = false;
-        }
-
-        /// <summary>
-        /// Updates the measurement cache with the results of the <paramref name="grid"/> measurement pass.
-        /// </summary>
-        /// <param name="grid">The <see cref="Grid"/> that has been measured.</param>
-        /// <param name="rowResult">Measurement result for the grid's <see cref="RowDefinitions"/></param>
-        /// <param name="columnResult">Measurement result for the grid's <see cref="ColumnDefinitions"/></param>
-        internal void UpdateMeasureStatus(Grid grid, GridLayout.MeasureResult rowResult, GridLayout.MeasureResult columnResult)
-        {
-            var cache = _measurementCaches.FirstOrDefault(mc => ReferenceEquals(mc.Grid, grid));
-            if (cache == null)
-                throw new AvaloniaInternalException("SharedSizeScopeHost: Attempted to update measurement status for a grid that wasn't registered!");
-
-            cache.UpdateMeasureResult(rowResult, columnResult);
-        }
-
-        /// <summary>
-        /// Calculates the measurement result including the impact of any SharedSizeGroups that might affect this grid.
-        /// </summary>
-        /// <param name="grid">The <see cref="Grid"/> that is being Arranged</param>
-        /// <param name="rowResult">The <paramref name="grid"/>'s cached measurement result.</param>
-        /// <param name="columnResult">The <paramref name="grid"/>'s cached measurement result.</param>
-        /// <returns>Row and column measurement result updated with the SharedSizeScope constraints.</returns>
-        internal (GridLayout.MeasureResult, GridLayout.MeasureResult) HandleArrange(Grid grid, GridLayout.MeasureResult rowResult, GridLayout.MeasureResult columnResult)
-        {
-            return (
-                 Arrange(grid.RowDefinitions, rowResult),
-                 Arrange(grid.ColumnDefinitions, columnResult)
-                 );
-        }
-
-        /// <summary>
-        /// Invalidates the measure of all grids affected by the SharedSizeGroups contained within.
-        /// </summary>
-        /// <param name="grid">The <see cref="Grid"/> that is being invalidated.</param>
-        private void InvalidateMeasureImpl(Grid grid)
-        {
-            var cache = _measurementCaches.FirstOrDefault(mc => ReferenceEquals(mc.Grid, grid));
-
-            if (cache == null)
-                throw new AvaloniaInternalException(
-                    $"SharedSizeScopeHost: InvalidateMeasureImpl - called with a grid not present in the internal cache");
-
-            // already invalidated the cache, early out.
-            if (cache.MeasurementState == MeasurementState.Invalidated)
-                return;
-
-            // we won't calculate, so we should not invalidate.
-            if (!ParticipatesInScope(grid))
-                return;
-
-            cache.InvalidateMeasure();
-
-            // maybe there is a condition to only call arrange on some of the calls?
-            grid.InvalidateMeasure();
-
-            // find all the scopes within the invalidated grid
-            var scopeNames = cache.Results
-                                  .Where(mr => mr.SizeGroup != null)
-                                  .Select(mr => mr.SizeGroup.Name)
-                                  .Distinct();
-            // find all grids related to those scopes
-            var otherGrids = scopeNames.SelectMany(sn => _groups[sn].Results)
-                                       .Select(r => r.OwningGrid)
-                                       .Where(g => g.IsMeasureValid)
-                                       .Distinct();
-
-            // invalidate them as well
-            foreach (var otherGrid in otherGrids)
-            {
-                InvalidateMeasureImpl(otherGrid);
-            }
-        }
-
-        /// <summary>
-        /// <see cref="IObserver{T}"/> callback notifying the scope that a <see cref="MeasurementResult"/> has changed its
-        /// SharedSizeGroup
-        /// </summary>
-        /// <param name="change">Old and New name (either can be null) of the SharedSizeGroup, as well as the result.</param>
-        private void SharedGroupChanged((string oldName, string newName, MeasurementResult result) change)
-        {
-            RemoveFromGroup(change.oldName, change.result);
-            AddToGroup(change.newName, change.result);
-        }
-
-        /// <summary>
-        /// Handles the impact of SharedSizeGroups on the Arrange of <see cref="RowDefinitions"/>/<see cref="ColumnDefinitions"/>
-        /// </summary>
-        /// <param name="definitions">Rows/Columns that were measured</param>
-        /// <param name="measureResult">The initial measurement result.</param>
-        /// <returns>Modified measure result</returns>
-        private GridLayout.MeasureResult Arrange(IReadOnlyList<DefinitionBase> definitions, GridLayout.MeasureResult measureResult)
-        {
-            var conventions = measureResult.LeanLengthList.ToList();
-            var lengths = measureResult.LengthList.ToList();
-            var desiredLength = 0.0;
-            for (int i = 0; i < definitions.Count; i++)
-            {
-                var definition = definitions[i];
-
-                // for empty SharedSizeGroups pass on unmodified result.
-                if (string.IsNullOrEmpty(definition.SharedSizeGroup))
-                {
-                    desiredLength += measureResult.LengthList[i];
-                    continue;
-                }
-
-                var group = _groups[definition.SharedSizeGroup];
-                // Length calculated over all Definitions participating in a SharedSizeGroup.
-                var length = group.CalculatedLength;
-
-                conventions[i] = new GridLayout.LengthConvention(
-                    new GridLength(length),
-                    measureResult.LeanLengthList[i].MinLength,
-                    measureResult.LeanLengthList[i].MaxLength
-                );
-                lengths[i] = length;
-                desiredLength += length;
-            }
-
-            return new GridLayout.MeasureResult(
-                    measureResult.ContainerLength,
-                    desiredLength,
-                    measureResult.GreedyDesiredLength,//??
-                    conventions,
-                    lengths,
-                    measureResult.MinLengths);
-        }
-
-        /// <summary>
-        /// Adds all measurement results for a grid to their repsective scopes.
-        /// </summary>
-        /// <param name="cache">The <see cref="MeasurementCache"/> for a grid to be added.</param>
-        private void AddGridToScopes(MeasurementCache cache)
-        {
-            cache.GroupChanged.Subscribe(SharedGroupChanged);
-
-            foreach (var result in cache.Results)
-            {
-                var scopeName = result.Definition.SharedSizeGroup;
-                AddToGroup(scopeName, result);
-            }
-        }
-
-        /// <summary>
-        /// Handles adding the <see cref="MeasurementResult"/> to a SharedSizeGroup.
-        /// Does nothing for empty SharedSizeGroups.
-        /// </summary>
-        /// <param name="scopeName">The name (can be null or empty) of the group to add the <paramref name="result"/> to.</param>
-        /// <param name="result">The <see cref="MeasurementResult"/> to add to a scope.</param>
-        private void AddToGroup(string scopeName, MeasurementResult result)
-        {
-            if (string.IsNullOrEmpty(scopeName))
-                return;
-
-            if (!_groups.TryGetValue(scopeName, out var group))
-                _groups.Add(scopeName, group = new Group(scopeName));
-
-            group.Add(result);
-        }
-
-        /// <summary>
-        /// Removes all measurement results for a grid from their respective scopes.
-        /// </summary>
-        /// <param name="cache">The <see cref="MeasurementCache"/> for a grid to be removed.</param>
-        private void RemoveGridFromScopes(MeasurementCache cache)
-        {
-            foreach (var result in cache.Results)
-            {
-                var scopeName = result.Definition.SharedSizeGroup;
-                RemoveFromGroup(scopeName, result);
-            }
-        }
-
-        /// <summary>
-        /// Handles removing the <see cref="MeasurementResult"/> from a SharedSizeGroup.
-        /// Does nothing for empty SharedSizeGroups.
-        /// </summary>
-        /// <param name="scopeName">The name (can be null or empty) of the group to remove the <paramref name="result"/> from.</param>
-        /// <param name="result">The <see cref="MeasurementResult"/> to remove from a scope.</param>
-        private void RemoveFromGroup(string scopeName, MeasurementResult result)
-        {
-            if (string.IsNullOrEmpty(scopeName))
-                return;
-
-            if (!_groups.TryGetValue(scopeName, out var group))
-                throw new AvaloniaInternalException($"SharedSizeScopeHost: The scope {scopeName} wasn't found in the shared size scope");
-
-            group.Remove(result);
-            if (!group.Results.Any())
-                _groups.Remove(scopeName);
-        }
-    }
-}

+ 94 - 102
src/Avalonia.Controls/WrapPanel.cs

@@ -6,6 +6,7 @@ using System.Diagnostics;
 using System.Linq;
 
 using Avalonia.Input;
+using Avalonia.Utilities;
 
 using static System.Math;
 
@@ -92,109 +93,127 @@ namespace Avalonia.Controls
             }
         }
 
-        private UVSize CreateUVSize(Size size) => new UVSize(Orientation, size);
-
-        private UVSize CreateUVSize() => new UVSize(Orientation);
-
         /// <inheritdoc/>
-        protected override Size MeasureOverride(Size availableSize)
+        protected override Size MeasureOverride(Size constraint)
         {
-            var desiredSize = CreateUVSize();
-            var lineSize = CreateUVSize();
-            var uvAvailableSize = CreateUVSize(availableSize);
+            var curLineSize = new UVSize(Orientation);
+            var panelSize = new UVSize(Orientation);
+            var uvConstraint = new UVSize(Orientation, constraint.Width, constraint.Height);
 
-            foreach (var child in Children)
+            var childConstraint = new Size(constraint.Width, constraint.Height);
+
+            for (int i = 0, count = Children.Count; i < count; i++)
             {
-                child.Measure(availableSize);
-                var childSize = CreateUVSize(child.DesiredSize);
-                if (lineSize.U + childSize.U <= uvAvailableSize.U) // same line
+                var child = Children[i];
+                if (child == null) continue;
+
+                //Flow passes its own constrint to children
+                child.Measure(childConstraint);
+
+                //this is the size of the child in UV space
+                var sz = new UVSize(Orientation, child.DesiredSize.Width, child.DesiredSize.Height);
+
+                if (MathUtilities.GreaterThan(curLineSize.U + sz.U, uvConstraint.U)) //need to switch to another line
                 {
-                    lineSize.U += childSize.U;
-                    lineSize.V = Max(lineSize.V, childSize.V);
+                    panelSize.U = Max(curLineSize.U, panelSize.U);
+                    panelSize.V += curLineSize.V;
+                    curLineSize = sz;
+
+                    if (MathUtilities.GreaterThan(sz.U, uvConstraint.U)) //the element is wider then the constrint - give it a separate line                    
+                    {
+                        panelSize.U = Max(sz.U, panelSize.U);
+                        panelSize.V += sz.V;
+                        curLineSize = new UVSize(Orientation);
+                    }
                 }
-                else // moving to next line
+                else //continue to accumulate a line
                 {
-                    desiredSize.U = Max(lineSize.U, uvAvailableSize.U);
-                    desiredSize.V += lineSize.V;
-                    lineSize = childSize;
+                    curLineSize.U += sz.U;
+                    curLineSize.V = Max(sz.V, curLineSize.V);
                 }
             }
-            // last element
-            desiredSize.U = Max(lineSize.U, desiredSize.U);
-            desiredSize.V += lineSize.V;
 
-            return desiredSize.ToSize();
+            //the last line size, if any should be added
+            panelSize.U = Max(curLineSize.U, panelSize.U);
+            panelSize.V += curLineSize.V;
+
+            //go from UV space to W/H space
+            return new Size(panelSize.Width, panelSize.Height);
         }
 
         /// <inheritdoc/>
         protected override Size ArrangeOverride(Size finalSize)
         {
+            int firstInLine = 0;
             double accumulatedV = 0;
-            var uvFinalSize = CreateUVSize(finalSize);
-            var lineSize = CreateUVSize();
-            int firstChildInLineIndex = 0;
-            for (int index = 0; index < Children.Count; index++)
+            UVSize curLineSize = new UVSize(Orientation);
+            UVSize uvFinalSize = new UVSize(Orientation, finalSize.Width, finalSize.Height);
+
+            for (int i = 0; i < Children.Count; i++)
             {
-                var child = Children[index];
-                var childSize = CreateUVSize(child.DesiredSize);
-                if (lineSize.U + childSize.U <= uvFinalSize.U) // same line
+                var child = Children[i];
+                if (child == null) continue;
+
+                var sz = new UVSize(Orientation, child.DesiredSize.Width, child.DesiredSize.Height);
+
+                if (MathUtilities.GreaterThan(curLineSize.U + sz.U, uvFinalSize.U)) //need to switch to another line
                 {
-                    lineSize.U += childSize.U;
-                    lineSize.V = Max(lineSize.V, childSize.V);
+                    arrangeLine(accumulatedV, curLineSize.V, firstInLine, i);
+
+                    accumulatedV += curLineSize.V;
+                    curLineSize = sz;
+
+                    if (MathUtilities.GreaterThan(sz.U, uvFinalSize.U)) //the element is wider then the constraint - give it a separate line                    
+                    {
+                        //switch to next line which only contain one element
+                        arrangeLine(accumulatedV, sz.V, i, ++i);
+
+                        accumulatedV += sz.V;
+                        curLineSize = new UVSize(Orientation);
+                    }
+                    firstInLine = i;
                 }
-                else // moving to next line
+                else //continue to accumulate a line
                 {
-                    var controlsInLine = GetControlsBetween(firstChildInLineIndex, index);
-                    ArrangeLine(accumulatedV, lineSize.V, controlsInLine);
-                    accumulatedV += lineSize.V;
-                    lineSize = childSize;
-                    firstChildInLineIndex = index;
+                    curLineSize.U += sz.U;
+                    curLineSize.V = Max(sz.V, curLineSize.V);
                 }
             }
 
-            if (firstChildInLineIndex < Children.Count)
+            //arrange the last line, if any
+            if (firstInLine < Children.Count)
             {
-                var controlsInLine = GetControlsBetween(firstChildInLineIndex, Children.Count);
-                ArrangeLine(accumulatedV, lineSize.V, controlsInLine);
+                arrangeLine(accumulatedV, curLineSize.V, firstInLine, Children.Count);
             }
-            return finalSize;
-        }
 
-        private IEnumerable<IControl> GetControlsBetween(int first, int last)
-        {
-            return Children.Skip(first).Take(last - first);
+            return finalSize;
         }
 
-        private void ArrangeLine(double v, double lineV, IEnumerable<IControl> controls)
+        private void arrangeLine(double v, double lineV, int start, int end)
         {
             double u = 0;
             bool isHorizontal = (Orientation == Orientation.Horizontal);
-            foreach (var child in controls)
+
+            for (int i = start; i < end; i++)
             {
-                var childSize = CreateUVSize(child.DesiredSize);
-                var x = isHorizontal ? u : v;
-                var y = isHorizontal ? v : u;
-                var width = isHorizontal ? childSize.U : lineV;
-                var height = isHorizontal ? lineV : childSize.U;
-                child.Arrange(new Rect(x, y, width, height));
-                u += childSize.U;
+                var child = Children[i];
+                if (child != null)
+                {
+                    UVSize childSize = new UVSize(Orientation, child.DesiredSize.Width, child.DesiredSize.Height);
+                    double layoutSlotU = childSize.U;
+                    child.Arrange(new Rect(
+                        (isHorizontal ? u : v),
+                        (isHorizontal ? v : u),
+                        (isHorizontal ? layoutSlotU : lineV),
+                        (isHorizontal ? lineV : layoutSlotU)));
+                    u += layoutSlotU;
+                }
             }
         }
-        /// <summary>
-        /// Used to not not write separate code for horizontal and vertical orientation.
-        /// U is direction in line. (x if orientation is horizontal)
-        /// V is direction of lines. (y if orientation is horizontal)
-        /// </summary>
-        [DebuggerDisplay("U = {U} V = {V}")]
+
         private struct UVSize
         {
-            private readonly Orientation _orientation;
-
-            internal double U;
-
-            internal double V;
-
-            private UVSize(Orientation orientation, double width, double height)
+            internal UVSize(Orientation orientation, double width, double height)
             {
                 U = V = 0d;
                 _orientation = orientation;
@@ -202,52 +221,25 @@ namespace Avalonia.Controls
                 Height = height;
             }
 
-            internal UVSize(Orientation orientation, Size size)
-                : this(orientation, size.Width, size.Height)
-            {
-            }
-
             internal UVSize(Orientation orientation)
             {
                 U = V = 0d;
                 _orientation = orientation;
             }
 
-            private double Width
+            internal double U;
+            internal double V;
+            private Orientation _orientation;
+
+            internal double Width
             {
                 get { return (_orientation == Orientation.Horizontal ? U : V); }
-                set
-                {
-                    if (_orientation == Orientation.Horizontal)
-                    {
-                        U = value;
-                    }
-                    else
-                    {
-                        V = value;
-                    }
-                }
+                set { if (_orientation == Orientation.Horizontal) U = value; else V = value; }
             }
-
-            private double Height
+            internal double Height
             {
                 get { return (_orientation == Orientation.Horizontal ? V : U); }
-                set
-                {
-                    if (_orientation == Orientation.Horizontal)
-                    {
-                        V = value;
-                    }
-                    else
-                    {
-                        U = value;
-                    }
-                }
-            }
-
-            public Size ToSize()
-            {
-                return new Size(Width, Height);
+                set { if (_orientation == Orientation.Horizontal) V = value; else U = value; }
             }
         }
     }

+ 2 - 4
src/Avalonia.DesignerSupport/Remote/RemoteDesignerEntryPoint.cs

@@ -9,7 +9,6 @@ using Avalonia.Remote.Protocol;
 using Avalonia.Remote.Protocol.Designer;
 using Avalonia.Remote.Protocol.Viewport;
 using Avalonia.Threading;
-using Portable.Xaml;
 
 namespace Avalonia.DesignerSupport.Remote
 {
@@ -206,7 +205,6 @@ namespace Avalonia.DesignerSupport.Remote
                 }
                 catch (Exception e)
                 {
-                    var xamlException = e as XamlException;
                     var xmlException = e as XmlException;
                     
                     s_transport.Send(new UpdateXamlResultMessage
@@ -216,8 +214,8 @@ namespace Avalonia.DesignerSupport.Remote
                         {
                             ExceptionType = e.GetType().FullName,
                             Message = e.Message.ToString(),
-                            LineNumber = xamlException?.LineNumber ?? xmlException?.LineNumber,
-                            LinePosition = xamlException?.LinePosition ?? xmlException?.LinePosition,
+                            LineNumber = xmlException?.LineNumber,
+                            LinePosition = xmlException?.LinePosition,
                         }
                     });
                 }

+ 1 - 0
src/Avalonia.Input/Cursors.cs

@@ -38,6 +38,7 @@ namespace Avalonia.Input
         DragMove,
         DragCopy,
         DragLink,
+        None,
 
         // Not available in GTK directly, see http://www.pixelbeat.org/programming/x_cursors/ 
         // We might enable them later, preferably, by loading pixmax direclty from theme with fallback image

+ 127 - 0
src/Avalonia.Input/GestureRecognizers/GestureRecognizerCollection.cs

@@ -0,0 +1,127 @@
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using Avalonia.Controls;
+using Avalonia.Data;
+using Avalonia.LogicalTree;
+using Avalonia.Styling;
+
+namespace Avalonia.Input.GestureRecognizers
+{
+    public class GestureRecognizerCollection : IReadOnlyCollection<IGestureRecognizer>, IGestureRecognizerActionsDispatcher
+    {
+        private readonly IInputElement _inputElement;
+        private List<IGestureRecognizer> _recognizers;
+        private Dictionary<IPointer, IGestureRecognizer> _pointerGrabs;
+        
+        
+        public GestureRecognizerCollection(IInputElement inputElement)
+        {
+            _inputElement = inputElement;
+        }
+        
+        public void Add(IGestureRecognizer recognizer)
+        {
+            if (_recognizers == null)
+            {
+                // We initialize the collection when the first recognizer is added
+                _recognizers = new List<IGestureRecognizer>();
+                _pointerGrabs = new Dictionary<IPointer, IGestureRecognizer>();
+            }
+
+            _recognizers.Add(recognizer);
+            recognizer.Initialize(_inputElement, this);
+
+            // Hacks to make bindings work
+            
+            if (_inputElement is ILogical logicalParent && recognizer is ISetLogicalParent logical)
+            {
+                logical.SetParent(logicalParent);
+                if (recognizer is IStyleable styleableRecognizer
+                    && _inputElement is IStyleable styleableParent)
+                    styleableRecognizer.Bind(StyledElement.TemplatedParentProperty,
+                        styleableParent.GetObservable(StyledElement.TemplatedParentProperty));
+            }
+        }
+
+        static readonly List<IGestureRecognizer> s_Empty = new List<IGestureRecognizer>();
+
+        public IEnumerator<IGestureRecognizer> GetEnumerator()
+            => _recognizers?.GetEnumerator() ?? s_Empty.GetEnumerator();
+
+        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
+
+        public int Count => _recognizers?.Count ?? 0;
+
+
+        internal bool HandlePointerPressed(PointerPressedEventArgs e)
+        {
+            if (_recognizers == null)
+                return false;
+            foreach (var r in _recognizers)
+            {
+                if(e.Handled)
+                    break;
+                r.PointerPressed(e);
+            }
+
+            return e.Handled;
+        }
+
+        internal bool HandlePointerReleased(PointerReleasedEventArgs e)
+        {
+            if (_recognizers == null)
+                return false;
+            if (_pointerGrabs.TryGetValue(e.Pointer, out var capture))
+            {
+                capture.PointerReleased(e);
+            }
+            else
+                foreach (var r in _recognizers)
+                {
+                    if (e.Handled)
+                        break;
+                    r.PointerReleased(e);
+                }
+            return e.Handled;
+        }
+
+        internal bool HandlePointerMoved(PointerEventArgs e)
+        {
+            if (_recognizers == null)
+                return false;
+            if (_pointerGrabs.TryGetValue(e.Pointer, out var capture))
+            {
+                capture.PointerMoved(e);
+            }
+            else
+                foreach (var r in _recognizers)
+                {
+                    if (e.Handled)
+                        break;
+                    r.PointerMoved(e);
+                }
+            return e.Handled;
+        }
+
+        internal void HandlePointerCaptureLost(PointerCaptureLostEventArgs e)
+        {
+            if (_recognizers == null)
+                return;
+            _pointerGrabs.Remove(e.Pointer);
+            foreach (var r in _recognizers)
+            {
+                if(e.Handled)
+                    break;
+                r.PointerCaptureLost(e);
+            }
+        }
+
+        void IGestureRecognizerActionsDispatcher.Capture(IPointer pointer, IGestureRecognizer recognizer)
+        {
+            pointer.Capture(_inputElement);
+            _pointerGrabs[pointer] = recognizer;
+        }
+
+    }
+}

+ 23 - 0
src/Avalonia.Input/GestureRecognizers/IGestureRecognizer.cs

@@ -0,0 +1,23 @@
+namespace Avalonia.Input.GestureRecognizers
+{
+    public interface IGestureRecognizer
+    {
+        void Initialize(IInputElement target, IGestureRecognizerActionsDispatcher actions);
+        void PointerPressed(PointerPressedEventArgs e);
+        void PointerReleased(PointerReleasedEventArgs e);
+        void PointerMoved(PointerEventArgs e);
+        void PointerCaptureLost(PointerCaptureLostEventArgs e);
+    }
+    
+    public interface IGestureRecognizerActionsDispatcher
+    {
+        void Capture(IPointer pointer, IGestureRecognizer recognizer);
+    }
+    
+    public enum GestureRecognizerResult
+    {
+        None,
+        Capture,
+        ReleaseCapture
+    }
+}

+ 183 - 0
src/Avalonia.Input/GestureRecognizers/ScrollGestureRecognizer.cs

@@ -0,0 +1,183 @@
+using System;
+using System.Diagnostics;
+using Avalonia.Interactivity;
+using Avalonia.Threading;
+
+namespace Avalonia.Input.GestureRecognizers
+{
+    public class ScrollGestureRecognizer 
+        : StyledElement, // It's not an "element" in any way, shape or form, but TemplateBinding refuse to work otherwise
+            IGestureRecognizer
+    {
+        private bool _scrolling;
+        private Point _trackedRootPoint;
+        private IPointer _tracking;
+        private IInputElement _target;
+        private IGestureRecognizerActionsDispatcher _actions;
+        private bool _canHorizontallyScroll;
+        private bool _canVerticallyScroll;
+        private int _gestureId;
+        
+        // Movement per second
+        private Vector _inertia;
+        private ulong? _lastMoveTimestamp;
+        
+        /// <summary>
+        /// Defines the <see cref="CanHorizontallyScroll"/> property.
+        /// </summary>
+        public static readonly DirectProperty<ScrollGestureRecognizer, bool> CanHorizontallyScrollProperty =
+            AvaloniaProperty.RegisterDirect<ScrollGestureRecognizer, bool>(
+                nameof(CanHorizontallyScroll),
+                o => o.CanHorizontallyScroll,
+                (o, v) => o.CanHorizontallyScroll = v);
+
+        /// <summary>
+        /// Defines the <see cref="CanVerticallyScroll"/> property.
+        /// </summary>
+        public static readonly DirectProperty<ScrollGestureRecognizer, bool> CanVerticallyScrollProperty =
+            AvaloniaProperty.RegisterDirect<ScrollGestureRecognizer, bool>(
+                nameof(CanVerticallyScroll),
+                o => o.CanVerticallyScroll,
+                (o, v) => o.CanVerticallyScroll = v);
+        
+        /// <summary>
+        /// Gets or sets a value indicating whether the content can be scrolled horizontally.
+        /// </summary>
+        public bool CanHorizontallyScroll
+        {
+            get => _canHorizontallyScroll;
+            set => SetAndRaise(CanHorizontallyScrollProperty, ref _canHorizontallyScroll, value);
+        }
+
+        /// <summary>
+        /// Gets or sets a value indicating whether the content can be scrolled horizontally.
+        /// </summary>
+        public bool CanVerticallyScroll
+        {
+            get => _canVerticallyScroll;
+            set => SetAndRaise(CanVerticallyScrollProperty, ref _canVerticallyScroll, value);
+        }
+        
+
+        public void Initialize(IInputElement target, IGestureRecognizerActionsDispatcher actions)
+        {
+            _target = target;
+            _actions = actions;
+        }
+        
+        public void PointerPressed(PointerPressedEventArgs e)
+        {
+            if (e.Pointer.IsPrimary && e.Pointer.Type == PointerType.Touch)
+            {
+                EndGesture();
+                _tracking = e.Pointer;
+                _gestureId = ScrollGestureEventArgs.GetNextFreeId();;
+                _trackedRootPoint = e.GetPosition(null);
+            }
+        }
+
+        // Arbitrary chosen value, probably need to move that to platform settings or something
+        private const double ScrollStartDistance = 30;
+        
+        // Pixels per second speed that is considered to be the stop of inertiall scroll
+        private const double InertialScrollSpeedEnd = 5;
+        
+        public void PointerMoved(PointerEventArgs e)
+        {
+            if (e.Pointer == _tracking)
+            {
+                var rootPoint = e.GetPosition(null);
+                if (!_scrolling)
+                {
+                    if (CanHorizontallyScroll && Math.Abs(_trackedRootPoint.X - rootPoint.X) > ScrollStartDistance)
+                        _scrolling = true;
+                    if (CanVerticallyScroll && Math.Abs(_trackedRootPoint.Y - rootPoint.Y) > ScrollStartDistance)
+                        _scrolling = true;
+                    if (_scrolling)
+                    {
+                        _actions.Capture(e.Pointer, this);
+                    }
+                }
+
+                if (_scrolling)
+                {
+                    var vector = _trackedRootPoint - rootPoint;
+                    var elapsed = _lastMoveTimestamp.HasValue ?
+                        TimeSpan.FromMilliseconds(e.Timestamp - _lastMoveTimestamp.Value) :
+                        TimeSpan.Zero;
+                    
+                    _lastMoveTimestamp = e.Timestamp;
+                    _trackedRootPoint = rootPoint;
+                    if (elapsed.TotalSeconds > 0)
+                        _inertia = vector / elapsed.TotalSeconds;
+                    _target.RaiseEvent(new ScrollGestureEventArgs(_gestureId, vector));
+                    e.Handled = true;
+                }
+            }
+        }
+
+        public void PointerCaptureLost(PointerCaptureLostEventArgs e)
+        {
+            if (e.Pointer == _tracking) EndGesture();
+        }
+
+        void EndGesture()
+        {
+            _tracking = null;
+            if (_scrolling)
+            {
+                _inertia = default;
+                _scrolling = false;
+                _target.RaiseEvent(new ScrollGestureEndedEventArgs(_gestureId));
+                _gestureId = 0;
+                _lastMoveTimestamp = null;
+            }
+            
+        }
+        
+        
+        public void PointerReleased(PointerReleasedEventArgs e)
+        {
+            if (e.Pointer == _tracking && _scrolling)
+            {
+                e.Handled = true;
+                if (_inertia == default
+                    || e.Timestamp == 0
+                    || _lastMoveTimestamp == 0
+                    || e.Timestamp - _lastMoveTimestamp > 200)
+                    EndGesture();
+                else
+                {
+                    var savedGestureId = _gestureId;
+                    var st = Stopwatch.StartNew();
+                    var lastTime = TimeSpan.Zero;
+                    DispatcherTimer.Run(() =>
+                    {
+                        // Another gesture has started, finish the current one
+                        if (_gestureId != savedGestureId)
+                        {
+                            return false;
+                        }
+
+                        var elapsedSinceLastTick = st.Elapsed - lastTime;
+                        lastTime = st.Elapsed;
+
+                        var speed = _inertia * Math.Pow(0.15, st.Elapsed.TotalSeconds);
+                        var distance = speed * elapsedSinceLastTick.TotalSeconds;
+                        _target.RaiseEvent(new ScrollGestureEventArgs(_gestureId, distance));
+
+
+
+                        if (Math.Abs(speed.X) < InertialScrollSpeedEnd || Math.Abs(speed.Y) <= InertialScrollSpeedEnd)
+                        {
+                            EndGesture();
+                            return false;
+                        }
+
+                        return true;
+                    }, TimeSpan.FromMilliseconds(16), DispatcherPriority.Background);
+                }
+            }
+        }
+    }
+}

+ 8 - 0
src/Avalonia.Input/Gestures.cs

@@ -18,6 +18,14 @@ namespace Avalonia.Input
             RoutingStrategies.Bubble,
             typeof(Gestures));
 
+        public static readonly RoutedEvent<ScrollGestureEventArgs> ScrollGestureEvent =
+            RoutedEvent.Register<ScrollGestureEventArgs>(
+                "ScrollGesture", RoutingStrategies.Bubble, typeof(Gestures));
+ 
+        public static readonly RoutedEvent<ScrollGestureEventArgs> ScrollGestureEndedEvent =
+            RoutedEvent.Register<ScrollGestureEventArgs>(
+                "ScrollGestureEnded", RoutingStrategies.Bubble, typeof(Gestures));
+
         private static WeakReference s_lastPress;
 
         static Gestures()

+ 39 - 0
src/Avalonia.Input/InputElement.cs

@@ -4,6 +4,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using Avalonia.Input.GestureRecognizers;
 using Avalonia.Interactivity;
 using Avalonia.VisualTree;
 
@@ -129,6 +130,14 @@ namespace Avalonia.Input
             RoutedEvent.Register<InputElement, PointerReleasedEventArgs>(
                 "PointerReleased",
                 RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
+        
+        /// <summary>
+        /// Defines the <see cref="PointerCaptureLost"/> routed event.
+        /// </summary>
+        public static readonly RoutedEvent<PointerCaptureLostEventArgs> PointerCaptureLostEvent =
+            RoutedEvent.Register<InputElement, PointerCaptureLostEventArgs>(
+                "PointerCaptureLost", 
+                RoutingStrategies.Direct);
 
         /// <summary>
         /// Defines the <see cref="PointerWheelChanged"/> event.
@@ -151,6 +160,7 @@ namespace Avalonia.Input
         private bool _isEffectivelyEnabled = true;
         private bool _isFocused;
         private bool _isPointerOver;
+        private GestureRecognizerCollection _gestureRecognizers;
 
         /// <summary>
         /// Initializes static members of the <see cref="InputElement"/> class.
@@ -169,6 +179,7 @@ namespace Avalonia.Input
             PointerMovedEvent.AddClassHandler<InputElement>(x => x.OnPointerMoved);
             PointerPressedEvent.AddClassHandler<InputElement>(x => x.OnPointerPressed);
             PointerReleasedEvent.AddClassHandler<InputElement>(x => x.OnPointerReleased);
+            PointerCaptureLostEvent.AddClassHandler<InputElement>(x => x.OnPointerCaptureLost);
             PointerWheelChangedEvent.AddClassHandler<InputElement>(x => x.OnPointerWheelChanged);
 
             PseudoClass<InputElement, bool>(IsEffectivelyEnabledProperty, x => !x, ":disabled");
@@ -266,6 +277,16 @@ namespace Avalonia.Input
             remove { RemoveHandler(PointerReleasedEvent, value); }
         }
 
+        /// <summary>
+        /// Occurs when the control or its child control loses the pointer capture for any reason,
+        /// event will not be triggered for a parent control if capture was transferred to another child of that parent control
+        /// </summary>
+        public event EventHandler<PointerCaptureLostEventArgs> PointerCaptureLost
+        {
+            add => AddHandler(PointerCaptureLostEvent, value);
+            remove => RemoveHandler(PointerCaptureLostEvent, value);
+        }
+        
         /// <summary>
         /// Occurs when the mouse wheen is scrolled over the control.
         /// </summary>
@@ -367,6 +388,9 @@ namespace Avalonia.Input
         /// </remarks>
         protected virtual bool IsEnabledCore => IsEnabled;
 
+        public GestureRecognizerCollection GestureRecognizers
+            => _gestureRecognizers ?? (_gestureRecognizers = new GestureRecognizerCollection(this));
+
         /// <summary>
         /// Focuses the control.
         /// </summary>
@@ -457,6 +481,8 @@ namespace Avalonia.Input
         /// <param name="e">The event args.</param>
         protected virtual void OnPointerMoved(PointerEventArgs e)
         {
+            if (_gestureRecognizers?.HandlePointerMoved(e) == true)
+                e.Handled = true;
         }
 
         /// <summary>
@@ -465,6 +491,8 @@ namespace Avalonia.Input
         /// <param name="e">The event args.</param>
         protected virtual void OnPointerPressed(PointerPressedEventArgs e)
         {
+            if (_gestureRecognizers?.HandlePointerPressed(e) == true)
+                e.Handled = true;
         }
 
         /// <summary>
@@ -473,6 +501,17 @@ namespace Avalonia.Input
         /// <param name="e">The event args.</param>
         protected virtual void OnPointerReleased(PointerReleasedEventArgs e)
         {
+            if (_gestureRecognizers?.HandlePointerReleased(e) == true)
+                e.Handled = true;
+        }
+
+        /// <summary>
+        /// Called before the <see cref="PointerCaptureLost"/> event occurs.
+        /// </summary>
+        /// <param name="e">The event args.</param>
+        protected virtual void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
+        {
+            _gestureRecognizers?.HandlePointerCaptureLost(e);
         }
 
         /// <summary>

+ 51 - 68
src/Avalonia.Input/MouseDevice.cs

@@ -14,17 +14,18 @@ namespace Avalonia.Input
     /// <summary>
     /// Represents a mouse device.
     /// </summary>
-    public class MouseDevice : IMouseDevice, IPointer
+    public class MouseDevice : IMouseDevice
     {
         private int _clickCount;
         private Rect _lastClickRect;
         private ulong _lastClickTime;
-        private IInputElement _captured;
-        private IDisposable _capturedSubscription;
 
-        PointerType IPointer.Type => PointerType.Mouse;
-        bool IPointer.IsPrimary => true;
-        int IPointer.Id { get; } = Pointer.GetNextFreeId();
+        private readonly Pointer _pointer;
+
+        public MouseDevice(Pointer pointer = null)
+        {
+            _pointer = pointer ?? new Pointer(Pointer.GetNextFreeId(), PointerType.Mouse, true);
+        }
         
         /// <summary>
         /// Gets the control that is currently capturing by the mouse, if any.
@@ -34,27 +35,9 @@ namespace Avalonia.Input
         /// within the control's bounds or not. To set the mouse capture, call the 
         /// <see cref="Capture"/> method.
         /// </remarks>
-        public IInputElement Captured
-        {
-            get => _captured;
-            protected set
-            {
-                _capturedSubscription?.Dispose();
-                _capturedSubscription = null;
-
-                if (value != null)
-                {
-                    _capturedSubscription = Observable.FromEventPattern<VisualTreeAttachmentEventArgs>(
-                        x => value.DetachedFromVisualTree += x,
-                        x => value.DetachedFromVisualTree -= x)
-                        .Take(1)
-                        .Subscribe(_ => Captured = null);
-                }
+        [Obsolete("Use IPointer instead")]
+        public IInputElement Captured => _pointer.Captured;
 
-                _captured = value;
-            }
-        }
-        
         /// <summary>
         /// Gets the mouse position, in screen coordinates.
         /// </summary>
@@ -73,10 +56,9 @@ namespace Avalonia.Input
         /// within the control's bounds or not. The current mouse capture control is exposed
         /// by the <see cref="Captured"/> property.
         /// </remarks>
-        public virtual void Capture(IInputElement control)
+        public void Capture(IInputElement control)
         {
-            // TODO: Check visibility and enabled state before setting capture.
-            Captured = control;
+            _pointer.Capture(control);
         }
 
         /// <summary>
@@ -110,13 +92,13 @@ namespace Avalonia.Input
 
             if (rect.Contains(clientPoint))
             {
-                if (Captured == null)
+                if (_pointer.Captured == null)
                 {
-                    SetPointerOver(this, root, clientPoint, InputModifiers.None);
+                    SetPointerOver(this, 0 /* TODO: proper timestamp */, root, clientPoint, InputModifiers.None);
                 }
                 else
                 {
-                    SetPointerOver(this, root, Captured, InputModifiers.None);
+                    SetPointerOver(this, 0 /* TODO: proper timestamp */, root, _pointer.Captured, InputModifiers.None);
                 }
             }
         }
@@ -144,13 +126,13 @@ namespace Avalonia.Input
             switch (e.Type)
             {
                 case RawPointerEventType.LeaveWindow:
-                    LeaveWindow(mouse, e.Root, e.InputModifiers);
+                    LeaveWindow(mouse, e.Timestamp, e.Root, e.InputModifiers);
                     break;
                 case RawPointerEventType.LeftButtonDown:
                 case RawPointerEventType.RightButtonDown:
                 case RawPointerEventType.MiddleButtonDown:
                     if (ButtonCount(props) > 1)
-                        e.Handled = MouseMove(mouse, e.Root, e.Position, props, e.InputModifiers);
+                        e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, e.InputModifiers);
                     else
                         e.Handled = MouseDown(mouse, e.Timestamp, e.Root, e.Position,
                             props, e.InputModifiers);
@@ -159,25 +141,25 @@ namespace Avalonia.Input
                 case RawPointerEventType.RightButtonUp:
                 case RawPointerEventType.MiddleButtonUp:
                     if (ButtonCount(props) != 0)
-                        e.Handled = MouseMove(mouse, e.Root, e.Position, props, e.InputModifiers);
+                        e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, e.InputModifiers);
                     else
-                        e.Handled = MouseUp(mouse, e.Root, e.Position, props, e.InputModifiers);
+                        e.Handled = MouseUp(mouse, e.Timestamp, e.Root, e.Position, props, e.InputModifiers);
                     break;
                 case RawPointerEventType.Move:
-                    e.Handled = MouseMove(mouse, e.Root, e.Position, props, e.InputModifiers);
+                    e.Handled = MouseMove(mouse, e.Timestamp, e.Root, e.Position, props, e.InputModifiers);
                     break;
                 case RawPointerEventType.Wheel:
-                    e.Handled = MouseWheel(mouse, e.Root, e.Position, props, ((RawMouseWheelEventArgs)e).Delta, e.InputModifiers);
+                    e.Handled = MouseWheel(mouse, e.Timestamp, e.Root, e.Position, props, ((RawMouseWheelEventArgs)e).Delta, e.InputModifiers);
                     break;
             }
         }
 
-        private void LeaveWindow(IMouseDevice device, IInputRoot root, InputModifiers inputModifiers)
+        private void LeaveWindow(IMouseDevice device, ulong timestamp, IInputRoot root, InputModifiers inputModifiers)
         {
             Contract.Requires<ArgumentNullException>(device != null);
             Contract.Requires<ArgumentNullException>(root != null);
 
-            ClearPointerOver(this, root, inputModifiers);
+            ClearPointerOver(this, timestamp, root, inputModifiers);
         }
 
 
@@ -195,7 +177,7 @@ namespace Avalonia.Input
                 rv.IsLeftButtonPressed = false;
             if (args.Type == RawPointerEventType.MiddleButtonUp)
                 rv.IsMiddleButtonPressed = false;
-            if (args.Type == RawPointerEventType.RightButtonDown)
+            if (args.Type == RawPointerEventType.RightButtonUp)
                 rv.IsRightButtonPressed = false;
             return rv;
         }
@@ -212,8 +194,8 @@ namespace Avalonia.Input
 
             if (hit != null)
             {
-                IInteractive source = GetSource(hit);
-
+                _pointer.Capture(hit);
+                var source = GetSource(hit);
                 if (source != null)
                 {
                     var settings = AvaloniaLocator.Current.GetService<IPlatformSettings>();
@@ -229,8 +211,7 @@ namespace Avalonia.Input
                     _lastClickRect = new Rect(p, new Size())
                         .Inflate(new Thickness(settings.DoubleClickSize.Width / 2, settings.DoubleClickSize.Height / 2));
                     _lastMouseDownButton = properties.GetObsoleteMouseButton();
-                    var e = new PointerPressedEventArgs(source, this, root, p, properties, inputModifiers, _clickCount);
-
+                    var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, _clickCount);
                     source.RaiseEvent(e);
                     return e.Handled;
                 }
@@ -239,7 +220,7 @@ namespace Avalonia.Input
             return false;
         }
 
-        private bool MouseMove(IMouseDevice device, IInputRoot root, Point p, PointerPointProperties properties,
+        private bool MouseMove(IMouseDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties properties,
             InputModifiers inputModifiers)
         {
             Contract.Requires<ArgumentNullException>(device != null);
@@ -247,24 +228,24 @@ namespace Avalonia.Input
 
             IInputElement source;
 
-            if (Captured == null)
+            if (_pointer.Captured == null)
             {
-                source = SetPointerOver(this, root, p, inputModifiers);
+                source = SetPointerOver(this, timestamp, root, p, inputModifiers);
             }
             else
             {
-                SetPointerOver(this, root, Captured, inputModifiers);
-                source = Captured;
+                SetPointerOver(this, timestamp, root, _pointer.Captured, inputModifiers);
+                source = _pointer.Captured;
             }
 
-            var e = new PointerEventArgs(InputElement.PointerMovedEvent, source, this, root,
-                p, properties, inputModifiers);
+            var e = new PointerEventArgs(InputElement.PointerMovedEvent, source, _pointer, root,
+                p, timestamp, properties, inputModifiers);
 
             source?.RaiseEvent(e);
             return e.Handled;
         }
 
-        private bool MouseUp(IMouseDevice device, IInputRoot root, Point p, PointerPointProperties props,
+        private bool MouseUp(IMouseDevice device, ulong timestamp, IInputRoot root, Point p, PointerPointProperties props,
             InputModifiers inputModifiers)
         {
             Contract.Requires<ArgumentNullException>(device != null);
@@ -275,16 +256,18 @@ namespace Avalonia.Input
             if (hit != null)
             {
                 var source = GetSource(hit);
-                var e = new PointerReleasedEventArgs(source, this, root, p, props, inputModifiers, _lastMouseDownButton);
+                var e = new PointerReleasedEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers,
+                    _lastMouseDownButton);
 
                 source?.RaiseEvent(e);
+                _pointer.Capture(null);
                 return e.Handled;
             }
 
             return false;
         }
 
-        private bool MouseWheel(IMouseDevice device, IInputRoot root, Point p,
+        private bool MouseWheel(IMouseDevice device, ulong timestamp, IInputRoot root, Point p,
             PointerPointProperties props,
             Vector delta, InputModifiers inputModifiers)
         {
@@ -296,7 +279,7 @@ namespace Avalonia.Input
             if (hit != null)
             {
                 var source = GetSource(hit);
-                var e = new PointerWheelEventArgs(source, this, root, p, props, inputModifiers, delta);
+                var e = new PointerWheelEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers, delta);
 
                 source?.RaiseEvent(e);
                 return e.Handled;
@@ -309,7 +292,7 @@ namespace Avalonia.Input
         {
             Contract.Requires<ArgumentNullException>(hit != null);
 
-            return Captured ??
+            return _pointer.Captured ??
                 (hit as IInteractive) ??
                 hit.GetSelfAndVisualAncestors().OfType<IInteractive>().FirstOrDefault();
         }
@@ -318,22 +301,22 @@ namespace Avalonia.Input
         {
             Contract.Requires<ArgumentNullException>(root != null);
 
-            return Captured ?? root.InputHitTest(p);
+            return _pointer.Captured ?? root.InputHitTest(p);
         }
 
-        PointerEventArgs CreateSimpleEvent(RoutedEvent ev, IInteractive source, InputModifiers inputModifiers)
+        PointerEventArgs CreateSimpleEvent(RoutedEvent ev, ulong timestamp, IInteractive source, InputModifiers inputModifiers)
         {
-            return new PointerEventArgs(ev, source, this, null, default,
-                new PointerPointProperties(inputModifiers), inputModifiers);
+            return new PointerEventArgs(ev, source, _pointer, null, default,
+                timestamp, new PointerPointProperties(inputModifiers), inputModifiers);
         }
 
-        private void ClearPointerOver(IPointerDevice device, IInputRoot root, InputModifiers inputModifiers)
+        private void ClearPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, InputModifiers inputModifiers)
         {
             Contract.Requires<ArgumentNullException>(device != null);
             Contract.Requires<ArgumentNullException>(root != null);
 
             var element = root.PointerOverElement;
-            var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, element, inputModifiers);
+            var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, timestamp, element, inputModifiers);
 
             if (element!=null && !element.IsAttachedToVisualTree)
             {
@@ -370,7 +353,7 @@ namespace Avalonia.Input
             }
         }
 
-        private IInputElement SetPointerOver(IPointerDevice device, IInputRoot root, Point p, InputModifiers inputModifiers)
+        private IInputElement SetPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, Point p, InputModifiers inputModifiers)
         {
             Contract.Requires<ArgumentNullException>(device != null);
             Contract.Requires<ArgumentNullException>(root != null);
@@ -381,18 +364,18 @@ namespace Avalonia.Input
             {
                 if (element != null)
                 {
-                    SetPointerOver(device, root, element, inputModifiers);
+                    SetPointerOver(device, timestamp, root, element, inputModifiers);
                 }
                 else
                 {
-                    ClearPointerOver(device, root, inputModifiers);
+                    ClearPointerOver(device, timestamp, root, inputModifiers);
                 }
             }
 
             return element;
         }
 
-        private void SetPointerOver(IPointerDevice device, IInputRoot root, IInputElement element, InputModifiers inputModifiers)
+        private void SetPointerOver(IPointerDevice device, ulong timestamp, IInputRoot root, IInputElement element, InputModifiers inputModifiers)
         {
             Contract.Requires<ArgumentNullException>(device != null);
             Contract.Requires<ArgumentNullException>(root != null);
@@ -414,7 +397,7 @@ namespace Avalonia.Input
 
             el = root.PointerOverElement;
 
-            var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, el, inputModifiers);
+            var e = CreateSimpleEvent(InputElement.PointerLeaveEvent, timestamp, el, inputModifiers);
             if (el!=null && branch!=null && !el.IsAttachedToVisualTree)
             {
                 ClearChildrenPointerOver(e,branch,false);

+ 30 - 20
src/Avalonia.Input/Pointer.cs

@@ -1,5 +1,7 @@
 using System;
+using System.Collections.Generic;
 using System.Linq;
+using Avalonia.Interactivity;
 using Avalonia.VisualTree;
 
 namespace Avalonia.Input
@@ -9,23 +11,46 @@ namespace Avalonia.Input
         private static int s_NextFreePointerId = 1000;
         public static int GetNextFreeId() => s_NextFreePointerId++;
         
-        public Pointer(int id, PointerType type, bool isPrimary, IInputElement implicitlyCaptured)
+        public Pointer(int id, PointerType type, bool isPrimary)
         {
             Id = id;
             Type = type;
             IsPrimary = isPrimary;
-            ImplicitlyCaptured = implicitlyCaptured;
-            if (ImplicitlyCaptured != null)
-                ImplicitlyCaptured.DetachedFromVisualTree += OnImplicitCaptureDetached;
         }
 
         public int Id { get; }
 
+        IInputElement FindCommonParent(IInputElement control1, IInputElement control2)
+        {
+            if (control1 == null || control2 == null)
+                return null;
+            var seen = new HashSet<IInputElement>(control1.GetSelfAndVisualAncestors().OfType<IInputElement>());
+            return control2.GetSelfAndVisualAncestors().OfType<IInputElement>().FirstOrDefault(seen.Contains);
+        }
+
+        protected virtual void PlatformCapture(IInputElement element)
+        {
+            
+        }
+        
         public void Capture(IInputElement control)
         {
             if (Captured != null)
                 Captured.DetachedFromVisualTree -= OnCaptureDetached;
+            var oldCapture = control;
             Captured = control;
+            PlatformCapture(control);
+            if (oldCapture != null)
+            {
+                var commonParent = FindCommonParent(control, oldCapture);
+                foreach (var notifyTarget in oldCapture.GetSelfAndVisualAncestors().OfType<IInputElement>())
+                {
+                    if (notifyTarget == commonParent)
+                        break;
+                    notifyTarget.RaiseEvent(new PointerCaptureLostEventArgs(notifyTarget, this));
+                }
+            }
+
             if (Captured != null)
                 Captured.DetachedFromVisualTree += OnCaptureDetached;
         }
@@ -38,26 +63,11 @@ namespace Avalonia.Input
             Capture(GetNextCapture(e.Parent));
         }
 
-        private void OnImplicitCaptureDetached(object sender, VisualTreeAttachmentEventArgs e)
-        {
-            ImplicitlyCaptured.DetachedFromVisualTree -= OnImplicitCaptureDetached;
-            ImplicitlyCaptured = GetNextCapture(e.Parent);
-            if (ImplicitlyCaptured != null)
-                ImplicitlyCaptured.DetachedFromVisualTree += OnImplicitCaptureDetached;
-        }
 
         public IInputElement Captured { get; private set; }
-        public IInputElement ImplicitlyCaptured { get; private set; }
-        public IInputElement GetEffectiveCapture() => Captured ?? ImplicitlyCaptured;
             
         public PointerType Type { get; }
         public bool IsPrimary { get; }
-        public void Dispose()
-        {
-            if (ImplicitlyCaptured != null)
-                ImplicitlyCaptured.DetachedFromVisualTree -= OnImplicitCaptureDetached;
-            if (Captured != null)
-                Captured.DetachedFromVisualTree -= OnCaptureDetached;
-        }
+        public void Dispose() => Capture(null);
     }
 }

+ 24 - 7
src/Avalonia.Input/PointerEventArgs.cs

@@ -17,7 +17,9 @@ namespace Avalonia.Input
         public PointerEventArgs(RoutedEvent routedEvent,
             IInteractive source,
             IPointer pointer,
-            IVisual rootVisual, Point rootVisualPosition, PointerPointProperties properties,
+            IVisual rootVisual, Point rootVisualPosition,
+            ulong timestamp,
+            PointerPointProperties properties,
             InputModifiers modifiers)
            : base(routedEvent)
         {
@@ -26,6 +28,7 @@ namespace Avalonia.Input
             _rootVisualPosition = rootVisualPosition;
             _properties = properties;
             Pointer = pointer;
+            Timestamp = timestamp;
             InputModifiers = modifiers;
         }
 
@@ -50,6 +53,7 @@ namespace Avalonia.Input
         }
 
         public IPointer Pointer { get; }
+        public ulong Timestamp { get; }
 
         private IPointerDevice _device;
 
@@ -86,11 +90,13 @@ namespace Avalonia.Input
         public PointerPressedEventArgs(
             IInteractive source,
             IPointer pointer,
-            IVisual rootVisual, Point rootVisualPosition, PointerPointProperties properties,
+            IVisual rootVisual, Point rootVisualPosition,
+            ulong timestamp,
+            PointerPointProperties properties,
             InputModifiers modifiers,
             int obsoleteClickCount = 1)
-            : base(InputElement.PointerPressedEvent, source, pointer, rootVisual, rootVisualPosition, properties,
-                modifiers)
+            : base(InputElement.PointerPressedEvent, source, pointer, rootVisual, rootVisualPosition,
+                timestamp, properties, modifiers)
         {
             _obsoleteClickCount = obsoleteClickCount;
         }
@@ -105,10 +111,10 @@ namespace Avalonia.Input
     {
         public PointerReleasedEventArgs(
             IInteractive source, IPointer pointer,
-            IVisual rootVisual, Point rootVisualPosition, PointerPointProperties properties, InputModifiers modifiers,
-            MouseButton obsoleteMouseButton)
+            IVisual rootVisual, Point rootVisualPosition, ulong timestamp,
+            PointerPointProperties properties, InputModifiers modifiers, MouseButton obsoleteMouseButton)
             : base(InputElement.PointerReleasedEvent, source, pointer, rootVisual, rootVisualPosition,
-                properties, modifiers)
+                timestamp, properties, modifiers)
         {
             MouseButton = obsoleteMouseButton;
         }
@@ -116,4 +122,15 @@ namespace Avalonia.Input
         [Obsolete()]
         public MouseButton MouseButton { get; private set; }
     }
+
+    public class PointerCaptureLostEventArgs : RoutedEventArgs
+    {
+        public IPointer Pointer { get; }
+
+        public PointerCaptureLostEventArgs(IInteractive source, IPointer pointer) : base(InputElement.PointerCaptureLostEvent)
+        {
+            Pointer = pointer;
+            Source = source;
+        }
+    }
 }

+ 3 - 2
src/Avalonia.Input/PointerWheelEventArgs.cs

@@ -11,9 +11,10 @@ namespace Avalonia.Input
         public Vector Delta { get; set; }
 
         public PointerWheelEventArgs(IInteractive source, IPointer pointer, IVisual rootVisual,
-            Point rootVisualPosition,
+            Point rootVisualPosition, ulong timestamp,
             PointerPointProperties properties, InputModifiers modifiers, Vector delta) 
-            : base(InputElement.PointerWheelChangedEvent, source, pointer, rootVisual, rootVisualPosition, properties, modifiers)
+            : base(InputElement.PointerWheelChangedEvent, source, pointer, rootVisual, rootVisualPosition,
+                timestamp, properties, modifiers)
         {
             Delta = delta;
         }

+ 1 - 0
src/Avalonia.Input/Properties/AssemblyInfo.cs

@@ -5,3 +5,4 @@ using System.Reflection;
 using Avalonia.Metadata;
 
 [assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Input")]
+[assembly: XmlnsDefinition("https://github.com/avaloniaui", "Avalonia.Input.GestureRecognizers")]

+ 29 - 0
src/Avalonia.Input/ScrollGestureEventArgs.cs

@@ -0,0 +1,29 @@
+using Avalonia.Interactivity;
+
+namespace Avalonia.Input
+{
+    public class ScrollGestureEventArgs : RoutedEventArgs
+    {
+        public int Id { get; }
+        public Vector Delta { get; }
+        private static int _nextId = 1;
+
+        public static int GetNextFreeId() => _nextId++;
+        
+        public ScrollGestureEventArgs(int id, Vector delta) : base(Gestures.ScrollGestureEvent)
+        {
+            Id = id;
+            Delta = delta;
+        }
+    }
+
+    public class ScrollGestureEndedEventArgs : RoutedEventArgs
+    {
+        public int Id { get; }
+
+        public ScrollGestureEndedEventArgs(int id) : base(Gestures.ScrollGestureEndedEvent)
+        {
+            Id = id;
+        }
+    }
+}

+ 11 - 9
src/Avalonia.Input/TouchDevice.cs

@@ -35,28 +35,30 @@ namespace Avalonia.Input
                 var hit = args.Root.InputHitTest(args.Position);
 
                 _pointers[args.TouchPointId] = pointer = new Pointer(Pointer.GetNextFreeId(),
-                    PointerType.Touch, _pointers.Count == 0, hit);
+                    PointerType.Touch, _pointers.Count == 0);
+                pointer.Capture(hit);
             }
             
 
-            var target = pointer.GetEffectiveCapture() ?? args.Root;
+            var target = pointer.Captured ?? args.Root;
             if (args.Type == RawPointerEventType.TouchBegin)
             {
-                var modifiers = GetModifiers(args.InputModifiers, false);
                 target.RaiseEvent(new PointerPressedEventArgs(target, pointer,
-                    args.Root, args.Position, new PointerPointProperties(modifiers),
-                    modifiers));
+                    args.Root, args.Position, ev.Timestamp,
+                    new PointerPointProperties(GetModifiers(args.InputModifiers, pointer.IsPrimary)),
+                    GetModifiers(args.InputModifiers, false)));
             }
 
             if (args.Type == RawPointerEventType.TouchEnd)
             {
                 _pointers.Remove(args.TouchPointId);
-                var modifiers = GetModifiers(args.InputModifiers, pointer.IsPrimary);
                 using (pointer)
                 {
                     target.RaiseEvent(new PointerReleasedEventArgs(target, pointer,
-                        args.Root, args.Position, new PointerPointProperties(modifiers),
-                        modifiers, pointer.IsPrimary ? MouseButton.Left : MouseButton.None));
+                        args.Root, args.Position, ev.Timestamp,
+                        new PointerPointProperties(GetModifiers(args.InputModifiers, false)),
+                        GetModifiers(args.InputModifiers, pointer.IsPrimary),
+                        pointer.IsPrimary ? MouseButton.Left : MouseButton.None));
                 }
             }
 
@@ -64,7 +66,7 @@ namespace Avalonia.Input
             {
                 var modifiers = GetModifiers(args.InputModifiers, pointer.IsPrimary);
                 target.RaiseEvent(new PointerEventArgs(InputElement.PointerMovedEvent, target, pointer, args.Root,
-                    args.Position, new PointerPointProperties(modifiers), modifiers));
+                    args.Position, ev.Timestamp, new PointerPointProperties(modifiers), modifiers));
             }
         }
         

+ 1 - 3
src/Avalonia.Native/Avalonia.Native.csproj

@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
   <PropertyGroup>
     <IsPackable>false</IsPackable>
@@ -7,8 +7,6 @@
     <CastXmlPath Condition="Exists('/usr/bin/castxml')">/usr/bin/castxml</CastXmlPath>
     <CastXmlPath Condition="Exists('/usr/local/bin/castxml')">/usr/local/bin/castxml</CastXmlPath>
     <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-    <!-- This is needed because Rider doesn't see generated files in obj for some reason -->
-    <SharpGenGeneratedCodeFolder>$(MSBuildThisFileDirectory)/Generated</SharpGenGeneratedCodeFolder>
   </PropertyGroup>
 
   <ItemGroup Condition="'$(Configuration)' == 'Release' AND '$([MSBuild]::IsOSPlatform(OSX))' == 'true'">

+ 18 - 0
src/Avalonia.OpenGL/AngleOptions.cs

@@ -0,0 +1,18 @@
+using System.Collections.Generic;
+
+namespace Avalonia.OpenGL
+{
+    public class AngleOptions
+    {
+        public enum PlatformApi
+        {
+			DirectX9,
+			DirectX11
+        }
+
+        public List<PlatformApi> AllowedPlatformApis = new List<PlatformApi>
+        {
+            PlatformApi.DirectX9
+        };
+    }
+}

+ 50 - 21
src/Avalonia.OpenGL/EglDisplay.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Collections.Generic;
 using System.Runtime.InteropServices;
 using Avalonia.Platform.Interop;
 using static Avalonia.OpenGL.EglConsts;
@@ -13,21 +14,42 @@ namespace Avalonia.OpenGL
         private readonly int[] _contextAttributes;
 
         public IntPtr Handle => _display;
+        private AngleOptions.PlatformApi? _angleApi;
         public EglDisplay(EglInterface egl)
         {
             _egl = egl;  
 
-            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && _egl.GetPlatformDisplayEXT != null)
+            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
             {
-                foreach (var dapi in new[] {EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE})
+                if (_egl.GetPlatformDisplayEXT == null)
+                    throw new OpenGlException("eglGetPlatformDisplayEXT is not supported by libegl.dll");
+                
+                var allowedApis = AvaloniaLocator.Current.GetService<AngleOptions>()?.AllowedPlatformApis
+                              ?? new List<AngleOptions.PlatformApi> {AngleOptions.PlatformApi.DirectX9};              
+                
+                foreach (var platformApi in allowedApis)
                 {
+                    int dapi;
+                    if (platformApi == AngleOptions.PlatformApi.DirectX9)
+                        dapi = EGL_PLATFORM_ANGLE_TYPE_D3D9_ANGLE;
+                    else if (platformApi == AngleOptions.PlatformApi.DirectX11)
+                        dapi = EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE;
+                    else 
+                        continue;
+                    
                     _display = _egl.GetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, IntPtr.Zero, new[]
                     {
                         EGL_PLATFORM_ANGLE_TYPE_ANGLE, dapi, EGL_NONE
                     });
-                    if(_display != IntPtr.Zero)
+                    if (_display != IntPtr.Zero)
+                    {
+                        _angleApi = platformApi;
                         break;
+                    }
                 }
+
+                if (_display == IntPtr.Zero)
+                    throw new OpenGlException("Unable to create ANGLE display");
             }
 
             if (_display == IntPtr.Zero)
@@ -64,29 +86,35 @@ namespace Avalonia.OpenGL
                 if (!_egl.BindApi(cfg.Api))
                     continue;
 
-                var attribs = new[]
+                foreach(var stencilSize in new[]{8, 1, 0})
+                foreach (var depthSize in new []{8, 1, 0})
                 {
-                    EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
-                    EGL_RENDERABLE_TYPE, cfg.RenderableTypeBit,
-                    EGL_RED_SIZE, 8,
-                    EGL_GREEN_SIZE, 8,
-                    EGL_BLUE_SIZE, 8,
-                    EGL_ALPHA_SIZE, 8,
-                    EGL_STENCIL_SIZE, 8,
-                    EGL_DEPTH_SIZE, 8,
-                    EGL_NONE
-                };
-                if (!_egl.ChooseConfig(_display, attribs, out _config, 1, out int numConfigs))
-                    continue;
-                if (numConfigs == 0)
-                    continue;
-                _contextAttributes = cfg.Attributes;
-                Type = cfg.Type;
+                    var attribs = new[]
+                    {
+                        EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
+
+                        EGL_RENDERABLE_TYPE, cfg.RenderableTypeBit,
+
+                        EGL_RED_SIZE, 8,
+                        EGL_GREEN_SIZE, 8,
+                        EGL_BLUE_SIZE, 8,
+                        EGL_ALPHA_SIZE, 8,
+                        EGL_STENCIL_SIZE, stencilSize,
+                        EGL_DEPTH_SIZE, depthSize,
+                        EGL_NONE
+                    };
+                    if (!_egl.ChooseConfig(_display, attribs, out _config, 1, out int numConfigs))
+                        continue;
+                    if (numConfigs == 0)
+                        continue;
+                    _contextAttributes = cfg.Attributes;
+                    Type = cfg.Type;
+                }
             }
 
             if (_contextAttributes == null)
                 throw new OpenGlException("No suitable EGL config was found");
-
+               
             GlInterface = GlInterface.FromNativeUtf8GetProcAddress(b => _egl.GetProcAddress(b));
         }
 
@@ -97,6 +125,7 @@ namespace Avalonia.OpenGL
         
         public GlDisplayType Type { get; }
         public GlInterface GlInterface { get; }
+        public EglInterface EglInterface => _egl;
         public IGlContext CreateContext(IGlContext share)
         {
             var shareCtx = (EglContext)share;

+ 27 - 6
src/Avalonia.OpenGL/EglGlPlatformSurface.cs

@@ -26,31 +26,44 @@ namespace Avalonia.OpenGL
         public IGlPlatformSurfaceRenderTarget CreateGlRenderTarget()
         {
             var glSurface = _display.CreateWindowSurface(_info.Handle);
-            return new RenderTarget(_context, glSurface, _info);
+            return new RenderTarget(_display, _context, glSurface, _info);
         }
 
-        class RenderTarget : IGlPlatformSurfaceRenderTarget
+        class RenderTarget : IGlPlatformSurfaceRenderTargetWithCorruptionInfo
         {
+            private readonly EglDisplay _display;
             private readonly EglContext _context;
             private readonly EglSurface _glSurface;
             private readonly IEglWindowGlPlatformSurfaceInfo _info;
+            private PixelSize _initialSize;
 
-            public RenderTarget(EglContext context, EglSurface glSurface, IEglWindowGlPlatformSurfaceInfo info)
+            public RenderTarget(EglDisplay display, EglContext context,
+                EglSurface glSurface, IEglWindowGlPlatformSurfaceInfo info)
             {
+                _display = display;
                 _context = context;
                 _glSurface = glSurface;
                 _info = info;
+                _initialSize = info.Size;
             }
 
             public void Dispose() => _glSurface.Dispose();
 
+            public bool IsCorrupted => _initialSize != _info.Size;
+            
             public IGlPlatformSurfaceRenderingSession BeginDraw()
             {
                 var l = _context.Lock();
                 try
                 {
+                    if (IsCorrupted)
+                        throw new RenderTargetCorruptedException();
                     _context.MakeCurrent(_glSurface);
-                    return new Session(_context, _glSurface, _info, l);
+                    _display.EglInterface.WaitClient();
+                    _display.EglInterface.WaitGL();
+                    _display.EglInterface.WaitNative();
+                    
+                    return new Session(_display, _context, _glSurface, _info, l);
                 }
                 catch
                 {
@@ -61,15 +74,19 @@ namespace Avalonia.OpenGL
             
             class Session : IGlPlatformSurfaceRenderingSession
             {
-                private readonly IGlContext _context;
+                private readonly EglContext _context;
                 private readonly EglSurface _glSurface;
                 private readonly IEglWindowGlPlatformSurfaceInfo _info;
+                private readonly EglDisplay _display;
                 private IDisposable _lock;
+                
 
-                public Session(IGlContext context, EglSurface glSurface, IEglWindowGlPlatformSurfaceInfo info,
+                public Session(EglDisplay display, EglContext context,
+                    EglSurface glSurface, IEglWindowGlPlatformSurfaceInfo info,
                     IDisposable @lock)
                 {
                     _context = context;
+                    _display = display;
                     _glSurface = glSurface;
                     _info = info;
                     _lock = @lock;
@@ -78,7 +95,11 @@ namespace Avalonia.OpenGL
                 public void Dispose()
                 {
                     _context.Display.GlInterface.Flush();
+                    _display.EglInterface.WaitGL();
                     _glSurface.SwapBuffers();
+                    _display.EglInterface.WaitClient();
+                    _display.EglInterface.WaitGL();
+                    _display.EglInterface.WaitNative();
                     _context.Display.ClearContext();
                     _lock.Dispose();
                 }

+ 42 - 1
src/Avalonia.OpenGL/EglInterface.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Runtime.InteropServices;
 using Avalonia.Platform;
 using Avalonia.Platform.Interop;
 
@@ -15,13 +16,28 @@ namespace Avalonia.OpenGL
         {
         }
 
+        [DllImport("libegl.dll", CharSet = CharSet.Ansi)]
+        static extern IntPtr eglGetProcAddress(string proc);
+        
         static Func<string, bool, IntPtr> Load()
         {
             var os = AvaloniaLocator.Current.GetService<IRuntimePlatform>().GetRuntimeInfo().OperatingSystem;
             if(os == OperatingSystemType.Linux || os == OperatingSystemType.Android)
                 return Load("libEGL.so.1");
             if (os == OperatingSystemType.WinNT)
-                return Load(@"libegl.dll");
+            {
+                var disp = eglGetProcAddress("eglGetPlatformDisplayEXT");
+                if (disp == IntPtr.Zero)
+                    throw new OpenGlException("libegl.dll doesn't have eglGetPlatformDisplayEXT entry point");
+                return (name, optional) =>
+                {
+                    var r = eglGetProcAddress(name);
+                    if (r == IntPtr.Zero && !optional)
+                        throw new OpenGlException($"Entry point {r} is not found");
+                    return r;
+                };
+            }
+
             throw new PlatformNotSupportedException();
         }
 
@@ -91,6 +107,31 @@ namespace Avalonia.OpenGL
         [GlEntryPoint("eglGetConfigAttrib")]
         public EglGetConfigAttrib GetConfigAttrib { get; }
 
+        public delegate bool EglWaitGL();
+        [GlEntryPoint("eglWaitGL")]
+        public EglWaitGL WaitGL { get; }
+        
+        public delegate bool EglWaitClient();
+        [GlEntryPoint("eglWaitClient")]
+        public EglWaitGL WaitClient { get; }
+        
+        public delegate bool EglWaitNative();
+        [GlEntryPoint("eglWaitNative")]
+        public EglWaitGL WaitNative { get; }
+        
+        public delegate IntPtr EglQueryString(IntPtr display, int i);
+        
+        [GlEntryPoint("eglQueryString")]
+        public EglQueryString QueryStringNative { get; }
+
+        public string QueryString(IntPtr display, int i)
+        {
+            var rv = QueryStringNative(display, i);
+            if (rv == IntPtr.Zero)
+                return null;
+            return Marshal.PtrToStringAnsi(rv);
+        }
+
         // ReSharper restore UnassignedGetOnlyAutoProperty
     }
 }

+ 6 - 1
src/Avalonia.OpenGL/IGlPlatformSurfaceRenderTarget.cs

@@ -6,4 +6,9 @@ namespace Avalonia.OpenGL
     {
         IGlPlatformSurfaceRenderingSession BeginDraw();
     }
-}
+
+    public interface IGlPlatformSurfaceRenderTargetWithCorruptionInfo : IGlPlatformSurfaceRenderTarget
+    {
+        bool IsCorrupted { get; }
+    }
+}

+ 2 - 3
src/Avalonia.ReactiveUI/AppBuilderExtensions.cs

@@ -21,9 +21,8 @@ namespace Avalonia.ReactiveUI
             return builder.AfterSetup(_ =>
             {
                 RxApp.MainThreadScheduler = AvaloniaScheduler.Instance;
-                Locator.CurrentMutable.Register(
-                    () => new AvaloniaActivationForViewFetcher(), 
-                    typeof(IActivationForViewFetcher));
+                Locator.CurrentMutable.RegisterConstant(new AvaloniaActivationForViewFetcher(), typeof(IActivationForViewFetcher));
+                Locator.CurrentMutable.RegisterConstant(new AutoDataTemplateBindingHook(), typeof(IPropertyBindingHook));
             });
         }
     }

+ 55 - 0
src/Avalonia.ReactiveUI/AutoDataTemplateBindingHook.cs

@@ -0,0 +1,55 @@
+using System;
+using System.Linq;
+using Avalonia.Controls;
+using Avalonia.Controls.Templates;
+using Avalonia.Layout;
+using Avalonia.Markup.Xaml;
+using Avalonia.Markup.Xaml.Templates;
+using ReactiveUI;
+
+namespace Avalonia.ReactiveUI
+{
+    /// <summary>
+    /// AutoDataTemplateBindingHook is a binding hook that checks ItemsControls
+    /// that don't have DataTemplates, and assigns a default DataTemplate that
+    /// loads the View associated with each ViewModel.
+    /// </summary>
+    public class AutoDataTemplateBindingHook : IPropertyBindingHook
+    {
+        private static FuncDataTemplate DefaultItemTemplate = new FuncDataTemplate<object>(x =>
+        {
+            var control = new ViewModelViewHost();
+            var context = control.GetObservable(Control.DataContextProperty);
+            control.Bind(ViewModelViewHost.ViewModelProperty, context);
+            control.HorizontalContentAlignment = HorizontalAlignment.Stretch;
+            control.VerticalContentAlignment = VerticalAlignment.Stretch;
+            return control;
+        },
+        true);
+
+        /// <inheritdoc/>
+        public bool ExecuteHook(
+            object source, object target,
+            Func<IObservedChange<object, object>[]> getCurrentViewModelProperties,
+            Func<IObservedChange<object, object>[]> getCurrentViewProperties,
+            BindingDirection direction)
+        {
+            var viewProperties = getCurrentViewProperties();
+            var lastViewProperty = viewProperties.LastOrDefault();
+            var itemsControl = lastViewProperty?.Sender as ItemsControl;
+            if (itemsControl == null)
+                return true;
+
+            var propertyName = viewProperties.Last().GetPropertyName();
+            if (propertyName != "Items" &&
+                propertyName != "ItemsSource")
+                return true;
+            
+            if (itemsControl.ItemTemplate != null)
+                return true;
+
+            itemsControl.ItemTemplate = DefaultItemTemplate;
+            return true;
+        }
+    }
+}

+ 17 - 126
src/Avalonia.ReactiveUI/RoutedViewHost.cs

@@ -53,33 +53,13 @@ namespace Avalonia.ReactiveUI
     /// ReactiveUI routing documentation website</see> for more info.
     /// </para>
     /// </remarks>
-    public class RoutedViewHost : UserControl, IActivatable, IEnableLogger
+    public class RoutedViewHost : TransitioningContentControl, IActivatable, IEnableLogger
     {
         /// <summary>
-        /// The router dependency property.
+        /// <see cref="AvaloniaProperty"/> for the <see cref="Router"/> property.
         /// </summary>
         public static readonly AvaloniaProperty<RoutingState> RouterProperty =
             AvaloniaProperty.Register<RoutedViewHost, RoutingState>(nameof(Router));
-        
-        /// <summary>
-        /// The default content property.
-        /// </summary> 
-        public static readonly AvaloniaProperty<object> DefaultContentProperty =
-            AvaloniaProperty.Register<RoutedViewHost, object>(nameof(DefaultContent));
-
-        /// <summary>
-        /// Fade in animation property.
-        /// </summary>
-        public static readonly AvaloniaProperty<IAnimation> FadeInAnimationProperty =
-            AvaloniaProperty.Register<RoutedViewHost, IAnimation>(nameof(DefaultContent),
-                CreateOpacityAnimation(0d, 1d, TimeSpan.FromSeconds(0.25)));
-
-        /// <summary>
-        /// Fade out animation property.
-        /// </summary>
-        public static readonly AvaloniaProperty<IAnimation> FadeOutAnimationProperty =
-            AvaloniaProperty.Register<RoutedViewHost, IAnimation>(nameof(DefaultContent),
-                CreateOpacityAnimation(1d, 0d, TimeSpan.FromSeconds(0.25)));
     
         /// <summary>
         /// Initializes a new instance of the <see cref="RoutedViewHost"/> class.
@@ -104,42 +84,6 @@ namespace Avalonia.ReactiveUI
             set => SetValue(RouterProperty, value);
         }
         
-        /// <summary>
-        /// Gets or sets the content displayed whenever there is no page currently routed.
-        /// </summary>
-        public object DefaultContent
-        {
-            get => GetValue(DefaultContentProperty);
-            set => SetValue(DefaultContentProperty, value);
-        }
-
-        /// <summary>
-        /// Gets or sets the animation played when page appears.
-        /// </summary>
-        public IAnimation FadeInAnimation
-        {
-            get => GetValue(FadeInAnimationProperty);
-            set => SetValue(FadeInAnimationProperty, value);
-        }
-
-        /// <summary>
-        /// Gets or sets the animation played when page disappears.
-        /// </summary>
-        public IAnimation FadeOutAnimation
-        {
-            get => GetValue(FadeOutAnimationProperty);
-            set => SetValue(FadeOutAnimationProperty, value);
-        }
-    
-        /// <summary>
-        /// Duplicates the Content property with a private setter.
-        /// </summary>
-        public new object Content
-        {
-            get => base.Content;
-            private set => base.Content = value;
-        }
-
         /// <summary>
         /// Gets or sets the ReactiveUI view locator used by this router.
         /// </summary>
@@ -149,82 +93,29 @@ namespace Avalonia.ReactiveUI
         /// Invoked when ReactiveUI router navigates to a view model.
         /// </summary>
         /// <param name="viewModel">ViewModel to which the user navigates.</param>
-        /// <exception cref="Exception">
-        /// Thrown when ViewLocator is unable to find the appropriate view.
-        /// </exception>
-        private void NavigateToViewModel(IRoutableViewModel viewModel)
+        private void NavigateToViewModel(object viewModel)
         {
             if (viewModel == null)
             {
-                this.Log().Info("ViewModel is null, falling back to default content.");
-                UpdateContent(DefaultContent);
+                this.Log().Info("ViewModel is null. Falling back to default content.");
+                Content = DefaultContent;
                 return;
             }
     
             var viewLocator = ViewLocator ?? global::ReactiveUI.ViewLocator.Current;
-            var view = viewLocator.ResolveView(viewModel);
-            if (view == null) throw new Exception($"Couldn't find view for '{viewModel}'. Is it registered?");
+            var viewInstance = viewLocator.ResolveView(viewModel);
+            if (viewInstance == null)
+            {
+                this.Log().Warn($"Couldn't find view for '{viewModel}'. Is it registered? Falling back to default content.");
+                Content = DefaultContent;
+                return;
+            }
     
-            this.Log().Info($"Ready to show {view} with autowired {viewModel}.");
-            view.ViewModel = viewModel;
-            if (view is IStyledElement styled)
+            this.Log().Info($"Ready to show {viewInstance} with autowired {viewModel}.");
+            viewInstance.ViewModel = viewModel;
+            if (viewInstance is IStyledElement styled)
                 styled.DataContext = viewModel;
-            UpdateContent(view);
-        }
-    
-        /// <summary>
-        /// Updates the content with transitions.
-        /// </summary>
-        /// <param name="newContent">New content to set.</param>
-        private async void UpdateContent(object newContent)
-        {
-            if (FadeOutAnimation != null)
-                await FadeOutAnimation.RunAsync(this, Clock);
-            Content = newContent;
-            if (FadeInAnimation != null)
-                await FadeInAnimation.RunAsync(this, Clock);
-        }
-    
-        /// <summary>
-        /// Creates opacity animation for this routed view host.
-        /// </summary>
-        /// <param name="from">Opacity to start from.</param>
-        /// <param name="to">Opacity to finish with.</param>
-        /// <param name="duration">Duration of the animation.</param>
-        /// <returns>Animation object instance.</returns>
-        private static IAnimation CreateOpacityAnimation(double from, double to, TimeSpan duration) 
-        {
-            return new Avalonia.Animation.Animation
-            {
-                Duration = duration,
-                Children =
-                {
-                    new KeyFrame
-                    {
-                        Setters =
-                        {
-                            new Setter
-                            {
-                                Property = OpacityProperty,
-                                Value = from
-                            }
-                        },
-                        Cue = new Cue(0d)
-                    },
-                    new KeyFrame
-                    {
-                        Setters =
-                        {
-                            new Setter
-                            {
-                                Property = OpacityProperty,
-                                Value = to
-                            }
-                        },
-                        Cue = new Cue(1d)
-                    }
-                }
-            };
+            Content = viewInstance;
         }
     }
-}
+}

+ 75 - 0
src/Avalonia.ReactiveUI/TransitioningContentControl.cs

@@ -0,0 +1,75 @@
+// Copyright (c) The Avalonia Project. All rights reserved.
+// Licensed under the MIT license. See licence.md file in the project root for full license information.
+
+using System;
+using Avalonia.Animation;
+using Avalonia.Controls;
+using Avalonia.Styling;
+
+namespace Avalonia.ReactiveUI
+{
+    /// <summary>
+    /// A ContentControl that animates the transition when its content is changed.
+    /// </summary>
+    public class TransitioningContentControl : ContentControl, IStyleable
+    {
+        /// <summary>
+        /// <see cref="AvaloniaProperty"/> for the <see cref="PageTransition"/> property.
+        /// </summary>
+        public static readonly AvaloniaProperty<IPageTransition> PageTransitionProperty =
+            AvaloniaProperty.Register<TransitioningContentControl, IPageTransition>(nameof(PageTransition),
+                new CrossFade(TimeSpan.FromSeconds(0.5)));
+
+        /// <summary>
+        /// <see cref="AvaloniaProperty"/> for the <see cref="DefaultContent"/> property.
+        /// </summary>
+        public static readonly AvaloniaProperty<object> DefaultContentProperty =
+            AvaloniaProperty.Register<TransitioningContentControl, object>(nameof(DefaultContent));
+        
+        /// <summary>
+        /// Gets or sets the animation played when content appears and disappears.
+        /// </summary>
+        public IPageTransition PageTransition
+        {
+            get => GetValue(PageTransitionProperty);
+            set => SetValue(PageTransitionProperty, value);
+        }
+
+        /// <summary>
+        /// Gets or sets the content displayed whenever there is no page currently routed.
+        /// </summary>
+        public object DefaultContent
+        {
+            get => GetValue(DefaultContentProperty);
+            set => SetValue(DefaultContentProperty, value);
+        }
+
+        /// <summary>
+        /// Gets or sets the content with animation.
+        /// </summary>
+        public new object Content
+        {
+            get => base.Content;
+            set => UpdateContentWithTransition(value);
+        }
+        
+        /// <summary>
+        /// TransitioningContentControl uses the default ContentControl 
+        /// template from Avalonia default theme.
+        /// </summary>
+        Type IStyleable.StyleKey => typeof(ContentControl);
+
+        /// <summary>
+        /// Updates the content with transitions.
+        /// </summary>
+        /// <param name="content">New content to set.</param>
+        private async void UpdateContentWithTransition(object content)
+        {
+            if (PageTransition != null)
+                await PageTransition.Start(this, null, true);
+            base.Content = content;
+            if (PageTransition != null)
+                await PageTransition.Start(null, this, true);
+        }
+    }
+}

+ 80 - 0
src/Avalonia.ReactiveUI/ViewModelViewHost.cs

@@ -0,0 +1,80 @@
+// Copyright (c) The Avalonia Project. All rights reserved.
+// Licensed under the MIT license. See licence.md file in the project root for full license information.
+
+using System;
+using System.Reactive.Disposables;
+using ReactiveUI;
+using Splat;
+
+namespace Avalonia.ReactiveUI 
+{
+    /// <summary>
+    /// This content control will automatically load the View associated with
+    /// the ViewModel property and display it. This control is very useful
+    /// inside a DataTemplate to display the View associated with a ViewModel.
+    /// </summary>
+    public class ViewModelViewHost : TransitioningContentControl, IViewFor, IEnableLogger
+    {
+        /// <summary>
+        /// <see cref="AvaloniaProperty"/> for the <see cref="ViewModel"/> property.
+        /// </summary>
+        public static readonly AvaloniaProperty<object> ViewModelProperty =
+            AvaloniaProperty.Register<ViewModelViewHost, object>(nameof(ViewModel));
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="ViewModelViewHost"/> class.
+        /// </summary>
+        public ViewModelViewHost()
+        {
+            this.WhenActivated(disposables =>
+            {
+                this.WhenAnyValue(x => x.ViewModel)
+                    .Subscribe(NavigateToViewModel)
+                    .DisposeWith(disposables);
+            });
+        }
+
+        /// <summary>
+        /// Gets or sets the ViewModel to display.
+        /// </summary>
+        public object ViewModel
+        {
+            get => GetValue(ViewModelProperty);
+            set => SetValue(ViewModelProperty, value);
+        }
+        
+        /// <summary>
+        /// Gets or sets the view locator.
+        /// </summary>
+        public IViewLocator ViewLocator { get; set; }
+
+        /// <summary>
+        /// Invoked when ReactiveUI router navigates to a view model.
+        /// </summary>
+        /// <param name="viewModel">ViewModel to which the user navigates.</param>
+        private void NavigateToViewModel(object viewModel)
+        {
+            if (viewModel == null)
+            {
+                this.Log().Info("ViewModel is null. Falling back to default content.");
+                Content = DefaultContent;
+                return;
+            }
+    
+            var viewLocator = ViewLocator ?? global::ReactiveUI.ViewLocator.Current;
+            var viewInstance = viewLocator.ResolveView(viewModel);
+            if (viewInstance == null)
+            {
+                this.Log().Warn($"Couldn't find view for '{viewModel}'. Is it registered? Falling back to default content.");
+                Content = DefaultContent;
+                return;
+            }
+    
+            this.Log().Info($"Ready to show {viewInstance} with autowired {viewModel}.");
+            viewInstance.ViewModel = viewModel;
+            if (viewInstance is IStyledElement styled)
+                styled.DataContext = viewModel;
+            Content = viewInstance;
+        }
+    }
+}

+ 7 - 5
src/Avalonia.Themes.Default/Accents/BaseDark.xaml

@@ -22,6 +22,7 @@
         <Color x:Key="ThemeForegroundLowColor">#FF808080</Color>
 
         <Color x:Key="HighlightColor">#FF119EDA</Color>
+        <Color x:Key="HighlightForegroundColor">#FFFFFFFF</Color>
         <Color x:Key="ErrorColor">#FFFF0000</Color>
         <Color x:Key="ErrorLowColor">#10FF0000</Color>
 
@@ -39,6 +40,7 @@
         <SolidColorBrush x:Key="ThemeForegroundLowBrush" Color="{DynamicResource ThemeForegroundLowColor}"></SolidColorBrush>
 
         <SolidColorBrush x:Key="HighlightBrush" Color="{DynamicResource HighlightColor}"></SolidColorBrush>
+        <SolidColorBrush x:Key="HighlightForegroundBrush" Color="{DynamicResource HighlightForegroundColor}"></SolidColorBrush>
         <SolidColorBrush x:Key="ThemeAccentBrush" Color="{DynamicResource ThemeAccentColor}"></SolidColorBrush>
         <SolidColorBrush x:Key="ThemeAccentBrush2" Color="{DynamicResource ThemeAccentColor2}"></SolidColorBrush>
         <SolidColorBrush x:Key="ThemeAccentBrush3" Color="{DynamicResource ThemeAccentColor3}"></SolidColorBrush>
@@ -46,11 +48,11 @@
         <SolidColorBrush x:Key="ErrorBrush" Color="{DynamicResource ErrorColor}"></SolidColorBrush>
         <SolidColorBrush x:Key="ErrorLowBrush" Color="{DynamicResource ErrorLowColor}"></SolidColorBrush>
 
-        <SolidColorBrush x:Key="NotificationCardBackgroundBrush" Color="#444444"/>
-        <SolidColorBrush x:Key="NotificationCardInformationBackgroundBrush" Color="Teal"/>
-        <SolidColorBrush x:Key="NotificationCardSuccessBackgroundBrush" Color="LimeGreen"/>
-        <SolidColorBrush x:Key="NotificationCardWarningBackgroundBrush" Color="Orange"/>
-        <SolidColorBrush x:Key="NotificationCardErrorBackgroundBrush" Color="OrangeRed"/>
+        <SolidColorBrush x:Key="NotificationCardBackgroundBrush" Color="#444444" Opacity="0.75"/>
+        <SolidColorBrush x:Key="NotificationCardInformationBackgroundBrush" Color="#007ACC" Opacity="0.75"/>
+        <SolidColorBrush x:Key="NotificationCardSuccessBackgroundBrush" Color="#1F9E45" Opacity="0.75"/>
+        <SolidColorBrush x:Key="NotificationCardWarningBackgroundBrush" Color="#FDB328" Opacity="0.75"/>
+        <SolidColorBrush x:Key="NotificationCardErrorBackgroundBrush" Color="#BD202C" Opacity="0.75"/>
 
         <Thickness x:Key="ThemeBorderThickness">1,1,1,1</Thickness>
         <sys:Double x:Key="ThemeDisabledOpacity">0.5</sys:Double>

+ 7 - 5
src/Avalonia.Themes.Default/Accents/BaseLight.xaml

@@ -22,6 +22,7 @@
         <Color x:Key="ThemeForegroundLowColor">#FF808080</Color>
 
         <Color x:Key="HighlightColor">#FF086F9E</Color>
+        <Color x:Key="HighlightForegroundColor">#FFFFFFFF</Color>
         <Color x:Key="ErrorColor">#FFFF0000</Color>
         <Color x:Key="ErrorLowColor">#10FF0000</Color>
 
@@ -39,6 +40,7 @@
         <SolidColorBrush x:Key="ThemeForegroundLowBrush" Color="{DynamicResource ThemeForegroundLowColor}"></SolidColorBrush>
 
         <SolidColorBrush x:Key="HighlightBrush" Color="{DynamicResource HighlightColor}"></SolidColorBrush>
+        <SolidColorBrush x:Key="HighlightForegroundBrush" Color="{DynamicResource HighlightForegroundColor}"></SolidColorBrush>
         <SolidColorBrush x:Key="ThemeAccentBrush" Color="{DynamicResource ThemeAccentColor}"></SolidColorBrush>
         <SolidColorBrush x:Key="ThemeAccentBrush2" Color="{DynamicResource ThemeAccentColor2}"></SolidColorBrush>
         <SolidColorBrush x:Key="ThemeAccentBrush3" Color="{DynamicResource ThemeAccentColor3}"></SolidColorBrush>
@@ -46,11 +48,11 @@
         <SolidColorBrush x:Key="ErrorBrush" Color="{DynamicResource ErrorColor}"></SolidColorBrush>
         <SolidColorBrush x:Key="ErrorLowBrush" Color="{DynamicResource ErrorLowColor}"></SolidColorBrush>
 
-        <SolidColorBrush x:Key="NotificationCardBackgroundBrush" Color="#444444"/>
-        <SolidColorBrush x:Key="NotificationCardInformationBackgroundBrush" Color="Teal"/>
-        <SolidColorBrush x:Key="NotificationCardSuccessBackgroundBrush" Color="LimeGreen"/>
-        <SolidColorBrush x:Key="NotificationCardWarningBackgroundBrush" Color="Orange"/>
-        <SolidColorBrush x:Key="NotificationCardErrorBackgroundBrush" Color="OrangeRed"/>
+        <SolidColorBrush x:Key="NotificationCardBackgroundBrush" Color="#444444" Opacity="0.75"/>
+        <SolidColorBrush x:Key="NotificationCardInformationBackgroundBrush" Color="#007ACC" Opacity="0.75"/>
+        <SolidColorBrush x:Key="NotificationCardSuccessBackgroundBrush" Color="#1F9E45" Opacity="0.75"/>
+        <SolidColorBrush x:Key="NotificationCardWarningBackgroundBrush" Color="#FDB328" Opacity="0.75"/>
+        <SolidColorBrush x:Key="NotificationCardErrorBackgroundBrush" Color="#BD202C" Opacity="0.75"/>
 
         <Thickness x:Key="ThemeBorderThickness">1</Thickness>
         <sys:Double x:Key="ThemeDisabledOpacity">0.5</sys:Double>

+ 3 - 3
src/Avalonia.Themes.Default/Button.xaml

@@ -22,13 +22,13 @@
       </ControlTemplate>
     </Setter>
   </Style>
-  <Style Selector="Button:pointerover /template/ ContentPresenter">
+  <Style Selector="Button:pointerover">
     <Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}"/>
   </Style>
-  <Style Selector="Button:pressed  /template/ ContentPresenter">
+  <Style Selector="Button:pressed">
     <Setter Property="Background" Value="{DynamicResource ThemeControlHighBrush}"/>
   </Style>
   <Style Selector="Button:disabled">
     <Setter Property="Opacity" Value="{DynamicResource ThemeDisabledOpacity}"/>
   </Style>
-</Styles>
+</Styles>

+ 5 - 1
src/Avalonia.Themes.Default/NotificationCard.xaml

@@ -13,7 +13,7 @@
                             BorderBrush="{TemplateBinding BorderBrush}" 
                             BorderThickness="{TemplateBinding BorderThickness}"
                             Margin="8,8,0,0">
-                        <ContentControl MinHeight="150" Content="{TemplateBinding Content}" />
+                        <ContentControl Name="PART_Content" Content="{TemplateBinding Content}" />
                     </Border>
                 </LayoutTransformControl>
             </ControlTemplate>
@@ -40,6 +40,10 @@
         </Style.Animations>
     </Style>
 
+    <Style Selector="NotificationCard/template/ ContentControl#PART_Content">
+        <Setter Property="MinHeight" Value="150" />
+    </Style>
+
     <Style Selector="NotificationCard[IsClosing=true] /template/ LayoutTransformControl#PART_LayoutTransformControl">
         <Setter Property="RenderTransformOrigin" Value="50%,0%"/>
         <Style.Animations>

+ 9 - 2
src/Avalonia.Themes.Default/ScrollViewer.xaml

@@ -12,7 +12,14 @@
                                 Extent="{TemplateBinding Extent, Mode=TwoWay}"
                                 Margin="{TemplateBinding Padding}"
                                 Offset="{TemplateBinding Offset, Mode=TwoWay}"
-                                Viewport="{TemplateBinding Viewport, Mode=TwoWay}"/>
+                                Viewport="{TemplateBinding Viewport, Mode=TwoWay}">
+          <ScrollContentPresenter.GestureRecognizers>
+            <ScrollGestureRecognizer 
+              CanHorizontallyScroll="{TemplateBinding CanHorizontallyScroll}"
+              CanVerticallyScroll="{TemplateBinding CanVerticallyScroll}"
+            />
+          </ScrollContentPresenter.GestureRecognizers>  
+        </ScrollContentPresenter>
         <ScrollBar Name="horizontalScrollBar"
                    Orientation="Horizontal"
                    Maximum="{TemplateBinding HorizontalScrollBarMaximum}"
@@ -32,4 +39,4 @@
       </Grid>
     </ControlTemplate>
   </Setter>
-</Style>
+</Style>

+ 6 - 1
src/Avalonia.Themes.Default/TextBox.xaml

@@ -3,6 +3,8 @@
     <Setter Property="Background" Value="{DynamicResource ThemeBackgroundBrush}"/>
     <Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}"/>
     <Setter Property="BorderThickness" Value="{DynamicResource ThemeBorderThickness}"/>
+    <Setter Property="SelectionBrush" Value="{DynamicResource HighlightBrush}"/>
+    <Setter Property="SelectionForegroundBrush" Value="{DynamicResource HighlightForegroundBrush}"/>
     <Setter Property="Padding" Value="4"/>
     <Setter Property="Template">
       <ControlTemplate>
@@ -44,7 +46,10 @@
                                  SelectionEnd="{TemplateBinding SelectionEnd}"
                                  TextAlignment="{TemplateBinding TextAlignment}"
                                  TextWrapping="{TemplateBinding TextWrapping}"
-                                 PasswordChar="{TemplateBinding PasswordChar}"/>
+                                 PasswordChar="{TemplateBinding PasswordChar}"
+                                 SelectionBrush="{TemplateBinding SelectionBrush}"
+                                 SelectionForegroundBrush="{TemplateBinding SelectionForegroundBrush}"
+                                 CaretBrush="{TemplateBinding CaretBrush}"/>
                 </Panel>
               </ScrollViewer>
             </DataValidationErrors>

+ 4 - 4
src/Avalonia.Themes.Default/ToggleButton.xaml

@@ -22,17 +22,17 @@
       </ControlTemplate>
     </Setter>
   </Style>
-  <Style Selector="ToggleButton:checked /template/ ContentPresenter">
+  <Style Selector="ToggleButton:checked">
     <Setter Property="Background" Value="{DynamicResource ThemeControlHighBrush}"/>
     <Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}"/>
   </Style>
-  <Style Selector="ToggleButton:pointerover /template/ ContentPresenter">
+  <Style Selector="ToggleButton:pointerover">
     <Setter Property="BorderBrush" Value="{DynamicResource ThemeBorderMidBrush}"/>
   </Style>
-  <Style Selector="ToggleButton:pressed  /template/ ContentPresenter">
+  <Style Selector="ToggleButton:pressed">
     <Setter Property="Background" Value="{DynamicResource ThemeControlHighBrush}"/>
   </Style>
   <Style Selector="ToggleButton:disabled">
     <Setter Property="Opacity" Value="{DynamicResource ThemeDisabledOpacity}"/>
   </Style>
-</Styles>
+</Styles>

+ 4 - 4
src/Avalonia.Visuals/Animation/CrossFade.cs

@@ -14,8 +14,8 @@ namespace Avalonia.Animation
     /// </summary>
     public class CrossFade : IPageTransition
     {
-        private Animation _fadeOutAnimation;
-        private Animation _fadeInAnimation;
+        private readonly Animation _fadeOutAnimation;
+        private readonly Animation _fadeInAnimation;
 
         /// <summary>
         /// Initializes a new instance of the <see cref="CrossFade"/> class.
@@ -61,10 +61,10 @@ namespace Avalonia.Animation
                             new Setter
                             {
                                 Property = Visual.OpacityProperty,
-                                Value = 0d
+                                Value = 1d
                             }
                         },
-                        Cue = new Cue(0d)
+                        Cue = new Cue(1d)
                     }
 
                 }

+ 5 - 0
src/Avalonia.Visuals/Platform/IRenderTarget.cs

@@ -23,4 +23,9 @@ namespace Avalonia.Platform
         /// </param>
         IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer);
     }
+
+    public interface IRenderTargetWithCorruptionInfo : IRenderTarget
+    {
+        bool IsCorrupted { get; }
+    }
 }

+ 5 - 0
src/Avalonia.Visuals/Rendering/DeferredRenderer.cs

@@ -245,6 +245,11 @@ namespace Avalonia.Rendering
                         {
                             if (context != null)
                                 return context;
+                            if ((RenderTarget as IRenderTargetWithCorruptionInfo)?.IsCorrupted == true)
+                            {
+                                RenderTarget.Dispose();
+                                RenderTarget = null;
+                            }
                             if (RenderTarget == null)
                                 RenderTarget = ((IRenderRoot)_root).CreateRenderTarget();
                             return context = RenderTarget.CreateDrawingContext(this);

+ 14 - 0
src/Avalonia.Visuals/Rendering/ManagedDeferredRendererLock.cs

@@ -7,11 +7,25 @@ namespace Avalonia.Rendering
     public class ManagedDeferredRendererLock : IDeferredRendererLock
     {
         private readonly object _lock = new object();
+        
+        /// <summary>
+        /// Tries to lock the target surface or window
+        /// </summary>
+        /// <returns>IDisposable if succeeded to obtain the lock</returns>
         public IDisposable TryLock()
         {
             if (Monitor.TryEnter(_lock))
                 return Disposable.Create(() => Monitor.Exit(_lock));
             return null;
         }
+
+        /// <summary>
+        /// Enters a waiting lock, only use from platform code, not from the renderer
+        /// </summary>
+        public IDisposable Lock()
+        {
+            Monitor.Enter(_lock);
+            return Disposable.Create(() => Monitor.Exit(_lock));
+        }
     }
 }

+ 32 - 0
src/Avalonia.Visuals/Rendering/UiThreadRenderTimer.cs

@@ -0,0 +1,32 @@
+using System;
+using System.Diagnostics;
+using System.Reactive.Disposables;
+using Avalonia.Threading;
+
+namespace Avalonia.Rendering
+{
+    /// <summary>
+    /// Render timer that ticks on UI thread. Useful for debugging or bootstrapping on new platforms 
+    /// </summary>
+    
+    public class UiThreadRenderTimer : DefaultRenderTimer
+    {
+        public UiThreadRenderTimer(int framesPerSecond) : base(framesPerSecond)
+        {
+        }
+
+        protected override IDisposable StartCore(Action<TimeSpan> tick)
+        {
+            bool cancelled = false;
+            var st = Stopwatch.StartNew();
+            DispatcherTimer.Run(() =>
+            {
+                if (cancelled)
+                    return false;
+                tick(st.Elapsed);
+                return !cancelled;
+            }, TimeSpan.FromSeconds(1.0 / FramesPerSecond), DispatcherPriority.Render);
+            return Disposable.Create(() => cancelled = true);
+        }
+    }
+}

+ 22 - 2
src/Avalonia.X11/X11CursorFactory.cs

@@ -8,6 +8,8 @@ namespace Avalonia.X11
 {
     class X11CursorFactory : IStandardCursorFactory
     {
+        private static IntPtr _nullCursor;
+
         private readonly IntPtr _display;
         private Dictionary<CursorFontShape, IntPtr> _cursors;
 
@@ -42,16 +44,34 @@ namespace Avalonia.X11
         public X11CursorFactory(IntPtr display)
         {
             _display = display;
+            _nullCursor = GetNullCursor(display);
             _cursors = Enum.GetValues(typeof(CursorFontShape)).Cast<CursorFontShape>()
                 .ToDictionary(id => id, id => XLib.XCreateFontCursor(_display, id));
         }
-        
+
         public IPlatformHandle GetCursor(StandardCursorType cursorType)
         {
-            var handle = s_mapping.TryGetValue(cursorType, out var shape)
+            IntPtr handle;
+            if (cursorType == StandardCursorType.None)
+            {
+                handle = _nullCursor;
+            }
+            else
+            {
+                handle = s_mapping.TryGetValue(cursorType, out var shape)
                 ? _cursors[shape]
                 : _cursors[CursorFontShape.XC_top_left_arrow];
+            }
             return new PlatformHandle(handle, "XCURSOR");
         }
+
+        private static IntPtr GetNullCursor(IntPtr display)
+        {
+            XColor color = new XColor();
+            byte[] data = new byte[] { 0 };
+            IntPtr window = XLib.XRootWindow(display, 0);
+            IntPtr pixmap = XLib.XCreateBitmapFromData(display, window, data, 1, 1);
+            return XLib.XCreatePixmapCursor(display, pixmap, pixmap, ref color, ref color, 0, 0);
+        }
     }
 }

+ 3 - 0
src/Avalonia.X11/XLib.cs

@@ -321,6 +321,9 @@ namespace Avalonia.X11
         public static extern IntPtr XCreatePixmapCursor(IntPtr display, IntPtr source, IntPtr mask,
             ref XColor foreground_color, ref XColor background_color, int x_hot, int y_hot);
 
+        [DllImport(libX11)]
+        public static extern IntPtr XCreateBitmapFromData(IntPtr display, IntPtr drawable, byte[] data, int width, int height);
+
         [DllImport(libX11)]
         public static extern IntPtr XCreatePixmapFromBitmapData(IntPtr display, IntPtr drawable, byte[] data, int width,
             int height, IntPtr fg, IntPtr bg, int depth);

+ 2 - 1
src/Gtk/Avalonia.Gtk3/CursorFactory.cs

@@ -12,6 +12,7 @@ namespace Avalonia.Gtk3
         private static readonly Dictionary<StandardCursorType, object> CursorTypeMapping = new Dictionary
     <StandardCursorType, object>
         {
+            {StandardCursorType.None, CursorType.Blank},
             {StandardCursorType.AppStarting, CursorType.Watch},
             {StandardCursorType.Arrow, CursorType.LeftPtr},
             {StandardCursorType.Cross, CursorType.Cross},
@@ -80,4 +81,4 @@ namespace Avalonia.Gtk3
             return rv;
         }
     }
-}
+}

+ 1 - 0
src/Gtk/Avalonia.Gtk3/GdkCursor.cs

@@ -2,6 +2,7 @@
 {
     enum GdkCursorType
     {
+        Blank = -2,
         CursorIsPixmap = -1,
         XCursor = 0,
         Arrow = 2,

+ 2 - 19
src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj

@@ -11,38 +11,22 @@
     <ItemGroup>
         <Compile Include="AvaloniaXamlLoader.cs" />
         <Compile Include="Converters\AvaloniaUriTypeConverter.cs" />
-        <Compile Include="Converters\AvaloniaEventConverter.cs" />
         <Compile Include="Converters\FontFamilyTypeConverter.cs" />
         <Compile Include="Converters\MemberSelectorTypeConverter.cs" />
-        <Compile Include="Converters\NullableTypeConverter.cs" />
-        <Compile Include="Converters\ParseTypeConverter.cs" />
-        <Compile Include="Converters\SetterValueTypeConverter.cs" />
         <Compile Include="Converters\TimeSpanTypeConverter.cs" />
         <Compile Include="Extensions.cs" />
+        <Compile Include="MarkupExtension.cs" />
         <Compile Include="MarkupExtensions\DynamicResourceExtension.cs" />
         <Compile Include="MarkupExtensions\ResourceInclude.cs" />
         <Compile Include="MarkupExtensions\StaticResourceExtension.cs" />
         <Compile Include="MarkupExtensions\StyleIncludeExtension.cs" />
         <Compile Include="Parsers\PropertyParser.cs" />
-        <Compile Include="PortableXaml\AvaloniaResourceXamlInfo.cs" />
-        <Compile Include="PortableXaml\AvaloniaXamlContext.cs" />
-        <Compile Include="PortableXaml\AttributeExtensions.cs" />
-        <Compile Include="PortableXaml\AvaloniaMemberAttributeProvider.cs" />
-        <Compile Include="PortableXaml\AvaloniaNameScope.cs" />
-        <Compile Include="AvaloniaTypeConverters.cs" />
-        <Compile Include="PortableXaml\AvaloniaXamlObjectWriter.cs" />
-        <Compile Include="PortableXaml\AvaloniaRuntimeTypeProvider.cs" />
-        <Compile Include="PortableXaml\AvaloniaXamlSchemaContext.cs" />
         <Compile Include="Converters\BitmapTypeConverter.cs" />
         <Compile Include="Converters\IconTypeConverter.cs" />
         <Compile Include="Converters\AvaloniaPropertyTypeConverter.cs" />
         <Compile Include="Converters\PointsListTypeConverter.cs" />
-        <Compile Include="Converters\SelectorTypeConverter.cs" />
         <Compile Include="MarkupExtensions\BindingExtension.cs" />
         <Compile Include="MarkupExtensions\RelativeSourceExtension.cs" />
-        <Compile Include="PortableXaml\AvaloniaTypeAttributeProvider.cs" />
-        <Compile Include="PortableXaml\AvaloniaXamlType.cs" />
-        <Compile Include="PortableXaml\TypeDescriptorExtensions.cs" />
         <Compile Include="Properties\AssemblyInfo.cs" />
         <Compile Include="Styling\StyleInclude.cs" />
         <Compile Include="Templates\ControlTemplate.cs" />
@@ -52,7 +36,6 @@
         <Compile Include="Templates\MemberSelector.cs" />
         <Compile Include="Templates\Template.cs" />
         <Compile Include="Templates\TemplateContent.cs" />
-        <Compile Include="Templates\TemplateLoader.cs" />
         <Compile Include="Templates\TreeDataTemplate.cs" />
         <Compile Include="XamlIl\AvaloniaXamlIlRuntimeCompiler.cs" />
         <Compile Include="XamlIl\CompilerExtensions\Transformers\AvaloniaBindingExtensionHackTransformer.cs" />
@@ -76,11 +59,11 @@
         <Compile Include="XamlIl\Runtime\IAvaloniaXamlIlXmlNamespaceInfoProviderV1.cs" />
         <Compile Include="XamlIl\Runtime\XamlIlRuntimeHelpers.cs" />
         <Compile Include="XamlLoadException.cs" />
-        <Compile Include="PortableXaml\portable.xaml.github\src\Portable.Xaml\**\*.cs" Exclude="PortableXaml\portable.xaml.github\src\Portable.Xaml\Assembly\**\*.cs" />
         <Compile Include="XamlIl\xamlil.github\src\XamlIl\**\*.cs" />
         <Compile Condition="$(UseCecil) == true" Include="XamlIl\xamlil.github\src\XamlIl.Cecil\**\*.cs" />
         <Compile Remove="XamlIl\xamlil.github\**\obj\**\*.cs" />
         <Compile Include="..\Avalonia.Markup\Markup\Parsers\SelectorGrammar.cs" />
+        <Compile Include="XamlTypes.cs" />
     </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\..\Avalonia.Animation\Avalonia.Animation.csproj" />

+ 0 - 107
src/Markup/Avalonia.Markup.Xaml/AvaloniaTypeConverters.cs

@@ -1,107 +0,0 @@
-using System;
-using System.ComponentModel;
-using System.Collections.Generic;
-using System.Globalization;
-using Avalonia.Collections;
-using Avalonia.Controls;
-using Avalonia.Markup.Xaml.Converters;
-using Avalonia.Media.Imaging;
-using Avalonia.Styling;
-using Avalonia.Controls.Templates;
-
-namespace Avalonia.Markup.Xaml
-{
-    using System.Reflection;
-    using Avalonia.Media;
-
-    /// <summary>
-    /// Maintains a repository of <see cref="TypeConverter"/>s for XAML parsing on top of those
-    /// maintained by <see cref="TypeDescriptor"/>.
-    /// </summary>
-    /// <remarks>
-    /// The default method of defining type converters using <see cref="TypeConverterAttribute"/>
-    /// isn't powerful enough for our purposes:
-    /// 
-    /// - It doesn't handle non-constructed generic types (such as <see cref="AvaloniaList{T}"/>)
-    /// - Type converters which require XAML features cannot be defined in non-XAML assemblies and
-    ///   so can't be referenced using <see cref="TypeConverterAttribute"/>
-    /// - Many types have a static `Parse(string)` method which can be used implicitly; this class
-    ///   detects such methods and auto-creates a type converter
-    /// </remarks>
-    public static class AvaloniaTypeConverters
-    {
-        // When adding item to that list make sure to modify AvaloniaXamlIlLanguage
-        private static Dictionary<Type, Type> _converters = new Dictionary<Type, Type>()
-        {
-            { typeof(AvaloniaList<>), typeof(AvaloniaListConverter<>) },
-            { typeof(AvaloniaProperty), typeof(AvaloniaPropertyTypeConverter) },
-            { typeof(IBitmap), typeof(BitmapTypeConverter) },
-            { typeof(IList<Point>), typeof(PointsListTypeConverter) },
-            { typeof(IMemberSelector), typeof(MemberSelectorTypeConverter) },
-            { typeof(Selector), typeof(SelectorTypeConverter) },
-            { typeof(TimeSpan), typeof(TimeSpanTypeConverter) },
-            { typeof(WindowIcon), typeof(IconTypeConverter) },
-            { typeof(CultureInfo), typeof(CultureInfoConverter) },
-            { typeof(Uri), typeof(AvaloniaUriTypeConverter) },
-            { typeof(FontFamily), typeof(FontFamilyTypeConverter) },
-            { typeof(EventInfo), typeof(AvaloniaEventConverter) },
-        };
-
-        internal static Type GetBuiltinTypeConverter(Type type)
-        {
-            _converters.TryGetValue(type, out var result);
-            return result;
-        }
-
-        /// <summary>
-        /// Tries to lookup a <see cref="TypeConverter"/> for a type.
-        /// </summary>
-        /// <param name="type">The type.</param>
-        /// <returns>The type converter.</returns>
-        public static Type GetTypeConverter(Type type)
-        {
-            if (type.IsConstructedGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
-            {
-                var inner = GetTypeConverter(type.GetGenericArguments()[0]);
-                if (inner == null)
-                    return null;
-                return typeof(NullableTypeConverter<>).MakeGenericType(inner);
-            }
-            
-            if (_converters.TryGetValue(type, out var result))
-            {
-                return result;
-            }
-
-            // Converters for non-constructed generic types can't be specified using
-            // TypeConverterAttribute. Allow them to be registered here and handle them sanely.
-            if (type.IsConstructedGenericType &&
-                _converters.TryGetValue(type.GetGenericTypeDefinition(), out result))
-            {
-                return result?.MakeGenericType(type.GetGenericArguments());
-            }
-
-            // If the type isn't a primitive or a type that XAML already handles, but has a static
-            // Parse method, use that
-            if (!type.IsPrimitive &&
-                type != typeof(DateTime) &&
-                type != typeof(Uri) &&
-                ParseTypeConverter.HasParseMethod(type))
-            {
-                result = typeof(ParseTypeConverter<>).MakeGenericType(type);
-                _converters.Add(type, result);
-                return result;
-            }
-
-            _converters.Add(type, null);
-            return null;
-        }
-
-        /// <summary>
-        /// Registers a type converter for a type.
-        /// </summary>
-        /// <param name="type">The type. Maybe be a non-constructed generic type.</param>
-        /// <param name="converterType">The converter type. Maybe be a non-constructed generic type.</param>
-        public static void Register(Type type, Type converterType) => _converters[type] = converterType;
-    }
-}

+ 6 - 153
src/Markup/Avalonia.Markup.Xaml/AvaloniaXamlLoader.cs

@@ -15,7 +15,6 @@ using Avalonia.Controls;
 using Avalonia.Markup.Data;
 using Avalonia.Markup.Xaml.PortableXaml;
 using Avalonia.Platform;
-using Portable.Xaml;
 
 namespace Avalonia.Markup.Xaml
 {
@@ -26,71 +25,14 @@ namespace Avalonia.Markup.Xaml
     {
         public bool IsDesignMode { get; set; }
 
-        public static bool UseLegacyXamlLoader { get; set; } = false;
-        
-        /// <summary>
-        /// Initializes a new instance of the <see cref="AvaloniaXamlLoader"/> class.
-        /// </summary>
-        public AvaloniaXamlLoader()
-        {
-        }
-
         /// <summary>
         /// Loads the XAML into a Avalonia component.
         /// </summary>
         /// <param name="obj">The object to load the XAML into.</param>
         public static void Load(object obj)
         {
-            Contract.Requires<ArgumentNullException>(obj != null);
-
-            var loader = new AvaloniaXamlLoader();
-            loader.Load(obj.GetType(), obj);
-        }
-
-        /// <summary>
-        /// Loads the XAML for a type.
-        /// </summary>
-        /// <param name="type">The type.</param>
-        /// <param name="rootInstance">
-        /// The optional instance into which the XAML should be loaded.
-        /// </param>
-        /// <returns>The loaded object.</returns>
-        public object Load(Type type, object rootInstance = null)
-        {
-            Contract.Requires<ArgumentNullException>(type != null);
-
-            // HACK: Currently Visual Studio is forcing us to change the extension of xaml files
-            // in certain situations, so we try to load .xaml and if that's not found we try .xaml.
-            // Ideally we'd be able to use .xaml everywhere
-            var assetLocator = AvaloniaLocator.Current.GetService<IAssetLoader>();
-
-            if (assetLocator == null)
-            {
-                throw new InvalidOperationException(
-                    "Could not create IAssetLoader : maybe Application.RegisterServices() wasn't called?");
-            }           
-            
-            foreach (var uri in GetUrisFor(assetLocator, type))
-            {
-                if (assetLocator.Exists(uri))
-                {
-                    using (var stream = assetLocator.Open(uri))
-                    {
-                        var initialize = rootInstance as ISupportInitialize;
-                        initialize?.BeginInit();
-                        try
-                        {
-                            return Load(stream, type.Assembly, rootInstance, uri);
-                        }
-                        finally
-                        {
-                            initialize?.EndInit();
-                        }
-                    }
-                }
-            }
-
-            throw new FileNotFoundException("Unable to find view for " + type.FullName);
+            throw new XamlLoadException(
+                $"No precompiled XAML found for {obj.GetType()}, make sure to specify x:Class and include your XAML file as AvaloniaResource");
         }
 
         /// <summary>
@@ -100,11 +42,8 @@ namespace Avalonia.Markup.Xaml
         /// <param name="baseUri">
         /// A base URI to use if <paramref name="uri"/> is relative.
         /// </param>
-        /// <param name="rootInstance">
-        /// The optional instance into which the XAML should be loaded.
-        /// </param>
         /// <returns>The loaded object.</returns>
-        public object Load(Uri uri, Uri baseUri = null, object rootInstance = null)
+        public object Load(Uri uri, Uri baseUri = null)
         {
             Contract.Requires<ArgumentNullException>(uri != null);
 
@@ -133,7 +72,7 @@ namespace Avalonia.Markup.Xaml
             using (var stream = asset.stream)
             {
                 var absoluteUri = uri.IsAbsoluteUri ? uri : new Uri(baseUri, uri);
-                return Load(stream, asset.assembly, rootInstance, absoluteUri);
+                return Load(stream, asset.assembly, null, absoluteUri);
             }
         }
         
@@ -166,95 +105,9 @@ namespace Avalonia.Markup.Xaml
         /// </param>
         /// <param name="uri">The URI of the XAML</param>
         /// <returns>The loaded object.</returns>
-        public object Load(Stream stream, Assembly localAssembly, object rootInstance = null, Uri uri = null)
-        {
-            if (!UseLegacyXamlLoader)
-                return AvaloniaXamlIlRuntimeCompiler.Load(stream, localAssembly, rootInstance, uri, IsDesignMode);
-
-
-            var readerSettings = new XamlXmlReaderSettings()
-            {
-                BaseUri = uri,
-                LocalAssembly = localAssembly,
-                ProvideLineInfo = true,
-            };
-
-            var context = IsDesignMode ? AvaloniaXamlSchemaContext.DesignInstance : AvaloniaXamlSchemaContext.Instance;
-            var reader = new XamlXmlReader(stream, context, readerSettings);
-
-            object result = LoadFromReader(
-                reader,
-                AvaloniaXamlContext.For(readerSettings, rootInstance));
-
-            var topLevel = result as TopLevel;
-
-            if (topLevel != null)
-            {
-                DelayedBinding.ApplyBindings(topLevel);
-            }
-
-            return result;
-        }
-
-        internal static object LoadFromReader(XamlReader reader, AvaloniaXamlContext context = null, IAmbientProvider parentAmbientProvider = null)
-        {
-            var writer = AvaloniaXamlObjectWriter.Create(
-                                    (AvaloniaXamlSchemaContext)reader.SchemaContext,
-                                    context,
-                                    parentAmbientProvider);
+        public object Load(Stream stream, Assembly localAssembly, object rootInstance = null, Uri uri = null) 
+            => AvaloniaXamlIlRuntimeCompiler.Load(stream, localAssembly, rootInstance, uri, IsDesignMode);
 
-            XamlServices.Transform(reader, writer);
-            writer.ApplyAllDelayedProperties();
-            return writer.Result;
-        }
-
-        internal static object LoadFromReader(XamlReader reader)
-        {
-            //return XamlServices.Load(reader);
-            return LoadFromReader(reader, null);
-        }
-
-
-        private static readonly DataContractSerializer s_xamlInfoSerializer =
-            new DataContractSerializer(typeof(AvaloniaResourceXamlInfo));
-        /// <summary>
-        /// Gets the URI for a type.
-        /// </summary>
-        /// <param name="assetLocator"></param>
-        /// <param name="type">The type.</param>
-        /// <returns>The URI.</returns>
-        private static IEnumerable<Uri> GetUrisFor(IAssetLoader assetLocator, Type type)
-        {
-            var asm = type.GetTypeInfo().Assembly.GetName().Name;
-            var xamlInfoUri = new Uri($"avares://{asm}/!AvaloniaResourceXamlInfo");
-            var typeName = type.FullName;
-            if (typeName == null)
-                throw new ArgumentException("Type doesn't have a FullName");
-            
-            if (assetLocator.Exists(xamlInfoUri))
-            {
-                using (var xamlInfoStream = assetLocator.Open(xamlInfoUri))
-                {
-                    var assetDoc = XDocument.Load(xamlInfoStream);
-                    XNamespace assetNs = assetDoc.Root.Attribute("xmlns").Value;
-                    XNamespace arrayNs = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";
-                    Dictionary<string,string> xamlInfo =
-                        assetDoc.Root.Element(assetNs + "ClassToResourcePathIndex").Elements(arrayNs + "KeyValueOfstringstring")
-                         .ToDictionary(entry =>entry.Element(arrayNs + "Key").Value,
-                                entry => entry.Element(arrayNs + "Value").Value);
-                    
-                    if (xamlInfo.TryGetValue(typeName, out var rv))
-                    {
-                        yield return new Uri($"avares://{asm}{rv}");
-                        yield break;
-                    }
-                }
-            }           
-            
-            yield return new Uri("resm:" + typeName + ".xaml?assembly=" + asm);
-            yield return new Uri("resm:" + typeName + ".paml?assembly=" + asm);
-        }
-        
         public static object Parse(string xaml, Assembly localAssembly = null)
             => new AvaloniaXamlLoader().Load(xaml, localAssembly);
 

+ 0 - 99
src/Markup/Avalonia.Markup.Xaml/Converters/AvaloniaEventConverter.cs

@@ -1,99 +0,0 @@
-using System;
-using System.ComponentModel;
-using System.Globalization;
-using System.Linq;
-using System.Linq.Expressions;
-using System.Reflection;
-using Avalonia.Controls;
-using Avalonia.Markup.Xaml.PortableXaml;
-using Portable.Xaml;
-
-namespace Avalonia.Markup.Xaml.Converters
-{
-    internal class AvaloniaEventConverter : TypeConverter
-    {
-        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
-        {
-            return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
-        }
-
-        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
-        {
-            var text = value as string;
-            if (text != null)
-            {
-                var rootObjectProvider = context.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider;
-                var destinationTypeProvider = context.GetService(typeof(IDestinationTypeProvider)) as IDestinationTypeProvider;
-                if (rootObjectProvider != null && destinationTypeProvider != null)
-                {
-                    var target = rootObjectProvider.RootObject;
-                    var eventType = destinationTypeProvider.GetDestinationType();
-                    var eventParameters = eventType.GetRuntimeMethods().First(r => r.Name == "Invoke").GetParameters();
-                    // go in reverse to match System.Xaml behaviour
-                    var methods = target.GetType().GetRuntimeMethods().Reverse();
-
-                    // find based on exact match parameter types first
-                    foreach (var method in methods)
-                    {
-                        if (method.Name != text)
-                            continue;
-                        var parameters = method.GetParameters();
-                        if (eventParameters.Length != parameters.Length)
-                            continue;
-                        if (parameters.Length == 0)
-                            return method.CreateDelegate(eventType, target);
-
-                        for (int i = 0; i < parameters.Length; i++)
-                        {
-                            var param = parameters[i];
-                            var eventParam = eventParameters[i];
-                            if (param.ParameterType != eventParam.ParameterType)
-                                break;
-                            if (i == parameters.Length - 1)
-                                return method.CreateDelegate(eventType, target);
-                        }
-                    }
-
-                    // EnhancedXaml: Find method with compatible base class parameters
-                    foreach (var method in methods)
-                    {
-                        if (method.Name != text)
-                            continue;
-                        var parameters = method.GetParameters();
-                        if (parameters.Length == 0 || eventParameters.Length != parameters.Length)
-                            continue;
-
-                        for (int i = 0; i < parameters.Length; i++)
-                        {
-                            var param = parameters[i];
-                            var eventParam = eventParameters[i];
-                            if (!param.ParameterType.GetTypeInfo().IsAssignableFrom(eventParam.ParameterType.GetTypeInfo()))
-                                break;
-                            if (i == parameters.Length - 1)
-                                return method.CreateDelegate(eventType, target);
-                        }
-                    }
-
-                    var contextProvider = (IXamlSchemaContextProvider)context.GetService(typeof(IXamlSchemaContextProvider));
-                    var avaloniaContext = (AvaloniaXamlSchemaContext)contextProvider.SchemaContext;
-
-                    if (avaloniaContext.IsDesignMode)
-                    {
-                        // We want to ignore missing events in the designer, so if event handler
-                        // wasn't found create an empty delegate.
-                        var lambdaExpression = Expression.Lambda(
-                            eventType,
-                            Expression.Empty(),
-                            eventParameters.Select(x => Expression.Parameter(x.ParameterType)));
-                        return lambdaExpression.Compile();
-                    }
-                    else
-                    {
-                        throw new XamlObjectWriterException($"Referenced value method {text} in type {target.GetType()} indicated by event {eventType.FullName} was not found");
-                    }
-                }
-            }
-            return base.ConvertFrom(context, culture, value);
-        }
-    }
-}

+ 0 - 1
src/Markup/Avalonia.Markup.Xaml/Converters/AvaloniaPropertyTypeConverter.cs

@@ -11,7 +11,6 @@ using Avalonia.Markup.Xaml.Parsers;
 using Avalonia.Markup.Xaml.Templates;
 using Avalonia.Styling;
 using Avalonia.Utilities;
-using Portable.Xaml.ComponentModel;
 
 namespace Avalonia.Markup.Xaml.Converters
 {

+ 1 - 2
src/Markup/Avalonia.Markup.Xaml/Converters/BitmapTypeConverter.cs

@@ -8,8 +8,7 @@ using Avalonia.Platform;
 
 namespace Avalonia.Markup.Xaml.Converters
 {
-    using Portable.Xaml.ComponentModel;
-	using System.ComponentModel;
+    using System.ComponentModel;
 
     public class BitmapTypeConverter : TypeConverter
     {

+ 0 - 1
src/Markup/Avalonia.Markup.Xaml/Converters/FontFamilyTypeConverter.cs

@@ -7,7 +7,6 @@ using System.Globalization;
 
 using Avalonia.Media;
 
-using Portable.Xaml.ComponentModel;
 
 namespace Avalonia.Markup.Xaml.Converters
 {

+ 0 - 1
src/Markup/Avalonia.Markup.Xaml/Converters/IconTypeConverter.cs

@@ -9,7 +9,6 @@ using System.Globalization;
 
 namespace Avalonia.Markup.Xaml.Converters
 {
-    using Portable.Xaml.ComponentModel;
 	using System.ComponentModel;
 
     public class IconTypeConverter : TypeConverter

+ 0 - 89
src/Markup/Avalonia.Markup.Xaml/Converters/NullableTypeConverter.cs

@@ -1,89 +0,0 @@
-using System;
-using System.Collections;
-using System.ComponentModel;
-using System.Globalization;
-
-namespace Avalonia.Markup.Xaml.Converters
-{
-    public class NullableTypeConverter<T> : TypeConverter where T : TypeConverter, new()
-    {
-        private TypeConverter _inner;
-
-        public NullableTypeConverter()
-        {
-            _inner = new T();
-        }
-
-        public NullableTypeConverter(TypeConverter inner)
-        {
-            _inner = inner;
-        }
-
-        
-        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
-        {
-            if (value == null)
-                return null;
-            return _inner.ConvertTo(context, culture, value, destinationType);
-        }
-
-        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
-        {
-            if (value == null)
-                return null;
-            if (value as string == "")
-                return null;
-            return _inner.ConvertFrom(context, culture, value);
-        }
-        
-        public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
-        {
-            return _inner.CreateInstance(context, propertyValues);
-        }
-        
-        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
-        {
-            return _inner.GetStandardValuesSupported(context);
-        }
-
-        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
-        {
-            return _inner.GetStandardValuesExclusive(context);
-        }
-
-        public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
-        {
-            return _inner.GetCreateInstanceSupported(context);
-        }
-
-        public override bool GetPropertiesSupported(ITypeDescriptorContext context)
-        {
-            return _inner.GetPropertiesSupported(context);
-        }
-
-        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
-        {
-            return _inner.GetStandardValues(context);
-        }
-
-        public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
-        {
-            return _inner.GetProperties(context, value, attributes);
-        }
-
-        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
-        {
-            return _inner.CanConvertTo(context, destinationType);
-        }
-
-        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
-        {
-            return _inner.CanConvertFrom(context, sourceType);
-        }
-
-        public override bool IsValid(ITypeDescriptorContext context, object value)
-        {
-            return _inner.IsValid(context, value);
-        }
-    }
-}

+ 0 - 79
src/Markup/Avalonia.Markup.Xaml/Converters/ParseTypeConverter.cs

@@ -1,79 +0,0 @@
-using System;
-using System.ComponentModel;
-using System.Globalization;
-using System.Reflection;
-
-namespace Avalonia.Markup.Xaml.Converters
-{
-    /// <summary>
-    /// Base class for type converters which call a static Parse method.
-    /// </summary>
-    public abstract class ParseTypeConverter : TypeConverter
-    {
-        protected const BindingFlags PublicStatic = BindingFlags.Public | BindingFlags.Static;
-        protected static readonly Type[] StringParameter = new[] { typeof(string) };
-        protected static readonly Type[] StringIFormatProviderParameters = new[] { typeof(string), typeof(IFormatProvider) };
-
-        /// <summary>
-        /// Checks whether a type has a suitable Parse method.
-        /// </summary>
-        /// <param name="type">The type.</param>
-        /// <returns>True if the type has a suitable parse method, otherwise false.</returns>
-        public static bool HasParseMethod(Type type)
-        {
-            return type.GetMethod("Parse", PublicStatic, null, StringIFormatProviderParameters, null) != null ||
-                   type.GetMethod("Parse", PublicStatic, null, StringParameter, null) != null;
-        }
-    }
-
-    /// <summary>
-    /// A type converter which calls a static Parse method.
-    /// </summary>
-    /// <typeparam name="T">The type with the Parse method.</typeparam>
-    public class ParseTypeConverter<T> : ParseTypeConverter
-    {
-        private static Func<string, T> _parse;
-        private static Func<string, IFormatProvider, T> _parseWithFormat;
-
-        static ParseTypeConverter()
-        {
-            var method = typeof(T).GetMethod("Parse", PublicStatic, null, StringIFormatProviderParameters, null);
-
-            if (method != null)
-            {
-                _parseWithFormat = (Func<string, IFormatProvider, T>)method
-                    .CreateDelegate(typeof(Func<string, IFormatProvider, T>));
-                return;
-            }
-
-            method = typeof(T).GetMethod("Parse", PublicStatic, null, StringParameter, null);
-
-            if (method != null)
-            {
-                _parse = (Func<string, T>)method.CreateDelegate(typeof(Func<string, T>));
-            }
-        }
-
-        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
-        {
-            return sourceType == typeof(string);
-        }
-
-        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
-        {
-            if (value != null)
-            {
-                if (_parse != null)
-                {
-                    return _parse(value.ToString());
-                }
-                else if (_parseWithFormat != null)
-                {
-                    return _parseWithFormat(value.ToString(), culture);
-                }
-            }
-
-            return null;
-        }
-    }
-}

+ 0 - 27
src/Markup/Avalonia.Markup.Xaml/Converters/SelectorTypeConverter.cs

@@ -1,27 +0,0 @@
-// Copyright (c) The Avalonia Project. All rights reserved.
-// Licensed under the MIT license. See licence.md file in the project root for full license information.
-
-using System;
-using System.Globalization;
-using Avalonia.Markup.Parsers;
-
-namespace Avalonia.Markup.Xaml.Converters
-{
-    using Portable.Xaml.ComponentModel;
-    using System.ComponentModel;
-
-    public class SelectorTypeConverter : TypeConverter
-    {
-        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
-        {
-            return sourceType == typeof(string);
-        }
-
-        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
-        {
-            var parser = new SelectorParser(context.ResolveType);
-
-            return parser.Parse((string)value);
-        }
-    }
-}

+ 0 - 49
src/Markup/Avalonia.Markup.Xaml/Converters/SetterValueTypeConverter.cs

@@ -1,49 +0,0 @@
-// Copyright (c) The Avalonia Project. All rights reserved.
-// Licensed under the MIT license. See licence.md file in the project root for full license information.
-
-using Avalonia.Styling;
-using Portable.Xaml;
-using Portable.Xaml.ComponentModel;
-using System.ComponentModel;
-using Portable.Xaml.Markup;
-using System;
-using System.Globalization;
-
-namespace Avalonia.Markup.Xaml.Converters
-{
-    public class SetterValueTypeConverter : TypeConverter
-    {
-        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
-        {
-            return sourceType == typeof(string);
-        }
-
-        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
-        {
-            object setter = context.GetService<IProvideValueTarget>().TargetObject;
-            var schemaContext = context.GetService<IXamlSchemaContextProvider>().SchemaContext;
-
-            return ConvertSetterValue(context, schemaContext, culture, (setter as Setter), value);
-        }
-
-        [Obsolete("TODO: try assosiate Setter.Value property with SetterValueTypeConverter, so far coouldn't make it :(")]
-        internal static object ConvertSetterValue(ITypeDescriptorContext dcontext, XamlSchemaContext context, CultureInfo info, Setter setter, object value)
-        {
-            Type targetType = setter?.Property?.PropertyType;
-
-            if (targetType == null)
-            {
-                return value;
-            }
-
-            var ttConv = context.GetXamlType(targetType)?.TypeConverter?.ConverterInstance;
-
-            if (ttConv != null)
-            {
-                value = ttConv.ConvertFromString(dcontext, info, value as string);
-            }
-
-            return value;
-        }
-    }
-}

+ 8 - 33
src/Markup/Avalonia.Markup.Xaml/Extensions.cs

@@ -3,8 +3,6 @@ using System.Collections.Generic;
 using System.ComponentModel;
 using System.Linq;
 using Avalonia.Markup.Xaml.XamlIl.Runtime;
-using Portable.Xaml;
-using Portable.Xaml.Markup;
 
 namespace Avalonia.Markup.Xaml
 {
@@ -13,42 +11,19 @@ namespace Avalonia.Markup.Xaml
         public static T GetService<T>(this IServiceProvider sp) => (T)sp?.GetService(typeof(T));
         
         
-        public static Uri GetContextBaseUri(this IServiceProvider ctx)
-        {
-            var properService = ctx.GetService<IUriContext>();
-            if (properService != null)
-                return properService.BaseUri;
-            // Ugly hack with casts
-            return Portable.Xaml.ComponentModel.TypeDescriptorExtensions.GetBaseUri((ITypeDescriptorContext)ctx);
-        }
+        public static Uri GetContextBaseUri(this IServiceProvider ctx) => ctx.GetService<IUriContext>().BaseUri;
 
-        public static T GetFirstParent<T>(this IServiceProvider ctx) where T : class
-        {
-            var parentStack = ctx.GetService<IAvaloniaXamlIlParentStackProvider>();
-            if (parentStack != null)
-                return parentStack.Parents.OfType<T>().FirstOrDefault();
-            return Portable.Xaml.ComponentModel.TypeDescriptorExtensions.GetFirstAmbientValue<T>((ITypeDescriptorContext)ctx);
-        }
-        
-        public static T GetLastParent<T>(this IServiceProvider ctx) where T : class
-        {
-            var parentStack = ctx.GetService<IAvaloniaXamlIlParentStackProvider>();
-            if (parentStack != null)
-                return parentStack.Parents.OfType<T>().LastOrDefault();
-            return Portable.Xaml.ComponentModel.TypeDescriptorExtensions.GetLastOrDefaultAmbientValue<T>(
-                (ITypeDescriptorContext)ctx);
-        }
+        public static T GetFirstParent<T>(this IServiceProvider ctx) where T : class 
+            => ctx.GetService<IAvaloniaXamlIlParentStackProvider>().Parents.OfType<T>().FirstOrDefault();
+
+        public static T GetLastParent<T>(this IServiceProvider ctx) where T : class 
+            => ctx.GetService<IAvaloniaXamlIlParentStackProvider>().Parents.OfType<T>().LastOrDefault();
 
         public static IEnumerable<T> GetParents<T>(this IServiceProvider sp)
         {
-            var stack = sp.GetService<IAvaloniaXamlIlParentStackProvider>();
-            if (stack != null)
-                return stack.Parents.OfType<T>();
+            return sp.GetService<IAvaloniaXamlIlParentStackProvider>().Parents.OfType<T>();
+            
             
-            var context = (ITypeDescriptorContext)sp;
-            var schemaContext = context.GetService<IXamlSchemaContextProvider>().SchemaContext;
-            var ambientProvider = context.GetService<IAmbientProvider>();
-            return ambientProvider.GetAllAmbientValues(schemaContext.GetXamlType(typeof(T))).OfType<T>();
         }
 
         public static Type ResolveType(this IServiceProvider ctx, string namespacePrefix, string type)

+ 9 - 0
src/Markup/Avalonia.Markup.Xaml/MarkupExtension.cs

@@ -0,0 +1,9 @@
+using System;
+
+namespace Avalonia.Markup.Xaml
+{
+    public abstract class MarkupExtension
+    {
+        public abstract object ProvideValue(IServiceProvider serviceProvider);
+    }
+}

+ 2 - 12
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/BindingExtension.cs

@@ -10,14 +10,9 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
     using Avalonia.Data.Converters;
     using Avalonia.Markup.Data;
     using Avalonia.Styling;
-    using Portable.Xaml;
-    using Portable.Xaml.ComponentModel;
-    using Portable.Xaml.Markup;
-    using PortableXaml;
     using System.ComponentModel;
 
-    [MarkupExtensionReturnType(typeof(IBinding))]
-    public class BindingExtension : MarkupExtension
+    public class BindingExtension
     {
         public BindingExtension()
         {
@@ -28,12 +23,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
             Path = path;
         }
 
-        public override object ProvideValue(IServiceProvider serviceProvider)
-        {
-            return ProvideTypedValue(serviceProvider);
-        }
-        
-        public Binding ProvideTypedValue(IServiceProvider serviceProvider)
+        public Binding ProvideValue(IServiceProvider serviceProvider)
         {
             var descriptorContext = (ITypeDescriptorContext)serviceProvider;
 

+ 2 - 7
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/DynamicResourceExtension.cs

@@ -7,13 +7,10 @@ using System.Linq;
 using System.Reactive.Linq;
 using Avalonia.Controls;
 using Avalonia.Data;
-using Portable.Xaml;
-using Portable.Xaml.ComponentModel;
-using Portable.Xaml.Markup;
 
 namespace Avalonia.Markup.Xaml.MarkupExtensions
 {
-    public class DynamicResourceExtension : MarkupExtension, IBinding
+    public class DynamicResourceExtension : IBinding
     {
         private IResourceNode _anchor;
 
@@ -28,9 +25,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
 
         public object ResourceKey { get; set; }
 
-        public override object ProvideValue(IServiceProvider serviceProvider) => ProvideTypedValue(serviceProvider);
-        
-        public IBinding ProvideTypedValue(IServiceProvider serviceProvider)
+        public IBinding ProvideValue(IServiceProvider serviceProvider)
         {
             var provideTarget = serviceProvider.GetService<IProvideValueTarget>();
 

+ 3 - 4
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/RelativeSourceExtension.cs

@@ -3,11 +3,10 @@
 
 using System;
 using Avalonia.Data;
-using Portable.Xaml.Markup;
 
 namespace Avalonia.Markup.Xaml.MarkupExtensions
 {
-    public class RelativeSourceExtension : MarkupExtension
+    public class RelativeSourceExtension
     {
         public RelativeSourceExtension()
         {
@@ -18,7 +17,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
             Mode = mode;
         }
 
-        public override object ProvideValue(IServiceProvider serviceProvider)
+        public RelativeSource ProvideValue(IServiceProvider serviceProvider)
         {
             return new RelativeSource
             {
@@ -38,4 +37,4 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
 
         public int AncestorLevel { get; set; } = 1;
     }
-}
+}

+ 2 - 10
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/ResourceInclude.cs

@@ -1,15 +1,13 @@
 using System;
 using System.ComponentModel;
 using Avalonia.Controls;
-using Portable.Xaml.ComponentModel;
-using Portable.Xaml.Markup;
 
 namespace Avalonia.Markup.Xaml.MarkupExtensions
 {
     /// <summary>
     /// Loads a resource dictionary from a specified URL.
     /// </summary>
-    public class ResourceInclude : MarkupExtension, IResourceProvider
+    public class ResourceInclude :IResourceProvider
     {
         private Uri _baseUri;
         private IResourceDictionary _loaded;
@@ -52,13 +50,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
             return Loaded.TryGetResource(key, out value);
         }
 
-        /// <inhertidoc/>
-        public override object ProvideValue(IServiceProvider serviceProvider)
-        {
-            return ProvideTypedValue(serviceProvider);
-        }
-        
-        public ResourceInclude ProvideTypedValue(IServiceProvider serviceProvider)
+        public ResourceInclude ProvideValue(IServiceProvider serviceProvider)
         {
             var tdc = (ITypeDescriptorContext)serviceProvider;
             _baseUri = tdc?.GetContextBaseUri();

+ 3 - 19
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StaticResourceExtension.cs

@@ -7,13 +7,10 @@ using System.ComponentModel;
 using System.Reflection;
 using Avalonia.Controls;
 using Avalonia.Markup.Data;
-using Portable.Xaml;
-using Portable.Xaml.ComponentModel;
-using Portable.Xaml.Markup;
 
 namespace Avalonia.Markup.Xaml.MarkupExtensions
 {
-    public class StaticResourceExtension : MarkupExtension
+    public class StaticResourceExtension
     {
         public StaticResourceExtension()
         {
@@ -26,26 +23,13 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions
 
         public string ResourceKey { get; set; }
 
-        public override object ProvideValue(IServiceProvider serviceProvider)
+        public object ProvideValue(IServiceProvider serviceProvider)
         {
-
-
             // Look upwards though the ambient context for IResourceProviders which might be able
             // to give us the resource.
             foreach (var resourceProvider in serviceProvider.GetParents<IResourceNode>())
             {
-                // We override XamlType.CanAssignTo in BindingXamlType so the results we get back
-                // from GetAllAmbientValues aren't necessarily of the correct type.
-
-                if (AvaloniaXamlLoader.UseLegacyXamlLoader 
-                    && resourceProvider is IControl control && control.StylingParent != null)
-                {
-                    // If we've got to a control that has a StylingParent then it's probably
-                    // a top level control and its StylingParent is pointing to the global
-                    // styles. If this is case just do a FindResource on it.
-                    return control.FindResource(ResourceKey);
-                }
-                else if (resourceProvider.TryGetResource(ResourceKey, out var value))
+                if (resourceProvider.TryGetResource(ResourceKey, out var value))
                 {
                     return value;
                 }

+ 2 - 7
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/StyleIncludeExtension.cs

@@ -3,23 +3,18 @@
 
 using Avalonia.Markup.Xaml.Styling;
 using Avalonia.Styling;
-using Portable.Xaml;
-using Portable.Xaml.ComponentModel;
 using System.ComponentModel;
-using Portable.Xaml.Markup;
 using System;
 
 namespace Avalonia.Markup.Xaml.MarkupExtensions
 {
-    [MarkupExtensionReturnType(typeof(IStyle))]
-    public class StyleIncludeExtension : MarkupExtension
+    public class StyleIncludeExtension
     {
         public StyleIncludeExtension()
         {
         }
 
-        public override object ProvideValue(IServiceProvider serviceProvider) => ProvideTypedValue(serviceProvider);
-        public IStyle ProvideTypedValue(IServiceProvider serviceProvider)
+        public IStyle ProvideValue(IServiceProvider serviceProvider)
         {
             return new StyleInclude(serviceProvider.GetContextBaseUri()) { Source = Source };
         }

+ 0 - 39
src/Markup/Avalonia.Markup.Xaml/PortableXaml/AttributeExtensions.cs

@@ -1,39 +0,0 @@
-using Avalonia.Markup.Xaml.Templates;
-using avm = Avalonia.Metadata;
-using pm = Portable.Xaml.Markup;
-
-namespace Avalonia.Markup.Xaml.PortableXaml
-{
-    internal static class AttributeExtensions
-    {
-        public static pm.XamlDeferLoadAttribute ToPortableXaml(this avm.TemplateContentAttribute attrib)
-        {
-            if (attrib == null)
-            {
-                return null;
-            }
-
-            return new pm.XamlDeferLoadAttribute(typeof(TemplateLoader), typeof(TemplateContent));
-        }
-
-        public static pm.AmbientAttribute ToPortableXaml(this avm.AmbientAttribute attrib)
-        {
-            if (attrib == null)
-            {
-                return null;
-            }
-
-            return new pm.AmbientAttribute();
-        }
-
-        public static pm.DependsOnAttribute ToPortableXaml(this avm.DependsOnAttribute attrib)
-        {
-            if (attrib == null)
-            {
-                return null;
-            }
-
-            return new pm.DependsOnAttribute(attrib.Name);
-        }
-    }
-}

+ 0 - 83
src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaMemberAttributeProvider.cs

@@ -1,83 +0,0 @@
-using Avalonia.Markup.Xaml.Converters;
-using Avalonia.Styling;
-using Portable.Xaml.ComponentModel;
-using System.ComponentModel;
-using System;
-using System.Linq;
-using System.Reflection;
-using avm = Avalonia.Metadata;
-using pm = Portable.Xaml.Markup;
-
-namespace Avalonia.Markup.Xaml.PortableXaml
-{
-    public class AvaloniaMemberAttributeProvider : ICustomAttributeProvider
-    {
-        public AvaloniaMemberAttributeProvider(MemberInfo info)
-        {
-            _info = info;
-        }
-
-        public object[] GetCustomAttributes(bool inherit)
-        {
-            throw new NotImplementedException();
-        }
-
-        public object[] GetCustomAttributes(Type attributeType, bool inherit)
-        {
-            Attribute result = null;
-
-            if (attributeType == typeof(pm.XamlDeferLoadAttribute))
-            {
-                result = _info.GetCustomAttribute<avm.TemplateContentAttribute>(inherit)
-                                .ToPortableXaml();
-            }
-            else if (attributeType == typeof(pm.AmbientAttribute))
-            {
-                result = _info.GetCustomAttribute<avm.AmbientAttribute>(inherit)
-                                .ToPortableXaml();
-            }
-            else if (attributeType == typeof(pm.DependsOnAttribute))
-            {
-                result = _info.GetCustomAttribute<avm.DependsOnAttribute>(inherit)
-                                .ToPortableXaml();
-            }
-            else if (attributeType == typeof(TypeConverterAttribute) &&
-                        _info.DeclaringType == typeof(Setter) &&
-                        _info.Name == nameof(Setter.Value))
-            {
-                //actually it never comes here looks like if property type is object
-                //Portable.Xaml is not searching for Type Converter
-                result = new TypeConverterAttribute(typeof(SetterValueTypeConverter));
-            }
-            else if (attributeType == typeof(TypeConverterAttribute) && _info is EventInfo)
-            {
-                // If a type converter for `EventInfo` is registered, then use that to convert
-                // event handler values. This is used by the designer to override the lookup
-                // for event handlers with a null handler.
-                var eventConverter = AvaloniaTypeConverters.GetTypeConverter(typeof(EventInfo));
-
-                if (eventConverter != null)
-                {
-                    result = new TypeConverterAttribute(eventConverter);
-                }
-            }
-
-            if (result == null)
-            {
-                var attr = _info.GetCustomAttributes(attributeType, inherit);
-                return (attr as object[]) ?? attr.ToArray();
-            }
-            else
-            {
-                return new object[] { result };
-            }
-        }
-
-        public bool IsDefined(Type attributeType, bool inherit)
-        {
-            throw new NotImplementedException();
-        }
-
-        private readonly MemberInfo _info;
-    }
-}

+ 0 - 56
src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaNameScope.cs

@@ -1,56 +0,0 @@
-using System.Collections.Generic;
-using Avalonia.Controls;
-
-namespace Avalonia.Markup.Xaml.PortableXaml
-{
-    internal class AvaloniaNameScope : Portable.Xaml.Markup.INameScope
-    {
-        public object Instance { get; set; }
-
-        private Dictionary<string, object> _names = new Dictionary<string, object>();
-
-        public object FindName(string name)
-        {
-            object result;
-            if (_names.TryGetValue(name, out result))
-                return result;
-            return null;
-        }
-
-        public void RegisterName(string name, object scopedElement)
-        {
-            if (scopedElement != null)
-                _names.Add(name, scopedElement);
-
-            //TODO: ???
-            //var control = scopedElement as Control;
-
-            //if (control != null)
-            //{
-            //    var nameScope = (Instance as INameScope) ?? control.FindNameScope();
-
-            //    if (nameScope != null)
-            //    {
-            //        nameScope.Register(name, scopedElement);
-            //    }
-            //}
-        }
-
-        public void UnregisterName(string name)
-        {
-        }
-
-        public void RegisterOnNameScope(object target)
-        {
-            var nameScope = target as INameScope;
-
-            if (nameScope != null)
-            {
-                foreach (var v in _names)
-                {
-                    nameScope.Register(v.Key, v.Value);
-                }
-            }
-        }
-    }
-}

+ 0 - 147
src/Markup/Avalonia.Markup.Xaml/PortableXaml/AvaloniaRuntimeTypeProvider.cs

@@ -1,147 +0,0 @@
-// Copyright (c) The Avalonia Project. All rights reserved.
-// Licensed under the MIT license. See licence.md file in the project root for full license information.
-
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Reflection;
-using Avalonia.Controls;
-using Avalonia.Data;
-using Avalonia.Markup.Xaml.Templates;
-using Avalonia.Media;
-using Avalonia.Metadata;
-using Avalonia.Platform;
-using Avalonia.Styling;
-
-namespace Avalonia.Markup.Xaml.Context
-{
-    using ClrNamespaceInfo = Tuple<string, Assembly>;
-
-    public interface IRuntimeTypeProvider
-    {
-        Type FindType(string xamlNamespace, string name, Type[] genArgs);
-
-        IEnumerable<Assembly> ReferencedAssemblies { get; }
-    }
-
-    public class AvaloniaRuntimeTypeProvider : IRuntimeTypeProvider
-    {
-        private const string ClrNamespace = "clr-namespace:";
-        // private const string AvaloniaNs = "https://github.com/avaloniaui";
-
-        private static readonly IEnumerable<Assembly> ForcedAssemblies = new[]
-        {
-            typeof(AvaloniaObject).GetTypeInfo().Assembly,
-            typeof(Animation.Animation).GetTypeInfo().Assembly,
-            typeof(Control).GetTypeInfo().Assembly,
-            typeof(Style).GetTypeInfo().Assembly,
-            typeof(DataTemplate).GetTypeInfo().Assembly,
-            typeof(SolidColorBrush).GetTypeInfo().Assembly,
-            typeof(Binding).GetTypeInfo().Assembly,
-        };
-
-        private Dictionary<string, HashSet<ClrNamespaceInfo>> _namespaces = new Dictionary<string, HashSet<ClrNamespaceInfo>>();
-
-        private List<Assembly> _scanned = new List<Assembly>();
-
-        public IEnumerable<Assembly> ReferencedAssemblies => _scanned;
-
-        public AvaloniaRuntimeTypeProvider()
-        {
-            ScanAssemblies(ForcedAssemblies);
-            ScanNewAssemblies();
-        }
-
-        private static bool IsClrNamespace(string ns)
-        {
-            return ns.StartsWith(ClrNamespace);
-        }
-
-        private static Assembly GetAssembly(string assemblyName)
-        {
-            return Assembly.Load(new AssemblyName(assemblyName));
-        }
-
-        private void ScanAssemblies(IEnumerable<Assembly> assemblies)
-        {
-            foreach (var assembly in assemblies)
-            {
-                var namespaces = assembly.GetCustomAttributes<XmlnsDefinitionAttribute>()
-                    .Select(x => new { x.XmlNamespace, x.ClrNamespace })
-                    .GroupBy(x => x.XmlNamespace);
-
-                foreach (var nsa in namespaces)
-                {
-                    HashSet<ClrNamespaceInfo> reg;
-
-                    if (!_namespaces.TryGetValue(nsa.Key, out reg))
-                    {
-                        _namespaces[nsa.Key] = reg = new HashSet<Tuple<string, Assembly>>();
-                    }
-
-                    foreach (var child in nsa)
-                    {
-                        reg.Add(new ClrNamespaceInfo(child.ClrNamespace, assembly));
-                    }
-                }
-
-                _scanned.Add(assembly);
-            }
-        }
-
-        private void ScanNewAssemblies()
-        {
-            IEnumerable<Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies();
-
-            if (assemblies != null)
-            {
-                assemblies = assemblies.Except(_scanned);
-                ScanAssemblies(assemblies);
-            }
-        }
-
-        private Dictionary<string, Type> _typeCache = new Dictionary<string, Type>();
-
-        public Type FindType(string xamlNamespace, string name, Type[] genArgs)
-        {
-            if (IsClrNamespace(xamlNamespace))
-            {
-                //we need to handle only xaml url namespaces for avalonia,
-                //the other namespaces are handled well in portable.xaml
-                return null;
-            }
-
-            string key = $"{xamlNamespace}:{name}";
-
-            Type type;
-
-            if (_typeCache.TryGetValue(key, out type))
-            {
-                return type;
-            }
-
-            HashSet<ClrNamespaceInfo> reg;
-
-            if (!_namespaces.TryGetValue(xamlNamespace, out reg))
-            {
-                return null;
-            }
-
-            if (genArgs != null)
-                name += "`" + genArgs.Length;
-
-            foreach (var ns in reg)
-            {
-                var n = ns.Item1 + "." + name;
-                var t = ns.Item2.GetType(n);
-                if (t != null)
-                {
-                    _typeCache[key] = t;
-                    return t;
-                }
-            }
-
-            return null;
-        }
-    }
-}

Some files were not shown because too many files changed in this diff