Border.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. Application.GrabbingMouse += Application_GrabbingMouse;
  55. Application.UnGrabbingMouse += Application_UnGrabbingMouse;
  56. HighlightStyle |= HighlightStyle.Pressed;
  57. Highlight += Border_Highlight;
  58. }
  59. #if SUBVIEW_BASED_BORDER
  60. private Line _left;
  61. /// <summary>
  62. /// The close button for the border. Set to <see cref="View.Visible"/>, to <see langword="true"/> to enable.
  63. /// </summary>
  64. public Button CloseButton { get; internal set; }
  65. #endif
  66. /// <inheritdoc/>
  67. public override void BeginInit ()
  68. {
  69. base.BeginInit ();
  70. #if SUBVIEW_BASED_BORDER
  71. if (Parent is { })
  72. {
  73. // Left
  74. _left = new ()
  75. {
  76. Orientation = Orientation.Vertical,
  77. };
  78. Add (_left);
  79. CloseButton = new Button ()
  80. {
  81. Text = "X",
  82. CanFocus = true,
  83. Visible = false,
  84. };
  85. CloseButton.Accept += (s, e) =>
  86. {
  87. e.Cancel = Parent.InvokeCommand (Command.QuitToplevel) == true;
  88. };
  89. Add (CloseButton);
  90. LayoutStarted += OnLayoutStarted;
  91. }
  92. #endif
  93. }
  94. #if SUBVIEW_BASED_BORDER
  95. private void OnLayoutStarted (object sender, LayoutEventArgs e)
  96. {
  97. _left.Border.LineStyle = LineStyle;
  98. _left.X = Thickness.Left - 1;
  99. _left.Y = Thickness.Top - 1;
  100. _left.Width = 1;
  101. _left.Height = Height;
  102. CloseButton.X = Pos.AnchorEnd (Thickness.Right / 2 + 1) -
  103. (Pos.Right (CloseButton) -
  104. Pos.Left (CloseButton));
  105. CloseButton.Y = 0;
  106. }
  107. #endif
  108. /// <summary>
  109. /// The color scheme for the Border. If set to <see langword="null"/>, gets the <see cref="Adornment.Parent"/>
  110. /// scheme. color scheme.
  111. /// </summary>
  112. public override ColorScheme ColorScheme
  113. {
  114. get
  115. {
  116. if (base.ColorScheme is { })
  117. {
  118. return base.ColorScheme;
  119. }
  120. return Parent?.ColorScheme;
  121. }
  122. set
  123. {
  124. base.ColorScheme = value;
  125. Parent?.SetNeedsDisplay ();
  126. }
  127. }
  128. internal Rectangle GetBorderRectangle ()
  129. {
  130. Rectangle screenRect = ViewportToScreen (Viewport);
  131. return new (
  132. screenRect.X + Math.Max (0, Thickness.Left - 1),
  133. screenRect.Y + Math.Max (0, Thickness.Top - 1),
  134. Math.Max (
  135. 0,
  136. screenRect.Width
  137. - Math.Max (
  138. 0,
  139. Math.Max (0, Thickness.Left - 1)
  140. + Math.Max (0, Thickness.Right - 1)
  141. )
  142. ),
  143. Math.Max (
  144. 0,
  145. screenRect.Height
  146. - Math.Max (
  147. 0,
  148. Math.Max (0, Thickness.Top - 1)
  149. + Math.Max (0, Thickness.Bottom - 1)
  150. )
  151. )
  152. );
  153. }
  154. /// <summary>
  155. /// Sets the style of the border by changing the <see cref="Thickness"/>. This is a helper API for setting the
  156. /// <see cref="Thickness"/> to <c>(1,1,1,1)</c> and setting the line style of the views that comprise the border. If
  157. /// set to <see cref="LineStyle.None"/> no border will be drawn.
  158. /// </summary>
  159. public LineStyle LineStyle
  160. {
  161. get
  162. {
  163. if (_lineStyle.HasValue)
  164. {
  165. return _lineStyle.Value;
  166. }
  167. // TODO: Make Border.LineStyle inherit from the SuperView hierarchy
  168. // TODO: Right now, Window and FrameView use CM to set BorderStyle, which negates
  169. // TODO: all this.
  170. return Parent.SuperView?.BorderStyle ?? LineStyle.None;
  171. }
  172. set => _lineStyle = value;
  173. }
  174. private BorderSettings _settings = BorderSettings.Title;
  175. /// <summary>
  176. /// Gets or sets the settings for the border.
  177. /// </summary>
  178. public BorderSettings Settings
  179. {
  180. get => _settings;
  181. set
  182. {
  183. if (value == _settings)
  184. {
  185. return;
  186. }
  187. _settings = value;
  188. Parent?.SetNeedsDisplay ();
  189. }
  190. }
  191. #region Mouse Support
  192. private Color? _savedForeColor;
  193. private void Border_Highlight (object sender, CancelEventArgs<HighlightStyle> e)
  194. {
  195. if (!Parent.Arrangement.HasFlag (ViewArrangement.Movable))
  196. {
  197. e.Cancel = true;
  198. return;
  199. }
  200. if (e.NewValue.HasFlag (HighlightStyle.Pressed))
  201. {
  202. if (!_savedForeColor.HasValue)
  203. {
  204. _savedForeColor = ColorScheme.Normal.Foreground;
  205. }
  206. var cs = new ColorScheme (ColorScheme)
  207. {
  208. Normal = new (ColorScheme.Normal.Foreground.GetHighlightColor (), ColorScheme.Normal.Background)
  209. };
  210. ColorScheme = cs;
  211. }
  212. if (e.NewValue == HighlightStyle.None && _savedForeColor.HasValue)
  213. {
  214. var cs = new ColorScheme (ColorScheme)
  215. {
  216. Normal = new (_savedForeColor.Value, ColorScheme.Normal.Background)
  217. };
  218. ColorScheme = cs;
  219. }
  220. Parent?.SetNeedsDisplay ();
  221. e.Cancel = true;
  222. }
  223. private Point? _dragPosition;
  224. private Point _startGrabPoint;
  225. /// <inheritdoc/>
  226. protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
  227. {
  228. if (base.OnMouseEvent (mouseEvent))
  229. {
  230. return true;
  231. }
  232. // BUGBUG: Shouldn't non-focusable views be draggable??
  233. //if (!Parent.CanFocus)
  234. //{
  235. // return false;
  236. //}
  237. if (!Parent.Arrangement.HasFlag (ViewArrangement.Movable))
  238. {
  239. return false;
  240. }
  241. // BUGBUG: See https://github.com/gui-cs/Terminal.Gui/issues/3312
  242. if (!_dragPosition.HasValue && mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  243. {
  244. Parent.SetFocus ();
  245. ApplicationOverlapped.BringOverlappedTopToFront ();
  246. // Only start grabbing if the user clicks in the Thickness area
  247. // Adornment.Contains takes Parent SuperView=relative coords.
  248. if (Contains (new (mouseEvent.Position.X + Parent.Frame.X + Frame.X, mouseEvent.Position.Y + Parent.Frame.Y + Frame.Y)))
  249. {
  250. // Set the start grab point to the Frame coords
  251. _startGrabPoint = new (mouseEvent.Position.X + Frame.X, mouseEvent.Position.Y + Frame.Y);
  252. _dragPosition = mouseEvent.Position;
  253. Application.GrabMouse (this);
  254. SetHighlight (HighlightStyle);
  255. }
  256. return true;
  257. }
  258. if (mouseEvent.Flags is (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  259. {
  260. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  261. {
  262. if (Parent.SuperView is null)
  263. {
  264. // Redraw the entire app window.
  265. Application.Top.SetNeedsDisplay ();
  266. }
  267. else
  268. {
  269. Parent.SuperView.SetNeedsDisplay ();
  270. }
  271. _dragPosition = mouseEvent.Position;
  272. Point parentLoc = Parent.SuperView?.ScreenToViewport (new (mouseEvent.ScreenPosition.X, mouseEvent.ScreenPosition.Y))
  273. ?? mouseEvent.ScreenPosition;
  274. GetLocationEnsuringFullVisibility (
  275. Parent,
  276. parentLoc.X - _startGrabPoint.X,
  277. parentLoc.Y - _startGrabPoint.Y,
  278. out int nx,
  279. out int ny,
  280. out _
  281. );
  282. Parent.X = nx;
  283. Parent.Y = ny;
  284. return true;
  285. }
  286. }
  287. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && _dragPosition.HasValue)
  288. {
  289. _dragPosition = null;
  290. Application.UngrabMouse ();
  291. SetHighlight (HighlightStyle.None);
  292. return true;
  293. }
  294. return false;
  295. }
  296. /// <inheritdoc/>
  297. protected override void Dispose (bool disposing)
  298. {
  299. Application.GrabbingMouse -= Application_GrabbingMouse;
  300. Application.UnGrabbingMouse -= Application_UnGrabbingMouse;
  301. _dragPosition = null;
  302. base.Dispose (disposing);
  303. }
  304. private void Application_GrabbingMouse (object sender, GrabMouseEventArgs e)
  305. {
  306. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  307. {
  308. e.Cancel = true;
  309. }
  310. }
  311. private void Application_UnGrabbingMouse (object sender, GrabMouseEventArgs e)
  312. {
  313. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  314. {
  315. e.Cancel = true;
  316. }
  317. }
  318. #endregion Mouse Support
  319. /// <inheritdoc/>
  320. public override void OnDrawContent (Rectangle viewport)
  321. {
  322. base.OnDrawContent (viewport);
  323. if (Thickness == Thickness.Empty)
  324. {
  325. return;
  326. }
  327. //Driver.SetAttribute (Colors.ColorSchemes ["Error"].Normal);
  328. Rectangle screenBounds = ViewportToScreen (viewport);
  329. //OnDrawSubviews (bounds);
  330. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  331. // The border adornment (and title) are drawn at the outermost edge of border;
  332. // For Border
  333. // ...thickness extends outward (border/title is always as far in as possible)
  334. // PERF: How about a call to Rectangle.Offset?
  335. Rectangle borderBounds = GetBorderRectangle ();
  336. int topTitleLineY = borderBounds.Y;
  337. int titleY = borderBounds.Y;
  338. var titleBarsLength = 0; // the little vertical thingies
  339. int maxTitleWidth = Math.Max (
  340. 0,
  341. Math.Min (
  342. Parent.TitleTextFormatter.FormatAndGetSize ().Width,
  343. Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
  344. )
  345. );
  346. Parent.TitleTextFormatter.ConstrainToSize = new (maxTitleWidth, 1);
  347. int sideLineLength = borderBounds.Height;
  348. bool canDrawBorder = borderBounds is { Width: > 0, Height: > 0 };
  349. if (Settings.FastHasFlags (BorderSettings.Title))
  350. {
  351. if (Thickness.Top == 2)
  352. {
  353. topTitleLineY = borderBounds.Y - 1;
  354. titleY = topTitleLineY + 1;
  355. titleBarsLength = 2;
  356. }
  357. // ┌────┐
  358. //┌┘View└
  359. //│
  360. if (Thickness.Top == 3)
  361. {
  362. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  363. titleY = topTitleLineY + 1;
  364. titleBarsLength = 3;
  365. sideLineLength++;
  366. }
  367. // ┌────┐
  368. //┌┘View└
  369. //│
  370. if (Thickness.Top > 3)
  371. {
  372. topTitleLineY = borderBounds.Y - 2;
  373. titleY = topTitleLineY + 1;
  374. titleBarsLength = 3;
  375. sideLineLength++;
  376. }
  377. }
  378. if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && Settings.FastHasFlags (BorderSettings.Title) && !string.IsNullOrEmpty (Parent?.Title))
  379. {
  380. Attribute focus = Parent.GetNormalColor ();
  381. if (Parent.SuperView is { } && Parent.SuperView?.Subviews!.Count (s => s.CanFocus) > 1)
  382. {
  383. // Only use focus color if there are multiple focusable views
  384. focus = Parent.GetFocusColor ();
  385. }
  386. Parent.TitleTextFormatter.Draw (
  387. new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  388. Parent.HasFocus ? focus : Parent.GetNormalColor (),
  389. Parent.HasFocus ? focus : Parent.GetHotNormalColor ());
  390. }
  391. if (canDrawBorder && LineStyle != LineStyle.None)
  392. {
  393. LineCanvas lc = Parent?.LineCanvas;
  394. bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height >= 1;
  395. bool drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  396. bool drawBottom = Thickness.Bottom > 0 && Frame.Width > 1 && Frame.Height > 1;
  397. bool drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  398. Attribute prevAttr = Driver.GetAttribute ();
  399. if (ColorScheme is { })
  400. {
  401. Driver.SetAttribute (GetNormalColor ());
  402. }
  403. else
  404. {
  405. Driver.SetAttribute (Parent.GetNormalColor ());
  406. }
  407. if (drawTop)
  408. {
  409. // ╔╡Title╞═════╗
  410. // ╔╡╞═════╗
  411. if (borderBounds.Width < 4 || !Settings.FastHasFlags (BorderSettings.Title) || string.IsNullOrEmpty (Parent?.Title))
  412. {
  413. // ╔╡╞╗ should be ╔══╗
  414. lc.AddLine (
  415. new (borderBounds.Location.X, titleY),
  416. borderBounds.Width,
  417. Orientation.Horizontal,
  418. LineStyle,
  419. Driver.GetAttribute ()
  420. );
  421. }
  422. else
  423. {
  424. // ┌────┐
  425. //┌┘View└
  426. //│
  427. if (Thickness.Top == 2)
  428. {
  429. lc.AddLine (
  430. new (borderBounds.X + 1, topTitleLineY),
  431. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  432. Orientation.Horizontal,
  433. LineStyle,
  434. Driver.GetAttribute ()
  435. );
  436. }
  437. // ┌────┐
  438. //┌┘View└
  439. //│
  440. if (borderBounds.Width >= 4 && Thickness.Top > 2)
  441. {
  442. lc.AddLine (
  443. new (borderBounds.X + 1, topTitleLineY),
  444. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  445. Orientation.Horizontal,
  446. LineStyle,
  447. Driver.GetAttribute ()
  448. );
  449. lc.AddLine (
  450. new (borderBounds.X + 1, topTitleLineY + 2),
  451. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  452. Orientation.Horizontal,
  453. LineStyle,
  454. Driver.GetAttribute ()
  455. );
  456. }
  457. // ╔╡Title╞═════╗
  458. // Add a short horiz line for ╔╡
  459. lc.AddLine (
  460. new (borderBounds.Location.X, titleY),
  461. 2,
  462. Orientation.Horizontal,
  463. LineStyle,
  464. Driver.GetAttribute ()
  465. );
  466. // Add a vert line for ╔╡
  467. lc.AddLine (
  468. new (borderBounds.X + 1, topTitleLineY),
  469. titleBarsLength,
  470. Orientation.Vertical,
  471. LineStyle.Single,
  472. Driver.GetAttribute ()
  473. );
  474. // Add a vert line for ╞
  475. lc.AddLine (
  476. new (
  477. borderBounds.X
  478. + 1
  479. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  480. - 1,
  481. topTitleLineY
  482. ),
  483. titleBarsLength,
  484. Orientation.Vertical,
  485. LineStyle.Single,
  486. Driver.GetAttribute ()
  487. );
  488. // Add the right hand line for ╞═════╗
  489. lc.AddLine (
  490. new (
  491. borderBounds.X
  492. + 1
  493. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  494. - 1,
  495. titleY
  496. ),
  497. borderBounds.Width - Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  498. Orientation.Horizontal,
  499. LineStyle,
  500. Driver.GetAttribute ()
  501. );
  502. }
  503. }
  504. #if !SUBVIEW_BASED_BORDER
  505. if (drawLeft)
  506. {
  507. lc.AddLine (
  508. new (borderBounds.Location.X, titleY),
  509. sideLineLength,
  510. Orientation.Vertical,
  511. LineStyle,
  512. Driver.GetAttribute ()
  513. );
  514. }
  515. #endif
  516. if (drawBottom)
  517. {
  518. lc.AddLine (
  519. new (borderBounds.X, borderBounds.Y + borderBounds.Height - 1),
  520. borderBounds.Width,
  521. Orientation.Horizontal,
  522. LineStyle,
  523. Driver.GetAttribute ()
  524. );
  525. }
  526. if (drawRight)
  527. {
  528. lc.AddLine (
  529. new (borderBounds.X + borderBounds.Width - 1, titleY),
  530. sideLineLength,
  531. Orientation.Vertical,
  532. LineStyle,
  533. Driver.GetAttribute ()
  534. );
  535. }
  536. Driver.SetAttribute (prevAttr);
  537. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  538. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Ruler))
  539. {
  540. // Top
  541. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  542. if (drawTop)
  543. {
  544. hruler.Draw (new (screenBounds.X, screenBounds.Y));
  545. }
  546. // Redraw title
  547. if (drawTop && maxTitleWidth > 0 && Settings.FastHasFlags (BorderSettings.Title))
  548. {
  549. Parent.TitleTextFormatter.Draw (
  550. new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  551. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor (),
  552. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor ());
  553. }
  554. //Left
  555. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  556. if (drawLeft)
  557. {
  558. vruler.Draw (new (screenBounds.X, screenBounds.Y + 1), 1);
  559. }
  560. // Bottom
  561. if (drawBottom)
  562. {
  563. hruler.Draw (new (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  564. }
  565. // Right
  566. if (drawRight)
  567. {
  568. vruler.Draw (new (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  569. }
  570. }
  571. // TODO: This should not be done on each draw?
  572. if (Settings.FastHasFlags (BorderSettings.Gradient))
  573. {
  574. SetupGradientLineCanvas (lc, screenBounds);
  575. }
  576. else
  577. {
  578. lc.Fill = null;
  579. }
  580. }
  581. }
  582. private void SetupGradientLineCanvas (LineCanvas lc, Rectangle rect)
  583. {
  584. GetAppealingGradientColors (out List<Color> stops, out List<int> steps);
  585. var g = new Gradient (stops, steps);
  586. var fore = new GradientFill (rect, g, GradientDirection.Diagonal);
  587. var back = new SolidFill (GetNormalColor ().Background);
  588. lc.Fill = new (fore, back);
  589. }
  590. private static void GetAppealingGradientColors (out List<Color> stops, out List<int> steps)
  591. {
  592. // Define the colors of the gradient stops with more appealing colors
  593. stops = new()
  594. {
  595. new (0, 128, 255), // Bright Blue
  596. new (0, 255, 128), // Bright Green
  597. new (255, 255), // Bright Yellow
  598. new (255, 128), // Bright Orange
  599. new (255, 0, 128) // Bright Pink
  600. };
  601. // Define the number of steps between each color for smoother transitions
  602. // If we pass only a single value then it will assume equal steps between all pairs
  603. steps = new() { 15 };
  604. }
  605. }