Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obsolete compatibility layout #23710

Merged
merged 9 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Compatibility/Core/src/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.StyleSheets;
#pragma warning disable CS0618 // Type or member is obsolete
using CFlexLayout = Microsoft.Maui.Controls.Compatibility.FlexLayout;
using CGrid = Microsoft.Maui.Controls.Compatibility.Grid;
using CStackLayout = Microsoft.Maui.Controls.Compatibility.StackLayout;
Expand Down Expand Up @@ -29,4 +30,5 @@

//xf specific
[assembly: StyleProperty("-maui-spacing", typeof(CStackLayout), nameof(CStackLayout.SpacingProperty))]
[assembly: StyleProperty("-maui-orientation", typeof(CStackLayout), nameof(CStackLayout.OrientationProperty))]
[assembly: StyleProperty("-maui-orientation", typeof(CStackLayout), nameof(CStackLayout.OrientationProperty))]
#pragma warning restore CS0618 // Type or member is obsolete
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Layouts;

namespace Maui.Controls.Sample.Pages
{
Expand All @@ -22,8 +22,13 @@ public enum Dock
Bottom
}

public class DockLayout : Layout<View>
public class DockLayout : Layout
{
protected override ILayoutManager CreateLayoutManager()
{
return new DockLayoutManager(this);
}

public static readonly BindableProperty DockProperty =
BindableProperty.Create(nameof(Dock), typeof(Dock), typeof(DockLayout), Dock.Left,
BindingMode.TwoWay, null);
Expand Down Expand Up @@ -53,119 +58,132 @@ public bool LastChildFill
set { SetValue(LastChildFillProperty, value); }
}

protected override void LayoutChildren(double x, double y, double width, double height)

class DockLayoutManager : LayoutManager
{
SizeRequest sizeRequest = new SizeRequest();
int i = 0;
readonly DockLayout _layout;

foreach (var child in Children)
public DockLayoutManager(DockLayout layout) : base(layout)
{
if (child.IsVisible)
{
i++;

sizeRequest = child.Measure(width, height, MeasureFlags.IncludeMargins);

double childX = 0;
double childY = 0;
Size request = sizeRequest.Request;
double childWidth = Math.Min(width, request.Width);
double childHeight = Math.Min(height, request.Height);
_layout = layout;
}

bool lastItem = i == Children.Count;
if (lastItem & LastChildFill)
{
LayoutChildIntoBoundingRegion(child, new Rect(x, y, width, height));
return;
}
public override Size ArrangeChildren(Rect bounds)
{
var (x, y, width, height) = bounds;
Size sizeRequest = Size.Zero;
int i = 0;

switch (GetDock(child))
foreach (View child in _layout)
{
if (child.IsVisible)
{
case Dock.Left:
{
childX = x;
childY = y;
childHeight = height;
x += childWidth;
width -= childWidth;
break;
}
case Dock.Top:
{
childX = x;
childY = y;
childWidth = width;
y += childHeight;
height -= childHeight;
break;
}
case Dock.Right:
{
childX = x + width - childWidth;
childY = y;
childHeight = height;
width -= childWidth;
break;
}
case Dock.Bottom:
{
childX = x;
childY = y + height - childHeight;
childWidth = width;
height -= childHeight;
break;
}
default:
{
goto case Dock.Left;
}
i++;

double childX = 0;
double childY = 0;
Size request = sizeRequest;
double childWidth = Math.Min(width, request.Width);
double childHeight = Math.Min(height, request.Height);

bool lastItem = i == _layout.Count;
if (lastItem & _layout.LastChildFill)
{
((IView)child).Arrange(new Rect(x, y, width, height));
return sizeRequest;
}

switch (_layout.GetDock(child))
{
case Dock.Left:
{
childX = x;
childY = y;
childHeight = height;
x += childWidth;
width -= childWidth;
break;
}
case Dock.Top:
{
childX = x;
childY = y;
childWidth = width;
y += childHeight;
height -= childHeight;
break;
}
case Dock.Right:
{
childX = x + width - childWidth;
childY = y;
childHeight = height;
width -= childWidth;
break;
}
case Dock.Bottom:
{
childX = x;
childY = y + height - childHeight;
childWidth = width;
height -= childHeight;
break;
}
default:
{
goto case Dock.Left;
}
}

((IView)child).Arrange(new Rect(childX, childY, childWidth, childHeight));
}

LayoutChildIntoBoundingRegion(child, new Rect(childX, childY, childWidth, childHeight));
}
}
}

protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
{
double height = 0;
double width = 0;
double finalWidth = 0;
double finalHeight = 0;
return sizeRequest;
}

foreach (var child in Children)
public override Size Measure(double widthConstraint, double heightConstraint)
{
if (child.IsVisible)
{
SizeRequest sizeRequest = child.Measure(widthConstraint, heightConstraint, MeasureFlags.IncludeMargins);
Size request = sizeRequest.Request;
double height = 0;
double width = 0;
double finalWidth = 0;
double finalHeight = 0;

switch (GetDock(child))
foreach (View child in _layout)
{
if (child.IsVisible)
{
case Dock.Left:
case Dock.Right:
{
width += request.Width;
finalWidth = Math.Max(finalWidth, width);
finalHeight = Math.Max(finalHeight, height + request.Height);
break;
}
case Dock.Top:
case Dock.Bottom:
{
height += request.Height;
finalWidth = Math.Max(finalWidth, width + request.Width);
finalHeight = Math.Max(finalHeight, height);
break;
}
default:
{
goto case Dock.Right;
}
SizeRequest sizeRequest = ((IView)child).Measure(widthConstraint, heightConstraint);
Size request = sizeRequest.Request;

switch (_layout.GetDock(child))
{
case Dock.Left:
case Dock.Right:
{
width += request.Width;
finalWidth = Math.Max(finalWidth, width);
finalHeight = Math.Max(finalHeight, height + request.Height);
break;
}
case Dock.Top:
case Dock.Bottom:
{
height += request.Height;
finalWidth = Math.Max(finalWidth, width + request.Width);
finalHeight = Math.Max(finalHeight, height);
break;
}
default:
{
goto case Dock.Right;
}
}
}
}

return new Size(finalWidth, finalHeight);
}
return new SizeRequest(new Size(finalWidth, finalHeight));
}
}
}
3 changes: 2 additions & 1 deletion src/Controls/src/Core/Layout/Layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui.Controls.Xaml.Diagnostics;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Layouts;

namespace Microsoft.Maui.Controls
Expand Down Expand Up @@ -32,7 +33,7 @@ static ILayoutManager GetLayoutManagerFromFactory(Layout layout)
}

// The actual backing store for the IViews in the ILayout
readonly List<IView> _children = new();
readonly private protected List<IView> _children = new();

/// <summary>
/// Gets the child objects contained in this layout.
Expand Down
1 change: 1 addition & 0 deletions src/Controls/src/Core/LegacyLayouts/AbsoluteLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Microsoft.Maui.Controls.Compatibility
{
[ContentProperty(nameof(Children))]
[Obsolete("Use Microsoft.Maui.Controls.AbsoluteLayout instead. For more information, see https://learn.microsoft.com/dotnet/maui/migration/layouts")]
public class AbsoluteLayout : Layout<View>, IElementConfiguration<AbsoluteLayout>
{
/// <summary>Bindable property for attached property <c>LayoutFlags</c>.</summary>
Expand Down
1 change: 1 addition & 0 deletions src/Controls/src/Core/LegacyLayouts/FlexLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Microsoft.Maui.Controls.Compatibility
{
[ContentProperty(nameof(Children))]
[Obsolete("Use Microsoft.Maui.Controls.FlexLayout instead. For more information, see https://learn.microsoft.com/dotnet/maui/migration/layouts")]
public class FlexLayout : Layout<View>
{
/// <summary>Bindable property for <see cref="Direction"/>.</summary>
Expand Down
1 change: 1 addition & 0 deletions src/Controls/src/Core/LegacyLayouts/Grid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Microsoft.Maui.Controls.Compatibility
{
[ContentProperty(nameof(Children))]
[Obsolete("Use Microsoft.Maui.Controls.Grid instead. For more information, see https://learn.microsoft.com/dotnet/maui/migration/layouts")]
public partial class Grid : Layout<View>, IGridController, IElementConfiguration<Grid>, IGridLayout
{
/// <summary>Bindable property for attached property <c>Row</c>.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Microsoft.Maui.Controls.Compatibility
/// </summary>
/// <typeparam name="T">The type of <see cref="View"/> that can be added to the layout.</typeparam>
[ContentProperty(nameof(Children))]
[Obsolete("Use Microsoft.Maui.Controls.Layout instead. For more information, see https://learn.microsoft.com/dotnet/maui/user-interface/layouts/custom")]
public abstract partial class Layout<T> : Layout, Microsoft.Maui.ILayout, ILayoutManager, IBindableLayout, IViewContainer<T> where T : View
{
readonly ElementCollection<T> _children;
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/src/Core/LegacyLayouts/RelativeLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
namespace Microsoft.Maui.Controls.Compatibility
{
[ContentProperty(nameof(Children))]
#pragma warning disable CS0618 // Type or member is obsolete
public class RelativeLayout : Layout<View>, IElementConfiguration<RelativeLayout>
#pragma warning restore CS0618 // Type or member is obsolete
{
/// <summary>Bindable property for attached property <c>XConstraint</c>.</summary>
public static readonly BindableProperty XConstraintProperty = BindableProperty.CreateAttached("XConstraint", typeof(Constraint), typeof(RelativeLayout), null, propertyChanged: ConstraintChanged);
Expand Down
1 change: 1 addition & 0 deletions src/Controls/src/Core/LegacyLayouts/StackLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Microsoft.Maui.Controls.Compatibility
{
[ContentProperty(nameof(Children))]
[Obsolete("Use Microsoft.Maui.Controls.StackLayout instead. For more information, see https://learn.microsoft.com/dotnet/maui/migration/layouts")]
public class StackLayout : Layout<View>, IElementConfiguration<StackLayout>, IView
{
/// <summary>Bindable property for <see cref="Orientation"/>.</summary>
Expand Down
6 changes: 5 additions & 1 deletion src/Controls/src/Core/MergedStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ sealed class MergedStyle : IStyle
{
////If the base type is one of these, stop registering dynamic resources further
////The last one (typeof(Element)) is a safety guard as we might be creating VisualElement directly in internal code
static readonly IList<Type> s_stopAtTypes = new List<Type> { typeof(View), typeof(Compatibility.Layout<>), typeof(VisualElement), typeof(NavigableElement), typeof(Element) };
static readonly IList<Type> s_stopAtTypes = new List<Type> { typeof(View),
#pragma warning disable CS0618 // Type or member is obsolete
typeof(Compatibility.Layout<>),
#pragma warning restore CS0618 // Type or member is obsolete
typeof(VisualElement), typeof(NavigableElement), typeof(Element) };

IList<BindableProperty> _classStyleProperties;

Expand Down
Loading