ViewText.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. using static Terminal.Gui.SpinnerStyle;
  2. namespace Terminal.Gui;
  3. public partial class View
  4. {
  5. private string _text;
  6. /// <summary>
  7. /// Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
  8. /// or not when <see cref="TextFormatter.WordWrap"/> is enabled.
  9. /// If <see langword="true"/> trailing spaces at the end of wrapped lines will be removed when
  10. /// <see cref="Text"/> is formatted for display. The default is <see langword="false"/>.
  11. /// </summary>
  12. public virtual bool PreserveTrailingSpaces
  13. {
  14. get => TextFormatter.PreserveTrailingSpaces;
  15. set
  16. {
  17. if (TextFormatter.PreserveTrailingSpaces != value)
  18. {
  19. TextFormatter.PreserveTrailingSpaces = value;
  20. TextFormatter.NeedsFormat = true;
  21. }
  22. }
  23. }
  24. /// <summary>
  25. /// The text displayed by the <see cref="View"/>.
  26. /// </summary>
  27. /// <remarks>
  28. /// <para>
  29. /// The text will be drawn before any subviews are drawn.
  30. /// </para>
  31. /// <para>
  32. /// The text will be drawn starting at the view origin (0, 0) and will be formatted according
  33. /// to <see cref="TextAlignment"/> and <see cref="TextDirection"/>.
  34. /// </para>
  35. /// <para>
  36. /// The text will word-wrap to additional lines if it does not fit horizontally. If <see cref="ContentSize"/>'s height
  37. /// is 1, the text will be clipped.
  38. /// </para>
  39. /// <para>If <see cref="AutoSize"/> is <c>true</c>, the <see cref="ContentSize"/> will be adjusted to fit the text.</para>
  40. /// <para>When the text changes, the <see cref="TextChanged"/> is fired.</para>
  41. /// </remarks>
  42. public virtual string Text
  43. {
  44. get => _text;
  45. set
  46. {
  47. if (value == _text)
  48. {
  49. return;
  50. }
  51. string old = _text;
  52. _text = value;
  53. UpdateTextFormatterText ();
  54. OnResizeNeeded ();
  55. #if DEBUG
  56. if (_text is { } && string.IsNullOrEmpty (Id))
  57. {
  58. Id = _text;
  59. }
  60. #endif
  61. OnTextChanged (old, Text);
  62. }
  63. }
  64. /// <summary>
  65. /// Called when the <see cref="Text"/> has changed. Fires the <see cref="TextChanged"/> event.
  66. /// </summary>
  67. /// <param name="oldValue"></param>
  68. /// <param name="newValue"></param>
  69. public void OnTextChanged (string oldValue, string newValue)
  70. {
  71. TextChanged?.Invoke (this, new StateEventArgs<string> (oldValue, newValue));
  72. }
  73. /// <summary>
  74. /// Text changed event, raised when the text has changed.
  75. /// </summary>
  76. public event EventHandler<StateEventArgs<string>> TextChanged;
  77. /// <summary>
  78. /// Gets or sets how the View's <see cref="Text"/> is aligned horizontally when drawn. Changing this property will
  79. /// redisplay the <see cref="View"/>.
  80. /// </summary>
  81. /// <remarks>
  82. /// <para>If <see cref="AutoSize"/> is <c>true</c>, the <see cref="ContentSize"/> will be adjusted to fit the text.</para>
  83. /// </remarks>
  84. /// <value>The text alignment.</value>
  85. public virtual TextAlignment TextAlignment
  86. {
  87. get => TextFormatter.Alignment;
  88. set
  89. {
  90. TextFormatter.Alignment = value;
  91. UpdateTextFormatterText ();
  92. OnResizeNeeded ();
  93. }
  94. }
  95. /// <summary>
  96. /// Gets or sets the direction of the View's <see cref="Text"/>. Changing this property will redisplay the
  97. /// <see cref="View"/>.
  98. /// </summary>
  99. /// <remarks>
  100. /// <para>If <see cref="AutoSize"/> is <c>true</c>, the <see cref="ContentSize"/> will be adjusted to fit the text.</para>
  101. /// </remarks>
  102. /// <value>The text alignment.</value>
  103. public virtual TextDirection TextDirection
  104. {
  105. get => TextFormatter.Direction;
  106. set
  107. {
  108. UpdateTextDirection (value);
  109. TextFormatter.Direction = value;
  110. }
  111. }
  112. /// <summary>
  113. /// Gets or sets the <see cref="Gui.TextFormatter"/> used to format <see cref="Text"/>.
  114. /// </summary>
  115. public TextFormatter TextFormatter { get; init; } = new ();
  116. /// <summary>
  117. /// Gets or sets how the View's <see cref="Text"/> is aligned vertically when drawn. Changing this property will
  118. /// redisplay
  119. /// the <see cref="View"/>.
  120. /// </summary>
  121. /// <remarks>
  122. /// <para>If <see cref="AutoSize"/> is <c>true</c>, the <see cref="ContentSize"/> will be adjusted to fit the text.</para>
  123. /// </remarks>
  124. /// <value>The text alignment.</value>
  125. public virtual VerticalTextAlignment VerticalTextAlignment
  126. {
  127. get => TextFormatter.VerticalAlignment;
  128. set
  129. {
  130. TextFormatter.VerticalAlignment = value;
  131. SetNeedsDisplay ();
  132. }
  133. }
  134. /// <summary>
  135. /// Gets the width or height of the <see cref="TextFormatter.HotKeySpecifier"/> characters
  136. /// in the <see cref="Text"/> property.
  137. /// </summary>
  138. /// <remarks>
  139. /// Only the first HotKey specifier found in <see cref="Text"/> is supported.
  140. /// </remarks>
  141. /// <param name="isWidth">
  142. /// If <see langword="true"/> (the default) the width required for the HotKey specifier is returned. Otherwise the
  143. /// height
  144. /// is returned.
  145. /// </param>
  146. /// <returns>
  147. /// The number of characters required for the <see cref="TextFormatter.HotKeySpecifier"/>. If the text
  148. /// direction specified
  149. /// by <see cref="TextDirection"/> does not match the <paramref name="isWidth"/> parameter, <c>0</c> is returned.
  150. /// </returns>
  151. public int GetHotKeySpecifierLength (bool isWidth = true)
  152. {
  153. if (isWidth)
  154. {
  155. return TextFormatter.IsHorizontalDirection (TextDirection) && TextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  156. ? Math.Max (HotKeySpecifier.GetColumns (), 0)
  157. : 0;
  158. }
  159. return TextFormatter.IsVerticalDirection (TextDirection) && TextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
  160. ? Math.Max (HotKeySpecifier.GetColumns (), 0)
  161. : 0;
  162. }
  163. // BUGBUG: This API is fundamentally broken. Text autosize should have nothing to do with what's visible (Viewport) and only ContentSize.
  164. /// <summary>
  165. /// Gets the Frame dimensions required to fit <see cref="Text"/> within <see cref="ContentSize"/> using the text
  166. /// <see cref="NavigationDirection"/> specified by the <see cref="TextFormatter"/> property and accounting for any
  167. /// <see cref="HotKeySpecifier"/> characters.
  168. /// </summary>
  169. /// <remarks>
  170. /// </remarks>
  171. /// <returns>The <see cref="Size"/> of the <see cref="Frame"/> required to fit the formatted text.</returns>
  172. public Size GetTextAutoSize ()
  173. {
  174. var x = 0;
  175. var y = 0;
  176. if (IsInitialized)
  177. {
  178. x = Viewport.X;
  179. y = Viewport.Y;
  180. }
  181. Rectangle rect = TextFormatter.CalcRect (x, y, TextFormatter.Text, TextFormatter.Direction);
  182. int newWidth = rect.Size.Width
  183. - GetHotKeySpecifierLength ()
  184. + (Margin == null ? 0 : Margin.Thickness.Horizontal + Border.Thickness.Horizontal + Padding.Thickness.Horizontal);
  185. int newHeight = rect.Size.Height
  186. - GetHotKeySpecifierLength (false)
  187. + (Margin == null ? 0 : Margin.Thickness.Vertical + Border.Thickness.Vertical + Padding.Thickness.Vertical);
  188. return new (newWidth, newHeight);
  189. }
  190. /// <summary>
  191. /// Can be overridden if the <see cref="Terminal.Gui.TextFormatter.Text"/> has
  192. /// different format than the default.
  193. /// </summary>
  194. protected virtual void UpdateTextFormatterText ()
  195. {
  196. if (TextFormatter is { })
  197. {
  198. TextFormatter.Text = _text;
  199. }
  200. }
  201. /// <summary>
  202. /// Gets the dimensions required for <see cref="Text"/> ignoring a <see cref="TextFormatter.HotKeySpecifier"/>.
  203. /// </summary>
  204. /// <returns></returns>
  205. internal Size GetSizeNeededForTextWithoutHotKey ()
  206. {
  207. return new Size (
  208. TextFormatter.Size.Width - GetHotKeySpecifierLength (),
  209. TextFormatter.Size.Height - GetHotKeySpecifierLength (false));
  210. }
  211. /// <summary>
  212. /// Internal API. Sets <see cref="TextFormatter"/>.Size to the current <see cref="Viewport"/> size, adjusted for
  213. /// <see cref="TextFormatter.HotKeySpecifier"/>.
  214. /// </summary>
  215. /// <remarks>
  216. /// Use this API to set <see cref="TextFormatter.Size"/> when the view has changed such that the
  217. /// size required to fit the text has changed.
  218. /// changes.
  219. /// </remarks>
  220. /// <returns></returns>
  221. internal void SetTextFormatterSize ()
  222. {
  223. if (!IsInitialized)
  224. {
  225. TextFormatter.Size = Size.Empty;
  226. return;
  227. }
  228. if (string.IsNullOrEmpty (TextFormatter.Text))
  229. {
  230. TextFormatter.Size = ContentSize;
  231. return;
  232. }
  233. int w = Viewport.Size.Width + GetHotKeySpecifierLength ();
  234. // TODO: This is a hack. Figure out how to move this into DimDimAuto
  235. if (Width is Dim.DimAuto widthAuto && widthAuto._style != Dim.DimAutoStyle.Subviews)
  236. {
  237. if (Height is Dim.DimAuto)
  238. {
  239. // Both are auto.
  240. TextFormatter.Size = new Size (SuperView?.ContentSize.Width ?? 0, SuperView?.ContentSize.Height ?? 0);
  241. }
  242. else
  243. {
  244. TextFormatter.Size = new Size (SuperView?.ContentSize.Width ?? 0, ContentSize.Height + GetHotKeySpecifierLength ());
  245. }
  246. w = TextFormatter.FormatAndGetSize ().Width;
  247. }
  248. else
  249. {
  250. TextFormatter.Size = new Size (w, SuperView?.Viewport.Height ?? 0);
  251. }
  252. int h = ContentSize.Height + GetHotKeySpecifierLength ();
  253. // TODO: This is a hack. Figure out how to move this into DimDimAuto
  254. if (Height is Dim.DimAuto heightAuto && heightAuto._style != Dim.DimAutoStyle.Subviews)
  255. {
  256. TextFormatter.NeedsFormat = true;
  257. h = TextFormatter.FormatAndGetSize ().Height;
  258. }
  259. TextFormatter.Size = new Size (w, h);
  260. }
  261. //private bool IsValidAutoSize (out Size autoSize)
  262. //{
  263. // Rectangle rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  264. // autoSize = new Size (
  265. // rect.Size.Width - GetHotKeySpecifierLength (),
  266. // rect.Size.Height - GetHotKeySpecifierLength (false));
  267. // return !((ValidatePosDim && (!(Width is Dim.DimAbsolute) || !(Height is Dim.DimAbsolute)))
  268. // || _frame.Size.Width != rect.Size.Width - GetHotKeySpecifierLength ()
  269. // || _frame.Size.Height != rect.Size.Height - GetHotKeySpecifierLength (false));
  270. //}
  271. private bool IsValidAutoSizeHeight (Dim height)
  272. {
  273. Rectangle rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  274. int dimValue = height.Anchor (0);
  275. return !((ValidatePosDim && !(height is Dim.DimAbsolute)) || dimValue != rect.Size.Height - GetHotKeySpecifierLength (false));
  276. }
  277. private bool IsValidAutoSizeWidth (Dim width)
  278. {
  279. Rectangle rect = TextFormatter.CalcRect (_frame.X, _frame.Y, TextFormatter.Text, TextDirection);
  280. int dimValue = width.Anchor (0);
  281. return !((ValidatePosDim && !(width is Dim.DimAbsolute)) || dimValue != rect.Size.Width - GetHotKeySpecifierLength ());
  282. }
  283. ///// <summary>
  284. ///// Sets the size of the View to the minimum width or height required to fit <see cref="Text"/>.
  285. ///// </summary>
  286. ///// <returns>
  287. ///// <see langword="true"/> if the size was changed; <see langword="false"/> if <see cref="AutoSize"/> ==
  288. ///// <see langword="true"/> or
  289. ///// <see cref="Text"/> will not fit.
  290. ///// </returns>
  291. ///// <remarks>
  292. ///// Always returns <see langword="false"/> if <see cref="AutoSize"/> is <see langword="true"/> or
  293. ///// if <see cref="Height"/> (Horizontal) or <see cref="Width"/> (Vertical) are not not set or zero.
  294. ///// Does not take into account word wrapping.
  295. ///// </remarks>
  296. //private bool SetFrameToFitText ()
  297. //{
  298. // if (AutoSize == false)
  299. // {
  300. // throw new InvalidOperationException ("SetFrameToFitText can only be called when AutoSize is true");
  301. // }
  302. // // BUGBUG: This API is broken - should not assume Frame.Height == ContentSize.Height
  303. // // <summary>
  304. // // Gets the minimum dimensions required to fit the View's <see cref="Text"/>, factoring in <see cref="TextDirection"/>.
  305. // // </summary>
  306. // // <param name="sizeRequired">The minimum dimensions required.</param>
  307. // // <returns><see langword="true"/> if the dimensions fit within the View's <see cref="ContentSize"/>, <see langword="false"/> otherwise.</returns>
  308. // // <remarks>
  309. // // Always returns <see langword="false"/> if <see cref="AutoSize"/> is <see langword="true"/> or
  310. // // if <see cref="Height"/> (Horizontal) or <see cref="Width"/> (Vertical) are not not set or zero.
  311. // // Does not take into account word wrapping.
  312. // // </remarks>
  313. // bool GetMinimumSizeOfText (out Size sizeRequired)
  314. // {
  315. // if (!IsInitialized)
  316. // {
  317. // sizeRequired = Size.Empty;
  318. // return false;
  319. // }
  320. // sizeRequired = ContentSize;
  321. // if (AutoSize || string.IsNullOrEmpty (TextFormatter.Text))
  322. // {
  323. // return false;
  324. // }
  325. // switch (TextFormatter.IsVerticalDirection (TextDirection))
  326. // {
  327. // case true:
  328. // int colWidth = TextFormatter.GetSumMaxCharWidth (TextFormatter.Text, 0, 1);
  329. // // TODO: v2 - This uses frame.Width; it should only use ContentSize
  330. // if (_frame.Width < colWidth
  331. // && (Width is null || (ContentSize.Width >= 0 && Width is Dim.DimAbsolute && Width.Anchor (0) >= 0 && Width.Anchor (0) < colWidth)))
  332. // {
  333. // sizeRequired = new (colWidth, ContentSize.Height);
  334. // return true;
  335. // }
  336. // break;
  337. // default:
  338. // if (_frame.Height < 1 && (Height is null || (Height is Dim.DimAbsolute && Height.Anchor (0) == 0)))
  339. // {
  340. // sizeRequired = new (ContentSize.Width, 1);
  341. // return true;
  342. // }
  343. // break;
  344. // }
  345. // return false;
  346. // }
  347. // if (GetMinimumSizeOfText (out Size size))
  348. // {
  349. // // TODO: This is a hack.
  350. // //_width = size.Width;
  351. // //_height = size.Height;
  352. // SetFrame (new (_frame.Location, size));
  353. // //throw new InvalidOperationException ("This is a hack.");
  354. // return true;
  355. // }
  356. // return false;
  357. //}
  358. private void UpdateTextDirection (TextDirection newDirection)
  359. {
  360. bool directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction) != TextFormatter.IsHorizontalDirection (newDirection);
  361. TextFormatter.Direction = newDirection;
  362. //bool isValidOldAutoSize = AutoSize && IsValidAutoSize (out Size _);
  363. UpdateTextFormatterText ();
  364. if (directionChanged)
  365. {
  366. OnResizeNeeded ();
  367. }
  368. //if ((!ValidatePosDim && directionChanged && AutoSize) || (ValidatePosDim && directionChanged && AutoSize && isValidOldAutoSize))
  369. //{
  370. // OnResizeNeeded ();
  371. //}
  372. //else if (directionChanged && IsAdded)
  373. //{
  374. // ResizeViewportToFit (Viewport.Size);
  375. //}
  376. SetTextFormatterSize ();
  377. SetNeedsDisplay ();
  378. }
  379. }