Browse Source

Renamed Pos.Function->Func to align iwth C# Func type.
Pos API doc fixes and code cleanup.

Tig 1 year ago
parent
commit
00daccfcf1

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

@@ -51,7 +51,7 @@ public enum Side
 ///             </listheader>
 ///             </listheader>
 ///             <item>
 ///             <item>
 ///                 <term>
 ///                 <term>
-///                     <see cref="Pos.Function(Func{int})"/>
+///                     <see cref="Func"/>
 ///                 </term>
 ///                 </term>
 ///                 <description>
 ///                 <description>
 ///                     Creates a <see cref="Pos"/> object that computes the position by executing the provided
 ///                     Creates a <see cref="Pos"/> object that computes the position by executing the provided
@@ -220,7 +220,7 @@ public class Pos
     /// </summary>
     /// </summary>
     /// <param name="function">The function to be executed.</param>
     /// <param name="function">The function to be executed.</param>
     /// <returns>The <see cref="Pos"/> returned from the function.</returns>
     /// <returns>The <see cref="Pos"/> returned from the function.</returns>
-    public static Pos Function (Func<int> function) { return new PosFunc (function); }
+    public static Pos Func (Func<int> function) { return new PosFunc (function); }
 
 
     /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
     /// <summary>Creates a percentage <see cref="Pos"/> object</summary>
     /// <returns>The percent <see cref="Pos"/> object.</returns>
     /// <returns>The percent <see cref="Pos"/> object.</returns>

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

@@ -68,7 +68,7 @@ public class FileDialog : Dialog
 
 
         _btnOk = new Button
         _btnOk = new Button
         {
         {
-            Y = Pos.AnchorEnd (1), X = Pos.Function (CalculateOkButtonPosX), IsDefault = true, Text = Style.OkButtonText
+            Y = Pos.AnchorEnd (1), X = Pos.Func (CalculateOkButtonPosX), IsDefault = true, Text = Style.OkButtonText
         };
         };
         _btnOk.Accept += (s, e) => Accept (true);
         _btnOk.Accept += (s, e) => Accept (true);
 
 
@@ -457,7 +457,7 @@ public class FileDialog : Dialog
 
 
         if (Style.FlipOkCancelButtonLayoutOrder)
         if (Style.FlipOkCancelButtonLayoutOrder)
         {
         {
-            _btnCancel.X = Pos.Function (CalculateOkButtonPosX);
+            _btnCancel.X = Pos.Func (CalculateOkButtonPosX);
             _btnOk.X = Pos.Right (_btnCancel) + 1;
             _btnOk.X = Pos.Right (_btnCancel) + 1;
 
 
             // Flip tab order too for consistency
             // Flip tab order too for consistency

+ 1 - 1
UICatalog/Scenarios/Adornments.cs

@@ -224,7 +224,7 @@ public class Adornments : Scenario
 
 
             _leftEdit = new ()
             _leftEdit = new ()
             {
             {
-                X = Pos.Left (_topEdit) - Pos.Function (() => _topEdit.Digits) - 2, Y = Pos.Bottom (_topEdit)
+                X = Pos.Left (_topEdit) - Pos.Func (() => _topEdit.Digits) - 2, Y = Pos.Bottom (_topEdit)
             };
             };
 
 
             _leftEdit.ValueChanging += Left_ValueChanging;
             _leftEdit.ValueChanging += Left_ValueChanging;

+ 5 - 7
UnitTests/View/Layout/Dim.FuncTests.cs

@@ -7,19 +7,18 @@ public class DimFuncTests (ITestOutputHelper output)
 {
 {
     private readonly ITestOutputHelper _output = output;
     private readonly ITestOutputHelper _output = output;
 
 
-
     [Fact]
     [Fact]
     public void DimFunc_Equal ()
     public void DimFunc_Equal ()
     {
     {
         Func<int> f1 = () => 0;
         Func<int> f1 = () => 0;
         Func<int> f2 = () => 0;
         Func<int> f2 = () => 0;
 
 
-        Dim dim1 = Dim.Func (f1);
-        Dim dim2 = Dim.Func (f2);
+        Dim dim1 = Func (f1);
+        Dim dim2 = Func (f2);
         Assert.Equal (dim1, dim2);
         Assert.Equal (dim1, dim2);
 
 
         f2 = () => 1;
         f2 = () => 1;
-        dim2 = Dim.Func (f2);
+        dim2 = Func (f2);
         Assert.NotEqual (dim1, dim2);
         Assert.NotEqual (dim1, dim2);
     }
     }
 
 
@@ -27,7 +26,7 @@ public class DimFuncTests (ITestOutputHelper output)
     public void DimFunc_SetsValue ()
     public void DimFunc_SetsValue ()
     {
     {
         var text = "Test";
         var text = "Test";
-        Dim dim = Dim.Func (() => text.Length);
+        Dim dim = Func (() => text.Length);
         Assert.Equal ("DimFunc(4)", dim.ToString ());
         Assert.Equal ("DimFunc(4)", dim.ToString ());
 
 
         text = "New Test";
         text = "New Test";
@@ -37,12 +36,11 @@ public class DimFuncTests (ITestOutputHelper output)
         Assert.Equal ("DimFunc(0)", dim.ToString ());
         Assert.Equal ("DimFunc(0)", dim.ToString ());
     }
     }
 
 
-
     [Fact]
     [Fact]
     public void DimFunc_Calculate_ReturnsCorrectValue ()
     public void DimFunc_Calculate_ReturnsCorrectValue ()
     {
     {
         var dim = new DimFunc (() => 10);
         var dim = new DimFunc (() => 10);
-        var result = dim.Calculate (0, 100, null, Dimension.None);
+        int result = dim.Calculate (0, 100, null, Dimension.None);
         Assert.Equal (10, result);
         Assert.Equal (10, result);
     }
     }
 }
 }

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

@@ -195,7 +195,7 @@ public class PosAnchorEndTests (ITestOutputHelper output)
 
 
         int Btn_Width () { return btn?.Viewport.Width ?? 0; }
         int Btn_Width () { return btn?.Viewport.Width ?? 0; }
 
 
-        btn = new () { Text = "Ok", X = Pos.AnchorEnd (0) - Pos.Function (Btn_Width) };
+        btn = new () { Text = "Ok", X = Pos.AnchorEnd (0) - Pos.Func (Btn_Width) };
 
 
         var view = new View
         var view = new View
         {
         {

+ 45 - 0
UnitTests/View/Layout/Pos.FuncTests.cs

@@ -0,0 +1,45 @@
+using Xunit.Abstractions;
+
+namespace Terminal.Gui.PosDimTests;
+
+public class PosFuncTests (ITestOutputHelper output)
+{
+    private readonly ITestOutputHelper _output = output;
+
+    [Fact]
+    public void PosFunc_Equal ()
+    {
+        Func<int> f1 = () => 0;
+        Func<int> f2 = () => 0;
+
+        Pos pos1 = Pos.Func (f1);
+        Pos pos2 = Pos.Func (f2);
+        Assert.Equal (pos1, pos2);
+
+        f2 = () => 1;
+        pos2 = Pos.Func (f2);
+        Assert.NotEqual (pos1, pos2);
+    }
+
+    [Fact]
+    public void PosFunc_SetsValue ()
+    {
+        var text = "Test";
+        Pos pos = Pos.Func (() => text.Length);
+        Assert.Equal ("PosFunc(4)", pos.ToString ());
+
+        text = "New Test";
+        Assert.Equal ("PosFunc(8)", pos.ToString ());
+
+        text = "";
+        Assert.Equal ("PosFunc(0)", pos.ToString ());
+    }
+
+    [Fact]
+    public void PosFunc_Calculate_ReturnsCorrectValue ()
+    {
+        var pos = new PosFunc (() => 10);
+        int result = pos.Calculate (0, 100, null, Dimension.None);
+        Assert.Equal (10, result);
+    }
+}

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

@@ -185,12 +185,12 @@ public class PosTests (ITestOutputHelper output)
         Func<int> f1 = () => 0;
         Func<int> f1 = () => 0;
         Func<int> f2 = () => 0;
         Func<int> f2 = () => 0;
 
 
-        Pos pos1 = Pos.Function (f1);
-        Pos pos2 = Pos.Function (f2);
+        Pos pos1 = Pos.Func (f1);
+        Pos pos2 = Pos.Func (f2);
         Assert.Equal (pos1, pos2);
         Assert.Equal (pos1, pos2);
 
 
         f2 = () => 1;
         f2 = () => 1;
-        pos2 = Pos.Function (f2);
+        pos2 = Pos.Func (f2);
         Assert.NotEqual (pos1, pos2);
         Assert.NotEqual (pos1, pos2);
     }
     }
 
 
@@ -198,7 +198,7 @@ public class PosTests (ITestOutputHelper output)
     public void PosFunction_SetsValue ()
     public void PosFunction_SetsValue ()
     {
     {
         var text = "Test";
         var text = "Test";
-        Pos pos = Pos.Function (() => text.Length);
+        Pos pos = Pos.Func (() => text.Length);
         Assert.Equal ("PosFunc(4)", pos.ToString ());
         Assert.Equal ("PosFunc(4)", pos.ToString ());
 
 
         text = "New Test";
         text = "New Test";

+ 1 - 1
UnitTests/View/Layout/SetRelativeLayoutTests.cs

@@ -403,7 +403,7 @@ public class SetRelativeLayoutTests
             Width = Auto (DimAutoStyle.Text),
             Width = Auto (DimAutoStyle.Text),
             Height = Auto (DimAutoStyle.Text)
             Height = Auto (DimAutoStyle.Text)
         };
         };
-        view.X = Pos.AnchorEnd (0) - Pos.Function (GetViewWidth);
+        view.X = Pos.AnchorEnd (0) - Pos.Func (GetViewWidth);
 
 
         int GetViewWidth () { return view.Frame.Width; }
         int GetViewWidth () { return view.Frame.Width; }
 
 

+ 1 - 1
UnitTests/Views/LabelTests.cs

@@ -93,7 +93,7 @@ public class LabelTests
     public void AutoSize_Stays_True_AnchorEnd ()
     public void AutoSize_Stays_True_AnchorEnd ()
     {
     {
         var label = new Label { Y = Pos.Center (), Text = "Say Hello 你" };
         var label = new Label { Y = Pos.Center (), Text = "Say Hello 你" };
-        label.X = Pos.AnchorEnd (0) - Pos.Function (() => label.TextFormatter.Text.GetColumns ());
+        label.X = Pos.AnchorEnd (0) - Pos.Func (() => label.TextFormatter.Text.GetColumns ());
 
 
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () };
         win.Add (label);
         win.Add (label);

+ 1 - 1
UnitTests/Views/TileViewTests.cs

@@ -1663,7 +1663,7 @@ public class TileViewTests
         var ex = Assert.Throws<ArgumentException> (() => tileView.SetSplitterPos (0, Pos.Right (tileView)));
         var ex = Assert.Throws<ArgumentException> (() => tileView.SetSplitterPos (0, Pos.Right (tileView)));
         Assert.Equal ("Only Percent and Absolute values are supported. Passed value was PosView", ex.Message);
         Assert.Equal ("Only Percent and Absolute values are supported. Passed value was PosView", ex.Message);
 
 
-        ex = Assert.Throws<ArgumentException> (() => tileView.SetSplitterPos (0, Pos.Function (() => 1)));
+        ex = Assert.Throws<ArgumentException> (() => tileView.SetSplitterPos (0, Pos.Func (() => 1)));
         Assert.Equal ("Only Percent and Absolute values are supported. Passed value was PosFunc", ex.Message);
         Assert.Equal ("Only Percent and Absolute values are supported. Passed value was PosFunc", ex.Message);
 
 
         // Also not allowed because this results in a PosCombine
         // Also not allowed because this results in a PosCombine