浏览代码

Implemented AnchorEnd(); added .UseDimForOffset property

Tig 1 年之前
父节点
当前提交
162aa081ad
共有 3 个文件被更改,包括 54 次插入8 次删除
  1. 22 4
      Terminal.Gui/View/Layout/PosDim.cs
  2. 13 2
      Terminal.Gui/View/Layout/ViewLayout.cs
  3. 19 2
      UnitTests/View/Layout/AnchorEndTests.cs

+ 22 - 4
Terminal.Gui/View/Layout/PosDim.cs

@@ -139,8 +139,7 @@ public class Pos
     /// </example>
     public static Pos AnchorEnd ()
     {
-        throw new NotImplementedException ();
-        //return new PosAnchorEnd (0);
+        return new PosAnchorEnd ();
     }
 
     /// <summary>
@@ -336,11 +335,30 @@ public class Pos
     internal class PosAnchorEnd : Pos
     {
         private readonly int _offset;
+        public PosAnchorEnd () { UseDimForOffset = true; }
         public PosAnchorEnd (int offset) { _offset = offset; }
         public override bool Equals (object other) { return other is PosAnchorEnd anchorEnd && anchorEnd._offset == _offset; }
         public override int GetHashCode () { return _offset.GetHashCode (); }
-        public override string ToString () { return $"AnchorEnd({_offset})"; }
-        internal override int Anchor (int width) { return width - _offset; }
+
+        public bool UseDimForOffset { get; set; }
+
+        public override string ToString ()
+        {
+            if (UseDimForOffset)
+            {
+                return "AnchorEnd()";
+            }
+            return $"AnchorEnd({_offset})";
+        }
+
+        internal override int Anchor (int width)
+        {
+            if (UseDimForOffset)
+            {
+                return width;
+            }
+            return width - _offset;
+        }
     }
 
     internal class PosCenter : Pos

+ 13 - 2
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -1109,7 +1109,6 @@ public partial class View
                                              GetNewDimension (dim, newLocation, superviewDimension, autosizeDimension),
                                              0
                                             );
-
                     break;
 
                 case Pos.PosCombine combine:
@@ -1148,7 +1147,19 @@ public partial class View
 
                     break;
 
-                case Pos.PosAnchorEnd:
+                case Pos.PosAnchorEnd anchorEnd:
+                    newLocation = anchorEnd.Anchor (superviewDimension);
+                    if (anchorEnd.UseDimForOffset)
+                    {
+                        newLocation -= dim.Anchor (0);
+                    }
+
+                    newDimension = Math.Max (
+                                             GetNewDimension (dim, newLocation, superviewDimension, autosizeDimension),
+                                             0
+                                            );
+                    break;
+
                 case Pos.PosAbsolute:
                 case Pos.PosFactor:
                 case Pos.PosFunc:

+ 19 - 2
UnitTests/View/Layout/AnchorEndTests.cs

@@ -107,6 +107,23 @@ public class AnchorEndTests (ITestOutputHelper output)
         Assert.Equal (expectedXPosition, view.Frame.X);
     }
 
+    // UseDimForOffset tests
+
+    [Fact]
+    public void AnchorEnd_UseDimForOffset_CreatesCorrectInstance ()
+    {
+        var pos = Pos.AnchorEnd ();
+        Assert.IsType<PosAnchorEnd> (pos);
+        Assert.True (((PosAnchorEnd)pos).UseDimForOffset);
+    }
+
+    [Fact]
+    public void AnchorEnd_UseDimForOffset_SetsValue_Anchor_Is_Negative ()
+    {
+        Pos pos = Pos.AnchorEnd ();
+        Assert.Equal (-10, -pos.Anchor (10));
+    }
+
     [Theory]
     [InlineData (0, 25)]
     [InlineData (10, 15)]
@@ -114,14 +131,14 @@ public class AnchorEndTests (ITestOutputHelper output)
     [InlineData (11, 14)]
     [InlineData (25, 0)]
     [InlineData (26, -1)]
-    public void AnchorEnd_No_Offset_PositionsViewOffsetByWidth (int width, int expectedXPosition)
+    public void AnchorEnd_UseDimForOffset_PositionsViewOffsetByDim (int dim, int expectedXPosition)
     {
         // Arrange
         var superView = new View { Width = 25, Height = 25 };
         var view = new View
         {
             X = Pos.AnchorEnd (),
-            Width = width,
+            Width = dim,
             Height = 1
         };
         superView.Add (view);