ViewSubViews.cs 25 KB

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