ViewText.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. namespace Terminal.Gui;
  5. public partial class View {
  6. string _text;
  7. /// <summary>
  8. /// The text displayed by the <see cref="View"/>.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>
  12. /// The text will be drawn before any subviews are drawn.
  13. /// </para>
  14. /// <para>
  15. /// The text will be drawn starting at the view origin (0, 0) and will be formatted according
  16. /// to <see cref="TextAlignment"/> and <see cref="TextDirection"/>.
  17. /// </para>
  18. /// <para>
  19. /// The text will word-wrap to additional lines if it does not fit horizontally. If <see cref="Bounds"/>'s height
  20. /// is 1, the text will be clipped.
  21. /// </para>
  22. /// <para>
  23. /// Set the <see cref="HotKeySpecifier"/> to enable hotkey support. To disable hotkey support set
  24. /// <see cref="HotKeySpecifier"/> to
  25. /// <c>(Rune)0xffff</c>.
  26. /// </para>
  27. /// <para>
  28. /// If <see cref="AutoSize"/> is <c>true</c>, the <see cref="Bounds"/> will be adjusted to fit the text.
  29. /// </para>
  30. /// </remarks>
  31. public virtual string Text {
  32. get => _text;
  33. set {
  34. _text = value;
  35. SetHotKey ();
  36. UpdateTextFormatterText ();
  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. return false;
  179. }
  180. switch (TextFormatter.IsVerticalDirection (TextDirection)) {
  181. case true:
  182. var colWidth = TextFormatter.GetSumMaxCharWidth (new List<string> { TextFormatter.Text }, 0, 1);
  183. // TODO: v2 - This uses frame.Width; it should only use Bounds
  184. if (_frame.Width < colWidth &&
  185. (Width == null ||
  186. Bounds.Width >= 0 &&
  187. Width is Dim.DimAbsolute &&
  188. Width.Anchor (0) >= 0 &&
  189. Width.Anchor (0) < colWidth)) {
  190. sizeRequired = new Size (colWidth, Bounds.Height);
  191. return true;
  192. }
  193. break;
  194. default:
  195. if (_frame.Height < 1 &&
  196. (Height == null ||
  197. Height is Dim.DimAbsolute &&
  198. Height.Anchor (0) == 0)) {
  199. sizeRequired = new Size (Bounds.Width, 1);
  200. return true;
  201. }
  202. break;
  203. }
  204. return false;
  205. }
  206. if (GetMinimumSizeOfText (out var size)) {
  207. // TODO: This is a hack.
  208. //_width = size.Width;
  209. //_height = size.Height;
  210. _frame = new Rect (_frame.Location, size);
  211. //throw new InvalidOperationException ("This is a hack.");
  212. return true;
  213. }
  214. return false;
  215. }
  216. /// <summary>
  217. /// Gets the width or height of the <see cref="TextFormatter.HotKeySpecifier"/> characters
  218. /// in the <see cref="Text"/> property.
  219. /// </summary>
  220. /// <remarks>
  221. /// Only the first HotKey specifier found in <see cref="Text"/> is supported.
  222. /// </remarks>
  223. /// <param name="isWidth">
  224. /// If <see langword="true"/> (the default) the width required for the HotKey specifier is returned. Otherwise the height
  225. /// is returned.
  226. /// </param>
  227. /// <returns>
  228. /// The number of characters required for the <see cref="TextFormatter.HotKeySpecifier"/>. If the text
  229. /// direction specified
  230. /// by <see cref="TextDirection"/> does not match the <paramref name="isWidth"/> parameter, <c>0</c> is returned.
  231. /// </returns>
  232. public int GetHotKeySpecifierLength (bool isWidth = true)
  233. {
  234. if (isWidth) {
  235. return TextFormatter.IsHorizontalDirection (TextDirection) &&
  236. TextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  237. ? Math.Max (HotKeySpecifier.GetColumns (), 0) : 0;
  238. }
  239. return TextFormatter.IsVerticalDirection (TextDirection) &&
  240. TextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  241. ? Math.Max (HotKeySpecifier.GetColumns (), 0) : 0;
  242. }
  243. /// <summary>
  244. /// Gets the dimensions required for <see cref="Text"/> ignoring a <see cref="TextFormatter.HotKeySpecifier"/>.
  245. /// </summary>
  246. /// <returns></returns>
  247. internal Size GetSizeNeededForTextWithoutHotKey () => new (TextFormatter.Size.Width - GetHotKeySpecifierLength (),
  248. TextFormatter.Size.Height - GetHotKeySpecifierLength (false));
  249. /// <summary>
  250. /// Sets <see cref="TextFormatter"/>.Size to the current <see cref="Bounds"/> size, adjusted for
  251. /// <see cref="TextFormatter.HotKeySpecifier"/>.
  252. /// </summary>
  253. /// <remarks>
  254. /// Use this API to set <see cref="TextFormatter.Size"/> when the view has changed such that the
  255. /// size required to fit the text has changed.
  256. /// changes.
  257. /// </remarks>
  258. /// <returns></returns>
  259. internal void SetTextFormatterSize ()
  260. {
  261. if (!IsInitialized) {
  262. TextFormatter.Size = Size.Empty;
  263. return;
  264. }
  265. if (string.IsNullOrEmpty (TextFormatter.Text)) {
  266. TextFormatter.Size = Bounds.Size;
  267. return;
  268. }
  269. var w = Bounds.Size.Width + GetHotKeySpecifierLength ();
  270. if (Width is Dim.DimAuto widthAuto && widthAuto._style != Dim.DimAutoStyle.Subviews) {
  271. if (Height is Dim.DimAuto) {
  272. // Both are auto.
  273. TextFormatter.Size = new Size (SuperView?.Bounds.Width ?? 0, SuperView?.Bounds.Height ?? 0);
  274. } else {
  275. TextFormatter.Size = new Size (SuperView?.Bounds.Width ?? 0, Bounds.Size.Height + GetHotKeySpecifierLength ());
  276. }
  277. w = TextFormatter.GetFormattedSize ().Width;
  278. } else {
  279. TextFormatter.Size = new Size (w, SuperView?.Bounds.Height ?? 0);
  280. }
  281. var h = Bounds.Size.Height + GetHotKeySpecifierLength ();
  282. if (Height is Dim.DimAuto heightAuto && heightAuto._style != Dim.DimAutoStyle.Subviews) {
  283. h = SuperView?.Bounds.Height ?? 0;
  284. h = TextFormatter.GetFormattedSize ().Height;
  285. }
  286. TextFormatter.Size = new Size (w, h);
  287. }
  288. // TODO: Refactor this to return the Bounds size, not Frame. Move code that accounts for
  289. // TODO: Thickness out to callers.
  290. /// <summary>
  291. /// Gets the size of the <see cref="Frame"/> required to fit <see cref="Text"/> within <see cref="Bounds"/> using the text
  292. /// formatting settings of <see cref="View.TextFormatter"/> and accounting for <see cref="HotKeySpecifier"/>.
  293. /// </summary>
  294. /// <remarks>
  295. /// </remarks>
  296. /// <returns>The <see cref="Size"/> of the <see cref="Frame"/> required to fit the formatted text.</returns>
  297. public Size GetTextAutoSize ()
  298. {
  299. var x = 0;
  300. var y = 0;
  301. if (IsInitialized) {
  302. x = Bounds.X;
  303. y = Bounds.Y;
  304. }
  305. var rect = TextFormatter.CalcRect (x, y, TextFormatter.Text, TextFormatter.Direction);
  306. int newWidth = rect.Size.Width - GetHotKeySpecifierLength () + (Margin == null ? 0 : Margin.Thickness.Horizontal + Border.Thickness.Horizontal + Padding.Thickness.Horizontal);
  307. int newHeight = rect.Size.Height - GetHotKeySpecifierLength (false) + (Margin == null ? 0 : Margin.Thickness.Vertical + Border.Thickness.Vertical + Padding.Thickness.Vertical);
  308. return new Size (newWidth, newHeight);
  309. }
  310. bool IsValidAutoSize (out Size autoSize)
  311. {
  312. var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  313. autoSize = new Size (rect.Size.Width - GetHotKeySpecifierLength (),
  314. rect.Size.Height - GetHotKeySpecifierLength (false));
  315. return !(ValidatePosDim && (!(Width is Dim.DimAbsolute) || !(Height is Dim.DimAbsolute)) ||
  316. _frame.Size.Width != rect.Size.Width - GetHotKeySpecifierLength () ||
  317. _frame.Size.Height != rect.Size.Height - GetHotKeySpecifierLength (false));
  318. }
  319. bool IsValidAutoSizeWidth (Dim width)
  320. {
  321. var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  322. var dimValue = width.Anchor (0);
  323. return !(ValidatePosDim && !(width is Dim.DimAbsolute) || dimValue != rect.Size.Width - GetHotKeySpecifierLength ());
  324. }
  325. bool IsValidAutoSizeHeight (Dim height)
  326. {
  327. var rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  328. var dimValue = height.Anchor (0);
  329. return !(ValidatePosDim && !(height is Dim.DimAbsolute) || dimValue != rect.Size.Height - GetHotKeySpecifierLength (false));
  330. }
  331. }