From 652f7aa1b9b33f41fa43fb27a1752b4664eb2a09 Mon Sep 17 00:00:00 2001 From: Tig Date: Wed, 17 Apr 2024 08:05:21 -0600 Subject: [PATCH] Renamed API to simplify and make more clear --- Terminal.Gui/View/Layout/PosDim.cs | 85 ++++++++++++++++++-------- Terminal.Gui/View/Layout/ViewLayout.cs | 8 +-- UnitTests/View/Layout/DimTests.cs | 12 ++-- UnitTests/View/Layout/PosTests.cs | 14 ++--- 4 files changed, 76 insertions(+), 43 deletions(-) diff --git a/Terminal.Gui/View/Layout/PosDim.cs b/Terminal.Gui/View/Layout/PosDim.cs index 2644a8f5a..f48888b02 100644 --- a/Terminal.Gui/View/Layout/PosDim.cs +++ b/Terminal.Gui/View/Layout/PosDim.cs @@ -229,6 +229,7 @@ public class Pos } var newPos = new PosCombine (true, left, right); + if (left is PosView view) { view.Target.SetNeedsLayout (); @@ -257,6 +258,7 @@ public class Pos } var newPos = new PosCombine (false, left, right); + if (left is PosView view) { view.Target.SetNeedsLayout (); @@ -315,7 +317,23 @@ public class Pos internal virtual int Anchor (int width) { return 0; } - internal virtual int GetLocation (int superviewDimension, Dim dim, int autosize, bool autoSize) { return Anchor (superviewDimension); } + /// + /// Calculates and returns the position of a object. It takes into account the dimension of the + /// superview and the dimension of the view itself. + /// + /// + /// The dimension of the superview. This could be the width for x-coordinate calculation or the + /// height for y-coordinate calculation. + /// + /// The dimension of the View. It could be the current width or height. + /// Obsolete; to be deprecated. + /// Obsolete; to be deprecated. + /// + /// The calculated position of the View. The way this position is calculated depends on the specific subclass of Pos that + /// is used. + /// + + internal virtual int Calculate (int superviewDimension, Dim dim, int autosize, bool autoSize) { return Anchor (superviewDimension); } internal class PosAbsolute (int n) : Pos { @@ -351,7 +369,7 @@ public class Pos return width - _offset; } - internal override int GetLocation (int superviewDimension, Dim dim, int autosize, bool autoSize) + internal override int Calculate (int superviewDimension, Dim dim, int autosize, bool autoSize) { int newLocation = Anchor (superviewDimension); @@ -369,9 +387,9 @@ public class Pos public override string ToString () { return "Center"; } internal override int Anchor (int width) { return width / 2; } - internal override int GetLocation (int superviewDimension, Dim dim, int autosize, bool autoSize) + internal override int Calculate (int superviewDimension, Dim dim, int autosize, bool autoSize) { - int newDimension = Math.Max (dim.GetDimension (0, superviewDimension, autosize, autoSize), 0); + int newDimension = Math.Max (dim.Calculate (0, superviewDimension, autosize, autoSize), 0); return Anchor (superviewDimension - newDimension); } @@ -397,11 +415,11 @@ public class Pos return la - ra; } - internal override int GetLocation (int superviewDimension, Dim dim, int autosize, bool autoSize) + internal override int Calculate (int superviewDimension, Dim dim, int autosize, bool autoSize) { - int newDimension = dim.GetDimension (0, superviewDimension, autosize, autoSize); - int left = _left.GetLocation (superviewDimension, dim, autosize, autoSize); - int right = _right.GetLocation (superviewDimension, dim, autosize, autoSize); + int newDimension = dim.Calculate (0, superviewDimension, autosize, autoSize); + int left = _left.Calculate (superviewDimension, dim, autosize, autoSize); + int right = _right.Calculate (superviewDimension, dim, autosize, autoSize); if (_add) { @@ -449,13 +467,13 @@ public class Pos public override string ToString () { string sideString = side switch - { - Side.X => "x", - Side.Y => "y", - Side.Right => "right", - Side.Bottom => "bottom", - _ => "unknown" - }; + { + Side.X => "x", + Side.Y => "y", + Side.Right => "right", + Side.Bottom => "bottom", + _ => "unknown" + }; if (Target == null) { @@ -666,7 +684,22 @@ public class Dim internal virtual int Anchor (int width) { return 0; } - internal virtual int GetDimension (int location, int dimension, int autosize, bool autoSize) + /// + /// Calculates and returns the dimension of a object. It takes into account the location of the + /// , its current size, and whether it should automatically adjust its size based on its content. + /// + /// + /// The starting point from where the size calculation begins. It could be the left edge for width calculation or the + /// top edge for height calculation. + /// + /// The current size of the View. It could be the current width or height. + /// Obsolete; To be deprecated. + /// Obsolete; To be deprecated. + /// + /// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that + /// is used. + /// + internal virtual int Calculate (int location, int dimension, int autosize, bool autoSize) { int newDimension = Math.Max (Anchor (dimension - location), 0); @@ -681,7 +714,7 @@ public class Dim public override string ToString () { return $"Absolute({_n})"; } internal override int Anchor (int width) { return _n; } - internal override int GetDimension (int location, int dimension, int autosize, bool autoSize) + internal override int Calculate (int location, int dimension, int autosize, bool autoSize) { // DimAbsolute.Anchor (int width) ignores width and returns n int newDimension = Math.Max (Anchor (0), 0); @@ -710,10 +743,10 @@ public class Dim return la - ra; } - internal override int GetDimension (int location, int dimension, int autosize, bool autoSize) + internal override int Calculate (int location, int dimension, int autosize, bool autoSize) { - int leftNewDim = _left.GetDimension (location, dimension, autosize, autoSize); - int rightNewDim = _right.GetDimension (location, dimension, autosize, autoSize); + int leftNewDim = _left.Calculate (location, dimension, autosize, autoSize); + int rightNewDim = _right.Calculate (location, dimension, autosize, autoSize); int newDimension; @@ -741,7 +774,7 @@ public class Dim public override string ToString () { return $"Factor({_factor},{_remaining})"; } internal override int Anchor (int width) { return (int)(width * _factor); } - internal override int GetDimension (int location, int dimension, int autosize, bool autoSize) + internal override int Calculate (int location, int dimension, int autosize, bool autoSize) { int newDimension = _remaining ? Math.Max (Anchor (dimension - location), 0) : Anchor (dimension); @@ -796,11 +829,11 @@ public class Dim } string sideString = _side switch - { - Side.Height => "Height", - Side.Width => "Width", - _ => "unknown" - }; + { + Side.Height => "Height", + Side.Width => "Width", + _ => "unknown" + }; return $"View({sideString},{Target})"; } diff --git a/Terminal.Gui/View/Layout/ViewLayout.cs b/Terminal.Gui/View/Layout/ViewLayout.cs index 82bc4f463..1afa0994b 100644 --- a/Terminal.Gui/View/Layout/ViewLayout.cs +++ b/Terminal.Gui/View/Layout/ViewLayout.cs @@ -1024,10 +1024,10 @@ public partial class View autoSize = GetAutoSize (); } - int newX = _x.GetLocation (superviewContentSize.Width, _width, autoSize.Width, AutoSize); - int newW = _width.GetDimension (newX, superviewContentSize.Width, autoSize.Width, AutoSize); - int newY = _y.GetLocation (superviewContentSize.Height, _height, autoSize.Height, AutoSize); - int newH = _height.GetDimension (newY, superviewContentSize.Height, autoSize.Height, AutoSize); + int newX = _x.Calculate (superviewContentSize.Width, _width, autoSize.Width, AutoSize); + int newW = _width.Calculate (newX, superviewContentSize.Width, autoSize.Width, AutoSize); + int newY = _y.Calculate (superviewContentSize.Height, _height, autoSize.Height, AutoSize); + int newH = _height.Calculate (newY, superviewContentSize.Height, autoSize.Height, AutoSize); Rectangle newFrame = new (newX, newY, newW, newH); diff --git a/UnitTests/View/Layout/DimTests.cs b/UnitTests/View/Layout/DimTests.cs index f6d3b737a..0083ad50a 100644 --- a/UnitTests/View/Layout/DimTests.cs +++ b/UnitTests/View/Layout/DimTests.cs @@ -28,7 +28,7 @@ public class DimTests public void DimAbsolute_GetDimension_ReturnsCorrectValue () { var dim = new DimAbsolute (10); - var result = dim.GetDimension (0, 100, 50, false); + var result = dim.Calculate (0, 100, 50, false); Assert.Equal (10, result); } @@ -38,7 +38,7 @@ public class DimTests var dim1 = new DimAbsolute (10); var dim2 = new DimAbsolute (20); var dim = dim1 + dim2; - var result = dim.GetDimension (0, 100, 50, false); + var result = dim.Calculate (0, 100, 50, false); Assert.Equal (30, result); } @@ -46,7 +46,7 @@ public class DimTests public void DimFactor_GetDimension_ReturnsCorrectValue () { var dim = new DimFactor (0.5f); - var result = dim.GetDimension (0, 100, 50, false); + var result = dim.Calculate (0, 100, 50, false); Assert.Equal (50, result); } @@ -54,7 +54,7 @@ public class DimTests public void DimFill_GetDimension_ReturnsCorrectValue () { var dim = Dim.Fill (); - var result = dim.GetDimension (0, 100, 50, false); + var result = dim.Calculate (0, 100, 50, false); Assert.Equal (100, result); } @@ -62,7 +62,7 @@ public class DimTests public void DimFunc_GetDimension_ReturnsCorrectValue () { var dim = new DimFunc (() => 10); - var result = dim.GetDimension (0, 100, 50, false); + var result = dim.Calculate (0, 100, 50, false); Assert.Equal (10, result); } @@ -71,7 +71,7 @@ public class DimTests { var view = new View { Width = 10 }; var dim = new DimView (view, Side.Width); - var result = dim.GetDimension (0, 100, 50, false); + var result = dim.Calculate (0, 100, 50, false); Assert.Equal (10, result); } diff --git a/UnitTests/View/Layout/PosTests.cs b/UnitTests/View/Layout/PosTests.cs index 96a85ff82..d83332def 100644 --- a/UnitTests/View/Layout/PosTests.cs +++ b/UnitTests/View/Layout/PosTests.cs @@ -10,7 +10,7 @@ public class PosTests (ITestOutputHelper output) public void PosAbsolute_GetLocation_ReturnsExpectedValue () { var posAbsolute = new PosAbsolute (5); - var result = posAbsolute.GetLocation (10, new DimAbsolute (2), 1, false); + var result = posAbsolute.Calculate (10, new DimAbsolute (2), 1, false); Assert.Equal (5, result); } @@ -18,7 +18,7 @@ public class PosTests (ITestOutputHelper output) public void PosAnchorEnd_GetLocation_ReturnsExpectedValue () { var posAnchorEnd = new PosAnchorEnd (5); - var result = posAnchorEnd.GetLocation (10, new DimAbsolute (2), 1, false); + var result = posAnchorEnd.Calculate (10, new DimAbsolute (2), 1, false); Assert.Equal (5, result); } @@ -26,7 +26,7 @@ public class PosTests (ITestOutputHelper output) public void PosCenter_GetLocation_ReturnsExpectedValue () { var posCenter = new PosCenter (); - var result = posCenter.GetLocation (10, new DimAbsolute (2), 1, false); + var result = posCenter.Calculate (10, new DimAbsolute (2), 1, false); Assert.Equal (4, result); } @@ -34,7 +34,7 @@ public class PosTests (ITestOutputHelper output) public void PosCombine_GetLocation_ReturnsExpectedValue () { var posCombine = new PosCombine (true, new PosAbsolute (5), new PosAbsolute (3)); - var result = posCombine.GetLocation (10, new DimAbsolute (2), 1, false); + var result = posCombine.Calculate (10, new DimAbsolute (2), 1, false); Assert.Equal (8, result); } @@ -42,7 +42,7 @@ public class PosTests (ITestOutputHelper output) public void PosFactor_GetLocation_ReturnsExpectedValue () { var posFactor = new PosFactor (0.5f); - var result = posFactor.GetLocation (10, new DimAbsolute (2), 1, false); + var result = posFactor.Calculate (10, new DimAbsolute (2), 1, false); Assert.Equal (5, result); } @@ -50,7 +50,7 @@ public class PosTests (ITestOutputHelper output) public void PosFunc_GetLocation_ReturnsExpectedValue () { var posFunc = new PosFunc (() => 5); - var result = posFunc.GetLocation (10, new DimAbsolute (2), 1, false); + var result = posFunc.Calculate (10, new DimAbsolute (2), 1, false); Assert.Equal (5, result); } @@ -58,7 +58,7 @@ public class PosTests (ITestOutputHelper output) public void PosView_GetLocation_ReturnsExpectedValue () { var posView = new PosView (new View { Frame = new Rectangle (5, 5, 10, 10) }, 0); - var result = posView.GetLocation (10, new DimAbsolute (2), 1, false); + var result = posView.Calculate (10, new DimAbsolute (2), 1, false); Assert.Equal (5, result); }