Border.cs 17 KB

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