FormattedText.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #nullable enable
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. namespace Terminal.Gui.Text;
  6. /// <summary>
  7. /// Represents the result of text formatting, containing formatted lines, size requirements, and metadata.
  8. /// </summary>
  9. public sealed class FormattedText
  10. {
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="FormattedText"/> class.
  13. /// </summary>
  14. /// <param name="lines">The formatted text lines.</param>
  15. /// <param name="requiredSize">The size required to display the text.</param>
  16. /// <param name="hotKey">The HotKey found in the text, if any.</param>
  17. /// <param name="hotKeyPosition">The position of the HotKey in the original text.</param>
  18. public FormattedText(
  19. IReadOnlyList<FormattedLine> lines,
  20. Size requiredSize,
  21. Key hotKey = default,
  22. int hotKeyPosition = -1)
  23. {
  24. Lines = lines ?? throw new ArgumentNullException(nameof(lines));
  25. RequiredSize = requiredSize;
  26. HotKey = hotKey;
  27. HotKeyPosition = hotKeyPosition;
  28. }
  29. /// <summary>Gets the formatted text lines.</summary>
  30. public IReadOnlyList<FormattedLine> Lines { get; }
  31. /// <summary>Gets the size required to display the formatted text.</summary>
  32. public Size RequiredSize { get; }
  33. /// <summary>Gets the HotKey found in the text, if any.</summary>
  34. public Key HotKey { get; }
  35. /// <summary>Gets the position of the HotKey in the original text (-1 if no HotKey).</summary>
  36. public int HotKeyPosition { get; }
  37. /// <summary>Gets a value indicating whether the text contains a HotKey.</summary>
  38. public bool HasHotKey => HotKeyPosition >= 0;
  39. }
  40. /// <summary>
  41. /// Represents a single formatted line of text.
  42. /// </summary>
  43. public sealed class FormattedLine
  44. {
  45. /// <summary>
  46. /// Initializes a new instance of the <see cref="FormattedLine"/> class.
  47. /// </summary>
  48. /// <param name="runs">The text runs that make up this line.</param>
  49. /// <param name="width">The display width of this line.</param>
  50. public FormattedLine(IReadOnlyList<FormattedRun> runs, int width)
  51. {
  52. Runs = runs;
  53. Width = width;
  54. }
  55. /// <summary>Gets the text runs that make up this line.</summary>
  56. public IReadOnlyList<FormattedRun> Runs { get; }
  57. /// <summary>Gets the display width of this line.</summary>
  58. public int Width { get; }
  59. }
  60. /// <summary>
  61. /// Represents a run of text with consistent formatting.
  62. /// </summary>
  63. public sealed class FormattedRun
  64. {
  65. /// <summary>
  66. /// Initializes a new instance of the <see cref="FormattedRun"/> class.
  67. /// </summary>
  68. /// <param name="text">The text content of this run.</param>
  69. /// <param name="isHotKey">Whether this run represents a HotKey.</param>
  70. public FormattedRun(string text, bool isHotKey = false)
  71. {
  72. Text = text;
  73. IsHotKey = isHotKey;
  74. }
  75. /// <summary>Gets the text content of this run.</summary>
  76. public string Text { get; }
  77. /// <summary>Gets a value indicating whether this run represents a HotKey.</summary>
  78. public bool IsHotKey { get; }
  79. }