Border.cs 51 KB

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