ViewSubViews.cs 24 KB

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