Removed Vertical/Horizontal Alignment. Now uses LayoutAlignment from Maui

This should clean up and fix more bugs with Layout. I still want to change FrameConstraints to set the values for Alignment as well.
This commit is contained in:
James Clancey 2022-01-13 21:22:05 -09:00
Родитель 7a5911aa21
Коммит f4bb8db601
22 изменённых файлов: 55 добавлений и 78 удалений

Просмотреть файл

@ -34,7 +34,7 @@ namespace Comet.Samples.Comparisons
{
[Body]
View body() =>
new VStack(alignment: HorizontalAlignment.Leading){
new VStack(alignment: LayoutAlignment.Start){
new Text("Turtle Rock"),
new HStack {
new Text("Joshua Tree National Park")

Просмотреть файл

@ -1,4 +1,5 @@
global using Microsoft.Maui;
global using Microsoft.Maui.Graphics;
global using Comet;
global using System.Linq;
global using System.Linq;
global using Microsoft.Maui.Primitives;

Просмотреть файл

@ -8,7 +8,7 @@ namespace Comet.Samples.LiveStreamIssues
{
[Body]
View body() =>
new VStack(HorizontalAlignment.Center)
new VStack(LayoutAlignment.Center)
{
new HStack
{

Просмотреть файл

@ -8,7 +8,7 @@ namespace Comet.Samples.LiveStreamIssues
{
[Body]
View body() =>
new VStack(HorizontalAlignment.Center)
new VStack(LayoutAlignment.Center)
{
new ShapeView(new Circle().Stroke(Colors.Black, 2f)).Frame(44,44)
};

Просмотреть файл

@ -8,7 +8,7 @@ namespace Comet.Samples.LiveStreamIssues
{
[Body]
View body() =>
new HStack(VerticalAlignment.Center)
new HStack(LayoutAlignment.Center)
{
new Spacer(),
new ShapeView(new Circle().Stroke(Colors.Black, 2f)).Frame(44,44),

Просмотреть файл

@ -8,7 +8,7 @@ namespace Comet.Samples.LiveStreamIssues
{
[Body]
View body() =>
new VStack(HorizontalAlignment.Center)
new VStack(LayoutAlignment.Center)
{
new HStack
{

Просмотреть файл

@ -34,7 +34,7 @@ namespace Comet.Samples
{
ViewFor = (report) => new HStack()
{
new VStack(HorizontalAlignment.Leading)
new VStack(LayoutAlignment.Start)
{
new Text (report.View).FontSize(20),
new Text ($"Handler: {report.Handler}"),

Просмотреть файл

@ -30,7 +30,7 @@ namespace Comet.Samples
ViewFor = (song) => new HStack
{
new Image (song.ArtworkUrl).Frame(52, 52).Margin(4),
new VStack(HorizontalAlignment.Leading, spacing:2)
new VStack(LayoutAlignment.Start, spacing:2)
{
new Text (song.Title).FontSize(17),
new Text (song.Artist).Color(Colors.Grey),

Просмотреть файл

@ -36,7 +36,7 @@ namespace Comet.Samples
.Frame(44,44, Alignment.Center)
.Margin(left:10f)
.ClipShape(new Circle()),
new VStack(HorizontalAlignment.Leading)
new VStack(LayoutAlignment.Start)
{
new Text(song.Title),
new Text(song.Artist),

Просмотреть файл

@ -29,7 +29,7 @@ namespace Comet.Samples
new Image()
.Background(Colors.SkyBlue).FillHorizontal().FillVertical()
.ClipShape(new RoundedRectangle(36)),
new VStack(Comet.HorizontalAlignment.Leading) {
new VStack(LayoutAlignment.Start) {
new VStack
{
new Text(() => "$100")

Просмотреть файл

@ -1,25 +1,27 @@
using Microsoft.Maui.Primitives;
namespace Comet
{
public class Alignment
{
public static readonly Alignment Bottom = new Alignment(HorizontalAlignment.Center, VerticalAlignment.Bottom);
public static readonly Alignment BottomLeading = new Alignment(HorizontalAlignment.Leading, VerticalAlignment.Bottom);
public static readonly Alignment BottomTrailing = new Alignment(HorizontalAlignment.Trailing, VerticalAlignment.Bottom);
public static readonly Alignment Center = new Alignment(HorizontalAlignment.Center, VerticalAlignment.Center);
public static readonly Alignment Leading = new Alignment(HorizontalAlignment.Leading, VerticalAlignment.Center);
public static readonly Alignment Trailing = new Alignment(HorizontalAlignment.Trailing, VerticalAlignment.Center);
public static readonly Alignment Top = new Alignment(HorizontalAlignment.Center, VerticalAlignment.Top);
public static readonly Alignment TopLeading = new Alignment(HorizontalAlignment.Leading, VerticalAlignment.Top);
public static readonly Alignment TopTrailing = new Alignment(HorizontalAlignment.Trailing, VerticalAlignment.Top);
public static readonly Alignment Bottom = new Alignment(LayoutAlignment.Center, LayoutAlignment.End);
public static readonly Alignment BottomLeading = new Alignment(LayoutAlignment.Start, LayoutAlignment.End);
public static readonly Alignment BottomTrailing = new Alignment(LayoutAlignment.End, LayoutAlignment.End);
public static readonly Alignment Center = new Alignment(LayoutAlignment.Center, LayoutAlignment.Center);
public static readonly Alignment Leading = new Alignment(LayoutAlignment.Start, LayoutAlignment.Center);
public static readonly Alignment Trailing = new Alignment(LayoutAlignment.End, LayoutAlignment.Center);
public static readonly Alignment Top = new Alignment(LayoutAlignment.Center, LayoutAlignment.Start);
public static readonly Alignment TopLeading = new Alignment(LayoutAlignment.Start, LayoutAlignment.Start);
public static readonly Alignment TopTrailing = new Alignment(LayoutAlignment.End, LayoutAlignment.Start);
public Alignment(HorizontalAlignment horizontal, VerticalAlignment vertical)
public Alignment(LayoutAlignment horizontal, LayoutAlignment vertical)
{
Horizontal = horizontal;
Vertical = vertical;
}
public HorizontalAlignment Horizontal { get; }
public VerticalAlignment Vertical { get; }
public LayoutAlignment Horizontal { get; }
public LayoutAlignment Vertical { get; }
protected bool Equals(Alignment other) => Horizontal == other.Horizontal && Vertical == other.Vertical;

Просмотреть файл

@ -40,13 +40,6 @@ namespace Comet
InvalidateMeasurement();
}
public override void LayoutSubviews(Rectangle frame)
{
base.LayoutSubviews(frame);
var padding = this.GetPadding();
var bounds = new Rectangle(Point.Zero, Frame.Size);//.ApplyPadding(padding);
CrossPlatformArrange(bounds);
}
public override Size GetDesiredSize(Size availableSize)
{

Просмотреть файл

@ -6,11 +6,11 @@ namespace Comet
{
public class HStack : AbstractLayout, IStackLayout
{
private readonly VerticalAlignment alignment;
private readonly LayoutAlignment alignment;
private readonly float? spacing;
public HStack(
VerticalAlignment alignment = VerticalAlignment.Center,
LayoutAlignment alignment = LayoutAlignment.Center,
float? spacing = null) : base()
{
this.alignment = alignment;

Просмотреть файл

@ -6,11 +6,11 @@ namespace Comet
{
public class VStack : AbstractLayout, IStackLayout
{
private readonly HorizontalAlignment alignment;
private readonly LayoutAlignment alignment;
private readonly float? spacing;
public VStack(
HorizontalAlignment alignment = HorizontalAlignment.Center,
LayoutAlignment alignment = LayoutAlignment.Center,
float? spacing = null)
{
this.alignment = alignment;

Просмотреть файл

@ -5,4 +5,5 @@ global using Microsoft.Maui.Handlers;
global using Microsoft.Maui.Graphics;
global using System.Collections.Generic;
global using System.Threading.Tasks;
global using System.Linq;
global using System.Linq;
global using Microsoft.Maui.Primitives;

Просмотреть файл

@ -94,7 +94,7 @@ namespace Comet
public static void SetFrameFromNativeView(
this View view,
Rectangle frame)
Rectangle frame, LayoutAlignment defaultHorizontalAlignment = LayoutAlignment.Center, LayoutAlignment defaultVerticalAlignment = LayoutAlignment.Center)
{
if (view == null)
return;
@ -119,13 +119,17 @@ namespace Comet
var frameConstraints = view.GetFrameConstraints();
var horizontalSizing = frameConstraints?.Alignment?.Horizontal ?? view.GetHorizontalLayoutAlignment(view.Parent as ContainerView, defaultHorizontalAlignment);
var verticalSizing = frameConstraints?.Alignment?.Vertical ?? view.GetVerticalLayoutAlignment(view.Parent as ContainerView, defaultVerticalAlignment);
if (frameConstraints?.Width != null)
{
width = (float)frameConstraints.Width;
}
else
{
var horizontalSizing = view.GetHorizontalLayoutAlignment(view.Parent as ContainerView, LayoutAlignment.Start);
if (horizontalSizing == LayoutAlignment.Fill)
width = frame.Width;
}
@ -136,7 +140,6 @@ namespace Comet
}
else
{
var verticalSizing = view.GetVerticalLayoutAlignment(view.Parent as ContainerView, LayoutAlignment.Start);
if (verticalSizing == LayoutAlignment.Fill)
height = frame.Height;
}
@ -144,23 +147,23 @@ namespace Comet
var alignment = frameConstraints?.Alignment ?? Alignment.Center;
var xFactor = .5f;
switch (alignment.Horizontal)
switch (horizontalSizing)
{
case HorizontalAlignment.Leading:
case LayoutAlignment.Start:
xFactor = 0;
break;
case HorizontalAlignment.Trailing:
case LayoutAlignment.End:
xFactor = 1;
break;
}
var yFactor = .5f;
switch (alignment.Vertical)
switch (verticalSizing)
{
case VerticalAlignment.Bottom:
case LayoutAlignment.End:
yFactor = 1;
break;
case VerticalAlignment.Top:
case LayoutAlignment.Start:
yFactor = 0;
break;
}
@ -223,6 +226,9 @@ namespace Comet
public static T FrameConstraints<T>(this T view, FrameConstraints constraints, bool cascades = false) where T : View
{
view.SetEnvironment(EnvironmentKeys.Layout.VerticalLayoutAlignment, constraints?.Alignment?.Vertical, cascades);
view.SetEnvironment(EnvironmentKeys.Layout.HorizontalLayoutAlignment, constraints?.Alignment?.Horizontal, cascades);
view.SetEnvironment(EnvironmentKeys.Layout.FrameConstraints, constraints, cascades);
return view;
}

Просмотреть файл

@ -1,13 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Comet
{
public enum HorizontalAlignment
{
Center,
Leading,
Trailing
}
}

Просмотреть файл

@ -5,11 +5,11 @@ namespace Comet.Layout
{
public class HStackLayoutManager : Microsoft.Maui.Layouts.ILayoutManager
{
private readonly VerticalAlignment _defaultAlignment;
private readonly LayoutAlignment _defaultAlignment;
private readonly float _spacing;
public HStackLayoutManager(ContainerView layout,
VerticalAlignment alignment = VerticalAlignment.Center,
LayoutAlignment alignment = LayoutAlignment.Center,
float? spacing = null)
{
this.layout = layout;
@ -33,7 +33,7 @@ namespace Comet.Layout
var size = view.MeasuredSize;
layoutRect.Width = size.Width;
view.LayoutSubviews(layoutRect);
view.SetFrameFromNativeView(layoutRect,LayoutAlignment.Start, _defaultAlignment);
layoutRect.X = view.Frame.Right + _spacing;
}
return new Size(layoutRect.Left, layoutRect.Bottom);
@ -95,6 +95,7 @@ namespace Comet.Layout
width = widthConstraint;
return new Size(width, height);
}
}

Просмотреть файл

@ -4,10 +4,10 @@ namespace Comet.Layout;
public class VStackLayoutManager : Microsoft.Maui.Layouts.ILayoutManager
{
private readonly HorizontalAlignment _defaultAlignment;
private readonly LayoutAlignment _defaultAlignment;
private readonly double _spacing;
public VStackLayoutManager(ContainerView layout, HorizontalAlignment alignment = HorizontalAlignment.Center,
public VStackLayoutManager(ContainerView layout, LayoutAlignment alignment = LayoutAlignment.Center,
double? spacing = null)
{
_defaultAlignment = alignment;
@ -31,7 +31,7 @@ public class VStackLayoutManager : Microsoft.Maui.Layouts.ILayoutManager
var size = view.MeasuredSize;
layoutRect.Height = size.Height;
view.LayoutSubviews(layoutRect);
view.SetFrameFromNativeView(layoutRect,_defaultAlignment, LayoutAlignment.Start);
layoutRect.Y = view.Frame.Bottom + _spacing;
}

Просмотреть файл

@ -18,7 +18,7 @@ namespace Comet.Styles
public FlowDirection FlowDirection { get; set; } = System.Globalization.CultureInfo.CurrentCulture.TextInfo.IsRightToLeft ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;
public HorizontalAlignment HorizontalAlignment { get; set; } = System.Globalization.CultureInfo.CurrentCulture.TextInfo.IsRightToLeft ? HorizontalAlignment.Trailing : HorizontalAlignment.Leading;
public LayoutAlignment HorizontalAlignment { get; set; } = System.Globalization.CultureInfo.CurrentCulture.TextInfo.IsRightToLeft ? LayoutAlignment.End : LayoutAlignment.Start;
public TextStyle Label { get; set; } = new TextStyle
{

Просмотреть файл

@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Comet
{
public enum VerticalAlignment
{
Center,
Bottom,
Top,
FirstTextBaseline,
LastTextBaseline
}
}

Просмотреть файл

@ -3,3 +3,4 @@ global using Microsoft.Maui.Graphics;
global using Comet;
global using System;
global using System.Linq;
global using Microsoft.Maui.Primitives;