Thickness.cs 11 KB

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