using static Terminal.Gui.SpinnerStyle; namespace Terminal.Gui; public partial class View { private string _text; /// /// Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved /// or not when is enabled. /// If trailing spaces at the end of wrapped lines will be removed when /// is formatted for display. The default is . /// public virtual bool PreserveTrailingSpaces { get => TextFormatter.PreserveTrailingSpaces; set { if (TextFormatter.PreserveTrailingSpaces != value) { TextFormatter.PreserveTrailingSpaces = value; TextFormatter.NeedsFormat = true; } } } /// /// The text displayed by the . /// /// /// /// The text will be drawn before any subviews are drawn. /// /// /// The text will be drawn starting at the view origin (0, 0) and will be formatted according /// to and . /// /// /// 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. /// When the text changes, the is fired. /// 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); } } /// /// Called when the has changed. Fires the event. /// /// /// public void OnTextChanged (string oldValue, string newValue) { TextChanged?.Invoke (this, new StateEventArgs (oldValue, newValue)); } /// /// Text changed event, raised when the text has changed. /// public event EventHandler> TextChanged; /// /// 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. /// /// The text alignment. public virtual TextAlignment TextAlignment { get => TextFormatter.Alignment; set { TextFormatter.Alignment = value; UpdateTextFormatterText (); OnResizeNeeded (); } } /// /// 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. /// /// The text alignment. public virtual TextDirection TextDirection { get => TextFormatter.Direction; set { UpdateTextDirection (value); TextFormatter.Direction = value; } } /// /// Gets or sets the used to format . /// public TextFormatter TextFormatter { get; init; } = new () { }; /// /// Gets or sets how the View's is aligned vertically when drawn. Changing this property will /// redisplay /// the . /// /// /// or are using , the will be adjusted to fit the text. /// /// The text alignment. public virtual VerticalTextAlignment VerticalTextAlignment { get => TextFormatter.VerticalAlignment; set { TextFormatter.VerticalAlignment = value; SetNeedsDisplay (); } } /// /// Can be overridden if the has /// different format than the default. /// protected virtual void UpdateTextFormatterText () { if (TextFormatter is { }) { TextFormatter.Text = _text; } } /// /// Gets the dimensions required for ignoring a . /// /// internal Size GetSizeNeededForTextWithoutHotKey () { return new Size ( TextFormatter.Size.Width - TextFormatter.GetHotKeySpecifierLength (), TextFormatter.Size.Height - TextFormatter.GetHotKeySpecifierLength (false)); } /// /// Internal API. Sets .Size to the current size, adjusted for /// . /// /// /// Use this API to set when the view has changed such that the /// size required to fit the text has changed. /// changes. /// /// 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.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 == Size.Empty ? null : TextFormatter.Size; return; } TextFormatter.AutoSize = false; TextFormatter.Size = new Size (ContentSize.GetValueOrDefault ().Width, ContentSize.GetValueOrDefault ().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 /// /// Gets or sets a flag that determines whether the View will be automatically resized to fit the /// within . /// /// The default is . Set to to turn on AutoSize. If /// then and will be used if can /// fit; if won't fit the view will be resized as needed. /// /// /// If is set to then and /// will be changed to if they are not already. /// /// /// If is set to then and /// will left unchanged. /// /// [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.GetValueOrDefault ().Height; _width = ContentSize.GetValueOrDefault ().Width; // Force ContentSize to be reset to Viewport _contentSize = null; OnResizeNeeded (); } } } #endregion AutoSize }