Border.cs 50 KB

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