Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3469, #3473, #3482 - Dim.Auto fixes and Pos/Dim refactor to support TGD #3480

Merged
merged 100 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
cb678f3
Merge branch 'v2_develop' of tig:gui-cs/Terminal.Gui into v2_develop
tig Apr 27, 2024
65caf28
Merge branch 'v2_develop' of tig:gui-cs/Terminal.Gui into v2_develop
tig May 13, 2024
a8b9ddb
Partial fix
tig May 13, 2024
ba4139f
WIP fixing 3469
tig May 13, 2024
c8690af
Fixed AnchorEnd issue
tig May 13, 2024
4e01779
Moved AutoSizeTrue tests to more logical locations; nuked invalid tests
tig May 13, 2024
e28b12e
Moved AutoSizeFalse tests to more logical locations; nuked invalid tests
tig May 13, 2024
c80f606
Moved AutoSizeFalse tests to more logical locations; nuked invalid te…
tig May 13, 2024
922d3cb
Moved up TextTests.cs
tig May 13, 2024
ef9ee5a
Checkbox now uses DimAuto for Height
tig May 13, 2024
d27b0b6
Added // BUGBUG: Views should avoid setting Height as doing so implie…
tig May 13, 2024
7786938
Fixed ProgressBar to not set Height
tig May 13, 2024
36b9171
Removed ContentSize setter, replaced with SetContentSize
tig May 13, 2024
a5eca55
Removed nullable from ContentSize
tig May 13, 2024
82e681c
Clarified what dimauot min/max mean.
tig May 14, 2024
070d31d
Modernized RadioGroup to use Dim.Auto.
tig May 14, 2024
e6f883b
Modernized RadioGroup to use Dim.Auto.
tig May 14, 2024
05935f7
Fixed checkbox dependency on internal API
tig May 14, 2024
67f6ea8
Broke Pos & Dim out to separate .cs files
tig May 14, 2024
3e287c4
Moved PosXXX to not be nested classes
tig May 14, 2024
e28f73f
Pos / Dim code cleanup
tig May 14, 2024
d9abfc7
Updated migration doc
tig May 14, 2024
1c01556
PosAbsoulte to public.
tig May 15, 2024
90c8f83
PosAnchorEnd now public
tig May 15, 2024
8d4f706
PosCenter -> public
tig May 15, 2024
746e8e6
PosCombine -> public
tig May 15, 2024
dbd575e
PosFactor -> PosPercent (for consistency) and -> public
tig May 15, 2024
643f2a7
PosFunc -> public
tig May 15, 2024
6994bf3
PosView -> public
tig May 15, 2024
d6e641e
DimAbsolute -> public
tig May 15, 2024
c26e62f
DimAuto -> public
tig May 15, 2024
2c4fd4a
DimCombine -> public
tig May 15, 2024
3d2e8d9
DimFactor -> PosPercent -> public
tig May 15, 2024
b69d3ea
DimFill -> public
tig May 15, 2024
07ac849
DimFunc -> public
tig May 15, 2024
560a573
DimView -> public
tig May 15, 2024
65cbb06
Added API docs indicating low-level nature of pos/dom classes
tig May 15, 2024
71e1d99
Fixed all views tester unit test
tig May 15, 2024
08199f8
Fixed spelling error
tig May 15, 2024
d2e2499
Dim.Sized -> Dim.Absolute
tig May 15, 2024
a5609cc
Fixed Factor->Percent issue
tig May 15, 2024
f0865e8
TextField modernization
tig May 15, 2024
d21489c
TextView modernization
tig May 15, 2024
11da4cf
DatePicker now uses DimAuto with caveats
tig May 15, 2024
a4bb63a
LineView uses DimAuto
tig May 15, 2024
6bc5ac4
Code cleanup
tig May 15, 2024
617960d
Spinner upgrade
tig May 15, 2024
9766ee0
Removed bad Checkbox code.
tig May 15, 2024
e4e3804
UICatalog Category list use dimauto
tig May 15, 2024
87a7666
Fixed broken unit tests.
tig May 15, 2024
c738d2b
Tweaked validation logic
tig May 15, 2024
63b60e2
Upgrade Dialogs scenario
tig May 15, 2024
edeb11e
Tweaked validation logic
tig May 15, 2024
6cdb4c4
Code cleanup
tig May 15, 2024
0b969d9
Revamped Slider - Make it a showcase for how to build a View that use…
tig May 16, 2024
ace119e
Fixed unit tests after changing defining of DimAuto 'min:'
tig May 16, 2024
358acc6
Fixed bug in Adornments editor
tig May 16, 2024
c12c206
Pos API Doc improvements and code cleanup
tig May 16, 2024
80d9a97
Renamed Dim.Function->Func to align iwth C# Func type.
tig May 16, 2024
f7b75cd
Renamed Dim.Function->Func moar
tig May 16, 2024
00daccf
Renamed Pos.Function->Func to align iwth C# Func type.
tig May 16, 2024
d05544b
Dim API Doc improvements and code cleanup
tig May 16, 2024
f5abf3d
Broke out PosAbsoulte tests. Test reorg
tig May 16, 2024
9000ad9
Broke out PosAbsoulte tests. Test reorg
tig May 16, 2024
c473d80
Broke out PosView tests.
tig May 16, 2024
aa1b5ed
Broke out DimView tests.
tig May 16, 2024
ec36787
Revamped Slider - fixed multiple issues
tig May 16, 2024
fb96ea0
Commented diag code
tig May 16, 2024
02593ae
Slider code cleanup
tig May 16, 2024
327a8ff
Updated developer docs
tig May 16, 2024
c372c06
Fixed mouse scenario
tig May 16, 2024
01c30ef
WIP: Fixing MessagBox to use Dim.Auto
tig May 17, 2024
5cebe96
WIP: Fixing MessagBox to use Dim.Auto - Disabled some tests
tig May 17, 2024
1129fd4
Added =0 to enum
tig May 17, 2024
dc614f5
Addressed warning
tig May 17, 2024
334f7fa
fixed nullability
tig May 17, 2024
44e496d
Anchor -> GetAnchor
tig May 17, 2024
c035426
Pos & Dim -> abstract
tig May 17, 2024
a89db3c
Removed Equal/GetHashCode from base classes
tig May 17, 2024
40e0d87
Removed unnecesary comment
tig May 17, 2024
3d92acd
DimAuto.Equals readability
tig May 17, 2024
61ccc5b
Simplified DimAuto.GetHashCode
tig May 17, 2024
59201cf
Fixed comments
tig May 17, 2024
084c735
DimAuto -> use required on properties
tig May 17, 2024
27a347e
Use switch
tig May 17, 2024
d4d2c92
Pos/DimCombine now use AddOrSubtract enum instead of bool
tig May 17, 2024
d151267
AddOrSubtract.cs (one type per file!) <g>
tig May 17, 2024
18f2ee3
One type per file!
tig May 17, 2024
7ac767a
Layout/View.cs back to ViewLayout.cs
tig May 17, 2024
7ece3bc
Inlined code
tig May 17, 2024
dfc8c01
DimPercent -> int vs. float
tig May 17, 2024
8969127
PosPercent -> int vs. float
tig May 17, 2024
61c9b5c
DimPercentMode vs UsePosition
tig May 17, 2024
01a4de6
xml doc tweak
tig May 17, 2024
b1aa34b
backported XML doc fix from #3451
tig May 17, 2024
c7039a6
Removed debug code.
tig May 22, 2024
0a60ce3
Added [GenerateEnumExtensionMethods] to all Layout enums
tig May 22, 2024
675ef57
ViewportSettings enum to sep file
tig May 22, 2024
6df35c0
Code cleanup
tig May 22, 2024
f08f9c9
Fixed FastFlags thing
tig May 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Terminal.Gui/Text/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public bool AutoSize
}
}

private Size GetAutoSize ()
internal Size GetAutoSize ()
{
Size size = CalcRect (0, 0, Text, Direction, TabWidth).Size;
return size with
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/View/Adornment/Adornment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public override bool Contains (in Point location)
return base.OnMouseEnter (mouseEvent);
}

/// <inheritdoc/>
/// <inheritdoc/>
protected internal override bool OnMouseLeave (MouseEvent mouseEvent)
{
// Invert Normal
Expand Down
20 changes: 20 additions & 0 deletions Terminal.Gui/View/Layout/AddOrSubtract.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Terminal.Gui.Analyzers.Internal.Attributes;

namespace Terminal.Gui;

/// <summary>
/// Describes whether an operation should add or subtract values.
/// </summary>
[GenerateEnumExtensionMethods]
public enum AddOrSubtract
{
/// <summary>
/// The operation should use addition.
/// </summary>
Add = 0,

/// <summary>
/// The operation should use subtraction.
/// </summary>
Subtract = 1
}
273 changes: 273 additions & 0 deletions Terminal.Gui/View/Layout/Dim.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,273 @@
#nullable enable
using System.Diagnostics;

namespace Terminal.Gui;

/// <summary>
/// <para>
/// A Dim object describes the dimensions of a <see cref="View"/>. Dim is the type of the
/// <see cref="View.Width"/> and <see cref="View.Height"/> properties of <see cref="View"/>. Dim objects enable
/// Computed Layout (see <see cref="LayoutStyle.Computed"/>) to automatically manage the dimensions of a view.
/// </para>
/// <para>
/// Integer values are implicitly convertible to an absolute <see cref="Dim"/>. These objects are created using
/// the static methods described below. The <see cref="Dim"/> objects can be combined with the addition and
/// subtraction operators.
/// </para>
/// </summary>
/// <remarks>
/// <para>
/// <list type="table">
/// <listheader>
/// <term>Dim Object</term> <description>Description</description>
/// </listheader>
/// <item>
/// <term>
/// <see cref="Dim.Auto"/>
/// </term>
/// <description>
/// Creates a <see cref="Dim"/> object that automatically sizes the view to fit
/// the view's Text, SubViews, or ContentArea.
/// </description>
/// </item>
/// <item>
/// <term>
/// <see cref="Func"/>
/// </term>
/// <description>
/// Creates a <see cref="Dim"/> object that computes the dimension by executing the provided
/// function. The function will be called every time the dimension is needed.
/// </description>
/// </item>
/// <item>
/// <term>
/// <see cref="Dim.Percent(int, DimPercentMode)"/>
/// </term>
/// <description>
/// Creates a <see cref="Dim"/> object that is a percentage of the width or height of the
/// SuperView.
/// </description>
/// </item>
/// <item>
/// <term>
/// <see cref="Dim.Fill(int)"/>
/// </term>
/// <description>
/// Creates a <see cref="Dim"/> object that fills the dimension from the View's X position
/// to the end of the super view's width, leaving the specified number of columns for a margin.
/// </description>
/// </item>
/// <item>
/// <term>
/// <see cref="Dim.Width(View)"/>
/// </term>
/// <description>
/// Creates a <see cref="Dim"/> object that tracks the Width of the specified
/// <see cref="View"/>.
/// </description>
/// </item>
/// <item>
/// <term>
/// <see cref="Dim.Height(View)"/>
/// </term>
/// <description>
/// Creates a <see cref="Dim"/> object that tracks the Height of the specified
/// <see cref="View"/>.
/// </description>
/// </item>
/// </list>
/// </para>
/// <para></para>
/// </remarks>
public abstract class Dim
{
#region static Dim creation methods

/// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
/// <returns>The Absolute <see cref="Dim"/>.</returns>
/// <param name="size">The value to convert to the <see cref="Dim"/>.</param>
public static Dim? Absolute (int size) { return new DimAbsolute (size); }

/// <summary>
/// Creates a <see cref="Dim"/> object that automatically sizes the view to fit all the view's Content, Subviews, and/or Text.
/// </summary>
/// <remarks>
/// <para>
/// See <see cref="DimAutoStyle"/>.
/// </para>
/// </remarks>
/// <example>
/// This initializes a <see cref="View"/> with two SubViews. The view will be automatically sized to fit the two
/// SubViews.
/// <code>
/// var button = new Button () { Text = "Click Me!", X = 1, Y = 1, Width = 10, Height = 1 };
/// var textField = new TextField { Text = "Type here", X = 1, Y = 2, Width = 20, Height = 1 };
/// var view = new Window () { Title = "MyWindow", X = 0, Y = 0, Width = Dim.Auto (), Height = Dim.Auto () };
/// view.Add (button, textField);
/// </code>
/// </example>
/// <returns>The <see cref="Dim"/> object.</returns>
/// <param name="style">
/// Specifies how <see cref="Dim.Auto"/> will compute the dimension. The default is <see cref="DimAutoStyle.Auto"/>.
/// </param>
/// <param name="minimumContentDim">The minimum dimension the View's ContentSize will be constrained to.</param>
/// <param name="maximumContentDim">The maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.</param>
public static Dim? Auto (DimAutoStyle style = DimAutoStyle.Auto, Dim? minimumContentDim = null, Dim? maximumContentDim = null)
{
if (maximumContentDim is { })
{
Debug.WriteLine (@"WARNING: maximumContentDim is not fully implemented.");
}

return new DimAuto ()
{
MinimumContentDim = minimumContentDim,
MaximumContentDim = maximumContentDim,
Style = style
};
}

/// <summary>
/// Creates a <see cref="Dim"/> object that fills the dimension, leaving the specified margin.
/// </summary>
/// <returns>The Fill dimension.</returns>
/// <param name="margin">Margin to use.</param>
public static Dim? Fill (int margin = 0) { return new DimFill (margin); }

/// <summary>
/// Creates a function <see cref="Dim"/> object that computes the dimension by executing the provided function.
/// The function will be called every time the dimension is needed.
/// </summary>
/// <param name="function">The function to be executed.</param>
/// <returns>The <see cref="Dim"/> returned from the function.</returns>
public static Dim Func (Func<int> function) { return new DimFunc (function); }

/// <summary>Creates a <see cref="Dim"/> object that tracks the Height of the specified <see cref="View"/>.</summary>
/// <returns>The height <see cref="Dim"/> of the other <see cref="View"/>.</returns>
/// <param name="view">The view that will be tracked.</param>
public static Dim Height (View view) { return new DimView (view, Dimension.Height); }

/// <summary>Creates a percentage <see cref="Dim"/> object that is a percentage of the width or height of the SuperView.</summary>
/// <returns>The percent <see cref="Dim"/> object.</returns>
/// <param name="percent">A value between 0 and 100 representing the percentage.</param>
/// <param name="mode"></param>
/// <example>
/// This initializes a <see cref="TextField"/> that will be centered horizontally, is 50% of the way down, is 30% the
/// height,
/// and is 80% the width of the SuperView.
/// <code>
/// var textView = new TextField {
/// X = Pos.Center (),
/// Y = Pos.Percent (50),
/// Width = Dim.Percent (80),
/// Height = Dim.Percent (30),
/// };
/// </code>
/// </example>
public static Dim? Percent (int percent, DimPercentMode mode = DimPercentMode.ContentSize)
{
if (percent is < 0 /*or > 100*/)
{
throw new ArgumentException ("Percent value must be positive.");
}

return new DimPercent (percent, mode);
}

/// <summary>Creates a <see cref="Dim"/> object that tracks the Width of the specified <see cref="View"/>.</summary>
/// <returns>The width <see cref="Dim"/> of the other <see cref="View"/>.</returns>
/// <param name="view">The view that will be tracked.</param>
public static Dim Width (View view) { return new DimView (view, Dimension.Width); }

#endregion static Dim creation methods

#region virtual methods

/// <summary>
/// Gets a dimension that is anchored to a certain point in the layout.
/// This method is typically used internally by the layout system to determine the size of a View.
/// </summary>
/// <param name="size">The width of the area where the View is being sized (Superview.ContentSize).</param>
/// <returns>
/// An integer representing the calculated dimension. The way this dimension is calculated depends on the specific
/// 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.
/// </returns>
internal virtual int GetAnchor (int size) { return 0; }

/// <summary>
/// Calculates and returns the dimension of a <see cref="View"/> object. It takes into account the location of the
/// <see cref="View"/>, it's SuperView's ContentSize, and whether it should automatically adjust its size based on its
/// content.
/// </summary>
/// <param name="location">
/// 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.
/// </param>
/// <param name="superviewContentSize">The size of the SuperView's content. It could be width or height.</param>
/// <param name="us">The View that holds this Pos object.</param>
/// <param name="dimension">Width or Height</param>
/// <returns>
/// The calculated size of the View. The way this size is calculated depends on the specific subclass of Dim that
/// is used.
/// </returns>
internal virtual int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
{
return Math.Max (GetAnchor (superviewContentSize - location), 0);
}

/// <summary>
/// Diagnostics API to determine if this Dim object references other views.
/// </summary>
/// <returns></returns>
internal virtual bool ReferencesOtherViews () { return false; }

#endregion virtual methods

#region operators

/// <summary>Adds a <see cref="Dim"/> to a <see cref="Dim"/>, yielding a new <see cref="Dim"/>.</summary>
/// <param name="left">The first <see cref="Dim"/> to add.</param>
/// <param name="right">The second <see cref="Dim"/> to add.</param>
/// <returns>The <see cref="Dim"/> that is the sum of the values of <c>left</c> and <c>right</c>.</returns>
public static Dim operator + (Dim? left, Dim? right)
{
if (left is DimAbsolute && right is DimAbsolute)
{
return new DimAbsolute (left.GetAnchor (0) + right.GetAnchor (0));
}

var newDim = new DimCombine (AddOrSubtract.Add, left, right);
(left as DimView)?.Target.SetNeedsLayout ();

return newDim;
}

/// <summary>Creates an Absolute <see cref="Dim"/> from the specified integer value.</summary>
/// <returns>The Absolute <see cref="Dim"/>.</returns>
/// <param name="n">The value to convert to the pos.</param>
public static implicit operator Dim (int n) { return new DimAbsolute (n); }
tig marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Subtracts a <see cref="Dim"/> from a <see cref="Dim"/>, yielding a new
/// <see cref="Dim"/>.
/// </summary>
/// <param name="left">The <see cref="Dim"/> to subtract from (the minuend).</param>
/// <param name="right">The <see cref="Dim"/> to subtract (the subtrahend).</param>
/// <returns>The <see cref="Dim"/> that is the <c>left</c> minus <c>right</c>.</returns>
public static Dim operator - (Dim? left, Dim? right)
{
if (left is DimAbsolute && right is DimAbsolute)
{
return new DimAbsolute (left.GetAnchor (0) - right.GetAnchor (0));
}

var newDim = new DimCombine (AddOrSubtract.Subtract, left, right);
(left as DimView)?.Target.SetNeedsLayout ();

return newDim;
}

#endregion operators

}
36 changes: 36 additions & 0 deletions Terminal.Gui/View/Layout/DimAbsolute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#nullable enable
namespace Terminal.Gui;

/// <summary>
/// Represents a dimension that is a fixed size.
/// </summary>
/// <remarks>
/// <para>
/// This is a low-level API that is typically used internally by the layout system. Use the various static
/// methods on the <see cref="Dim"/> class to create <see cref="Dim"/> objects instead.
/// </para>
/// </remarks>
/// <param name="size"></param>
public class DimAbsolute (int size) : Dim
{
/// <inheritdoc/>
public override bool Equals (object? other) { return other is DimAbsolute abs && abs.Size == Size; }

/// <inheritdoc/>
public override int GetHashCode () { return Size.GetHashCode (); }

/// <summary>
/// Gets the size of the dimension.
/// </summary>
public int Size { get; } = size;

/// <inheritdoc/>
public override string ToString () { return $"Absolute({Size})"; }

internal override int GetAnchor (int size) { return Size; }

internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
{
return Math.Max (GetAnchor (0), 0);
}
}
Loading
Loading