ViewText.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Terminal.Gui;
  4. public partial class View {
  5. string _text;
  6. /// <summary>
  7. /// The text displayed by the <see cref="View"/>.
  8. /// </summary>
  9. /// <remarks>
  10. /// <para>
  11. /// The text will be drawn before any subviews are drawn.
  12. /// </para>
  13. /// <para>
  14. /// The text will be drawn starting at the view origin (0, 0) and will be formatted according
  15. /// to <see cref="TextAlignment"/> and <see cref="TextDirection"/>.
  16. /// </para>
  17. /// <para>
  18. /// The text will word-wrap to additional lines if it does not fit horizontally. If <see cref="Bounds"/>'s height
  19. /// is 1, the text will be clipped.
  20. /// </para>
  21. /// <para>
  22. /// Set the <see cref="HotKeySpecifier"/> to enable hotkey support. To disable hotkey support set
  23. /// <see cref="HotKeySpecifier"/> to
  24. /// <c>(Rune)0xffff</c>.
  25. /// </para>
  26. /// <para>
  27. /// If <see cref="AutoSize"/> is <c>true</c>, the <see cref="Bounds"/> will be adjusted to fit the text.
  28. /// </para>
  29. /// </remarks>
  30. public virtual string Text {
  31. get => _text;
  32. set {
  33. _text = value;
  34. SetHotKey ();
  35. UpdateTextFormatterText ();
  36. OnResizeNeeded ();
  37. #if DEBUG
  38. if (_text != null && string.IsNullOrEmpty (Id)) {
  39. Id = _text;
  40. }
  41. #endif
  42. }
  43. }
  44. /// <summary>
  45. /// Gets or sets the <see cref="Gui.TextFormatter"/> used to format <see cref="Text"/>.
  46. /// </summary>
  47. public TextFormatter TextFormatter { get; set; }
  48. /// <summary>
  49. /// Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
  50. /// or not when <see cref="TextFormatter.WordWrap"/> is enabled.
  51. /// If <see langword="true"/> trailing spaces at the end of wrapped lines will be removed when
  52. /// <see cref="Text"/> is formatted for display. The default is <see langword="false"/>.
  53. /// </summary>
  54. public virtual bool PreserveTrailingSpaces {
  55. get => TextFormatter.PreserveTrailingSpaces;
  56. set {
  57. if (TextFormatter.PreserveTrailingSpaces != value) {
  58. TextFormatter.PreserveTrailingSpaces = value;
  59. TextFormatter.NeedsFormat = true;
  60. }
  61. }
  62. }
  63. /// <summary>
  64. /// Gets or sets how the View's <see cref="Text"/> is aligned horizontally when drawn. Changing this property will
  65. /// redisplay the <see cref="View"/>.
  66. /// </summary>
  67. /// <remarks>
  68. /// <para>
  69. /// If <see cref="AutoSize"/> is <c>true</c>, the <see cref="Bounds"/> will be adjusted to fit the text.
  70. /// </para>
  71. /// </remarks>
  72. /// <value>The text alignment.</value>
  73. public virtual TextAlignment TextAlignment {
  74. get => TextFormatter.Alignment;
  75. set {
  76. TextFormatter.Alignment = value;
  77. UpdateTextFormatterText ();
  78. OnResizeNeeded ();
  79. }
  80. }
  81. /// <summary>
  82. /// Gets or sets how the View's <see cref="Text"/> is aligned vertically when drawn. Changing this property will redisplay
  83. /// the <see cref="View"/>.
  84. /// </summary>
  85. /// <remarks>
  86. /// <para>
  87. /// If <see cref="AutoSize"/> is <c>true</c>, the <see cref="Bounds"/> will be adjusted to fit the text.
  88. /// </para>
  89. /// </remarks>
  90. /// <value>The text alignment.</value>
  91. public virtual VerticalTextAlignment VerticalTextAlignment {
  92. get => TextFormatter.VerticalAlignment;
  93. set {
  94. TextFormatter.VerticalAlignment = value;
  95. SetNeedsDisplay ();
  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>
  104. /// If <see cref="AutoSize"/> is <c>true</c>, the <see cref="Bounds"/> will be adjusted to fit the text.
  105. /// </para>
  106. /// </remarks>
  107. /// <value>The text alignment.</value>
  108. public virtual TextDirection TextDirection {
  109. get => TextFormatter.Direction;
  110. set {
  111. UpdateTextDirection (value);
  112. TextFormatter.Direction = value;
  113. }
  114. }
  115. /// <summary>
  116. /// Can be overridden if the <see cref="Terminal.Gui.TextFormatter.Text"/> has
  117. /// different format than the default.
  118. /// </summary>
  119. protected virtual void UpdateTextFormatterText ()
  120. {
  121. if (TextFormatter != null) {
  122. TextFormatter.Text = _text;
  123. }
  124. }
  125. void UpdateTextDirection (TextDirection newDirection)
  126. {
  127. var directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction) != TextFormatter.IsHorizontalDirection (newDirection);
  128. TextFormatter.Direction = newDirection;
  129. var isValidOldAutoSize = AutoSize && IsValidAutoSize (out var _);
  130. UpdateTextFormatterText ();
  131. if (!ValidatePosDim && directionChanged && AutoSize || ValidatePosDim && directionChanged && AutoSize && isValidOldAutoSize) {
  132. OnResizeNeeded ();
  133. } else if (directionChanged && IsAdded) {
  134. ResizeBoundsToFit (Bounds.Size);
  135. // BUGBUG: I think this call is redundant.
  136. SetFrameToFitText ();
  137. } else {
  138. SetFrameToFitText ();
  139. }
  140. SetTextFormatterSize ();
  141. SetNeedsDisplay ();
  142. }
  143. /// <summary>
  144. /// Sets the size of the View to the minimum width or height required to fit <see cref="Text"/>.
  145. /// </summary>
  146. /// <returns>
  147. /// <see langword="true"/> if the size was changed; <see langword="false"/> if <see cref="AutoSize"/> ==
  148. /// <see langword="true"/> or
  149. /// <see cref="Text"/> will not fit.
  150. /// </returns>
  151. /// <remarks>
  152. /// Always returns <see langword="false"/> if <see cref="AutoSize"/> is <see langword="true"/> or
  153. /// if <see cref="Height"/> (Horizontal) or <see cref="Width"/> (Vertical) are not not set or zero.
  154. /// Does not take into account word wrapping.
  155. /// </remarks>
  156. bool SetFrameToFitText ()
  157. {
  158. // BUGBUG: This API is broken - should not assume Frame.Height == Bounds.Height
  159. // <summary>
  160. // Gets the minimum dimensions required to fit the View's <see cref="Text"/>, factoring in <see cref="TextDirection"/>.
  161. // </summary>
  162. // <param name="sizeRequired">The minimum dimensions required.</param>
  163. // <returns><see langword="true"/> if the dimensions fit within the View's <see cref="Bounds"/>, <see langword="false"/> otherwise.</returns>
  164. // <remarks>
  165. // Always returns <see langword="false"/> if <see cref="AutoSize"/> is <see langword="true"/> or
  166. // if <see cref="Height"/> (Horizontal) or <see cref="Width"/> (Vertical) are not not set or zero.
  167. // Does not take into account word wrapping.
  168. // </remarks>
  169. bool GetMinimumSizeOfText (out Size sizeRequired)
  170. {
  171. if (!IsInitialized) {
  172. sizeRequired = new Size (0, 0);
  173. return false;
  174. }
  175. sizeRequired = Bounds.Size;
  176. if (!AutoSize && !string.IsNullOrEmpty (TextFormatter.Text)) {
  177. switch (TextFormatter.IsVerticalDirection (TextDirection)) {
  178. case true:
  179. var colWidth = TextFormatter.GetSumMaxCharWidth (new List<string> { TextFormatter.Text }, 0, 1);
  180. // TODO: v2 - This uses frame.Width; it should only use Bounds
  181. if (_frame.Width < colWidth &&
  182. (Width == null ||
  183. Bounds.Width >= 0 &&
  184. Width is Dim.DimAbsolute &&
  185. Width.Anchor (0) >= 0 &&
  186. Width.Anchor (0) < colWidth)) {
  187. sizeRequired = new Size (colWidth, Bounds.Height);
  188. return true;
  189. }
  190. break;
  191. default:
  192. if (_frame.Height < 1 &&
  193. (Height == null ||
  194. Height is Dim.DimAbsolute &&
  195. Height.Anchor (0) == 0)) {
  196. sizeRequired = new Size (Bounds.Width, 1);
  197. return true;
  198. }
  199. break;
  200. }
  201. }
  202. return false;
  203. }
  204. if (GetMinimumSizeOfText (out var size)) {
  205. _frame = new Rect (_frame.Location, size);
  206. return true;
  207. }
  208. return false;
  209. }
  210. /// <summary>
  211. /// Gets the width or height of the <see cref="TextFormatter.HotKeySpecifier"/> characters
  212. /// in the <see cref="Text"/> property.
  213. /// </summary>
  214. /// <remarks>
  215. /// Only the first HotKey specifier found in <see cref="Text"/> is supported.
  216. /// </remarks>
  217. /// <param name="isWidth">
  218. /// If <see langword="true"/> (the default) the width required for the HotKey specifier is returned. Otherwise the height
  219. /// is returned.
  220. /// </param>
  221. /// <returns>
  222. /// The number of characters required for the <see cref="TextFormatter.HotKeySpecifier"/>. If the text
  223. /// direction specified
  224. /// by <see cref="TextDirection"/> does not match the <paramref name="isWidth"/> parameter, <c>0</c> is returned.
  225. /// </returns>
  226. public int GetHotKeySpecifierLength (bool isWidth = true)
  227. {
  228. if (isWidth) {
  229. return TextFormatter.IsHorizontalDirection (TextDirection) &&
  230. TextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  231. ? Math.Max (HotKeySpecifier.GetColumns (), 0) : 0;
  232. }
  233. return TextFormatter.IsVerticalDirection (TextDirection) &&
  234. TextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  235. ? Math.Max (HotKeySpecifier.GetColumns (), 0) : 0;
  236. }
  237. /// <summary>
  238. /// Gets the dimensions required for <see cref="Text"/> ignoring a <see cref="TextFormatter.HotKeySpecifier"/>.
  239. /// </summary>
  240. /// <returns></returns>
  241. internal Size GetSizeNeededForTextWithoutHotKey () => new (TextFormatter.Size.Width - GetHotKeySpecifierLength (),
  242. TextFormatter.Size.Height - GetHotKeySpecifierLength (false));
  243. /// <summary>
  244. /// Sets <see cref="TextFormatter"/>.Size to the current <see cref="Bounds"/> size, adjusted for
  245. /// <see cref="TextFormatter.HotKeySpecifier"/>.
  246. /// </summary>
  247. /// <remarks>
  248. /// Use this API to set <see cref="TextFormatter.Size"/> when the view has changed such that the
  249. /// size required to fit the text has changed.
  250. /// changes.
  251. /// </remarks>
  252. /// <returns></returns>
  253. internal void SetTextFormatterSize ()
  254. {
  255. if (!IsInitialized) {
  256. TextFormatter.Size = Size.Empty;
  257. }
  258. if (string.IsNullOrEmpty (TextFormatter.Text)) {
  259. TextFormatter.Size = Bounds.Size;
  260. }
  261. TextFormatter.Size = new Size (Bounds.Size.Width + GetHotKeySpecifierLength (),
  262. Bounds.Size.Height + GetHotKeySpecifierLength (false));
  263. }
  264. /// <summary>
  265. /// Gets the Frame dimensions required to fit <see cref="Text"/> within <see cref="Bounds"/> using the text
  266. /// <see cref="Direction"/> specified by the
  267. /// <see cref="TextFormatter"/> property and accounting for any <see cref="HotKeySpecifier"/> characters.
  268. /// </summary>
  269. /// <returns>The <see cref="Size"/> the <see cref="Frame"/> needs to be set to fit the text.</returns>
  270. public Size GetAutoSize ()
  271. {
  272. var x = 0;
  273. var y = 0;
  274. if (IsInitialized) {
  275. x = Bounds.X;
  276. y = Bounds.Y;
  277. }
  278. var rect = TextFormatter.CalcRect (x, y, TextFormatter.Text, TextFormatter.Direction);
  279. var newWidth = rect.Size.Width - GetHotKeySpecifierLength () + Margin.Thickness.Horizontal + Border.Thickness.Horizontal + Padding.Thickness.Horizontal;
  280. var newHeight = rect.Size.Height - GetHotKeySpecifierLength (false) + Margin.Thickness.Vertical + Border.Thickness.Vertical + Padding.Thickness.Vertical;
  281. return new Size (newWidth, newHeight);
  282. }
  283. bool IsValidAutoSize (out Size autoSize)
  284. {
  285. var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  286. autoSize = new Size (rect.Size.Width - GetHotKeySpecifierLength (),
  287. rect.Size.Height - GetHotKeySpecifierLength (false));
  288. return !(ValidatePosDim && (!(Width is Dim.DimAbsolute) || !(Height is Dim.DimAbsolute)) ||
  289. _frame.Size.Width != rect.Size.Width - GetHotKeySpecifierLength () ||
  290. _frame.Size.Height != rect.Size.Height - GetHotKeySpecifierLength (false));
  291. }
  292. bool IsValidAutoSizeWidth (Dim width)
  293. {
  294. var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  295. var dimValue = width.Anchor (0);
  296. return !(ValidatePosDim && !(width is Dim.DimAbsolute) || dimValue != rect.Size.Width - GetHotKeySpecifierLength ());
  297. }
  298. bool IsValidAutoSizeHeight (Dim height)
  299. {
  300. var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  301. var dimValue = height.Anchor (0);
  302. return !(ValidatePosDim && !(height is Dim.DimAbsolute) || dimValue != rect.Size.Height - GetHotKeySpecifierLength (false));
  303. }
  304. }