Border.cs 51 KB

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