Border.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui.ViewBase;
  4. /// <summary>The Border for a <see cref="View"/>. Accessed via <see cref="View.Border"/></summary>
  5. /// <remarks>
  6. /// <para>
  7. /// Renders a border around the view with the <see cref="View.Title"/>. A border using <see cref="LineStyle"/>
  8. /// will be drawn on the sides of <see cref="Drawing.Thickness"/> that are greater than zero.
  9. /// </para>
  10. /// <para>
  11. /// The <see cref="View.Title"/> of <see cref="Adornment.Parent"/> will be drawn based on the value of
  12. /// <see cref="Drawing.Thickness.Top"/>:
  13. /// <example>
  14. /// // If Thickness.Top is 1:
  15. /// ┌┤1234├──┐
  16. /// │ │
  17. /// └────────┘
  18. /// // If Thickness.Top is 2:
  19. /// ┌────┐
  20. /// ┌┤1234├──┐
  21. /// │ │
  22. /// └────────┘
  23. /// If Thickness.Top is 3:
  24. /// ┌────┐
  25. /// ┌┤1234├──┐
  26. /// │└────┘ │
  27. /// │ │
  28. /// └────────┘
  29. /// </example>
  30. /// </para>
  31. /// <para>
  32. /// The Border provides keyboard and mouse support for moving and resizing the View. See <see cref="ViewArrangement"/>.
  33. /// </para>
  34. /// </remarks>
  35. public partial class Border : Adornment
  36. {
  37. private LineStyle? _lineStyle;
  38. /// <inheritdoc/>
  39. public Border ()
  40. { /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
  41. }
  42. /// <inheritdoc/>
  43. public Border (View parent) : base (parent)
  44. {
  45. Parent = parent;
  46. CanFocus = false;
  47. TabStop = TabBehavior.TabGroup;
  48. Application.GrabbingMouse += Application_GrabbingMouse;
  49. Application.UnGrabbingMouse += Application_UnGrabbingMouse;
  50. HighlightStyle |= HighlightStyle.Pressed;
  51. ThicknessChanged += OnThicknessChanged;
  52. }
  53. // TODO: Move DrawIndicator out of Border and into View
  54. private void OnThicknessChanged (object? sender, EventArgs e)
  55. {
  56. if (IsInitialized)
  57. {
  58. ShowHideDrawIndicator ();
  59. }
  60. }
  61. private void ShowHideDrawIndicator ()
  62. {
  63. if (View.Diagnostics.HasFlag (ViewDiagnosticFlags.DrawIndicator) && Thickness != Thickness.Empty)
  64. {
  65. if (DrawIndicator is null)
  66. {
  67. DrawIndicator = new ()
  68. {
  69. Id = "DrawIndicator",
  70. X = 1,
  71. Style = new SpinnerStyle.Dots2 (),
  72. SpinDelay = 0,
  73. Visible = false
  74. };
  75. Add (DrawIndicator);
  76. }
  77. }
  78. else if (DrawIndicator is { })
  79. {
  80. Remove (DrawIndicator);
  81. DrawIndicator!.Dispose ();
  82. DrawIndicator = null;
  83. }
  84. }
  85. internal void AdvanceDrawIndicator ()
  86. {
  87. if (View.Diagnostics.HasFlag (ViewDiagnosticFlags.DrawIndicator) && DrawIndicator is { })
  88. {
  89. DrawIndicator.AdvanceAnimation (false);
  90. DrawIndicator.Render ();
  91. }
  92. }
  93. #if SUBVIEW_BASED_BORDER
  94. private Line _left;
  95. /// <summary>
  96. /// The close button for the border. Set to <see cref="View.Visible"/>, to <see langword="true"/> to enable.
  97. /// </summary>
  98. public Button CloseButton { get; internal set; }
  99. #endif
  100. /// <inheritdoc/>
  101. public override void BeginInit ()
  102. {
  103. base.BeginInit ();
  104. ShowHideDrawIndicator ();
  105. #if SUBVIEW_BASED_BORDER
  106. if (Parent is { })
  107. {
  108. // Left
  109. _left = new ()
  110. {
  111. Orientation = Orientation.Vertical,
  112. };
  113. Add (_left);
  114. CloseButton = new Button ()
  115. {
  116. Text = "X",
  117. CanFocus = true,
  118. Visible = false,
  119. };
  120. CloseButton.Accept += (s, e) =>
  121. {
  122. e.Handled = Parent.InvokeCommand (Command.QuitToplevel) == true;
  123. };
  124. Add (CloseButton);
  125. LayoutStarted += OnLayoutStarted;
  126. }
  127. #endif
  128. }
  129. #if SUBVIEW_BASED_BORDER
  130. private void OnLayoutStarted (object sender, LayoutEventArgs e)
  131. {
  132. _left.Border.LineStyle = LineStyle;
  133. _left.X = Thickness.Left - 1;
  134. _left.Y = Thickness.Top - 1;
  135. _left.Width = 1;
  136. _left.Height = Height;
  137. CloseButton.X = Pos.AnchorEnd (Thickness.Right / 2 + 1) -
  138. (Pos.Right (CloseButton) -
  139. Pos.Left (CloseButton));
  140. CloseButton.Y = 0;
  141. }
  142. #endif
  143. internal Rectangle GetBorderRectangle ()
  144. {
  145. Rectangle screenRect = ViewportToScreen (Viewport);
  146. return new (
  147. screenRect.X + Math.Max (0, Thickness.Left - 1),
  148. screenRect.Y + Math.Max (0, Thickness.Top - 1),
  149. Math.Max (
  150. 0,
  151. screenRect.Width
  152. - Math.Max (
  153. 0,
  154. Math.Max (0, Thickness.Left - 1)
  155. + Math.Max (0, Thickness.Right - 1)
  156. )
  157. ),
  158. Math.Max (
  159. 0,
  160. screenRect.Height
  161. - Math.Max (
  162. 0,
  163. Math.Max (0, Thickness.Top - 1)
  164. + Math.Max (0, Thickness.Bottom - 1)
  165. )
  166. )
  167. );
  168. }
  169. // TODO: Make LineStyle nullable https://github.com/gui-cs/Terminal.Gui/issues/4021
  170. /// <summary>
  171. /// Sets the style of the border by changing the <see cref="Thickness"/>. This is a helper API for setting the
  172. /// <see cref="Thickness"/> to <c>(1,1,1,1)</c> and setting the line style of the views that comprise the border. If
  173. /// set to <see cref="LineStyle.None"/> no border will be drawn.
  174. /// </summary>
  175. public LineStyle LineStyle
  176. {
  177. get
  178. {
  179. if (_lineStyle.HasValue)
  180. {
  181. return _lineStyle.Value;
  182. }
  183. // TODO: Make Border.LineStyle inherit from the SuperView hierarchy
  184. // TODO: Right now, Window and FrameView use CM to set BorderStyle, which negates
  185. // TODO: all this.
  186. return Parent?.SuperView?.BorderStyle ?? LineStyle.None;
  187. }
  188. set => _lineStyle = value;
  189. }
  190. private BorderSettings _settings = BorderSettings.Title;
  191. /// <summary>
  192. /// Gets or sets the settings for the border.
  193. /// </summary>
  194. public BorderSettings Settings
  195. {
  196. get => _settings;
  197. set
  198. {
  199. if (value == _settings)
  200. {
  201. return;
  202. }
  203. _settings = value;
  204. Parent?.SetNeedsDraw ();
  205. }
  206. }
  207. /// <inheritdoc/>
  208. protected override bool OnDrawingContent ()
  209. {
  210. if (Thickness == Thickness.Empty)
  211. {
  212. return true;
  213. }
  214. Rectangle screenBounds = ViewportToScreen (Viewport);
  215. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  216. // The border adornment (and title) are drawn at the outermost edge of border;
  217. // For Border
  218. // ...thickness extends outward (border/title is always as far in as possible)
  219. // PERF: How about a call to Rectangle.Offset?
  220. Rectangle borderBounds = GetBorderRectangle ();
  221. int topTitleLineY = borderBounds.Y;
  222. int titleY = borderBounds.Y;
  223. var titleBarsLength = 0; // the little vertical thingies
  224. int maxTitleWidth = Math.Max (
  225. 0,
  226. Math.Min (
  227. Parent?.TitleTextFormatter.FormatAndGetSize ().Width ?? 0,
  228. Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
  229. )
  230. );
  231. if (Parent is { })
  232. {
  233. Parent.TitleTextFormatter.ConstrainToSize = new (maxTitleWidth, 1);
  234. }
  235. int sideLineLength = borderBounds.Height;
  236. bool canDrawBorder = borderBounds is { Width: > 0, Height: > 0 };
  237. LineStyle lineStyle = LineStyle;
  238. if (Settings.FastHasFlags (BorderSettings.Title))
  239. {
  240. if (Thickness.Top == 2)
  241. {
  242. topTitleLineY = borderBounds.Y - 1;
  243. titleY = topTitleLineY + 1;
  244. titleBarsLength = 2;
  245. }
  246. // ┌────┐
  247. //┌┘View└
  248. //│
  249. if (Thickness.Top == 3)
  250. {
  251. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  252. titleY = topTitleLineY + 1;
  253. titleBarsLength = 3;
  254. sideLineLength++;
  255. }
  256. // ┌────┐
  257. //┌┘View└
  258. //│
  259. if (Thickness.Top > 3)
  260. {
  261. topTitleLineY = borderBounds.Y - 2;
  262. titleY = topTitleLineY + 1;
  263. titleBarsLength = 3;
  264. sideLineLength++;
  265. }
  266. }
  267. if (Parent is { }
  268. && canDrawBorder
  269. && Thickness.Top > 0
  270. && maxTitleWidth > 0
  271. && Settings.FastHasFlags (BorderSettings.Title)
  272. && !string.IsNullOrEmpty (Parent?.Title))
  273. {
  274. Rectangle titleRect = new (borderBounds.X + 2, titleY, maxTitleWidth, 1);
  275. Parent.TitleTextFormatter.Draw (
  276. titleRect,
  277. GetAttributeForRole (Parent.HasFocus ? VisualRole.Focus : VisualRole.Normal),
  278. GetAttributeForRole (Parent.HasFocus ? VisualRole.HotFocus : VisualRole.HotNormal));
  279. Parent?.LineCanvas.Exclude (new (titleRect));
  280. }
  281. if (canDrawBorder && LineStyle != LineStyle.None)
  282. {
  283. LineCanvas? lc = Parent?.LineCanvas;
  284. bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height >= 1;
  285. bool drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  286. bool drawBottom = Thickness.Bottom > 0 && Frame.Width > 1 && Frame.Height > 1;
  287. bool drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  288. Attribute prevAttr = Driver?.GetAttribute () ?? Attribute.Default;
  289. SetAttributeForRole (VisualRole.Normal);
  290. if (drawTop)
  291. {
  292. // ╔╡Title╞═════╗
  293. // ╔╡╞═════╗
  294. if (borderBounds.Width < 4 || !Settings.FastHasFlags (BorderSettings.Title) || string.IsNullOrEmpty (Parent?.Title))
  295. {
  296. // ╔╡╞╗ should be ╔══╗
  297. lc?.AddLine (
  298. new (borderBounds.Location.X, titleY),
  299. borderBounds.Width,
  300. Orientation.Horizontal,
  301. lineStyle,
  302. Driver?.GetAttribute ()
  303. );
  304. }
  305. else
  306. {
  307. // ┌────┐
  308. //┌┘View└
  309. //│
  310. if (Thickness.Top == 2)
  311. {
  312. lc?.AddLine (
  313. new (borderBounds.X + 1, topTitleLineY),
  314. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  315. Orientation.Horizontal,
  316. lineStyle,
  317. Driver?.GetAttribute ()
  318. );
  319. }
  320. // ┌────┐
  321. //┌┘View└
  322. //│
  323. if (borderBounds.Width >= 4 && Thickness.Top > 2)
  324. {
  325. lc?.AddLine (
  326. new (borderBounds.X + 1, topTitleLineY),
  327. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  328. Orientation.Horizontal,
  329. lineStyle,
  330. Driver?.GetAttribute ()
  331. );
  332. lc?.AddLine (
  333. new (borderBounds.X + 1, topTitleLineY + 2),
  334. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  335. Orientation.Horizontal,
  336. lineStyle,
  337. Driver?.GetAttribute ()
  338. );
  339. }
  340. // ╔╡Title╞═════╗
  341. // Add a short horiz line for ╔╡
  342. lc?.AddLine (
  343. new (borderBounds.Location.X, titleY),
  344. 2,
  345. Orientation.Horizontal,
  346. lineStyle,
  347. Driver?.GetAttribute ()
  348. );
  349. // Add a vert line for ╔╡
  350. lc?.AddLine (
  351. new (borderBounds.X + 1, topTitleLineY),
  352. titleBarsLength,
  353. Orientation.Vertical,
  354. LineStyle.Single,
  355. Driver?.GetAttribute ()
  356. );
  357. // Add a vert line for ╞
  358. lc?.AddLine (
  359. new (
  360. borderBounds.X
  361. + 1
  362. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  363. - 1,
  364. topTitleLineY
  365. ),
  366. titleBarsLength,
  367. Orientation.Vertical,
  368. LineStyle.Single,
  369. Driver?.GetAttribute ()
  370. );
  371. // Add the right hand line for ╞═════╗
  372. lc?.AddLine (
  373. new (
  374. borderBounds.X
  375. + 1
  376. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  377. - 1,
  378. titleY
  379. ),
  380. borderBounds.Width - Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  381. Orientation.Horizontal,
  382. lineStyle,
  383. Driver?.GetAttribute ()
  384. );
  385. }
  386. }
  387. #if !SUBVIEW_BASED_BORDER
  388. if (drawLeft)
  389. {
  390. lc?.AddLine (
  391. new (borderBounds.Location.X, titleY),
  392. sideLineLength,
  393. Orientation.Vertical,
  394. lineStyle,
  395. Driver?.GetAttribute ()
  396. );
  397. }
  398. #endif
  399. if (drawBottom)
  400. {
  401. lc?.AddLine (
  402. new (borderBounds.X, borderBounds.Y + borderBounds.Height - 1),
  403. borderBounds.Width,
  404. Orientation.Horizontal,
  405. lineStyle,
  406. Driver?.GetAttribute ()
  407. );
  408. }
  409. if (drawRight)
  410. {
  411. lc?.AddLine (
  412. new (borderBounds.X + borderBounds.Width - 1, titleY),
  413. sideLineLength,
  414. Orientation.Vertical,
  415. lineStyle,
  416. Driver?.GetAttribute ()
  417. );
  418. }
  419. SetAttribute (prevAttr);
  420. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  421. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Ruler))
  422. {
  423. // Top
  424. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  425. if (drawTop)
  426. {
  427. hruler.Draw (new (screenBounds.X, screenBounds.Y));
  428. }
  429. // Redraw title
  430. if (drawTop && maxTitleWidth > 0 && Settings.FastHasFlags (BorderSettings.Title))
  431. {
  432. Parent!.TitleTextFormatter.Draw (
  433. new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  434. Parent.HasFocus ? Parent.GetAttributeForRole (VisualRole.Focus) : Parent.GetAttributeForRole (VisualRole.Normal),
  435. Parent.HasFocus ? Parent.GetAttributeForRole (VisualRole.Focus) : Parent.GetAttributeForRole (VisualRole.Normal));
  436. }
  437. //Left
  438. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  439. if (drawLeft)
  440. {
  441. vruler.Draw (new (screenBounds.X, screenBounds.Y + 1), 1);
  442. }
  443. // Bottom
  444. if (drawBottom)
  445. {
  446. hruler.Draw (new (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  447. }
  448. // Right
  449. if (drawRight)
  450. {
  451. vruler.Draw (new (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  452. }
  453. }
  454. // TODO: This should not be done on each draw?
  455. if (Settings.FastHasFlags (BorderSettings.Gradient))
  456. {
  457. SetupGradientLineCanvas (lc!, screenBounds);
  458. }
  459. else
  460. {
  461. lc!.Fill = null;
  462. }
  463. }
  464. return true;
  465. ;
  466. }
  467. /// <summary>
  468. /// Gets the subview used to render <see cref="ViewDiagnosticFlags.DrawIndicator"/>.
  469. /// </summary>
  470. public SpinnerView? DrawIndicator { get; private set; }
  471. private void SetupGradientLineCanvas (LineCanvas lc, Rectangle rect)
  472. {
  473. GetAppealingGradientColors (out List<Color> stops, out List<int> steps);
  474. var g = new Gradient (stops, steps);
  475. var fore = new GradientFill (rect, g, GradientDirection.Diagonal);
  476. var back = new SolidFill (GetAttributeForRole (VisualRole.Normal).Background);
  477. lc.Fill = new (fore, back);
  478. }
  479. private static void GetAppealingGradientColors (out List<Color> stops, out List<int> steps)
  480. {
  481. // Define the colors of the gradient stops with more appealing colors
  482. stops =
  483. [
  484. new (0, 128, 255), // Bright Blue
  485. new (0, 255, 128), // Bright Green
  486. new (255, 255), // Bright Yellow
  487. new (255, 128), // Bright Orange
  488. new (255, 0, 128)
  489. ];
  490. // Define the number of steps between each color for smoother transitions
  491. // If we pass only a single value then it will assume equal steps between all pairs
  492. steps = [15];
  493. }
  494. }