Border.cs 15 KB

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