Border.cs 17 KB

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