Border.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.Linq;
  3. namespace Terminal.Gui;
  4. /// <summary>
  5. /// The Border for a <see cref="View"/>.
  6. /// </summary>
  7. /// <remarks>
  8. /// <para>
  9. /// Renders a border around the view with the <see cref="View.Title"/>. A border using <see cref="LineStyle"/>
  10. /// will be drawn on the sides of <see cref="Thickness"/> that are greater than zero.
  11. /// </para>
  12. /// <para>
  13. /// The <see cref="View.Title"/> of <see cref="Adornment.Parent"/> will be drawn based on the value of <see cref="Thickness.Top"/>:
  14. /// </para>
  15. /// <para>
  16. /// If <c>1</c>:
  17. /// <code>
  18. /// ┌┤1234├──┐
  19. /// │ │
  20. /// └────────┘
  21. /// </code>
  22. /// </para>
  23. /// <para>
  24. /// If <c>2</c>:
  25. /// <code>
  26. /// ┌────┐
  27. /// ┌┤1234├──┐
  28. /// │ │
  29. /// └────────┘
  30. /// </code>
  31. /// </para>
  32. /// <para>
  33. /// If <c>3</c>:
  34. /// <code>
  35. /// ┌────┐
  36. /// ┌┤1234├──┐
  37. /// │└────┘ │
  38. /// │ │
  39. /// └────────┘
  40. /// </code>
  41. /// </para>
  42. /// <para/>
  43. /// <para>
  44. /// See the <see cref="Adornment"/> class.
  45. /// </para>
  46. /// </remarks>
  47. public class Border : Adornment {
  48. LineStyle? _lineStyle = null;
  49. /// <inheritdoc />
  50. public Border () { /* Do nothing; A parameter-less constructor is required to support all views unit tests. */ }
  51. /// <inheritdoc />
  52. public Border (View parent) : base (parent) { /* Do nothing; View.CreateAdornment requires a constructor that takes a parent */ }
  53. /// <summary>
  54. /// The color scheme for the Border. If set to <see langword="null"/>, gets the <see cref="Adornment.Parent"/> scheme.
  55. /// color scheme.
  56. /// </summary>
  57. public override ColorScheme ColorScheme {
  58. get {
  59. if (base.ColorScheme != null) {
  60. return base.ColorScheme;
  61. }
  62. return Parent?.ColorScheme;
  63. }
  64. set {
  65. base.ColorScheme = value;
  66. Parent?.SetNeedsDisplay ();
  67. }
  68. }
  69. /// <summary>
  70. /// Sets the style of the border by changing the <see cref="Thickness"/>. This is a helper API for
  71. /// setting the <see cref="Thickness"/> to <c>(1,1,1,1)</c> and setting the line style of the
  72. /// views that comprise the border. If set to <see cref="LineStyle.None"/> no border will be drawn.
  73. /// </summary>
  74. public LineStyle LineStyle {
  75. get {
  76. if (_lineStyle.HasValue) {
  77. return _lineStyle.Value;
  78. }
  79. // TODO: Make Border.LineStyle inherit from the SuperView hierarchy
  80. // TODO: Right now, Window and FrameView use CM to set BorderStyle, which negates
  81. // TODO: all this.
  82. return Parent.SuperView?.BorderStyle ?? LineStyle.None;
  83. }
  84. set => _lineStyle = value;
  85. }
  86. /// <inheritdoc />
  87. public override void OnDrawContent (Rect contentArea)
  88. {
  89. base.OnDrawContent (contentArea);
  90. if (Thickness == Thickness.Empty) {
  91. return;
  92. }
  93. //Driver.SetAttribute (Colors.ColorSchemes ["Error"].Normal);
  94. var screenBounds = BoundsToScreen (Frame);
  95. //OnDrawSubviews (bounds);
  96. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  97. // The border adornment (and title) are drawn at the outermost edge of border;
  98. // For Border
  99. // ...thickness extends outward (border/title is always as far in as possible)
  100. var borderBounds = new Rect (
  101. screenBounds.X + Math.Max (0, Thickness.Left - 1),
  102. screenBounds.Y + Math.Max (0, Thickness.Top - 1),
  103. Math.Max (0, screenBounds.Width - Math.Max (0, Math.Max (0, Thickness.Left - 1) + Math.Max (0, Thickness.Right - 1))),
  104. Math.Max (0, screenBounds.Height - Math.Max (0, Math.Max (0, Thickness.Top - 1) + Math.Max (0, Thickness.Bottom - 1))));
  105. var topTitleLineY = borderBounds.Y;
  106. var titleY = borderBounds.Y;
  107. var titleBarsLength = 0; // the little vertical thingies
  108. var maxTitleWidth = Math.Min (Parent.Title.GetColumns (), Math.Min (screenBounds.Width - 4, borderBounds.Width - 4));
  109. var sideLineLength = borderBounds.Height;
  110. var canDrawBorder = borderBounds.Width > 0 && borderBounds.Height > 0;
  111. if (!string.IsNullOrEmpty (Parent?.Title)) {
  112. if (Thickness.Top == 2) {
  113. topTitleLineY = borderBounds.Y - 1;
  114. titleY = topTitleLineY + 1;
  115. titleBarsLength = 2;
  116. }
  117. // ┌────┐
  118. //┌┘View└
  119. //│
  120. if (Thickness.Top == 3) {
  121. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  122. titleY = topTitleLineY + 1;
  123. titleBarsLength = 3;
  124. sideLineLength++;
  125. }
  126. // ┌────┐
  127. //┌┘View└
  128. //│
  129. if (Thickness.Top > 3) {
  130. topTitleLineY = borderBounds.Y - 2;
  131. titleY = topTitleLineY + 1;
  132. titleBarsLength = 3;
  133. sideLineLength++;
  134. }
  135. }
  136. if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title)) {
  137. var prevAttr = Driver.GetAttribute ();
  138. Driver.SetAttribute (Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor ());
  139. DrawTitle (new Rect (borderBounds.X, titleY, maxTitleWidth, 1), Parent?.Title);
  140. Driver.SetAttribute (prevAttr);
  141. }
  142. if (canDrawBorder && LineStyle != LineStyle.None) {
  143. var lc = Parent?.LineCanvas;
  144. var drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height > 1;
  145. var drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  146. var drawBottom = Thickness.Bottom > 0 && Frame.Width > 1;
  147. var drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  148. var prevAttr = Driver.GetAttribute ();
  149. if (ColorScheme != null) {
  150. Driver.SetAttribute (GetNormalColor ());
  151. } else {
  152. Driver.SetAttribute (Parent.GetNormalColor ());
  153. }
  154. if (drawTop) {
  155. // ╔╡Title╞═════╗
  156. // ╔╡╞═════╗
  157. if (borderBounds.Width < 4 || string.IsNullOrEmpty (Parent?.Title)) {
  158. // ╔╡╞╗ should be ╔══╗
  159. lc.AddLine (new Point (borderBounds.Location.X, titleY), borderBounds.Width, Orientation.Horizontal, LineStyle, Driver.GetAttribute ());
  160. } else {
  161. // ┌────┐
  162. //┌┘View└
  163. //│
  164. if (Thickness.Top == 2) {
  165. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY), Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, LineStyle, Driver.GetAttribute ());
  166. }
  167. // ┌────┐
  168. //┌┘View└
  169. //│
  170. if (borderBounds.Width >= 4 && Thickness.Top > 2) {
  171. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY), Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, LineStyle, Driver.GetAttribute ());
  172. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY + 2), Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, LineStyle, Driver.GetAttribute ());
  173. }
  174. // ╔╡Title╞═════╗
  175. // Add a short horiz line for ╔╡
  176. lc.AddLine (new Point (borderBounds.Location.X, titleY), 2, Orientation.Horizontal, LineStyle, Driver.GetAttribute ());
  177. // Add a vert line for ╔╡
  178. lc.AddLine (new Point (borderBounds.X + 1, topTitleLineY), titleBarsLength, Orientation.Vertical, LineStyle.Single, Driver.GetAttribute ());
  179. // Add a vert line for ╞
  180. lc.AddLine (new Point (borderBounds.X + 1 + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2) - 1, topTitleLineY), titleBarsLength, Orientation.Vertical, LineStyle.Single, Driver.GetAttribute ());
  181. // Add the right hand line for ╞═════╗
  182. lc.AddLine (new Point (borderBounds.X + 1 + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2) - 1, titleY), borderBounds.Width - Math.Min (borderBounds.Width - 2, maxTitleWidth + 2), Orientation.Horizontal, LineStyle, Driver.GetAttribute ());
  183. }
  184. }
  185. if (drawLeft) {
  186. lc.AddLine (new Point (borderBounds.Location.X, titleY), sideLineLength, Orientation.Vertical, LineStyle, Driver.GetAttribute ());
  187. }
  188. if (drawBottom) {
  189. lc.AddLine (new Point (borderBounds.X, borderBounds.Y + borderBounds.Height - 1), borderBounds.Width, Orientation.Horizontal, LineStyle, Driver.GetAttribute ());
  190. }
  191. if (drawRight) {
  192. lc.AddLine (new Point (borderBounds.X + borderBounds.Width - 1, titleY), sideLineLength, Orientation.Vertical, LineStyle, Driver.GetAttribute ());
  193. }
  194. Driver.SetAttribute (prevAttr);
  195. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  196. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  197. // Top
  198. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  199. if (drawTop) {
  200. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  201. }
  202. // Redraw title
  203. if (drawTop && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title)) {
  204. prevAttr = Driver.GetAttribute ();
  205. if (ColorScheme != null) {
  206. Driver.SetAttribute (HasFocus ? GetHotNormalColor () : GetNormalColor ());
  207. } else {
  208. Driver.SetAttribute (Parent.HasFocus ? Parent.GetHotNormalColor () : Parent.GetNormalColor ());
  209. }
  210. DrawTitle (new Rect (borderBounds.X, titleY, Parent.Title.GetColumns (), 1), Parent?.Title);
  211. Driver.SetAttribute (prevAttr);
  212. }
  213. //Left
  214. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  215. if (drawLeft) {
  216. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  217. }
  218. // Bottom
  219. if (drawBottom) {
  220. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  221. }
  222. // Right
  223. if (drawRight) {
  224. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  225. }
  226. }
  227. }
  228. //base.OnDrawContent (contentArea);
  229. }
  230. /// <summary>
  231. /// Draws the title for a Window-style view.
  232. /// </summary>
  233. /// <param name="region">Screen relative region where the title will be drawn.</param>
  234. /// <param name="title">The title.</param>
  235. public void DrawTitle (Rect region, string title)
  236. {
  237. var width = region.Width;
  238. if (!string.IsNullOrEmpty (title)) {
  239. Driver.Move (region.X + 2, region.Y);
  240. //Driver.AddRune (' ');
  241. var str = title.EnumerateRunes ().Sum (r => Math.Max (r.GetColumns (), 1)) >= width
  242. ? TextFormatter.Format (title, width, false, false) [0] : title;
  243. Driver.AddStr (str);
  244. }
  245. }
  246. /// <summary>
  247. /// Draws a frame in the current view, clipped by the boundary of this view
  248. /// </summary>
  249. /// <param name="region">View-relative region for the frame to be drawn.</param>
  250. /// <param name="clear">If set to <see langword="true"/> it clear the region.</param>
  251. [Obsolete ("This method is obsolete in v2. Use use LineCanvas or Frame instead.", false)]
  252. public void DrawFrame (Rect region, bool clear)
  253. {
  254. var savedClip = ClipToBounds ();
  255. var screenBounds = BoundsToScreen (region);
  256. if (clear) {
  257. Driver.FillRect (region);
  258. }
  259. var lc = new LineCanvas ();
  260. var drawTop = region.Width > 1 && region.Height > 1;
  261. var drawLeft = region.Width > 1 && region.Height > 1;
  262. var drawBottom = region.Width > 1 && region.Height > 1;
  263. var drawRight = region.Width > 1 && region.Height > 1;
  264. if (drawTop) {
  265. lc.AddLine (screenBounds.Location, screenBounds.Width, Orientation.Horizontal, LineStyle);
  266. }
  267. if (drawLeft) {
  268. lc.AddLine (screenBounds.Location, screenBounds.Height, Orientation.Vertical, LineStyle);
  269. }
  270. if (drawBottom) {
  271. lc.AddLine (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1), screenBounds.Width, Orientation.Horizontal, LineStyle);
  272. }
  273. if (drawRight) {
  274. lc.AddLine (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y), screenBounds.Height, Orientation.Vertical, LineStyle);
  275. }
  276. foreach (var p in lc.GetMap ()) {
  277. Driver.Move (p.Key.X, p.Key.Y);
  278. Driver.AddRune (p.Value);
  279. }
  280. lc.Clear ();
  281. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  282. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler) == ConsoleDriver.DiagnosticFlags.FrameRuler) {
  283. // Top
  284. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  285. if (drawTop) {
  286. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  287. }
  288. //Left
  289. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  290. if (drawLeft) {
  291. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  292. }
  293. // Bottom
  294. if (drawBottom) {
  295. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  296. }
  297. // Right
  298. if (drawRight) {
  299. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  300. }
  301. }
  302. Driver.Clip = savedClip;
  303. }
  304. }