Border.cs 52 KB

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