Border.cs 19 KB

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