浏览代码

- Made Dim.DimAutoStyle flags.
Made View.ContentSize nullable, so _contentSize can be used to express 'desired contentsize

Tig 1 年之前
父节点
当前提交
f18c166d0f

+ 2 - 2
Terminal.Gui/Application.cs

@@ -1304,14 +1304,14 @@ public static partial class Application
     {
         SizeChanging?.Invoke (null, args);
 
-        if (args.Cancel)
+        if (args.Cancel || args.Size is null)
         {
             return false;
         }
 
         foreach (Toplevel t in _topLevels)
         {
-            t.SetRelativeLayout (args.Size);
+            t.SetRelativeLayout (args.Size.Value);
             t.LayoutSubviews ();
             t.PositionToplevels ();
             t.OnSizeChanging (new (args.Size));

+ 10 - 5
Terminal.Gui/ConsoleDrivers/WindowsDriver.cs

@@ -1517,23 +1517,28 @@ internal class WindowsDriver : ConsoleDriver
 #if HACK_CHECK_WINCHANGED
     private void ChangeWin (object s, SizeChangedEventArgs e)
     {
-        int w = e.Size.Width;
+        if (e.Size is null)
+        {
+            return;
+        }
+
+        int w = e.Size.Value.Width;
 
-        if (w == Cols - 3 && e.Size.Height < Rows)
+        if (w == Cols - 3 && e.Size.Value.Height < Rows)
         {
             w += 3;
         }
 
         Left = 0;
         Top = 0;
-        Cols = e.Size.Width;
-        Rows = e.Size.Height;
+        Cols = e.Size.Value.Width;
+        Rows = e.Size.Value.Height;
 
         if (!RunningUnitTests)
         {
             Size newSize = WinConsole.SetConsoleWindow (
                                                         (short)Math.Max (w, 16),
-                                                        (short)Math.Max (e.Size.Height, 0));
+                                                        (short)Math.Max (e.Size.Value.Height, 0));
 
             Cols = newSize.Width;
             Rows = newSize.Height;

+ 14 - 16
Terminal.Gui/View/Layout/PosDim.cs

@@ -1,8 +1,4 @@
 using System.Diagnostics;
-using System.Runtime.InteropServices.JavaScript;
-using static System.Net.Mime.MediaTypeNames;
-using static Terminal.Gui.Dialog;
-using static Terminal.Gui.Dim;
 
 namespace Terminal.Gui;
 
@@ -350,7 +346,7 @@ public class Pos
     ///     that
     ///     is used.
     /// </returns>
-    internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
+    internal virtual int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
     {
         return Anchor (superviewDimension);
     }
@@ -399,7 +395,7 @@ public class Pos
             return width - _offset;
         }
 
-        internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
+        internal override int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
         {
             int newLocation = Anchor (superviewDimension);
 
@@ -417,7 +413,7 @@ public class Pos
         public override string ToString () { return "Center"; }
         internal override int Anchor (int width) { return width / 2; }
 
-        internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
+        internal override int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
         {
             int newDimension = Math.Max (dim.Calculate (0, superviewDimension, us, dimension), 0);
 
@@ -445,7 +441,7 @@ public class Pos
             return la - ra;
         }
 
-        internal override int Calculate (int superviewDimension, Dim dim, View us, Dimension dimension)
+        internal override int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
         {
             int newDimension = dim.Calculate (0, superviewDimension, us, dimension);
             int left = _left.Calculate (superviewDimension, dim, us, dimension);
@@ -654,20 +650,21 @@ public class Dim
     /// <summary>
     ///     Specifies how <see cref="DimAuto"/> will compute the dimension.
     /// </summary>
+    [Flags]
     public enum DimAutoStyle
     {
         /// <summary>
         ///     The dimension will be computed using both the view's <see cref="View.Text"/> and
         ///     <see cref="View.Subviews"/> (whichever is larger).
         /// </summary>
-        Auto,
+        Auto = Subviews | Text,
 
         /// <summary>
         ///     The Subview in <see cref="View.Subviews"/> with the largest corresponding position plus dimension
         ///     will determine the dimension.
         ///     The corresponding dimension of the view's <see cref="View.Text"/> will be ignored.
         /// </summary>
-        Subviews,
+        Subviews = 1,
 
         /// <summary>
         ///     The corresponding dimension of the view's <see cref="View.Text"/>, formatted using the
@@ -675,7 +672,7 @@ public class Dim
         ///     will be used to determine the dimension.
         ///     The corresponding dimensions of the <see cref="View.Subviews"/> will be ignored.
         /// </summary>
-        Text
+        Text = 2
     }
 
 
@@ -932,16 +929,16 @@ public class Dim
                 return superviewContentSize;
             }
 
-            if (_style is Dim.DimAutoStyle.Text or Dim.DimAutoStyle.Auto)
+            if (_style.HasFlag (Dim.DimAutoStyle.Text))
             {
                 textSize = int.Max (autoMin, dimension == Dimension.Width ? us.TextFormatter.Size.Width : us.TextFormatter.Size.Height);
             }
 
-            if (_style is Dim.DimAutoStyle.Subviews or Dim.DimAutoStyle.Auto)
+            if (_style.HasFlag (DimAutoStyle.Subviews))
             {
-                if (us.IdealContentSize.HasValue)
+                if (us._contentSize is { })
                 {
-                    subviewsSize = dimension == Dimension.Width ? us.IdealContentSize.Value.Width : us.IdealContentSize.Value.Height;
+                    subviewsSize = dimension == Dimension.Width ? us.ContentSize.Value.Width : us.ContentSize.Value.Height;
                 }
                 else
                 {
@@ -976,7 +973,8 @@ public class Dim
         /// <returns></returns>
         internal override bool ReferencesOtherViews ()
         {
-            return _style is Dim.DimAutoStyle.Subviews or Dim.DimAutoStyle.Auto;
+            // BUGBUG: This is not correct. _contentSize may be null.
+            return _style.HasFlag (Dim.DimAutoStyle.Subviews);
         }
 
     }

+ 2 - 2
Terminal.Gui/View/Layout/SizeChangedEventArgs.cs

@@ -5,11 +5,11 @@ public class SizeChangedEventArgs : EventArgs
 {
     /// <summary>Creates a new instance of the <see cref="SizeChangedEventArgs"/> class.</summary>
     /// <param name="size"></param>
-    public SizeChangedEventArgs (Size size) { Size = size; }
+    public SizeChangedEventArgs (Size? size) { Size = size; }
 
     /// <summary>Set to <see langword="true"/> to cause the resize to be cancelled, if appropriate.</summary>
     public bool Cancel { get; set; }
 
     /// <summary>Gets the size the event describes.  This should reflect the new/current size after the event resolved.</summary>
-    public Size Size { get; }
+    public Size? Size { get; }
 }

+ 19 - 14
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -91,7 +91,7 @@ public partial class View
     private void SetFrame (Rectangle frame)
     {
         var oldViewport = Rectangle.Empty;
-        var oldContentSize = Size.Empty;
+        Size? oldContentSize = null;
 
         if (IsInitialized)
         {
@@ -106,7 +106,7 @@ public partial class View
 
         if (!TextFormatter.AutoSize)
         {
-            TextFormatter.Size = ContentSize;
+            TextFormatter.Size = ContentSize.GetValueOrDefault ();
         }
     }
 
@@ -284,7 +284,7 @@ public partial class View
             if (_height is Dim.DimAuto)
             {
                 // Reset ContentSize to Viewport
-                _contentSize = Size.Empty;
+                _contentSize = null;
             }
 
             _height = value ?? throw new ArgumentNullException (nameof (value), @$"{nameof (Height)} cannot be null");
@@ -330,7 +330,7 @@ public partial class View
             if (_width is Dim.DimAuto)
             {
                 // Reset ContentSize to Viewport
-                _contentSize = Size.Empty;
+                _contentSize = null;
             }
 
             _width = value ?? throw new ArgumentNullException (nameof (value), @$"{nameof (Width)} cannot be null");
@@ -672,7 +672,7 @@ public partial class View
 
         LayoutAdornments ();
 
-        OnLayoutStarted (new (ContentSize));
+        OnLayoutStarted (new (ContentSize.GetValueOrDefault ()));
 
         SetTextFormatterSize ();
 
@@ -712,13 +712,13 @@ public partial class View
         {
             foreach ((View from, View to) in edges)
             {
-                LayoutSubview (to, from.ContentSize);
+                LayoutSubview (to, from.ContentSize.GetValueOrDefault ());
             }
         }
 
         LayoutNeeded = false;
 
-        OnLayoutComplete (new (ContentSize));
+        OnLayoutComplete (new (ContentSize.GetValueOrDefault ()));
     }
 
     // TODO: Move this logic into the Pos/Dim classes
@@ -835,13 +835,13 @@ public partial class View
 
         // First try SuperView.Viewport, then Application.Top, then Driver.Viewport.
         // Finally, if none of those are valid, use int.MaxValue (for Unit tests).
-        Size contentSize = SuperView is { IsInitialized: true } ? SuperView.ContentSize :
+        Size? contentSize = SuperView is { IsInitialized: true } ? SuperView.ContentSize :
                            Application.Top is { } && Application.Top != this && Application.Top.IsInitialized ? Application.Top.ContentSize :
                            Application.Driver?.Screen.Size ?? new (int.MaxValue, int.MaxValue);
 
         SetTextFormatterSize ();
 
-        SetRelativeLayout (contentSize);
+        SetRelativeLayout (contentSize.GetValueOrDefault ());
 
         if (IsInitialized)
         {
@@ -892,18 +892,23 @@ public partial class View
     /// <param name="superviewContentSize">
     ///     The size of the SuperView's content (nominally the same as <c>this.SuperView.ContentSize</c>).
     /// </param>
-    internal void SetRelativeLayout (Size superviewContentSize)
+    internal void SetRelativeLayout (Size? superviewContentSize)
     {
         Debug.Assert (_x is { });
         Debug.Assert (_y is { });
         Debug.Assert (_width is { });
         Debug.Assert (_height is { });
 
+        if (superviewContentSize is null)
+        {
+            return;
+        }
+
         CheckDimAuto ();
-        int newX = _x.Calculate (superviewContentSize.Width, _width, this, Dim.Dimension.Width);
-        int newW = _width.Calculate (newX, superviewContentSize.Width, this, Dim.Dimension.Width);
-        int newY = _y.Calculate (superviewContentSize.Height, _height, this, Dim.Dimension.Height);
-        int newH = _height.Calculate (newY, superviewContentSize.Height, this, Dim.Dimension.Height);
+        int newX = _x.Calculate (superviewContentSize.Value.Width, _width, this, Dim.Dimension.Width);
+        int newW = _width.Calculate (newX, superviewContentSize.Value.Width, this, Dim.Dimension.Width);
+        int newY = _y.Calculate (superviewContentSize.Value.Height, _height, this, Dim.Dimension.Height);
+        int newH = _height.Calculate (newY, superviewContentSize.Value.Height, this, Dim.Dimension.Height);
 
         Rectangle newFrame = new (newX, newY, newW, newH);
 

+ 16 - 17
Terminal.Gui/View/ViewContent.cs

@@ -120,10 +120,10 @@ public partial class View
 {
     #region Content Area
 
-    private Size _contentSize;
+    internal Size? _contentSize;
 
     /// <summary>
-    ///     Gets or sets the size of the View's content. If not set, the value will be the same as the size of <see cref="Viewport"/>,
+    ///     Gets or sets the size of the View's content. If <see langword="null"/>, the value will be the same as the size of <see cref="Viewport"/>,
     ///     and <c>Viewport.Location</c> will always be <c>0, 0</c>.
     /// </summary>
     /// <remarks>
@@ -135,12 +135,12 @@ public partial class View
     ///         Negative sizes are not supported.
     ///     </para>
     /// </remarks>
-    public Size ContentSize
+    public Size? ContentSize
     {
-        get => _contentSize == Size.Empty ? Viewport.Size : _contentSize;
+        get => _contentSize ?? Viewport.Size;
         set
         {
-            if (value.Width < 0 || value.Height < 0)
+            if (value?.Width < 0 || value?.Height < 0)
             {
                 throw new ArgumentException (@"ContentSize cannot be negative.", nameof (value));
             }
@@ -155,8 +155,6 @@ public partial class View
         }
     }
 
-    public Size? IdealContentSize { get; set; }
-
     /// <summary>
     ///     Called when <see cref="ContentSize"/> changes. Invokes the <see cref="ContentSizeChanged"/> event.
     /// </summary>
@@ -253,7 +251,8 @@ public partial class View
     /// <summary>
     ///     Gets or sets the rectangle describing the portion of the View's content that is visible to the user.
     ///     The viewport Location is relative to the top-left corner of the inner rectangle of <see cref="Padding"/>.
-    ///     If the viewport Size is the same as <see cref="ContentSize"/> the Location will be <c>0, 0</c>.
+    ///     If the viewport Size is the same as <see cref="ContentSize"/>, or <see cref="ContentSize"/> is
+    ///     <see langword="null"/> the Location will be <c>0, 0</c>.
     /// </summary>
     /// <value>
     ///     The rectangle describing the location and size of the viewport into the View's virtual content, described by
@@ -305,14 +304,13 @@ public partial class View
                 return new (_viewportLocation, Frame.Size);
             }
 
-            Thickness thickness = GetAdornmentsThickness ();
-
+            // BUGBUG: This is a hack. Viewport_get should not have side effects.
             if (Frame.Size == Size.Empty)
             {
                 // The Frame has not been set yet (e.g. the view has not been added to a SuperView yet).
                 // 
-                if ((Width is Dim.DimAuto widthAuto && widthAuto._style != Dim.DimAutoStyle.Subviews)
-                    || (Height is Dim.DimAuto heightAuto && heightAuto._style != Dim.DimAutoStyle.Subviews))
+                if ((Width is Dim.DimAuto widthAuto && widthAuto._style.HasFlag(Dim.DimAutoStyle.Text))
+                    || (Height is Dim.DimAuto heightAuto && heightAuto._style.HasFlag (Dim.DimAutoStyle.Text)))
                 {
                     if (TextFormatter.NeedsFormat)
                     {
@@ -320,11 +318,12 @@ public partial class View
                         TextFormatter.AutoSize = true;
 
                         // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
-                        ContentSize = TextFormatter.Size;
+                        ContentSize = TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
                     }
                 }
             }
 
+            Thickness thickness = GetAdornmentsThickness ();
             return new (
                         _viewportLocation,
                         new (
@@ -372,9 +371,9 @@ public partial class View
         {
             if (!ViewportSettings.HasFlag (ViewportSettings.AllowXGreaterThanContentWidth))
             {
-                if (newViewport.X >= ContentSize.Width)
+                if (newViewport.X >= ContentSize.GetValueOrDefault ().Width)
                 {
-                    newViewport.X = ContentSize.Width - 1;
+                    newViewport.X = ContentSize.GetValueOrDefault ().Width - 1;
                 }
             }
 
@@ -389,9 +388,9 @@ public partial class View
 
             if (!ViewportSettings.HasFlag (ViewportSettings.AllowYGreaterThanContentHeight))
             {
-                if (newViewport.Y >= ContentSize.Height)
+                if (newViewport.Y >= ContentSize.GetValueOrDefault().Height)
                 {
-                    newViewport.Y = ContentSize.Height - 1;
+                    newViewport.Y = ContentSize.GetValueOrDefault ().Height - 1;
                 }
             }
 

+ 6 - 6
Terminal.Gui/View/ViewDrawing.cs

@@ -106,11 +106,11 @@ public partial class View
 
         if (ViewportSettings.HasFlag (ViewportSettings.ClearContentOnly))
         {
-            Rectangle visibleContent = ViewportToScreen (new (new (-Viewport.X, -Viewport.Y), ContentSize));
+            Rectangle visibleContent = ViewportToScreen (new (new (-Viewport.X, -Viewport.Y), ContentSize.GetValueOrDefault ()));
             toClear = Rectangle.Intersect (toClear, visibleContent);
         }
 
-        Attribute prev = Driver.SetAttribute (GetNormalColor());
+        Attribute prev = Driver.SetAttribute (GetNormalColor ());
         Driver.FillRect (toClear);
         Driver.SetAttribute (prev);
 
@@ -134,7 +134,7 @@ public partial class View
 
         Driver.Clip = Rectangle.Intersect (prevClip, ViewportToScreen (Viewport with { Location = new (0, 0) }));
 
-        Attribute prev = Driver.SetAttribute (new (color ?? GetNormalColor().Background));
+        Attribute prev = Driver.SetAttribute (new (color ?? GetNormalColor ().Background));
         Driver.FillRect (toClear);
         Driver.SetAttribute (prev);
 
@@ -172,7 +172,7 @@ public partial class View
         if (ViewportSettings.HasFlag (ViewportSettings.ClipContentOnly))
         {
             // Clamp the Clip to the just content area that is within the viewport
-            Rectangle visibleContent = ViewportToScreen (new (new (-Viewport.X, -Viewport.Y), ContentSize));
+            Rectangle visibleContent = ViewportToScreen (new (new (-Viewport.X, -Viewport.Y), ContentSize.GetValueOrDefault ()));
             clip = Rectangle.Intersect (clip, visibleContent);
         }
 
@@ -413,7 +413,7 @@ public partial class View
     {
         if (!IsInitialized)
         {
-           return false;
+            return false;
         }
 
         // Each of these renders lines to either this View's LineCanvas 
@@ -475,7 +475,7 @@ public partial class View
 
             // This should NOT clear 
             // TODO: If the output is not in the Viewport, do nothing
-            var drawRect = new Rectangle (ContentToScreen (Point.Empty), ContentSize);
+            var drawRect = new Rectangle (ContentToScreen (Point.Empty), ContentSize.GetValueOrDefault ());
 
             TextFormatter?.Draw (
                                  drawRect,

+ 8 - 8
Terminal.Gui/View/ViewText.cs

@@ -119,7 +119,7 @@ public partial class View
     /// <summary>
     ///     Gets or sets the <see cref="Gui.TextFormatter"/> used to format <see cref="Text"/>.
     /// </summary>
-    public TextFormatter TextFormatter { get; init; } = new () {};
+    public TextFormatter TextFormatter { get; init; } = new () { };
 
     /// <summary>
     ///     Gets or sets how the View's <see cref="Text"/> is aligned vertically when drawn. Changing this property will
@@ -179,19 +179,19 @@ public partial class View
 
         // TODO: This is a hack. Figure out how to move this into DimDimAuto
         // Use _width & _height instead of Width & Height to avoid debug spew
-        if ((_width is Dim.DimAuto widthAuto && widthAuto._style != Dim.DimAutoStyle.Subviews)
-            || (_height is Dim.DimAuto heightAuto && heightAuto._style != Dim.DimAutoStyle.Subviews))
+        if ((_width is Dim.DimAuto widthAuto && widthAuto._style.HasFlag (Dim.DimAutoStyle.Text))
+            || (_height is Dim.DimAuto heightAuto && heightAuto._style.HasFlag (Dim.DimAutoStyle.Text)))
         {
             // This updates TextFormatter.Size to the text size
             TextFormatter.AutoSize = true;
 
             // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
-            ContentSize = TextFormatter.Size;
+            ContentSize = TextFormatter.Size == Size.Empty ? null : TextFormatter.Size;
             return;
         }
 
         TextFormatter.AutoSize = false;
-        TextFormatter.Size = new Size (ContentSize.Width, ContentSize.Height);
+        TextFormatter.Size = new Size (ContentSize.GetValueOrDefault ().Width, ContentSize.GetValueOrDefault ().Height);
     }
 
     private void UpdateTextDirection (TextDirection newDirection)
@@ -257,11 +257,11 @@ public partial class View
             }
             else
             {
-                _height = ContentSize.Height;
-                _width = ContentSize.Width;
+                _height = ContentSize.GetValueOrDefault ().Height;
+                _width = ContentSize.GetValueOrDefault ().Width;
 
                 // Force ContentSize to be reset to Viewport
-                _contentSize = Size.Empty;
+                _contentSize = null;
                 OnResizeNeeded ();
             }
         }

+ 2 - 2
Terminal.Gui/Views/CheckBox.cs

@@ -191,11 +191,11 @@ public class CheckBox : View
 
     private string GetFormatterText ()
     {
-        if (Width is Dim.DimAuto || string.IsNullOrEmpty (Title) || ContentSize.Width <= 2)
+        if (Width is Dim.DimAuto || string.IsNullOrEmpty (Title) || ContentSize?.Width <= 2)
         {
             return Text;
         }
 
-        return Text [..Math.Min (ContentSize.Width - 2, Text.GetRuneCount ())];
+        return ContentSize is null ? Text : Text [..Math.Min (ContentSize.Value.Width - 2, Text.GetRuneCount ())];
     }
 }

+ 3 - 2
Terminal.Gui/Views/ComboBox.cs

@@ -612,13 +612,14 @@ public class ComboBox : View
             Height = _minimumHeight;
         }
 
+        // BUGBUG: This uses Viewport. Should use ContentSize
         if ((!_autoHide && Viewport.Width > 0 && _search.Frame.Width != Viewport.Width)
             || (_autoHide && Viewport.Width > 0 && _search.Frame.Width != Viewport.Width - 1))
         {
             _search.Width = _listview.Width = _autoHide ? Viewport.Width - 1 : Viewport.Width;
             _listview.Height = CalculatetHeight ();
-            _search.SetRelativeLayout (ContentSize);
-            _listview.SetRelativeLayout (ContentSize);
+            _search.SetRelativeLayout (ContentSize.GetValueOrDefault());
+            _listview.SetRelativeLayout (ContentSize.GetValueOrDefault ());
         }
     }
 

+ 1 - 1
Terminal.Gui/Views/MessageBox.cs

@@ -467,7 +467,7 @@ public static class MessageBox
                                      + adornmentsThickness.Vertical);
             }
 
-            d.SetRelativeLayout (d.SuperView?.ContentSize ?? Application.Top.ContentSize);
+            d.SetRelativeLayout (d.SuperView?.ContentSize.GetValueOrDefault () ?? Application.Top.ContentSize.GetValueOrDefault ());
             d.LayoutSubviews ();
         }
     }

+ 24 - 20
Terminal.Gui/Views/ScrollView.cs

@@ -88,10 +88,10 @@ public class ScrollView : View
         AddCommand (Command.PageDown, () => ScrollDown (Viewport.Height));
         AddCommand (Command.PageLeft, () => ScrollLeft (Viewport.Width));
         AddCommand (Command.PageRight, () => ScrollRight (Viewport.Width));
-        AddCommand (Command.TopHome, () => ScrollUp (ContentSize.Height));
-        AddCommand (Command.BottomEnd, () => ScrollDown (ContentSize.Height));
-        AddCommand (Command.LeftHome, () => ScrollLeft (ContentSize.Width));
-        AddCommand (Command.RightEnd, () => ScrollRight (ContentSize.Width));
+        AddCommand (Command.TopHome, () => ScrollUp (ContentSize.Value.Height));
+        AddCommand (Command.BottomEnd, () => ScrollDown (ContentSize.Value.Height));
+        AddCommand (Command.LeftHome, () => ScrollLeft (ContentSize.Value.Width));
+        AddCommand (Command.RightEnd, () => ScrollRight (ContentSize.Value.Width));
 
         // Default keybindings for this view
         KeyBindings.Add (Key.CursorUp, Command.ScrollUp);
@@ -127,7 +127,7 @@ public class ScrollView : View
                            }
 
                            SetContentOffset (_contentOffset);
-                           _contentView.Frame = new Rectangle (ContentOffset, ContentSize);
+                           _contentView.Frame = new Rectangle (ContentOffset, ContentSize.GetValueOrDefault ());
 
                            // PERF: How about calls to Point.Offset instead?
                            _vertical.ChangedPosition += delegate { ContentOffset = new Point (ContentOffset.X, _vertical.Position); };
@@ -138,9 +138,13 @@ public class ScrollView : View
 
     private void ScrollViewContentSizeChanged (object sender, SizeChangedEventArgs e)
     {
-        _contentView.Frame = new Rectangle (ContentOffset, e.Size with {Width = e.Size.Width-1, Height = e.Size.Height-1});
-        _vertical.Size = e.Size.Height;
-        _horizontal.Size = e.Size.Width;
+        if (e.Size is null)
+        {
+            return;
+        }
+        _contentView.Frame = new Rectangle (ContentOffset, e.Size.Value with { Width = e.Size.Value.Width - 1, Height = e.Size.Value.Height - 1 });
+        _vertical.Size = e.Size.Value.Height;
+        _horizontal.Size = e.Size.Value.Width;
     }
 
     private void Application_UnGrabbedMouse (object sender, ViewEventArgs e)
@@ -240,26 +244,26 @@ public class ScrollView : View
                 _horizontal.OtherScrollBarView.KeepContentAlwaysInViewport = value;
                 Point p = default;
 
-                if (value && -_contentOffset.X + Viewport.Width > ContentSize.Width)
+                if (value && -_contentOffset.X + Viewport.Width > ContentSize.GetValueOrDefault ().Width)
                 {
                     p = new Point (
-                                   ContentSize.Width - Viewport.Width + (_showVerticalScrollIndicator ? 1 : 0),
+                                   ContentSize.GetValueOrDefault ().Width - Viewport.Width + (_showVerticalScrollIndicator ? 1 : 0),
                                    -_contentOffset.Y
                                   );
                 }
 
-                if (value && -_contentOffset.Y + Viewport.Height > ContentSize.Height)
+                if (value && -_contentOffset.Y + Viewport.Height > ContentSize.GetValueOrDefault ().Height)
                 {
                     if (p == default (Point))
                     {
                         p = new Point (
                                        -_contentOffset.X,
-                                       ContentSize.Height - Viewport.Height + (_showHorizontalScrollIndicator ? 1 : 0)
+                                       ContentSize.GetValueOrDefault ().Height - Viewport.Height + (_showHorizontalScrollIndicator ? 1 : 0)
                                       );
                     }
                     else
                     {
-                        p.Y = ContentSize.Height - Viewport.Height + (_showHorizontalScrollIndicator ? 1 : 0);
+                        p.Y = ContentSize.GetValueOrDefault ().Height - Viewport.Height + (_showHorizontalScrollIndicator ? 1 : 0);
                     }
                 }
 
@@ -410,7 +414,7 @@ public class ScrollView : View
     }
 
     /// <inheritdoc/>
-    protected internal override bool OnMouseEvent  (MouseEvent me)
+    protected internal override bool OnMouseEvent (MouseEvent me)
     {
         if (!Enabled)
         {
@@ -447,7 +451,7 @@ public class ScrollView : View
             Application.UngrabMouse ();
         }
 
-        return base.OnMouseEvent(me);
+        return base.OnMouseEvent (me);
     }
 
     /// <inheritdoc/>
@@ -615,7 +619,7 @@ public class ScrollView : View
     {
         // INTENT: Unclear intent. How about a call to Offset?
         _contentOffset = new Point (-Math.Abs (offset.X), -Math.Abs (offset.Y));
-        _contentView.Frame = new Rectangle (_contentOffset, ContentSize);
+        _contentView.Frame = new Rectangle (_contentOffset, ContentSize.GetValueOrDefault ());
         int p = Math.Max (0, -_contentOffset.Y);
 
         if (_vertical.Position != p)
@@ -646,7 +650,7 @@ public class ScrollView : View
         bool v = false, h = false;
         var p = false;
 
-        if (Viewport.Height == 0 || Viewport.Height > ContentSize.Height)
+        if (ContentSize is { } && (Viewport.Height == 0 || Viewport.Height > ContentSize.Value.Height))
         {
             if (ShowVerticalScrollIndicator)
             {
@@ -655,7 +659,7 @@ public class ScrollView : View
 
             v = false;
         }
-        else if (Viewport.Height > 0 && Viewport.Height == ContentSize.Height)
+        else if (ContentSize is { } && Viewport.Height > 0 && Viewport.Height == ContentSize.Value.Height)
         {
             p = true;
         }
@@ -669,7 +673,7 @@ public class ScrollView : View
             v = true;
         }
 
-        if (Viewport.Width == 0 || Viewport.Width > ContentSize.Width)
+        if (ContentSize is { } && (Viewport.Width == 0 || Viewport.Width > ContentSize.Value.Width))
         {
             if (ShowHorizontalScrollIndicator)
             {
@@ -678,7 +682,7 @@ public class ScrollView : View
 
             h = false;
         }
-        else if (Viewport.Width > 0 && Viewport.Width == ContentSize.Width && p)
+        else if (ContentSize is { } && Viewport.Width > 0 && Viewport.Width == ContentSize.Value.Width && p)
         {
             if (ShowHorizontalScrollIndicator)
             {

+ 8 - 8
Terminal.Gui/Views/Slider.cs

@@ -400,8 +400,8 @@ public class Slider<T> : View
             }
             else
             {
-                Width = ContentSize.Width;
-                Height = ContentSize.Height;
+                Width = ContentSize.GetValueOrDefault ().Width;
+                Height = ContentSize.GetValueOrDefault ().Height;
             }
         }
     }
@@ -792,11 +792,11 @@ public class Slider<T> : View
                           Viewport.Location,
                           new (
                                int.Min (
-                                        SuperView.ContentSize.Width - adornmentsThickness.Horizontal,
+                                        SuperView.ContentSize.GetValueOrDefault ().Width - adornmentsThickness.Horizontal,
                                         CalcBestLength ()
                                        ),
                                int.Min (
-                                        SuperView.ContentSize.Height - adornmentsThickness.Vertical,
+                                        SuperView.ContentSize.GetValueOrDefault ().Height - adornmentsThickness.Vertical,
                                         CalcThickness ()
                                        )
                               )
@@ -804,14 +804,14 @@ public class Slider<T> : View
         }
         else
         {
-            IdealContentSize = new (
+            ContentSize = new (
                           new (
                                int.Min (
-                                        SuperView.ContentSize.Width - adornmentsThickness.Horizontal,
+                                        SuperView.ContentSize.GetValueOrDefault ().Width - adornmentsThickness.Horizontal,
                                         CalcThickness ()
                                        ),
                                int.Min (
-                                        SuperView.ContentSize.Height - adornmentsThickness.Vertical,
+                                        SuperView.ContentSize.GetValueOrDefault ().Height - adornmentsThickness.Vertical,
                                         CalcBestLength ()
                                        )
                               )
@@ -1532,7 +1532,7 @@ public class Slider<T> : View
     private Point? _moveRenderPosition;
 
     /// <inheritdoc/>
-    protected internal override bool OnMouseEvent  (MouseEvent mouseEvent)
+    protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
     {
         // Note(jmperricone): Maybe we click to focus the cursor, and on next click we set the option.
         //                    That will makes OptionFocused Event more relevant.

+ 2 - 2
UICatalog/Scenarios/ASCIICustomButton.cs

@@ -273,7 +273,7 @@ public class ASCIICustomButtonTest : Scenario
                 case KeyCode.End:
                     _scrollView.ContentOffset = new Point (
                                                            _scrollView.ContentOffset.X,
-                                                           -(_scrollView.ContentSize.Height
+                                                           -(_scrollView.ContentSize.GetValueOrDefault ().Height
                                                              - _scrollView.Frame.Height
                                                              + (_scrollView.ShowHorizontalScrollIndicator ? 1 : 0))
                                                           );
@@ -291,7 +291,7 @@ public class ASCIICustomButtonTest : Scenario
                                                            Math.Max (
                                                                      _scrollView.ContentOffset.Y
                                                                      - _scrollView.Frame.Height,
-                                                                     -(_scrollView.ContentSize.Height
+                                                                     -(_scrollView.ContentSize.GetValueOrDefault ().Height
                                                                        - _scrollView.Frame.Height
                                                                        + (_scrollView.ShowHorizontalScrollIndicator
                                                                               ? 1

+ 6 - 6
UICatalog/Scenarios/ContentScrolling.cs

@@ -115,7 +115,7 @@ public class ContentScrolling : Scenario
         var view = new ScrollingDemoView
         {
             Title = "Demo View",
-            X = Pos.Right(editor),
+            X = Pos.Right (editor),
             Width = Dim.Fill (),
             Height = Dim.Fill ()
         };
@@ -227,7 +227,7 @@ public class ContentScrolling : Scenario
 
         var contentSizeWidth = new Buttons.NumericUpDown<int>
         {
-            Value = view.ContentSize.Width,
+            Value = view.ContentSize.GetValueOrDefault ().Width,
             X = Pos.Right (labelContentSize) + 1,
             Y = Pos.Top (labelContentSize)
         };
@@ -242,7 +242,7 @@ public class ContentScrolling : Scenario
                 return;
             }
 
-            view.ContentSize = view.ContentSize with { Width = e.NewValue };
+            view.ContentSize = view.ContentSize.GetValueOrDefault () with { Width = e.NewValue };
         }
 
         var labelComma = new Label
@@ -254,7 +254,7 @@ public class ContentScrolling : Scenario
 
         var contentSizeHeight = new Buttons.NumericUpDown<int>
         {
-            Value = view.ContentSize.Height,
+            Value = view.ContentSize.GetValueOrDefault ().Height,
             X = Pos.Right (labelComma) + 1,
             Y = Pos.Top (labelContentSize),
             CanFocus = false
@@ -270,7 +270,7 @@ public class ContentScrolling : Scenario
                 return;
             }
 
-            view.ContentSize = view.ContentSize with { Height = e.NewValue };
+            view.ContentSize = view.ContentSize.GetValueOrDefault () with { Height = e.NewValue };
         }
 
         var cbClearOnlyVisible = new CheckBox
@@ -397,7 +397,7 @@ public class ContentScrolling : Scenario
             BorderStyle = LineStyle.Double,
             Title = "_Slider"
         };
-        view.Add(slider);
+        view.Add (slider);
 
         editor.Initialized += (s, e) => { editor.ViewToEdit = view; };
 

+ 5 - 1
UICatalog/Scenarios/DimAutoDemo.cs

@@ -27,7 +27,11 @@ public class DimAutoDemo : Scenario
         };
         view.ValidatePosDim = true;
 
-        var textEdit = new TextView { Text = "", X = 1, Y = 0, Width = 20, Height = 4 };
+        var textEdit = new TextView
+        {
+            Text = "",
+            X = 1, Y = 0, Width = 20, Height = 4
+        };
         view.Add (textEdit);
 
         var hlabel = new Label

+ 2 - 2
UnitTests/ConsoleDrivers/ConsoleDriverTests.cs

@@ -211,8 +211,8 @@ public class ConsoleDriverTests
         driver.SizeChanged += (s, e) =>
                               {
                                   wasTerminalResized = true;
-                                  Assert.Equal (120, e.Size.Width);
-                                  Assert.Equal (40, e.Size.Height);
+                                  Assert.Equal (120, e.Size.GetValueOrDefault ().Width);
+                                  Assert.Equal (40, e.Size.GetValueOrDefault ().Height);
                               };
 
         Assert.Equal (80, driver.Cols);

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

@@ -581,7 +581,7 @@ public class DimAutoTests (ITestOutputHelper output)
 
         superView.BeginInit ();
         superView.EndInit ();
-        superView.SetRelativeLayout (superView.ContentSize);
+        superView.SetRelativeLayout (superView.ContentSize.GetValueOrDefault ());
 
         superView.LayoutSubviews ();
         Assert.Equal (expectedSubWidth, subView.Frame.Width);

+ 2 - 2
UnitTests/Views/ScrollViewTests.cs

@@ -485,8 +485,8 @@ public class ScrollViewTests
         top.Add (sv);
         Application.Begin (top);
 
-        Assert.Equal (50, sv.ContentSize.Width);
-        Assert.Equal (50, sv.ContentSize.Height);
+        Assert.Equal (50, sv.ContentSize.GetValueOrDefault ().Width);
+        Assert.Equal (50, sv.ContentSize.GetValueOrDefault ().Height);
         Assert.True (sv.AutoHideScrollBars);
         Assert.True (sv.ShowHorizontalScrollIndicator);
         Assert.True (sv.ShowVerticalScrollIndicator);

+ 0 - 1
UnitTests/Views/SliderTests.cs

@@ -502,7 +502,6 @@ public class SliderTests
             Type = SliderType.Multiple,
             Width = Dim.Auto (Dim.DimAutoStyle.Subviews),
             Height = Dim.Auto (Dim.DimAutoStyle.Subviews),
-            //IdealContentSize = new (6, 2)
         };
         view.Add (slider);
         view.BeginInit ();