From 48eba3bc55ee9f08e2f460202baaababb817418b Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 18 Jul 2024 15:32:51 -0600 Subject: [PATCH] Code cleanup --- Terminal.Gui/View/ViewText.cs | 91 +++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/Terminal.Gui/View/ViewText.cs b/Terminal.Gui/View/ViewText.cs index a620e6a2b..9ae02d2db 100644 --- a/Terminal.Gui/View/ViewText.cs +++ b/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 { - /// - /// Initializes the Text of the View. Called by the constructor. - /// - private void SetupText () - { - Text = string.Empty; - TextDirection = TextDirection.LeftRight_TopBottom; - } - private string _text; + /// + /// Called when the has changed. Fires the event. + /// + public void OnTextChanged () { TextChanged?.Invoke (this, EventArgs.Empty); } + + // TODO: Make this non-virtual. Nobody overrides it. /// /// Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved /// or not when is enabled. @@ -49,11 +43,14 @@ public partial class View /// to and . /// /// - /// The text will word-wrap to additional lines if it does not fit horizontally. If 's height + /// The text will word-wrap to additional lines if it does not fit horizontally. If + /// 's height /// is 1, the text will be clipped. /// - /// If or are using , - /// the will be adjusted to fit the text. + /// + /// If or are using , + /// the will be adjusted to fit the text. + /// /// When the text changes, the is fired. /// public virtual string Text @@ -76,26 +73,16 @@ public partial class View } } - /// - /// Called when the has changed. Fires the event. - /// - public void OnTextChanged () - { - TextChanged?.Invoke (this, EventArgs.Empty); - } - - /// - /// Text changed event, raised when the text has changed. - /// - public event EventHandler? TextChanged; - // TODO: Make this non-virtual. Nobody overrides it. /// /// Gets or sets how the View's is aligned horizontally when drawn. Changing this property will /// redisplay the . /// /// - /// or are using , the will be adjusted to fit the text. + /// + /// or are using , the + /// will be adjusted to fit the text. + /// /// /// The text alignment. public virtual Alignment TextAlignment @@ -109,13 +96,21 @@ public partial class View } } + /// + /// Text changed event, raised when the text has changed. + /// + public event EventHandler? TextChanged; + // TODO: Make this non-virtual. Nobody overrides it. /// /// Gets or sets the direction of the View's . Changing this property will redisplay the /// . /// /// - /// or are using , the will be adjusted to fit the text. + /// + /// or are using , the + /// will be adjusted to fit the text. + /// /// /// The text direction. public virtual TextDirection TextDirection @@ -127,7 +122,7 @@ public partial class View /// /// Gets or sets the used to format . /// - public TextFormatter TextFormatter { get; init; } = new () { }; + public TextFormatter TextFormatter { get; init; } = new (); // TODO: Make this non-virtual. Nobody overrides it. /// @@ -136,7 +131,10 @@ public partial class View /// the . /// /// - /// or are using , the will be adjusted to fit the text. + /// + /// or are using , the + /// will be adjusted to fit the text. + /// /// /// The vertical text alignment. 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. /// - /// Can be overridden if the has + /// Can be overridden if the has /// different format than the default. /// + /// + /// Overrides must call base.UpdateTextFormatterText before updating . + /// protected virtual void UpdateTextFormatterText () { if (TextFormatter is { }) @@ -165,11 +166,10 @@ public partial class View } /// - /// Internal API. Sets .Size to the current size, adjusted for - /// . + /// Internal API. Sets .Width/Height. /// /// - /// Use this API to set when the view has changed such that the + /// Use this API to set /Height when the view has changed such that the /// size required to fit the text has changed. /// changes. /// @@ -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 } } + /// + /// Initializes the Text of the View. Called by the constructor. + /// + private void SetupText () + { + Text = string.Empty; + TextDirection = TextDirection.LeftRight_TopBottom; + } + private void UpdateTextDirection (TextDirection newDirection) { bool directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction) != TextFormatter.IsHorizontalDirection (newDirection);