ViewSubViews.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. namespace Terminal.Gui;
  2. public partial class View
  3. {
  4. private static readonly IList<View> _empty = new List<View> (0).AsReadOnly ();
  5. internal bool _addingView;
  6. private List<View> _subviews; // This is null, and allocated on demand.
  7. private View _superView;
  8. /// <summary>Indicates whether the view was added to <see cref="SuperView"/>.</summary>
  9. public bool IsAdded { get; private set; }
  10. /// <summary>Returns a value indicating if this View is currently on Top (Active)</summary>
  11. public bool IsCurrentTop => Application.Current == this;
  12. /// <summary>This returns a list of the subviews contained by this view.</summary>
  13. /// <value>The subviews.</value>
  14. public IList<View> Subviews => _subviews?.AsReadOnly () ?? _empty;
  15. /// <summary>Returns the container for this view, or null if this view has not been added to a container.</summary>
  16. /// <value>The super view.</value>
  17. public virtual View SuperView
  18. {
  19. get => _superView;
  20. set => throw new NotImplementedException ();
  21. }
  22. // Internally, we use InternalSubviews rather than subviews, as we do not expect us
  23. // to make the same mistakes our users make when they poke at the Subviews.
  24. internal IList<View> InternalSubviews => _subviews ?? _empty;
  25. /// <summary>Adds a subview (child) to this view.</summary>
  26. /// <remarks>
  27. /// <para>
  28. /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also
  29. /// <seealso cref="Remove(View)"/> <seealso cref="RemoveAll"/>
  30. /// </para>
  31. /// <para>
  32. /// Subviews will be disposed when this View is disposed. In other-words, calling this method causes
  33. /// the lifecycle of the subviews to be transferred to this View.
  34. /// </para>
  35. /// </remarks>
  36. public virtual void Add (View view)
  37. {
  38. if (view is null)
  39. {
  40. return;
  41. }
  42. if (_subviews is null)
  43. {
  44. _subviews = new List<View> ();
  45. }
  46. if (_tabIndexes is null)
  47. {
  48. _tabIndexes = new List<View> ();
  49. }
  50. _subviews.Add (view);
  51. _tabIndexes.Add (view);
  52. view._superView = this;
  53. if (view.CanFocus)
  54. {
  55. _addingView = true;
  56. if (SuperView?.CanFocus == false)
  57. {
  58. SuperView._addingView = true;
  59. SuperView.CanFocus = true;
  60. SuperView._addingView = false;
  61. }
  62. CanFocus = true;
  63. view._tabIndex = _tabIndexes.IndexOf (view);
  64. _addingView = false;
  65. }
  66. if (view.Enabled && !Enabled)
  67. {
  68. view._oldEnabled = true;
  69. view.Enabled = false;
  70. }
  71. OnAdded (new SuperViewChangedEventArgs (this, view));
  72. if (IsInitialized && !view.IsInitialized)
  73. {
  74. view.BeginInit ();
  75. view.EndInit ();
  76. }
  77. SetNeedsLayout ();
  78. SetNeedsDisplay ();
  79. }
  80. /// <summary>Adds the specified views (children) to the view.</summary>
  81. /// <param name="views">Array of one or more views (can be optional parameter).</param>
  82. /// <remarks>
  83. /// <para>
  84. /// The Views that have been added to this view can be retrieved via the <see cref="Subviews"/> property. See also
  85. /// <seealso cref="Remove(View)"/> and <seealso cref="RemoveAll"/>.
  86. /// </para>
  87. /// <para>
  88. /// Subviews will be disposed when this View is disposed. In other-words, calling this method causes
  89. /// the lifecycle of the subviews to be transferred to this View.
  90. /// </para>
  91. /// </remarks>
  92. public void Add (params View [] views)
  93. {
  94. if (views is null)
  95. {
  96. return;
  97. }
  98. foreach (View view in views)
  99. {
  100. Add (view);
  101. }
  102. }
  103. /// <summary>Event fired when this view is added to another.</summary>
  104. public event EventHandler<SuperViewChangedEventArgs> Added;
  105. /// <summary>Moves the subview backwards in the hierarchy, only one step</summary>
  106. /// <param name="subview">The subview to send backwards</param>
  107. /// <remarks>If you want to send the view all the way to the back use SendSubviewToBack.</remarks>
  108. public void BringSubviewForward (View subview)
  109. {
  110. PerformActionForSubview (
  111. subview,
  112. x =>
  113. {
  114. int idx = _subviews.IndexOf (x);
  115. if (idx + 1 < _subviews.Count)
  116. {
  117. _subviews.Remove (x);
  118. _subviews.Insert (idx + 1, x);
  119. }
  120. }
  121. );
  122. }
  123. /// <summary>Brings the specified subview to the front so it is drawn on top of any other views.</summary>
  124. /// <param name="subview">The subview to send to the front</param>
  125. /// <remarks><seealso cref="SendSubviewToBack"/>.</remarks>
  126. public void BringSubviewToFront (View subview)
  127. {
  128. PerformActionForSubview (
  129. subview,
  130. x =>
  131. {
  132. _subviews.Remove (x);
  133. _subviews.Add (x);
  134. }
  135. );
  136. }
  137. /// <summary>Get the top superview of a given <see cref="View"/>.</summary>
  138. /// <returns>The superview view.</returns>
  139. public View GetTopSuperView (View view = null, View superview = null)
  140. {
  141. View top = superview ?? Application.Top;
  142. for (View v = view?.SuperView ?? this?.SuperView; v != null; v = v.SuperView)
  143. {
  144. top = v;
  145. if (top == superview)
  146. {
  147. break;
  148. }
  149. }
  150. return top;
  151. }
  152. /// <summary>Method invoked when a subview is being added to this view.</summary>
  153. /// <param name="e">Event where <see cref="ViewEventArgs.View"/> is the subview being added.</param>
  154. public virtual void OnAdded (SuperViewChangedEventArgs e)
  155. {
  156. View view = e.Child;
  157. view.IsAdded = true;
  158. view.OnResizeNeeded ();
  159. view.Added?.Invoke (this, e);
  160. }
  161. /// <summary>Method invoked when a subview is being removed from this view.</summary>
  162. /// <param name="e">Event args describing the subview being removed.</param>
  163. public virtual void OnRemoved (SuperViewChangedEventArgs e)
  164. {
  165. View view = e.Child;
  166. view.IsAdded = false;
  167. view.Removed?.Invoke (this, e);
  168. }
  169. /// <summary>Removes a subview added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.</summary>
  170. /// <remarks>
  171. /// <para>
  172. /// Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the Subview's
  173. /// lifecycle to be transferred to the caller; the caller must call <see cref="Dispose"/>.
  174. /// </para>
  175. /// </remarks>
  176. public virtual void Remove (View view)
  177. {
  178. if (view is null || _subviews is null)
  179. {
  180. return;
  181. }
  182. Rectangle touched = view.Frame;
  183. _subviews.Remove (view);
  184. _tabIndexes.Remove (view);
  185. view._superView = null;
  186. view._tabIndex = -1;
  187. SetNeedsLayout ();
  188. SetNeedsDisplay ();
  189. foreach (View v in _subviews)
  190. {
  191. if (v.Frame.IntersectsWith (touched))
  192. {
  193. view.SetNeedsDisplay ();
  194. }
  195. }
  196. OnRemoved (new SuperViewChangedEventArgs (this, view));
  197. if (Focused == view)
  198. {
  199. Focused = null;
  200. }
  201. }
  202. /// <summary>
  203. /// Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.
  204. /// </summary>
  205. /// <remarks>
  206. /// <para>
  207. /// Normally Subviews will be disposed when this View is disposed. Removing a Subview causes ownership of the Subview's
  208. /// lifecycle to be transferred to the caller; the caller must call <see cref="Dispose"/> on any Views that were added.
  209. /// </para>
  210. /// </remarks>
  211. public virtual void RemoveAll ()
  212. {
  213. if (_subviews is null)
  214. {
  215. return;
  216. }
  217. while (_subviews.Count > 0)
  218. {
  219. Remove (_subviews [0]);
  220. }
  221. }
  222. /// <summary>Event fired when this view is removed from another.</summary>
  223. public event EventHandler<SuperViewChangedEventArgs> Removed;
  224. /// <summary>Moves the subview backwards in the hierarchy, only one step</summary>
  225. /// <param name="subview">The subview to send backwards</param>
  226. /// <remarks>If you want to send the view all the way to the back use SendSubviewToBack.</remarks>
  227. public void SendSubviewBackwards (View subview)
  228. {
  229. PerformActionForSubview (
  230. subview,
  231. x =>
  232. {
  233. int idx = _subviews.IndexOf (x);
  234. if (idx > 0)
  235. {
  236. _subviews.Remove (x);
  237. _subviews.Insert (idx - 1, x);
  238. }
  239. }
  240. );
  241. }
  242. /// <summary>Sends the specified subview to the front so it is the first view drawn</summary>
  243. /// <param name="subview">The subview to send to the front</param>
  244. /// <remarks><seealso cref="BringSubviewToFront(View)"/>.</remarks>
  245. public void SendSubviewToBack (View subview)
  246. {
  247. PerformActionForSubview (
  248. subview,
  249. x =>
  250. {
  251. _subviews.Remove (x);
  252. _subviews.Insert (0, subview);
  253. }
  254. );
  255. }
  256. private void PerformActionForSubview (View subview, Action<View> action)
  257. {
  258. if (_subviews.Contains (subview))
  259. {
  260. action (subview);
  261. }
  262. SetNeedsDisplay ();
  263. subview.SetNeedsDisplay ();
  264. }
  265. #region Focus
  266. /// <summary>Exposed as `internal` for unit tests. Indicates focus navigation direction.</summary>
  267. internal enum NavigationDirection
  268. {
  269. /// <summary>Navigate forward.</summary>
  270. Forward,
  271. /// <summary>Navigate backwards.</summary>
  272. Backward
  273. }
  274. /// <summary>Event fired when the view gets focus.</summary>
  275. public event EventHandler<FocusEventArgs> Enter;
  276. /// <summary>Event fired when the view looses focus.</summary>
  277. public event EventHandler<FocusEventArgs> Leave;
  278. private NavigationDirection _focusDirection;
  279. internal NavigationDirection FocusDirection
  280. {
  281. get => SuperView?.FocusDirection ?? _focusDirection;
  282. set
  283. {
  284. if (SuperView is { })
  285. {
  286. SuperView.FocusDirection = value;
  287. }
  288. else
  289. {
  290. _focusDirection = value;
  291. }
  292. }
  293. }
  294. private bool _hasFocus;
  295. /// <inheritdoc/>
  296. public bool HasFocus
  297. {
  298. set => SetHasFocus (value, this, true);
  299. get => _hasFocus;
  300. }
  301. private void SetHasFocus (bool value, View view, bool force = false)
  302. {
  303. if (HasFocus != value || force)
  304. {
  305. _hasFocus = value;
  306. if (value)
  307. {
  308. OnEnter (view);
  309. }
  310. else
  311. {
  312. OnLeave (view);
  313. }
  314. SetNeedsDisplay ();
  315. }
  316. // Remove focus down the chain of subviews if focus is removed
  317. if (!value && Focused is { })
  318. {
  319. View f = Focused;
  320. f.OnLeave (view);
  321. f.SetHasFocus (false, view);
  322. Focused = null;
  323. }
  324. }
  325. /// <summary>Event fired when the <see cref="CanFocus"/> value is being changed.</summary>
  326. public event EventHandler CanFocusChanged;
  327. /// <summary>Method invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  328. public virtual void OnCanFocusChanged () { CanFocusChanged?.Invoke (this, EventArgs.Empty); }
  329. private bool _oldCanFocus;
  330. private bool _canFocus;
  331. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> can focus.</summary>
  332. public bool CanFocus
  333. {
  334. get => _canFocus;
  335. set
  336. {
  337. if (!_addingView && IsInitialized && SuperView?.CanFocus == false && value)
  338. {
  339. throw new InvalidOperationException ("Cannot set CanFocus to true if the SuperView CanFocus is false!");
  340. }
  341. if (_canFocus == value)
  342. {
  343. return;
  344. }
  345. _canFocus = value;
  346. switch (_canFocus)
  347. {
  348. case false when _tabIndex > -1:
  349. TabIndex = -1;
  350. break;
  351. case true when SuperView?.CanFocus == false && _addingView:
  352. SuperView.CanFocus = true;
  353. break;
  354. }
  355. if (_canFocus && _tabIndex == -1)
  356. {
  357. TabIndex = SuperView is { } ? SuperView._tabIndexes.IndexOf (this) : -1;
  358. }
  359. TabStop = _canFocus;
  360. if (!_canFocus && SuperView?.Focused == this)
  361. {
  362. SuperView.Focused = null;
  363. }
  364. if (!_canFocus && HasFocus)
  365. {
  366. SetHasFocus (false, this);
  367. SuperView?.EnsureFocus ();
  368. if (SuperView is { } && SuperView.Focused is null)
  369. {
  370. SuperView.FocusNext ();
  371. if (SuperView.Focused is null && Application.Current is { })
  372. {
  373. Application.Current.FocusNext ();
  374. }
  375. Application.BringOverlappedTopToFront ();
  376. }
  377. }
  378. if (_subviews is { } && IsInitialized)
  379. {
  380. foreach (View view in _subviews)
  381. {
  382. if (view.CanFocus != value)
  383. {
  384. if (!value)
  385. {
  386. view._oldCanFocus = view.CanFocus;
  387. view._oldTabIndex = view._tabIndex;
  388. view.CanFocus = false;
  389. view._tabIndex = -1;
  390. }
  391. else
  392. {
  393. if (_addingView)
  394. {
  395. view._addingView = true;
  396. }
  397. view.CanFocus = view._oldCanFocus;
  398. view._tabIndex = view._oldTabIndex;
  399. view._addingView = false;
  400. }
  401. }
  402. }
  403. }
  404. OnCanFocusChanged ();
  405. SetNeedsDisplay ();
  406. }
  407. }
  408. /// <summary>Method invoked when a view gets focus.</summary>
  409. /// <param name="view">The view that is losing focus.</param>
  410. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  411. public virtual bool OnEnter (View view)
  412. {
  413. var args = new FocusEventArgs (view);
  414. Enter?.Invoke (this, args);
  415. if (args.Handled)
  416. {
  417. return true;
  418. }
  419. return false;
  420. }
  421. /// <summary>Method invoked when a view loses focus.</summary>
  422. /// <param name="view">The view that is getting focus.</param>
  423. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  424. public virtual bool OnLeave (View view)
  425. {
  426. var args = new FocusEventArgs (view);
  427. Leave?.Invoke (this, args);
  428. if (args.Handled)
  429. {
  430. return true;
  431. }
  432. Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  433. return false;
  434. }
  435. /// <summary>Returns the currently focused view inside this view, or null if nothing is focused.</summary>
  436. /// <value>The focused.</value>
  437. public View Focused { get; private set; }
  438. /// <summary>Returns the most focused view in the chain of subviews (the leaf view that has the focus).</summary>
  439. /// <value>The most focused View.</value>
  440. public View MostFocused
  441. {
  442. get
  443. {
  444. if (Focused is null)
  445. {
  446. return null;
  447. }
  448. View most = Focused.MostFocused;
  449. if (most is { })
  450. {
  451. return most;
  452. }
  453. return Focused;
  454. }
  455. }
  456. /// <summary>Causes the specified subview to have focus.</summary>
  457. /// <param name="view">View.</param>
  458. private void SetFocus (View view)
  459. {
  460. if (view is null)
  461. {
  462. return;
  463. }
  464. //Console.WriteLine ($"Request to focus {view}");
  465. if (!view.CanFocus || !view.Visible || !view.Enabled)
  466. {
  467. return;
  468. }
  469. if (Focused?._hasFocus == true && Focused == view)
  470. {
  471. return;
  472. }
  473. if ((Focused?._hasFocus == true && Focused?.SuperView == view) || view == this)
  474. {
  475. if (!view._hasFocus)
  476. {
  477. view._hasFocus = true;
  478. }
  479. return;
  480. }
  481. // Make sure that this view is a subview
  482. View c;
  483. for (c = view._superView; c != null; c = c._superView)
  484. {
  485. if (c == this)
  486. {
  487. break;
  488. }
  489. }
  490. if (c is null)
  491. {
  492. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  493. }
  494. if (Focused is { })
  495. {
  496. Focused.SetHasFocus (false, view);
  497. }
  498. View f = Focused;
  499. Focused = view;
  500. Focused.SetHasFocus (true, f);
  501. Focused.EnsureFocus ();
  502. // Send focus upwards
  503. if (SuperView is { })
  504. {
  505. SuperView.SetFocus (this);
  506. }
  507. else
  508. {
  509. SetFocus (this);
  510. }
  511. }
  512. /// <summary>Causes the specified view and the entire parent hierarchy to have the focused order updated.</summary>
  513. public void SetFocus ()
  514. {
  515. if (!CanBeVisible (this) || !Enabled)
  516. {
  517. if (HasFocus)
  518. {
  519. SetHasFocus (false, this);
  520. }
  521. return;
  522. }
  523. if (SuperView is { })
  524. {
  525. SuperView.SetFocus (this);
  526. }
  527. else
  528. {
  529. SetFocus (this);
  530. }
  531. }
  532. /// <summary>
  533. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise,
  534. /// does nothing.
  535. /// </summary>
  536. public void EnsureFocus ()
  537. {
  538. if (Focused is null && _subviews?.Count > 0)
  539. {
  540. if (FocusDirection == NavigationDirection.Forward)
  541. {
  542. FocusFirst ();
  543. }
  544. else
  545. {
  546. FocusLast ();
  547. }
  548. }
  549. }
  550. /// <summary>Focuses the first focusable subview if one exists.</summary>
  551. public void FocusFirst ()
  552. {
  553. if (!CanBeVisible (this))
  554. {
  555. return;
  556. }
  557. if (_tabIndexes is null)
  558. {
  559. SuperView?.SetFocus (this);
  560. return;
  561. }
  562. foreach (View view in _tabIndexes)
  563. {
  564. if (view.CanFocus && view._tabStop && view.Visible && view.Enabled)
  565. {
  566. SetFocus (view);
  567. return;
  568. }
  569. }
  570. }
  571. /// <summary>Focuses the last focusable subview if one exists.</summary>
  572. public void FocusLast ()
  573. {
  574. if (!CanBeVisible (this))
  575. {
  576. return;
  577. }
  578. if (_tabIndexes is null)
  579. {
  580. SuperView?.SetFocus (this);
  581. return;
  582. }
  583. for (int i = _tabIndexes.Count; i > 0;)
  584. {
  585. i--;
  586. View v = _tabIndexes [i];
  587. if (v.CanFocus && v._tabStop && v.Visible && v.Enabled)
  588. {
  589. SetFocus (v);
  590. return;
  591. }
  592. }
  593. }
  594. /// <summary>Focuses the previous view.</summary>
  595. /// <returns><see langword="true"/> if previous was focused, <see langword="false"/> otherwise.</returns>
  596. public bool FocusPrev ()
  597. {
  598. if (!CanBeVisible (this))
  599. {
  600. return false;
  601. }
  602. FocusDirection = NavigationDirection.Backward;
  603. if (_tabIndexes is null || _tabIndexes.Count == 0)
  604. {
  605. return false;
  606. }
  607. if (Focused is null)
  608. {
  609. FocusLast ();
  610. return Focused != null;
  611. }
  612. int focusedIdx = -1;
  613. for (int i = _tabIndexes.Count; i > 0;)
  614. {
  615. i--;
  616. View w = _tabIndexes [i];
  617. if (w.HasFocus)
  618. {
  619. if (w.FocusPrev ())
  620. {
  621. return true;
  622. }
  623. focusedIdx = i;
  624. continue;
  625. }
  626. if (w.CanFocus && focusedIdx != -1 && w._tabStop && w.Visible && w.Enabled)
  627. {
  628. Focused.SetHasFocus (false, w);
  629. if (w.CanFocus && w._tabStop && w.Visible && w.Enabled)
  630. {
  631. w.FocusLast ();
  632. }
  633. SetFocus (w);
  634. return true;
  635. }
  636. }
  637. if (Focused is { })
  638. {
  639. Focused.SetHasFocus (false, this);
  640. Focused = null;
  641. }
  642. return false;
  643. }
  644. /// <summary>Focuses the next view.</summary>
  645. /// <returns><see langword="true"/> if next was focused, <see langword="false"/> otherwise.</returns>
  646. public bool FocusNext ()
  647. {
  648. if (!CanBeVisible (this))
  649. {
  650. return false;
  651. }
  652. FocusDirection = NavigationDirection.Forward;
  653. if (_tabIndexes is null || _tabIndexes.Count == 0)
  654. {
  655. return false;
  656. }
  657. if (Focused is null)
  658. {
  659. FocusFirst ();
  660. return Focused != null;
  661. }
  662. int focusedIdx = -1;
  663. for (var i = 0; i < _tabIndexes.Count; i++)
  664. {
  665. View w = _tabIndexes [i];
  666. if (w.HasFocus)
  667. {
  668. if (w.FocusNext ())
  669. {
  670. return true;
  671. }
  672. focusedIdx = i;
  673. continue;
  674. }
  675. if (w.CanFocus && focusedIdx != -1 && w._tabStop && w.Visible && w.Enabled)
  676. {
  677. Focused.SetHasFocus (false, w);
  678. if (w.CanFocus && w._tabStop && w.Visible && w.Enabled)
  679. {
  680. w.FocusFirst ();
  681. }
  682. SetFocus (w);
  683. return true;
  684. }
  685. }
  686. if (Focused is { })
  687. {
  688. Focused.SetHasFocus (false, this);
  689. Focused = null;
  690. }
  691. return false;
  692. }
  693. private View GetMostFocused (View view)
  694. {
  695. if (view is null)
  696. {
  697. return null;
  698. }
  699. return view.Focused is { } ? GetMostFocused (view.Focused) : view;
  700. }
  701. /// <summary>Positions the cursor in the right position based on the currently focused view in the chain.</summary>
  702. /// Views that are focusable should override
  703. /// <see cref="PositionCursor"/>
  704. /// to ensure
  705. /// the cursor is placed in a location that makes sense. Unix terminals do not have
  706. /// a way of hiding the cursor, so it can be distracting to have the cursor left at
  707. /// the last focused view. Views should make sure that they place the cursor
  708. /// in a visually sensible place.
  709. public virtual void PositionCursor ()
  710. {
  711. if (!CanBeVisible (this) || !Enabled)
  712. {
  713. return;
  714. }
  715. // BUGBUG: v2 - This needs to support Subviews of Adornments too
  716. if (Focused is null && SuperView is { })
  717. {
  718. SuperView.EnsureFocus ();
  719. }
  720. else if (Focused is { Visible: true, Enabled: true, Frame: { Width: > 0, Height: > 0 } })
  721. {
  722. Focused.PositionCursor ();
  723. }
  724. else if (Focused?.Visible == true && Focused?.Enabled == false)
  725. {
  726. Focused = null;
  727. }
  728. else if (CanFocus && HasFocus && Visible && Frame.Width > 0 && Frame.Height > 0)
  729. {
  730. Move (TextFormatter.HotKeyPos == -1 ? 0 : TextFormatter.CursorPosition, 0);
  731. }
  732. else
  733. {
  734. Move (_frame.X, _frame.Y);
  735. }
  736. }
  737. #endregion Focus
  738. }