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