Border.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. /// <summary>Draws a frame in the current view, clipped by the boundary of this view</summary>
  95. /// <param name="region">View-relative region for the frame to be drawn.</param>
  96. /// <param name="clear">If set to <see langword="true"/> it clear the region.</param>
  97. [Obsolete ("This method is obsolete in v2. Use use LineCanvas or Frame instead.", false)]
  98. public void DrawFrame (Rectangle region, bool clear)
  99. {
  100. Rectangle savedClip = ClipToBounds ();
  101. Rectangle screenBounds = BoundsToScreen (region);
  102. if (clear)
  103. {
  104. Driver.FillRect (region);
  105. }
  106. var lc = new LineCanvas ();
  107. bool drawTop = region.Width > 1 && region.Height > 1;
  108. bool drawLeft = region.Width > 1 && region.Height > 1;
  109. bool drawBottom = region.Width > 1 && region.Height > 1;
  110. bool drawRight = region.Width > 1 && region.Height > 1;
  111. if (drawTop)
  112. {
  113. lc.AddLine (screenBounds.Location, screenBounds.Width, Orientation.Horizontal, LineStyle);
  114. }
  115. if (drawLeft)
  116. {
  117. lc.AddLine (screenBounds.Location, screenBounds.Height, Orientation.Vertical, LineStyle);
  118. }
  119. if (drawBottom)
  120. {
  121. lc.AddLine (
  122. new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1),
  123. screenBounds.Width,
  124. Orientation.Horizontal,
  125. LineStyle
  126. );
  127. }
  128. if (drawRight)
  129. {
  130. lc.AddLine (
  131. new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y),
  132. screenBounds.Height,
  133. Orientation.Vertical,
  134. LineStyle
  135. );
  136. }
  137. foreach (KeyValuePair<Point, Rune> p in lc.GetMap ())
  138. {
  139. Driver.Move (p.Key.X, p.Key.Y);
  140. Driver.AddRune (p.Value);
  141. }
  142. lc.Clear ();
  143. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  144. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler)
  145. == ConsoleDriver.DiagnosticFlags.FrameRuler)
  146. {
  147. // Top
  148. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  149. if (drawTop)
  150. {
  151. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  152. }
  153. //Left
  154. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  155. if (drawLeft)
  156. {
  157. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  158. }
  159. // Bottom
  160. if (drawBottom)
  161. {
  162. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  163. }
  164. // Right
  165. if (drawRight)
  166. {
  167. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  168. }
  169. }
  170. Driver.Clip = savedClip;
  171. }
  172. /// <inheritdoc/>
  173. public override void OnDrawContent (Rectangle contentArea)
  174. {
  175. base.OnDrawContent (contentArea);
  176. if (Thickness == Thickness.Empty)
  177. {
  178. return;
  179. }
  180. //Driver.SetAttribute (Colors.ColorSchemes ["Error"].Normal);
  181. Rectangle screenBounds = BoundsToScreen (Frame);
  182. //OnDrawSubviews (bounds);
  183. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  184. // The border adornment (and title) are drawn at the outermost edge of border;
  185. // For Border
  186. // ...thickness extends outward (border/title is always as far in as possible)
  187. // PERF: How about a call to Rectangle.Offset?
  188. Rectangle borderBounds = new (
  189. screenBounds.X + Math.Max (0, Thickness.Left - 1),
  190. screenBounds.Y + Math.Max (0, Thickness.Top - 1),
  191. Math.Max (
  192. 0,
  193. screenBounds.Width
  194. - Math.Max (
  195. 0,
  196. Math.Max (0, Thickness.Left - 1)
  197. + Math.Max (0, Thickness.Right - 1)
  198. )
  199. ),
  200. Math.Max (
  201. 0,
  202. screenBounds.Height
  203. - Math.Max (
  204. 0,
  205. Math.Max (0, Thickness.Top - 1)
  206. + Math.Max (0, Thickness.Bottom - 1)
  207. )
  208. )
  209. );
  210. int topTitleLineY = borderBounds.Y;
  211. int titleY = borderBounds.Y;
  212. var titleBarsLength = 0; // the little vertical thingies
  213. int maxTitleWidth = Math.Max (0,
  214. Math.Min (
  215. Parent.TitleTextFormatter.FormatAndGetSize ().Width,
  216. Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
  217. )
  218. );
  219. Parent.TitleTextFormatter.Size = new (maxTitleWidth, 1);
  220. int sideLineLength = borderBounds.Height;
  221. bool canDrawBorder = borderBounds is { Width: > 0, Height: > 0 };
  222. if (!string.IsNullOrEmpty (Parent?.Title))
  223. {
  224. if (Thickness.Top == 2)
  225. {
  226. topTitleLineY = borderBounds.Y - 1;
  227. titleY = topTitleLineY + 1;
  228. titleBarsLength = 2;
  229. }
  230. // ┌────┐
  231. //┌┘View└
  232. //│
  233. if (Thickness.Top == 3)
  234. {
  235. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  236. titleY = topTitleLineY + 1;
  237. titleBarsLength = 3;
  238. sideLineLength++;
  239. }
  240. // ┌────┐
  241. //┌┘View└
  242. //│
  243. if (Thickness.Top > 3)
  244. {
  245. topTitleLineY = borderBounds.Y - 2;
  246. titleY = topTitleLineY + 1;
  247. titleBarsLength = 3;
  248. sideLineLength++;
  249. }
  250. }
  251. if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title))
  252. {
  253. Parent.TitleTextFormatter.Draw (new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  254. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor (),
  255. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetHotNormalColor ());
  256. }
  257. if (canDrawBorder && LineStyle != LineStyle.None)
  258. {
  259. LineCanvas lc = Parent?.LineCanvas;
  260. bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height > 1;
  261. bool drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  262. bool drawBottom = Thickness.Bottom > 0 && Frame.Width > 1;
  263. bool drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  264. Attribute prevAttr = Driver.GetAttribute ();
  265. if (ColorScheme is { })
  266. {
  267. Driver.SetAttribute (GetNormalColor ());
  268. }
  269. else
  270. {
  271. Driver.SetAttribute (Parent.GetNormalColor ());
  272. }
  273. if (drawTop)
  274. {
  275. // ╔╡Title╞═════╗
  276. // ╔╡╞═════╗
  277. if (borderBounds.Width < 4 || string.IsNullOrEmpty (Parent?.Title))
  278. {
  279. // ╔╡╞╗ should be ╔══╗
  280. lc.AddLine (
  281. new Point (borderBounds.Location.X, titleY),
  282. borderBounds.Width,
  283. Orientation.Horizontal,
  284. LineStyle,
  285. Driver.GetAttribute ()
  286. );
  287. }
  288. else
  289. {
  290. // ┌────┐
  291. //┌┘View└
  292. //│
  293. if (Thickness.Top == 2)
  294. {
  295. lc.AddLine (
  296. new Point (borderBounds.X + 1, topTitleLineY),
  297. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  298. Orientation.Horizontal,
  299. LineStyle,
  300. Driver.GetAttribute ()
  301. );
  302. }
  303. // ┌────┐
  304. //┌┘View└
  305. //│
  306. if (borderBounds.Width >= 4 && Thickness.Top > 2)
  307. {
  308. lc.AddLine (
  309. new Point (borderBounds.X + 1, topTitleLineY),
  310. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  311. Orientation.Horizontal,
  312. LineStyle,
  313. Driver.GetAttribute ()
  314. );
  315. lc.AddLine (
  316. new Point (borderBounds.X + 1, topTitleLineY + 2),
  317. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  318. Orientation.Horizontal,
  319. LineStyle,
  320. Driver.GetAttribute ()
  321. );
  322. }
  323. // ╔╡Title╞═════╗
  324. // Add a short horiz line for ╔╡
  325. lc.AddLine (
  326. new Point (borderBounds.Location.X, titleY),
  327. 2,
  328. Orientation.Horizontal,
  329. LineStyle,
  330. Driver.GetAttribute ()
  331. );
  332. // Add a vert line for ╔╡
  333. lc.AddLine (
  334. new Point (borderBounds.X + 1, topTitleLineY),
  335. titleBarsLength,
  336. Orientation.Vertical,
  337. LineStyle.Single,
  338. Driver.GetAttribute ()
  339. );
  340. // Add a vert line for ╞
  341. lc.AddLine (
  342. new Point (
  343. borderBounds.X
  344. + 1
  345. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  346. - 1,
  347. topTitleLineY
  348. ),
  349. titleBarsLength,
  350. Orientation.Vertical,
  351. LineStyle.Single,
  352. Driver.GetAttribute ()
  353. );
  354. // Add the right hand line for ╞═════╗
  355. lc.AddLine (
  356. new Point (
  357. borderBounds.X
  358. + 1
  359. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  360. - 1,
  361. titleY
  362. ),
  363. borderBounds.Width - Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  364. Orientation.Horizontal,
  365. LineStyle,
  366. Driver.GetAttribute ()
  367. );
  368. }
  369. }
  370. if (drawLeft)
  371. {
  372. lc.AddLine (
  373. new Point (borderBounds.Location.X, titleY),
  374. sideLineLength,
  375. Orientation.Vertical,
  376. LineStyle,
  377. Driver.GetAttribute ()
  378. );
  379. }
  380. if (drawBottom)
  381. {
  382. lc.AddLine (
  383. new Point (borderBounds.X, borderBounds.Y + borderBounds.Height - 1),
  384. borderBounds.Width,
  385. Orientation.Horizontal,
  386. LineStyle,
  387. Driver.GetAttribute ()
  388. );
  389. }
  390. if (drawRight)
  391. {
  392. lc.AddLine (
  393. new Point (borderBounds.X + borderBounds.Width - 1, titleY),
  394. sideLineLength,
  395. Orientation.Vertical,
  396. LineStyle,
  397. Driver.GetAttribute ()
  398. );
  399. }
  400. Driver.SetAttribute (prevAttr);
  401. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  402. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler)
  403. == ConsoleDriver.DiagnosticFlags.FrameRuler)
  404. {
  405. // Top
  406. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  407. if (drawTop)
  408. {
  409. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  410. }
  411. // Redraw title
  412. if (drawTop && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title))
  413. {
  414. Parent.TitleTextFormatter.Draw (new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  415. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor (),
  416. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor ());
  417. }
  418. //Left
  419. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  420. if (drawLeft)
  421. {
  422. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  423. }
  424. // Bottom
  425. if (drawBottom)
  426. {
  427. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  428. }
  429. // Right
  430. if (drawRight)
  431. {
  432. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  433. }
  434. }
  435. }
  436. //base.OnDrawContent (contentArea);
  437. }
  438. }