diff --git a/Terminal.Gui/View/Layout/Dim.cs b/Terminal.Gui/View/Layout/Dim.cs index 525504636..33af8f7fa 100644 --- a/Terminal.Gui/View/Layout/Dim.cs +++ b/Terminal.Gui/View/Layout/Dim.cs @@ -291,7 +291,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. /// - internal virtual int Anchor (int width) { return 0; } + internal virtual int Anchor (int size) { return 0; } /// /// Calculates and returns the dimension of a object. It takes into account the location of the @@ -353,11 +353,11 @@ public class DimAbsolute (int size) : Dim /// public override string ToString () { return $"Absolute({Size})"; } - internal override int Anchor (int width) { return Size; } + internal override int Anchor (int size) { return Size; } internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension) { - // DimAbsolute.Anchor (int width) ignores width and returns n + // DimAbsolute.Anchor (int size) ignores width and returns n return Math.Max (Anchor (0), 0); } } @@ -533,10 +533,10 @@ public class DimCombine (bool add, Dim left, Dim right) : Dim /// public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; } - internal override int Anchor (int width) + internal override int Anchor (int size) { - int la = Left.Anchor (width); - int ra = Right.Anchor (width); + int la = Left.Anchor (size); + int ra = Right.Anchor (size); if (Add) { @@ -621,7 +621,7 @@ public class DimPercent (float percent, bool usePosition = false) : Dim /// public override string ToString () { return $"Percent({Percent},{UsePosition})"; } - internal override int Anchor (int width) { return (int)(width * Percent); } + internal override int Anchor (int size) { return (int)(size * Percent); } internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension) { @@ -653,7 +653,7 @@ public class DimFill (int margin) : Dim /// public override string ToString () { return $"Fill({Margin})"; } - internal override int Anchor (int width) { return width - Margin; } + internal override int Anchor (int size) { return size - Margin; } } /// @@ -680,7 +680,7 @@ public class DimFunc (Func dim) : Dim /// public override string ToString () { return $"DimFunc({Func ()})"; } - internal override int Anchor (int width) { return Func (); } + internal override int Anchor (int size) { return Func (); } } /// @@ -737,7 +737,7 @@ public class DimView : Dim return $"View({dimString},{Target})"; } - internal override int Anchor (int width) + internal override int Anchor (int size) { return Dimension switch { diff --git a/Terminal.Gui/View/Layout/Pos.cs b/Terminal.Gui/View/Layout/Pos.cs index c841b9191..a457b0c2c 100644 --- a/Terminal.Gui/View/Layout/Pos.cs +++ b/Terminal.Gui/View/Layout/Pos.cs @@ -150,9 +150,16 @@ public enum Side /// public class Pos { + #region static Pos creation methods + + /// 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 Absolute (int position) { return new PosAbsolute (position); } + /// /// Creates a object that is anchored to the end (right side or - /// bottom) of the SuperView, minus the respective dimension of the View. This is equivalent to using + /// bottom) of the SuperView's Content Area, minus the respective size of the View. This is equivalent to using /// , /// with an offset equivalent to the View's respective dimension. /// @@ -167,7 +174,8 @@ public class Pos public static Pos AnchorEnd () { return new PosAnchorEnd (); } /// - /// Creates a object that is anchored to the end (right side or bottom) of the SuperView, + /// Creates a object that is anchored to the end (right side or bottom) of the SuperView's Content + /// Area, /// useful to flush the layout from the right or bottom. See also , which uses the view /// dimension to ensure the view is fully visible. /// @@ -190,11 +198,6 @@ public class Pos return new PosAnchorEnd (offset); } - /// 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 Absolute (int position) { return new PosAbsolute (position); } - /// Creates a object that can be used to center the . /// The center Pos. /// @@ -211,14 +214,6 @@ public class Pos /// public static Pos Center () { return new PosCenter (); } - /// Determines whether the specified object is equal to the current object. - /// The object to compare with the current object. - /// - /// if the specified object is equal to the current object; otherwise, - /// . - /// - public override bool Equals (object other) { return other is Pos abs && abs == this; } - /// /// Creates a object that computes the position by executing the provided function. The function /// will be called every time the position is needed. @@ -227,60 +222,6 @@ public class Pos /// The returned from the function. public static Pos Function (Func function) { return new PosFunc (function); } - /// Serves as the default hash function. - /// A hash code for the current object. - public override int GetHashCode () { return Anchor (0).GetHashCode (); } - - /// Adds a to a , yielding a new . - /// The first to add. - /// The second to add. - /// The that is the sum of the values of left and right. - public static Pos operator + (Pos left, Pos right) - { - if (left is PosAbsolute && right is PosAbsolute) - { - return new PosAbsolute (left.Anchor (0) + right.Anchor (0)); - } - - var newPos = new PosCombine (true, left, right); - - if (left is PosView view) - { - view.Target.SetNeedsLayout (); - } - - return newPos; - } - - /// Creates an Absolute from the specified integer value. - /// The Absolute . - /// The value to convert to the . - public static implicit operator Pos (int n) { return new PosAbsolute (n); } - - /// - /// Subtracts a from a , yielding a new - /// . - /// - /// The to subtract from (the minuend). - /// The to subtract (the subtrahend). - /// The that is the left minus right. - public static Pos operator - (Pos left, Pos right) - { - if (left is PosAbsolute && right is PosAbsolute) - { - return new PosAbsolute (left.Anchor (0) - right.Anchor (0)); - } - - var newPos = new PosCombine (false, left, right); - - if (left is PosView view) - { - view.Target.SetNeedsLayout (); - } - - return newPos; - } - /// Creates a percentage object /// The percent object. /// A value between 0 and 100 representing the percentage. @@ -342,22 +283,32 @@ public class Pos /// The that will be tracked. public static Pos Right (View view) { return new PosView (view, Side.Right); } + #endregion static Pos creation methods + + #region virtual methods + /// - /// Gets a position that is anchored to a certain point in the layout. This method is typically used + /// Calculates and returns the starting point of an element based on the size of the parent element (typically + /// Superview.ContentSize). + /// This method is meant to be overridden by subclasses to provide different ways of calculating the starting point. + /// This method is used /// internally by the layout system to determine where a View should be positioned. /// - /// The width of the area where the View is being positioned (Superview.ContentSize). + /// The size of the parent element (typically Superview.ContentSize). /// /// An integer representing the calculated position. The way this position is calculated depends on the specific /// 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. /// - internal virtual int Anchor (int width) { return 0; } + internal virtual int Anchor (int size) { return 0; } /// - /// Calculates and returns the position of a object. It takes into account the dimension of the + /// Calculates and returns the final 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. @@ -377,35 +328,102 @@ public class Pos /// /// internal virtual bool ReferencesOtherViews () { return false; } + + #endregion virtual methods + + #region operators + + /// Adds a to a , yielding a new . + /// The first to add. + /// The second to add. + /// The that is the sum of the values of left and right. + public static Pos operator + (Pos left, Pos right) + { + if (left is PosAbsolute && right is PosAbsolute) + { + return new PosAbsolute (left.Anchor (0) + right.Anchor (0)); + } + + var newPos = new PosCombine (true, left, right); + + if (left is PosView view) + { + view.Target.SetNeedsLayout (); + } + + return newPos; + } + + /// Creates an Absolute from the specified integer value. + /// The Absolute . + /// The value to convert to the . + public static implicit operator Pos (int n) { return new PosAbsolute (n); } + + /// + /// Subtracts a from a , yielding a new + /// . + /// + /// The to subtract from (the minuend). + /// The to subtract (the subtrahend). + /// The that is the left minus right. + public static Pos operator - (Pos left, Pos right) + { + if (left is PosAbsolute && right is PosAbsolute) + { + return new PosAbsolute (left.Anchor (0) - right.Anchor (0)); + } + + var newPos = new PosCombine (false, left, right); + + if (left is PosView view) + { + view.Target.SetNeedsLayout (); + } + + return newPos; + } + + #endregion operators + + #region overrides + + /// + public override bool Equals (object other) { return other is Pos abs && abs == this; } + + /// Serves as the default hash function. + /// A hash code for the current object. + public override int GetHashCode () { return Anchor (0).GetHashCode (); } + + #endregion overrides } /// -/// Represents an absolute position in the layout. This is used to specify a fixed position in the layout. +/// Represents an absolute position in the layout. This is used to specify a fixed position in the layout. /// /// /// -/// This is a low-level API that is typically used internally by the layout system. Use the various static -/// methods on the class to create objects instead. +/// This is a low-level API that is typically used internally by the layout system. Use the various static +/// methods on the class to create objects instead. /// /// /// public class PosAbsolute (int position) : Pos { /// - /// The position of the in the layout. + /// 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 override int Anchor (int size) { return Position; } } /// @@ -413,14 +431,14 @@ public class PosAbsolute (int position) : Pos /// /// /// -/// This is a low-level API that is typically used internally by the layout system. Use the various static -/// methods on the class to create objects instead. +/// This is a low-level API that is typically used internally by the layout system. Use the various static +/// methods on the class to create objects instead. /// /// public class PosAnchorEnd : Pos { /// - /// Gets the offset of the position from the right/bottom. + /// Gets the offset of the position from the right/bottom. /// public int Offset { get; } @@ -437,10 +455,10 @@ public class PosAnchorEnd : Pos /// 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 (); } /// @@ -448,17 +466,17 @@ public class PosAnchorEnd : Pos /// public bool UseDimForOffset { get; } - /// + /// public override string ToString () { return UseDimForOffset ? "AnchorEnd()" : $"AnchorEnd({Offset})"; } - internal override int Anchor (int width) + internal override int Anchor (int size) { if (UseDimForOffset) { - return width; + return size; } - return width - Offset; + return size - Offset; } internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) @@ -479,10 +497,10 @@ public class PosAnchorEnd : Pos /// public class PosCenter : Pos { - /// + /// public override string ToString () { return "Center"; } - internal override int Anchor (int width) { return width / 2; } + internal override int Anchor (int size) { return size / 2; } internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { @@ -493,41 +511,45 @@ public class PosCenter : Pos } /// -/// Represents a position that is a combination of two other positions. +/// Represents a position that is a combination of two other positions. /// /// /// -/// This is a low-level API that is typically used internally by the layout system. Use the various static -/// methods on the class to create objects instead. +/// This is a low-level API that is typically used internally by the layout system. Use the various static +/// methods on the class to create objects instead. /// /// -/// Indicates whether the two positions are added or subtracted. If , the positions are added, otherwise they are subtracted. +/// +/// Indicates whether the two positions are added or subtracted. If , the positions are added, +/// otherwise they are subtracted. +/// /// The left position. /// The right position. public class PosCombine (bool add, Pos left, Pos right) : Pos { /// - /// Gets whether the two positions are added or subtracted. If , the positions are added, otherwise they are subtracted. + /// Gets whether the two positions are added or subtracted. If , the positions are added, + /// otherwise they are subtracted. /// public bool Add { get; } = add; /// - /// Gets the left position. + /// Gets the left position. /// public new Pos Left { get; } = left; /// - /// Gets the right position. + /// Gets the right position. /// public new Pos Right { get; } = right; - /// + /// public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; } - internal override int Anchor (int width) + internal override int Anchor (int size) { - int la = Left.Anchor (width); - int ra = Right.Anchor (width); + int la = Left.Anchor (size); + int ra = Right.Anchor (size); if (Add) { @@ -539,7 +561,6 @@ public class PosCombine (bool add, Pos left, Pos right) : Pos internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension) { - int newDimension = dim.Calculate (0, superviewDimension, us, dimension); int left = Left.Calculate (superviewDimension, dim, us, dimension); int right = Right.Calculate (superviewDimension, dim, us, dimension); @@ -572,8 +593,8 @@ public class PosCombine (bool add, Pos left, Pos right) : Pos /// /// /// -/// This is a low-level API that is typically used internally by the layout system. Use the various static -/// methods on the class to create objects instead. +/// This is a low-level API that is typically used internally by the layout system. Use the various static +/// methods on the class to create objects instead. /// /// /// @@ -584,45 +605,45 @@ public class PosPercent (float percent) : Pos /// public new float Percent { get; } = percent; - /// + /// public override bool Equals (object other) { return other is PosPercent f && f.Percent == Percent; } - /// + /// public override int GetHashCode () { return Percent.GetHashCode (); } - /// + /// public override string ToString () { return $"Percent({Percent})"; } - internal override int Anchor (int width) { return (int)(width * Percent); } + internal override int Anchor (int size) { return (int)(size * Percent); } } /// -/// Represents a position that is computed by executing a function that returns an integer position. +/// Represents a position that is computed by executing a function that returns an integer position. /// /// /// -/// This is a low-level API that is typically used internally by the layout system. Use the various static -/// methods on the class to create objects instead. +/// This is a low-level API that is typically used internally by the layout system. Use the various static +/// methods on the class to create objects instead. /// /// /// The position. public class PosFunc (Func pos) : Pos { /// - /// Gets the function that computes the position. + /// Gets the function that computes the position. /// public Func Func { get; } = pos; - /// + /// public override bool Equals (object other) { return other is PosFunc f && f.Func () == Func (); } - /// + /// public override int GetHashCode () { return Func.GetHashCode (); } - /// + /// public override string ToString () { return $"PosFunc({Func ()})"; } - internal override int Anchor (int width) { return Func (); } + internal override int Anchor (int size) { return Func (); } } /// @@ -630,8 +651,8 @@ public class PosFunc (Func pos) : Pos /// /// /// -/// This is a low-level API that is typically used internally by the layout system. Use the various static -/// methods on the class to create objects instead. +/// This is a low-level API that is typically used internally by the layout system. Use the various static +/// methods on the class to create objects instead. /// /// /// The View the position is anchored to. @@ -639,32 +660,32 @@ public class PosFunc (Func pos) : Pos public class PosView (View view, Side side) : Pos { /// - /// Gets the View the position is anchored to. + /// Gets the View the position is anchored to. /// public View Target { get; } = view; /// - /// Gets the side of the View the position is anchored to. + /// Gets the side of the View the position is anchored to. /// public Side Side { get; } = side; - /// + /// public override bool Equals (object other) { return other is PosView abs && abs.Target == Target; } - /// + /// public override int GetHashCode () { return Target.GetHashCode (); } - /// + /// public override string ToString () { string sideString = Side switch - { - Side.Left => "left", - Side.Top => "top", - Side.Right => "right", - Side.Bottom => "bottom", - _ => "unknown" - }; + { + Side.Left => "left", + Side.Top => "top", + Side.Right => "right", + Side.Bottom => "bottom", + _ => "unknown" + }; if (Target == null) { @@ -674,21 +695,17 @@ public class PosView (View view, Side side) : Pos return $"View(side={sideString},target={Target})"; } - internal override int Anchor (int width) + internal override int Anchor (int size) { return Side switch - { - Side.Left => Target.Frame.X, - Side.Top => Target.Frame.Y, - Side.Right => Target.Frame.Right, - Side.Bottom => Target.Frame.Bottom, - _ => 0 - }; + { + Side.Left => Target.Frame.X, + Side.Top => Target.Frame.Y, + Side.Right => Target.Frame.Right, + Side.Bottom => Target.Frame.Bottom, + _ => 0 + }; } - /// - /// Diagnostics API to determine if this Pos object references other views. - /// - /// internal override bool ReferencesOtherViews () { return true; } }