Border.cs 52 KB

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