Border.cs 23 KB

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