Browse Source

Code cleanup

Tig 1 year ago
parent
commit
48eba3bc55
1 changed files with 49 additions and 40 deletions
  1. 49 40
      Terminal.Gui/View/ViewText.cs

+ 49 - 40
Terminal.Gui/View/ViewText.cs

@@ -1,23 +1,17 @@
 #nullable enable
 
-using System.Diagnostics;
-using static Unix.Terminal.Curses;
-
 namespace Terminal.Gui;
 
 public partial class View
 {
+    private string _text;
+
     /// <summary>
-    ///    Initializes the Text of the View. Called by the constructor.
+    ///     Called when the <see cref="Text"/> has changed. Fires the <see cref="TextChanged"/> event.
     /// </summary>
-    private void SetupText ()
-    {
-        Text = string.Empty;
-        TextDirection = TextDirection.LeftRight_TopBottom;
-    }
-
-    private string _text;
+    public void OnTextChanged () { TextChanged?.Invoke (this, EventArgs.Empty); }
 
+    // TODO: Make this non-virtual. Nobody overrides it.
     /// <summary>
     ///     Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
     ///     or not when <see cref="TextFormatter.WordWrap"/> is enabled.
@@ -49,11 +43,14 @@ public partial class View
     ///         to <see cref="TextAlignment"/> and <see cref="TextDirection"/>.
     ///     </para>
     ///     <para>
-    ///         The text will word-wrap to additional lines if it does not fit horizontally. If <see cref="GetContentSize ()"/>'s height
+    ///         The text will word-wrap to additional lines if it does not fit horizontally. If <see cref="GetContentSize ()"/>
+    ///         's height
     ///         is 1, the text will be clipped.
     ///     </para>
-    ///     <para>If <see cref="View.Width"/> or <see cref="View.Height"/> are using <see cref="DimAutoStyle.Text"/>,
-    ///     the <see cref="GetContentSize ()"/> will be adjusted to fit the text.</para>
+    ///     <para>
+    ///         If <see cref="View.Width"/> or <see cref="View.Height"/> are using <see cref="DimAutoStyle.Text"/>,
+    ///         the <see cref="GetContentSize ()"/> will be adjusted to fit the text.
+    ///     </para>
     ///     <para>When the text changes, the <see cref="TextChanged"/> is fired.</para>
     /// </remarks>
     public virtual string Text
@@ -76,26 +73,16 @@ public partial class View
         }
     }
 
-    /// <summary>
-    /// Called when the <see cref="Text"/> has changed. Fires the <see cref="TextChanged"/> event.
-    /// </summary>
-    public void OnTextChanged ()
-    {
-        TextChanged?.Invoke (this, EventArgs.Empty);
-    }
-
-    /// <summary>
-    ///     Text changed event, raised when the text has changed.
-    /// </summary>
-    public event EventHandler? TextChanged;
-
     // TODO: Make this non-virtual. Nobody overrides it.
     /// <summary>
     ///     Gets or sets how the View's <see cref="Text"/> is aligned horizontally when drawn. Changing this property will
     ///     redisplay the <see cref="View"/>.
     /// </summary>
     /// <remarks>
-    ///     <para> <see cref="View.Width"/> or <see cref="View.Height"/> are using <see cref="DimAutoStyle.Text"/>, the <see cref="GetContentSize ()"/> will be adjusted to fit the text.</para>
+    ///     <para>
+    ///         <see cref="View.Width"/> or <see cref="View.Height"/> are using <see cref="DimAutoStyle.Text"/>, the
+    ///         <see cref="GetContentSize ()"/> will be adjusted to fit the text.
+    ///     </para>
     /// </remarks>
     /// <value>The text alignment.</value>
     public virtual Alignment TextAlignment
@@ -109,13 +96,21 @@ public partial class View
         }
     }
 
+    /// <summary>
+    ///     Text changed event, raised when the text has changed.
+    /// </summary>
+    public event EventHandler? TextChanged;
+
     // TODO: Make this non-virtual. Nobody overrides it.
     /// <summary>
     ///     Gets or sets the direction of the View's <see cref="Text"/>. Changing this property will redisplay the
     ///     <see cref="View"/>.
     /// </summary>
     /// <remarks>
-    ///     <para> <see cref="View.Width"/> or <see cref="View.Height"/> are using <see cref="DimAutoStyle.Text"/>, the <see cref="GetContentSize ()"/> will be adjusted to fit the text.</para>
+    ///     <para>
+    ///         <see cref="View.Width"/> or <see cref="View.Height"/> are using <see cref="DimAutoStyle.Text"/>, the
+    ///         <see cref="GetContentSize ()"/> will be adjusted to fit the text.
+    ///     </para>
     /// </remarks>
     /// <value>The text direction.</value>
     public virtual TextDirection TextDirection
@@ -127,7 +122,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 ();
 
     // TODO: Make this non-virtual. Nobody overrides it.
     /// <summary>
@@ -136,7 +131,10 @@ public partial class View
     ///     the <see cref="View"/>.
     /// </summary>
     /// <remarks>
-    ///     <para> <see cref="View.Width"/> or <see cref="View.Height"/> are using <see cref="DimAutoStyle.Text"/>, the <see cref="GetContentSize ()"/> will be adjusted to fit the text.</para>
+    ///     <para>
+    ///         <see cref="View.Width"/> or <see cref="View.Height"/> are using <see cref="DimAutoStyle.Text"/>, the
+    ///         <see cref="GetContentSize ()"/> will be adjusted to fit the text.
+    ///     </para>
     /// </remarks>
     /// <value>The vertical text alignment.</value>
     public virtual Alignment VerticalTextAlignment
@@ -151,9 +149,12 @@ public partial class View
 
     // TODO: Add a OnUpdateTextFormatterText method that invokes UpdateTextFormatterText so that overrides don't have to call base.
     /// <summary>
-    ///     Can be overridden if the <see cref="Terminal.Gui.TextFormatter.Text"/> has
+    ///     Can be overridden if the <see cref="TextFormatter.Text"/> has
     ///     different format than the default.
     /// </summary>
+    /// <remarks>
+    ///     Overrides must call <c>base.UpdateTextFormatterText</c> before updating <see cref="TextFormatter.Text"/>.
+    /// </remarks>
     protected virtual void UpdateTextFormatterText ()
     {
         if (TextFormatter is { })
@@ -165,11 +166,10 @@ public partial class View
     }
 
     /// <summary>
-    ///     Internal API. Sets <see cref="TextFormatter"/>.Size to the current <see cref="Viewport"/> size, adjusted for
-    ///     <see cref="TextFormatter.HotKeySpecifier"/>.
+    ///     Internal API. Sets <see cref="TextFormatter"/>.Width/Height.
     /// </summary>
     /// <remarks>
-    ///     Use this API to set <see cref="TextFormatter.Size"/> when the view has changed such that the
+    ///     Use this API to set <see cref="TextFormatter.Width"/>/Height when the view has changed such that the
     ///     size required to fit the text has changed.
     ///     changes.
     /// </remarks>
@@ -183,11 +183,11 @@ public partial class View
         // Default is to use GetContentSize ().
         Size? size = _contentSize;
 
-        // TODO: This is a hack. Figure out how to move this logic into DimAuto
         // Use _width & _height instead of Width & Height to avoid debug spew
-        DimAuto? widthAuto = _width as DimAuto;
-        DimAuto? heightAuto = _height as DimAuto;
-        if ((widthAuto is { } && widthAuto.Style.FastHasFlags (DimAutoStyle.Text)))
+        var widthAuto = _width as DimAuto;
+        var heightAuto = _height as DimAuto;
+
+        if (widthAuto is { } && widthAuto.Style.FastHasFlags (DimAutoStyle.Text))
         {
             TextFormatter.Width = null;
         }
@@ -199,7 +199,7 @@ public partial class View
             }
         }
 
-        if ((heightAuto is { } && heightAuto.Style.FastHasFlags (DimAutoStyle.Text)))
+        if (heightAuto is { } && heightAuto.Style.FastHasFlags (DimAutoStyle.Text))
         {
             TextFormatter.Height = null;
         }
@@ -212,6 +212,15 @@ public partial class View
         }
     }
 
+    /// <summary>
+    ///     Initializes the Text of the View. Called by the constructor.
+    /// </summary>
+    private void SetupText ()
+    {
+        Text = string.Empty;
+        TextDirection = TextDirection.LeftRight_TopBottom;
+    }
+
     private void UpdateTextDirection (TextDirection newDirection)
     {
         bool directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction) != TextFormatter.IsHorizontalDirection (newDirection);