Thickness.cs 12 KB

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