Thickness.cs 11 KB

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