#nullable enable 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; /// /// 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 (); } } /// /// 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; /// /// 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 Alignment 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 direction. 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 vertical text alignment. public virtual Alignment 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; } } /// /// 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 () { // View subclasses can override UpdateTextFormatterText to modify the Text it holds (e.g. Checkbox and Button). // We need to ensure TextFormatter is accurate by calling it here. UpdateTextFormatterText (); // Default is to use GetContentSize (). var size = GetContentSize (); // TODO: This is a hack. Figure out how to move this into DimDimAuto // 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)) || (heightAuto is { } && heightAuto.Style.FastHasFlags (DimAutoStyle.Text))) { // BUGBUG: This ignores wordwrap and other formatting options. size = TextFormatter.GetAutoSize (); if (widthAuto is null || !widthAuto.Style.FastHasFlags (DimAutoStyle.Text)) { size.Width = GetContentSize ().Width; } if (heightAuto is null || !heightAuto.Style.FastHasFlags (DimAutoStyle.Text)) { size.Height = GetContentSize ().Height; } } TextFormatter.Size = size; } private void UpdateTextDirection (TextDirection newDirection) { bool directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction) != TextFormatter.IsHorizontalDirection (newDirection); TextFormatter.Direction = newDirection; UpdateTextFormatterText (); if (directionChanged) { OnResizeNeeded (); } SetTextFormatterSize (); SetNeedsDisplay (); } }