Border.cs 17 KB

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