ViewText.cs 16 KB

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