Border.cs 52 KB

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