From 1c01556a1da786404ce5facac23ea7a03b73ed3a Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 14 May 2024 18:16:37 -0700 Subject: [PATCH] PosAbsoulte to public. Pos.At -> Pos.Absoulte for consistency --- Terminal.Gui/View/Layout/Pos.cs | 32 ++++++++++++++------ Terminal.Gui/View/Layout/ViewLayout.cs | 4 +-- UICatalog/Scenarios/AllViewsTester.cs | 4 +-- UICatalog/Scenarios/ComputedLayout.cs | 2 +- UnitTests/UICatalog/ScenarioTests.cs | 4 +-- UnitTests/View/Layout/AbsoluteLayoutTests.cs | 28 ++++++++--------- UnitTests/View/Layout/Dim.AutoTests.cs | 2 +- UnitTests/View/Layout/FrameTests.cs | 16 +++++----- UnitTests/View/Layout/Pos.Tests.cs | 16 +++++----- UnitTests/View/Layout/ViewportTests.cs | 16 +++++----- docfx/docs/migratingfromv1.md | 3 +- 11 files changed, 71 insertions(+), 56 deletions(-) diff --git a/Terminal.Gui/View/Layout/Pos.cs b/Terminal.Gui/View/Layout/Pos.cs index d45b242d2..c5db51695 100644 --- a/Terminal.Gui/View/Layout/Pos.cs +++ b/Terminal.Gui/View/Layout/Pos.cs @@ -84,7 +84,7 @@ public enum Side /// /// /// -/// +/// /// /// /// Creates a object that is an absolute position based on the specified @@ -192,8 +192,8 @@ public class Pos /// Creates a object that is an absolute position based on the specified integer value. /// The Absolute . - /// The value to convert to the . - public static Pos At (int n) { return new PosAbsolute (n); } + /// The value to convert to the . + public static Pos Absolute (int position) { return new PosAbsolute (position); } /// Creates a object that can be used to center the . /// The center Pos. @@ -379,13 +379,27 @@ public class Pos internal virtual bool ReferencesOtherViews () { return false; } } -internal class PosAbsolute (int n) : Pos +/// +/// Represents an absolute position in the layout. This is used to specify a fixed position in the layout. +/// +/// +public class PosAbsolute (int position) : Pos { - private readonly int _n = n; - public override bool Equals (object other) { return other is PosAbsolute abs && abs._n == _n; } - public override int GetHashCode () { return _n.GetHashCode (); } - public override string ToString () { return $"Absolute({_n})"; } - internal override int Anchor (int width) { return _n; } + /// + /// The position of the in the layout. + /// + public int Position { get; } = position; + + /// + public override bool Equals (object other) { return other is PosAbsolute abs && abs.Position == Position; } + + /// + public override int GetHashCode () { return Position.GetHashCode (); } + + /// + public override string ToString () { return $"Absolute({Position})"; } + + internal override int Anchor (int width) { return Position; } } internal class PosAnchorEnd : Pos diff --git a/Terminal.Gui/View/Layout/ViewLayout.cs b/Terminal.Gui/View/Layout/ViewLayout.cs index f1d5e9051..77ff05924 100644 --- a/Terminal.Gui/View/Layout/ViewLayout.cs +++ b/Terminal.Gui/View/Layout/ViewLayout.cs @@ -163,7 +163,7 @@ public partial class View return frame; } - private Pos _x = Pos.At (0); + private Pos _x = Pos.Absolute (0); /// Gets or sets the X position for the view (the column). /// The object representing the X position. @@ -202,7 +202,7 @@ public partial class View } } - private Pos _y = Pos.At (0); + private Pos _y = Pos.Absolute (0); /// Gets or sets the Y position for the view (the row). /// The object representing the Y position. diff --git a/UICatalog/Scenarios/AllViewsTester.cs b/UICatalog/Scenarios/AllViewsTester.cs index 4f448edec..116c9aaf5 100644 --- a/UICatalog/Scenarios/AllViewsTester.cs +++ b/UICatalog/Scenarios/AllViewsTester.cs @@ -426,7 +426,7 @@ public class AllViewsTester : Scenario 0 => Pos.Percent (_xVal), 1 => Pos.AnchorEnd (), 2 => Pos.Center (), - 3 => Pos.At (_xVal), + 3 => Pos.Absolute (_xVal), _ => view.X }; @@ -435,7 +435,7 @@ public class AllViewsTester : Scenario 0 => Pos.Percent (_yVal), 1 => Pos.AnchorEnd (), 2 => Pos.Center (), - 3 => Pos.At (_yVal), + 3 => Pos.Absolute (_yVal), _ => view.Y }; diff --git a/UICatalog/Scenarios/ComputedLayout.cs b/UICatalog/Scenarios/ComputedLayout.cs index 6cfcb378c..94d3a1937 100644 --- a/UICatalog/Scenarios/ComputedLayout.cs +++ b/UICatalog/Scenarios/ComputedLayout.cs @@ -64,7 +64,7 @@ public class ComputedLayout : Scenario app.Add (verticalRuler); // Demonstrate At - Using Pos.At to locate a view in an absolute location - var atButton = new Button { Text = "At(2,1)", X = Pos.At (2), Y = Pos.At (1) }; + var atButton = new Button { Text = "At(2,1)", X = Pos.Absolute (2), Y = Pos.Absolute (1) }; app.Add (atButton); // Throw in a literal absolute - Should function identically to above diff --git a/UnitTests/UICatalog/ScenarioTests.cs b/UnitTests/UICatalog/ScenarioTests.cs index d6865c65a..90b825342 100644 --- a/UnitTests/UICatalog/ScenarioTests.cs +++ b/UnitTests/UICatalog/ScenarioTests.cs @@ -372,7 +372,7 @@ public class ScenarioTests : TestsAllViews break; case 3: - view.X = Pos.At (_xVal); + view.X = Pos.Absolute (_xVal); break; } @@ -392,7 +392,7 @@ public class ScenarioTests : TestsAllViews break; case 3: - view.Y = Pos.At (_yVal); + view.Y = Pos.Absolute (_yVal); break; } diff --git a/UnitTests/View/Layout/AbsoluteLayoutTests.cs b/UnitTests/View/Layout/AbsoluteLayoutTests.cs index 7d8472a81..621a397c5 100644 --- a/UnitTests/View/Layout/AbsoluteLayoutTests.cs +++ b/UnitTests/View/Layout/AbsoluteLayoutTests.cs @@ -29,8 +29,8 @@ public class AbsoluteLayoutTests new Rectangle (0, 0, newFrame.Width, newFrame.Height), v.Viewport ); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal ($"Absolute({newFrame.Height})", v.Height.ToString ()); Assert.Equal ($"Absolute({newFrame.Width})", v.Width.ToString ()); v.Dispose (); @@ -178,8 +178,8 @@ public class AbsoluteLayoutTests new Rectangle (0, 0, frame.Width, frame.Height), v.Viewport ); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (0), v.X); - Assert.Equal (Pos.At (0), v.Y); + Assert.Equal (Pos.Absolute (0), v.X); + Assert.Equal (Pos.Absolute (0), v.Y); Assert.Equal (Dim.Sized (0), v.Width); Assert.Equal (Dim.Sized (0), v.Height); v.Dispose (); @@ -193,8 +193,8 @@ public class AbsoluteLayoutTests new Rectangle (0, 0, frame.Width, frame.Height), v.Viewport ); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal (Dim.Sized (3), v.Width); Assert.Equal (Dim.Sized (4), v.Height); v.Dispose (); @@ -207,8 +207,8 @@ public class AbsoluteLayoutTests new Rectangle (0, 0, frame.Width, frame.Height), v.Viewport ); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal (Dim.Sized (3), v.Width); Assert.Equal (Dim.Sized (4), v.Height); v.Dispose (); @@ -221,8 +221,8 @@ public class AbsoluteLayoutTests // and the size wasn't set on the initializer Assert.Equal (new Rectangle (frame.X, frame.Y, 0, 0), v.Frame); Assert.Equal (new Rectangle (0, 0, 0, 0), v.Viewport); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal (Dim.Sized (0), v.Width); Assert.Equal (Dim.Sized (0), v.Height); v.Dispose (); @@ -231,8 +231,8 @@ public class AbsoluteLayoutTests Assert.True (v.LayoutStyle == LayoutStyle.Absolute); Assert.Equal (new Rectangle (0, 0, 0, 0), v.Frame); Assert.Equal (new Rectangle (0, 0, 0, 0), v.Viewport); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (0), v.X); - Assert.Equal (Pos.At (0), v.Y); + Assert.Equal (Pos.Absolute (0), v.X); + Assert.Equal (Pos.Absolute (0), v.Y); Assert.Equal (Dim.Sized (0), v.Width); Assert.Equal (Dim.Sized (0), v.Height); v.Dispose (); @@ -241,8 +241,8 @@ public class AbsoluteLayoutTests Assert.True (v.LayoutStyle == LayoutStyle.Absolute); Assert.Equal (new Rectangle (frame.X, frame.Y, 3, 4), v.Frame); Assert.Equal (new Rectangle (0, 0, 3, 4), v.Viewport); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal (Dim.Sized (3), v.Width); Assert.Equal (Dim.Sized (4), v.Height); v.Dispose (); diff --git a/UnitTests/View/Layout/Dim.AutoTests.cs b/UnitTests/View/Layout/Dim.AutoTests.cs index afc97f014..637ab436a 100644 --- a/UnitTests/View/Layout/Dim.AutoTests.cs +++ b/UnitTests/View/Layout/Dim.AutoTests.cs @@ -1127,7 +1127,7 @@ public class DimAutoTests (ITestOutputHelper output) public void With_Subview_At_PosAt () { var view = new View (); - var subview = new View () { X = Pos.At (10), Y = Pos.At (5), Width = 20, Height = 10 }; + var subview = new View () { X = Pos.Absolute (10), Y = Pos.Absolute (5), Width = 20, Height = 10 }; view.Add (subview); var dimWidth = Dim.Auto (); diff --git a/UnitTests/View/Layout/FrameTests.cs b/UnitTests/View/Layout/FrameTests.cs index 75f17d308..fcaddbc2c 100644 --- a/UnitTests/View/Layout/FrameTests.cs +++ b/UnitTests/View/Layout/FrameTests.cs @@ -54,8 +54,8 @@ public class FrameTests (ITestOutputHelper output) new Rectangle (0, 0, newFrame.Width, newFrame.Height), v.Viewport ); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal (Dim.Sized (30), v.Width); Assert.Equal (Dim.Sized (40), v.Height); v.Dispose (); @@ -69,8 +69,8 @@ public class FrameTests (ITestOutputHelper output) new Rectangle (0, 0, newFrame.Width, newFrame.Height), v.Viewport ); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal (Dim.Sized (30), v.Width); Assert.Equal (Dim.Sized (40), v.Height); v.Dispose (); @@ -85,8 +85,8 @@ public class FrameTests (ITestOutputHelper output) new Rectangle (0, 0, newFrame.Width, newFrame.Height), v.Viewport ); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (10), v.X); - Assert.Equal (Pos.At (20), v.Y); + Assert.Equal (Pos.Absolute (10), v.X); + Assert.Equal (Pos.Absolute (20), v.Y); Assert.Equal (Dim.Sized (30), v.Width); Assert.Equal (Dim.Sized (40), v.Height); v.Dispose (); @@ -100,8 +100,8 @@ public class FrameTests (ITestOutputHelper output) new Rectangle (0, 0, newFrame.Width, newFrame.Height), v.Viewport ); // With Absolute Viewport *is* deterministic before Layout - Assert.Equal (Pos.At (10), v.X); - Assert.Equal (Pos.At (20), v.Y); + Assert.Equal (Pos.Absolute (10), v.X); + Assert.Equal (Pos.Absolute (20), v.Y); Assert.Equal (Dim.Sized (30), v.Width); Assert.Equal (Dim.Sized (40), v.Height); v.Dispose (); diff --git a/UnitTests/View/Layout/Pos.Tests.cs b/UnitTests/View/Layout/Pos.Tests.cs index b5ae383fb..63e4290d4 100644 --- a/UnitTests/View/Layout/Pos.Tests.cs +++ b/UnitTests/View/Layout/Pos.Tests.cs @@ -16,7 +16,7 @@ public class PosTests (ITestOutputHelper output) Toplevel t = new (); - var w = new Window { X = Pos.Left (t) + 2, Y = Pos.At (2) }; + var w = new Window { X = Pos.Left (t) + 2, Y = Pos.Absolute (2) }; var v = new View { X = Pos.Center (), Y = Pos.Percent (10) }; @@ -78,26 +78,26 @@ public class PosTests (ITestOutputHelper output) } [Fact] - public void PosAt_Equal () + public void PosAbsolute_Equal () { var n1 = 0; var n2 = 0; - Pos pos1 = Pos.At (n1); - Pos pos2 = Pos.At (n2); + Pos pos1 = Pos.Absolute (n1); + Pos pos2 = Pos.Absolute (n2); Assert.Equal (pos1, pos2); } [Fact] - public void PosAt_SetsValue () + public void PosAbsolute_SetsValue () { - Pos pos = Pos.At (0); + Pos pos = Pos.Absolute (0); Assert.Equal ("Absolute(0)", pos.ToString ()); - pos = Pos.At (5); + pos = Pos.Absolute (5); Assert.Equal ("Absolute(5)", pos.ToString ()); - pos = Pos.At (-1); + pos = Pos.Absolute (-1); Assert.Equal ("Absolute(-1)", pos.ToString ()); } diff --git a/UnitTests/View/Layout/ViewportTests.cs b/UnitTests/View/Layout/ViewportTests.cs index d9a379e2f..2cce68985 100644 --- a/UnitTests/View/Layout/ViewportTests.cs +++ b/UnitTests/View/Layout/ViewportTests.cs @@ -188,8 +188,8 @@ public class ViewportTests (ITestOutputHelper output) Assert.Equal (newViewport, v.Viewport); Assert.Equal (new Rectangle (1, 2, newViewport.Width, newViewport.Height), v.Frame); Assert.Equal (new Rectangle (0, 0, newViewport.Width, newViewport.Height), v.Viewport); - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal (Dim.Sized (30), v.Width); Assert.Equal (Dim.Sized (40), v.Height); @@ -198,8 +198,8 @@ public class ViewportTests (ITestOutputHelper output) Assert.Equal (newViewport, v.Viewport); Assert.Equal (new Rectangle (1, 2, newViewport.Width, newViewport.Height), v.Frame); Assert.Equal (new Rectangle (0, 0, newViewport.Width, newViewport.Height), v.Viewport); - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal (Dim.Sized (3), v.Width); Assert.Equal (Dim.Sized (4), v.Height); @@ -210,8 +210,8 @@ public class ViewportTests (ITestOutputHelper output) // Frame should not change Assert.Equal (new Rectangle (1, 2, 3, 4), v.Frame); - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal (Dim.Sized (3), v.Width); Assert.Equal (Dim.Sized (4), v.Height); @@ -223,8 +223,8 @@ public class ViewportTests (ITestOutputHelper output) // Frame grows because there's now a border Assert.Equal (new Rectangle (1, 2, 5, 6), v.Frame); Assert.Equal (new Rectangle (0, 0, newViewport.Width, newViewport.Height), v.Viewport); - Assert.Equal (Pos.At (1), v.X); - Assert.Equal (Pos.At (2), v.Y); + Assert.Equal (Pos.Absolute (1), v.X); + Assert.Equal (Pos.Absolute (2), v.Y); Assert.Equal (Dim.Sized (5), v.Width); Assert.Equal (Dim.Sized (6), v.Height); } diff --git a/docfx/docs/migratingfromv1.md b/docfx/docs/migratingfromv1.md index df58daabf..c701ceb97 100644 --- a/docfx/docs/migratingfromv1.md +++ b/docfx/docs/migratingfromv1.md @@ -83,12 +83,13 @@ In v1, `Application.Init` automatically created a toplevel view and set `Applica ## `Pos` and `Dim` types are no-longer internal nested classes -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. +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. As part of this, the static method that creates a `PosAbsolute`, `Pos.At`, was renamed to `Pos.Absoulte` for consistency ### How to Fix * Search and replace `Pos.Pos` -> `Pos`. * Search and replace `Dim.Dim` -> `Dim`. +* Search and replace `Pos.At` -> `Pos.Absolute` ## Layout Improvements