Border.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. #nullable enable
  2. using System.Diagnostics;
  3. using Microsoft.CodeAnalysis;
  4. using static Terminal.Gui.SpinnerStyle;
  5. namespace Terminal.Gui;
  6. /// <summary>The Border for a <see cref="View"/>.</summary>
  7. /// <remarks>
  8. /// <para>
  9. /// Renders a border around the view with the <see cref="View.Title"/>. A border using <see cref="LineStyle"/>
  10. /// will be drawn on the sides of <see cref="Thickness"/> that are greater than zero.
  11. /// </para>
  12. /// <para>
  13. /// The <see cref="View.Title"/> of <see cref="Adornment.Parent"/> will be drawn based on the value of
  14. /// <see cref="Thickness.Top"/>:
  15. /// </para>
  16. /// <para>
  17. /// If <c>1</c>:
  18. /// <code>
  19. /// ┌┤1234├──┐
  20. /// │ │
  21. /// └────────┘
  22. /// </code>
  23. /// </para>
  24. /// <para>
  25. /// If <c>2</c>:
  26. /// <code>
  27. /// ┌────┐
  28. /// ┌┤1234├──┐
  29. /// │ │
  30. /// └────────┘
  31. /// </code>
  32. /// </para>
  33. /// <para>
  34. /// If <c>3</c>:
  35. /// <code>
  36. /// ┌────┐
  37. /// ┌┤1234├──┐
  38. /// │└────┘ │
  39. /// │ │
  40. /// └────────┘
  41. /// </code>
  42. /// </para>
  43. /// <para/>
  44. /// <para>See the <see cref="Adornment"/> class.</para>
  45. /// </remarks>
  46. public class Border : Adornment
  47. {
  48. private LineStyle? _lineStyle;
  49. /// <inheritdoc/>
  50. public Border ()
  51. { /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
  52. }
  53. /// <inheritdoc/>
  54. public Border (View parent) : base (parent)
  55. {
  56. Parent = parent;
  57. CanFocus = false;
  58. Application.GrabbingMouse += Application_GrabbingMouse;
  59. Application.UnGrabbingMouse += Application_UnGrabbingMouse;
  60. HighlightStyle |= HighlightStyle.Pressed;
  61. Highlight += Border_Highlight;
  62. }
  63. #if SUBVIEW_BASED_BORDER
  64. private Line _left;
  65. /// <summary>
  66. /// The close button for the border. Set to <see cref="View.Visible"/>, to <see langword="true"/> to enable.
  67. /// </summary>
  68. public Button CloseButton { get; internal set; }
  69. #endif
  70. /// <inheritdoc/>
  71. public override void BeginInit ()
  72. {
  73. #if HOVER
  74. // TOOD: Hack - make Arrangement overridable
  75. if ((Parent?.Arrangement & ViewArrangement.Movable) != 0)
  76. {
  77. HighlightStyle |= HighlightStyle.Hover;
  78. }
  79. #endif
  80. base.BeginInit ();
  81. #if SUBVIEW_BASED_BORDER
  82. if (Parent is { })
  83. {
  84. // Left
  85. _left = new ()
  86. {
  87. Orientation = Orientation.Vertical,
  88. };
  89. Add (_left);
  90. CloseButton = new Button ()
  91. {
  92. Text = "X",
  93. CanFocus = true,
  94. Visible = false,
  95. };
  96. CloseButton.Accept += (s, e) =>
  97. {
  98. e.Cancel = Parent.InvokeCommand (Command.QuitToplevel) == true;
  99. };
  100. Add (CloseButton);
  101. LayoutStarted += OnLayoutStarted;
  102. }
  103. #endif
  104. }
  105. #if SUBVIEW_BASED_BORDER
  106. private void OnLayoutStarted (object sender, LayoutEventArgs e)
  107. {
  108. _left.Border.LineStyle = LineStyle;
  109. _left.X = Thickness.Left - 1;
  110. _left.Y = Thickness.Top - 1;
  111. _left.Width = 1;
  112. _left.Height = Height;
  113. CloseButton.X = Pos.AnchorEnd (Thickness.Right / 2 + 1) -
  114. (Pos.Right (CloseButton) -
  115. Pos.Left (CloseButton));
  116. CloseButton.Y = 0;
  117. }
  118. #endif
  119. /// <summary>
  120. /// The color scheme for the Border. If set to <see langword="null"/>, gets the <see cref="Adornment.Parent"/>
  121. /// scheme. color scheme.
  122. /// </summary>
  123. public override ColorScheme? ColorScheme
  124. {
  125. get
  126. {
  127. if (base.ColorScheme is { })
  128. {
  129. return base.ColorScheme;
  130. }
  131. return Parent?.ColorScheme;
  132. }
  133. set
  134. {
  135. base.ColorScheme = value;
  136. Parent?.SetNeedsDisplay ();
  137. }
  138. }
  139. internal Rectangle GetBorderRectangle ()
  140. {
  141. Rectangle screenRect = ViewportToScreen (Viewport);
  142. return new (
  143. screenRect.X + Math.Max (0, Thickness.Left - 1),
  144. screenRect.Y + Math.Max (0, Thickness.Top - 1),
  145. Math.Max (
  146. 0,
  147. screenRect.Width
  148. - Math.Max (
  149. 0,
  150. Math.Max (0, Thickness.Left - 1)
  151. + Math.Max (0, Thickness.Right - 1)
  152. )
  153. ),
  154. Math.Max (
  155. 0,
  156. screenRect.Height
  157. - Math.Max (
  158. 0,
  159. Math.Max (0, Thickness.Top - 1)
  160. + Math.Max (0, Thickness.Bottom - 1)
  161. )
  162. )
  163. );
  164. }
  165. /// <summary>
  166. /// Sets the style of the border by changing the <see cref="Thickness"/>. This is a helper API for setting the
  167. /// <see cref="Thickness"/> to <c>(1,1,1,1)</c> and setting the line style of the views that comprise the border. If
  168. /// set to <see cref="LineStyle.None"/> no border will be drawn.
  169. /// </summary>
  170. public LineStyle LineStyle
  171. {
  172. get
  173. {
  174. if (_lineStyle.HasValue)
  175. {
  176. return _lineStyle.Value;
  177. }
  178. // TODO: Make Border.LineStyle inherit from the SuperView hierarchy
  179. // TODO: Right now, Window and FrameView use CM to set BorderStyle, which negates
  180. // TODO: all this.
  181. return Parent!.SuperView?.BorderStyle ?? LineStyle.None;
  182. }
  183. set => _lineStyle = value;
  184. }
  185. private BorderSettings _settings = BorderSettings.Title;
  186. /// <summary>
  187. /// Gets or sets the settings for the border.
  188. /// </summary>
  189. public BorderSettings Settings
  190. {
  191. get => _settings;
  192. set
  193. {
  194. if (value == _settings)
  195. {
  196. return;
  197. }
  198. _settings = value;
  199. Parent?.SetNeedsDisplay ();
  200. }
  201. }
  202. #region Mouse Support
  203. private Color? _savedForeColor;
  204. private void Border_Highlight (object? sender, CancelEventArgs<HighlightStyle> e)
  205. {
  206. if (!Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  207. {
  208. e.Cancel = true;
  209. return;
  210. }
  211. if (e.NewValue.HasFlag (HighlightStyle.Pressed))
  212. {
  213. if (!_savedForeColor.HasValue)
  214. {
  215. _savedForeColor = ColorScheme!.Normal.Foreground;
  216. }
  217. var cs = new ColorScheme (ColorScheme)
  218. {
  219. Normal = new (ColorScheme!.Normal.Foreground.GetHighlightColor (), ColorScheme.Normal.Background)
  220. };
  221. ColorScheme = cs;
  222. }
  223. #if HOVER
  224. else if (e.HighlightStyle.HasFlag (HighlightStyle.Hover))
  225. {
  226. if (!_savedHighlightLineStyle.HasValue)
  227. {
  228. _savedHighlightLineStyle = Parent?.BorderStyle ?? LineStyle;
  229. }
  230. LineStyle = LineStyle.Double;
  231. }
  232. #endif
  233. if (e.NewValue == HighlightStyle.None && _savedForeColor.HasValue)
  234. {
  235. var cs = new ColorScheme (ColorScheme)
  236. {
  237. Normal = new (_savedForeColor.Value, ColorScheme!.Normal.Background)
  238. };
  239. ColorScheme = cs;
  240. }
  241. Parent?.SetNeedsDisplay ();
  242. e.Cancel = true;
  243. }
  244. private Point? _dragPosition;
  245. private Point _startGrabPoint;
  246. /// <inheritdoc/>
  247. protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
  248. {
  249. if (base.OnMouseEvent (mouseEvent))
  250. {
  251. return true;
  252. }
  253. if (!Parent!.Arrangement.HasFlag (ViewArrangement.Movable)
  254. && !Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable)
  255. && !Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable)
  256. && !Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable)
  257. && !Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable)
  258. )
  259. {
  260. return false;
  261. }
  262. // BUGBUG: See https://github.com/gui-cs/Terminal.Gui/issues/3312
  263. if (!_dragPosition.HasValue && mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  264. {
  265. Parent.SetFocus ();
  266. ApplicationOverlapped.BringOverlappedTopToFront ();
  267. // Only start grabbing if the user clicks in the Thickness area
  268. // Adornment.Contains takes Parent SuperView=relative coords.
  269. if (Contains (new (mouseEvent.Position.X + Parent.Frame.X + Frame.X, mouseEvent.Position.Y + Parent.Frame.Y + Frame.Y)))
  270. {
  271. // Set the start grab point to the Frame coords
  272. _startGrabPoint = new (mouseEvent.Position.X + Frame.X, mouseEvent.Position.Y + Frame.Y);
  273. _dragPosition = mouseEvent.Position;
  274. Application.GrabMouse (this);
  275. SetHighlight (HighlightStyle);
  276. // If resizable and movable: Drag on top is move, other 3 sides are size
  277. // If not movable, but resizable: Drag on any side sizes.
  278. // Get rectangle representing Thickness.Top
  279. // If mouse is in that rectangle, set _arranging to ViewArrangement.Movable
  280. Rectangle sideRect = new (Frame.X + Thickness.Left, Frame.Y, Frame.Width - Thickness.Left - Thickness.Right, Thickness.Top);
  281. if (Parent!.Arrangement.HasFlag (ViewArrangement.Movable) && sideRect.Contains (_startGrabPoint))
  282. {
  283. EnterArrangeMode (ViewArrangement.Movable);
  284. return true;
  285. }
  286. // If mouse is in any other rectangle, set _arranging to ViewArrangement.<side>
  287. if (Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  288. {
  289. sideRect = new (Frame.X, Frame.Y + Thickness.Top, Thickness.Left, Frame.Height - Thickness.Top - Thickness.Bottom);
  290. if (sideRect.Contains (_startGrabPoint))
  291. {
  292. EnterArrangeMode (ViewArrangement.LeftResizable);
  293. return true;
  294. }
  295. }
  296. if (Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  297. {
  298. sideRect = new (Frame.X + Frame.Width - Thickness.Right, Frame.Y + Thickness.Top, Thickness.Right, Frame.Height - Thickness.Top - Thickness.Bottom);
  299. if (sideRect.Contains (_startGrabPoint))
  300. {
  301. EnterArrangeMode (ViewArrangement.RightResizable);
  302. return true;
  303. }
  304. }
  305. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable))
  306. {
  307. sideRect = new (Frame.X + Thickness.Left, Frame.Y, Frame.Width - Thickness.Left - Thickness.Right, Thickness.Top);
  308. if (sideRect.Contains (_startGrabPoint))
  309. {
  310. EnterArrangeMode (ViewArrangement.TopResizable);
  311. return true;
  312. }
  313. }
  314. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable))
  315. {
  316. sideRect = new (Frame.X + Thickness.Left, Frame.Y + Frame.Height - Thickness.Bottom, Frame.Width - Thickness.Left - Thickness.Right, Thickness.Bottom);
  317. if (sideRect.Contains (_startGrabPoint))
  318. {
  319. EnterArrangeMode (ViewArrangement.BottomResizable);
  320. return true;
  321. }
  322. }
  323. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  324. {
  325. sideRect = new (Frame.X, Frame.Height - Thickness.Top, Thickness.Left, Thickness.Bottom);
  326. if (sideRect.Contains (_startGrabPoint))
  327. {
  328. EnterArrangeMode (ViewArrangement.BottomResizable | ViewArrangement.LeftResizable);
  329. return true;
  330. }
  331. }
  332. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  333. {
  334. sideRect = new (Frame.X + Frame.Width - Thickness.Right, Frame.Height - Thickness.Top, Thickness.Right, Thickness.Bottom);
  335. if (sideRect.Contains (_startGrabPoint))
  336. {
  337. EnterArrangeMode (ViewArrangement.BottomResizable | ViewArrangement.RightResizable);
  338. return true;
  339. }
  340. }
  341. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  342. {
  343. sideRect = new (Frame.X + Frame.Width - Thickness.Right, Frame.Y, Thickness.Right, Thickness.Top);
  344. if (sideRect.Contains (_startGrabPoint))
  345. {
  346. EnterArrangeMode (ViewArrangement.TopResizable | ViewArrangement.RightResizable);
  347. return true;
  348. }
  349. }
  350. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  351. {
  352. sideRect = new (Frame.X, Frame.Y, Thickness.Left, Thickness.Top);
  353. if (sideRect.Contains (_startGrabPoint))
  354. {
  355. EnterArrangeMode (ViewArrangement.TopResizable | ViewArrangement.LeftResizable);
  356. return true;
  357. }
  358. }
  359. }
  360. return true;
  361. }
  362. if (mouseEvent.Flags is (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && Application.MouseGrabView == this)
  363. {
  364. if (_dragPosition.HasValue)
  365. {
  366. if (Parent.SuperView is null)
  367. {
  368. // Redraw the entire app window.
  369. Application.Top!.SetNeedsDisplay ();
  370. }
  371. else
  372. {
  373. Parent.SuperView.SetNeedsDisplay ();
  374. }
  375. _dragPosition = mouseEvent.Position;
  376. Point parentLoc = Parent.SuperView?.ScreenToViewport (new (mouseEvent.ScreenPosition.X, mouseEvent.ScreenPosition.Y))
  377. ?? mouseEvent.ScreenPosition;
  378. int minHeight = Thickness.Vertical + Parent!.Margin.Thickness.Bottom;
  379. int minWidth = Thickness.Horizontal + Parent!.Margin.Thickness.Right;
  380. switch (_arranging)
  381. {
  382. case ViewArrangement.Movable:
  383. GetLocationEnsuringFullVisibility (
  384. Parent,
  385. parentLoc.X - _startGrabPoint.X,
  386. parentLoc.Y - _startGrabPoint.Y,
  387. out int nx,
  388. out int ny,
  389. out _
  390. );
  391. Parent.X = nx;
  392. Parent.Y = ny;
  393. break;
  394. case ViewArrangement.TopResizable:
  395. // Get how much the mouse has moved since the start of the drag
  396. // and adjust the height of the parent by that amount
  397. int deltaY = parentLoc.Y - Parent.Frame.Y;
  398. int newHeight = Math.Max (minHeight, Parent.Frame.Height - deltaY);
  399. if (newHeight != Parent.Frame.Height)
  400. {
  401. Parent.Height = newHeight;
  402. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  403. }
  404. break;
  405. case ViewArrangement.BottomResizable:
  406. Parent.Height = Math.Max (minHeight, parentLoc.Y - Parent.Frame.Y + 1);
  407. break;
  408. case ViewArrangement.LeftResizable:
  409. // Get how much the mouse has moved since the start of the drag
  410. // and adjust the height of the parent by that amount
  411. int deltaX = parentLoc.X - Parent.Frame.X;
  412. int newWidth = Math.Max (minWidth, Parent.Frame.Width - deltaX);
  413. if (newWidth != Parent.Frame.Width)
  414. {
  415. Parent.Width = newWidth;
  416. Parent.X = parentLoc.X - _startGrabPoint.X;
  417. }
  418. break;
  419. case ViewArrangement.RightResizable:
  420. Parent.Width = Math.Max (minWidth, parentLoc.X - Parent.Frame.X + 1);
  421. break;
  422. case ViewArrangement.BottomResizable | ViewArrangement.RightResizable:
  423. Parent.Width = Math.Max (minWidth, parentLoc.X - Parent.Frame.X + 1);
  424. Parent.Height = Math.Max (minHeight, parentLoc.Y - Parent.Frame.Y + 1);
  425. break;
  426. case ViewArrangement.BottomResizable | ViewArrangement.LeftResizable:
  427. int dX = parentLoc.X - Parent.Frame.X;
  428. int newW = Math.Max (minWidth, Parent.Frame.Width - dX);
  429. if (newW != Parent.Frame.Width)
  430. {
  431. Parent.Width = newW;
  432. Parent.X = parentLoc.X - _startGrabPoint.X;
  433. }
  434. Parent.Height = Math.Max (minHeight, parentLoc.Y - Parent.Frame.Y + 1);
  435. break;
  436. case ViewArrangement.TopResizable | ViewArrangement.RightResizable:
  437. int dY = parentLoc.Y - Parent.Frame.Y;
  438. int newH = Math.Max (minHeight, Parent.Frame.Height - dY);
  439. if (newH != Parent.Frame.Height)
  440. {
  441. Parent.Height = newH;
  442. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  443. }
  444. Parent.Width = Math.Max (minWidth, parentLoc.X - Parent.Frame.X + 1);
  445. break;
  446. case ViewArrangement.TopResizable | ViewArrangement.LeftResizable:
  447. int dY2 = parentLoc.Y - Parent.Frame.Y;
  448. int newH2 = Math.Max (minHeight, Parent.Frame.Height - dY2);
  449. if (newH2 != Parent.Frame.Height)
  450. {
  451. Parent.Height = newH2;
  452. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  453. }
  454. int dX2 = parentLoc.X - Parent.Frame.X;
  455. int newW2 = Math.Max (minWidth, Parent.Frame.Width - dX2);
  456. if (newW2 != Parent.Frame.Width)
  457. {
  458. Parent.Width = newW2;
  459. Parent.X = parentLoc.X - _startGrabPoint.X;
  460. }
  461. break;
  462. }
  463. return true;
  464. }
  465. }
  466. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && _dragPosition.HasValue)
  467. {
  468. _dragPosition = null;
  469. Application.UngrabMouse ();
  470. SetHighlight (HighlightStyle.None);
  471. EndArrange ();
  472. return true;
  473. }
  474. return false;
  475. }
  476. private void Application_GrabbingMouse (object? sender, GrabMouseEventArgs e)
  477. {
  478. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  479. {
  480. e.Cancel = true;
  481. }
  482. }
  483. private void Application_UnGrabbingMouse (object? sender, GrabMouseEventArgs e)
  484. {
  485. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  486. {
  487. e.Cancel = true;
  488. }
  489. }
  490. #endregion Mouse Support
  491. /// <inheritdoc/>
  492. public override void OnDrawContent (Rectangle viewport)
  493. {
  494. base.OnDrawContent (viewport);
  495. if (Thickness == Thickness.Empty)
  496. {
  497. return;
  498. }
  499. //Driver.SetAttribute (Colors.ColorSchemes ["Error"].Normal);
  500. Rectangle screenBounds = ViewportToScreen (viewport);
  501. //OnDrawSubviews (bounds);
  502. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  503. // The border adornment (and title) are drawn at the outermost edge of border;
  504. // For Border
  505. // ...thickness extends outward (border/title is always as far in as possible)
  506. // PERF: How about a call to Rectangle.Offset?
  507. Rectangle borderBounds = GetBorderRectangle ();
  508. int topTitleLineY = borderBounds.Y;
  509. int titleY = borderBounds.Y;
  510. var titleBarsLength = 0; // the little vertical thingies
  511. int maxTitleWidth = Math.Max (
  512. 0,
  513. Math.Min (
  514. Parent!.TitleTextFormatter.FormatAndGetSize ().Width,
  515. Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
  516. )
  517. );
  518. Parent.TitleTextFormatter.ConstrainToSize = new (maxTitleWidth, 1);
  519. int sideLineLength = borderBounds.Height;
  520. bool canDrawBorder = borderBounds is { Width: > 0, Height: > 0 };
  521. LineStyle lineStyle = LineStyle;
  522. if (Settings.FastHasFlags (BorderSettings.Title))
  523. {
  524. if (Thickness.Top == 2)
  525. {
  526. topTitleLineY = borderBounds.Y - 1;
  527. titleY = topTitleLineY + 1;
  528. titleBarsLength = 2;
  529. }
  530. // ┌────┐
  531. //┌┘View└
  532. //│
  533. if (Thickness.Top == 3)
  534. {
  535. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  536. titleY = topTitleLineY + 1;
  537. titleBarsLength = 3;
  538. sideLineLength++;
  539. }
  540. // ┌────┐
  541. //┌┘View└
  542. //│
  543. if (Thickness.Top > 3)
  544. {
  545. topTitleLineY = borderBounds.Y - 2;
  546. titleY = topTitleLineY + 1;
  547. titleBarsLength = 3;
  548. sideLineLength++;
  549. }
  550. }
  551. if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && Settings.FastHasFlags (BorderSettings.Title) && !string.IsNullOrEmpty (Parent?.Title))
  552. {
  553. Attribute focus = Parent.GetNormalColor ();
  554. if (Parent.SuperView is { } && Parent.SuperView?.Subviews!.Count (s => s.CanFocus) > 1)
  555. {
  556. // Only use focus color if there are multiple focusable views
  557. focus = Parent.GetFocusColor ();
  558. }
  559. Parent.TitleTextFormatter.Draw (
  560. new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  561. Parent.HasFocus ? focus : Parent.GetNormalColor (),
  562. Parent.HasFocus ? focus : Parent.GetHotNormalColor ());
  563. }
  564. if (canDrawBorder && LineStyle != LineStyle.None)
  565. {
  566. LineCanvas? lc = Parent?.LineCanvas;
  567. bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height >= 1;
  568. bool drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  569. bool drawBottom = Thickness.Bottom > 0 && Frame.Width > 1 && Frame.Height > 1;
  570. bool drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  571. Attribute prevAttr = Driver.GetAttribute ();
  572. if (ColorScheme is { })
  573. {
  574. Driver.SetAttribute (GetNormalColor ());
  575. }
  576. else
  577. {
  578. Driver.SetAttribute (Parent!.GetNormalColor ());
  579. }
  580. if (drawTop)
  581. {
  582. // ╔╡Title╞═════╗
  583. // ╔╡╞═════╗
  584. if (borderBounds.Width < 4 || !Settings.FastHasFlags (BorderSettings.Title) || string.IsNullOrEmpty (Parent?.Title))
  585. {
  586. // ╔╡╞╗ should be ╔══╗
  587. lc?.AddLine (
  588. new (borderBounds.Location.X, titleY),
  589. borderBounds.Width,
  590. Orientation.Horizontal,
  591. lineStyle,
  592. Driver.GetAttribute ()
  593. );
  594. }
  595. else
  596. {
  597. // ┌────┐
  598. //┌┘View└
  599. //│
  600. if (Thickness.Top == 2)
  601. {
  602. lc?.AddLine (
  603. new (borderBounds.X + 1, topTitleLineY),
  604. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  605. Orientation.Horizontal,
  606. lineStyle,
  607. Driver.GetAttribute ()
  608. );
  609. }
  610. // ┌────┐
  611. //┌┘View└
  612. //│
  613. if (borderBounds.Width >= 4 && Thickness.Top > 2)
  614. {
  615. lc?.AddLine (
  616. new (borderBounds.X + 1, topTitleLineY),
  617. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  618. Orientation.Horizontal,
  619. lineStyle,
  620. Driver.GetAttribute ()
  621. );
  622. lc?.AddLine (
  623. new (borderBounds.X + 1, topTitleLineY + 2),
  624. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  625. Orientation.Horizontal,
  626. lineStyle,
  627. Driver.GetAttribute ()
  628. );
  629. }
  630. // ╔╡Title╞═════╗
  631. // Add a short horiz line for ╔╡
  632. lc?.AddLine (
  633. new (borderBounds.Location.X, titleY),
  634. 2,
  635. Orientation.Horizontal,
  636. lineStyle,
  637. Driver.GetAttribute ()
  638. );
  639. // Add a vert line for ╔╡
  640. lc?.AddLine (
  641. new (borderBounds.X + 1, topTitleLineY),
  642. titleBarsLength,
  643. Orientation.Vertical,
  644. LineStyle.Single,
  645. Driver.GetAttribute ()
  646. );
  647. // Add a vert line for ╞
  648. lc?.AddLine (
  649. new (
  650. borderBounds.X
  651. + 1
  652. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  653. - 1,
  654. topTitleLineY
  655. ),
  656. titleBarsLength,
  657. Orientation.Vertical,
  658. LineStyle.Single,
  659. Driver.GetAttribute ()
  660. );
  661. // Add the right hand line for ╞═════╗
  662. lc?.AddLine (
  663. new (
  664. borderBounds.X
  665. + 1
  666. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  667. - 1,
  668. titleY
  669. ),
  670. borderBounds.Width - Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  671. Orientation.Horizontal,
  672. lineStyle,
  673. Driver.GetAttribute ()
  674. );
  675. }
  676. }
  677. #if !SUBVIEW_BASED_BORDER
  678. if (drawLeft)
  679. {
  680. lc?.AddLine (
  681. new (borderBounds.Location.X, titleY),
  682. sideLineLength,
  683. Orientation.Vertical,
  684. lineStyle,
  685. Driver.GetAttribute ()
  686. );
  687. }
  688. #endif
  689. if (drawBottom)
  690. {
  691. lc?.AddLine (
  692. new (borderBounds.X, borderBounds.Y + borderBounds.Height - 1),
  693. borderBounds.Width,
  694. Orientation.Horizontal,
  695. lineStyle,
  696. Driver.GetAttribute ()
  697. );
  698. }
  699. if (drawRight)
  700. {
  701. lc?.AddLine (
  702. new (borderBounds.X + borderBounds.Width - 1, titleY),
  703. sideLineLength,
  704. Orientation.Vertical,
  705. lineStyle,
  706. Driver.GetAttribute ()
  707. );
  708. }
  709. Driver.SetAttribute (prevAttr);
  710. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  711. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Ruler))
  712. {
  713. // Top
  714. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  715. if (drawTop)
  716. {
  717. hruler.Draw (new (screenBounds.X, screenBounds.Y));
  718. }
  719. // Redraw title
  720. if (drawTop && maxTitleWidth > 0 && Settings.FastHasFlags (BorderSettings.Title))
  721. {
  722. Parent!.TitleTextFormatter.Draw (
  723. new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  724. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor (),
  725. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor ());
  726. }
  727. //Left
  728. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  729. if (drawLeft)
  730. {
  731. vruler.Draw (new (screenBounds.X, screenBounds.Y + 1), 1);
  732. }
  733. // Bottom
  734. if (drawBottom)
  735. {
  736. hruler.Draw (new (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  737. }
  738. // Right
  739. if (drawRight)
  740. {
  741. vruler.Draw (new (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  742. }
  743. }
  744. // TODO: This should not be done on each draw?
  745. if (Settings.FastHasFlags (BorderSettings.Gradient))
  746. {
  747. SetupGradientLineCanvas (lc!, screenBounds);
  748. }
  749. else
  750. {
  751. lc!.Fill = null;
  752. }
  753. }
  754. }
  755. private void SetupGradientLineCanvas (LineCanvas lc, Rectangle rect)
  756. {
  757. GetAppealingGradientColors (out List<Color> stops, out List<int> steps);
  758. var g = new Gradient (stops, steps);
  759. var fore = new GradientFill (rect, g, GradientDirection.Diagonal);
  760. var back = new SolidFill (GetNormalColor ().Background);
  761. lc.Fill = new (fore, back);
  762. }
  763. private static void GetAppealingGradientColors (out List<Color> stops, out List<int> steps)
  764. {
  765. // Define the colors of the gradient stops with more appealing colors
  766. stops =
  767. [
  768. new (0, 128, 255), // Bright Blue
  769. new (0, 255, 128), // Bright Green
  770. new (255, 255), // Bright Yellow
  771. new (255, 128), // Bright Orange
  772. new (255, 0, 128)
  773. ];
  774. // Define the number of steps between each color for smoother transitions
  775. // If we pass only a single value then it will assume equal steps between all pairs
  776. steps = [15];
  777. }
  778. private ViewArrangement _arranging;
  779. private Button? _arrangeButton;
  780. /// <summary>
  781. /// Starts "Arrange Mode" where <see cref="Adornment.Parent"/> can be moved and/or resized using the mouse
  782. /// or keyboard.
  783. /// </summary>
  784. /// <remarks>
  785. /// Arrange Mode is exited by the user pressing <see cref="Application.ArrangeKey"/>, <see cref="Key.Esc"/>, or by clicking
  786. /// the mouse out of the <see cref="Adornment.Parent"/>'s Frame.
  787. /// </remarks>
  788. /// <returns></returns>
  789. public bool? EnterArrangeMode (ViewArrangement arrangement)
  790. {
  791. Debug.Assert (_arranging == ViewArrangement.Fixed);
  792. if (!Parent!.Arrangement.HasFlag (ViewArrangement.Movable)
  793. && !Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable)
  794. && !Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable)
  795. && !Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable)
  796. && !Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable)
  797. )
  798. {
  799. return false;
  800. }
  801. Debug.Assert (_arrangeButton is null);
  802. _arrangeButton = new Button
  803. {
  804. CanFocus = true,
  805. Width = 1,
  806. Height = 1,
  807. NoDecorations = true,
  808. NoPadding = true,
  809. ShadowStyle = ShadowStyle.None,
  810. Text = $"{Glyphs.Diamond}",
  811. };
  812. Add (_arrangeButton);
  813. CanFocus = true;
  814. AddCommand (Command.Quit, EndArrange);
  815. AddCommand (Command.Up,
  816. () =>
  817. {
  818. if (Parent is null)
  819. {
  820. return false;
  821. }
  822. if (_arranging == ViewArrangement.Movable)
  823. {
  824. Parent!.Y = Parent.Y - 1;
  825. }
  826. if (_arranging == ViewArrangement.Resizable)
  827. {
  828. if (Parent!.Viewport.Height > 0)
  829. {
  830. Parent!.Height = Parent.Height! - 1;
  831. }
  832. }
  833. return true;
  834. });
  835. AddCommand (Command.Down,
  836. () =>
  837. {
  838. if (Parent is null)
  839. {
  840. return false;
  841. }
  842. if (_arranging == ViewArrangement.Movable)
  843. {
  844. Parent!.Y = Parent.Y + 1;
  845. }
  846. if (_arranging == ViewArrangement.Resizable)
  847. {
  848. Parent!.Height = Parent.Height! + 1;
  849. }
  850. return true;
  851. });
  852. AddCommand (Command.Left,
  853. () =>
  854. {
  855. if (Parent is null)
  856. {
  857. return false;
  858. }
  859. if (_arranging == ViewArrangement.Movable)
  860. {
  861. Parent!.X = Parent.X - 1;
  862. }
  863. if (_arranging == ViewArrangement.Resizable)
  864. {
  865. if (Parent!.Viewport.Width > 0)
  866. {
  867. Parent!.Width = Parent.Width! - 1;
  868. }
  869. }
  870. return true;
  871. });
  872. AddCommand (Command.Right,
  873. () =>
  874. {
  875. if (Parent is null)
  876. {
  877. return false;
  878. }
  879. if (_arranging == ViewArrangement.Movable)
  880. {
  881. Parent!.X = Parent.X + 1;
  882. }
  883. if (_arranging == ViewArrangement.Resizable)
  884. {
  885. Parent!.Width = Parent.Width! + 1;
  886. }
  887. return true;
  888. });
  889. AddCommand (Command.Tab, Navigate);
  890. AddCommand (Command.BackTab, Navigate);
  891. KeyBindings.Add (Key.Esc, KeyBindingScope.HotKey, Command.Quit);
  892. KeyBindings.Add (Application.ArrangeKey, KeyBindingScope.HotKey, Command.Quit);
  893. KeyBindings.Add (Key.CursorUp, KeyBindingScope.HotKey, Command.Up);
  894. KeyBindings.Add (Key.CursorDown, KeyBindingScope.HotKey, Command.Down);
  895. KeyBindings.Add (Key.CursorLeft, KeyBindingScope.HotKey, Command.Left);
  896. KeyBindings.Add (Key.CursorRight, KeyBindingScope.HotKey, Command.Right);
  897. KeyBindings.Add (Key.Tab, KeyBindingScope.HotKey, Command.Tab);
  898. KeyBindings.Add (Key.Tab.WithShift, KeyBindingScope.HotKey, Command.BackTab);
  899. Application.MouseEvent += ApplicationOnMouseEvent;
  900. if (arrangement == ViewArrangement.Fixed)
  901. {
  902. if (Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  903. {
  904. _arranging = ViewArrangement.Movable;
  905. _arrangeButton.X = 0;
  906. _arrangeButton.Y = 0;
  907. return true;
  908. }
  909. if (Parent!.Arrangement.HasFlag (ViewArrangement.Resizable))
  910. {
  911. _arranging = ViewArrangement.Resizable;
  912. _arrangeButton.X = Pos.AnchorEnd ();
  913. _arrangeButton.Y = Pos.AnchorEnd ();
  914. return true;
  915. }
  916. }
  917. else
  918. {
  919. _arranging = arrangement;
  920. switch (_arranging)
  921. {
  922. case ViewArrangement.Movable:
  923. _arrangeButton.X = 0;
  924. _arrangeButton.Y = 0;
  925. return true;
  926. case ViewArrangement.RightResizable | ViewArrangement.BottomResizable:
  927. case ViewArrangement.Resizable:
  928. _arrangeButton.X = Pos.AnchorEnd ();
  929. _arrangeButton.Y = Pos.AnchorEnd ();
  930. return true;
  931. case ViewArrangement.LeftResizable:
  932. _arrangeButton.X = 0;
  933. _arrangeButton.Y = Pos.Center ();
  934. return true;
  935. case ViewArrangement.RightResizable:
  936. _arrangeButton.X = Pos.AnchorEnd ();
  937. _arrangeButton.Y = Pos.Center ();
  938. return true;
  939. case ViewArrangement.TopResizable:
  940. _arrangeButton.X = Pos.Center ();
  941. _arrangeButton.Y = 0;
  942. return true;
  943. case ViewArrangement.BottomResizable:
  944. _arrangeButton.X = Pos.Center ();
  945. _arrangeButton.Y = Pos.AnchorEnd ();
  946. return true;
  947. case ViewArrangement.LeftResizable | ViewArrangement.BottomResizable:
  948. _arrangeButton.X = 0;
  949. _arrangeButton.Y = Pos.AnchorEnd ();
  950. return true;
  951. case ViewArrangement.LeftResizable | ViewArrangement.TopResizable:
  952. _arrangeButton.X = 0;
  953. _arrangeButton.Y = 0;
  954. return true;
  955. case ViewArrangement.RightResizable | ViewArrangement.TopResizable:
  956. _arrangeButton.X = Pos.AnchorEnd (); ;
  957. _arrangeButton.Y = 0;
  958. return true;
  959. }
  960. }
  961. // Hack for now
  962. EndArrange ();
  963. return false;
  964. bool? Navigate ()
  965. {
  966. if (_arranging == ViewArrangement.Movable)
  967. {
  968. if (Parent!.Arrangement.HasFlag (ViewArrangement.Resizable))
  969. {
  970. _arranging = ViewArrangement.Resizable;
  971. _arrangeButton.X = Pos.AnchorEnd ();
  972. _arrangeButton.Y = Pos.AnchorEnd ();
  973. }
  974. }
  975. else if (_arranging == ViewArrangement.Resizable)
  976. {
  977. if (Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  978. {
  979. _arranging = ViewArrangement.Movable;
  980. _arrangeButton.X = 0;
  981. _arrangeButton.Y = 0;
  982. }
  983. }
  984. return true;
  985. }
  986. }
  987. private void ApplicationOnMouseEvent (object? sender, MouseEvent e)
  988. {
  989. if (e.Flags != MouseFlags.Button1Clicked)
  990. {
  991. return;
  992. }
  993. // If mouse click is outside of Border.Thickness then exit Arrange Mode
  994. // e.Position is screen relative
  995. Point framePos = ScreenToFrame (e.ScreenPosition);
  996. if (!Thickness.Contains (Frame, framePos))
  997. {
  998. EndArrange ();
  999. }
  1000. }
  1001. private bool? EndArrange ()
  1002. {
  1003. // Debug.Assert (_arranging != ViewArrangement.Fixed);
  1004. _arranging = ViewArrangement.Fixed;
  1005. Application.MouseEvent -= ApplicationOnMouseEvent;
  1006. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  1007. {
  1008. Application.UngrabMouse ();
  1009. }
  1010. CanFocus = false;
  1011. if (_arrangeButton is { })
  1012. {
  1013. Remove (_arrangeButton);
  1014. _arrangeButton.Dispose ();
  1015. _arrangeButton = null;
  1016. }
  1017. KeyBindings.Clear ();
  1018. return true;
  1019. }
  1020. /// <inheritdoc/>
  1021. protected override void Dispose (bool disposing)
  1022. {
  1023. Application.GrabbingMouse -= Application_GrabbingMouse;
  1024. Application.UnGrabbingMouse -= Application_UnGrabbingMouse;
  1025. _dragPosition = null;
  1026. base.Dispose (disposing);
  1027. }
  1028. }