Border.cs 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  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. public override void OnDrawContent (Rectangle viewport)
  495. {
  496. base.OnDrawContent (viewport);
  497. if (Thickness == Thickness.Empty)
  498. {
  499. return;
  500. }
  501. //Driver.SetAttribute (Colors.ColorSchemes ["Error"].Normal);
  502. Rectangle screenBounds = ViewportToScreen (viewport);
  503. //OnDrawSubviews (bounds);
  504. // TODO: v2 - this will eventually be two controls: "BorderView" and "Label" (for the title)
  505. // The border adornment (and title) are drawn at the outermost edge of border;
  506. // For Border
  507. // ...thickness extends outward (border/title is always as far in as possible)
  508. // PERF: How about a call to Rectangle.Offset?
  509. Rectangle borderBounds = GetBorderRectangle ();
  510. int topTitleLineY = borderBounds.Y;
  511. int titleY = borderBounds.Y;
  512. var titleBarsLength = 0; // the little vertical thingies
  513. int maxTitleWidth = Math.Max (
  514. 0,
  515. Math.Min (
  516. Parent?.TitleTextFormatter.FormatAndGetSize ().Width ?? 0,
  517. Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
  518. )
  519. );
  520. if (Parent is { })
  521. {
  522. Parent.TitleTextFormatter.ConstrainToSize = new (maxTitleWidth, 1);
  523. }
  524. int sideLineLength = borderBounds.Height;
  525. bool canDrawBorder = borderBounds is { Width: > 0, Height: > 0 };
  526. LineStyle lineStyle = LineStyle;
  527. if (Settings.FastHasFlags (BorderSettings.Title))
  528. {
  529. if (Thickness.Top == 2)
  530. {
  531. topTitleLineY = borderBounds.Y - 1;
  532. titleY = topTitleLineY + 1;
  533. titleBarsLength = 2;
  534. }
  535. // ┌────┐
  536. //┌┘View└
  537. //│
  538. if (Thickness.Top == 3)
  539. {
  540. topTitleLineY = borderBounds.Y - (Thickness.Top - 1);
  541. titleY = topTitleLineY + 1;
  542. titleBarsLength = 3;
  543. sideLineLength++;
  544. }
  545. // ┌────┐
  546. //┌┘View└
  547. //│
  548. if (Thickness.Top > 3)
  549. {
  550. topTitleLineY = borderBounds.Y - 2;
  551. titleY = topTitleLineY + 1;
  552. titleBarsLength = 3;
  553. sideLineLength++;
  554. }
  555. }
  556. if (Parent is {} && canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && Settings.FastHasFlags (BorderSettings.Title) && !string.IsNullOrEmpty (Parent?.Title))
  557. {
  558. Attribute focus = Parent.GetNormalColor ();
  559. if (Parent.SuperView is { } && Parent.SuperView?.Subviews!.Count (s => s.CanFocus) > 1)
  560. {
  561. // Only use focus color if there are multiple focusable views
  562. focus = Parent.GetFocusColor ();
  563. }
  564. Parent.TitleTextFormatter.Draw (
  565. new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  566. Parent.HasFocus ? focus : Parent.GetNormalColor (),
  567. Parent.HasFocus ? focus : Parent.GetHotNormalColor ());
  568. }
  569. if (canDrawBorder && LineStyle != LineStyle.None)
  570. {
  571. LineCanvas? lc = Parent?.LineCanvas;
  572. bool drawTop = Thickness.Top > 0 && Frame.Width > 1 && Frame.Height >= 1;
  573. bool drawLeft = Thickness.Left > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  574. bool drawBottom = Thickness.Bottom > 0 && Frame.Width > 1 && Frame.Height > 1;
  575. bool drawRight = Thickness.Right > 0 && (Frame.Height > 1 || Thickness.Top == 0);
  576. Attribute prevAttr = Driver?.GetAttribute () ?? Attribute.Default;
  577. if (ColorScheme is { })
  578. {
  579. Driver?.SetAttribute (GetNormalColor ());
  580. }
  581. else
  582. {
  583. Driver?.SetAttribute (Parent!.GetNormalColor ());
  584. }
  585. if (drawTop)
  586. {
  587. // ╔╡Title╞═════╗
  588. // ╔╡╞═════╗
  589. if (borderBounds.Width < 4 || !Settings.FastHasFlags (BorderSettings.Title) || string.IsNullOrEmpty (Parent?.Title))
  590. {
  591. // ╔╡╞╗ should be ╔══╗
  592. lc?.AddLine (
  593. new (borderBounds.Location.X, titleY),
  594. borderBounds.Width,
  595. Orientation.Horizontal,
  596. lineStyle,
  597. Driver?.GetAttribute ()
  598. );
  599. }
  600. else
  601. {
  602. // ┌────┐
  603. //┌┘View└
  604. //│
  605. if (Thickness.Top == 2)
  606. {
  607. lc?.AddLine (
  608. new (borderBounds.X + 1, topTitleLineY),
  609. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  610. Orientation.Horizontal,
  611. lineStyle,
  612. Driver?.GetAttribute ()
  613. );
  614. }
  615. // ┌────┐
  616. //┌┘View└
  617. //│
  618. if (borderBounds.Width >= 4 && Thickness.Top > 2)
  619. {
  620. lc?.AddLine (
  621. new (borderBounds.X + 1, topTitleLineY),
  622. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  623. Orientation.Horizontal,
  624. lineStyle,
  625. Driver?.GetAttribute ()
  626. );
  627. lc?.AddLine (
  628. new (borderBounds.X + 1, topTitleLineY + 2),
  629. Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  630. Orientation.Horizontal,
  631. lineStyle,
  632. Driver?.GetAttribute ()
  633. );
  634. }
  635. // ╔╡Title╞═════╗
  636. // Add a short horiz line for ╔╡
  637. lc?.AddLine (
  638. new (borderBounds.Location.X, titleY),
  639. 2,
  640. Orientation.Horizontal,
  641. lineStyle,
  642. Driver?.GetAttribute ()
  643. );
  644. // Add a vert line for ╔╡
  645. lc?.AddLine (
  646. new (borderBounds.X + 1, topTitleLineY),
  647. titleBarsLength,
  648. Orientation.Vertical,
  649. LineStyle.Single,
  650. Driver?.GetAttribute ()
  651. );
  652. // Add a vert line for ╞
  653. lc?.AddLine (
  654. new (
  655. borderBounds.X
  656. + 1
  657. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  658. - 1,
  659. topTitleLineY
  660. ),
  661. titleBarsLength,
  662. Orientation.Vertical,
  663. LineStyle.Single,
  664. Driver?.GetAttribute ()
  665. );
  666. // Add the right hand line for ╞═════╗
  667. lc?.AddLine (
  668. new (
  669. borderBounds.X
  670. + 1
  671. + Math.Min (borderBounds.Width - 2, maxTitleWidth + 2)
  672. - 1,
  673. titleY
  674. ),
  675. borderBounds.Width - Math.Min (borderBounds.Width - 2, maxTitleWidth + 2),
  676. Orientation.Horizontal,
  677. lineStyle,
  678. Driver?.GetAttribute ()
  679. );
  680. }
  681. }
  682. #if !SUBVIEW_BASED_BORDER
  683. if (drawLeft)
  684. {
  685. lc?.AddLine (
  686. new (borderBounds.Location.X, titleY),
  687. sideLineLength,
  688. Orientation.Vertical,
  689. lineStyle,
  690. Driver?.GetAttribute ()
  691. );
  692. }
  693. #endif
  694. if (drawBottom)
  695. {
  696. lc?.AddLine (
  697. new (borderBounds.X, borderBounds.Y + borderBounds.Height - 1),
  698. borderBounds.Width,
  699. Orientation.Horizontal,
  700. lineStyle,
  701. Driver?.GetAttribute ()
  702. );
  703. }
  704. if (drawRight)
  705. {
  706. lc?.AddLine (
  707. new (borderBounds.X + borderBounds.Width - 1, titleY),
  708. sideLineLength,
  709. Orientation.Vertical,
  710. lineStyle,
  711. Driver?.GetAttribute ()
  712. );
  713. }
  714. Driver?.SetAttribute (prevAttr);
  715. // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler
  716. if (Diagnostics.HasFlag (ViewDiagnosticFlags.Ruler))
  717. {
  718. // Top
  719. var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal };
  720. if (drawTop)
  721. {
  722. hruler.Draw (new (screenBounds.X, screenBounds.Y));
  723. }
  724. // Redraw title
  725. if (drawTop && maxTitleWidth > 0 && Settings.FastHasFlags (BorderSettings.Title))
  726. {
  727. Parent!.TitleTextFormatter.Draw (
  728. new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  729. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor (),
  730. Parent.HasFocus ? Parent.GetFocusColor () : Parent.GetNormalColor ());
  731. }
  732. //Left
  733. var vruler = new Ruler { Length = screenBounds.Height - 2, Orientation = Orientation.Vertical };
  734. if (drawLeft)
  735. {
  736. vruler.Draw (new (screenBounds.X, screenBounds.Y + 1), 1);
  737. }
  738. // Bottom
  739. if (drawBottom)
  740. {
  741. hruler.Draw (new (screenBounds.X, screenBounds.Y + screenBounds.Height - 1));
  742. }
  743. // Right
  744. if (drawRight)
  745. {
  746. vruler.Draw (new (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1);
  747. }
  748. }
  749. // TODO: This should not be done on each draw?
  750. if (Settings.FastHasFlags (BorderSettings.Gradient))
  751. {
  752. SetupGradientLineCanvas (lc!, screenBounds);
  753. }
  754. else
  755. {
  756. lc!.Fill = null;
  757. }
  758. }
  759. }
  760. private void SetupGradientLineCanvas (LineCanvas lc, Rectangle rect)
  761. {
  762. GetAppealingGradientColors (out List<Color> stops, out List<int> steps);
  763. var g = new Gradient (stops, steps);
  764. var fore = new GradientFill (rect, g, GradientDirection.Diagonal);
  765. var back = new SolidFill (GetNormalColor ().Background);
  766. lc.Fill = new (fore, back);
  767. }
  768. private static void GetAppealingGradientColors (out List<Color> stops, out List<int> steps)
  769. {
  770. // Define the colors of the gradient stops with more appealing colors
  771. stops =
  772. [
  773. new (0, 128, 255), // Bright Blue
  774. new (0, 255, 128), // Bright Green
  775. new (255, 255), // Bright Yellow
  776. new (255, 128), // Bright Orange
  777. new (255, 0, 128)
  778. ];
  779. // Define the number of steps between each color for smoother transitions
  780. // If we pass only a single value then it will assume equal steps between all pairs
  781. steps = [15];
  782. }
  783. private ViewArrangement _arranging;
  784. private Button? _moveButton; // always top-left
  785. private Button? _allSizeButton;
  786. private Button? _leftSizeButton;
  787. private Button? _rightSizeButton;
  788. private Button? _topSizeButton;
  789. private Button? _bottomSizeButton;
  790. /// <summary>
  791. /// Starts "Arrange Mode" where <see cref="Adornment.Parent"/> can be moved and/or resized using the mouse
  792. /// or keyboard. If <paramref name="arrangement"/> is <see cref="ViewArrangement.Fixed"/> keyboard mode is enabled.
  793. /// </summary>
  794. /// <remarks>
  795. /// Arrange Mode is exited by the user pressing <see cref="Application.ArrangeKey"/>, <see cref="Key.Esc"/>, or by
  796. /// clicking
  797. /// the mouse out of the <see cref="Adornment.Parent"/>'s Frame.
  798. /// </remarks>
  799. /// <returns></returns>
  800. public bool? EnterArrangeMode (ViewArrangement arrangement)
  801. {
  802. Debug.Assert (_arranging == ViewArrangement.Fixed);
  803. if (!Parent!.Arrangement.HasFlag (ViewArrangement.Movable)
  804. && !Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable)
  805. && !Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable)
  806. && !Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable)
  807. && !Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable)
  808. )
  809. {
  810. return false;
  811. }
  812. // Add Commands and Keybindigs - Note it's ok these get added each time. KeyBindings are cleared in EndArrange()
  813. AddArrangeModeKeyBindings ();
  814. Application.MouseEvent += ApplicationOnMouseEvent;
  815. // TODO: This code can be refactored to be more readable and maintainable.
  816. // Create buttons for resizing and moving
  817. if (Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  818. {
  819. Debug.Assert (_moveButton is null);
  820. _moveButton = new ()
  821. {
  822. Id = "moveButton",
  823. CanFocus = true,
  824. Width = 1,
  825. Height = 1,
  826. NoDecorations = true,
  827. NoPadding = true,
  828. ShadowStyle = ShadowStyle.None,
  829. Text = $"{Glyphs.Move}",
  830. Visible = false,
  831. Data = ViewArrangement.Movable
  832. };
  833. Add (_moveButton);
  834. }
  835. if (Parent!.Arrangement.HasFlag (ViewArrangement.Resizable))
  836. {
  837. Debug.Assert (_allSizeButton is null);
  838. _allSizeButton = new ()
  839. {
  840. Id = "allSizeButton",
  841. CanFocus = true,
  842. Width = 1,
  843. Height = 1,
  844. NoDecorations = true,
  845. NoPadding = true,
  846. ShadowStyle = ShadowStyle.None,
  847. Text = $"{Glyphs.SizeBottomRight}",
  848. X = Pos.AnchorEnd (),
  849. Y = Pos.AnchorEnd (),
  850. Visible = false,
  851. Data = ViewArrangement.Resizable
  852. };
  853. Add (_allSizeButton);
  854. }
  855. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable))
  856. {
  857. Debug.Assert (_topSizeButton is null);
  858. _topSizeButton = new ()
  859. {
  860. Id = "topSizeButton",
  861. CanFocus = true,
  862. Width = 1,
  863. Height = 1,
  864. NoDecorations = true,
  865. NoPadding = true,
  866. ShadowStyle = ShadowStyle.None,
  867. Text = $"{Glyphs.SizeVertical}",
  868. X = Pos.Center () + Parent!.Margin.Thickness.Horizontal,
  869. Y = 0,
  870. Visible = false,
  871. Data = ViewArrangement.TopResizable
  872. };
  873. Add (_topSizeButton);
  874. }
  875. if (Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  876. {
  877. Debug.Assert (_rightSizeButton is null);
  878. _rightSizeButton = new ()
  879. {
  880. Id = "rightSizeButton",
  881. CanFocus = true,
  882. Width = 1,
  883. Height = 1,
  884. NoDecorations = true,
  885. NoPadding = true,
  886. ShadowStyle = ShadowStyle.None,
  887. Text = $"{Glyphs.SizeHorizontal}",
  888. X = Pos.AnchorEnd (),
  889. Y = Pos.Center () + Parent!.Margin.Thickness.Vertical / 2,
  890. Visible = false,
  891. Data = ViewArrangement.RightResizable
  892. };
  893. Add (_rightSizeButton);
  894. }
  895. if (Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  896. {
  897. Debug.Assert (_leftSizeButton is null);
  898. _leftSizeButton = new ()
  899. {
  900. Id = "leftSizeButton",
  901. CanFocus = true,
  902. Width = 1,
  903. Height = 1,
  904. NoDecorations = true,
  905. NoPadding = true,
  906. ShadowStyle = ShadowStyle.None,
  907. Text = $"{Glyphs.SizeHorizontal}",
  908. X = 0,
  909. Y = Pos.Center () + Parent!.Margin.Thickness.Vertical / 2,
  910. Visible = false,
  911. Data = ViewArrangement.LeftResizable
  912. };
  913. Add (_leftSizeButton);
  914. }
  915. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable))
  916. {
  917. Debug.Assert (_bottomSizeButton is null);
  918. _bottomSizeButton = new ()
  919. {
  920. Id = "bottomSizeButton",
  921. CanFocus = true,
  922. Width = 1,
  923. Height = 1,
  924. NoDecorations = true,
  925. NoPadding = true,
  926. ShadowStyle = ShadowStyle.None,
  927. Text = $"{Glyphs.SizeVertical}",
  928. X = Pos.Center () + Parent!.Margin.Thickness.Horizontal / 2,
  929. Y = Pos.AnchorEnd (),
  930. Visible = false,
  931. Data = ViewArrangement.BottomResizable
  932. };
  933. Add (_bottomSizeButton);
  934. }
  935. if (arrangement == ViewArrangement.Fixed)
  936. {
  937. // Keyboard mode
  938. if (Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  939. {
  940. _moveButton!.Visible = true;
  941. }
  942. if (Parent!.Arrangement.HasFlag (ViewArrangement.Resizable))
  943. {
  944. _allSizeButton!.Visible = true;
  945. }
  946. _arranging = ViewArrangement.Movable;
  947. CanFocus = true;
  948. SetFocus ();
  949. }
  950. else
  951. {
  952. // Mouse mode
  953. _arranging = arrangement;
  954. switch (_arranging)
  955. {
  956. case ViewArrangement.Movable:
  957. _moveButton!.Visible = true;
  958. break;
  959. case ViewArrangement.RightResizable | ViewArrangement.BottomResizable:
  960. case ViewArrangement.Resizable:
  961. _rightSizeButton!.Visible = true;
  962. _bottomSizeButton!.Visible = true;
  963. if (_allSizeButton is { })
  964. {
  965. _allSizeButton!.X = Pos.AnchorEnd ();
  966. _allSizeButton!.Y = Pos.AnchorEnd ();
  967. _allSizeButton!.Visible = true;
  968. }
  969. break;
  970. case ViewArrangement.LeftResizable:
  971. _leftSizeButton!.Visible = true;
  972. break;
  973. case ViewArrangement.RightResizable:
  974. _rightSizeButton!.Visible = true;
  975. break;
  976. case ViewArrangement.TopResizable:
  977. _topSizeButton!.Visible = true;
  978. break;
  979. case ViewArrangement.BottomResizable:
  980. _bottomSizeButton!.Visible = true;
  981. break;
  982. case ViewArrangement.LeftResizable | ViewArrangement.BottomResizable:
  983. _rightSizeButton!.Visible = true;
  984. _bottomSizeButton!.Visible = true;
  985. if (_allSizeButton is { })
  986. {
  987. _allSizeButton.X = 0;
  988. _allSizeButton.Y = Pos.AnchorEnd ();
  989. _allSizeButton.Visible = true;
  990. }
  991. break;
  992. case ViewArrangement.LeftResizable | ViewArrangement.TopResizable:
  993. _leftSizeButton!.Visible = true;
  994. _topSizeButton!.Visible = true;
  995. break;
  996. case ViewArrangement.RightResizable | ViewArrangement.TopResizable:
  997. _rightSizeButton!.Visible = true;
  998. _topSizeButton!.Visible = true;
  999. if (_allSizeButton is { })
  1000. {
  1001. _allSizeButton.X = Pos.AnchorEnd ();
  1002. _allSizeButton.Y = 0;
  1003. _allSizeButton.Visible = true;
  1004. }
  1005. break;
  1006. }
  1007. }
  1008. if (_arranging != ViewArrangement.Fixed)
  1009. {
  1010. if (arrangement == ViewArrangement.Fixed)
  1011. {
  1012. // Keyboard mode - enable nav
  1013. // TODO: Keyboard mode only supports sizing from bottom/right.
  1014. _arranging = (ViewArrangement)(Focused?.Data ?? ViewArrangement.Fixed);
  1015. }
  1016. return true;
  1017. }
  1018. // Hack for now
  1019. EndArrangeMode ();
  1020. return false;
  1021. }
  1022. private void AddArrangeModeKeyBindings ()
  1023. {
  1024. AddCommand (Command.Quit, EndArrangeMode);
  1025. AddCommand (
  1026. Command.Up,
  1027. () =>
  1028. {
  1029. if (Parent is null)
  1030. {
  1031. return false;
  1032. }
  1033. if (_arranging == ViewArrangement.Movable)
  1034. {
  1035. Parent!.Y = Parent.Y - 1;
  1036. }
  1037. if (_arranging == ViewArrangement.Resizable)
  1038. {
  1039. if (Parent!.Viewport.Height > 0)
  1040. {
  1041. Parent!.Height = Parent.Height! - 1;
  1042. }
  1043. }
  1044. Application.Refresh ();
  1045. return true;
  1046. });
  1047. AddCommand (
  1048. Command.Down,
  1049. () =>
  1050. {
  1051. if (Parent is null)
  1052. {
  1053. return false;
  1054. }
  1055. if (_arranging == ViewArrangement.Movable)
  1056. {
  1057. Parent!.Y = Parent.Y + 1;
  1058. }
  1059. if (_arranging == ViewArrangement.Resizable)
  1060. {
  1061. Parent!.Height = Parent.Height! + 1;
  1062. }
  1063. Application.Refresh ();
  1064. return true;
  1065. });
  1066. AddCommand (
  1067. Command.Left,
  1068. () =>
  1069. {
  1070. if (Parent is null)
  1071. {
  1072. return false;
  1073. }
  1074. if (_arranging == ViewArrangement.Movable)
  1075. {
  1076. Parent!.X = Parent.X - 1;
  1077. }
  1078. if (_arranging == ViewArrangement.Resizable)
  1079. {
  1080. if (Parent!.Viewport.Width > 0)
  1081. {
  1082. Parent!.Width = Parent.Width! - 1;
  1083. }
  1084. }
  1085. Application.Refresh ();
  1086. return true;
  1087. });
  1088. AddCommand (
  1089. Command.Right,
  1090. () =>
  1091. {
  1092. if (Parent is null)
  1093. {
  1094. return false;
  1095. }
  1096. if (_arranging == ViewArrangement.Movable)
  1097. {
  1098. Parent!.X = Parent.X + 1;
  1099. }
  1100. if (_arranging == ViewArrangement.Resizable)
  1101. {
  1102. Parent!.Width = Parent.Width! + 1;
  1103. }
  1104. Application.Refresh ();
  1105. return true;
  1106. });
  1107. AddCommand (
  1108. Command.Tab,
  1109. () =>
  1110. {
  1111. // BUGBUG: If an arrangable view has only arrangable subviews, it's not possible to activate
  1112. // BUGBUG: ArrangeMode with keyboard for the superview.
  1113. // BUGBUG: AdvanceFocus should be wise to this and when in ArrangeMode, should move across
  1114. // BUGBUG: the view hierachy.
  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, MouseEventArgs 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. }