ViewSubViews.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 muse 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>Removes all subviews (children) added via <see cref="Add(View)"/> or <see cref="Add(View[])"/> from this View.</summary>
  203. public virtual void RemoveAll ()
  204. {
  205. if (_subviews is null)
  206. {
  207. return;
  208. }
  209. while (_subviews.Count > 0)
  210. {
  211. Remove (_subviews [0]);
  212. }
  213. }
  214. /// <summary>Event fired when this view is removed from another.</summary>
  215. public event EventHandler<SuperViewChangedEventArgs> Removed;
  216. /// <summary>Moves the subview backwards in the hierarchy, only one step</summary>
  217. /// <param name="subview">The subview to send backwards</param>
  218. /// <remarks>If you want to send the view all the way to the back use SendSubviewToBack.</remarks>
  219. public void SendSubviewBackwards (View subview)
  220. {
  221. PerformActionForSubview (
  222. subview,
  223. x =>
  224. {
  225. int idx = _subviews.IndexOf (x);
  226. if (idx > 0)
  227. {
  228. _subviews.Remove (x);
  229. _subviews.Insert (idx - 1, x);
  230. }
  231. }
  232. );
  233. }
  234. /// <summary>Sends the specified subview to the front so it is the first view drawn</summary>
  235. /// <param name="subview">The subview to send to the front</param>
  236. /// <remarks><seealso cref="BringSubviewToFront(View)"/>.</remarks>
  237. public void SendSubviewToBack (View subview)
  238. {
  239. PerformActionForSubview (
  240. subview,
  241. x =>
  242. {
  243. _subviews.Remove (x);
  244. _subviews.Insert (0, subview);
  245. }
  246. );
  247. }
  248. private void PerformActionForSubview (View subview, Action<View> action)
  249. {
  250. if (_subviews.Contains (subview))
  251. {
  252. action (subview);
  253. }
  254. SetNeedsDisplay ();
  255. subview.SetNeedsDisplay ();
  256. }
  257. #region Focus
  258. /// <summary>Exposed as `internal` for unit tests. Indicates focus navigation direction.</summary>
  259. internal enum NavigationDirection
  260. {
  261. /// <summary>Navigate forward.</summary>
  262. Forward,
  263. /// <summary>Navigate backwards.</summary>
  264. Backward
  265. }
  266. /// <summary>Event fired when the view gets focus.</summary>
  267. public event EventHandler<FocusEventArgs> Enter;
  268. /// <summary>Event fired when the view looses focus.</summary>
  269. public event EventHandler<FocusEventArgs> Leave;
  270. private NavigationDirection _focusDirection;
  271. internal NavigationDirection FocusDirection
  272. {
  273. get => SuperView?.FocusDirection ?? _focusDirection;
  274. set
  275. {
  276. if (SuperView is { })
  277. {
  278. SuperView.FocusDirection = value;
  279. }
  280. else
  281. {
  282. _focusDirection = value;
  283. }
  284. }
  285. }
  286. private bool _hasFocus;
  287. /// <inheritdoc/>
  288. public bool HasFocus
  289. {
  290. set => SetHasFocus (value, this, true);
  291. get => _hasFocus;
  292. }
  293. private void SetHasFocus (bool value, View view, bool force = false)
  294. {
  295. if (HasFocus != value || force)
  296. {
  297. _hasFocus = value;
  298. if (value)
  299. {
  300. OnEnter (view);
  301. }
  302. else
  303. {
  304. OnLeave (view);
  305. }
  306. SetNeedsDisplay ();
  307. }
  308. // Remove focus down the chain of subviews if focus is removed
  309. if (!value && Focused is { })
  310. {
  311. View f = Focused;
  312. f.OnLeave (view);
  313. f.SetHasFocus (false, view);
  314. Focused = null;
  315. }
  316. }
  317. /// <summary>Event fired when the <see cref="CanFocus"/> value is being changed.</summary>
  318. public event EventHandler CanFocusChanged;
  319. /// <summary>Method invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  320. public virtual void OnCanFocusChanged () { CanFocusChanged?.Invoke (this, EventArgs.Empty); }
  321. private bool _oldCanFocus;
  322. private bool _canFocus;
  323. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> can focus.</summary>
  324. public bool CanFocus
  325. {
  326. get => _canFocus;
  327. set
  328. {
  329. if (!_addingView && IsInitialized && SuperView?.CanFocus == false && value)
  330. {
  331. throw new InvalidOperationException ("Cannot set CanFocus to true if the SuperView CanFocus is false!");
  332. }
  333. if (_canFocus == value)
  334. {
  335. return;
  336. }
  337. _canFocus = value;
  338. switch (_canFocus)
  339. {
  340. case false when _tabIndex > -1:
  341. TabIndex = -1;
  342. break;
  343. case true when SuperView?.CanFocus == false && _addingView:
  344. SuperView.CanFocus = true;
  345. break;
  346. }
  347. if (_canFocus && _tabIndex == -1)
  348. {
  349. TabIndex = SuperView is { } ? SuperView._tabIndexes.IndexOf (this) : -1;
  350. }
  351. TabStop = _canFocus;
  352. if (!_canFocus && SuperView?.Focused == this)
  353. {
  354. SuperView.Focused = null;
  355. }
  356. if (!_canFocus && HasFocus)
  357. {
  358. SetHasFocus (false, this);
  359. SuperView?.EnsureFocus ();
  360. if (SuperView is { } && SuperView.Focused is null)
  361. {
  362. SuperView.FocusNext ();
  363. if (SuperView.Focused is null && Application.Current is { })
  364. {
  365. Application.Current.FocusNext ();
  366. }
  367. Application.BringOverlappedTopToFront ();
  368. }
  369. }
  370. if (_subviews is { } && IsInitialized)
  371. {
  372. foreach (View view in _subviews)
  373. {
  374. if (view.CanFocus != value)
  375. {
  376. if (!value)
  377. {
  378. view._oldCanFocus = view.CanFocus;
  379. view._oldTabIndex = view._tabIndex;
  380. view.CanFocus = false;
  381. view._tabIndex = -1;
  382. }
  383. else
  384. {
  385. if (_addingView)
  386. {
  387. view._addingView = true;
  388. }
  389. view.CanFocus = view._oldCanFocus;
  390. view._tabIndex = view._oldTabIndex;
  391. view._addingView = false;
  392. }
  393. }
  394. }
  395. }
  396. OnCanFocusChanged ();
  397. SetNeedsDisplay ();
  398. }
  399. }
  400. /// <summary>Method invoked when a view gets focus.</summary>
  401. /// <param name="view">The view that is losing focus.</param>
  402. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  403. public virtual bool OnEnter (View view)
  404. {
  405. var args = new FocusEventArgs (view);
  406. Enter?.Invoke (this, args);
  407. if (args.Handled)
  408. {
  409. return true;
  410. }
  411. return false;
  412. }
  413. /// <summary>Method invoked when a view loses focus.</summary>
  414. /// <param name="view">The view that is getting focus.</param>
  415. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  416. public virtual bool OnLeave (View view)
  417. {
  418. var args = new FocusEventArgs (view);
  419. Leave?.Invoke (this, args);
  420. if (args.Handled)
  421. {
  422. return true;
  423. }
  424. Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  425. return false;
  426. }
  427. /// <summary>Returns the currently focused view inside this view, or null if nothing is focused.</summary>
  428. /// <value>The focused.</value>
  429. public View Focused { get; private set; }
  430. /// <summary>Returns the most focused view in the chain of subviews (the leaf view that has the focus).</summary>
  431. /// <value>The most focused View.</value>
  432. public View MostFocused
  433. {
  434. get
  435. {
  436. if (Focused is null)
  437. {
  438. return null;
  439. }
  440. View most = Focused.MostFocused;
  441. if (most is { })
  442. {
  443. return most;
  444. }
  445. return Focused;
  446. }
  447. }
  448. /// <summary>Causes the specified subview to have focus.</summary>
  449. /// <param name="view">View.</param>
  450. private void SetFocus (View view)
  451. {
  452. if (view is null)
  453. {
  454. return;
  455. }
  456. //Console.WriteLine ($"Request to focus {view}");
  457. if (!view.CanFocus || !view.Visible || !view.Enabled)
  458. {
  459. return;
  460. }
  461. if (Focused?._hasFocus == true && Focused == view)
  462. {
  463. return;
  464. }
  465. if ((Focused?._hasFocus == true && Focused?.SuperView == view) || view == this)
  466. {
  467. if (!view._hasFocus)
  468. {
  469. view._hasFocus = true;
  470. }
  471. return;
  472. }
  473. // Make sure that this view is a subview
  474. View c;
  475. for (c = view._superView; c != null; c = c._superView)
  476. {
  477. if (c == this)
  478. {
  479. break;
  480. }
  481. }
  482. if (c is null)
  483. {
  484. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  485. }
  486. if (Focused is { })
  487. {
  488. Focused.SetHasFocus (false, view);
  489. }
  490. View f = Focused;
  491. Focused = view;
  492. Focused.SetHasFocus (true, f);
  493. Focused.EnsureFocus ();
  494. // Send focus upwards
  495. if (SuperView is { })
  496. {
  497. SuperView.SetFocus (this);
  498. }
  499. else
  500. {
  501. SetFocus (this);
  502. }
  503. }
  504. /// <summary>Causes the specified view and the entire parent hierarchy to have the focused order updated.</summary>
  505. public void SetFocus ()
  506. {
  507. if (!CanBeVisible (this) || !Enabled)
  508. {
  509. if (HasFocus)
  510. {
  511. SetHasFocus (false, this);
  512. }
  513. return;
  514. }
  515. if (SuperView is { })
  516. {
  517. SuperView.SetFocus (this);
  518. }
  519. else
  520. {
  521. SetFocus (this);
  522. }
  523. }
  524. /// <summary>
  525. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise,
  526. /// does nothing.
  527. /// </summary>
  528. public void EnsureFocus ()
  529. {
  530. if (Focused is null && _subviews?.Count > 0)
  531. {
  532. if (FocusDirection == NavigationDirection.Forward)
  533. {
  534. FocusFirst ();
  535. }
  536. else
  537. {
  538. FocusLast ();
  539. }
  540. }
  541. }
  542. /// <summary>Focuses the first focusable subview if one exists.</summary>
  543. public void FocusFirst ()
  544. {
  545. if (!CanBeVisible (this))
  546. {
  547. return;
  548. }
  549. if (_tabIndexes is null)
  550. {
  551. SuperView?.SetFocus (this);
  552. return;
  553. }
  554. foreach (View view in _tabIndexes)
  555. {
  556. if (view.CanFocus && view._tabStop && view.Visible && view.Enabled)
  557. {
  558. SetFocus (view);
  559. return;
  560. }
  561. }
  562. }
  563. /// <summary>Focuses the last focusable subview if one exists.</summary>
  564. public void FocusLast ()
  565. {
  566. if (!CanBeVisible (this))
  567. {
  568. return;
  569. }
  570. if (_tabIndexes is null)
  571. {
  572. SuperView?.SetFocus (this);
  573. return;
  574. }
  575. for (int i = _tabIndexes.Count; i > 0;)
  576. {
  577. i--;
  578. View v = _tabIndexes [i];
  579. if (v.CanFocus && v._tabStop && v.Visible && v.Enabled)
  580. {
  581. SetFocus (v);
  582. return;
  583. }
  584. }
  585. }
  586. /// <summary>Focuses the previous view.</summary>
  587. /// <returns><see langword="true"/> if previous was focused, <see langword="false"/> otherwise.</returns>
  588. public bool FocusPrev ()
  589. {
  590. if (!CanBeVisible (this))
  591. {
  592. return false;
  593. }
  594. FocusDirection = NavigationDirection.Backward;
  595. if (_tabIndexes is null || _tabIndexes.Count == 0)
  596. {
  597. return false;
  598. }
  599. if (Focused is null)
  600. {
  601. FocusLast ();
  602. return Focused != null;
  603. }
  604. int focusedIdx = -1;
  605. for (int i = _tabIndexes.Count; i > 0;)
  606. {
  607. i--;
  608. View w = _tabIndexes [i];
  609. if (w.HasFocus)
  610. {
  611. if (w.FocusPrev ())
  612. {
  613. return true;
  614. }
  615. focusedIdx = i;
  616. continue;
  617. }
  618. if (w.CanFocus && focusedIdx != -1 && w._tabStop && w.Visible && w.Enabled)
  619. {
  620. Focused.SetHasFocus (false, w);
  621. if (w.CanFocus && w._tabStop && w.Visible && w.Enabled)
  622. {
  623. w.FocusLast ();
  624. }
  625. SetFocus (w);
  626. return true;
  627. }
  628. }
  629. if (Focused is { })
  630. {
  631. Focused.SetHasFocus (false, this);
  632. Focused = null;
  633. }
  634. return false;
  635. }
  636. /// <summary>Focuses the next view.</summary>
  637. /// <returns><see langword="true"/> if next was focused, <see langword="false"/> otherwise.</returns>
  638. public bool FocusNext ()
  639. {
  640. if (!CanBeVisible (this))
  641. {
  642. return false;
  643. }
  644. FocusDirection = NavigationDirection.Forward;
  645. if (_tabIndexes is null || _tabIndexes.Count == 0)
  646. {
  647. return false;
  648. }
  649. if (Focused is null)
  650. {
  651. FocusFirst ();
  652. return Focused != null;
  653. }
  654. int focusedIdx = -1;
  655. for (var i = 0; i < _tabIndexes.Count; i++)
  656. {
  657. View w = _tabIndexes [i];
  658. if (w.HasFocus)
  659. {
  660. if (w.FocusNext ())
  661. {
  662. return true;
  663. }
  664. focusedIdx = i;
  665. continue;
  666. }
  667. if (w.CanFocus && focusedIdx != -1 && w._tabStop && w.Visible && w.Enabled)
  668. {
  669. Focused.SetHasFocus (false, w);
  670. if (w.CanFocus && w._tabStop && w.Visible && w.Enabled)
  671. {
  672. w.FocusFirst ();
  673. }
  674. SetFocus (w);
  675. return true;
  676. }
  677. }
  678. if (Focused is { })
  679. {
  680. Focused.SetHasFocus (false, this);
  681. Focused = null;
  682. }
  683. return false;
  684. }
  685. private View GetMostFocused (View view)
  686. {
  687. if (view is null)
  688. {
  689. return null;
  690. }
  691. return view.Focused is { } ? GetMostFocused (view.Focused) : view;
  692. }
  693. /// <summary>Positions the cursor in the right position based on the currently focused view in the chain.</summary>
  694. /// Views that are focusable should override
  695. /// <see cref="PositionCursor"/>
  696. /// to ensure
  697. /// the cursor is placed in a location that makes sense. Unix terminals do not have
  698. /// a way of hiding the cursor, so it can be distracting to have the cursor left at
  699. /// the last focused view. Views should make sure that they place the cursor
  700. /// in a visually sensible place.
  701. public virtual void PositionCursor ()
  702. {
  703. if (!CanBeVisible (this) || !Enabled)
  704. {
  705. return;
  706. }
  707. // BUGBUG: v2 - This needs to support children of Frames too
  708. if (Focused is null && SuperView is { })
  709. {
  710. SuperView.EnsureFocus ();
  711. }
  712. else if (Focused?.Visible == true
  713. && Focused?.Enabled == true
  714. && Focused?.Frame.Width > 0
  715. && Focused.Frame.Height > 0)
  716. {
  717. Focused.PositionCursor ();
  718. }
  719. else if (Focused?.Visible == true && Focused?.Enabled == false)
  720. {
  721. Focused = null;
  722. }
  723. else if (CanFocus && HasFocus && Visible && Frame.Width > 0 && Frame.Height > 0)
  724. {
  725. Move (TextFormatter.HotKeyPos == -1 ? 0 : TextFormatter.CursorPosition, 0);
  726. }
  727. else
  728. {
  729. Move (_frame.X, _frame.Y);
  730. }
  731. }
  732. #endregion Focus
  733. }