Border.cs 20 KB

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