diff --git a/src/Avalonia.Controls/AppBuilderBase.cs b/src/Avalonia.Controls/AppBuilderBase.cs index 97de093a59..1610d33f9b 100644 --- a/src/Avalonia.Controls/AppBuilderBase.cs +++ b/src/Avalonia.Controls/AppBuilderBase.cs @@ -55,8 +55,7 @@ namespace Avalonia.Controls public Action AfterSetupCallback { get; private set; } = builder => { }; /// - /// Gets or sets a method to call before is called on the - /// . + /// Gets or sets a method to call before Startis called on the . /// public Action BeforeStartCallback { get; private set; } = builder => { }; @@ -94,8 +93,7 @@ namespace Avalonia.Controls protected TAppBuilder Self => (TAppBuilder) this; /// - /// Registers a callback to call before is called on the - /// . + /// Registers a callback to call before Start is called on the . /// /// The callback. /// An instance. @@ -129,6 +127,24 @@ namespace Avalonia.Controls Instance.Run(window); } + /// + /// Starts the application with the provided instance of . + /// + /// The window type. + /// Instance of type TMainWindow to use when starting the app + /// A delegate that will be called to create a data context for the window (optional). + public void Start(TMainWindow mainWindow, Func dataContextProvider = null) + where TMainWindow : Window + { + Setup(); + BeforeStartCallback(Self); + + if (dataContextProvider != null) + mainWindow.DataContext = dataContextProvider(); + mainWindow.Show(); + Instance.Run(mainWindow); + } + /// /// Sets up the platform-specific services for the application, but does not run it. /// diff --git a/src/Avalonia.Layout/Layoutable.cs b/src/Avalonia.Layout/Layoutable.cs index 5abd6c52f7..95a0dd625c 100644 --- a/src/Avalonia.Layout/Layoutable.cs +++ b/src/Avalonia.Layout/Layoutable.cs @@ -456,10 +456,9 @@ namespace Avalonia.Layout ApplyTemplate(); - var constrained = LayoutHelper - .ApplyLayoutConstraints(this, availableSize) - .Deflate(margin); - + var constrained = LayoutHelper.ApplyLayoutConstraints( + this, + availableSize.Deflate(margin)); var measured = MeasureOverride(constrained); var width = measured.Width; diff --git a/tests/Avalonia.Layout.UnitTests/MeasureTests.cs b/tests/Avalonia.Layout.UnitTests/MeasureTests.cs index b5a3539da9..570628b5b7 100644 --- a/tests/Avalonia.Layout.UnitTests/MeasureTests.cs +++ b/tests/Avalonia.Layout.UnitTests/MeasureTests.cs @@ -100,5 +100,58 @@ namespace Avalonia.Layout.UnitTests Assert.Equal(0, target.DesiredSize.Height); } + + [Fact] + public void Margin_Should_Affect_AvailableSize() + { + MeasureTest target; + + var outer = new Decorator + { + Width = 100, + Height = 100, + Child = target = new MeasureTest + { + Margin = new Thickness(10), + } + }; + + outer.Measure(Size.Infinity); + + Assert.Equal(new Size(80, 80), target.AvailableSize); + } + + [Fact] + public void Margin_Should_Be_Applied_Before_Width_Height() + { + MeasureTest target; + + var outer = new Decorator + { + Width = 100, + Height = 100, + Child = target = new MeasureTest + { + Width = 80, + Height = 80, + Margin = new Thickness(10), + } + }; + + outer.Measure(Size.Infinity); + + Assert.Equal(new Size(80, 80), target.AvailableSize); + } + + class MeasureTest : Control + { + public Size? AvailableSize { get; private set; } + + protected override Size MeasureOverride(Size availableSize) + { + AvailableSize = availableSize; + return availableSize; + } + } } }