Border.Arrangment.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. #nullable enable
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. namespace Terminal.Gui.ViewBase;
  5. // Border Arrange Mode
  6. public partial class Border
  7. {
  8. internal ViewArrangement Arranging { get; set; }
  9. private Button? _moveButton; // always top-left
  10. private Button? _allSizeButton;
  11. private Button? _leftSizeButton;
  12. private Button? _rightSizeButton;
  13. private Button? _topSizeButton;
  14. private Button? _bottomSizeButton;
  15. /// <summary>
  16. /// Starts "Arrange Mode" where <see cref="Adornment.Parent"/> can be moved and/or resized using the mouse
  17. /// or keyboard. If <paramref name="arrangement"/> is <see cref="ViewArrangement.Fixed"/> keyboard mode is enabled.
  18. /// </summary>
  19. /// <remarks>
  20. /// Arrange Mode is exited by the user pressing <see cref="Application.ArrangeKey"/>, <see cref="Key.Esc"/>, or by
  21. /// clicking the mouse out of the <see cref="Adornment.Parent"/>'s Frame.
  22. /// </remarks>
  23. /// <returns></returns>
  24. public bool? EnterArrangeMode (ViewArrangement arrangement)
  25. {
  26. Debug.Assert (Arranging == ViewArrangement.Fixed);
  27. if (!HasAnyArrangementOptions ())
  28. {
  29. return false;
  30. }
  31. // Add Commands and KeyBindings - Note it's ok these get added each time. KeyBindings are cleared in EndArrange()
  32. AddArrangeModeKeyBindings ();
  33. Application.MouseEvent += ApplicationOnMouseEvent;
  34. // Create all necessary arrangement buttons
  35. CreateArrangementButtons ();
  36. if (arrangement == ViewArrangement.Fixed)
  37. {
  38. // Keyboard mode
  39. SetVisibilityForKeyboardMode ();
  40. Arranging = ViewArrangement.Movable;
  41. CanFocus = true;
  42. SetFocus ();
  43. }
  44. else
  45. {
  46. // Mouse mode
  47. Arranging = arrangement;
  48. SetVisibilityForMouseMode (arrangement);
  49. }
  50. if (Arranging != ViewArrangement.Fixed)
  51. {
  52. if (arrangement == ViewArrangement.Fixed)
  53. {
  54. // Keyboard mode - enable nav
  55. // TODO: Keyboard mode only supports sizing from bottom/right.
  56. Arranging = (ViewArrangement)(Focused?.Data ?? ViewArrangement.Fixed);
  57. }
  58. return true;
  59. }
  60. // Hack for now
  61. EndArrangeMode ();
  62. return false;
  63. }
  64. /// <summary>
  65. /// Checks if the parent view has any arrangement options enabled
  66. /// </summary>
  67. private bool HasAnyArrangementOptions ()
  68. {
  69. return Parent!.Arrangement.HasFlag (ViewArrangement.Movable)
  70. || Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable)
  71. || Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable)
  72. || Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable)
  73. || Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable);
  74. }
  75. /// <summary>
  76. /// Creates all the buttons required for the arrange mode based on allowed arrangement options
  77. /// </summary>
  78. private void CreateArrangementButtons ()
  79. {
  80. if (Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  81. {
  82. _moveButton = CreateArrangementButton ("moveButton", Glyphs.Move, 0, 0, ViewArrangement.Movable);
  83. }
  84. if (Parent!.Arrangement.HasFlag (ViewArrangement.Resizable))
  85. {
  86. _allSizeButton = CreateArrangementButton (
  87. "allSizeButton",
  88. Glyphs.SizeBottomRight,
  89. Pos.AnchorEnd (),
  90. Pos.AnchorEnd (),
  91. ViewArrangement.Resizable);
  92. }
  93. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable))
  94. {
  95. _topSizeButton = CreateArrangementButton (
  96. "topSizeButton",
  97. Glyphs.SizeVertical,
  98. Pos.Center () + Parent!.Margin!.Thickness.Horizontal,
  99. 0,
  100. ViewArrangement.TopResizable);
  101. }
  102. if (Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  103. {
  104. _rightSizeButton = CreateArrangementButton (
  105. "rightSizeButton",
  106. Glyphs.SizeHorizontal,
  107. Pos.AnchorEnd (),
  108. Pos.Center () + Parent!.Margin!.Thickness.Vertical / 2,
  109. ViewArrangement.RightResizable);
  110. }
  111. if (Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  112. {
  113. _leftSizeButton = CreateArrangementButton (
  114. "leftSizeButton",
  115. Glyphs.SizeHorizontal,
  116. 0,
  117. Pos.Center () + Parent!.Margin!.Thickness.Vertical / 2,
  118. ViewArrangement.LeftResizable);
  119. }
  120. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable))
  121. {
  122. _bottomSizeButton = CreateArrangementButton (
  123. "bottomSizeButton",
  124. Glyphs.SizeVertical,
  125. Pos.Center () + Parent!.Margin!.Thickness.Horizontal / 2,
  126. Pos.AnchorEnd (),
  127. ViewArrangement.BottomResizable);
  128. }
  129. }
  130. /// <summary>
  131. /// Factory method to create a standardized arrangement button
  132. /// </summary>
  133. private Button CreateArrangementButton (string id, Rune glyph, Pos x, Pos y, ViewArrangement arrangement)
  134. {
  135. var button = new Button
  136. {
  137. Id = id,
  138. CanFocus = true,
  139. Width = 1,
  140. Height = 1,
  141. NoDecorations = true,
  142. NoPadding = true,
  143. ShadowStyle = ShadowStyle.None,
  144. Text = $"{glyph}",
  145. X = x,
  146. Y = y,
  147. Visible = false,
  148. Data = arrangement
  149. };
  150. Add (button);
  151. return button;
  152. }
  153. /// <summary>
  154. /// Sets button visibility for keyboard arrangement mode
  155. /// </summary>
  156. private void SetVisibilityForKeyboardMode ()
  157. {
  158. if (_moveButton != null)
  159. {
  160. _moveButton.Visible = true;
  161. }
  162. if (_allSizeButton != null)
  163. {
  164. _allSizeButton.Visible = true;
  165. }
  166. }
  167. /// <summary>
  168. /// Sets button visibility based on the specified mouse arrangement mode
  169. /// </summary>
  170. private void SetVisibilityForMouseMode (ViewArrangement arrangement)
  171. {
  172. switch (arrangement)
  173. {
  174. case ViewArrangement.Movable:
  175. SetVisibleButton (_moveButton);
  176. break;
  177. case ViewArrangement.RightResizable | ViewArrangement.BottomResizable:
  178. case ViewArrangement.Resizable:
  179. SetVisibleButton (_rightSizeButton);
  180. SetVisibleButton (_bottomSizeButton);
  181. if (_allSizeButton != null)
  182. {
  183. _allSizeButton.X = Pos.AnchorEnd ();
  184. _allSizeButton.Y = Pos.AnchorEnd ();
  185. _allSizeButton.Visible = true;
  186. }
  187. break;
  188. case ViewArrangement.LeftResizable:
  189. SetVisibleButton (_leftSizeButton);
  190. break;
  191. case ViewArrangement.RightResizable:
  192. SetVisibleButton (_rightSizeButton);
  193. break;
  194. case ViewArrangement.TopResizable:
  195. SetVisibleButton (_topSizeButton);
  196. break;
  197. case ViewArrangement.BottomResizable:
  198. SetVisibleButton (_bottomSizeButton);
  199. break;
  200. case ViewArrangement.LeftResizable | ViewArrangement.BottomResizable:
  201. SetVisibleButton (_leftSizeButton);
  202. SetVisibleButton (_bottomSizeButton);
  203. if (_allSizeButton != null)
  204. {
  205. _allSizeButton.X = 0;
  206. _allSizeButton.Y = Pos.AnchorEnd ();
  207. _allSizeButton.Visible = true;
  208. }
  209. break;
  210. case ViewArrangement.LeftResizable | ViewArrangement.TopResizable:
  211. SetVisibleButton (_leftSizeButton);
  212. SetVisibleButton (_topSizeButton);
  213. break;
  214. case ViewArrangement.RightResizable | ViewArrangement.TopResizable:
  215. SetVisibleButton (_rightSizeButton);
  216. SetVisibleButton (_topSizeButton);
  217. if (_allSizeButton != null)
  218. {
  219. _allSizeButton.X = Pos.AnchorEnd ();
  220. _allSizeButton.Y = 0;
  221. _allSizeButton.Visible = true;
  222. }
  223. break;
  224. }
  225. }
  226. /// <summary>
  227. /// Helper method to make a button visible if it's not null
  228. /// </summary>
  229. private void SetVisibleButton (Button? button)
  230. {
  231. if (button != null)
  232. {
  233. button.Visible = true;
  234. }
  235. }
  236. private void AddArrangeModeKeyBindings ()
  237. {
  238. AddCommand (Command.Quit, EndArrangeMode);
  239. AddCommand (
  240. Command.Up,
  241. () =>
  242. {
  243. if (Parent is null)
  244. {
  245. return false;
  246. }
  247. if (Arranging == ViewArrangement.Movable)
  248. {
  249. Parent.Y = Parent.Y - 1;
  250. }
  251. if (Arranging == ViewArrangement.Resizable)
  252. {
  253. if (Parent.Viewport.Height > 0)
  254. {
  255. Parent.Height = Parent.Height! - 1;
  256. }
  257. }
  258. return true;
  259. });
  260. AddCommand (
  261. Command.Down,
  262. () =>
  263. {
  264. if (Parent is null)
  265. {
  266. return false;
  267. }
  268. if (Arranging == ViewArrangement.Movable)
  269. {
  270. Parent.Y = Parent.Y + 1;
  271. }
  272. if (Arranging == ViewArrangement.Resizable)
  273. {
  274. Parent.Height = Parent.Height! + 1;
  275. }
  276. return true;
  277. });
  278. AddCommand (
  279. Command.Left,
  280. () =>
  281. {
  282. if (Parent is null)
  283. {
  284. return false;
  285. }
  286. if (Arranging == ViewArrangement.Movable)
  287. {
  288. Parent.X = Parent.X - 1;
  289. }
  290. if (Arranging == ViewArrangement.Resizable)
  291. {
  292. if (Parent.Viewport.Width > 0)
  293. {
  294. Parent.Width = Parent.Width! - 1;
  295. }
  296. }
  297. return true;
  298. });
  299. AddCommand (
  300. Command.Right,
  301. () =>
  302. {
  303. if (Parent is null)
  304. {
  305. return false;
  306. }
  307. if (Arranging == ViewArrangement.Movable)
  308. {
  309. Parent.X = Parent.X + 1;
  310. }
  311. if (Arranging == ViewArrangement.Resizable)
  312. {
  313. Parent.Width = Parent.Width! + 1;
  314. }
  315. return true;
  316. });
  317. AddCommand (
  318. Command.Tab,
  319. () =>
  320. {
  321. // BUGBUG: If an arrangeable view has only arrangeable subviews, it's not possible to activate
  322. // BUGBUG: ArrangeMode with keyboard for the superview.
  323. // BUGBUG: AdvanceFocus should be wise to this and when in ArrangeMode, should move across
  324. // BUGBUG: the view hierarchy.
  325. AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  326. Arranging = (ViewArrangement)(Focused?.Data ?? ViewArrangement.Fixed);
  327. return true; // Always eat
  328. });
  329. AddCommand (
  330. Command.BackTab,
  331. () =>
  332. {
  333. AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop);
  334. Arranging = (ViewArrangement)(Focused?.Data ?? ViewArrangement.Fixed);
  335. return true; // Always eat
  336. });
  337. HotKeyBindings.Add (Key.Esc, Command.Quit);
  338. HotKeyBindings.Add (Application.ArrangeKey, Command.Quit);
  339. HotKeyBindings.Add (Key.CursorUp, Command.Up);
  340. HotKeyBindings.Add (Key.CursorDown, Command.Down);
  341. HotKeyBindings.Add (Key.CursorLeft, Command.Left);
  342. HotKeyBindings.Add (Key.CursorRight, Command.Right);
  343. HotKeyBindings.Add (Key.Tab, Command.Tab);
  344. HotKeyBindings.Add (Key.Tab.WithShift, Command.BackTab);
  345. }
  346. private void ApplicationOnMouseEvent (object? sender, MouseEventArgs e)
  347. {
  348. if (e.Flags != MouseFlags.Button1Clicked)
  349. {
  350. return;
  351. }
  352. // If mouse click is outside of Border.Thickness then exit Arrange Mode
  353. // e.Position is screen relative
  354. Point framePos = ScreenToFrame (e.ScreenPosition);
  355. if (!Thickness.Contains (Frame, framePos))
  356. {
  357. EndArrangeMode ();
  358. }
  359. }
  360. private bool? EndArrangeMode ()
  361. {
  362. // Debug.Assert (_arranging != ViewArrangement.Fixed);
  363. Arranging = ViewArrangement.Fixed;
  364. Application.MouseEvent -= ApplicationOnMouseEvent;
  365. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  366. {
  367. Application.UngrabMouse ();
  368. }
  369. // Clean up all arrangement buttons
  370. DisposeSizeButton (ref _moveButton);
  371. DisposeSizeButton (ref _allSizeButton);
  372. DisposeSizeButton (ref _leftSizeButton);
  373. DisposeSizeButton (ref _rightSizeButton);
  374. DisposeSizeButton (ref _topSizeButton);
  375. DisposeSizeButton (ref _bottomSizeButton);
  376. HotKeyBindings.Clear ();
  377. if (CanFocus)
  378. {
  379. CanFocus = false;
  380. }
  381. return true;
  382. }
  383. /// <summary>
  384. /// Helper method to dispose and remove a button
  385. /// </summary>
  386. private void DisposeSizeButton (ref Button? button)
  387. {
  388. if (button != null)
  389. {
  390. Remove (button);
  391. button.Dispose ();
  392. button = null;
  393. }
  394. }
  395. #region Mouse Support
  396. private Point? _dragPosition;
  397. private Point _startGrabPoint;
  398. /// <inheritdoc/>
  399. protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
  400. {
  401. // BUGBUG: See https://github.com/gui-cs/Terminal.Gui/issues/3312
  402. if (!_dragPosition.HasValue && mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  403. {
  404. Parent!.SetFocus ();
  405. if (!HasAnyArrangementOptions ())
  406. {
  407. return false;
  408. }
  409. // Only start grabbing if the user clicks in the Thickness area
  410. // Adornment.Contains takes Parent SuperView=relative coords.
  411. if (Contains (new (mouseEvent.Position.X + Parent.Frame.X + Frame.X, mouseEvent.Position.Y + Parent.Frame.Y + Frame.Y)))
  412. {
  413. if (Arranging != ViewArrangement.Fixed)
  414. {
  415. EndArrangeMode ();
  416. }
  417. // Set the start grab point to the Frame coords
  418. _startGrabPoint = new (mouseEvent.Position.X + Frame.X, mouseEvent.Position.Y + Frame.Y);
  419. _dragPosition = mouseEvent.Position;
  420. Application.GrabMouse (this);
  421. SetPressedHighlight (HighlightStyle);
  422. // Determine the mode based on where the click occurred
  423. ViewArrangement arrangeMode = DetermineArrangeModeFromClick ();
  424. EnterArrangeMode (arrangeMode);
  425. // BUGBUG: Should we return the result of EnterArrangeMode?
  426. return true;
  427. }
  428. return true;
  429. }
  430. if (mouseEvent.Flags is (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) && Application.MouseGrabView == this)
  431. {
  432. if (_dragPosition.HasValue)
  433. {
  434. HandleDragOperation (mouseEvent);
  435. return true;
  436. }
  437. }
  438. if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Released) && _dragPosition.HasValue)
  439. {
  440. _dragPosition = null;
  441. Application.UngrabMouse ();
  442. SetPressedHighlight (HighlightStyle.None);
  443. EndArrangeMode ();
  444. return true;
  445. }
  446. return false;
  447. }
  448. /// <summary>
  449. /// Determines the arrangement mode based on where the mouse was clicked
  450. /// </summary>
  451. internal ViewArrangement DetermineArrangeModeFromClick ()
  452. {
  453. Rectangle sideRect;
  454. // Check for left resizable region
  455. if (Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  456. {
  457. sideRect = new (Frame.X, Frame.Y + Thickness.Top, Thickness.Left, Frame.Height - Thickness.Top - Thickness.Bottom);
  458. if (sideRect.Contains (_startGrabPoint))
  459. {
  460. return ViewArrangement.LeftResizable;
  461. }
  462. }
  463. // Check for right resizable region
  464. if (Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  465. {
  466. sideRect = new (
  467. Frame.X + Frame.Width - Thickness.Right,
  468. Frame.Y + Thickness.Top,
  469. Thickness.Right,
  470. Frame.Height - Thickness.Top - Thickness.Bottom);
  471. if (sideRect.Contains (_startGrabPoint))
  472. {
  473. return (ViewArrangement.RightResizable);
  474. }
  475. }
  476. // Check for top resizable region (only if not movable)
  477. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable) && !Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  478. {
  479. sideRect = new (Frame.X + Thickness.Left, Frame.Y, Frame.Width - Thickness.Left - Thickness.Right, Thickness.Top);
  480. if (sideRect.Contains (_startGrabPoint))
  481. {
  482. return (ViewArrangement.TopResizable);
  483. }
  484. }
  485. // Check for bottom resizable region
  486. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable))
  487. {
  488. sideRect = new (
  489. Frame.X + Thickness.Left,
  490. Frame.Y + Frame.Height - Thickness.Bottom,
  491. Frame.Width - Thickness.Left - Thickness.Right,
  492. Thickness.Bottom);
  493. if (sideRect.Contains (_startGrabPoint))
  494. {
  495. return (ViewArrangement.BottomResizable);
  496. }
  497. }
  498. // Check for bottom-left corner region
  499. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  500. {
  501. sideRect = new (Frame.X, Frame.Height - Thickness.Top, Thickness.Left, Thickness.Bottom);
  502. if (sideRect.Contains (_startGrabPoint))
  503. {
  504. return (ViewArrangement.BottomResizable | ViewArrangement.LeftResizable);
  505. }
  506. }
  507. // Check for bottom-right corner region
  508. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  509. {
  510. sideRect = new (Frame.X + Frame.Width - Thickness.Right, Frame.Height - Thickness.Top, Thickness.Right, Thickness.Bottom);
  511. if (sideRect.Contains (_startGrabPoint))
  512. {
  513. return (ViewArrangement.BottomResizable | ViewArrangement.RightResizable);
  514. }
  515. }
  516. // Check for top-right corner region
  517. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  518. {
  519. sideRect = new (Frame.X + Frame.Width - Thickness.Right, Frame.Y, Thickness.Right, Thickness.Top);
  520. if (sideRect.Contains (_startGrabPoint))
  521. {
  522. return (ViewArrangement.TopResizable | ViewArrangement.RightResizable);
  523. }
  524. }
  525. // Check for top-left corner region
  526. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  527. {
  528. sideRect = new (Frame.X, Frame.Y, Thickness.Left, Thickness.Top);
  529. if (sideRect.Contains (_startGrabPoint))
  530. {
  531. return (ViewArrangement.TopResizable | ViewArrangement.LeftResizable);
  532. }
  533. }
  534. // Default to movable if enabled
  535. if (Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  536. {
  537. return ViewArrangement.Movable;
  538. }
  539. return ViewArrangement.Fixed;
  540. }
  541. /// <summary>
  542. /// Handles drag operations for moving and resizing
  543. /// </summary>
  544. internal void HandleDragOperation (MouseEventArgs mouseEvent)
  545. {
  546. if (Parent!.SuperView is null)
  547. {
  548. // Redraw the entire app window.
  549. Application.Top!.SetNeedsDraw ();
  550. }
  551. else
  552. {
  553. Parent.SuperView.SetNeedsDraw ();
  554. }
  555. _dragPosition = mouseEvent.Position;
  556. Point parentLoc = Parent!.SuperView?.ScreenToViewport (new (mouseEvent.ScreenPosition.X, mouseEvent.ScreenPosition.Y))
  557. ?? mouseEvent.ScreenPosition;
  558. int minHeight = Thickness.Vertical + Parent!.Margin!.Thickness.Bottom;
  559. int minWidth = Thickness.Horizontal + Parent!.Margin!.Thickness.Right;
  560. switch (Arranging)
  561. {
  562. case ViewArrangement.Movable:
  563. Parent.X = parentLoc.X - _startGrabPoint.X;
  564. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  565. break;
  566. case ViewArrangement.TopResizable:
  567. // Get how much the mouse has moved since the start of the drag
  568. // and adjust the height of the parent by that amount
  569. int deltaY = parentLoc.Y - Parent.Frame.Y;
  570. int newHeight = Math.Max (minHeight, Parent.Frame.Height - deltaY);
  571. if (newHeight != Parent.Frame.Height)
  572. {
  573. Parent.Height = newHeight;
  574. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  575. }
  576. break;
  577. case ViewArrangement.BottomResizable:
  578. Parent.Height = Math.Max (minHeight, parentLoc.Y - Parent.Frame.Y + Parent!.Margin.Thickness.Bottom + 1);
  579. break;
  580. case ViewArrangement.LeftResizable:
  581. // Get how much the mouse has moved since the start of the drag
  582. // and adjust the width of the parent by that amount
  583. int deltaX = parentLoc.X - Parent.Frame.X;
  584. int newWidth = Math.Max (minWidth, Parent.Frame.Width - deltaX);
  585. if (newWidth != Parent.Frame.Width)
  586. {
  587. Parent.Width = newWidth;
  588. Parent.X = parentLoc.X - _startGrabPoint.X;
  589. }
  590. break;
  591. case ViewArrangement.RightResizable:
  592. Parent.Width = Math.Max (minWidth, parentLoc.X - Parent.Frame.X + Parent!.Margin.Thickness.Right + 1);
  593. break;
  594. case ViewArrangement.BottomResizable | ViewArrangement.RightResizable:
  595. Parent.Width = Math.Max (minWidth, parentLoc.X - Parent.Frame.X + Parent!.Margin.Thickness.Right + 1);
  596. Parent.Height = Math.Max (minHeight, parentLoc.Y - Parent.Frame.Y + Parent!.Margin.Thickness.Bottom + 1);
  597. break;
  598. case ViewArrangement.BottomResizable | ViewArrangement.LeftResizable:
  599. int dX = parentLoc.X - Parent.Frame.X;
  600. int newW = Math.Max (minWidth, Parent.Frame.Width - dX);
  601. if (newW != Parent.Frame.Width)
  602. {
  603. Parent.Width = newW;
  604. Parent.X = parentLoc.X - _startGrabPoint.X;
  605. }
  606. Parent.Height = Math.Max (minHeight, parentLoc.Y - Parent.Frame.Y + Parent!.Margin.Thickness.Bottom + 1);
  607. break;
  608. case ViewArrangement.TopResizable | ViewArrangement.RightResizable:
  609. int dY = parentLoc.Y - Parent.Frame.Y;
  610. int newH = Math.Max (minHeight, Parent.Frame.Height - dY);
  611. if (newH != Parent.Frame.Height)
  612. {
  613. Parent.Height = newH;
  614. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  615. }
  616. Parent.Width = Math.Max (minWidth, parentLoc.X - Parent.Frame.X + Parent!.Margin.Thickness.Right + 1);
  617. break;
  618. case ViewArrangement.TopResizable | ViewArrangement.LeftResizable:
  619. int dY2 = parentLoc.Y - Parent.Frame.Y;
  620. int newH2 = Math.Max (minHeight, Parent.Frame.Height - dY2);
  621. if (newH2 != Parent.Frame.Height)
  622. {
  623. Parent.Height = newH2;
  624. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  625. }
  626. int dX2 = parentLoc.X - Parent.Frame.X;
  627. int newW2 = Math.Max (minWidth, Parent.Frame.Width - dX2);
  628. if (newW2 != Parent.Frame.Width)
  629. {
  630. Parent.Width = newW2;
  631. Parent.X = parentLoc.X - _startGrabPoint.X;
  632. }
  633. break;
  634. }
  635. }
  636. private void Application_GrabbingMouse (object? sender, GrabMouseEventArgs e)
  637. {
  638. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  639. {
  640. e.Cancel = true;
  641. }
  642. }
  643. private void Application_UnGrabbingMouse (object? sender, GrabMouseEventArgs e)
  644. {
  645. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  646. {
  647. e.Cancel = true;
  648. }
  649. }
  650. #endregion Mouse Support
  651. /// <inheritdoc/>
  652. protected override void Dispose (bool disposing)
  653. {
  654. Application.GrabbingMouse -= Application_GrabbingMouse;
  655. Application.UnGrabbingMouse -= Application_UnGrabbingMouse;
  656. _dragPosition = null;
  657. base.Dispose (disposing);
  658. }
  659. }