Thickness.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #nullable enable
  2. using System.Numerics;
  3. using System.Text.Json.Serialization;
  4. namespace Terminal.Gui.Drawing;
  5. /// <summary>
  6. /// Describes the thickness of a frame around a rectangle. Four <see cref="int"/> values describe the
  7. /// <see cref="Left"/>, <see cref="Top"/>, <see cref="Right"/>, and <see cref="Bottom"/> sides of the rectangle,
  8. /// respectively.
  9. /// </summary>
  10. /// <remarks>
  11. /// <para>
  12. /// Use the helper API (<see cref="GetInside(Rectangle)"/> to get the rectangle describing the insides of the
  13. /// frame,
  14. /// with the thickness widths subtracted.
  15. /// </para>
  16. /// <para>
  17. /// Thickness uses <see langword="float"/> internally. As a result, there is a potential precision loss for very
  18. /// large numbers. This is typically not an issue for UI dimensions but could be relevant in other contexts.
  19. /// </para>
  20. /// </remarks>
  21. public record struct Thickness
  22. {
  23. /// <summary>Initializes a new instance of the <see cref="Thickness"/> class with all widths set to 0.</summary>
  24. public Thickness () { _sides = Vector4.Zero; }
  25. /// <summary>Initializes a new instance of the <see cref="Thickness"/> class with a uniform width to each side.</summary>
  26. /// <param name="width"></param>
  27. public Thickness (int width) : this (width, width, width, width) { }
  28. /// <summary>
  29. /// Initializes a new instance of the <see cref="Thickness"/> class that has specific widths applied to each side
  30. /// of the rectangle.
  31. /// </summary>
  32. /// <param name="left"></param>
  33. /// <param name="top"></param>
  34. /// <param name="right"></param>
  35. /// <param name="bottom"></param>
  36. public Thickness (int left, int top, int right, int bottom)
  37. {
  38. Left = left;
  39. Top = top;
  40. Right = right;
  41. Bottom = bottom;
  42. }
  43. private Vector4 _sides;
  44. /// <summary>
  45. /// Adds the thickness widths of another <see cref="Thickness"/> to the current <see cref="Thickness"/>, returning a
  46. /// new <see cref="Thickness"/>.
  47. /// </summary>
  48. /// <param name="other"></param>
  49. /// <returns></returns>
  50. public readonly Thickness Add (Thickness other) { return new (Left + other.Left, Top + other.Top, Right + other.Right, Bottom + other.Bottom); }
  51. /// <summary>Gets or sets the width of the lower side of the rectangle.</summary>
  52. [JsonInclude]
  53. public int Bottom
  54. {
  55. readonly get => (int)_sides.W;
  56. set => _sides.W = value;
  57. }
  58. /// <summary>
  59. /// Gets whether the specified coordinates lie within the thickness (inside the bounding rectangle but outside
  60. /// the rectangle described by <see cref="GetInside(Rectangle)"/>.
  61. /// </summary>
  62. /// <param name="outside">Describes the location and size of the rectangle that contains the thickness.</param>
  63. /// <param name="location">The coordinate to check.</param>
  64. /// <returns><see langword="true"/> if the specified coordinate is within the thickness; <see langword="false"/> otherwise.</returns>
  65. public bool Contains (in Rectangle outside, in Point location)
  66. {
  67. Rectangle inside = GetInside (outside);
  68. return outside.Contains (location) && !inside.Contains (location);
  69. }
  70. /// <summary>Draws the <see cref="Thickness"/> rectangle with an optional diagnostics label.</summary>
  71. /// <remarks>
  72. /// If <see cref="ViewDiagnosticFlags"/> is set to
  73. /// <see cref="ViewDiagnosticFlags.Thickness"/> then 'T', 'L', 'R', and 'B' glyphs will be used instead of
  74. /// space. If <see cref="ViewDiagnosticFlags"/> is set to
  75. /// <see cref="ViewDiagnosticFlags.Ruler"/> then a ruler will be drawn on the outer edge of the
  76. /// Thickness.
  77. /// </remarks>
  78. /// <param name="rect">The location and size of the rectangle that bounds the thickness rectangle, in screen coordinates.</param>
  79. /// <param name="diagnosticFlags"></param>
  80. /// <param name="label">The diagnostics label to draw on the bottom of the <see cref="Bottom"/>.</param>
  81. /// <param name="driver">Optional driver. If not specified, <see cref="Application.Driver"/> will be used.</param>
  82. /// <returns>The inner rectangle remaining to be drawn.</returns>
  83. public Rectangle Draw (Rectangle rect, ViewDiagnosticFlags diagnosticFlags = ViewDiagnosticFlags.Off, string? label = null, IConsoleDriver? driver = null)
  84. {
  85. if (rect.Size.Width < 1 || rect.Size.Height < 1)
  86. {
  87. return Rectangle.Empty;
  88. }
  89. driver ??= Application.Driver;
  90. var clearChar = (Rune)' ';
  91. Rune leftChar = clearChar;
  92. Rune rightChar = clearChar;
  93. Rune topChar = clearChar;
  94. Rune bottomChar = clearChar;
  95. if (diagnosticFlags.HasFlag (ViewDiagnosticFlags.Thickness))
  96. {
  97. leftChar = (Rune)'L';
  98. rightChar = (Rune)'R';
  99. topChar = (Rune)'T';
  100. bottomChar = (Rune)'B';
  101. if (!string.IsNullOrEmpty (label))
  102. {
  103. leftChar = rightChar = bottomChar = topChar = (Rune)label [0];
  104. }
  105. }
  106. // Draw the Top side
  107. if (Top > 0)
  108. {
  109. driver?.FillRect (rect with { Height = Math.Min (rect.Height, Top) }, topChar);
  110. }
  111. // Draw the Left side
  112. // Draw the Left side
  113. if (Left > 0)
  114. {
  115. driver?.FillRect (rect with { Width = Math.Min (rect.Width, Left) }, leftChar);
  116. }
  117. // Draw the Right side
  118. if (Right > 0)
  119. {
  120. driver?.FillRect (
  121. rect with
  122. {
  123. X = Math.Max (0, rect.X + rect.Width - Right),
  124. Width = Math.Min (rect.Width, Right)
  125. },
  126. rightChar
  127. );
  128. }
  129. // Draw the Bottom side
  130. if (Bottom > 0)
  131. {
  132. driver?.FillRect (
  133. rect with
  134. {
  135. Y = rect.Y + Math.Max (0, rect.Height - Bottom),
  136. Height = Bottom
  137. },
  138. bottomChar
  139. );
  140. }
  141. if (diagnosticFlags.HasFlag (ViewDiagnosticFlags.Ruler))
  142. {
  143. // PERF: This can almost certainly be simplified down to a single point offset and fewer calls to Draw
  144. // Top
  145. Ruler hRuler = new () { Length = rect.Width, Orientation = Orientation.Horizontal };
  146. if (Top > 0)
  147. {
  148. hRuler.Draw (rect.Location, driver: driver);
  149. }
  150. //Left
  151. Ruler vRuler = new () { Length = rect.Height - 2, Orientation = Orientation.Vertical };
  152. if (Left > 0)
  153. {
  154. vRuler.Draw (rect.Location with { Y = rect.Y + 1 }, 1, driver);
  155. }
  156. // Bottom
  157. if (Bottom > 0)
  158. {
  159. hRuler.Draw (rect.Location with { Y = rect.Y + rect.Height - 1 }, driver: driver);
  160. }
  161. // Right
  162. if (Right > 0)
  163. {
  164. vRuler.Draw (new (rect.X + rect.Width - 1, rect.Y + 1), 1, driver);
  165. }
  166. }
  167. if (diagnosticFlags.HasFlag (ViewDiagnosticFlags.Thickness))
  168. {
  169. // Draw the diagnostics label on the bottom
  170. string text = label is null ? string.Empty : $"{label} {this}";
  171. TextFormatter tf = new ()
  172. {
  173. Text = text,
  174. Alignment = Alignment.Center,
  175. VerticalAlignment = Alignment.End,
  176. ConstrainToWidth = text.GetColumns (),
  177. ConstrainToHeight = 1
  178. };
  179. if (driver?.CurrentAttribute is { })
  180. {
  181. tf.Draw (rect, driver!.CurrentAttribute, driver!.CurrentAttribute, rect, driver);
  182. }
  183. }
  184. return GetInside (rect);
  185. }
  186. /// <summary>Gets an empty thickness.</summary>
  187. public static Thickness Empty => new (0);
  188. /// <summary>
  189. /// Returns a rectangle describing the location and size of the inside area of <paramref name="rect"/> with the
  190. /// thickness widths subtracted. The height and width of the returned rectangle will never be less than 0.
  191. /// </summary>
  192. /// <remarks>
  193. /// If a thickness width is negative, the inside rectangle will be larger than <paramref name="rect"/>. e.g. a
  194. /// <c>
  195. /// Thickness (-1, -1, -1, -1) will result in a rectangle skewed -1 in the X and Y directions and with a Size
  196. /// increased by 1.
  197. /// </c>
  198. /// </remarks>
  199. /// <param name="rect">The source rectangle</param>
  200. /// <returns></returns>
  201. public Rectangle GetInside (Rectangle rect)
  202. {
  203. int x = rect.X + Left;
  204. int y = rect.Y + Top;
  205. int width = Math.Max (0, rect.Size.Width - Horizontal);
  206. int height = Math.Max (0, rect.Size.Height - Vertical);
  207. return new (x, y, width, height);
  208. }
  209. /// <summary>
  210. /// Returns a region describing the thickness.
  211. /// </summary>
  212. /// <param name="rect">The source rectangle</param>
  213. /// <returns></returns>
  214. public Region AsRegion (Rectangle rect)
  215. {
  216. var region = new Region (rect);
  217. region.Exclude (GetInside (rect));
  218. return region;
  219. }
  220. /// <summary>
  221. /// Gets the total width of the left and right sides of the rectangle. Sets the width of the left and right sides
  222. /// of the rectangle to half the specified value.
  223. /// </summary>
  224. public int Horizontal
  225. {
  226. get => Left + Right;
  227. set => Left = Right = value / 2;
  228. }
  229. /// <summary>Gets or sets the width of the left side of the rectangle.</summary>
  230. [JsonInclude]
  231. public int Left
  232. {
  233. readonly get => (int)_sides.X;
  234. set => _sides.X = value;
  235. }
  236. /// <summary>
  237. /// Adds the thickness widths of another <see cref="Thickness"/> to another <see cref="Thickness"/>.
  238. /// </summary>
  239. /// <param name="a"></param>
  240. /// <param name="b"></param>
  241. /// <returns></returns>
  242. public static Thickness operator + (Thickness a, Thickness b) { return a.Add (b); }
  243. /// <summary>Gets or sets the width of the right side of the rectangle.</summary>
  244. [JsonInclude]
  245. public int Right
  246. {
  247. readonly get => (int)_sides.Z;
  248. set => _sides.Z = value;
  249. }
  250. /// <summary>Gets or sets the width of the upper side of the rectangle.</summary>
  251. [JsonInclude]
  252. public int Top
  253. {
  254. readonly get => (int)_sides.Y;
  255. set => _sides.Y = value;
  256. }
  257. /// <summary>Returns the thickness widths of the Thickness formatted as a string.</summary>
  258. /// <returns>The thickness widths as a string.</returns>
  259. public override string ToString () { return $"(Left={Left},Top={Top},Right={Right},Bottom={Bottom})"; }
  260. /// <summary>
  261. /// Gets the total height of the top and bottom sides of the rectangle. Sets the height of the top and bottom
  262. /// sides of the rectangle to half the specified value.
  263. /// </summary>
  264. public int Vertical
  265. {
  266. get => Top + Bottom;
  267. set => Top = Bottom = value / 2;
  268. }
  269. }