Przeglądaj źródła

fixed nullability

Tig 1 rok temu
rodzic
commit
334f7fac86

+ 27 - 27
Terminal.Gui/View/Layout/Dim.cs

@@ -1,5 +1,5 @@
+#nullable enable
 using System.Diagnostics;
-using System.Drawing;
 
 namespace Terminal.Gui;
 
@@ -149,7 +149,7 @@ public class Dim
     /// <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); }
+    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.
@@ -175,7 +175,7 @@ public class Dim
     /// </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)
+    public static Dim? Auto (DimAutoStyle style = DimAutoStyle.Auto, Dim? minimumContentDim = null, Dim? maximumContentDim = null)
     {
         //if (maximumContentDim != null)
         //{
@@ -190,7 +190,7 @@ public class Dim
     /// </summary>
     /// <returns>The Fill dimension.</returns>
     /// <param name="margin">Margin to use.</param>
-    public static Dim Fill (int margin = 0) { return new DimFill (margin); }
+    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.
@@ -226,7 +226,7 @@ public class Dim
     ///  };
     ///  </code>
     /// </example>
-    public static Dim Percent (float percent, bool usePosition = false)
+    public static Dim? Percent (float percent, bool usePosition = false)
     {
         if (percent is < 0 or > 100)
         {
@@ -249,7 +249,7 @@ public class Dim
     ///     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="width">The width of the area where the View is being sized (Superview.ContentSize).</param>
+    /// <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
@@ -286,7 +286,7 @@ public class Dim
     /// <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)
+    public static Dim operator + (Dim? left, Dim? right)
     {
         if (left is DimAbsolute && right is DimAbsolute)
         {
@@ -311,7 +311,7 @@ public class Dim
     /// <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)
+    public static Dim operator - (Dim? left, Dim? right)
     {
         if (left is DimAbsolute && right is DimAbsolute)
         {
@@ -335,7 +335,7 @@ public class Dim
     internal virtual bool ReferencesOtherViews () { return false; }
 
     /// <inheritdoc/>
-    public override bool Equals (object other) { return other is Dim abs && abs == this; }
+    public override bool Equals (object? other) { return other is Dim abs && abs == this; }
 
     /// <inheritdoc/>
     public override int GetHashCode () { return Anchor (0).GetHashCode (); }
@@ -356,7 +356,7 @@ public class Dim
 public class DimAbsolute (int size) : Dim
 {
     /// <inheritdoc/>
-    public override bool Equals (object other) { return other is DimAbsolute abs && abs.Size == Size; }
+    public override bool Equals (object? other) { return other is DimAbsolute abs && abs.Size == Size; }
 
     /// <inheritdoc/>
     public override int GetHashCode () { return Size.GetHashCode (); }
@@ -395,10 +395,10 @@ public class DimAbsolute (int size) : Dim
 /// </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 class DimAuto (DimAutoStyle style, Dim minimumContentDim, Dim maximumContentDim) : Dim
+public class DimAuto (DimAutoStyle style, Dim? minimumContentDim, Dim? maximumContentDim) : Dim
 {
     /// <inheritdoc/>
-    public override bool Equals (object other)
+    public override bool Equals (object? other)
     {
         return other is DimAuto auto && auto.MinimumContentDim == MinimumContentDim && auto.MaximumContentDim == MaximumContentDim && auto.Style == Style;
     }
@@ -409,12 +409,12 @@ public class DimAuto (DimAutoStyle style, Dim minimumContentDim, Dim maximumCont
     /// <summary>
     ///     Gets the maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.
     /// </summary>
-    public Dim MaximumContentDim { get; } = maximumContentDim;
+    public Dim? MaximumContentDim { get; } = maximumContentDim;
 
     /// <summary>
     ///     Gets the minimum dimension the View's ContentSize will be constrained to.
     /// </summary>
-    public Dim MinimumContentDim { get; } = minimumContentDim;
+    public Dim? MinimumContentDim { get; } = minimumContentDim;
 
     /// <summary>
     ///     Gets the style of the DimAuto.
@@ -571,7 +571,7 @@ public class DimAuto (DimAutoStyle style, Dim minimumContentDim, Dim maximumCont
 /// </remarks>
 /// <param name="left">The left dimension.</param>
 /// <param name="right">The right dimension.</param>
-public class DimCombine (bool add, Dim left, Dim right) : Dim
+public class DimCombine (bool add, Dim? left, Dim? right) : Dim
 {
     /// <summary>
     ///     Gets whether the two dimensions are added or subtracted.
@@ -581,20 +581,20 @@ public class DimCombine (bool add, Dim left, Dim right) : Dim
     /// <summary>
     ///     Gets the left dimension.
     /// </summary>
-    public Dim Left { get; } = left;
+    public Dim? Left { get; } = left;
 
     /// <summary>
     ///     Gets the right dimension.
     /// </summary>
-    public Dim Right { get; } = right;
+    public Dim? Right { get; } = right;
 
     /// <inheritdoc/>
     public override string ToString () { return $"Combine({Left}{(Add ? '+' : '-')}{Right})"; }
 
     internal override int Anchor (int size)
     {
-        int la = Left.Anchor (size);
-        int ra = Right.Anchor (size);
+        int la = Left!.Anchor (size);
+        int ra = Right!.Anchor (size);
 
         if (Add)
         {
@@ -606,8 +606,8 @@ public class DimCombine (bool add, Dim left, Dim right) : Dim
 
     internal override int Calculate (int location, int superviewContentSize, View us, Dimension dimension)
     {
-        int leftNewDim = Left.Calculate (location, superviewContentSize, us, dimension);
-        int rightNewDim = Right.Calculate (location, superviewContentSize, us, dimension);
+        int leftNewDim = Left!.Calculate (location, superviewContentSize, us, dimension);
+        int rightNewDim = Right!.Calculate (location, superviewContentSize, us, dimension);
 
         int newDimension;
 
@@ -629,12 +629,12 @@ public class DimCombine (bool add, Dim left, Dim right) : Dim
     /// <returns></returns>
     internal override bool ReferencesOtherViews ()
     {
-        if (Left.ReferencesOtherViews ())
+        if (Left!.ReferencesOtherViews ())
         {
             return true;
         }
 
-        if (Right.ReferencesOtherViews ())
+        if (Right!.ReferencesOtherViews ())
         {
             return true;
         }
@@ -659,7 +659,7 @@ public class DimCombine (bool add, Dim left, Dim right) : Dim
 public class DimPercent (float percent, bool usePosition = false) : Dim
 {
     /// <inheritdoc/>
-    public override bool Equals (object other) { return other is DimPercent f && f.Percent == Percent && f.UsePosition == UsePosition; }
+    public override bool Equals (object? other) { return other is DimPercent f && f.Percent == Percent && f.UsePosition == UsePosition; }
 
     /// <inheritdoc/>
     public override int GetHashCode () { return Percent.GetHashCode (); }
@@ -698,7 +698,7 @@ public class DimPercent (float percent, bool usePosition = false) : Dim
 public class DimFill (int margin) : Dim
 {
     /// <inheritdoc/>
-    public override bool Equals (object other) { return other is DimFill fill && fill.Margin == Margin; }
+    public override bool Equals (object? other) { return other is DimFill fill && fill.Margin == Margin; }
 
     /// <inheritdoc/>
     public override int GetHashCode () { return Margin.GetHashCode (); }
@@ -725,7 +725,7 @@ public class DimFill (int margin) : Dim
 public class DimFunc (Func<int> dim) : Dim
 {
     /// <inheritdoc/>
-    public override bool Equals (object other) { return other is DimFunc f && f.Func () == Func (); }
+    public override bool Equals (object? other) { return other is DimFunc f && f.Func () == Func (); }
 
     /// <summary>
     ///     Gets the function that computes the dimension.
@@ -767,7 +767,7 @@ public class DimView : Dim
     public Dimension Dimension { get; }
 
     /// <inheritdoc/>
-    public override bool Equals (object other) { return other is DimView abs && abs.Target == Target && abs.Dimension == Dimension; }
+    public override bool Equals (object? other) { return other is DimView abs && abs.Target == Target && abs.Dimension == Dimension; }
 
     /// <inheritdoc/>
     public override int GetHashCode () { return Target.GetHashCode (); }

+ 25 - 27
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -1,3 +1,4 @@
+#nullable enable
 using System.Diagnostics;
 using Microsoft.CodeAnalysis;
 
@@ -240,7 +241,7 @@ public partial class View
         }
     }
 
-    private Dim _height = Dim.Absolute (0);
+    private Dim? _height = Dim.Absolute (0);
 
     /// <summary>Gets or sets the height dimension of the view.</summary>
     /// <value>The <see cref="Dim"/> object representing the height of the view (the number of rows).</value>
@@ -264,7 +265,7 @@ public partial class View
     ///     </para>
     ///     <para>The default value is <c>Dim.Sized (0)</c>.</para>
     /// </remarks>
-    public Dim Height
+    public Dim? Height
     {
         get => VerifyIsInitialized (_height, nameof (Height));
         set
@@ -286,7 +287,7 @@ public partial class View
         }
     }
 
-    private Dim _width = Dim.Absolute (0);
+    private Dim? _width = Dim.Absolute (0);
 
     /// <summary>Gets or sets the width dimension of the view.</summary>
     /// <value>The <see cref="Dim"/> object representing the width of the view (the number of columns).</value>
@@ -310,7 +311,7 @@ public partial class View
     ///     </para>
     ///     <para>The default value is <c>Dim.Sized (0)</c>.</para>
     /// </remarks>
-    public Dim Width
+    public Dim? Width
     {
         get => VerifyIsInitialized (_width, nameof (Width));
         set
@@ -394,7 +395,6 @@ public partial class View
     /// <returns><see langword="true"/> if the specified SuperView-relative coordinates are within the View.</returns>
     public virtual bool Contains (in Point location) { return Frame.Contains (location); }
 
-#nullable enable
     /// <summary>Finds the first Subview of <paramref name="start"/> that is visible at the provided location.</summary>
     /// <remarks>
     ///     <para>
@@ -469,8 +469,6 @@ public partial class View
         return null;
     }
 
-#nullable restore
-
     /// <summary>
     ///     Gets a new location of the <see cref="View"/> that is within the Viewport of the <paramref name="viewToMove"/>'s
     ///     <see cref="View.SuperView"/> (e.g. for dragging a Window). The `out` parameters are the new X and Y coordinates.
@@ -501,7 +499,7 @@ public partial class View
     {
         int maxDimension;
         View superView;
-        statusBar = null;
+        statusBar = null!;
 
         if (viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top)
         {
@@ -511,11 +509,11 @@ public partial class View
         else
         {
             // Use the SuperView's Viewport, not Frame
-            maxDimension = viewToMove.SuperView.Viewport.Width;
+            maxDimension = viewToMove!.SuperView.Viewport.Width;
             superView = viewToMove.SuperView;
         }
 
-        if (superView?.Margin is { } && superView == viewToMove.SuperView)
+        if (superView?.Margin is { } && superView == viewToMove!.SuperView)
         {
             maxDimension -= superView.GetAdornmentsThickness ().Left + superView.GetAdornmentsThickness ().Right;
         }
@@ -545,7 +543,7 @@ public partial class View
         }
         else
         {
-            View t = viewToMove.SuperView;
+            View t = viewToMove!.SuperView;
 
             while (t is { } and not Toplevel)
             {
@@ -572,21 +570,21 @@ public partial class View
         if (viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top)
         {
             statusVisible = Application.Top?.StatusBar?.Visible == true;
-            statusBar = Application.Top?.StatusBar;
+            statusBar = Application.Top?.StatusBar!;
         }
         else
         {
-            View t = viewToMove.SuperView;
+            View t = viewToMove!.SuperView;
 
             while (t is { } and not Toplevel)
             {
                 t = t.SuperView;
             }
 
-            if (t is Toplevel toplevel)
+            if (t is Toplevel topLevel)
             {
-                statusVisible = toplevel.StatusBar?.Visible == true;
-                statusBar = toplevel.StatusBar;
+                statusVisible = topLevel.StatusBar?.Visible == true;
+                statusBar = topLevel.StatusBar!;
             }
         }
 
@@ -596,7 +594,7 @@ public partial class View
         }
         else
         {
-            maxDimension = statusVisible ? viewToMove.SuperView.Viewport.Height - 1 : viewToMove.SuperView.Viewport.Height;
+            maxDimension = statusVisible ? viewToMove!.SuperView.Viewport.Height - 1 : viewToMove!.SuperView.Viewport.Height;
         }
 
         if (superView?.Margin is { } && superView == viewToMove?.SuperView)
@@ -620,7 +618,7 @@ public partial class View
 
         //System.Diagnostics.Debug.WriteLine ($"ny:{ny}, rHeight:{rHeight}");
 
-        return superView;
+        return superView!;
     }
 
     /// <summary>Fired after the View's <see cref="LayoutSubviews"/> method has completed.</summary>
@@ -868,7 +866,7 @@ public partial class View
 
     internal void CollectAll (View from, ref HashSet<View> nNodes, ref HashSet<(View, View)> nEdges)
     {
-        foreach (View v in from.InternalSubviews)
+        foreach (View? v in from.InternalSubviews)
         {
             nNodes.Add (v);
 
@@ -884,7 +882,7 @@ public partial class View
         }
     }
 
-    internal void CollectDim (Dim dim, View from, ref HashSet<View> nNodes, ref HashSet<(View, View)> nEdges)
+    internal void CollectDim (Dim? dim, View from, ref HashSet<View> nNodes, ref HashSet<(View, View)> nEdges)
     {
         switch (dim)
         {
@@ -1042,7 +1040,7 @@ public partial class View
     }
 
     // Diagnostics to highlight when Width or Height is read before the view has been initialized
-    private Dim VerifyIsInitialized (Dim dim, string member)
+    private Dim? VerifyIsInitialized (Dim? dim, string member)
     {
         //#if DEBUG
         //        if (dim.ReferencesOtherViews () && !IsInitialized)
@@ -1100,9 +1098,9 @@ public partial class View
 
         return;
 
-        void ThrowInvalid (View view, object checkPosDim, string name)
+        void ThrowInvalid (View view, object? checkPosDim, string name)
         {
-            object bad = null;
+            object? bad = null;
 
             switch (checkPosDim)
             {
@@ -1116,8 +1114,8 @@ public partial class View
 
                 case Pos pos and PosCombine:
                     // Recursively check for not Absolute or not View
-                    ThrowInvalid (view, (pos as PosCombine).Left, name);
-                    ThrowInvalid (view, (pos as PosCombine).Right, name);
+                    ThrowInvalid (view, (pos as PosCombine)?.Left, name);
+                    ThrowInvalid (view, (pos as PosCombine)?.Right, name);
 
                     break;
 
@@ -1134,8 +1132,8 @@ public partial class View
 
                 case Dim dim and DimCombine:
                     // Recursively check for not Absolute or not View
-                    ThrowInvalid (view, (dim as DimCombine).Left, name);
-                    ThrowInvalid (view, (dim as DimCombine).Right, name);
+                    ThrowInvalid (view, (dim as DimCombine)?.Left, name);
+                    ThrowInvalid (view, (dim as DimCombine)?.Right, name);
 
                     break;
             }

+ 0 - 1
UnitTests/View/Layout/Dim.AutoTests.cs

@@ -1002,7 +1002,6 @@ public class DimAutoTests (ITestOutputHelper output)
     [Theory]
     [InlineData (0, 15, 15)]
     [InlineData (1, 15, 16)]
-    [InlineData (0, 15, 15)]
     [InlineData (-1, 15, 14)]
     public void With_Subview_Using_DimAbsolute (int subViewOffset, int dimAbsoluteSize, int expectedSize)
     {

+ 1 - 1
UnitTests/View/Layout/Dim.PercentTests.cs

@@ -7,7 +7,7 @@ namespace Terminal.Gui.LayoutTests;
 
 public class DimPercentTests
 {
-    private readonly ITestOutputHelper _output;
+    //private readonly ITestOutputHelper _output;
 
     [Fact]
     public void DimFactor_Calculate_ReturnsCorrectValue ()

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

@@ -4,7 +4,7 @@ using static Terminal.Gui.Pos;
 
 namespace Terminal.Gui.LayoutTests;
 
-public class PosTests (ITestOutputHelper output)
+public class PosTests ()
 {
     // Was named AutoSize_Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
     // but doesn't actually have anything to do with AutoSize.