Border.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. var borderBounds = new Rectangle (
  188. screenBounds.X + Math.Max (0, Thickness.Left - 1),
  189. screenBounds.Y + Math.Max (0, Thickness.Top - 1),
  190. Math.Max (
  191. 0,
  192. screenBounds.Width
  193. - Math.Max (
  194. 0,
  195. Math.Max (0, Thickness.Left - 1)
  196. + Math.Max (0, Thickness.Right - 1)
  197. )
  198. ),
  199. Math.Max (
  200. 0,
  201. screenBounds.Height
  202. - Math.Max (
  203. 0,
  204. Math.Max (0, Thickness.Top - 1)
  205. + Math.Max (0, Thickness.Bottom - 1)
  206. )
  207. )
  208. );
  209. int topTitleLineY = borderBounds.Y;
  210. int titleY = borderBounds.Y;
  211. var titleBarsLength = 0; // the little vertical thingies
  212. int maxTitleWidth = Math.Max (0,
  213. Math.Min (
  214. Parent.TitleTextFormatter.FormatAndGetSize ().Width,
  215. Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
  216. )
  217. );
  218. Parent.TitleTextFormatter.Size = new Size (maxTitleWidth, 1);
  219. int sideLineLength = borderBounds.Height;
  220. bool canDrawBorder = borderBounds.Width > 0 && borderBounds.Height > 0;
  221. if (!string.IsNullOrEmpty (Parent?.Title))
  222. {
  223. if (Thickness.Top == 2)
  224. {
  225. topTitleLineY = borderBounds.Y - 1;
  226. titleY = topTitleLineY + 1;
  227. titleBarsLength = 2;
  228. }
  229. // ┌────┐
  230. //┌┘View└
  231. //│
  232. if (Thickness.Top == 3)
  233. {
  234. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  235. titleY = topTitleLineY + 1;
  236. titleBarsLength = 3;
  237. sideLineLength++;
  238. }
  239. // ┌────┐
  240. //┌┘View└
  241. //│
  242. if (Thickness.Top > 3)
  243. {
  244. topTitleLineY = borderBounds.Y - 2;
  245. titleY = topTitleLineY + 1;
  246. titleBarsLength = 3;
  247. sideLineLength++;
  248. }
  249. }
  250. if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title))
  251. {
  252. Parent.TitleTextFormatter.Draw (new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  253. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor (),
  254. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetHotNormalColor ());
  255. }
  256. if (canDrawBorder && LineStyle != LineStyle.None)
  257. {
  258. LineCanvas lc = Parent?.LineCanvas;
  259. bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height > 1;
  260. bool drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  261. bool drawBottom = Thickness.Bottom > 0 && Frame.Width > 1;
  262. bool drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  263. Attribute prevAttr = Driver.GetAttribute ();
  264. if (ColorScheme is { })
  265. {
  266. Driver.SetAttribute (GetNormalColor ());
  267. }
  268. else
  269. {
  270. Driver.SetAttribute (Parent.GetNormalColor ());
  271. }
  272. if (drawTop)
  273. {
  274. // ╔╡Title╞═════╗
  275. // ╔╡╞═════╗
  276. if (borderBounds.Width < 4 || string.IsNullOrEmpty (Parent?.Title))
  277. {
  278. // ╔╡╞╗ should be ╔══╗
  279. lc.AddLine (
  280. new Point (borderBounds.Location.X, titleY),
  281. borderBounds.Width,
  282. Orientation.Horizontal,
  283. LineStyle,
  284. Driver.GetAttribute ()
  285. );
  286. }
  287. else
  288. {
  289. // ┌────┐
  290. //┌┘View└
  291. //│
  292. if (Thickness.Top == 2)
  293. {
  294. lc.AddLine (
  295. new Point (borderBounds.X + 1, topTitleLineY),
  296. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  297. Orientation.Horizontal,
  298. LineStyle,
  299. Driver.GetAttribute ()
  300. );
  301. }
  302. // ┌────┐
  303. //┌┘View└
  304. //│
  305. if (borderBounds.Width >= 4 && Thickness.Top > 2)
  306. {
  307. lc.AddLine (
  308. new Point (borderBounds.X + 1, topTitleLineY),
  309. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  310. Orientation.Horizontal,
  311. LineStyle,
  312. Driver.GetAttribute ()
  313. );
  314. lc.AddLine (
  315. new Point (borderBounds.X + 1, topTitleLineY + 2),
  316. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  317. Orientation.Horizontal,
  318. LineStyle,
  319. Driver.GetAttribute ()
  320. );
  321. }
  322. // ╔╡Title╞═════╗
  323. // Add a short horiz line for ╔╡
  324. lc.AddLine (
  325. new Point (borderBounds.Location.X, titleY),
  326. 2,
  327. Orientation.Horizontal,
  328. LineStyle,
  329. Driver.GetAttribute ()
  330. );
  331. // Add a vert line for ╔╡
  332. lc.AddLine (
  333. new Point (borderBounds.X + 1, topTitleLineY),
  334. titleBarsLength,
  335. Orientation.Vertical,
  336. LineStyle.Single,
  337. Driver.GetAttribute ()
  338. );
  339. // Add a vert line for ╞
  340. lc.AddLine (
  341. new Point (
  342. borderBounds.X
  343. + 1
  344. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  345. - 1,
  346. topTitleLineY
  347. ),
  348. titleBarsLength,
  349. Orientation.Vertical,
  350. LineStyle.Single,
  351. Driver.GetAttribute ()
  352. );
  353. // Add the right hand line for ╞═════╗
  354. lc.AddLine (
  355. new Point (
  356. borderBounds.X
  357. + 1
  358. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  359. - 1,
  360. titleY
  361. ),
  362. borderBounds.Width - Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  363. Orientation.Horizontal,
  364. LineStyle,
  365. Driver.GetAttribute ()
  366. );
  367. }
  368. }
  369. if (drawLeft)
  370. {
  371. lc.AddLine (
  372. new Point (borderBounds.Location.X, titleY),
  373. sideLineLength,
  374. Orientation.Vertical,
  375. LineStyle,
  376. Driver.GetAttribute ()
  377. );
  378. }
  379. if (drawBottom)
  380. {
  381. lc.AddLine (
  382. new Point (borderBounds.X, borderBounds.Y + borderBounds.Height - 1),
  383. borderBounds.Width,
  384. Orientation.Horizontal,
  385. LineStyle,
  386. Driver.GetAttribute ()
  387. );
  388. }
  389. if (drawRight)
  390. {
  391. lc.AddLine (
  392. new Point (borderBounds.X + borderBounds.Width - 1, titleY),
  393. sideLineLength,
  394. Orientation.Vertical,
  395. LineStyle,
  396. Driver.GetAttribute ()
  397. );
  398. }
  399. Driver.SetAttribute (prevAttr);
  400. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  401. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler)
  402. == ConsoleDriver.DiagnosticFlags.FrameRuler)
  403. {
  404. // Top
  405. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  406. if (drawTop)
  407. {
  408. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  409. }
  410. // Redraw title
  411. if (drawTop && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title))
  412. {
  413. Parent.TitleTextFormatter.Draw (new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  414. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor (),
  415. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor ());
  416. }
  417. //Left
  418. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  419. if (drawLeft)
  420. {
  421. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  422. }
  423. // Bottom
  424. if (drawBottom)
  425. {
  426. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  427. }
  428. // Right
  429. if (drawRight)
  430. {
  431. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  432. }
  433. }
  434. }
  435. //base.OnDrawContent (contentArea);
  436. }
  437. }