Border.cs 31 KB

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