Border.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 (Rect region, bool clear)
  99. {
  100. Rect savedClip = ClipToBounds ();
  101. Rect 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. /// <summary>Draws the title for a Window-style view.</summary>
  173. /// <param name="region">Screen relative region where the title will be drawn.</param>
  174. /// <param name="title">The title.</param>
  175. public void DrawTitle (Rect region, string title)
  176. {
  177. int width = region.Width;
  178. if (!string.IsNullOrEmpty (title))
  179. {
  180. Driver.Move (region.X + 2, region.Y);
  181. //Driver.AddRune (' ');
  182. string str = title.EnumerateRunes ().Sum (r => Math.Max (r.GetColumns (), 1)) >= width
  183. ? TextFormatter.Format (title, width, false, false) [0]
  184. : title;
  185. Driver.AddStr (str);
  186. }
  187. }
  188. /// <inheritdoc/>
  189. public override void OnDrawContent (Rect contentArea)
  190. {
  191. base.OnDrawContent (contentArea);
  192. if (Thickness == Thickness.Empty)
  193. {
  194. return;
  195. }
  196. //Driver.SetAttribute (Colors.ColorSchemes ["Error"].Normal);
  197. Rect screenBounds = BoundsToScreen (Frame);
  198. //OnDrawSubviews (bounds);
  199. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  200. // The border adornment (and title) are drawn at the outermost edge of border;
  201. // For Border
  202. // ...thickness extends outward (border/title is always as far in as possible)
  203. var borderBounds = new Rect (
  204. screenBounds.X + Math.Max (0, Thickness.Left - 1),
  205. screenBounds.Y + Math.Max (0, Thickness.Top - 1),
  206. Math.Max (
  207. 0,
  208. screenBounds.Width
  209. - Math.Max (
  210. 0,
  211. Math.Max (0, Thickness.Left - 1)
  212. + Math.Max (0, Thickness.Right - 1)
  213. )
  214. ),
  215. Math.Max (
  216. 0,
  217. screenBounds.Height
  218. - Math.Max (
  219. 0,
  220. Math.Max (0, Thickness.Top - 1)
  221. + Math.Max (0, Thickness.Bottom - 1)
  222. )
  223. )
  224. );
  225. int topTitleLineY = borderBounds.Y;
  226. int titleY = borderBounds.Y;
  227. var titleBarsLength = 0; // the little vertical thingies
  228. int maxTitleWidth = Math.Min (
  229. Parent.Title.GetColumns (),
  230. Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
  231. );
  232. int sideLineLength = borderBounds.Height;
  233. bool canDrawBorder = borderBounds.Width > 0 && borderBounds.Height > 0;
  234. if (!string.IsNullOrEmpty (Parent?.Title))
  235. {
  236. if (Thickness.Top == 2)
  237. {
  238. topTitleLineY = borderBounds.Y - 1;
  239. titleY = topTitleLineY + 1;
  240. titleBarsLength = 2;
  241. }
  242. // ┌────┐
  243. //┌┘View└
  244. //│
  245. if (Thickness.Top == 3)
  246. {
  247. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  248. titleY = topTitleLineY + 1;
  249. titleBarsLength = 3;
  250. sideLineLength++;
  251. }
  252. // ┌────┐
  253. //┌┘View└
  254. //│
  255. if (Thickness.Top > 3)
  256. {
  257. topTitleLineY = borderBounds.Y - 2;
  258. titleY = topTitleLineY + 1;
  259. titleBarsLength = 3;
  260. sideLineLength++;
  261. }
  262. }
  263. if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title))
  264. {
  265. Attribute prevAttr = Driver.GetAttribute ();
  266. Driver.SetAttribute (Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor ());
  267. DrawTitle (new Rect (borderBounds.X, titleY, maxTitleWidth, 1), Parent?.Title);
  268. Driver.SetAttribute (prevAttr);
  269. }
  270. if (canDrawBorder && LineStyle != LineStyle.None)
  271. {
  272. LineCanvas lc = Parent?.LineCanvas;
  273. bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height > 1;
  274. bool drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  275. bool drawBottom = Thickness.Bottom > 0 && Frame.Width > 1;
  276. bool drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  277. Attribute prevAttr = Driver.GetAttribute ();
  278. if (ColorScheme is { })
  279. {
  280. Driver.SetAttribute (GetNormalColor ());
  281. }
  282. else
  283. {
  284. Driver.SetAttribute (Parent.GetNormalColor ());
  285. }
  286. if (drawTop)
  287. {
  288. // ╔╡Title╞═════╗
  289. // ╔╡╞═════╗
  290. if (borderBounds.Width < 4 || string.IsNullOrEmpty (Parent?.Title))
  291. {
  292. // ╔╡╞╗ should be ╔══╗
  293. lc.AddLine (
  294. new Point (borderBounds.Location.X, titleY),
  295. borderBounds.Width,
  296. Orientation.Horizontal,
  297. LineStyle,
  298. Driver.GetAttribute ()
  299. );
  300. }
  301. else
  302. {
  303. // ┌────┐
  304. //┌┘View└
  305. //│
  306. if (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. }
  316. // ┌────┐
  317. //┌┘View└
  318. //│
  319. if (borderBounds.Width >= 4 && Thickness.Top > 2)
  320. {
  321. lc.AddLine (
  322. new Point (borderBounds.X + 1, topTitleLineY),
  323. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  324. Orientation.Horizontal,
  325. LineStyle,
  326. Driver.GetAttribute ()
  327. );
  328. lc.AddLine (
  329. new Point (borderBounds.X + 1, topTitleLineY + 2),
  330. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  331. Orientation.Horizontal,
  332. LineStyle,
  333. Driver.GetAttribute ()
  334. );
  335. }
  336. // ╔╡Title╞═════╗
  337. // Add a short horiz line for ╔╡
  338. lc.AddLine (
  339. new Point (borderBounds.Location.X, titleY),
  340. 2,
  341. Orientation.Horizontal,
  342. LineStyle,
  343. Driver.GetAttribute ()
  344. );
  345. // Add a vert line for ╔╡
  346. lc.AddLine (
  347. new Point (borderBounds.X + 1, topTitleLineY),
  348. titleBarsLength,
  349. Orientation.Vertical,
  350. LineStyle.Single,
  351. Driver.GetAttribute ()
  352. );
  353. // Add a vert line for ╞
  354. lc.AddLine (
  355. new Point (
  356. borderBounds.X
  357. + 1
  358. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  359. - 1,
  360. topTitleLineY
  361. ),
  362. titleBarsLength,
  363. Orientation.Vertical,
  364. LineStyle.Single,
  365. Driver.GetAttribute ()
  366. );
  367. // Add the right hand line for ╞═════╗
  368. lc.AddLine (
  369. new Point (
  370. borderBounds.X
  371. + 1
  372. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  373. - 1,
  374. titleY
  375. ),
  376. borderBounds.Width - Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  377. Orientation.Horizontal,
  378. LineStyle,
  379. Driver.GetAttribute ()
  380. );
  381. }
  382. }
  383. if (drawLeft)
  384. {
  385. lc.AddLine (
  386. new Point (borderBounds.Location.X, titleY),
  387. sideLineLength,
  388. Orientation.Vertical,
  389. LineStyle,
  390. Driver.GetAttribute ()
  391. );
  392. }
  393. if (drawBottom)
  394. {
  395. lc.AddLine (
  396. new Point (borderBounds.X, borderBounds.Y + borderBounds.Height - 1),
  397. borderBounds.Width,
  398. Orientation.Horizontal,
  399. LineStyle,
  400. Driver.GetAttribute ()
  401. );
  402. }
  403. if (drawRight)
  404. {
  405. lc.AddLine (
  406. new Point (borderBounds.X + borderBounds.Width - 1, titleY),
  407. sideLineLength,
  408. Orientation.Vertical,
  409. LineStyle,
  410. Driver.GetAttribute ()
  411. );
  412. }
  413. Driver.SetAttribute (prevAttr);
  414. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  415. if ((ConsoleDriver.Diagnostics & ConsoleDriver.DiagnosticFlags.FrameRuler)
  416. == ConsoleDriver.DiagnosticFlags.FrameRuler)
  417. {
  418. // Top
  419. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  420. if (drawTop)
  421. {
  422. hruler.Draw (new Point (screenBounds.X, screenBounds.Y));
  423. }
  424. // Redraw title
  425. if (drawTop && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title))
  426. {
  427. prevAttr = Driver.GetAttribute ();
  428. if (ColorScheme is { })
  429. {
  430. Driver.SetAttribute (HasFocus ? GetHotNormalColor () : GetNormalColor ());
  431. }
  432. else
  433. {
  434. Driver.SetAttribute (Parent.HasFocus ? Parent.GetHotNormalColor () : Parent.GetNormalColor ());
  435. }
  436. DrawTitle (new Rect (borderBounds.X, titleY, Parent.Title.GetColumns (), 1), Parent?.Title);
  437. Driver.SetAttribute (prevAttr);
  438. }
  439. //Left
  440. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  441. if (drawLeft)
  442. {
  443. vruler.Draw (new Point (screenBounds.X, screenBounds.Y + 1), 1);
  444. }
  445. // Bottom
  446. if (drawBottom)
  447. {
  448. hruler.Draw (new Point (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  449. }
  450. // Right
  451. if (drawRight)
  452. {
  453. vruler.Draw (new Point (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  454. }
  455. }
  456. }
  457. //base.OnDrawContent (contentArea);
  458. }
  459. }