Border.Arrangment.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. using System.ComponentModel;
  2. using System.Diagnostics;
  3. namespace Terminal.Gui.ViewBase;
  4. // Border Arrange Mode
  5. public partial class Border
  6. {
  7. internal ViewArrangement Arranging { get; set; }
  8. private Button? _moveButton; // always top-left
  9. private Button? _allSizeButton;
  10. private Button? _leftSizeButton;
  11. private Button? _rightSizeButton;
  12. private Button? _topSizeButton;
  13. private Button? _bottomSizeButton;
  14. /// <summary>
  15. /// Starts "Arrange Mode" where <see cref="Adornment.Parent"/> can be moved and/or resized using the mouse
  16. /// or keyboard. If <paramref name="arrangement"/> is <see cref="ViewArrangement.Fixed"/> keyboard mode is enabled.
  17. /// </summary>
  18. /// <remarks>
  19. /// Arrange Mode is exited by the user pressing <see cref="Application.ArrangeKey"/>, <see cref="Key.Esc"/>, or by
  20. /// clicking the mouse out of the <see cref="Adornment.Parent"/>'s Frame.
  21. /// </remarks>
  22. /// <returns></returns>
  23. public bool? EnterArrangeMode (ViewArrangement arrangement)
  24. {
  25. Debug.Assert (Arranging == ViewArrangement.Fixed);
  26. if (!HasAnyArrangementOptions ())
  27. {
  28. return false;
  29. }
  30. MouseState |= MouseState.Pressed;
  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. MouseState &= ~MouseState.Pressed;
  365. Application.MouseEvent -= ApplicationOnMouseEvent;
  366. if (Application.Mouse.MouseGrabView == this && _dragPosition.HasValue)
  367. {
  368. Application.Mouse.UngrabMouse ();
  369. }
  370. // Clean up all arrangement buttons
  371. DisposeSizeButton (ref _moveButton);
  372. DisposeSizeButton (ref _allSizeButton);
  373. DisposeSizeButton (ref _leftSizeButton);
  374. DisposeSizeButton (ref _rightSizeButton);
  375. DisposeSizeButton (ref _topSizeButton);
  376. DisposeSizeButton (ref _bottomSizeButton);
  377. HotKeyBindings.Clear ();
  378. if (CanFocus)
  379. {
  380. CanFocus = false;
  381. }
  382. return true;
  383. }
  384. /// <summary>
  385. /// Helper method to dispose and remove a button
  386. /// </summary>
  387. private void DisposeSizeButton (ref Button? button)
  388. {
  389. if (button != null)
  390. {
  391. Remove (button);
  392. button.Dispose ();
  393. button = null;
  394. }
  395. }
  396. #region Mouse Support
  397. private Point? _dragPosition;
  398. private Point _startGrabPoint;
  399. /// <inheritdoc/>
  400. protected override bool OnMouseEvent (MouseEventArgs mouseEvent)
  401. {
  402. // BUGBUG: See https://github.com/gui-cs/Terminal.Gui/issues/3312
  403. if (!_dragPosition.HasValue && mouseEvent.Flags.HasFlag (MouseFlags.Button1Pressed))
  404. {
  405. Parent!.SetFocus ();
  406. if (!HasAnyArrangementOptions ())
  407. {
  408. return false;
  409. }
  410. // Only start grabbing if the user clicks in the Thickness area
  411. // Adornment.Contains takes Parent SuperView=relative coords.
  412. if (Contains (new (mouseEvent.Position.X + Parent.Frame.X + Frame.X, mouseEvent.Position.Y + Parent.Frame.Y + Frame.Y)))
  413. {
  414. if (Arranging != ViewArrangement.Fixed)
  415. {
  416. EndArrangeMode ();
  417. }
  418. // Set the start grab point to the Frame coords
  419. _startGrabPoint = new (mouseEvent.Position.X + Frame.X, mouseEvent.Position.Y + Frame.Y);
  420. _dragPosition = mouseEvent.Position;
  421. Application.Mouse.GrabMouse (this);
  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.Mouse.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.Mouse.UngrabMouse ();
  442. EndArrangeMode ();
  443. return true;
  444. }
  445. return false;
  446. }
  447. /// <summary>
  448. /// Determines the arrangement mode based on where the mouse was clicked
  449. /// </summary>
  450. internal ViewArrangement DetermineArrangeModeFromClick ()
  451. {
  452. Rectangle sideRect;
  453. // Check for left resizable region
  454. if (Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  455. {
  456. sideRect = new (Frame.X, Frame.Y + Thickness.Top, Thickness.Left, Frame.Height - Thickness.Top - Thickness.Bottom);
  457. if (sideRect.Contains (_startGrabPoint))
  458. {
  459. return ViewArrangement.LeftResizable;
  460. }
  461. }
  462. // Check for right resizable region
  463. if (Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  464. {
  465. sideRect = new (
  466. Frame.X + Frame.Width - Thickness.Right,
  467. Frame.Y + Thickness.Top,
  468. Thickness.Right,
  469. Frame.Height - Thickness.Top - Thickness.Bottom);
  470. if (sideRect.Contains (_startGrabPoint))
  471. {
  472. return (ViewArrangement.RightResizable);
  473. }
  474. }
  475. // Check for top resizable region (only if not movable)
  476. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable) && !Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  477. {
  478. sideRect = new (Frame.X + Thickness.Left, Frame.Y, Frame.Width - Thickness.Left - Thickness.Right, Thickness.Top);
  479. if (sideRect.Contains (_startGrabPoint))
  480. {
  481. return (ViewArrangement.TopResizable);
  482. }
  483. }
  484. // Check for bottom resizable region
  485. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable))
  486. {
  487. sideRect = new (
  488. Frame.X + Thickness.Left,
  489. Frame.Y + Frame.Height - Thickness.Bottom,
  490. Frame.Width - Thickness.Left - Thickness.Right,
  491. Thickness.Bottom);
  492. if (sideRect.Contains (_startGrabPoint))
  493. {
  494. return (ViewArrangement.BottomResizable);
  495. }
  496. }
  497. // Check for bottom-left corner region
  498. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  499. {
  500. sideRect = new (Frame.X, Frame.Height - Thickness.Top, Thickness.Left, Thickness.Bottom);
  501. if (sideRect.Contains (_startGrabPoint))
  502. {
  503. return (ViewArrangement.BottomResizable | ViewArrangement.LeftResizable);
  504. }
  505. }
  506. // Check for bottom-right corner region
  507. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  508. {
  509. sideRect = new (Frame.X + Frame.Width - Thickness.Right, Frame.Height - Thickness.Top, Thickness.Right, Thickness.Bottom);
  510. if (sideRect.Contains (_startGrabPoint))
  511. {
  512. return (ViewArrangement.BottomResizable | ViewArrangement.RightResizable);
  513. }
  514. }
  515. // Check for top-right corner region
  516. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  517. {
  518. sideRect = new (Frame.X + Frame.Width - Thickness.Right, Frame.Y, Thickness.Right, Thickness.Top);
  519. if (sideRect.Contains (_startGrabPoint))
  520. {
  521. return (ViewArrangement.TopResizable | ViewArrangement.RightResizable);
  522. }
  523. }
  524. // Check for top-left corner region
  525. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable) && Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  526. {
  527. sideRect = new (Frame.X, Frame.Y, Thickness.Left, Thickness.Top);
  528. if (sideRect.Contains (_startGrabPoint))
  529. {
  530. return (ViewArrangement.TopResizable | ViewArrangement.LeftResizable);
  531. }
  532. }
  533. // Default to movable if enabled
  534. if (Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  535. {
  536. return ViewArrangement.Movable;
  537. }
  538. return ViewArrangement.Fixed;
  539. }
  540. /// <summary>
  541. /// Handles drag operations for moving and resizing
  542. /// </summary>
  543. internal void HandleDragOperation (MouseEventArgs mouseEvent)
  544. {
  545. if (Parent!.SuperView is null)
  546. {
  547. // Redraw the entire app window.
  548. Application.Current!.SetNeedsDraw ();
  549. }
  550. else
  551. {
  552. Parent.SuperView.SetNeedsDraw ();
  553. }
  554. _dragPosition = mouseEvent.Position;
  555. Point parentLoc = Parent!.SuperView?.ScreenToViewport (new (mouseEvent.ScreenPosition.X, mouseEvent.ScreenPosition.Y))
  556. ?? mouseEvent.ScreenPosition;
  557. int minHeight = Thickness.Vertical + Parent!.Margin!.Thickness.Bottom;
  558. int minWidth = Thickness.Horizontal + Parent!.Margin!.Thickness.Right;
  559. switch (Arranging)
  560. {
  561. case ViewArrangement.Movable:
  562. Parent.X = parentLoc.X - _startGrabPoint.X;
  563. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  564. break;
  565. case ViewArrangement.TopResizable:
  566. // Get how much the mouse has moved since the start of the drag
  567. // and adjust the height of the parent by that amount
  568. int deltaY = parentLoc.Y - Parent.Frame.Y;
  569. int newHeight = Math.Max (minHeight, Parent.Frame.Height - deltaY);
  570. if (newHeight != Parent.Frame.Height)
  571. {
  572. Parent.Height = newHeight;
  573. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  574. }
  575. break;
  576. case ViewArrangement.BottomResizable:
  577. Parent.Height = Math.Max (minHeight, parentLoc.Y - Parent.Frame.Y + Parent!.Margin!.Thickness.Bottom + 1);
  578. break;
  579. case ViewArrangement.LeftResizable:
  580. // Get how much the mouse has moved since the start of the drag
  581. // and adjust the width of the parent by that amount
  582. int deltaX = parentLoc.X - Parent.Frame.X;
  583. int newWidth = Math.Max (minWidth, Parent.Frame.Width - deltaX);
  584. if (newWidth != Parent.Frame.Width)
  585. {
  586. Parent.Width = newWidth;
  587. Parent.X = parentLoc.X - _startGrabPoint.X;
  588. }
  589. break;
  590. case ViewArrangement.RightResizable:
  591. Parent.Width = Math.Max (minWidth, parentLoc.X - Parent.Frame.X + Parent!.Margin!.Thickness.Right + 1);
  592. break;
  593. case ViewArrangement.BottomResizable | ViewArrangement.RightResizable:
  594. Parent.Width = Math.Max (minWidth, parentLoc.X - Parent.Frame.X + Parent!.Margin!.Thickness.Right + 1);
  595. Parent.Height = Math.Max (minHeight, parentLoc.Y - Parent.Frame.Y + Parent!.Margin!.Thickness.Bottom + 1);
  596. break;
  597. case ViewArrangement.BottomResizable | ViewArrangement.LeftResizable:
  598. int dX = parentLoc.X - Parent.Frame.X;
  599. int newW = Math.Max (minWidth, Parent.Frame.Width - dX);
  600. if (newW != Parent.Frame.Width)
  601. {
  602. Parent.Width = newW;
  603. Parent.X = parentLoc.X - _startGrabPoint.X;
  604. }
  605. Parent.Height = Math.Max (minHeight, parentLoc.Y - Parent.Frame.Y + Parent!.Margin!.Thickness.Bottom + 1);
  606. break;
  607. case ViewArrangement.TopResizable | ViewArrangement.RightResizable:
  608. int dY = parentLoc.Y - Parent.Frame.Y;
  609. int newH = Math.Max (minHeight, Parent.Frame.Height - dY);
  610. if (newH != Parent.Frame.Height)
  611. {
  612. Parent.Height = newH;
  613. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  614. }
  615. Parent.Width = Math.Max (minWidth, parentLoc.X - Parent.Frame.X + Parent!.Margin!.Thickness.Right + 1);
  616. break;
  617. case ViewArrangement.TopResizable | ViewArrangement.LeftResizable:
  618. int dY2 = parentLoc.Y - Parent.Frame.Y;
  619. int newH2 = Math.Max (minHeight, Parent.Frame.Height - dY2);
  620. if (newH2 != Parent.Frame.Height)
  621. {
  622. Parent.Height = newH2;
  623. Parent.Y = parentLoc.Y - _startGrabPoint.Y;
  624. }
  625. int dX2 = parentLoc.X - Parent.Frame.X;
  626. int newW2 = Math.Max (minWidth, Parent.Frame.Width - dX2);
  627. if (newW2 != Parent.Frame.Width)
  628. {
  629. Parent.Width = newW2;
  630. Parent.X = parentLoc.X - _startGrabPoint.X;
  631. }
  632. break;
  633. }
  634. }
  635. private void Application_GrabbingMouse (object? sender, GrabMouseEventArgs e)
  636. {
  637. if (Application.Mouse.MouseGrabView == this && _dragPosition.HasValue)
  638. {
  639. e.Cancel = true;
  640. }
  641. }
  642. private void Application_UnGrabbingMouse (object? sender, GrabMouseEventArgs e)
  643. {
  644. if (Application.Mouse.MouseGrabView == this && _dragPosition.HasValue)
  645. {
  646. e.Cancel = true;
  647. }
  648. }
  649. #endregion Mouse Support
  650. /// <inheritdoc/>
  651. protected override void Dispose (bool disposing)
  652. {
  653. Application.Mouse.GrabbingMouse -= Application_GrabbingMouse;
  654. Application.Mouse.UnGrabbingMouse -= Application_UnGrabbingMouse;
  655. _dragPosition = null;
  656. base.Dispose (disposing);
  657. }
  658. }