Border.cs 51 KB

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