ViewText.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 ();
  67. }
  68. }
  69. /// <summary>
  70. /// Called when the <see cref="Text"/> has changed. Fires the <see cref="TextChanged"/> event.
  71. /// </summary>
  72. public void OnTextChanged ()
  73. {
  74. TextChanged?.Invoke (this, EventArgs.Empty);
  75. }
  76. /// <summary>
  77. /// Text changed event, raised when the text has changed.
  78. /// </summary>
  79. public event EventHandler<EventArgs> TextChanged;
  80. /// <summary>
  81. /// Gets or sets how the View's <see cref="Text"/> is aligned horizontally when drawn. Changing this property will
  82. /// redisplay the <see cref="View"/>.
  83. /// </summary>
  84. /// <remarks>
  85. /// <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>
  86. /// </remarks>
  87. /// <value>The text alignment.</value>
  88. public virtual Alignment TextAlignment
  89. {
  90. get => TextFormatter.Alignment;
  91. set
  92. {
  93. TextFormatter.Alignment = value;
  94. UpdateTextFormatterText ();
  95. OnResizeNeeded ();
  96. }
  97. }
  98. /// <summary>
  99. /// Gets or sets the direction of the View's <see cref="Text"/>. Changing this property will redisplay the
  100. /// <see cref="View"/>.
  101. /// </summary>
  102. /// <remarks>
  103. /// <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>
  104. /// </remarks>
  105. /// <value>The text direction.</value>
  106. public virtual TextDirection TextDirection
  107. {
  108. get => TextFormatter.Direction;
  109. set
  110. {
  111. UpdateTextDirection (value);
  112. TextFormatter.Direction = value;
  113. }
  114. }
  115. /// <summary>
  116. /// Gets or sets the <see cref="Gui.TextFormatter"/> used to format <see cref="Text"/>.
  117. /// </summary>
  118. public TextFormatter TextFormatter { get; init; } = new () { };
  119. /// <summary>
  120. /// Gets or sets how the View's <see cref="Text"/> is aligned vertically when drawn. Changing this property will
  121. /// redisplay
  122. /// the <see cref="View"/>.
  123. /// </summary>
  124. /// <remarks>
  125. /// <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>
  126. /// </remarks>
  127. /// <value>The vertical text alignment.</value>
  128. public virtual Alignment VerticalTextAlignment
  129. {
  130. get => TextFormatter.VerticalAlignment;
  131. set
  132. {
  133. TextFormatter.VerticalAlignment = value;
  134. SetNeedsDisplay ();
  135. }
  136. }
  137. /// <summary>
  138. /// Can be overridden if the <see cref="Terminal.Gui.TextFormatter.Text"/> has
  139. /// different format than the default.
  140. /// </summary>
  141. protected virtual void UpdateTextFormatterText ()
  142. {
  143. if (TextFormatter is { })
  144. {
  145. TextFormatter.Text = _text;
  146. }
  147. }
  148. /// <summary>
  149. /// Gets the dimensions required for <see cref="Text"/> ignoring a <see cref="TextFormatter.HotKeySpecifier"/>.
  150. /// </summary>
  151. /// <returns></returns>
  152. internal Size GetSizeNeededForTextWithoutHotKey ()
  153. {
  154. return new Size (
  155. TextFormatter.Size.Width - TextFormatter.GetHotKeySpecifierLength (),
  156. TextFormatter.Size.Height - TextFormatter.GetHotKeySpecifierLength (false));
  157. }
  158. /// <summary>
  159. /// Internal API. Sets <see cref="TextFormatter"/>.Size to the current <see cref="Viewport"/> size, adjusted for
  160. /// <see cref="TextFormatter.HotKeySpecifier"/>.
  161. /// </summary>
  162. /// <remarks>
  163. /// Use this API to set <see cref="TextFormatter.Size"/> when the view has changed such that the
  164. /// size required to fit the text has changed.
  165. /// changes.
  166. /// </remarks>
  167. /// <returns></returns>
  168. internal void SetTextFormatterSize ()
  169. {
  170. // View subclasses can override UpdateTextFormatterText to modify the Text it holds (e.g. Checkbox and Button).
  171. // We need to ensure TextFormatter is accurate by calling it here.
  172. UpdateTextFormatterText ();
  173. // Default is to use GetContentSize ().
  174. var size = GetContentSize ();
  175. // TODO: This is a hack. Figure out how to move this into DimDimAuto
  176. // Use _width & _height instead of Width & Height to avoid debug spew
  177. DimAuto widthAuto = _width as DimAuto;
  178. DimAuto heightAuto = _height as DimAuto;
  179. if ((widthAuto is { } && widthAuto.Style.FastHasFlags (DimAutoStyle.Text))
  180. || (heightAuto is { } && heightAuto.Style.FastHasFlags (DimAutoStyle.Text)))
  181. {
  182. size = TextFormatter.GetAutoSize ();
  183. if (widthAuto is null || !widthAuto.Style.FastHasFlags (DimAutoStyle.Text))
  184. {
  185. size.Width = GetContentSize ().Width;
  186. }
  187. if (heightAuto is null || !heightAuto.Style.FastHasFlags (DimAutoStyle.Text))
  188. {
  189. size.Height = GetContentSize ().Height;
  190. }
  191. }
  192. TextFormatter.Size = size;
  193. }
  194. private void UpdateTextDirection (TextDirection newDirection)
  195. {
  196. bool directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction) != TextFormatter.IsHorizontalDirection (newDirection);
  197. TextFormatter.Direction = newDirection;
  198. UpdateTextFormatterText ();
  199. if (directionChanged)
  200. {
  201. OnResizeNeeded ();
  202. }
  203. SetTextFormatterSize ();
  204. SetNeedsDisplay ();
  205. }
  206. }