diff --git a/Cirrious.FluentLayout/AdvancedFluentLayoutExtensions.cs b/Cirrious.FluentLayout/AdvancedFluentLayoutExtensions.cs index 5981529..53ef560 100644 --- a/Cirrious.FluentLayout/AdvancedFluentLayoutExtensions.cs +++ b/Cirrious.FluentLayout/AdvancedFluentLayoutExtensions.cs @@ -5,243 +5,232 @@ // // Project Lead - Stuart Lodge, @slodge, me@slodge.com -using System; -using System.Collections.Generic; -using System.Linq; -using UIKit; -using Cirrious.FluentLayouts.Touch.Extensions; +namespace Cirrious.FluentLayouts.Touch; -namespace Cirrious.FluentLayouts.Touch +public static class AdvancedFluentLayoutExtensions { - public static class AdvancedFluentLayoutExtensions - { - const float DefaultMargin = 0; - const float DefaultScale = 1; + const float DefaultMargin = 0; + const float DefaultScale = 1; + + public static FluentLayout AtTopOf(this UIView view, UIView parentView, nfloat? margin = null) => + view.Top().EqualTo().TopOf(parentView).Plus(margin.GetValueOrDefault(DefaultMargin)); + + public static FluentLayout AtTopOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) => + UIDevice.CurrentDevice.CheckSystemVersion(11, 0) + ? view.Top().EqualTo().TopOf(parentView.SafeAreaLayoutGuide).Plus(margin.GetValueOrDefault(DefaultMargin)) + : view.AtTopOf(parentView, margin); - public static FluentLayout AtTopOf(this UIView view, UIView parentView, nfloat? margin = null) => - view.Top().EqualTo().TopOf(parentView).Plus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout AtLeftOf(this UIView view, UIView parentView, nfloat? margin = null) => + view.Left().EqualTo().LeftOf(parentView).Plus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout AtTopOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) => - UIDevice.CurrentDevice.CheckSystemVersion(11, 0) - ? view.Top().EqualTo().TopOf(parentView.SafeAreaLayoutGuide).Plus(margin.GetValueOrDefault(DefaultMargin)) - : view.AtTopOf(parentView, margin); + public static FluentLayout AtLeftOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) => + UIDevice.CurrentDevice.CheckSystemVersion(11, 0) + ? view.Left().EqualTo().LeftOf(parentView.SafeAreaLayoutGuide).Plus(margin.GetValueOrDefault(DefaultMargin)) + : view.AtLeftOf(parentView, margin); - public static FluentLayout AtLeftOf(this UIView view, UIView parentView, nfloat? margin = null) => - view.Left().EqualTo().LeftOf(parentView).Plus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout AtRightOf(this UIView view, UIView parentView, nfloat? margin = null) => + view.Right().EqualTo().RightOf(parentView).Minus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout AtLeftOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) => - UIDevice.CurrentDevice.CheckSystemVersion(11, 0) - ? view.Left().EqualTo().LeftOf(parentView.SafeAreaLayoutGuide).Plus(margin.GetValueOrDefault(DefaultMargin)) - : view.AtLeftOf(parentView, margin); + public static FluentLayout AtRightOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) => + UIDevice.CurrentDevice.CheckSystemVersion(11, 0) + ? view.Right().EqualTo().RightOf(parentView.SafeAreaLayoutGuide).Minus(margin.GetValueOrDefault(DefaultMargin)) + : view.AtRightOf(parentView, margin); - public static FluentLayout AtRightOf(this UIView view, UIView parentView, nfloat? margin = null) => - view.Right().EqualTo().RightOf(parentView).Minus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout AtBottomOf(this UIView view, UIView parentView, nfloat? margin = null) => + view.Bottom().EqualTo().BottomOf(parentView).Minus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout AtRightOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) => - UIDevice.CurrentDevice.CheckSystemVersion(11, 0) - ? view.Right().EqualTo().RightOf(parentView.SafeAreaLayoutGuide).Minus(margin.GetValueOrDefault(DefaultMargin)) - : view.AtRightOf(parentView, margin); + public static FluentLayout AtBottomOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) => + UIDevice.CurrentDevice.CheckSystemVersion(11, 0) + ? view.Bottom().EqualTo().BottomOf(parentView.SafeAreaLayoutGuide).Minus(margin.GetValueOrDefault(DefaultMargin)) + : view.AtBottomOf(parentView, margin); - public static FluentLayout AtBottomOf(this UIView view, UIView parentView, nfloat? margin = null) => - view.Bottom().EqualTo().BottomOf(parentView).Minus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout AtLeadingOf(this UIView view, UIView parentView, nfloat? margin = null) => + view.Leading().EqualTo().LeadingOf(parentView).Plus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout AtBottomOfSafeArea(this UIView view, UIView parentView, nfloat? margin = null) => - UIDevice.CurrentDevice.CheckSystemVersion(11, 0) - ? view.Bottom().EqualTo().BottomOf(parentView.SafeAreaLayoutGuide).Minus(margin.GetValueOrDefault(DefaultMargin)) - : view.AtBottomOf(parentView, margin); + public static FluentLayout AtTrailingOf(this UIView view, UIView parentView, nfloat? margin = null) => + view.Trailing().EqualTo().TrailingOf(parentView).Minus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout AtLeadingOf(this UIView view, UIView parentView, nfloat? margin = null) => - view.Leading().EqualTo().LeadingOf(parentView).Plus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout Below(this UIView view, UIView previous, nfloat? margin = null) => + view.Top().EqualTo().BottomOf(previous).Plus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout AtTrailingOf(this UIView view, UIView parentView, nfloat? margin = null) => - view.Trailing().EqualTo().TrailingOf(parentView).Minus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout Above(this UIView view, UIView previous, nfloat? margin = null) => + view.Bottom().EqualTo().TopOf(previous).Minus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout Below(this UIView view, UIView previous, nfloat? margin = null) => - view.Top().EqualTo().BottomOf(previous).Plus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout WithSameLeft(this UIView view, UIView previous) => view.Left().EqualTo().LeftOf(previous); - public static FluentLayout Above(this UIView view, UIView previous, nfloat? margin = null) => - view.Bottom().EqualTo().TopOf(previous).Minus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout WithSameTop(this UIView view, UIView previous) => view.Top().EqualTo().TopOf(previous); - public static FluentLayout WithSameLeft(this UIView view, UIView previous) => view.Left().EqualTo().LeftOf(previous); + public static FluentLayout WithSameCenterX(this UIView view, UIView previous) => view.CenterX().EqualTo().CenterXOf(previous); - public static FluentLayout WithSameTop(this UIView view, UIView previous) => view.Top().EqualTo().TopOf(previous); + public static FluentLayout WithSameCenterY(this UIView view, UIView previous) => view.CenterY().EqualTo().CenterYOf(previous); - public static FluentLayout WithSameCenterX(this UIView view, UIView previous) => view.CenterX().EqualTo().CenterXOf(previous); + public static FluentLayout WithSameRight(this UIView view, UIView previous) => view.Right().EqualTo().RightOf(previous); - public static FluentLayout WithSameCenterY(this UIView view, UIView previous) => view.CenterY().EqualTo().CenterYOf(previous); + public static FluentLayout WithSameWidth(this UIView view, UIView previous) => view.Width().EqualTo().WidthOf(previous); - public static FluentLayout WithSameRight(this UIView view, UIView previous) => view.Right().EqualTo().RightOf(previous); + public static FluentLayout WithSameBottom(this UIView view, UIView previous) => view.Bottom().EqualTo().BottomOf(previous); - public static FluentLayout WithSameWidth(this UIView view, UIView previous) => view.Width().EqualTo().WidthOf(previous); + public static FluentLayout WithSameLeading(this UIView view, UIView previous) => view.Leading().EqualTo().LeadingOf(previous); - public static FluentLayout WithSameBottom(this UIView view, UIView previous) => view.Bottom().EqualTo().BottomOf(previous); + public static FluentLayout WithSameTrailing(this UIView view, UIView previous) => view.Trailing().EqualTo().TrailingOf(previous); - public static FluentLayout WithSameLeading(this UIView view, UIView previous) => view.Leading().EqualTo().LeadingOf(previous); + public static FluentLayout WithRelativeWidth(this UIView view, UIView previous, nfloat? scale = null) => + view.Width().EqualTo().WidthOf(previous).WithMultiplier(scale.GetValueOrDefault(DefaultScale)); - public static FluentLayout WithSameTrailing(this UIView view, UIView previous) => view.Trailing().EqualTo().TrailingOf(previous); + public static FluentLayout WithSameHeight(this UIView view, UIView previous) => view.Height().EqualTo().HeightOf(previous); - public static FluentLayout WithRelativeWidth(this UIView view, UIView previous, nfloat? scale = null) => - view.Width().EqualTo().WidthOf(previous).WithMultiplier(scale.GetValueOrDefault(DefaultScale)); + public static FluentLayout WithRelativeHeight(this UIView view, UIView previous, nfloat? scale = null) => + view.Height().EqualTo().HeightOf(previous).WithMultiplier(scale.GetValueOrDefault(DefaultScale)); - public static FluentLayout WithSameHeight(this UIView view, UIView previous) => view.Height().EqualTo().HeightOf(previous); + public static FluentLayout ToRightOf(this UIView view, UIView previous, nfloat? margin = null) => + view.Left().EqualTo().RightOf(previous).Plus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout WithRelativeHeight(this UIView view, UIView previous, nfloat? scale = null) => - view.Height().EqualTo().HeightOf(previous).WithMultiplier(scale.GetValueOrDefault(DefaultScale)); + public static FluentLayout ToLeftOf(this UIView view, UIView previous, nfloat? margin = null) => + view.Right().EqualTo().LeftOf(previous).Minus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout ToRightOf(this UIView view, UIView previous, nfloat? margin = null) => - view.Left().EqualTo().RightOf(previous).Plus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout ToTrailingOf(this UIView view, UIView previous, nfloat? margin = null) => + view.Leading().EqualTo().TrailingOf(previous).Plus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout ToLeftOf(this UIView view, UIView previous, nfloat? margin = null) => - view.Right().EqualTo().LeftOf(previous).Minus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout ToLeadingOf(this UIView view, UIView previous, nfloat? margin = null) => + view.Trailing().EqualTo().LeadingOf(previous).Minus(margin.GetValueOrDefault(DefaultMargin)); - public static FluentLayout ToTrailingOf(this UIView view, UIView previous, nfloat? margin = null) => - view.Leading().EqualTo().TrailingOf(previous).Plus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout ToLeftMargin(this UIView view, UIView previous) => + view.Leading().EqualTo().LeadingMarginOf(previous); - public static FluentLayout ToLeadingOf(this UIView view, UIView previous, nfloat? margin = null) => - view.Trailing().EqualTo().LeadingOf(previous).Minus(margin.GetValueOrDefault(DefaultMargin)); + public static FluentLayout ToRightMargin(this UIView view, UIView previous) => + view.Trailing().EqualTo().TrailingMarginOf(previous); - public static FluentLayout ToLeftMargin(this UIView view, UIView previous) => - view.Leading().EqualTo().LeadingMarginOf(previous); + public static FluentLayout ToTopMargin(this UIView view, UIView previous) => + view.Top().EqualTo().TopMarginOf(previous); - public static FluentLayout ToRightMargin(this UIView view, UIView previous) => - view.Trailing().EqualTo().TrailingMarginOf(previous); + public static FluentLayout ToBottomMargin(this UIView view, UIView previous) => + view.Bottom().EqualTo().BottomMarginOf(previous); - public static FluentLayout ToTopMargin(this UIView view, UIView previous) => - view.Top().EqualTo().TopMarginOf(previous); + public static FluentLayout ToLeftOfCenterOf(this UIView view, UIView previous, nfloat? margin = null) => + view.Right().EqualTo().CenterXOf(previous).Minus(margin.GetValueOrDefault(0)); - public static FluentLayout ToBottomMargin(this UIView view, UIView previous) => - view.Bottom().EqualTo().BottomMarginOf(previous); - - public static FluentLayout ToLeftOfCenterOf(this UIView view, UIView previous, nfloat? margin = null) => - view.Right().EqualTo().CenterXOf(previous).Minus(margin.GetValueOrDefault(0)); - - public static FluentLayout ToRightOfCenterOf(this UIView view, UIView previous, nfloat? margin = null) => - view.Left().EqualTo().CenterXOf(previous).Plus(margin.GetValueOrDefault(0)); - - public static FluentLayout AboveCenterOf(this UIView view, UIView previous, nfloat? margin = null) => - view.Bottom().EqualTo().CenterYOf(previous).Minus(margin.GetValueOrDefault(0)); - - public static FluentLayout BelowCenterOf(this UIView view, UIView previous, nfloat? margin = null) => - view.Top().EqualTo().CenterYOf(previous).Plus(margin.GetValueOrDefault(0)); + public static FluentLayout ToRightOfCenterOf(this UIView view, UIView previous, nfloat? margin = null) => + view.Left().EqualTo().CenterXOf(previous).Plus(margin.GetValueOrDefault(0)); - public static IEnumerable FullWidthOf(this UIView view, UIView parent, nfloat? margin = null) + public static FluentLayout AboveCenterOf(this UIView view, UIView previous, nfloat? margin = null) => + view.Bottom().EqualTo().CenterYOf(previous).Minus(margin.GetValueOrDefault(0)); + + public static FluentLayout BelowCenterOf(this UIView view, UIView previous, nfloat? margin = null) => + view.Top().EqualTo().CenterYOf(previous).Plus(margin.GetValueOrDefault(0)); + + public static IEnumerable FullWidthOf(this UIView view, UIView parent, nfloat? margin = null) + { + var marginValue = margin.GetValueOrDefault(DefaultMargin); + + return new List { - var marginValue = margin.GetValueOrDefault(DefaultMargin); + view.AtLeftOf(parent, marginValue).WithIdentifier("Left"), + view.AtRightOf(parent, marginValue).WithIdentifier("Right") + }; + } - return new List - { - view.AtLeftOf(parent, marginValue).WithIdentifier("Left"), - view.AtRightOf(parent, marginValue).WithIdentifier("Right") - }; - } + public static IEnumerable FullHeightOf(this UIView view, UIView parent, nfloat? margin = null) + { + var marginValue = margin.GetValueOrDefault(DefaultMargin); - public static IEnumerable FullHeightOf(this UIView view, UIView parent, nfloat? margin = null) + return new List { - var marginValue = margin.GetValueOrDefault(DefaultMargin); + view.AtTopOf(parent, marginValue).WithIdentifier("Top"), + view.AtBottomOf(parent, marginValue).WithIdentifier("Bottom") + }; + } - return new List - { - view.AtTopOf(parent, marginValue).WithIdentifier("Top"), - view.AtBottomOf(parent, marginValue).WithIdentifier("Bottom") - }; + public static IEnumerable FullSizeOf(this UIView view, UIView parent, nfloat? margin = null) => + FullSizeOf(view, parent, new Margins((float)margin.GetValueOrDefault(DefaultMargin))); + + public static IEnumerable FullSizeOf(this UIView view, UIView parent, Margins margins) + { + margins = margins ?? new Margins(); + + return new List + { + view.AtTopOf(parent, margins.Top).WithIdentifier("Top"), + view.AtBottomOf(parent, margins.Bottom).WithIdentifier("Bottom"), + view.AtLeftOf(parent, margins.Left).WithIdentifier("Left"), + view.AtRightOf(parent, margins.Right).WithIdentifier("Right") + }; + } + + public static FluentLayout? GetLayoutById(this IEnumerable layouts, string identifier) => + layouts.FirstOrDefault(x => x.Identifier?.Equals(identifier) is true); + + public static IEnumerable VerticalStackPanelConstraints(this UIView parentView, Margins margins, params UIView[] views) => + AdvancedVerticalStackPanelConstraints(parentView, margins, views: views); + + /// + /// Vertical stack panel constraints with support for children independent left, right and top margins + /// and a multiplier factor for all margins applied. The multiplier can be useful when dealing with iPad screens. + /// Example: + /// + /// scrollView.AddConstraints(scrollView.AdvancedVerticalStackPanelConstraints(null, + /// childrenLeftMargins: new float[] { 15, 0, 15, 0, 0, 15 }, + /// childrenTopMargins: new float[] { 15, 5, 15, 5, 8, 15, 22, 8, 8, 28, 28 }, + /// marginMultiplier: 2f, + /// views: scrollView.Subviews) + /// ); + /// + public static IEnumerable AdvancedVerticalStackPanelConstraints(this UIView parentView, + Margins margins, + float[]? childrenLeftMargins = null, + float[]? childrenTopMargins = null, + float[]? childrenRightMargins = null, + float marginMultiplier = 1, + params UIView[] views) + { + string? previousIdentifierPrefix = default; + margins ??= new Margins(); + var layouts = new List(); + + var count = views.Length; + for (var i = 0; i < count; i++) + { + var view = views[i]; + var viewIdentifierPrefix = $"{parentView.AccessibilityIdentifier ?? "VerticalStackPanel"}-{view.AccessibilityIdentifier ?? i.ToString()}-"; + + var childLeftMargin = childrenLeftMargins?[i] ?? default; + var marginLeft = Math.Max(margins.Left, childLeftMargin) * marginMultiplier; + layouts.Add(view.Left() + .EqualTo() + .LeftOf(parentView) + .Plus(marginLeft) + .WithIdentifier(viewIdentifierPrefix + "Left")); + + var childRightMargin = childrenRightMargins?[i] ?? default; + var marginRight = Math.Max(margins.Right, childRightMargin) * marginMultiplier; + layouts.Add(view.Width() + .EqualTo() + .WidthOf(parentView) + .Minus(marginRight + marginLeft) + .WithIdentifier(viewIdentifierPrefix + "Width")); + + var childTopMargin = childrenTopMargins?[i] ?? default; + layouts.Add(i == 0 + ? view.Top() + .EqualTo() + .TopOf(parentView) + .Plus(Math.Max(margins.Top, childTopMargin) * marginMultiplier) + .WithIdentifier(viewIdentifierPrefix + "Top") + : view.Top() + .EqualTo() + .BottomOf(views[i - 1]) + .Plus(Math.Max(margins.VSpacing, childTopMargin) * marginMultiplier) + .WithIdentifier(viewIdentifierPrefix + "Top")); + + previousIdentifierPrefix = viewIdentifierPrefix; } - public static IEnumerable FullSizeOf(this UIView view, UIView parent, nfloat? margin = null) => - FullSizeOf(view, parent, new Margins((float)margin.GetValueOrDefault(DefaultMargin))); - - public static IEnumerable FullSizeOf(this UIView view, UIView parent, Margins margins) - { - margins = margins ?? new Margins(); - - return new List - { - view.AtTopOf(parent, margins.Top).WithIdentifier("Top"), - view.AtBottomOf(parent, margins.Bottom).WithIdentifier("Bottom"), - view.AtLeftOf(parent, margins.Left).WithIdentifier("Left"), - view.AtRightOf(parent, margins.Right).WithIdentifier("Right") - }; - } - - public static FluentLayout GetLayoutById(this IEnumerable layouts, string identifier) => - layouts.FirstOrDefault(x => x.Identifier.Equals(identifier)); - - public static IEnumerable VerticalStackPanelConstraints(this UIView parentView, Margins margins, params UIView[] views) => - AdvancedVerticalStackPanelConstraints(parentView, margins, views: views); - - /// - /// Vertical stack panel constraints with support for children independent left, right and top margins - /// and a multiplier factor for all margins applied. The multiplier can be useful when dealing with iPad screens. - /// Example: - /// - /// scrollView.AddConstraints(scrollView.AdvancedVerticalStackPanelConstraints(null, - /// childrenLeftMargins: new float[] { 15, 0, 15, 0, 0, 15 }, - /// childrenTopMargins: new float[] { 15, 5, 15, 5, 8, 15, 22, 8, 8, 28, 28 }, - /// marginMultiplier: 2f, - /// views: scrollView.Subviews) - /// ); - /// - public static IEnumerable AdvancedVerticalStackPanelConstraints(this UIView parentView, - Margins margins, - float[] childrenLeftMargins = null, - float[] childrenTopMargins = null, - float[] childrenRightMargins = null, - float marginMultiplier = 1, - params UIView[] views) - { - string previousIdentifierPrefix = null; - margins = margins ?? new Margins(); - var layouts = new List(); - - var count = views.Length; - for (var i = 0; i < count; i++) - { - var view = views[i]; - var viewIdentifierPrefix = $"{parentView.AccessibilityIdentifier ?? "VerticalStackPanel"}-{view.AccessibilityIdentifier ?? i.ToString()}-"; - - float childLeftMargin; - childrenLeftMargins.TryGetElement(i, out childLeftMargin); - var marginLeft = Math.Max(margins.Left, childLeftMargin) * marginMultiplier; - layouts.Add(view.Left() - .EqualTo() - .LeftOf(parentView) - .Plus(marginLeft) - .WithIdentifier(viewIdentifierPrefix + "Left")); - - float childRightMargin; - childrenRightMargins.TryGetElement(i, out childRightMargin); - var marginRight = Math.Max(margins.Right, childRightMargin) * marginMultiplier; - layouts.Add(view.Width() - .EqualTo() - .WidthOf(parentView) - .Minus(marginRight + marginLeft) - .WithIdentifier(viewIdentifierPrefix + "Width")); - - float childTopMargin; - childrenTopMargins.TryGetElement(i, out childTopMargin); - - layouts.Add(i == 0 - ? view.Top() - .EqualTo() - .TopOf(parentView) - .Plus(Math.Max(margins.Top, childTopMargin)*marginMultiplier) - .WithIdentifier(viewIdentifierPrefix + "Top") - : view.Top() - .EqualTo() - .BottomOf(views[i - 1]) - .Plus(Math.Max(margins.VSpacing, childTopMargin)*marginMultiplier) - .WithIdentifier(viewIdentifierPrefix + "Top")); - - previousIdentifierPrefix = viewIdentifierPrefix; - } - - if (parentView is UIScrollView) - layouts.Add(views[views.Length - 1].Bottom() - .EqualTo() - .BottomOf(parentView) - .Minus(margins.Bottom * marginMultiplier) - .WithIdentifier(previousIdentifierPrefix + "Bottom")); - - return layouts; - } + if (parentView is UIScrollView) + layouts.Add(views[^1].Bottom() + .EqualTo() + .BottomOf(parentView) + .Minus(margins.Bottom * marginMultiplier) + .WithIdentifier(previousIdentifierPrefix + "Bottom")); + + return layouts; } } diff --git a/Cirrious.FluentLayout/Cirrious.FluentLayouts.Touch.csproj b/Cirrious.FluentLayout/Cirrious.FluentLayouts.Touch.csproj index 3dba747..339242d 100644 --- a/Cirrious.FluentLayout/Cirrious.FluentLayouts.Touch.csproj +++ b/Cirrious.FluentLayout/Cirrious.FluentLayouts.Touch.csproj @@ -1,5 +1,11 @@ - - + + + net6.0-ios + Exe + enable + true + 10.0 + Debug AnyCPU @@ -9,8 +15,6 @@ Cirrious.FluentLayouts.Touch Resources Cirrious.FluentLayouts.Touch - Xamarin.iOS - v1.0 true @@ -33,27 +37,4 @@ false None - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Cirrious.FluentLayout/Extensions/ArrayExtensions.cs b/Cirrious.FluentLayout/Extensions/ArrayExtensions.cs deleted file mode 100644 index 8183932..0000000 --- a/Cirrious.FluentLayout/Extensions/ArrayExtensions.cs +++ /dev/null @@ -1,26 +0,0 @@ -namespace Cirrious.FluentLayouts.Touch.Extensions -{ - internal static class ArrayExtensions - { - public static bool TryGetElement(this T[] array, int index, out T element) - { - if (array == null) - { - element = default(T); - - return false; - } - - if (index < array.Length) - { - element = array[index]; - - return true; - } - - element = default(T); - - return false; - } - } -} \ No newline at end of file diff --git a/Cirrious.FluentLayout/FluentLayout.cs b/Cirrious.FluentLayout/FluentLayout.cs index 4b3d858..057ae12 100644 --- a/Cirrious.FluentLayout/FluentLayout.cs +++ b/Cirrious.FluentLayout/FluentLayout.cs @@ -5,215 +5,210 @@ // // Project Lead - Stuart Lodge, @slodge, me@slodge.com -using System; -using System.Collections.Generic; -using UIKit; -using Foundation; +namespace Cirrious.FluentLayouts.Touch; -namespace Cirrious.FluentLayouts.Touch +public class FluentLayout { - public class FluentLayout - { - public FluentLayout( - UIView view, - NSLayoutAttribute attribute, - NSLayoutRelation relation, - NSObject secondItem, - NSLayoutAttribute secondAttribute) - { - Constraint = new Lazy(CreateConstraint); - View = view; - Attribute = attribute; - Relation = relation; - SecondItem = secondItem; - SecondAttribute = secondAttribute; - Priority = (float) UILayoutPriority.Required; - } + public FluentLayout( + UIView view, + NSLayoutAttribute attribute, + NSLayoutRelation relation, + NSObject secondItem, + NSLayoutAttribute secondAttribute) + { + Constraint = new Lazy(CreateConstraint); + View = view; + Attribute = attribute; + Relation = relation; + SecondItem = secondItem; + SecondAttribute = secondAttribute; + Priority = (float)UILayoutPriority.Required; + } - public FluentLayout(UIView view, - NSLayoutAttribute attribute, - NSLayoutRelation relation, - nfloat constant = default(nfloat)) - { - Constraint = new Lazy(CreateConstraint); - View = view; - Attribute = attribute; - Relation = relation; - Constant = constant; - Priority = (float) UILayoutPriority.Required; - } + public FluentLayout(UIView view, + NSLayoutAttribute attribute, + NSLayoutRelation relation, + nfloat constant = default(nfloat)) + { + Constraint = new Lazy(CreateConstraint); + View = view; + Attribute = attribute; + Relation = relation; + Constant = constant; + Priority = (float)UILayoutPriority.Required; + } - public UIView View { get; } - - public nfloat Multiplier { get; private set; } = 1f; - - private nfloat _constant; - public nfloat Constant - { - get { return _constant; } - set - { - _constant = value; - - if (Constraint.IsValueCreated) - Constraint.Value.Constant = _constant; - } - } - - private float _priority; - public float Priority - { - get { return _priority; } - set - { - _priority = value; - - if (Constraint.IsValueCreated) - Constraint.Value.Priority = _priority; - } - } - - private bool _active = true; - public bool Active - { - get { return _active; } - set - { - _active = value; - - if (Constraint.IsValueCreated) - Constraint.Value.Active = _active; - } - } - - private string _identifier; - public string Identifier - { - get { return _identifier; } - set - { - _identifier = value; - - if (Constraint.IsValueCreated) - Constraint.Value.SetIdentifier(_identifier); - } - } - - public NSLayoutAttribute Attribute { get; private set; } - public NSLayoutRelation Relation { get; private set; } - public NSObject SecondItem { get; private set; } - public NSLayoutAttribute SecondAttribute { get; private set; } - - internal Lazy Constraint { get; } - - private NSLayoutConstraint CreateConstraint() - { - var constraint = NSLayoutConstraint.Create( - View, - Attribute, - Relation, - SecondItem, - SecondAttribute, - Multiplier, - Constant); - constraint.Priority = Priority; - - if (!string.IsNullOrWhiteSpace(Identifier)) - constraint.SetIdentifier(Identifier); - - return constraint; - } - - public FluentLayout Plus(nfloat constant) - { - Constant += constant; - return this; - } + public UIView View { get; } - public FluentLayout Minus(nfloat constant) + public nfloat Multiplier { get; private set; } = 1f; + + private nfloat _constant; + public nfloat Constant + { + get { return _constant; } + set { - Constant -= constant; - return this; + _constant = value; + + if (Constraint.IsValueCreated) + Constraint.Value.Constant = _constant; } + } - public FluentLayout WithMultiplier(nfloat multiplier) + private float _priority; + public float Priority + { + get { return _priority; } + set { - Multiplier = multiplier; - return this; + _priority = value; + + if (Constraint.IsValueCreated) + Constraint.Value.Priority = _priority; } + } - public FluentLayout SetPriority(float priority) + private bool _active = true; + public bool Active + { + get { return _active; } + set { - Priority = priority; - return this; + _active = value; + + if (Constraint.IsValueCreated) + Constraint.Value.Active = _active; } + } + + private string? _identifier; - public FluentLayout SetPriority(UILayoutPriority priority) + public string? Identifier + { + get { return _identifier; } + set { - Priority = (float) priority; - return this; + _identifier = value; + + if (Constraint.IsValueCreated) + Constraint.Value.SetIdentifier(_identifier); } + } - public FluentLayout SetActive(bool active) - { - Active = active; - return this; - } + public NSLayoutAttribute Attribute { get; private set; } + public NSLayoutRelation Relation { get; private set; } + public NSObject? SecondItem { get; private set; } + public NSLayoutAttribute SecondAttribute { get; private set; } - public FluentLayout WithIdentifier(string identifier) - { - Identifier = identifier; - return this; - } + internal Lazy Constraint { get; } - public FluentLayout LeftOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Left); + private NSLayoutConstraint CreateConstraint() + { + var constraint = NSLayoutConstraint.Create( + View, + Attribute, + Relation, + SecondItem, + SecondAttribute, + Multiplier, + Constant); + constraint.Priority = Priority; + + if (!string.IsNullOrWhiteSpace(Identifier)) + constraint.SetIdentifier(Identifier); + + return constraint; + } - public FluentLayout RightOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Right); + public FluentLayout Plus(nfloat constant) + { + Constant += constant; + return this; + } - public FluentLayout TopOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Top); + public FluentLayout Minus(nfloat constant) + { + Constant -= constant; + return this; + } - public FluentLayout BottomOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Bottom); + public FluentLayout WithMultiplier(nfloat multiplier) + { + Multiplier = multiplier; + return this; + } - public FluentLayout BaselineOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Baseline); + public FluentLayout SetPriority(float priority) + { + Priority = priority; + return this; + } - public FluentLayout TrailingOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Trailing); + public FluentLayout SetPriority(UILayoutPriority priority) + { + Priority = (float)priority; + return this; + } - public FluentLayout LeadingOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Leading); + public FluentLayout SetActive(bool active) + { + Active = active; + return this; + } - public FluentLayout CenterXOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.CenterX); + public FluentLayout WithIdentifier(string identifier) + { + Identifier = identifier; + return this; + } - public FluentLayout CenterYOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.CenterY); + public FluentLayout LeftOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Left); - public FluentLayout HeightOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Height); + public FluentLayout RightOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Right); - public FluentLayout WidthOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Width); + public FluentLayout TopOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Top); - public FluentLayout TrailingMarginOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.TrailingMargin); + public FluentLayout BottomOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Bottom); - public FluentLayout LeadingMarginOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.LeadingMargin); + public FluentLayout BaselineOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Baseline); - public FluentLayout TopMarginOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.TopMargin); + public FluentLayout TrailingOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Trailing); - public FluentLayout BottomMarginOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.BottomMargin); + public FluentLayout LeadingOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Leading); - private FluentLayout SetSecondItem(NSObject view2, NSLayoutAttribute attribute2) - { - ThrowIfSecondItemAlreadySet(); - SecondAttribute = attribute2; - SecondItem = view2; - return this; - } + public FluentLayout CenterXOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.CenterX); - private void ThrowIfSecondItemAlreadySet() - { - if (SecondItem != null) - throw new Exception("You cannot set the second item in a layout relation more than once"); - } + public FluentLayout CenterYOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.CenterY); + + public FluentLayout HeightOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Height); + + public FluentLayout WidthOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.Width); + + public FluentLayout TrailingMarginOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.TrailingMargin); + + public FluentLayout LeadingMarginOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.LeadingMargin); + + public FluentLayout TopMarginOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.TopMargin); + + public FluentLayout BottomMarginOf(NSObject view2) => SetSecondItem(view2, NSLayoutAttribute.BottomMargin); - [Obsolete("This method will be removed in future versions, please let us know if you still need it!")] - public IEnumerable ToLayoutConstraints() - { - yield return Constraint.Value; - } + private FluentLayout SetSecondItem(NSObject view2, NSLayoutAttribute attribute2) + { + ThrowIfSecondItemAlreadySet(); + SecondAttribute = attribute2; + SecondItem = view2; + return this; + } + + private void ThrowIfSecondItemAlreadySet() + { + if (SecondItem != null) + throw new Exception("You cannot set the second item in a layout relation more than once"); + } + + [Obsolete("This method will be removed in future versions, please let us know if you still need it!")] + public IEnumerable ToLayoutConstraints() + { + yield return Constraint.Value; } } \ No newline at end of file diff --git a/Cirrious.FluentLayout/FluentLayoutExtensions.cs b/Cirrious.FluentLayout/FluentLayoutExtensions.cs index 6c7f3b4..46f8817 100644 --- a/Cirrious.FluentLayout/FluentLayoutExtensions.cs +++ b/Cirrious.FluentLayout/FluentLayoutExtensions.cs @@ -5,10 +5,6 @@ // // Project Lead - Stuart Lodge, @slodge, me@slodge.com -using System.Collections.Generic; -using System.Linq; -using UIKit; - namespace Cirrious.FluentLayouts.Touch { public static class FluentLayoutExtensions @@ -25,7 +21,7 @@ public static void SubviewsDoNotTranslateAutoresizingMaskIntoConstraints(this UI public static UIViewAndLayoutAttribute Right(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.Right); - public static UIViewAndLayoutAttribute Top(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.Top); + public static UIViewAndLayoutAttribute Top(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.Top); public static UIViewAndLayoutAttribute Bottom(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.Bottom); @@ -45,26 +41,26 @@ public static void SubviewsDoNotTranslateAutoresizingMaskIntoConstraints(this UI public static UIViewAndLayoutAttribute WithLayoutAttribute(this UIView view, NSLayoutAttribute attribute) => new UIViewAndLayoutAttribute(view, attribute); - public static UIViewAndLayoutAttribute LeadingMargin(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.LeadingMargin); + public static UIViewAndLayoutAttribute LeadingMargin(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.LeadingMargin); - public static UIViewAndLayoutAttribute TrailingMargin(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.TrailingMargin); + public static UIViewAndLayoutAttribute TrailingMargin(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.TrailingMargin); - public static UIViewAndLayoutAttribute TopMargin(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.TopMargin); + public static UIViewAndLayoutAttribute TopMargin(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.TopMargin); - public static UIViewAndLayoutAttribute BottomMargin(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.BottomMargin); + public static UIViewAndLayoutAttribute BottomMargin(this UIView view) => view.WithLayoutAttribute(NSLayoutAttribute.BottomMargin); - public static void AddConstraints(this UIView view, params FluentLayout[] fluentLayouts) => view.AddConstraints(fluentLayouts.AsEnumerable()); + public static void AddConstraints(this UIView view, params FluentLayout[] fluentLayouts) => view.AddConstraints(fluentLayouts.AsEnumerable()); public static void AddConstraints(this UIView view, IEnumerable fluentLayouts) => - view.AddConstraints(fluentLayouts + view.AddConstraints(fluentLayouts .Where(fluent => fluent != null) - .Select(fluent => fluent.Constraint.Value) + .Select(fluent => fluent.Constraint.Value) .ToArray()); - public static void RemoveConstraints(this UIView view, params FluentLayout[] fluentLayouts) => view.RemoveConstraints(fluentLayouts.AsEnumerable()); + public static void RemoveConstraints(this UIView view, params FluentLayout[] fluentLayouts) => view.RemoveConstraints(fluentLayouts.AsEnumerable()); - public static void RemoveConstraints(this UIView view, IEnumerable fluentLayouts) => - view.RemoveConstraints(fluentLayouts + public static void RemoveConstraints(this UIView view, IEnumerable fluentLayouts) => + view.RemoveConstraints(fluentLayouts .Where(fluent => fluent != null) .Select(fluent => fluent.Constraint.Value) .ToArray()); diff --git a/Cirrious.FluentLayout/Margins.cs b/Cirrious.FluentLayout/Margins.cs index ce2d60e..5779831 100644 --- a/Cirrious.FluentLayout/Margins.cs +++ b/Cirrious.FluentLayout/Margins.cs @@ -40,7 +40,7 @@ public Margins(float allHorizontal, float allVertical) HSpacing = allHorizontal; } - public Margins(float left, float top, float right, float bottom, float hspacing = 0, float vspacing = 0) + public Margins(float left, float top, float right, float bottom, float hspacing = 0, float vspacing = 0) { Top = top; Bottom = bottom; diff --git a/Cirrious.FluentLayout/NfloatExtensions.cs b/Cirrious.FluentLayout/NfloatExtensions.cs deleted file mode 100644 index 5ac6617..0000000 --- a/Cirrious.FluentLayout/NfloatExtensions.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace System -{ - internal static class NfloatExtensions - { - public static nfloat GetValueOrDefault(this nfloat? value) => value.GetValueOrDefault(0); - - public static nfloat GetValueOrDefault(this nfloat? value, nfloat defaultValue) => value ?? defaultValue; - } -} - diff --git a/Cirrious.FluentLayout/Properties/AssemblyInfo.cs b/Cirrious.FluentLayout/Properties/AssemblyInfo.cs deleted file mode 100644 index b56a76e..0000000 --- a/Cirrious.FluentLayout/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,46 +0,0 @@ -// AssemblyInfo.cs -// (c) Copyright Cirrious Ltd. http://www.cirrious.com -// MvvmCross is licensed using Microsoft Public License (Ms-PL) -// Contributions and inspirations noted in readme.md and license.txt -// -// Project Lead - Stuart Lodge, @slodge, me@slodge.com - -using System.Reflection; -using System.Runtime.InteropServices; - -// General Information about an assembly is controlled through the following -// set of attributes. Change these attribute values to modify the information -// associated with an assembly. - -[assembly: AssemblyTitle("Cirrious.FluentLayout")] -[assembly: AssemblyDescription("Easy API for using AutoLayout in iOS")] -[assembly: AssemblyConfiguration("")] -[assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("Cirrious.FluentLayout")] -[assembly: AssemblyCopyright("Copyright © 2018")] -[assembly: AssemblyTrademark("")] -[assembly: AssemblyCulture("")] - -// Setting ComVisible to false makes the types in this assembly not visible -// to COM components. If you need to access a type in this assembly from -// COM, set the ComVisible attribute to true on that type. - -[assembly: ComVisible(false)] - -// The following GUID is for the ID of the typelib if this project is exposed to COM - -[assembly: Guid("deb03d05-a92d-4319-b09d-3f890065a6ce")] - -// Version information for an assembly consists of the following four values: -// -// Major Version -// Minor Version -// Build Number -// Revision -// -// You can specify all the values or you can default the Build and Revision Numbers -// by using the '*' as shown below: -// [assembly: AssemblyVersion("1.0.*")] - -[assembly: AssemblyVersion("2.6.0.0")] -[assembly: AssemblyFileVersion("2.6.0.0")] \ No newline at end of file diff --git a/Cirrious.FluentLayout/RowSet-WorkInProgress/RowSet.cs b/Cirrious.FluentLayout/RowSet-WorkInProgress/RowSet.cs index 3f65675..043fe6a 100644 --- a/Cirrious.FluentLayout/RowSet-WorkInProgress/RowSet.cs +++ b/Cirrious.FluentLayout/RowSet-WorkInProgress/RowSet.cs @@ -1,167 +1,155 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using UIKit; +namespace Cirrious.FluentLayouts.Touch.RowSet; -namespace Cirrious.FluentLayouts.Touch.RowSet +public class RowSetTemplate { - public class RowSetTemplate - { - public float TopMargin { get; set; } - public float BottomMargin { get; set; } - public float VInterspacing { get; set; } + public float TopMargin { get; set; } + public float BottomMargin { get; set; } + public float VInterspacing { get; set; } - public IEnumerable Generate(UIView container, params Row[] rows) + public IEnumerable Generate(UIView container, params Row[] rows) + { + for (var i = 0; i < rows.Length; i++) { - for (var i = 0; i < rows.Length; i++) - { - var verticalGenerators = new List>(); + var verticalGenerators = new List>(); - var isFirst = i == 0; - if (isFirst) - verticalGenerators.Add(view => view.AtTopOf(container, TopMargin)); - else - { - var previousRowView = rows[i - 1].Views.First(); - verticalGenerators.Add(view => view.Below(previousRowView, VInterspacing)); - } + var isFirst = i == 0; + if (isFirst) + verticalGenerators.Add(view => view.AtTopOf(container, TopMargin)); + else + { + var previousRowView = rows[i - 1].Views.First(); + verticalGenerators.Add(view => view.Below(previousRowView, VInterspacing)); + } - var isLast = i == rows.Length - 1; - if (isLast) - verticalGenerators.Add(view => view.AtBottomOf(container, BottomMargin)); + var isLast = i == rows.Length - 1; + if (isLast) + verticalGenerators.Add(view => view.AtBottomOf(container, BottomMargin)); - foreach (var view in rows[i].Views) - { - foreach (var verticalGenerator in verticalGenerators) - yield return verticalGenerator(view); - } - - foreach (var horizontalConstraint in rows[i].Template.Generate(container, rows[i].Views.ToArray())) - yield return horizontalConstraint; + foreach (var view in rows[i].Views) + { + foreach (var verticalGenerator in verticalGenerators) + yield return verticalGenerator(view); } + + foreach (var horizontalConstraint in rows[i].Template.Generate(container, rows[i].Views.ToArray())) + yield return horizontalConstraint; } } +} - public class Row +public class Row +{ + public Row(IRowTemplate rowTemplate, params UIView[] views) { - public Row() - { - } + Template = rowTemplate; + Views = views; + } - public Row(IRowTemplate rowTemplate, params UIView[] views) - { - Template = rowTemplate; - Views = views; - } + public IRowTemplate Template { get; set; } - public IRowTemplate Template { get; set; } - public IEnumerable Views { get; set; } - } + public IEnumerable Views { get; set; } +} - public interface IRowTemplate +public interface IRowTemplate +{ + IEnumerable Generate(UIView container, params UIView[] views); +} + +public class RowTemplate : IRowTemplate +{ + public float LeftMargin { get; set; } + public float RightMargin { get; set; } + public float HInterspacing { get; set; } + + public class Column { - IEnumerable Generate(UIView container, params UIView[] views); + public static readonly Column Default = new WeightedWidthColumn(); } - public class RowTemplate : IRowTemplate + public class FixedWidthColumn + : Column { - public float LeftMargin { get; set; } - public float RightMargin { get; set; } - public float HInterspacing { get; set; } + public float Width { get; set; } - public class Column + public FixedWidthColumn(float width) { - public static readonly Column Default = new WeightedWidthColumn(); + Width = width; } + } - public class FixedWidthColumn - : Column - { - public float Width { get; set; } + public class WeightedWidthColumn + : Column + { + public float Weight { get; set; } - public FixedWidthColumn(float width) - { - Width = width; - } + public WeightedWidthColumn(float weight = 1.0f) + { + Weight = weight; } + } - public class WeightedWidthColumn - : Column - { - public float Weight { get; set; } + private readonly Dictionary _columnDefinitions = new Dictionary(); - public WeightedWidthColumn(float weight = 1.0f) - { - Weight = weight; - } - } + public void ColumnWidth(int position, float width) + { + _columnDefinitions[position] = new FixedWidthColumn(width); + } - private readonly Dictionary _columnDefinitions = new Dictionary(); + public void ColumnWeight(int position, float weight) + { + _columnDefinitions[position] = new WeightedWidthColumn(weight); + } - public void ColumnWidth(int position, float width) - { - _columnDefinitions[position] = new FixedWidthColumn(width); - } + public IEnumerable Generate(UIView container, params UIView[] views) + { + WeightedWidthColumn? firstWeightedColumn = default; + UIView? firstWeightedView = default; - public void ColumnWeight(int position, float weight) + for (var i = 0; i < views.Length; i++) { - _columnDefinitions[position] = new WeightedWidthColumn(weight); - } + var view = views[i]; + var column = GetColumn(i); - public IEnumerable Generate(UIView container, params UIView[] views) - { - WeightedWidthColumn firstWeightedColumn = null; - UIView firstWeightedView = null; + if (i == 0) + { + yield return view.AtLeftOf(container, LeftMargin); + } + else + { + yield return view.ToRightOf(views[i - 1], HInterspacing); + } - for (var i = 0; i < views.Length; i++) + if (i == views.Length - 1) { - var view = views[i]; - var column = GetColumn(i); + yield return view.AtRightOf(container, RightMargin); + } - if (i == 0) + if (column is WeightedWidthColumn weightedColumn) + { + if (firstWeightedColumn == null) { - yield return view.AtLeftOf(container, LeftMargin); + firstWeightedColumn = weightedColumn; + firstWeightedView = view; } else { - yield return view.ToRightOf(views[i - 1], HInterspacing); - } - - if (i == views.Length - 1) - { - yield return view.AtRightOf(container, RightMargin); - } - - var weightedColumn = column as WeightedWidthColumn; - if (weightedColumn != null) - { - if (firstWeightedColumn == null) - { - firstWeightedColumn = weightedColumn; - firstWeightedView = view; - } - else - { - var multiplier = weightedColumn.Weight / firstWeightedColumn.Weight; - yield return view.WithRelativeWidth(firstWeightedView, multiplier); - } + var multiplier = weightedColumn.Weight / firstWeightedColumn.Weight; + yield return view.WithRelativeWidth(firstWeightedView!, multiplier); } + } - var fixedColumn = column as FixedWidthColumn; - if (fixedColumn != null) - { - yield return view.Width().EqualTo(fixedColumn.Width); - } + if (column is FixedWidthColumn fixedColumn) + { + yield return view.Width().EqualTo(fixedColumn.Width); } } + } - private Column GetColumn(int index) - { - Column column; - if (_columnDefinitions.TryGetValue(index, out column)) - return column; + private Column GetColumn(int index) + { + if (_columnDefinitions.TryGetValue(index, out var column)) + return column; - return Column.Default; - } + return Column.Default; } } diff --git a/Cirrious.FluentLayout/UIViewAndLayoutAttribute.cs b/Cirrious.FluentLayout/UIViewAndLayoutAttribute.cs index 308bcc0..c6a041a 100644 --- a/Cirrious.FluentLayout/UIViewAndLayoutAttribute.cs +++ b/Cirrious.FluentLayout/UIViewAndLayoutAttribute.cs @@ -5,9 +5,6 @@ // // Project Lead - Stuart Lodge, @slodge, me@slodge.com -using UIKit; -using System; - namespace Cirrious.FluentLayouts.Touch { public class UIViewAndLayoutAttribute @@ -21,13 +18,13 @@ public UIViewAndLayoutAttribute(UIView view, NSLayoutAttribute attribute) public UIView View { get; } public NSLayoutAttribute Attribute { get; } - public FluentLayout EqualTo(nfloat constant = default(nfloat)) => + public FluentLayout EqualTo(nfloat constant = default(nfloat)) => new FluentLayout(View, Attribute, NSLayoutRelation.Equal, constant); - public FluentLayout GreaterThanOrEqualTo(nfloat constant = default(nfloat)) => + public FluentLayout GreaterThanOrEqualTo(nfloat constant = default(nfloat)) => new FluentLayout(View, Attribute, NSLayoutRelation.GreaterThanOrEqual, constant); - public FluentLayout LessThanOrEqualTo(nfloat constant = default(nfloat)) => + public FluentLayout LessThanOrEqualTo(nfloat constant = default(nfloat)) => new FluentLayout(View, Attribute, NSLayoutRelation.LessThanOrEqual, constant); } } \ No newline at end of file diff --git a/QuickLayout.sln b/QuickLayout.sln index 1d5b138..b601fcd 100644 --- a/QuickLayout.sln +++ b/QuickLayout.sln @@ -1,7 +1,9 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickLayout.Core", "QuickLayout.Core\QuickLayout.Core.csproj", "{849AFB57-994A-42D9-A786-EF34FC951CDE}" +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32901.215 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QuickLayout.Core", "QuickLayout.Core\QuickLayout.Core.csproj", "{849AFB57-994A-42D9-A786-EF34FC951CDE}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickLayout.Touch", "QuickLayout.Touch\QuickLayout.Touch.csproj", "{5739EE87-45C0-496A-8388-A09CDB906501}" EndProject @@ -27,6 +29,31 @@ Global Release|Mixed Platforms = Release|Mixed Platforms EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|Any CPU.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|Any CPU.Build.0 = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|iPhone.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|Mixed Platforms.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|Mixed Platforms.Build.0 = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|Any CPU.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|Any CPU.Build.0 = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|iPhone.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|iPhoneSimulator.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|Mixed Platforms.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|Mixed Platforms.Build.0 = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|iPhone.ActiveCfg = Debug|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|Any CPU.Build.0 = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|iPhone.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU + {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|Mixed Platforms.Build.0 = Release|Any CPU {5739EE87-45C0-496A-8388-A09CDB906501}.Ad-Hoc|Any CPU.ActiveCfg = Ad-Hoc|iPhone {5739EE87-45C0-496A-8388-A09CDB906501}.Ad-Hoc|iPhone.ActiveCfg = Ad-Hoc|iPhone {5739EE87-45C0-496A-8388-A09CDB906501}.Ad-Hoc|iPhone.Build.0 = Ad-Hoc|iPhone @@ -41,7 +68,7 @@ Global {5739EE87-45C0-496A-8388-A09CDB906501}.AppStore|iPhoneSimulator.Build.0 = AppStore|iPhoneSimulator {5739EE87-45C0-496A-8388-A09CDB906501}.AppStore|Mixed Platforms.ActiveCfg = AppStore|iPhone {5739EE87-45C0-496A-8388-A09CDB906501}.AppStore|Mixed Platforms.Build.0 = AppStore|iPhone - {5739EE87-45C0-496A-8388-A09CDB906501}.Debug|Any CPU.ActiveCfg = Debug|iPhone + {5739EE87-45C0-496A-8388-A09CDB906501}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {5739EE87-45C0-496A-8388-A09CDB906501}.Debug|iPhone.ActiveCfg = Debug|iPhone {5739EE87-45C0-496A-8388-A09CDB906501}.Debug|iPhone.Build.0 = Debug|iPhone {5739EE87-45C0-496A-8388-A09CDB906501}.Debug|iPhoneSimulator.ActiveCfg = Debug|iPhoneSimulator @@ -80,36 +107,14 @@ Global {75D2DA9D-DFD4-49A1-98FB-FE0F0677EF0F}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU {75D2DA9D-DFD4-49A1-98FB-FE0F0677EF0F}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {75D2DA9D-DFD4-49A1-98FB-FE0F0677EF0F}.Release|Mixed Platforms.Build.0 = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|Any CPU.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|Any CPU.Build.0 = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|iPhone.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|Mixed Platforms.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Ad-Hoc|Mixed Platforms.Build.0 = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|Any CPU.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|Any CPU.Build.0 = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|iPhone.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|iPhoneSimulator.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|Mixed Platforms.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.AppStore|Mixed Platforms.Build.0 = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|iPhone.ActiveCfg = Debug|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|Any CPU.Build.0 = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|iPhone.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU - {849AFB57-994A-42D9-A786-EF34FC951CDE}.Release|Mixed Platforms.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(MonoDevelopProperties) = preSolution - StartupItem = QuickLayout.Touch\QuickLayout.Touch.csproj EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {E15DDE52-BCF6-4379-A6E0-0DE62C109B84} + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = QuickLayout.Touch\QuickLayout.Touch.csproj + EndGlobalSection EndGlobal