Border.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 || !Parent.Arrangement.HasFlag (ViewArrangement.Movable))
  182. {
  183. return true;
  184. }
  185. // BUGBUG: See https://github.com/gui-cs/Terminal.Gui/issues/3312
  186. if (!_dragPosition.HasValue && mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  187. {
  188. Parent.SetFocus ();
  189. Application.BringOverlappedTopToFront ();
  190. // Only start grabbing if the user clicks in the Thickness area
  191. // Adornment.Contains takes Parent SuperView=relative coords.
  192. if (Contains (mouseEvent.X + Parent.Frame.X + Frame.X, mouseEvent.Y + Parent.Frame.Y + Frame.Y))
  193. {
  194. // Set the start grab point to the Frame coords
  195. _startGrabPoint = new (mouseEvent.X + Frame.X, mouseEvent.Y + Frame.Y);
  196. _dragPosition = new (mouseEvent.X, mouseEvent.Y);
  197. Application.GrabMouse (this);
  198. }
  199. return true;
  200. }
  201. if (mouseEvent.Flags is (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))
  202. {
  203. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  204. {
  205. if (Parent.SuperView is null)
  206. {
  207. // Redraw the entire app window.
  208. Application.Top.SetNeedsDisplay ();
  209. }
  210. else
  211. {
  212. Parent.SuperView.SetNeedsDisplay ();
  213. }
  214. _dragPosition = new Point (mouseEvent.X, mouseEvent.Y);
  215. Point parentLoc = Parent.SuperView?.ScreenToBounds (mouseEvent.ScreenPosition.X, mouseEvent.ScreenPosition.Y) ?? mouseEvent.ScreenPosition;
  216. GetLocationEnsuringFullVisibility (
  217. Parent,
  218. parentLoc.X - _startGrabPoint.X,
  219. parentLoc.Y - _startGrabPoint.Y,
  220. out int nx,
  221. out int ny,
  222. out _
  223. );
  224. Parent.X = nx;
  225. Parent.Y = ny;
  226. return true;
  227. }
  228. }
  229. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && _dragPosition.HasValue)
  230. {
  231. _dragPosition = null;
  232. Application.UngrabMouse ();
  233. return true;
  234. }
  235. return false;
  236. }
  237. /// <inheritdoc/>
  238. protected override void Dispose (bool disposing)
  239. {
  240. Application.GrabbingMouse -= Application_GrabbingMouse;
  241. Application.UnGrabbingMouse -= Application_UnGrabbingMouse;
  242. _dragPosition = null;
  243. base.Dispose (disposing);
  244. }
  245. private void Application_GrabbingMouse (object sender, GrabMouseEventArgs e)
  246. {
  247. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  248. {
  249. e.Cancel = true;
  250. }
  251. }
  252. private void Application_UnGrabbingMouse (object sender, GrabMouseEventArgs e)
  253. {
  254. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  255. {
  256. e.Cancel = true;
  257. }
  258. }
  259. #endregion Mouse Support
  260. /// <inheritdoc/>
  261. public override void OnDrawContent (Rectangle contentArea)
  262. {
  263. base.OnDrawContent (contentArea);
  264. if (Thickness == Thickness.Empty)
  265. {
  266. return;
  267. }
  268. //Driver.SetAttribute (Colors.ColorSchemes ["Error"].Normal);
  269. Rectangle screenBounds = BoundsToScreen (contentArea);
  270. //OnDrawSubviews (bounds);
  271. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  272. // The border adornment (and title) are drawn at the outermost edge of border;
  273. // For Border
  274. // ...thickness extends outward (border/title is always as far in as possible)
  275. // PERF: How about a call to Rectangle.Offset?
  276. var borderBounds = GetBorderBounds (screenBounds);
  277. int topTitleLineY = borderBounds.Y;
  278. int titleY = borderBounds.Y;
  279. var titleBarsLength = 0; // the little vertical thingies
  280. int maxTitleWidth = Math.Max (
  281. 0,
  282. Math.Min (
  283. Parent.TitleTextFormatter.FormatAndGetSize ().Width,
  284. Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
  285. )
  286. );
  287. Parent.TitleTextFormatter.Size = new (maxTitleWidth, 1);
  288. int sideLineLength = borderBounds.Height;
  289. bool canDrawBorder = borderBounds is { Width: > 0, Height: > 0 };
  290. if (!string.IsNullOrEmpty (Parent?.Title))
  291. {
  292. if (Thickness.Top == 2)
  293. {
  294. topTitleLineY = borderBounds.Y - 1;
  295. titleY = topTitleLineY + 1;
  296. titleBarsLength = 2;
  297. }
  298. // ┌────┐
  299. //┌┘View└
  300. //│
  301. if (Thickness.Top == 3)
  302. {
  303. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  304. titleY = topTitleLineY + 1;
  305. titleBarsLength = 3;
  306. sideLineLength++;
  307. }
  308. // ┌────┐
  309. //┌┘View└
  310. //│
  311. if (Thickness.Top > 3)
  312. {
  313. topTitleLineY = borderBounds.Y - 2;
  314. titleY = topTitleLineY + 1;
  315. titleBarsLength = 3;
  316. sideLineLength++;
  317. }
  318. }
  319. if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title))
  320. {
  321. Parent.TitleTextFormatter.Draw (
  322. new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  323. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor (),
  324. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetHotNormalColor ());
  325. }
  326. if (canDrawBorder && LineStyle != LineStyle.None)
  327. {
  328. LineCanvas lc = Parent?.LineCanvas;
  329. bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height > 1;
  330. bool drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  331. bool drawBottom = Thickness.Bottom > 0 && Frame.Width > 1;
  332. bool drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  333. Attribute prevAttr = Driver.GetAttribute ();
  334. if (ColorScheme is { })
  335. {
  336. Driver.SetAttribute (GetNormalColor ());
  337. }
  338. else
  339. {
  340. Driver.SetAttribute (Parent.GetNormalColor ());
  341. }
  342. if (drawTop)
  343. {
  344. // ╔╡Title╞═════╗
  345. // ╔╡╞═════╗
  346. if (borderBounds.Width < 4 || string.IsNullOrEmpty (Parent?.Title))
  347. {
  348. // ╔╡╞╗ should be ╔══╗
  349. lc.AddLine (
  350. new (borderBounds.Location.X, titleY),
  351. borderBounds.Width,
  352. Orientation.Horizontal,
  353. LineStyle,
  354. Driver.GetAttribute ()
  355. );
  356. }
  357. else
  358. {
  359. // ┌────┐
  360. //┌┘View└
  361. //│
  362. if (Thickness.Top == 2)
  363. {
  364. lc.AddLine (
  365. new (borderBounds.X + 1, topTitleLineY),
  366. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  367. Orientation.Horizontal,
  368. LineStyle,
  369. Driver.GetAttribute ()
  370. );
  371. }
  372. // ┌────┐
  373. //┌┘View└
  374. //│
  375. if (borderBounds.Width >= 4 && Thickness.Top > 2)
  376. {
  377. lc.AddLine (
  378. new (borderBounds.X + 1, topTitleLineY),
  379. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  380. Orientation.Horizontal,
  381. LineStyle,
  382. Driver.GetAttribute ()
  383. );
  384. lc.AddLine (
  385. new (borderBounds.X + 1, topTitleLineY + 2),
  386. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  387. Orientation.Horizontal,
  388. LineStyle,
  389. Driver.GetAttribute ()
  390. );
  391. }
  392. // ╔╡Title╞═════╗
  393. // Add a short horiz line for ╔╡
  394. lc.AddLine (
  395. new (borderBounds.Location.X, titleY),
  396. 2,
  397. Orientation.Horizontal,
  398. LineStyle,
  399. Driver.GetAttribute ()
  400. );
  401. // Add a vert line for ╔╡
  402. lc.AddLine (
  403. new (borderBounds.X + 1, topTitleLineY),
  404. titleBarsLength,
  405. Orientation.Vertical,
  406. LineStyle.Single,
  407. Driver.GetAttribute ()
  408. );
  409. // Add a vert line for ╞
  410. lc.AddLine (
  411. new (
  412. borderBounds.X
  413. + 1
  414. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  415. - 1,
  416. topTitleLineY
  417. ),
  418. titleBarsLength,
  419. Orientation.Vertical,
  420. LineStyle.Single,
  421. Driver.GetAttribute ()
  422. );
  423. // Add the right hand line for ╞═════╗
  424. lc.AddLine (
  425. new (
  426. borderBounds.X
  427. + 1
  428. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  429. - 1,
  430. titleY
  431. ),
  432. borderBounds.Width - Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  433. Orientation.Horizontal,
  434. LineStyle,
  435. Driver.GetAttribute ()
  436. );
  437. }
  438. }
  439. #if !SUBVIEW_BASED_BORDER
  440. if (drawLeft)
  441. {
  442. lc.AddLine (
  443. new (borderBounds.Location.X, titleY),
  444. sideLineLength,
  445. Orientation.Vertical,
  446. LineStyle,
  447. Driver.GetAttribute ()
  448. );
  449. }
  450. #endif
  451. if (drawBottom)
  452. {
  453. lc.AddLine (
  454. new (borderBounds.X, borderBounds.Y + borderBounds.Height - 1),
  455. borderBounds.Width,
  456. Orientation.Horizontal,
  457. LineStyle,
  458. Driver.GetAttribute ()
  459. );
  460. }
  461. if (drawRight)
  462. {
  463. lc.AddLine (
  464. new (borderBounds.X + borderBounds.Width - 1, titleY),
  465. sideLineLength,
  466. Orientation.Vertical,
  467. LineStyle,
  468. Driver.GetAttribute ()
  469. );
  470. }
  471. Driver.SetAttribute (prevAttr);
  472. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  473. if (View.Diagnostics.HasFlag (ViewDiagnosticFlags.Ruler))
  474. {
  475. // Top
  476. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  477. if (drawTop)
  478. {
  479. hruler.Draw (new (screenBounds.X, screenBounds.Y));
  480. }
  481. // Redraw title
  482. if (drawTop && maxTitleWidth > 0 && !string.IsNullOrEmpty (Parent?.Title))
  483. {
  484. Parent.TitleTextFormatter.Draw (
  485. new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  486. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor (),
  487. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor ());
  488. }
  489. //Left
  490. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  491. if (drawLeft)
  492. {
  493. vruler.Draw (new (screenBounds.X, screenBounds.Y + 1), 1);
  494. }
  495. // Bottom
  496. if (drawBottom)
  497. {
  498. hruler.Draw (new (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  499. }
  500. // Right
  501. if (drawRight)
  502. {
  503. vruler.Draw (new (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  504. }
  505. }
  506. }
  507. //base.OnDrawContent (contentArea);
  508. }
  509. }