ViewText.cs 8.2 KB

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