Thickness.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using NStack;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.Json.Serialization;
  6. using Terminal.Gui.Configuration;
  7. namespace Terminal.Gui {
  8. /// <summary>
  9. /// Describes the thickness of a frame around a rectangle. Four <see cref="int"/> values describe
  10. /// the <see cref="Left"/>, <see cref="Top"/>, <see cref="Right"/>, and <see cref="Bottom"/> sides
  11. /// of the rectangle, respectively. Provides a helper API (<see cref="Draw(Rect, string)"/> for
  12. /// drawing a frame with the specified thickness.
  13. /// </summary>
  14. public class Thickness : IEquatable<Thickness> {
  15. private int validate (int width)
  16. {
  17. if (width < 0) {
  18. throw new ArgumentException ("Thickness widths cannot be negative.");
  19. }
  20. return width;
  21. }
  22. /// <summary>
  23. /// Gets or sets the width of the left side of the rectangle.
  24. /// </summary>
  25. [JsonInclude]
  26. public int Left;
  27. /// <summary>
  28. /// Gets or sets the width of the upper side of the rectangle.
  29. /// </summary>
  30. [JsonInclude]
  31. public int Top;
  32. /// <summary>
  33. /// Gets or sets the width of the right side of the rectangle.
  34. /// </summary>
  35. [JsonInclude]
  36. public int Right;
  37. /// <summary>
  38. /// Gets or sets the width of the lower side of the rectangle.
  39. /// </summary>
  40. [JsonInclude]
  41. public int Bottom;
  42. /// <summary>
  43. /// Initializes a new instance of the <see cref="Thickness"/> class with all widths
  44. /// set to 0.
  45. /// </summary>
  46. public Thickness () { }
  47. /// <summary>
  48. /// Initializes a new instance of the <see cref="Thickness"/> class with a uniform width to each side.
  49. /// </summary>
  50. /// <param name="width"></param>
  51. public Thickness (int width) : this (width, width, width, width) { }
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="Thickness"/> class that has specific
  54. /// widths applied to each side of the rectangle.
  55. /// </summary>
  56. /// <param name="left"></param>
  57. /// <param name="top"></param>
  58. /// <param name="right"></param>
  59. /// <param name="bottom"></param>
  60. public Thickness (int left, int top, int right, int bottom)
  61. {
  62. Left = left;
  63. Top = top;
  64. Right = right;
  65. Bottom = bottom;
  66. }
  67. public int Vertical {
  68. get {
  69. return Top + Bottom;
  70. }
  71. set {
  72. Top = Bottom = value / 2;
  73. }
  74. }
  75. public int Horizontal {
  76. get {
  77. return Left + Right;
  78. }
  79. set {
  80. Left = Right = value / 2;
  81. }
  82. }
  83. //public virtual void OnChanged()
  84. //{
  85. // Changed?.Invoke (this, new ThicknessEventArgs () { Thickness = this });
  86. //}
  87. //public event EventHandler<ThicknessEventArgs> Changed;
  88. /// <summary>
  89. /// Returns a rectangle describing the location and size of the inner area of <paramref name="rect"/>
  90. /// with the thickness widths subracted. The height and width of the retunred rect may be zero.
  91. /// </summary>
  92. /// <param name="rect">The source rectangle</param>
  93. /// <returns></returns>
  94. public Rect GetInnerRect (Rect rect)
  95. {
  96. var width = rect.Size.Width - (Left + Right);
  97. var height = rect.Size.Height - (Top + Bottom);
  98. var size = new Size (Math.Max (0, width), Math.Max (0, height));
  99. return new Rect (new Point (rect.X + Left, rect.Y + Top), size);
  100. }
  101. private void FillRect (Rect rect, System.Rune rune = default)
  102. {
  103. for (var r = rect.Y; r < rect.Y + rect.Height; r++) {
  104. for (var c = rect.X; c < rect.X + rect.Width; c++) {
  105. Application.Driver.Move (c, r);
  106. Application.Driver.AddRune (rune == default ? ' ' : rune);
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// Draws the <see cref="Thickness"/> rectangle with an optional diagnostics label.
  112. /// </summary>
  113. /// <remarks>
  114. /// If <see cref="ConsoleDriver.DiagnosticFlags"/> is set to <see cref="ConsoleDriver.DiagnosticFlags.FramePadding"/> then
  115. /// 'T', 'L', 'R', and 'B' glyphs will be used instead of space. If <see cref="ConsoleDriver.DiagnosticFlags"/>
  116. /// is set to <see cref="ConsoleDriver.DiagnosticFlags.FrameRuler"/> then a ruler will be drawn on the outer edge of the
  117. /// Thickness.
  118. /// </remarks>
  119. /// <param name="rect">The location and size of the rectangle that bounds the thickness rectangle, in
  120. /// screen coordinates.</param>
  121. /// <param name="label">The diagnostics label to draw on the bottom of the <see cref="Bottom"/>.</param>
  122. /// <returns>The inner rectangle remaining to be drawn.</returns>
  123. public Rect Draw (Rect rect, string label = null)
  124. {
  125. System.Rune clearChar = ' ';
  126. System.Rune leftChar = clearChar;
  127. System.Rune rightChar = clearChar;
  128. System.Rune topChar = clearChar;
  129. System.Rune bottomChar = clearChar;
  130. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FramePadding) == ConsoleDriver.DiagnosticFlags.FramePadding) {
  131. leftChar = 'L';
  132. rightChar = 'R';
  133. topChar = 'T';
  134. bottomChar = 'B';
  135. if (!string.IsNullOrEmpty (label)) {
  136. leftChar = rightChar = bottomChar = topChar = label [0];
  137. }
  138. }
  139. ustring hrule = ustring.Empty;
  140. ustring vrule = ustring.Empty;
  141. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  142. string h = "0123456789";
  143. hrule = h.Repeat ((int)Math.Ceiling ((double)(rect.Width) / (double)h.Length)) [0..(rect.Width)];
  144. string v = "0123456789";
  145. vrule = v.Repeat ((int)Math.Ceiling ((double)(rect.Height * 2) / (double)v.Length)) [0..(rect.Height * 2)];
  146. };
  147. // Draw the Top side
  148. FillRect (new Rect (rect.X, rect.Y, rect.Width, Math.Min (rect.Height, Top)), topChar);
  149. // Draw the Left side
  150. FillRect (new Rect (rect.X, rect.Y, Math.Min (rect.Width, Left), rect.Height), leftChar);
  151. // Draw the Right side
  152. FillRect (new Rect (Math.Max (0, rect.X + rect.Width - Right), rect.Y, Math.Min (rect.Width, Right), rect.Height), rightChar);
  153. // Draw the Bottom side
  154. FillRect (new Rect (rect.X, rect.Y + Math.Max (0, rect.Height - Bottom), rect.Width, Bottom), bottomChar);
  155. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  156. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  157. // Top
  158. Application.Driver.Move (rect.X, rect.Y);
  159. Application.Driver.AddStr (hrule);
  160. //Left
  161. for (var r = rect.Y; r < rect.Y + rect.Height; r++) {
  162. Application.Driver.Move (rect.X, r);
  163. Application.Driver.AddRune (vrule [r - rect.Y]);
  164. }
  165. // Bottom
  166. Application.Driver.Move (rect.X, rect.Y + rect.Height - Bottom + 1);
  167. Application.Driver.AddStr (hrule);
  168. // Right
  169. for (var r = rect.Y + 1; r < rect.Y + rect.Height; r++) {
  170. Application.Driver.Move (rect.X + rect.Width - Right + 1, r);
  171. Application.Driver.AddRune (vrule [r - rect.Y]);
  172. }
  173. }
  174. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FramePadding) == ConsoleDriver.DiagnosticFlags.FramePadding) {
  175. // Draw the diagnostics label on the bottom
  176. var tf = new TextFormatter () {
  177. Text = label == null ? string.Empty : $"{label} {this}",
  178. Alignment = TextAlignment.Centered,
  179. VerticalAlignment = VerticalTextAlignment.Bottom
  180. };
  181. tf.Draw (rect, Application.Driver.CurrentAttribute, Application.Driver.CurrentAttribute, rect, false);
  182. }
  183. return GetInnerRect (rect);
  184. }
  185. // TODO: add operator overloads
  186. /// <summary>
  187. /// Gets an empty thickness.
  188. /// </summary>
  189. public static Thickness Empty => new Thickness (0);
  190. /// <inheritdoc/>
  191. public override bool Equals (object obj)
  192. {
  193. //Check for null and compare run-time types.
  194. if ((obj == null) || !this.GetType ().Equals (obj.GetType ())) {
  195. return false;
  196. } else {
  197. return Equals ((Thickness)obj);
  198. }
  199. }
  200. /// <summary>Returns the thickness widths of the Thickness formatted as a string.</summary>
  201. /// <returns>The thickness widths as a string.</returns>
  202. public override string ToString ()
  203. {
  204. return $"(Left={Left},Top={Top},Right={Right},Bottom={Bottom})";
  205. }
  206. // IEquitable
  207. /// <inheritdoc/>
  208. public bool Equals (Thickness other)
  209. {
  210. return other is not null &&
  211. Left == other.Left &&
  212. Right == other.Right &&
  213. Top == other.Top &&
  214. Bottom == other.Bottom;
  215. }
  216. /// <inheritdoc/>
  217. public override int GetHashCode ()
  218. {
  219. int hashCode = 1380952125;
  220. hashCode = hashCode * -1521134295 + Left.GetHashCode ();
  221. hashCode = hashCode * -1521134295 + Right.GetHashCode ();
  222. hashCode = hashCode * -1521134295 + Top.GetHashCode ();
  223. hashCode = hashCode * -1521134295 + Bottom.GetHashCode ();
  224. return hashCode;
  225. }
  226. /// <inheritdoc/>
  227. public static bool operator == (Thickness left, Thickness right)
  228. {
  229. return EqualityComparer<Thickness>.Default.Equals (left, right);
  230. }
  231. /// <inheritdoc/>
  232. public static bool operator != (Thickness left, Thickness right)
  233. {
  234. return !(left == right);
  235. }
  236. }
  237. internal static class StringExtensions {
  238. public static string Repeat (this string instr, int n)
  239. {
  240. if (n <= 0) {
  241. return null;
  242. }
  243. if (string.IsNullOrEmpty (instr) || n == 1) {
  244. return instr;
  245. }
  246. return new StringBuilder (instr.Length * n)
  247. .Insert (0, instr, n)
  248. .ToString ();
  249. }
  250. }
  251. }