Border.cs 52 KB

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