Border.cs 21 KB

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