ViewText.cs 7.9 KB

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