Thickness.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. if (Left > 0)
  144. {
  145. Application.Driver.FillRect (rect with { Width = Math.Min (rect.Width, Left) }, leftChar);
  146. }
  147. // Draw the Right side
  148. if (Right > 0)
  149. {
  150. Application.Driver.FillRect (
  151. rect with
  152. {
  153. X = Math.Max (0, rect.X + rect.Width - Right),
  154. Width = Math.Min (rect.Width, Right)
  155. },
  156. rightChar
  157. );
  158. }
  159. // Draw the Bottom side
  160. if (Bottom > 0)
  161. {
  162. Application.Driver.FillRect (
  163. rect with
  164. {
  165. Y = rect.Y + Math.Max (0, rect.Height - Bottom),
  166. Height = Bottom
  167. },
  168. bottomChar
  169. );
  170. }
  171. if (View.Diagnostics.HasFlag(ViewDiagnosticFlags.Ruler))
  172. {
  173. // PERF: This can almost certainly be simplified down to a single point offset and fewer calls to Draw
  174. // Top
  175. var hruler = new Ruler { Length = rect.Width, Orientation = Orientation.Horizontal };
  176. if (Top > 0)
  177. {
  178. hruler.Draw (rect.Location);
  179. }
  180. //Left
  181. var vruler = new Ruler { Length = rect.Height - 2, Orientation = Orientation.Vertical };
  182. if (Left > 0)
  183. {
  184. vruler.Draw (rect.Location with { Y = rect.Y + 1 }, 1);
  185. }
  186. // Bottom
  187. if (Bottom > 0)
  188. {
  189. hruler.Draw (rect.Location with { Y = rect.Y + rect.Height - 1 });
  190. }
  191. // Right
  192. if (Right > 0)
  193. {
  194. vruler.Draw (new (rect.X + rect.Width - 1, rect.Y + 1), 1);
  195. }
  196. }
  197. if (View.Diagnostics.HasFlag (ViewDiagnosticFlags.Padding))
  198. {
  199. // Draw the diagnostics label on the bottom
  200. var tf = new TextFormatter
  201. {
  202. Text = label is null ? string.Empty : $"{label} {this}",
  203. Alignment = TextAlignment.Centered,
  204. VerticalAlignment = VerticalTextAlignment.Bottom
  205. };
  206. tf.Draw (rect, Application.Driver.CurrentAttribute, Application.Driver.CurrentAttribute, rect);
  207. }
  208. return GetInside (rect);
  209. }
  210. /// <summary>Determines whether the specified object is equal to the current object.</summary>
  211. /// <param name="obj">The object to compare with the current object.</param>
  212. /// <returns><c>true</c> if the specified object is equal to the current object; otherwise, <c>false</c>.</returns>
  213. public override bool Equals (object obj)
  214. {
  215. //Check for null and compare run-time types.
  216. if (obj is null || !GetType ().Equals (obj.GetType ()))
  217. {
  218. return false;
  219. }
  220. return Equals ((Thickness)obj);
  221. }
  222. /// <inheritdoc/>
  223. public override int GetHashCode ()
  224. {
  225. var hashCode = 1380952125;
  226. hashCode = hashCode * -1521134295 + Left.GetHashCode ();
  227. hashCode = hashCode * -1521134295 + Right.GetHashCode ();
  228. hashCode = hashCode * -1521134295 + Top.GetHashCode ();
  229. hashCode = hashCode * -1521134295 + Bottom.GetHashCode ();
  230. return hashCode;
  231. }
  232. /// <summary>
  233. /// Returns a rectangle describing the location and size of the inside area of <paramref name="rect"/> with the
  234. /// thickness widths subtracted. The height and width of the returned rectangle will never be less than 0.
  235. /// </summary>
  236. /// <remarks>
  237. /// If a thickness width is negative, the inside rectangle will be larger than <paramref name="rect"/>. e.g. a
  238. /// <c>
  239. /// Thickness (-1, -1, -1, -1) will result in a rectangle skewed -1 in the X and Y directions and with a Size
  240. /// increased by 1.
  241. /// </c>
  242. /// </remarks>
  243. /// <param name="rect">The source rectangle</param>
  244. /// <returns></returns>
  245. public Rectangle GetInside (Rectangle rect)
  246. {
  247. int x = rect.X + Left;
  248. int y = rect.Y + Top;
  249. int width = Math.Max (0, rect.Size.Width - Horizontal);
  250. int height = Math.Max (0, rect.Size.Height - Vertical);
  251. return new (x, y, width, height);
  252. }
  253. /// <inheritdoc/>
  254. public static bool operator == (Thickness left, Thickness right) { return EqualityComparer<Thickness>.Default.Equals (left, right); }
  255. /// <inheritdoc/>
  256. public static bool operator != (Thickness left, Thickness right) { return !(left == right); }
  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. private int validate (int width)
  261. {
  262. if (width < 0)
  263. {
  264. throw new ArgumentException ("Thickness widths cannot be negative.");
  265. }
  266. return width;
  267. }
  268. }