Browse Source

Anchor -> GetAnchor

Tig 1 year ago
parent
commit
44e496d923

+ 18 - 18
Terminal.Gui/View/Layout/Dim.cs

@@ -255,7 +255,7 @@ public class Dim
     ///     subclass of Dim that is used. For example, DimAbsolute returns a fixed dimension, DimFactor returns a
     ///     dimension that is a certain percentage of the super view's size, and so on.
     /// </returns>
-    internal virtual int Anchor (int size) { return 0; }
+    internal virtual int GetAnchor (int size) { return 0; }
 
     /// <summary>
     ///     Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
@@ -275,7 +275,7 @@ public class Dim
     /// </returns>
     internal virtual int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
     {
-        return Math.Max (Anchor (superviewContentSize - location), 0);
+        return Math.Max (GetAnchor (superviewContentSize - location), 0);
     }
 
     #endregion virtual methods
@@ -290,7 +290,7 @@ public class Dim
     {
         if (left is DimAbsolute && right is DimAbsolute)
         {
-            return new DimAbsolute (left.Anchor (0) + right.Anchor (0));
+            return new DimAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
         }
 
         var newDim = new DimCombine (true, left, right);
@@ -315,7 +315,7 @@ public class Dim
     {
         if (left is DimAbsolute && right is DimAbsolute)
         {
-            return new DimAbsolute (left.Anchor (0) - right.Anchor (0));
+            return new DimAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
         }
 
         var newDim = new DimCombine (false, left, right);
@@ -338,7 +338,7 @@ public class Dim
     public override bool Equals (object? other) { return other is Dim abs && abs == this; }
 
     /// <inheritdoc/>
-    public override int GetHashCode () { return Anchor (0).GetHashCode (); }
+    public override int GetHashCode () { return GetAnchor (0).GetHashCode (); }
 
     #endregion overrides
 }
@@ -369,12 +369,12 @@ public class DimAbsolute (int size) : Dim
     /// <inheritdoc/>
     public override string ToString () { return $"Absolute({Size})"; }
 
-    internal override int Anchor (int size) { return Size; }
+    internal override int GetAnchor (int size) { return Size; }
 
     internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
     {
         // DimAbsolute.Anchor (int size) ignores width and returns n
-        return Math.Max (Anchor (0), 0);
+        return Math.Max (GetAnchor (0), 0);
     }
 }
 
@@ -428,13 +428,13 @@ public class DimAuto (DimAutoStyle style, Dim? minimumContentDim, Dim? maximumCo
     {
         if (us == null)
         {
-            return MaximumContentDim?.Anchor (0) ?? 0;
+            return MaximumContentDim?.GetAnchor (0) ?? 0;
         }
 
         var textSize = 0;
         var subviewsSize = 0;
 
-        int autoMin = MinimumContentDim?.Anchor (superviewContentSize) ?? 0;
+        int autoMin = MinimumContentDim?.GetAnchor (superviewContentSize) ?? 0;
 
         if (superviewContentSize < autoMin)
         {
@@ -548,7 +548,7 @@ public class DimAuto (DimAutoStyle style, Dim? minimumContentDim, Dim? maximumCo
         }
 
         // If max: is set, clamp the return - BUGBUG: Not tested
-        return int.Min (max, MaximumContentDim?.Anchor (superviewContentSize) ?? max);
+        return int.Min (max, MaximumContentDim?.GetAnchor (superviewContentSize) ?? max);
     }
 
     internal override bool ReferencesOtherViews ()
@@ -591,10 +591,10 @@ public class DimCombine (bool add, Dim? left, Dim? right) : Dim
     /// <inheritdoc/>
     public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
 
-    internal override int Anchor (int size)
+    internal override int GetAnchor (int size)
     {
-        int la = Left!.Anchor (size);
-        int ra = Right!.Anchor (size);
+        int la = Left!.GetAnchor (size);
+        int ra = Right!.GetAnchor (size);
 
         if (Add)
         {
@@ -679,11 +679,11 @@ public class DimPercent (float percent, bool usePosition = false) : Dim
     /// </summary>
     public bool UsePosition { get; } = usePosition;
 
-    internal override int Anchor (int size) { return (int)(size * Percent); }
+    internal override int GetAnchor (int size) { return (int)(size * Percent); }
 
     internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
     {
-        return UsePosition ? Math.Max (Anchor (superviewContentSize - location), 0) : Anchor (superviewContentSize);
+        return UsePosition ? Math.Max (GetAnchor (superviewContentSize - location), 0) : GetAnchor (superviewContentSize);
     }
 }
 
@@ -711,7 +711,7 @@ public class DimFill (int margin) : Dim
     /// <inheritdoc/>
     public override string ToString () { return $"Fill({Margin})"; }
 
-    internal override int Anchor (int size) { return size - Margin; }
+    internal override int GetAnchor (int size) { return size - Margin; }
 }
 
 /// <summary>
@@ -738,7 +738,7 @@ public class DimFunc (Func<int> dim) : Dim
     /// <inheritdoc/>
     public override string ToString () { return $"DimFunc({Func ()})"; }
 
-    internal override int Anchor (int size) { return Func (); }
+    internal override int GetAnchor (int size) { return Func (); }
 }
 
 /// <summary>
@@ -795,7 +795,7 @@ public class DimView : Dim
         return $"View({dimString},{Target})";
     }
 
-    internal override int Anchor (int size)
+    internal override int GetAnchor (int size)
     {
         return Dimension switch
         {

+ 18 - 18
Terminal.Gui/View/Layout/Pos.cs

@@ -288,7 +288,7 @@ public class Pos
     #region virtual methods
 
     /// <summary>
-    ///     Calculates and returns the starting point of an element based on the size of the parent element (typically
+    ///     Gets the starting point of an element based on the size of the parent element (typically
     ///     <c>Superview.ContentSize</c>).
     ///     This method is meant to be overridden by subclasses to provide different ways of calculating the starting point.
     ///     This method is used
@@ -300,7 +300,7 @@ public class Pos
     ///     subclass of Pos that is used. For example, PosAbsolute returns a fixed position, PosAnchorEnd returns a
     ///     position that is anchored to the end of the layout, and so on.
     /// </returns>
-    internal virtual int Anchor (int size) { return 0; }
+    internal virtual int GetAnchor (int size) { return 0; }
 
     /// <summary>
     ///     Calculates and returns the final position of a <see cref="View"/> object. It takes into account the dimension of
@@ -321,7 +321,7 @@ public class Pos
     ///     that
     ///     is used.
     /// </returns>
-    internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { return Anchor (superviewDimension); }
+    internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { return GetAnchor (superviewDimension); }
 
     /// <summary>
     ///     Diagnostics API to determine if this Pos object references other views.
@@ -341,7 +341,7 @@ public class Pos
     {
         if (left is PosAbsolute && right is PosAbsolute)
         {
-            return new PosAbsolute (left.Anchor (0) + right.Anchor (0));
+            return new PosAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
         }
 
         var newPos = new PosCombine (true, left, right);
@@ -370,7 +370,7 @@ public class Pos
     {
         if (left is PosAbsolute && right is PosAbsolute)
         {
-            return new PosAbsolute (left.Anchor (0) - right.Anchor (0));
+            return new PosAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
         }
 
         var newPos = new PosCombine (false, left, right);
@@ -392,7 +392,7 @@ public class Pos
 
     /// <summary>Serves as the default hash function. </summary>
     /// <returns>A hash code for the current object.</returns>
-    public override int GetHashCode () { return Anchor (0).GetHashCode (); }
+    public override int GetHashCode () { return GetAnchor (0).GetHashCode (); }
 
     #endregion overrides
 }
@@ -423,7 +423,7 @@ public class PosAbsolute (int position) : Pos
     /// <inheritdoc/>
     public override string ToString () { return $"Absolute({Position})"; }
 
-    internal override int Anchor (int size) { return Position; }
+    internal override int GetAnchor (int size) { return Position; }
 }
 
 /// <summary>
@@ -469,7 +469,7 @@ public class PosAnchorEnd : Pos
     /// <inheritdoc/>
     public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({Offset})"; }
 
-    internal override int Anchor (int size)
+    internal override int GetAnchor (int size)
     {
         if (UseDimForOffset)
         {
@@ -481,11 +481,11 @@ public class PosAnchorEnd : Pos
 
     internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
     {
-        int newLocation = Anchor (superviewDimension);
+        int newLocation = GetAnchor (superviewDimension);
 
         if (UseDimForOffset)
         {
-            newLocation -= dim.Anchor (superviewDimension);
+            newLocation -= dim.GetAnchor (superviewDimension);
         }
 
         return newLocation;
@@ -500,13 +500,13 @@ public class PosCenter : Pos
     /// <inheritdoc/>
     public override string ToString () { return "Center"; }
 
-    internal override int Anchor (int size) { return size / 2; }
+    internal override int GetAnchor (int size) { return size / 2; }
 
     internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
     {
         int newDimension = Math.Max (dim.Calculate (0, superviewDimension, us, dimension), 0);
 
-        return Anchor (superviewDimension - newDimension);
+        return GetAnchor (superviewDimension - newDimension);
     }
 }
 
@@ -546,10 +546,10 @@ public class PosCombine (bool add, Pos left, Pos right) : Pos
     /// <inheritdoc/>
     public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
 
-    internal override int Anchor (int size)
+    internal override int GetAnchor (int size)
     {
-        int la = Left.Anchor (size);
-        int ra = Right.Anchor (size);
+        int la = Left.GetAnchor (size);
+        int ra = Right.GetAnchor (size);
 
         if (Add)
         {
@@ -614,7 +614,7 @@ public class PosPercent (float percent) : Pos
     /// <inheritdoc/>
     public override string ToString () { return $"Percent({Percent})"; }
 
-    internal override int Anchor (int size) { return (int)(size * Percent); }
+    internal override int GetAnchor (int size) { return (int)(size * Percent); }
 }
 
 /// <summary>
@@ -643,7 +643,7 @@ public class PosFunc (Func<int> pos) : Pos
     /// <inheritdoc/>
     public override string ToString () { return $"PosFunc({Func ()})"; }
 
-    internal override int Anchor (int size) { return Func (); }
+    internal override int GetAnchor (int size) { return Func (); }
 }
 
 /// <summary>
@@ -695,7 +695,7 @@ public class PosView (View view, Side side) : Pos
         return $"View(side={sideString},target={Target})";
     }
 
-    internal override int Anchor (int size)
+    internal override int GetAnchor (int size)
     {
         return Side switch
                {

+ 2 - 2
Terminal.Gui/Views/TabView.cs

@@ -1265,7 +1265,7 @@ public class TabView : View
                         tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
                     }
 
-                    tab.Width = Math.Max (tab.Width.Anchor (0) - 1, 1);
+                    tab.Width = Math.Max (tab.Width.GetAnchor (0) - 1, 1);
                 }
                 else
                 {
@@ -1280,7 +1280,7 @@ public class TabView : View
                         tab.Margin.Thickness = new Thickness (0, 0, 0, 0);
                     }
 
-                    tab.Width = Math.Max (tab.Width.Anchor (0) - 1, 1);
+                    tab.Width = Math.Max (tab.Width.GetAnchor (0) - 1, 1);
                 }
 
                 tab.Text = toRender.TextToRender;

+ 9 - 9
Terminal.Gui/Views/TileView.cs

@@ -601,11 +601,11 @@ public class TileView : View
 
         TileViewLineView nextSplitter = visibleSplitterLines [i];
         Pos nextSplitterPos = Orientation == Orientation.Vertical ? nextSplitter.X : nextSplitter.Y;
-        int nextSplitterDistance = nextSplitterPos.Anchor (space);
+        int nextSplitterDistance = nextSplitterPos.GetAnchor (space);
 
         TileViewLineView lastSplitter = i >= 1 ? visibleSplitterLines [i - 1] : null;
         Pos lastSplitterPos = Orientation == Orientation.Vertical ? lastSplitter?.X : lastSplitter?.Y;
-        int lastSplitterDistance = lastSplitterPos?.Anchor (space) ?? 0;
+        int lastSplitterDistance = lastSplitterPos?.GetAnchor (space) ?? 0;
 
         int distance = nextSplitterDistance - lastSplitterDistance;
 
@@ -656,8 +656,8 @@ public class TileView : View
 
     private bool IsValidNewSplitterPos (int idx, Pos value, int fullSpace)
     {
-        int newSize = value.Anchor (fullSpace);
-        bool isGettingBigger = newSize > _splitterDistances [idx].Anchor (fullSpace);
+        int newSize = value.GetAnchor (fullSpace);
+        bool isGettingBigger = newSize > _splitterDistances [idx].GetAnchor (fullSpace);
         int lastSplitterOrBorder = HasBorder () ? 1 : 0;
         int nextSplitterOrBorder = HasBorder () ? fullSpace - 1 : fullSpace;
 
@@ -682,7 +682,7 @@ public class TileView : View
         // Do not allow splitter to move left of the one before
         if (idx > 0)
         {
-            int posLeft = _splitterDistances [idx - 1].Anchor (fullSpace);
+            int posLeft = _splitterDistances [idx - 1].GetAnchor (fullSpace);
 
             if (newSize <= posLeft)
             {
@@ -695,7 +695,7 @@ public class TileView : View
         // Do not allow splitter to move right of the one after
         if (idx + 1 < _splitterDistances.Count)
         {
-            int posRight = _splitterDistances [idx + 1].Anchor (fullSpace);
+            int posRight = _splitterDistances [idx + 1].GetAnchor (fullSpace);
 
             if (newSize >= posRight)
             {
@@ -848,7 +848,7 @@ public class TileView : View
         {
             Dim spaceDim = Tile.ContentView.Width;
 
-            int spaceAbs = spaceDim.Anchor (Parent.Viewport.Width);
+            int spaceAbs = spaceDim.GetAnchor (Parent.Viewport.Width);
 
             var title = $" {Tile.Title} ";
 
@@ -1005,7 +1005,7 @@ public class TileView : View
         private Pos ConvertToPosFactor (Pos p, int parentLength)
         {
             // calculate position in the 'middle' of the cell at p distance along parentLength
-            float position = p.Anchor (parentLength) + 0.5f;
+            float position = p.GetAnchor (parentLength) + 0.5f;
 
             return new PosPercent (position / parentLength);
         }
@@ -1066,7 +1066,7 @@ public class TileView : View
 
         private Pos Offset (Pos pos, int delta)
         {
-            int posAbsolute = pos.Anchor (
+            int posAbsolute = pos.GetAnchor (
                                           Orientation == Orientation.Horizontal
                                               ? Parent.Viewport.Height
                                               : Parent.Viewport.Width

+ 1 - 1
Terminal.Gui/Views/Toplevel.cs

@@ -419,7 +419,7 @@ public partial class Toplevel : View
             && !top.Subviews.Contains (sb)
             && ny + top.Frame.Height != superView.Frame.Height - (sb.Visible ? 1 : 0)
             && top.Height is DimFill
-            && -top.Height.Anchor (0) < 1)
+            && -top.Height.GetAnchor (0) < 1)
         {
             top.Height = Dim.Fill (sb.Visible ? 1 : 0);
             layoutSubviews = true;

+ 6 - 6
UnitTests/View/Layout/Dim.Tests.cs

@@ -286,24 +286,24 @@ public class DimTests
     public void Internal_Tests ()
     {
         var dimFactor = new DimPercent (0.10F);
-        Assert.Equal (10, dimFactor.Anchor (100));
+        Assert.Equal (10, dimFactor.GetAnchor (100));
 
         var dimAbsolute = new DimAbsolute (10);
-        Assert.Equal (10, dimAbsolute.Anchor (0));
+        Assert.Equal (10, dimAbsolute.GetAnchor (0));
 
         var dimFill = new DimFill (1);
-        Assert.Equal (99, dimFill.Anchor (100));
+        Assert.Equal (99, dimFill.GetAnchor (100));
 
         var dimCombine = new DimCombine (true, dimFactor, dimAbsolute);
         Assert.Equal (dimCombine.Left, dimFactor);
         Assert.Equal (dimCombine.Right, dimAbsolute);
-        Assert.Equal (20, dimCombine.Anchor (100));
+        Assert.Equal (20, dimCombine.GetAnchor (100));
 
         var view = new View { Frame = new Rectangle (20, 10, 20, 1) };
         var dimViewHeight = new DimView (view, Dimension.Height);
-        Assert.Equal (1, dimViewHeight.Anchor (0));
+        Assert.Equal (1, dimViewHeight.GetAnchor (0));
         var dimViewWidth = new DimView (view, Dimension.Width);
-        Assert.Equal (20, dimViewWidth.Anchor (0));
+        Assert.Equal (20, dimViewWidth.GetAnchor (0));
 
         view.Dispose ();
     }

+ 6 - 6
UnitTests/View/Layout/Pos.AnchorEndTests.cs

@@ -46,13 +46,13 @@ public class PosAnchorEndTests (ITestOutputHelper output)
     }
 
     [Fact]
-    public void PosAnchorEnd_Anchor ()
+    public void PosAnchorEnd_GetAnchor ()
     {
         var posAnchorEnd = new PosAnchorEnd (10);
         var width = 50;
         var expectedAnchor = width - 10;
 
-        Assert.Equal (expectedAnchor, posAnchorEnd.Anchor (width));
+        Assert.Equal (expectedAnchor, posAnchorEnd.GetAnchor (width));
     }
 
     [Fact]
@@ -73,10 +73,10 @@ public class PosAnchorEndTests (ITestOutputHelper output)
     [Theory]
     [InlineData (0)]
     [InlineData (1)]
-    public void  PosAnchorEnd_SetsValue_Anchor_Is_Negative (int offset)
+    public void  PosAnchorEnd_SetsValue_GetAnchor_Is_Negative (int offset)
     {
         Pos pos = Pos.AnchorEnd (offset);
-        Assert.Equal (offset, -pos.Anchor (0));
+        Assert.Equal (offset, -pos.GetAnchor (0));
     }
 
     [Theory]
@@ -119,10 +119,10 @@ public class PosAnchorEndTests (ITestOutputHelper output)
     }
 
     [Fact]
-    public void  PosAnchorEnd_UseDimForOffset_SetsValue_Anchor_Is_Negative ()
+    public void  PosAnchorEnd_UseDimForOffset_SetsValue_GetAnchor_Is_Negative ()
     {
         Pos pos = Pos.AnchorEnd ();
-        Assert.Equal (-10, -pos.Anchor (10));
+        Assert.Equal (-10, -pos.GetAnchor (10));
     }
 
     [Theory]

+ 2 - 2
UnitTests/View/Layout/Pos.CenterTests.cs

@@ -38,13 +38,13 @@ public class PosCenterTests (ITestOutputHelper output)
     }
 
     [Fact]
-    public void PosCenter_Anchor ()
+    public void PosCenter_GetAnchor ()
     {
         var posCenter = new PosCenter ();
         var width = 50;
         var expectedAnchor = width / 2;
 
-        Assert.Equal (expectedAnchor, posCenter.Anchor (width));
+        Assert.Equal (expectedAnchor, posCenter.GetAnchor (width));
     }
 
     [Fact]

+ 10 - 10
UnitTests/View/Layout/Pos.Tests.cs

@@ -181,36 +181,36 @@ public class PosTests ()
     public void Internal_Tests ()
     {
         var posFactor = new PosPercent (0.10F);
-        Assert.Equal (10, posFactor.Anchor (100));
+        Assert.Equal (10, posFactor.GetAnchor (100));
 
         var posAnchorEnd = new PosAnchorEnd (1);
-        Assert.Equal (99, posAnchorEnd.Anchor (100));
+        Assert.Equal (99, posAnchorEnd.GetAnchor (100));
 
         var posCenter = new PosCenter ();
-        Assert.Equal (50, posCenter.Anchor (100));
+        Assert.Equal (50, posCenter.GetAnchor (100));
 
         var posAbsolute = new PosAbsolute (10);
-        Assert.Equal (10, posAbsolute.Anchor (0));
+        Assert.Equal (10, posAbsolute.GetAnchor (0));
 
         var posCombine = new PosCombine (true, posFactor, posAbsolute);
         Assert.Equal (posCombine.Left, posFactor);
         Assert.Equal (posCombine.Right, posAbsolute);
-        Assert.Equal (20, posCombine.Anchor (100));
+        Assert.Equal (20, posCombine.GetAnchor (100));
 
         posCombine = new (true, posAbsolute, posFactor);
         Assert.Equal (posCombine.Left, posAbsolute);
         Assert.Equal (posCombine.Right, posFactor);
-        Assert.Equal (20, posCombine.Anchor (100));
+        Assert.Equal (20, posCombine.GetAnchor (100));
 
         var view = new View { Frame = new (20, 10, 20, 1) };
         var posViewX = new PosView (view, Side.Left);
-        Assert.Equal (20, posViewX.Anchor (0));
+        Assert.Equal (20, posViewX.GetAnchor (0));
         var posViewY = new PosView (view, Side.Top);
-        Assert.Equal (10, posViewY.Anchor (0));
+        Assert.Equal (10, posViewY.GetAnchor (0));
         var posRight = new PosView (view, Side.Right);
-        Assert.Equal (40, posRight.Anchor (0));
+        Assert.Equal (40, posRight.GetAnchor (0));
         var posViewBottom = new PosView (view, Side.Bottom);
-        Assert.Equal (11, posViewBottom.Anchor (0));
+        Assert.Equal (11, posViewBottom.GetAnchor (0));
 
         view.Dispose ();
     }

+ 5 - 1
docfx/docs/migratingfromv1.md

@@ -81,9 +81,11 @@ In v1, `Application.Init` automatically created a toplevel view and set `Applica
 * Update any code that assumed `Application.Init` automatically created a toplevel view and set `Applicaton.Top`.
 * Update any code that assumed `Application.Init` automatically disposed of the toplevel view when the application exited.
 
-## `Pos` and `Dim` types are no-longer internal nested classes
+## `Pos` and `Dim` types now adhere to standard C# idioms
 
 * In v1, the `Pos` and `Dim` types (e.g. `Pos.PosView`) were nested classes and marked `internal`. In v2, they are no longer nested, and have appropriate public APIs. 
+* Nullabilty is enabled.
+* Methods & properties follow standards.
 * The static method that creates a `PosAbsolute`, `Pos.At`, was renamed to `Pos.Absolute` for consistency.
 * The static method that crates as `DimAbsoulte`, `Dim.Sized`, was renamed to `Dim.Absolute` for consistency.
 
@@ -93,6 +95,8 @@ In v1, `Application.Init` automatically created a toplevel view and set `Applica
 * Search and replace `Dim.Dim` -> `Dim`.
 * Search and replace `Pos.At` -> `Pos.Absolute`
 * Search and replace `Dim.Sized` -> `Dim.Absolute`
+* Search and replace `Dim.Anchor` -> `Dim.GetAnchor`
+* Search and replace `Pos.Anchor` -> `Pos.GetAnchor`
 
 ## Layout Improvements