ViewSubViews.cs 24 KB

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