ViewText.cs 12 KB

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