123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- using static Terminal.Gui.SpinnerStyle;
- namespace Terminal.Gui;
- public partial class View
- {
- private string _text;
- /// <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.
- /// If <see langword="true"/> trailing spaces at the end of wrapped lines will be removed when
- /// <see cref="Text"/> is formatted for display. The default is <see langword="false"/>.
- /// </summary>
- public virtual bool PreserveTrailingSpaces
- {
- get => TextFormatter.PreserveTrailingSpaces;
- set
- {
- if (TextFormatter.PreserveTrailingSpaces != value)
- {
- TextFormatter.PreserveTrailingSpaces = value;
- TextFormatter.NeedsFormat = true;
- }
- }
- }
- /// <summary>
- /// The text displayed by the <see cref="View"/>.
- /// </summary>
- /// <remarks>
- /// <para>
- /// The text will be drawn before any subviews are drawn.
- /// </para>
- /// <para>
- /// The text will be drawn starting at the view origin (0, 0) and will be formatted according
- /// 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="ContentSize"/>'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="Dim.DimAutoStyle.Text"/>,
- /// the <see cref="ContentSize"/> 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
- {
- get => _text;
- set
- {
- string old = _text;
- _text = value;
- UpdateTextFormatterText ();
- OnResizeNeeded ();
- #if DEBUG
- if (_text is { } && string.IsNullOrEmpty (Id))
- {
- Id = _text;
- }
- #endif
- OnTextChanged (old, Text);
- }
- }
- /// <summary>
- /// Called when the <see cref="Text"/> has changed. Fires the <see cref="TextChanged"/> event.
- /// </summary>
- /// <param name="oldValue"></param>
- /// <param name="newValue"></param>
- public void OnTextChanged (string oldValue, string newValue)
- {
- TextChanged?.Invoke (this, new StateEventArgs<string> (oldValue, newValue));
- }
- /// <summary>
- /// Text changed event, raised when the text has changed.
- /// </summary>
- public event EventHandler<StateEventArgs<string>> TextChanged;
- /// <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="Dim.DimAutoStyle.Text"/>, the <see cref="ContentSize"/> will be adjusted to fit the text.</para>
- /// </remarks>
- /// <value>The text alignment.</value>
- public virtual TextAlignment TextAlignment
- {
- get => TextFormatter.Alignment;
- set
- {
- TextFormatter.Alignment = value;
- UpdateTextFormatterText ();
- OnResizeNeeded ();
- }
- }
- /// <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="Dim.DimAutoStyle.Text"/>, the <see cref="ContentSize"/> will be adjusted to fit the text.</para>
- /// </remarks>
- /// <value>The text alignment.</value>
- public virtual TextDirection TextDirection
- {
- get => TextFormatter.Direction;
- set
- {
- UpdateTextDirection (value);
- TextFormatter.Direction = value;
- }
- }
- /// <summary>
- /// Gets or sets the <see cref="Gui.TextFormatter"/> used to format <see cref="Text"/>.
- /// </summary>
- 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
- /// redisplay
- /// the <see cref="View"/>.
- /// </summary>
- /// <remarks>
- /// <para> <see cref="View.Width"/> or <see cref="View.Height"/> are using <see cref="Dim.DimAutoStyle.Text"/>, the <see cref="ContentSize"/> will be adjusted to fit the text.</para>
- /// </remarks>
- /// <value>The text alignment.</value>
- public virtual VerticalTextAlignment VerticalTextAlignment
- {
- get => TextFormatter.VerticalAlignment;
- set
- {
- TextFormatter.VerticalAlignment = value;
- SetNeedsDisplay ();
- }
- }
- /// <summary>
- /// Can be overridden if the <see cref="Terminal.Gui.TextFormatter.Text"/> has
- /// different format than the default.
- /// </summary>
- protected virtual void UpdateTextFormatterText ()
- {
- if (TextFormatter is { })
- {
- TextFormatter.Text = _text;
- }
- }
- /// <summary>
- /// Gets the dimensions required for <see cref="Text"/> ignoring a <see cref="TextFormatter.HotKeySpecifier"/>.
- /// </summary>
- /// <returns></returns>
- internal Size GetSizeNeededForTextWithoutHotKey ()
- {
- return new Size (
- TextFormatter.Size.Width - TextFormatter.GetHotKeySpecifierLength (),
- TextFormatter.Size.Height - TextFormatter.GetHotKeySpecifierLength (false));
- }
- /// <summary>
- /// Internal API. Sets <see cref="TextFormatter"/>.Size to the current <see cref="Viewport"/> size, adjusted for
- /// <see cref="TextFormatter.HotKeySpecifier"/>.
- /// </summary>
- /// <remarks>
- /// Use this API to set <see cref="TextFormatter.Size"/> when the view has changed such that the
- /// size required to fit the text has changed.
- /// changes.
- /// </remarks>
- /// <returns></returns>
- internal void SetTextFormatterSize ()
- {
- UpdateTextFormatterText ();
- // 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))
- {
- // This updates TextFormatter.Size to the text size
- TextFormatter.AutoSize = true;
- // Whenever DimAutoStyle.Text is set, ContentSize will match TextFormatter.Size.
- ContentSize = TextFormatter.Size;
- return;
- }
- TextFormatter.AutoSize = false;
- TextFormatter.Size = new Size (ContentSize.Width, ContentSize.Height);
- }
- private void UpdateTextDirection (TextDirection newDirection)
- {
- bool directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction) != TextFormatter.IsHorizontalDirection (newDirection);
- TextFormatter.Direction = newDirection;
- UpdateTextFormatterText ();
- if (directionChanged)
- {
- OnResizeNeeded ();
- }
- SetTextFormatterSize ();
- SetNeedsDisplay ();
- }
- #region AutoSize
- /// <summary>
- /// Gets or sets a flag that determines whether the View will be automatically resized to fit the <see cref="Text"/>
- /// within <see cref="Viewport"/>.
- /// <para>
- /// The default is <see langword="false"/>. Set to <see langword="true"/> to turn on AutoSize. If
- /// <see langword="true"/> then <see cref="Width"/> and <see cref="Height"/> will be used if <see cref="Text"/> can
- /// fit; if <see cref="Text"/> won't fit the view will be resized as needed.
- /// </para>
- /// <para>
- /// If <see cref="AutoSize"/> is set to <see langword="true"/> then <see cref="Width"/> and <see cref="Height"/>
- /// will be changed to <see cref="Dim.DimAbsolute"/> if they are not already.
- /// </para>
- /// <para>
- /// If <see cref="AutoSize"/> is set to <see langword="false"/> then <see cref="Width"/> and <see cref="Height"/>
- /// will left unchanged.
- /// </para>
- /// </summary>
- [ObsoleteAttribute ("Use Dim.Auto instead.", false)]
- public virtual bool AutoSize
- {
- get => _height is Dim.DimAuto && _width is Dim.DimAuto;
- set
- {
- TextFormatter.AutoSize = value;
- // BUGBUG: This is all a hack until AutoSize is removed
- if (value)
- {
- UpdateTextFormatterText ();
- if (IsInitialized)
- {
- Height = Dim.Auto (Dim.DimAutoStyle.Text);
- Width = Dim.Auto (Dim.DimAutoStyle.Text);
- }
- else
- {
- _height = Dim.Auto (Dim.DimAutoStyle.Text);
- _width = Dim.Auto (Dim.DimAutoStyle.Text);
- OnResizeNeeded ();
- }
- }
- else
- {
- _height = ContentSize.Height;
- _width = ContentSize.Width;
- // Force ContentSize to be reset to Viewport
- _contentSize = Size.Empty;
- OnResizeNeeded ();
- }
- }
- }
- #endregion AutoSize
- }
|