Border.cs 51 KB

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