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
  11. /// frame,
  12. /// with the thickness widths subtracted.
  13. /// </para>
  14. /// <para>Use the helper API (<see cref="Draw(Rectangle, string)"/> to draw the frame with the specified thickness.</para>
  15. /// </remarks>
  16. public class Thickness : IEquatable<Thickness>
  17. {
  18. /// <summary>Gets or sets the width of the lower side of the rectangle.</summary>
  19. [JsonInclude]
  20. public int Bottom;
  21. /// <summary>Gets or sets the width of the left side of the rectangle.</summary>
  22. [JsonInclude]
  23. public int Left;
  24. /// <summary>Gets or sets the width of the right side of the rectangle.</summary>
  25. [JsonInclude]
  26. public int Right;
  27. /// <summary>Gets or sets the width of the upper side of the rectangle.</summary>
  28. [JsonInclude]
  29. public int Top;
  30. /// <summary>Initializes a new instance of the <see cref="Thickness"/> class with all widths set to 0.</summary>
  31. public Thickness () { }
  32. /// <summary>Initializes a new instance of the <see cref="Thickness"/> class with a uniform width to each side.</summary>
  33. /// <param name="width"></param>
  34. public Thickness (int width) : this (width, width, width, width) { }
  35. /// <summary>
  36. /// Initializes a new instance of the <see cref="Thickness"/> class that has specific widths applied to each side
  37. /// of the rectangle.
  38. /// </summary>
  39. /// <param name="left"></param>
  40. /// <param name="top"></param>
  41. /// <param name="right"></param>
  42. /// <param name="bottom"></param>
  43. public Thickness (int left, int top, int right, int bottom)
  44. {
  45. Left = left;
  46. Top = top;
  47. Right = right;
  48. Bottom = bottom;
  49. }
  50. // TODO: add operator overloads
  51. /// <summary>Gets an empty thickness.</summary>
  52. public static Thickness Empty => new (0);
  53. /// <summary>
  54. /// Gets the total width of the left and right sides of the rectangle. Sets the width of the left and rigth sides
  55. /// of the rectangle to half the specified value.
  56. /// </summary>
  57. public int Horizontal
  58. {
  59. get => Left + Right;
  60. set => Left = Right = value / 2;
  61. }
  62. /// <summary>
  63. /// Gets the total height of the top and bottom sides of the rectangle. Sets the height of the top and bottom
  64. /// sides of the rectangle to half the specified value.
  65. /// </summary>
  66. public int Vertical
  67. {
  68. get => Top + Bottom;
  69. set => Top = Bottom = value / 2;
  70. }
  71. // IEquitable
  72. /// <summary>Indicates whether the current object is equal to another object of the same type.</summary>
  73. /// <param name="other"></param>
  74. /// <returns>true if the current object is equal to the other parameter; otherwise, false.</returns>
  75. public bool Equals (Thickness other) { return other is { } && Left == other.Left && Right == other.Right && Top == other.Top && Bottom == other.Bottom; }
  76. /// <summary>
  77. /// Gets whether the specified coordinates lie within the thickness (inside the bounding rectangle but outside
  78. /// the rectangle described by <see cref="GetInside(Rectangle)"/>.
  79. /// </summary>
  80. /// <param name="outside">Describes the location and size of the rectangle that contains the thickness.</param>
  81. /// <param name="location">The coordinate 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 (in Rectangle outside, in Point location)
  84. {
  85. Rectangle inside = GetInside (outside);
  86. return outside.Contains (location) && !inside.Contains (location);
  87. }
  88. /// <summary>
  89. /// Adds the thickness widths of another <see cref="Thickness"/> to the current <see cref="Thickness"/>, returning a
  90. /// new <see cref="Thickness"/>.
  91. /// </summary>
  92. /// <param name="other"></param>
  93. /// <returns></returns>
  94. public Thickness Add (Thickness other) { return new (Left + other.Left, Top + other.Top, Right + other.Right, Bottom + other.Bottom); }
  95. /// <summary>
  96. /// Adds the thickness widths of another <see cref="Thickness"/> to another <see cref="Thickness"/>.
  97. /// </summary>
  98. /// <param name="a"></param>
  99. /// <param name="b"></param>
  100. /// <returns></returns>
  101. public static Thickness operator + (Thickness a, Thickness b) { return a.Add (b); }
  102. /// <summary>Draws the <see cref="Thickness"/> rectangle with an optional diagnostics label.</summary>
  103. /// <remarks>
  104. /// If <see cref="ViewDiagnosticFlags"/> is set to
  105. /// <see cref="ViewDiagnosticFlags.Padding"/> then 'T', 'L', 'R', and 'B' glyphs will be used instead of
  106. /// space. If <see cref="ViewDiagnosticFlags"/> is set to
  107. /// <see cref="ViewDiagnosticFlags.Ruler"/> then a ruler will be drawn on the outer edge of the
  108. /// Thickness.
  109. /// </remarks>
  110. /// <param name="rect">The location and size of the rectangle that bounds the thickness rectangle, in screen coordinates.</param>
  111. /// <param name="label">The diagnostics label to draw on the bottom of the <see cref="Bottom"/>.</param>
  112. /// <returns>The inner rectangle remaining to be drawn.</returns>
  113. public Rectangle Draw (Rectangle rect, string label = null)
  114. {
  115. if (rect.Size.Width < 1 || rect.Size.Height < 1)
  116. {
  117. return Rectangle.Empty;
  118. }
  119. var clearChar = (Rune)' ';
  120. Rune leftChar = clearChar;
  121. Rune rightChar = clearChar;
  122. Rune topChar = clearChar;
  123. Rune bottomChar = clearChar;
  124. if (View.Diagnostics.HasFlag (ViewDiagnosticFlags.Padding))
  125. {
  126. leftChar = (Rune)'L';
  127. rightChar = (Rune)'R';
  128. topChar = (Rune)'T';
  129. bottomChar = (Rune)'B';
  130. if (!string.IsNullOrEmpty (label))
  131. {
  132. leftChar = rightChar = bottomChar = topChar = (Rune)label [0];
  133. }
  134. }
  135. // Draw the Top side
  136. if (Top > 0)
  137. {
  138. Application.Driver.FillRect (rect with { Height = Math.Min (rect.Height, Top) }, topChar);
  139. }
  140. // Draw the Left side
  141. // Draw the Left side
  142. if (Left > 0)
  143. {
  144. Application.Driver.FillRect (rect with { Width = Math.Min (rect.Width, Left) }, leftChar);
  145. }
  146. // Draw the Right side
  147. if (Right > 0)
  148. {
  149. Application.Driver.FillRect (
  150. rect with
  151. {
  152. X = Math.Max (0, rect.X + rect.Width - Right),
  153. Width = Math.Min (rect.Width, Right)
  154. },
  155. rightChar
  156. );
  157. }
  158. // Draw the Bottom side
  159. if (Bottom > 0)
  160. {
  161. Application.Driver.FillRect (
  162. rect with
  163. {
  164. Y = rect.Y + Math.Max (0, rect.Height - Bottom),
  165. Height = Bottom
  166. },
  167. bottomChar
  168. );
  169. }
  170. if (View.Diagnostics.HasFlag (ViewDiagnosticFlags.Ruler))
  171. {
  172. // PERF: This can almost certainly be simplified down to a single point offset and fewer calls to Draw
  173. // Top
  174. var hruler = new Ruler { Length = rect.Width, Orientation = Orientation.Horizontal };
  175. if (Top > 0)
  176. {
  177. hruler.Draw (rect.Location);
  178. }
  179. //Left
  180. var vruler = new Ruler { Length = rect.Height - 2, Orientation = Orientation.Vertical };
  181. if (Left > 0)
  182. {
  183. vruler.Draw (rect.Location with { Y = rect.Y + 1 }, 1);
  184. }
  185. // Bottom
  186. if (Bottom > 0)
  187. {
  188. hruler.Draw (rect.Location with { Y = rect.Y + rect.Height - 1 });
  189. }
  190. // Right
  191. if (Right > 0)
  192. {
  193. vruler.Draw (new (rect.X + rect.Width - 1, rect.Y + 1), 1);
  194. }
  195. }
  196. if (View.Diagnostics.HasFlag (ViewDiagnosticFlags.Padding))
  197. {
  198. // Draw the diagnostics label on the bottom
  199. var tf = new TextFormatter
  200. {
  201. Text = label is null ? string.Empty : $"{label} {this}",
  202. Alignment = Alignment.Center,
  203. VerticalAlignment = Alignment.End,
  204. AutoSize = true
  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. }