Border.Arrangment.cs 26 KB

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