Border.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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 (MouseEvent 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,
  517. Math.Min (screenBounds.Width - 4, borderBounds.Width - 4)
  518. )
  519. );
  520. Parent.TitleTextFormatter.ConstrainToSize = new (maxTitleWidth, 1);
  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 (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 = Parent.GetFocusColor ();
  560. }
  561. Parent.TitleTextFormatter.Draw (
  562. new (borderBounds.X + 2, titleY, maxTitleWidth, 1),
  563. Parent.HasFocus ? focus : Parent.GetNormalColor (),
  564. Parent.HasFocus ? focus : Parent.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 ();
  574. if (ColorScheme is { })
  575. {
  576. Driver.SetAttribute (GetNormalColor ());
  577. }
  578. else
  579. {
  580. Driver.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. Driver.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. }
  757. private void SetupGradientLineCanvas (LineCanvas lc, Rectangle rect)
  758. {
  759. GetAppealingGradientColors (out List<Color> stops, out List<int> steps);
  760. var g = new Gradient (stops, steps);
  761. var fore = new GradientFill (rect, g, GradientDirection.Diagonal);
  762. var back = new SolidFill (GetNormalColor ().Background);
  763. lc.Fill = new (fore, back);
  764. }
  765. private static void GetAppealingGradientColors (out List<Color> stops, out List<int> steps)
  766. {
  767. // Define the colors of the gradient stops with more appealing colors
  768. stops =
  769. [
  770. new (0, 128, 255), // Bright Blue
  771. new (0, 255, 128), // Bright Green
  772. new (255, 255), // Bright Yellow
  773. new (255, 128), // Bright Orange
  774. new (255, 0, 128)
  775. ];
  776. // Define the number of steps between each color for smoother transitions
  777. // If we pass only a single value then it will assume equal steps between all pairs
  778. steps = [15];
  779. }
  780. private ViewArrangement _arranging;
  781. private Button? _moveButton; // always top-left
  782. private Button? _allSizeButton;
  783. private Button? _leftSizeButton;
  784. private Button? _rightSizeButton;
  785. private Button? _topSizeButton;
  786. private Button? _bottomSizeButton;
  787. /// <summary>
  788. /// Starts "Arrange Mode" where <see cref="Adornment.Parent"/> can be moved and/or resized using the mouse
  789. /// or keyboard. If <paramref name="arrangement"/> is <see cref="ViewArrangement.Fixed"/> keyboard mode is enabled.
  790. /// </summary>
  791. /// <remarks>
  792. /// Arrange Mode is exited by the user pressing <see cref="Application.ArrangeKey"/>, <see cref="Key.Esc"/>, or by
  793. /// clicking
  794. /// the mouse out of the <see cref="Adornment.Parent"/>'s Frame.
  795. /// </remarks>
  796. /// <returns></returns>
  797. public bool? EnterArrangeMode (ViewArrangement arrangement)
  798. {
  799. Debug.Assert (_arranging == ViewArrangement.Fixed);
  800. if (!Parent!.Arrangement.HasFlag (ViewArrangement.Movable)
  801. && !Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable)
  802. && !Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable)
  803. && !Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable)
  804. && !Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable)
  805. )
  806. {
  807. return false;
  808. }
  809. // Add Commands and Keybindigs - Note it's ok these get added each time. KeyBindings are cleared in EndArrange()
  810. AddArrangeModeKeyBindings ();
  811. Application.MouseEvent += ApplicationOnMouseEvent;
  812. // TODO: This code can be refactored to be more readable and maintainable.
  813. // Create buttons for resizing and moving
  814. if (Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  815. {
  816. Debug.Assert (_moveButton is null);
  817. _moveButton = new ()
  818. {
  819. Id = "moveButton",
  820. CanFocus = true,
  821. Width = 1,
  822. Height = 1,
  823. NoDecorations = true,
  824. NoPadding = true,
  825. ShadowStyle = ShadowStyle.None,
  826. Text = $"{Glyphs.Move}",
  827. Visible = false,
  828. Data = ViewArrangement.Movable
  829. };
  830. Add (_moveButton);
  831. }
  832. if (Parent!.Arrangement.HasFlag (ViewArrangement.Resizable))
  833. {
  834. Debug.Assert (_allSizeButton is null);
  835. _allSizeButton = new ()
  836. {
  837. Id = "allSizeButton",
  838. CanFocus = true,
  839. Width = 1,
  840. Height = 1,
  841. NoDecorations = true,
  842. NoPadding = true,
  843. ShadowStyle = ShadowStyle.None,
  844. Text = $"{Glyphs.SizeBottomRight}",
  845. X = Pos.AnchorEnd (),
  846. Y = Pos.AnchorEnd (),
  847. Visible = false,
  848. Data = ViewArrangement.Resizable
  849. };
  850. Add (_allSizeButton);
  851. }
  852. if (Parent!.Arrangement.HasFlag (ViewArrangement.TopResizable))
  853. {
  854. Debug.Assert (_topSizeButton is null);
  855. _topSizeButton = new ()
  856. {
  857. Id = "topSizeButton",
  858. CanFocus = true,
  859. Width = 1,
  860. Height = 1,
  861. NoDecorations = true,
  862. NoPadding = true,
  863. ShadowStyle = ShadowStyle.None,
  864. Text = $"{Glyphs.SizeVertical}",
  865. X = Pos.Center () + Parent!.Margin.Thickness.Horizontal,
  866. Y = 0,
  867. Visible = false,
  868. Data = ViewArrangement.TopResizable
  869. };
  870. Add (_topSizeButton);
  871. }
  872. if (Parent!.Arrangement.HasFlag (ViewArrangement.RightResizable))
  873. {
  874. Debug.Assert (_rightSizeButton is null);
  875. _rightSizeButton = new ()
  876. {
  877. Id = "rightSizeButton",
  878. CanFocus = true,
  879. Width = 1,
  880. Height = 1,
  881. NoDecorations = true,
  882. NoPadding = true,
  883. ShadowStyle = ShadowStyle.None,
  884. Text = $"{Glyphs.SizeHorizontal}",
  885. X = Pos.AnchorEnd (),
  886. Y = Pos.Center () + Parent!.Margin.Thickness.Vertical / 2,
  887. Visible = false,
  888. Data = ViewArrangement.RightResizable
  889. };
  890. Add (_rightSizeButton);
  891. }
  892. if (Parent!.Arrangement.HasFlag (ViewArrangement.LeftResizable))
  893. {
  894. Debug.Assert (_leftSizeButton is null);
  895. _leftSizeButton = new ()
  896. {
  897. Id = "leftSizeButton",
  898. CanFocus = true,
  899. Width = 1,
  900. Height = 1,
  901. NoDecorations = true,
  902. NoPadding = true,
  903. ShadowStyle = ShadowStyle.None,
  904. Text = $"{Glyphs.SizeHorizontal}",
  905. X = 0,
  906. Y = Pos.Center () + Parent!.Margin.Thickness.Vertical / 2,
  907. Visible = false,
  908. Data = ViewArrangement.LeftResizable
  909. };
  910. Add (_leftSizeButton);
  911. }
  912. if (Parent!.Arrangement.HasFlag (ViewArrangement.BottomResizable))
  913. {
  914. Debug.Assert (_bottomSizeButton is null);
  915. _bottomSizeButton = new ()
  916. {
  917. Id = "bottomSizeButton",
  918. CanFocus = true,
  919. Width = 1,
  920. Height = 1,
  921. NoDecorations = true,
  922. NoPadding = true,
  923. ShadowStyle = ShadowStyle.None,
  924. Text = $"{Glyphs.SizeVertical}",
  925. X = Pos.Center () + Parent!.Margin.Thickness.Horizontal / 2,
  926. Y = Pos.AnchorEnd (),
  927. Visible = false,
  928. Data = ViewArrangement.BottomResizable
  929. };
  930. Add (_bottomSizeButton);
  931. }
  932. if (arrangement == ViewArrangement.Fixed)
  933. {
  934. // Keyboard mode
  935. if (Parent!.Arrangement.HasFlag (ViewArrangement.Movable))
  936. {
  937. _moveButton!.Visible = true;
  938. }
  939. if (Parent!.Arrangement.HasFlag (ViewArrangement.Resizable))
  940. {
  941. _allSizeButton!.Visible = true;
  942. }
  943. _arranging = ViewArrangement.Movable;
  944. CanFocus = true;
  945. SetFocus ();
  946. }
  947. else
  948. {
  949. // Mouse mode
  950. _arranging = arrangement;
  951. switch (_arranging)
  952. {
  953. case ViewArrangement.Movable:
  954. _moveButton!.Visible = true;
  955. break;
  956. case ViewArrangement.RightResizable | ViewArrangement.BottomResizable:
  957. case ViewArrangement.Resizable:
  958. _rightSizeButton!.Visible = true;
  959. _bottomSizeButton!.Visible = true;
  960. if (_allSizeButton is { })
  961. {
  962. _allSizeButton!.X = Pos.AnchorEnd ();
  963. _allSizeButton!.Y = Pos.AnchorEnd ();
  964. _allSizeButton!.Visible = true;
  965. }
  966. break;
  967. case ViewArrangement.LeftResizable:
  968. _leftSizeButton!.Visible = true;
  969. break;
  970. case ViewArrangement.RightResizable:
  971. _rightSizeButton!.Visible = true;
  972. break;
  973. case ViewArrangement.TopResizable:
  974. _topSizeButton!.Visible = true;
  975. break;
  976. case ViewArrangement.BottomResizable:
  977. _bottomSizeButton!.Visible = true;
  978. break;
  979. case ViewArrangement.LeftResizable | ViewArrangement.BottomResizable:
  980. _rightSizeButton!.Visible = true;
  981. _bottomSizeButton!.Visible = true;
  982. if (_allSizeButton is { })
  983. {
  984. _allSizeButton.X = 0;
  985. _allSizeButton.Y = Pos.AnchorEnd ();
  986. _allSizeButton.Visible = true;
  987. }
  988. break;
  989. case ViewArrangement.LeftResizable | ViewArrangement.TopResizable:
  990. _leftSizeButton!.Visible = true;
  991. _topSizeButton!.Visible = true;
  992. break;
  993. case ViewArrangement.RightResizable | ViewArrangement.TopResizable:
  994. _rightSizeButton!.Visible = true;
  995. _topSizeButton!.Visible = true;
  996. if (_allSizeButton is { })
  997. {
  998. _allSizeButton.X = Pos.AnchorEnd ();
  999. _allSizeButton.Y = 0;
  1000. _allSizeButton.Visible = true;
  1001. }
  1002. break;
  1003. }
  1004. }
  1005. if (_arranging != ViewArrangement.Fixed)
  1006. {
  1007. if (arrangement == ViewArrangement.Fixed)
  1008. {
  1009. // Keyboard mode - enable nav
  1010. // TODO: Keyboard mode only supports sizing from bottom/right.
  1011. _arranging = (ViewArrangement)(Focused?.Data ?? ViewArrangement.Fixed);
  1012. }
  1013. return true;
  1014. }
  1015. // Hack for now
  1016. EndArrangeMode ();
  1017. return false;
  1018. }
  1019. private void AddArrangeModeKeyBindings ()
  1020. {
  1021. AddCommand (Command.Quit, EndArrangeMode);
  1022. AddCommand (
  1023. Command.Up,
  1024. () =>
  1025. {
  1026. if (Parent is null)
  1027. {
  1028. return false;
  1029. }
  1030. if (_arranging == ViewArrangement.Movable)
  1031. {
  1032. Parent!.Y = Parent.Y - 1;
  1033. }
  1034. if (_arranging == ViewArrangement.Resizable)
  1035. {
  1036. if (Parent!.Viewport.Height > 0)
  1037. {
  1038. Parent!.Height = Parent.Height! - 1;
  1039. }
  1040. }
  1041. Application.Refresh ();
  1042. return true;
  1043. });
  1044. AddCommand (
  1045. Command.Down,
  1046. () =>
  1047. {
  1048. if (Parent is null)
  1049. {
  1050. return false;
  1051. }
  1052. if (_arranging == ViewArrangement.Movable)
  1053. {
  1054. Parent!.Y = Parent.Y + 1;
  1055. }
  1056. if (_arranging == ViewArrangement.Resizable)
  1057. {
  1058. Parent!.Height = Parent.Height! + 1;
  1059. }
  1060. Application.Refresh ();
  1061. return true;
  1062. });
  1063. AddCommand (
  1064. Command.Left,
  1065. () =>
  1066. {
  1067. if (Parent is null)
  1068. {
  1069. return false;
  1070. }
  1071. if (_arranging == ViewArrangement.Movable)
  1072. {
  1073. Parent!.X = Parent.X - 1;
  1074. }
  1075. if (_arranging == ViewArrangement.Resizable)
  1076. {
  1077. if (Parent!.Viewport.Width > 0)
  1078. {
  1079. Parent!.Width = Parent.Width! - 1;
  1080. }
  1081. }
  1082. Application.Refresh ();
  1083. return true;
  1084. });
  1085. AddCommand (
  1086. Command.Right,
  1087. () =>
  1088. {
  1089. if (Parent is null)
  1090. {
  1091. return false;
  1092. }
  1093. if (_arranging == ViewArrangement.Movable)
  1094. {
  1095. Parent!.X = Parent.X + 1;
  1096. }
  1097. if (_arranging == ViewArrangement.Resizable)
  1098. {
  1099. Parent!.Width = Parent.Width! + 1;
  1100. }
  1101. Application.Refresh ();
  1102. return true;
  1103. });
  1104. AddCommand (
  1105. Command.Tab,
  1106. () =>
  1107. {
  1108. // BUGBUG: If an arrangable view has only arrangable subviews, it's not possible to activate
  1109. // BUGBUG: ArrangeMode with keyboard for the superview.
  1110. // BUGBUG: AdvanceFocus should be wise to this and when in ArrangeMode, should move across
  1111. // BUGBUG: the view hierachy.
  1112. AdvanceFocus (NavigationDirection.Forward, TabBehavior.TabStop);
  1113. _arranging = (ViewArrangement)(Focused?.Data ?? ViewArrangement.Fixed);
  1114. return true; // Always eat
  1115. });
  1116. AddCommand (
  1117. Command.BackTab,
  1118. () =>
  1119. {
  1120. AdvanceFocus (NavigationDirection.Backward, TabBehavior.TabStop);
  1121. _arranging = (ViewArrangement)(Focused?.Data ?? ViewArrangement.Fixed);
  1122. return true; // Always eat
  1123. });
  1124. KeyBindings.Add (Key.Esc, KeyBindingScope.HotKey, Command.Quit);
  1125. KeyBindings.Add (Application.ArrangeKey, KeyBindingScope.HotKey, Command.Quit);
  1126. KeyBindings.Add (Key.CursorUp, KeyBindingScope.HotKey, Command.Up);
  1127. KeyBindings.Add (Key.CursorDown, KeyBindingScope.HotKey, Command.Down);
  1128. KeyBindings.Add (Key.CursorLeft, KeyBindingScope.HotKey, Command.Left);
  1129. KeyBindings.Add (Key.CursorRight, KeyBindingScope.HotKey, Command.Right);
  1130. KeyBindings.Add (Key.Tab, KeyBindingScope.HotKey, Command.Tab);
  1131. KeyBindings.Add (Key.Tab.WithShift, KeyBindingScope.HotKey, Command.BackTab);
  1132. }
  1133. private void ApplicationOnMouseEvent (object? sender, MouseEvent e)
  1134. {
  1135. if (e.Flags != MouseFlags.Button1Clicked)
  1136. {
  1137. return;
  1138. }
  1139. // If mouse click is outside of Border.Thickness then exit Arrange Mode
  1140. // e.Position is screen relative
  1141. Point framePos = ScreenToFrame (e.ScreenPosition);
  1142. if (!Thickness.Contains (Frame, framePos))
  1143. {
  1144. EndArrangeMode ();
  1145. }
  1146. }
  1147. private bool? EndArrangeMode ()
  1148. {
  1149. // Debug.Assert (_arranging != ViewArrangement.Fixed);
  1150. _arranging = ViewArrangement.Fixed;
  1151. Application.MouseEvent -= ApplicationOnMouseEvent;
  1152. if (Application.MouseGrabView == this && _dragPosition.HasValue)
  1153. {
  1154. Application.UngrabMouse ();
  1155. }
  1156. if (_moveButton is { })
  1157. {
  1158. Remove (_moveButton);
  1159. _moveButton.Dispose ();
  1160. _moveButton = null;
  1161. }
  1162. if (_allSizeButton is { })
  1163. {
  1164. Remove (_allSizeButton);
  1165. _allSizeButton.Dispose ();
  1166. _allSizeButton = null;
  1167. }
  1168. if (_leftSizeButton is { })
  1169. {
  1170. Remove (_leftSizeButton);
  1171. _leftSizeButton.Dispose ();
  1172. _leftSizeButton = null;
  1173. }
  1174. if (_rightSizeButton is { })
  1175. {
  1176. Remove (_rightSizeButton);
  1177. _rightSizeButton.Dispose ();
  1178. _rightSizeButton = null;
  1179. }
  1180. if (_topSizeButton is { })
  1181. {
  1182. Remove (_topSizeButton);
  1183. _topSizeButton.Dispose ();
  1184. _topSizeButton = null;
  1185. }
  1186. if (_bottomSizeButton is { })
  1187. {
  1188. Remove (_bottomSizeButton);
  1189. _bottomSizeButton.Dispose ();
  1190. _bottomSizeButton = null;
  1191. }
  1192. KeyBindings.Clear ();
  1193. if (CanFocus)
  1194. {
  1195. CanFocus = false;
  1196. }
  1197. return true;
  1198. }
  1199. /// <inheritdoc/>
  1200. protected override void Dispose (bool disposing)
  1201. {
  1202. Application.GrabbingMouse -= Application_GrabbingMouse;
  1203. Application.UnGrabbingMouse -= Application_UnGrabbingMouse;
  1204. _dragPosition = null;
  1205. base.Dispose (disposing);
  1206. }
  1207. }