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