Border.cs 22 KB

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