Browse Source

More #nullable enable

Tig 10 months ago
parent
commit
b3cb051ddf

+ 1 - 1
Terminal.Gui/Application/Application.Keyboard.cs

@@ -372,7 +372,7 @@ public static partial class Application // Keyboard handling
 
                         if (viewToArrange is { })
                         {
-                            return viewToArrange.Border?.Arrange ();
+                            return viewToArrange.Border?.EnterArrangeMode ();
                         }
 
                         return false;

+ 82 - 45
Terminal.Gui/View/Adornment/Border.cs

@@ -1,3 +1,4 @@
+#nullable enable
 using System.Diagnostics;
 using static Terminal.Gui.SpinnerStyle;
 
@@ -75,9 +76,6 @@ public class Border : Adornment
 #endif
 
 
-    [CanBeNull]
-    private Button _arrangeButton;
-
     /// <inheritdoc/>
     public override void BeginInit ()
     {
@@ -142,7 +140,7 @@ public class Border : Adornment
     ///     The color scheme for the Border. If set to <see langword="null"/>, gets the <see cref="Adornment.Parent"/>
     ///     scheme. color scheme.
     /// </summary>
-    public override ColorScheme ColorScheme
+    public override ColorScheme? ColorScheme
     {
         get
         {
@@ -151,7 +149,12 @@ public class Border : Adornment
                 return base.ColorScheme;
             }
 
-            return Parent?.ColorScheme;
+            if (Parent?.ColorScheme is { })
+            {
+                return Parent.ColorScheme;
+            }
+
+            return null;
         }
         set
         {
@@ -204,7 +207,7 @@ public class Border : Adornment
             // TODO: Make Border.LineStyle inherit from the SuperView hierarchy
             // TODO: Right now, Window and FrameView use CM to set BorderStyle, which negates
             // TODO: all this.
-            return Parent.SuperView?.BorderStyle ?? LineStyle.None;
+            return Parent!.SuperView?.BorderStyle ?? LineStyle.None;
         }
         set => _lineStyle = value;
     }
@@ -234,9 +237,9 @@ public class Border : Adornment
 
     private Color? _savedForeColor;
 
-    private void Border_Highlight (object sender, CancelEventArgs<HighlightStyle> e)
+    private void Border_Highlight (object? sender, CancelEventArgs<HighlightStyle> e)
     {
-        if (!Parent.Arrangement.HasFlag (ViewArrangement.Movable))
+        if (!Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
         {
             e.Cancel = true;
 
@@ -247,12 +250,12 @@ public class Border : Adornment
         {
             if (!_savedForeColor.HasValue)
             {
-                _savedForeColor = ColorScheme.Normal.Foreground;
+                _savedForeColor = ColorScheme!.Normal.Foreground;
             }
 
             var cs = new ColorScheme (ColorScheme)
             {
-                Normal = new (ColorScheme.Normal.Foreground.GetHighlightColor (), ColorScheme.Normal.Background)
+                Normal = new (ColorScheme!.Normal.Foreground.GetHighlightColor (), ColorScheme.Normal.Background)
             };
             ColorScheme = cs;
         }
@@ -271,7 +274,7 @@ public class Border : Adornment
         {
             var cs = new ColorScheme (ColorScheme)
             {
-                Normal = new (_savedForeColor.Value, ColorScheme.Normal.Background)
+                Normal = new (_savedForeColor.Value, ColorScheme!.Normal.Background)
             };
             ColorScheme = cs;
         }
@@ -297,7 +300,7 @@ public class Border : Adornment
         //    return false;
         //}
 
-        if (!Parent.Arrangement.HasFlag (ViewArrangement.Movable))
+        if (!Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
         {
             return false;
         }
@@ -330,7 +333,7 @@ public class Border : Adornment
                 if (Parent.SuperView is null)
                 {
                     // Redraw the entire app window.
-                    Application.Top.SetNeedsDisplay ();
+                    Application.Top!.SetNeedsDisplay ();
                 }
                 else
                 {
@@ -380,7 +383,7 @@ public class Border : Adornment
         base.Dispose (disposing);
     }
 
-    private void Application_GrabbingMouse (object sender, GrabMouseEventArgs e)
+    private void Application_GrabbingMouse (object? sender, GrabMouseEventArgs e)
     {
         if (Application.MouseGrabView == this && _dragPosition.HasValue)
         {
@@ -388,7 +391,7 @@ public class Border : Adornment
         }
     }
 
-    private void Application_UnGrabbingMouse (object sender, GrabMouseEventArgs e)
+    private void Application_UnGrabbingMouse (object? sender, GrabMouseEventArgs e)
     {
         if (Application.MouseGrabView == this && _dragPosition.HasValue)
         {
@@ -428,7 +431,7 @@ public class Border : Adornment
         int maxTitleWidth = Math.Max (
                                       0,
                                       Math.Min (
-                                                Parent.TitleTextFormatter.FormatAndGetSize ().Width,
+                                                Parent!.TitleTextFormatter.FormatAndGetSize ().Width,
                                                 Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
                                                )
                                      );
@@ -490,7 +493,7 @@ public class Border : Adornment
 
         if (canDrawBorder && LineStyle != LineStyle.None)
         {
-            LineCanvas lc = Parent?.LineCanvas;
+            LineCanvas? lc = Parent?.LineCanvas;
 
             bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height >= 1;
             bool drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
@@ -505,7 +508,7 @@ public class Border : Adornment
             }
             else
             {
-                Driver.SetAttribute (Parent.GetNormalColor ());
+                Driver.SetAttribute (Parent!.GetNormalColor ());
             }
 
             if (drawTop)
@@ -515,7 +518,7 @@ public class Border : Adornment
                 if (borderBounds.Width < 4 || !Settings.FastHasFlags (BorderSettings.Title) || string.IsNullOrEmpty (Parent?.Title))
                 {
                     // ╔╡╞╗ should be ╔══╗
-                    lc.AddLine (
+                    lc?.AddLine (
                                 new (borderBounds.Location.X, titleY),
                                 borderBounds.Width,
                                 Orientation.Horizontal,
@@ -530,7 +533,7 @@ public class Border : Adornment
                     //│
                     if (Thickness.Top == 2)
                     {
-                        lc.AddLine (
+                        lc?.AddLine (
                                     new (borderBounds.X + 1, topTitleLineY),
                                     Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
                                     Orientation.Horizontal,
@@ -544,7 +547,7 @@ public class Border : Adornment
                     //│
                     if (borderBounds.Width >= 4 && Thickness.Top > 2)
                     {
-                        lc.AddLine (
+                        lc?.AddLine (
                                     new (borderBounds.X + 1, topTitleLineY),
                                     Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
                                     Orientation.Horizontal,
@@ -552,7 +555,7 @@ public class Border : Adornment
                                     Driver.GetAttribute ()
                                    );
 
-                        lc.AddLine (
+                        lc?.AddLine (
                                     new (borderBounds.X + 1, topTitleLineY + 2),
                                     Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
                                     Orientation.Horizontal,
@@ -563,7 +566,7 @@ public class Border : Adornment
 
                     // ╔╡Title╞═════╗
                     // Add a short horiz line for ╔╡
-                    lc.AddLine (
+                    lc?.AddLine (
                                 new (borderBounds.Location.X, titleY),
                                 2,
                                 Orientation.Horizontal,
@@ -572,7 +575,7 @@ public class Border : Adornment
                                );
 
                     // Add a vert line for ╔╡
-                    lc.AddLine (
+                    lc?.AddLine (
                                 new (borderBounds.X + 1, topTitleLineY),
                                 titleBarsLength,
                                 Orientation.Vertical,
@@ -581,7 +584,7 @@ public class Border : Adornment
                                );
 
                     // Add a vert line for ╞
-                    lc.AddLine (
+                    lc?.AddLine (
                                 new (
                                      borderBounds.X
                                      + 1
@@ -596,7 +599,7 @@ public class Border : Adornment
                                );
 
                     // Add the right hand line for ╞═════╗
-                    lc.AddLine (
+                    lc?.AddLine (
                                 new (
                                      borderBounds.X
                                      + 1
@@ -616,7 +619,7 @@ public class Border : Adornment
 
             if (drawLeft)
             {
-                lc.AddLine (
+                lc?.AddLine (
                             new (borderBounds.Location.X, titleY),
                             sideLineLength,
                             Orientation.Vertical,
@@ -628,7 +631,7 @@ public class Border : Adornment
 
             if (drawBottom)
             {
-                lc.AddLine (
+                lc?.AddLine (
                             new (borderBounds.X, borderBounds.Y + borderBounds.Height - 1),
                             borderBounds.Width,
                             Orientation.Horizontal,
@@ -639,7 +642,7 @@ public class Border : Adornment
 
             if (drawRight)
             {
-                lc.AddLine (
+                lc?.AddLine (
                             new (borderBounds.X + borderBounds.Width - 1, titleY),
                             sideLineLength,
                             Orientation.Vertical,
@@ -664,7 +667,7 @@ public class Border : Adornment
                 // Redraw title 
                 if (drawTop && maxTitleWidth > 0 && Settings.FastHasFlags (BorderSettings.Title))
                 {
-                    Parent.TitleTextFormatter.Draw (
+                    Parent!.TitleTextFormatter.Draw (
                                                     new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
                                                     Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor (),
                                                     Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor ());
@@ -694,11 +697,11 @@ public class Border : Adornment
             // TODO: This should not be done on each draw?
             if (Settings.FastHasFlags (BorderSettings.Gradient))
             {
-                SetupGradientLineCanvas (lc, screenBounds);
+                SetupGradientLineCanvas (lc!, screenBounds);
             }
             else
             {
-                lc.Fill = null;
+                lc!.Fill = null;
             }
         }
     }
@@ -718,23 +721,34 @@ public class Border : Adornment
     private static void GetAppealingGradientColors (out List<Color> stops, out List<int> steps)
     {
         // Define the colors of the gradient stops with more appealing colors
-        stops = new ()
-        {
+        stops =
+        [
             new (0, 128, 255), // Bright Blue
             new (0, 255, 128), // Bright Green
             new (255, 255), // Bright Yellow
             new (255, 128), // Bright Orange
-            new (255, 0, 128) // Bright Pink
-        };
+            new (255, 0, 128)
+        ];
 
         // Define the number of steps between each color for smoother transitions
         // If we pass only a single value then it will assume equal steps between all pairs
-        steps = new () { 15 };
+        steps = [15];
     }
 
     private ViewArrangement _arranging;
 
-    public bool? Arrange ()
+    private Button? _arrangeButton;
+
+    /// <summary>
+    ///     Starts "Arrange Mode" where <see cref="Adornment.Parent"/> can be moved and/or resized using the mouse
+    ///     or keyboard.
+    /// </summary>
+    /// <remarks>
+    ///     Arrange Mode is exited by the user pressing <see cref="Application.ArrangeKey"/>, <see cref="Key.Esc"/>, or by clicking
+    ///     the mouse out of the <see cref="Adornment.Parent"/>'s Frame.
+    /// </remarks>
+    /// <returns></returns>
+    public bool? EnterArrangeMode ()
     {
         Debug.Assert (_arranging == ViewArrangement.Fixed);
 
@@ -769,6 +783,11 @@ public class Border : Adornment
         AddCommand (Command.Up,
                     () =>
                     {
+                        if (Parent is null)
+                        {
+                            return false;
+                        }
+
                         if (_arranging == ViewArrangement.Movable)
                         {
                             Parent!.Y = Parent.Y - 1;
@@ -778,7 +797,7 @@ public class Border : Adornment
                         {
                             if (Parent!.Viewport.Height > 0)
                             {
-                                Parent!.Height = Parent.Height - 1;
+                                Parent!.Height = Parent.Height! - 1;
                             }
                         }
 
@@ -788,6 +807,11 @@ public class Border : Adornment
         AddCommand (Command.Down,
                     () =>
                     {
+                        if (Parent is null)
+                        {
+                            return false;
+                        }
+
                         if (_arranging == ViewArrangement.Movable)
                         {
                             Parent!.Y = Parent.Y + 1;
@@ -795,7 +819,7 @@ public class Border : Adornment
 
                         if (_arranging == ViewArrangement.Resizable)
                         {
-                            Parent!.Height = Parent.Height + 1;
+                            Parent!.Height = Parent.Height! + 1;
                         }
 
                         return true;
@@ -803,6 +827,11 @@ public class Border : Adornment
         AddCommand (Command.Left,
                     () =>
                     {
+                        if (Parent is null)
+                        {
+                            return false;
+                        }
+
                         if (_arranging == ViewArrangement.Movable)
                         {
                             Parent!.X = Parent.X - 1;
@@ -812,7 +841,7 @@ public class Border : Adornment
                         {
                             if (Parent!.Viewport.Width > 0)
                             {
-                                Parent!.Width = Parent.Width - 1;
+                                Parent!.Width = Parent.Width! - 1;
                             }
                         }
                         return true;
@@ -821,6 +850,11 @@ public class Border : Adornment
         AddCommand (Command.Right,
                     () =>
                     {
+                        if (Parent is null)
+                        {
+                            return false;
+                        }
+
                         if (_arranging == ViewArrangement.Movable)
                         {
                             Parent!.X = Parent.X + 1;
@@ -828,7 +862,7 @@ public class Border : Adornment
 
                         if (_arranging == ViewArrangement.Resizable)
                         {
-                            Parent!.Width = Parent.Width + 1;
+                            Parent!.Width = Parent.Width! + 1;
                         }
 
                         return true;
@@ -899,9 +933,12 @@ public class Border : Adornment
         _arranging = ViewArrangement.Fixed;
         CanFocus = false;
 
-        Remove (_arrangeButton);
-        _arrangeButton.Dispose ();
-        _arrangeButton = null;
+        if (_arrangeButton is { })
+        {
+            Remove (_arrangeButton);
+            _arrangeButton.Dispose ();
+            _arrangeButton = null;
+        }
 
         KeyBindings.Clear ();
 

+ 12 - 13
Terminal.Gui/View/View.Drawing.cs

@@ -1,11 +1,12 @@
-namespace Terminal.Gui;
+#nullable enable
+namespace Terminal.Gui;
 
 public partial class View // Drawing APIs
 {
-    private ColorScheme _colorScheme;
+    private ColorScheme? _colorScheme;
 
     /// <summary>The color scheme for this view, if it is not defined, it returns the <see cref="SuperView"/>'s color scheme.</summary>
-    public virtual ColorScheme ColorScheme
+    public virtual ColorScheme? ColorScheme
     {
         get
         {
@@ -261,8 +262,7 @@ public partial class View // Drawing APIs
     ///         <see cref="View"/> .
     ///     </para>
     /// </remarks>
-    [CanBeNull]
-    public event EventHandler<DrawEventArgs> DrawContent;
+    public event EventHandler<DrawEventArgs>? DrawContent;
 
     /// <summary>Event invoked when the content area of the View is completed drawing.</summary>
     /// <remarks>
@@ -272,8 +272,7 @@ public partial class View // Drawing APIs
     ///         <see cref="View"/> .
     ///     </para>
     /// </remarks>
-    [CanBeNull]
-    public event EventHandler<DrawEventArgs> DrawContentComplete;
+    public event EventHandler<DrawEventArgs>? DrawContentComplete;
 
     /// <summary>Utility function to draw strings that contain a hotkey.</summary>
     /// <param name="text">String to display, the hotkey specifier before a letter flags the next letter as the hotkey.</param>
@@ -339,7 +338,7 @@ public partial class View // Drawing APIs
     /// </returns>
     public virtual Attribute GetFocusColor ()
     {
-        ColorScheme cs = ColorScheme;
+        ColorScheme? cs = ColorScheme;
 
         if (cs is null)
         {
@@ -357,7 +356,7 @@ public partial class View // Drawing APIs
     /// </returns>
     public virtual Attribute GetHotNormalColor ()
     {
-        ColorScheme cs = ColorScheme;
+        ColorScheme? cs = ColorScheme;
 
         if (cs is null)
         {
@@ -375,7 +374,7 @@ public partial class View // Drawing APIs
     /// </returns>
     public virtual Attribute GetNormalColor ()
     {
-        ColorScheme cs = ColorScheme;
+        ColorScheme? cs = ColorScheme;
 
         if (cs is null)
         {
@@ -499,7 +498,7 @@ public partial class View // Drawing APIs
             TextFormatter?.Draw (
                                  drawRect,
                                  HasFocus ? GetFocusColor () : GetNormalColor (),
-                                 HasFocus ? ColorScheme.HotFocus : GetHotNormalColor (),
+                                 HasFocus ? ColorScheme!.HotFocus : GetHotNormalColor (),
                                  Rectangle.Empty
                                 );
             SetSubViewNeedsDisplay ();
@@ -560,7 +559,7 @@ public partial class View // Drawing APIs
                 // Get the entire map
                 if (p.Value is { })
                 {
-                    Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme.Normal);
+                    Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
                     Driver.Move (p.Key.X, p.Key.Y);
 
                     // TODO: #2616 - Support combining sequences that don't normalize
@@ -585,7 +584,7 @@ public partial class View // Drawing APIs
                 // Get the entire map
                 if (p.Value is { })
                 {
-                    Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme.Normal);
+                    Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme!.Normal);
                     Driver.Move (p.Key.X, p.Key.Y);
 
                     // TODO: #2616 - Support combining sequences that don't normalize

+ 1 - 1
Terminal.Gui/Views/Menu/MenuBar.cs

@@ -306,7 +306,7 @@ public class MenuBar : View, IDesignable
 
             if (i == _selected && IsMenuOpen)
             {
-                hotColor = i == _selected ? ColorScheme.HotFocus : GetHotNormalColor ();
+                hotColor = i == _selected ? ColorScheme!.HotFocus : GetHotNormalColor ();
                 normalColor = i == _selected ? GetFocusColor () : GetNormalColor ();
             }
             else

+ 5 - 5
Terminal.Gui/Views/TextView.cs

@@ -3149,7 +3149,7 @@ public class TextView : View
             cs = new ();
         }
 
-        return Enabled ? cs.Focus : cs.Disabled;
+        return Enabled ? cs!.Focus : cs!.Disabled;
     }
 
     /// <summary>
@@ -4019,8 +4019,8 @@ public class TextView : View
         {
             Driver.SetAttribute (
                                  new (
-                                      ColorScheme.Focus.Background,
-                                      ColorScheme.Focus.Foreground
+                                      ColorScheme!.Focus.Background,
+                                      ColorScheme!.Focus.Foreground
                                      )
                                 );
         }
@@ -6258,11 +6258,11 @@ public class TextView : View
         DoNeededAction ();
     }
 
-    private static void SetValidUsedColor (ColorScheme colorScheme)
+    private static void SetValidUsedColor (ColorScheme? colorScheme)
     {
         // BUGBUG: (v2 truecolor) This code depends on 8-bit color names; disabling for now
         //if ((colorScheme!.HotNormal.Foreground & colorScheme.Focus.Background) == colorScheme.Focus.Foreground) {
-        Driver.SetAttribute (new (colorScheme.Focus.Background, colorScheme.Focus.Foreground));
+        Driver.SetAttribute (new (colorScheme!.Focus.Background, colorScheme!.Focus.Foreground));
     }
 
     /// <summary>Restore from original model.</summary>

+ 1 - 1
UICatalog/Scenarios/ArrangementEditor.cs

@@ -224,7 +224,7 @@ public sealed class ArrangementEditor : View
             else
             {
                 _viewToEdit.ShadowStyle = ShadowStyle.None;
-                _viewToEdit.ColorScheme = _viewToEdit.SuperView.ColorScheme;
+                _viewToEdit.ColorScheme = _viewToEdit!.SuperView!.ColorScheme;
             }
 
             if (_viewToEdit.Arrangement.HasFlag (ViewArrangement.Movable))