ViewText.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #nullable enable
  2. using static Unix.Terminal.Curses;
  3. namespace Terminal.Gui;
  4. public partial class View
  5. {
  6. /// <summary>
  7. /// Initializes the Text of the View. Called by the constructor.
  8. /// </summary>
  9. private void SetupText ()
  10. {
  11. Text = string.Empty;
  12. TextDirection = TextDirection.LeftRight_TopBottom;
  13. }
  14. private string _text;
  15. /// <summary>
  16. /// Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
  17. /// or not when <see cref="TextFormatter.WordWrap"/> is enabled.
  18. /// If <see langword="true"/> trailing spaces at the end of wrapped lines will be removed when
  19. /// <see cref="Text"/> is formatted for display. The default is <see langword="false"/>.
  20. /// </summary>
  21. public virtual bool PreserveTrailingSpaces
  22. {
  23. get => TextFormatter.PreserveTrailingSpaces;
  24. set
  25. {
  26. if (TextFormatter.PreserveTrailingSpaces != value)
  27. {
  28. TextFormatter.PreserveTrailingSpaces = value;
  29. TextFormatter.NeedsFormat = true;
  30. }
  31. }
  32. }
  33. /// <summary>
  34. /// The text displayed by the <see cref="View"/>.
  35. /// </summary>
  36. /// <remarks>
  37. /// <para>
  38. /// The text will be drawn before any subviews are drawn.
  39. /// </para>
  40. /// <para>
  41. /// The text will be drawn starting at the view origin (0, 0) and will be formatted according
  42. /// to <see cref="TextAlignment"/> and <see cref="TextDirection"/>.
  43. /// </para>
  44. /// <para>
  45. /// The text will word-wrap to additional lines if it does not fit horizontally. If <see cref="GetContentSize ()"/>'s height
  46. /// is 1, the text will be clipped.
  47. /// </para>
  48. /// <para>If <see cref="View.Width"/> or <see cref="View.Height"/> are using <see cref="DimAutoStyle.Text"/>,
  49. /// the <see cref="GetContentSize ()"/> will be adjusted to fit the text.</para>
  50. /// <para>When the text changes, the <see cref="TextChanged"/> is fired.</para>
  51. /// </remarks>
  52. public virtual string Text
  53. {
  54. get => _text;
  55. set
  56. {
  57. string old = _text;
  58. _text = value;
  59. UpdateTextFormatterText ();
  60. OnResizeNeeded ();
  61. #if DEBUG
  62. if (_text is { } && string.IsNullOrEmpty (Id))
  63. {
  64. Id = _text;
  65. }
  66. #endif
  67. OnTextChanged ();
  68. }
  69. }
  70. /// <summary>
  71. /// Called when the <see cref="Text"/> has changed. Fires the <see cref="TextChanged"/> event.
  72. /// </summary>
  73. public void OnTextChanged ()
  74. {
  75. TextChanged?.Invoke (this, EventArgs.Empty);
  76. }
  77. /// <summary>
  78. /// Text changed event, raised when the text has changed.
  79. /// </summary>
  80. public event EventHandler? TextChanged;
  81. /// <summary>
  82. /// Gets or sets how the View's <see cref="Text"/> is aligned horizontally when drawn. Changing this property will
  83. /// redisplay the <see cref="View"/>.
  84. /// </summary>
  85. /// <remarks>
  86. /// <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>
  87. /// </remarks>
  88. /// <value>The text alignment.</value>
  89. public virtual Alignment TextAlignment
  90. {
  91. get => TextFormatter.Alignment;
  92. set
  93. {
  94. TextFormatter.Alignment = value;
  95. UpdateTextFormatterText ();
  96. OnResizeNeeded ();
  97. }
  98. }
  99. /// <summary>
  100. /// Gets or sets the direction of the View's <see cref="Text"/>. Changing this property will redisplay the
  101. /// <see cref="View"/>.
  102. /// </summary>
  103. /// <remarks>
  104. /// <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>
  105. /// </remarks>
  106. /// <value>The text direction.</value>
  107. public virtual TextDirection TextDirection
  108. {
  109. get => TextFormatter.Direction;
  110. set
  111. {
  112. UpdateTextDirection (value);
  113. TextFormatter.Direction = value;
  114. }
  115. }
  116. /// <summary>
  117. /// Gets or sets the <see cref="Gui.TextFormatter"/> used to format <see cref="Text"/>.
  118. /// </summary>
  119. public TextFormatter TextFormatter { get; init; } = new () { };
  120. /// <summary>
  121. /// Gets or sets how the View's <see cref="Text"/> is aligned vertically when drawn. Changing this property will
  122. /// redisplay
  123. /// the <see cref="View"/>.
  124. /// </summary>
  125. /// <remarks>
  126. /// <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>
  127. /// </remarks>
  128. /// <value>The vertical text alignment.</value>
  129. public virtual Alignment VerticalTextAlignment
  130. {
  131. get => TextFormatter.VerticalAlignment;
  132. set
  133. {
  134. TextFormatter.VerticalAlignment = value;
  135. SetNeedsDisplay ();
  136. }
  137. }
  138. /// <summary>
  139. /// Can be overridden if the <see cref="Terminal.Gui.TextFormatter.Text"/> has
  140. /// different format than the default.
  141. /// </summary>
  142. protected virtual void UpdateTextFormatterText ()
  143. {
  144. if (TextFormatter is { })
  145. {
  146. TextFormatter.Text = _text;
  147. }
  148. }
  149. /// <summary>
  150. /// Internal API. Sets <see cref="TextFormatter"/>.Size to the current <see cref="Viewport"/> size, adjusted for
  151. /// <see cref="TextFormatter.HotKeySpecifier"/>.
  152. /// </summary>
  153. /// <remarks>
  154. /// Use this API to set <see cref="TextFormatter.Size"/> when the view has changed such that the
  155. /// size required to fit the text has changed.
  156. /// changes.
  157. /// </remarks>
  158. /// <returns></returns>
  159. internal void SetTextFormatterSize ()
  160. {
  161. // View subclasses can override UpdateTextFormatterText to modify the Text it holds (e.g. Checkbox and Button).
  162. // We need to ensure TextFormatter is accurate by calling it here.
  163. UpdateTextFormatterText ();
  164. // Default is to use GetContentSize ().
  165. var size = GetContentSize ();
  166. // TODO: This is a hack. Figure out how to move this into DimDimAuto
  167. // Use _width & _height instead of Width & Height to avoid debug spew
  168. DimAuto? widthAuto = _width as DimAuto;
  169. DimAuto? heightAuto = _height as DimAuto;
  170. if ((widthAuto is { } && widthAuto.Style.FastHasFlags (DimAutoStyle.Text))
  171. || (heightAuto is { } && heightAuto.Style.FastHasFlags (DimAutoStyle.Text)))
  172. {
  173. // BUGBUG: This ignores wordwrap and other formatting options.
  174. size = TextFormatter.GetAutoSize ();
  175. if (widthAuto is null || !widthAuto.Style.FastHasFlags (DimAutoStyle.Text))
  176. {
  177. size.Width = GetContentSize ().Width;
  178. }
  179. if (heightAuto is null || !heightAuto.Style.FastHasFlags (DimAutoStyle.Text))
  180. {
  181. size.Height = GetContentSize ().Height;
  182. }
  183. }
  184. TextFormatter.Size = size;
  185. }
  186. private void UpdateTextDirection (TextDirection newDirection)
  187. {
  188. bool directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction) != TextFormatter.IsHorizontalDirection (newDirection);
  189. TextFormatter.Direction = newDirection;
  190. UpdateTextFormatterText ();
  191. if (directionChanged)
  192. {
  193. OnResizeNeeded ();
  194. }
  195. SetTextFormatterSize ();
  196. SetNeedsDisplay ();
  197. }
  198. }