ViewText.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. return false;
  178. }
  179. switch (TextFormatter.IsVerticalDirection (TextDirection)) {
  180. case true:
  181. var colWidth = TextFormatter.GetSumMaxCharWidth (new List<string> { TextFormatter.Text }, 0, 1);
  182. // TODO: v2 - This uses frame.Width; it should only use Bounds
  183. if (_frame.Width < colWidth &&
  184. (Width == null ||
  185. Bounds.Width >= 0 &&
  186. Width is Dim.DimAbsolute &&
  187. Width.Anchor (0) >= 0 &&
  188. Width.Anchor (0) < colWidth)) {
  189. sizeRequired = new Size (colWidth, Bounds.Height);
  190. return true;
  191. }
  192. break;
  193. default:
  194. if (_frame.Height < 1 &&
  195. (Height == null ||
  196. Height is Dim.DimAbsolute &&
  197. Height.Anchor (0) == 0)) {
  198. sizeRequired = new Size (Bounds.Width, 1);
  199. return true;
  200. }
  201. break;
  202. }
  203. return false;
  204. }
  205. if (GetMinimumSizeOfText (out var size)) {
  206. // TODO: This is a hack.
  207. //_width = size.Width;
  208. //_height = size.Height;
  209. _frame = new Rect (_frame.Location, size);
  210. //throw new InvalidOperationException ("This is a hack.");
  211. return true;
  212. }
  213. return false;
  214. }
  215. /// <summary>
  216. /// Gets the width or height of the <see cref="TextFormatter.HotKeySpecifier"/> characters
  217. /// in the <see cref="Text"/> property.
  218. /// </summary>
  219. /// <remarks>
  220. /// Only the first HotKey specifier found in <see cref="Text"/> is supported.
  221. /// </remarks>
  222. /// <param name="isWidth">
  223. /// If <see langword="true"/> (the default) the width required for the HotKey specifier is returned. Otherwise the height
  224. /// is returned.
  225. /// </param>
  226. /// <returns>
  227. /// The number of characters required for the <see cref="TextFormatter.HotKeySpecifier"/>. If the text
  228. /// direction specified
  229. /// by <see cref="TextDirection"/> does not match the <paramref name="isWidth"/> parameter, <c>0</c> is returned.
  230. /// </returns>
  231. public int GetHotKeySpecifierLength (bool isWidth = true)
  232. {
  233. if (isWidth) {
  234. return TextFormatter.IsHorizontalDirection (TextDirection) &&
  235. TextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  236. ? Math.Max (HotKeySpecifier.GetColumns (), 0) : 0;
  237. }
  238. return TextFormatter.IsVerticalDirection (TextDirection) &&
  239. TextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  240. ? Math.Max (HotKeySpecifier.GetColumns (), 0) : 0;
  241. }
  242. /// <summary>
  243. /// Gets the dimensions required for <see cref="Text"/> ignoring a <see cref="TextFormatter.HotKeySpecifier"/>.
  244. /// </summary>
  245. /// <returns></returns>
  246. internal Size GetSizeNeededForTextWithoutHotKey () => new (TextFormatter.Size.Width - GetHotKeySpecifierLength (),
  247. TextFormatter.Size.Height - GetHotKeySpecifierLength (false));
  248. /// <summary>
  249. /// Sets <see cref="TextFormatter"/>.Size to the current <see cref="Bounds"/> size, adjusted for
  250. /// <see cref="TextFormatter.HotKeySpecifier"/>.
  251. /// </summary>
  252. /// <remarks>
  253. /// Use this API to set <see cref="TextFormatter.Size"/> when the view has changed such that the
  254. /// size required to fit the text has changed.
  255. /// changes.
  256. /// </remarks>
  257. /// <returns></returns>
  258. internal void SetTextFormatterSize ()
  259. {
  260. if (!IsInitialized) {
  261. TextFormatter.Size = Size.Empty;
  262. return;
  263. }
  264. if (string.IsNullOrEmpty (TextFormatter.Text)) {
  265. TextFormatter.Size = Bounds.Size;
  266. return;
  267. }
  268. TextFormatter.Size = new Size (Bounds.Size.Width + GetHotKeySpecifierLength (),
  269. Bounds.Size.Height + GetHotKeySpecifierLength (false));
  270. }
  271. /// <summary>
  272. /// Gets the Frame dimensions required to fit <see cref="Text"/> within <see cref="Bounds"/> using the text
  273. /// <see cref="Direction"/> specified by the
  274. /// <see cref="TextFormatter"/> property and accounting for any <see cref="HotKeySpecifier"/> characters.
  275. /// </summary>
  276. /// <returns>The <see cref="Size"/> the <see cref="Frame"/> needs to be set to fit the text.</returns>
  277. public Size GetAutoSize ()
  278. {
  279. var x = 0;
  280. var y = 0;
  281. if (IsInitialized) {
  282. x = Bounds.X;
  283. y = Bounds.Y;
  284. }
  285. var rect = TextFormatter.CalcRect (x, y, TextFormatter.Text, TextFormatter.Direction);
  286. int newWidth = rect.Size.Width - GetHotKeySpecifierLength () + (Margin == null ? 0 : Margin.Thickness.Horizontal + Border.Thickness.Horizontal + Padding.Thickness.Horizontal);
  287. int newHeight = rect.Size.Height - GetHotKeySpecifierLength (false) + (Margin == null ? 0 : Margin.Thickness.Vertical + Border.Thickness.Vertical + Padding.Thickness.Vertical);
  288. return new Size (newWidth, newHeight);
  289. }
  290. bool IsValidAutoSize (out Size autoSize)
  291. {
  292. var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  293. autoSize = new Size (rect.Size.Width - GetHotKeySpecifierLength (),
  294. rect.Size.Height - GetHotKeySpecifierLength (false));
  295. return !(ValidatePosDim && (!(Width is Dim.DimAbsolute) || !(Height is Dim.DimAbsolute)) ||
  296. _frame.Size.Width != rect.Size.Width - GetHotKeySpecifierLength () ||
  297. _frame.Size.Height != rect.Size.Height - GetHotKeySpecifierLength (false));
  298. }
  299. bool IsValidAutoSizeWidth (Dim width)
  300. {
  301. var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  302. var dimValue = width.Anchor (0);
  303. return !(ValidatePosDim && !(width is Dim.DimAbsolute) || dimValue != rect.Size.Width - GetHotKeySpecifierLength ());
  304. }
  305. bool IsValidAutoSizeHeight (Dim height)
  306. {
  307. var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  308. var dimValue = height.Anchor (0);
  309. return !(ValidatePosDim && !(height is Dim.DimAbsolute) || dimValue != rect.Size.Height - GetHotKeySpecifierLength (false));
  310. }
  311. }