ViewSubViews.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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>Method invoked when a view gets focus.</summary>
  413. /// <param name="view">The view that is losing focus.</param>
  414. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  415. public virtual bool OnEnter (View view)
  416. {
  417. var args = new FocusEventArgs (view);
  418. Enter?.Invoke (this, args);
  419. if (args.Handled)
  420. {
  421. return true;
  422. }
  423. return false;
  424. }
  425. /// <summary>Method invoked when a view loses focus.</summary>
  426. /// <param name="view">The view that is getting focus.</param>
  427. /// <returns><c>true</c>, if the event was handled, <c>false</c> otherwise.</returns>
  428. public virtual bool OnLeave (View view)
  429. {
  430. var args = new FocusEventArgs (view);
  431. Leave?.Invoke (this, args);
  432. if (args.Handled)
  433. {
  434. return true;
  435. }
  436. // BUGBUG: This is a hack to ensure that the cursor is hidden when the view loses focus.
  437. // BUGBUG: This is not needed as the minloop will take care of this.
  438. //Driver?.SetCursorVisibility (CursorVisibility.Invisible);
  439. return false;
  440. }
  441. /// <summary>Returns the currently focused view inside this view, or null if nothing is focused.</summary>
  442. /// <value>The focused.</value>
  443. public View Focused { get; private set; }
  444. /// <summary>Returns the most focused view in the chain of subviews (the leaf view that has the focus).</summary>
  445. /// <value>The most focused View.</value>
  446. public View MostFocused
  447. {
  448. get
  449. {
  450. if (Focused is null)
  451. {
  452. return null;
  453. }
  454. View most = Focused.MostFocused;
  455. if (most is { })
  456. {
  457. return most;
  458. }
  459. return Focused;
  460. }
  461. }
  462. /// <summary>Causes the specified subview to have focus.</summary>
  463. /// <param name="view">View.</param>
  464. private void SetFocus (View view)
  465. {
  466. if (view is null)
  467. {
  468. return;
  469. }
  470. //Console.WriteLine ($"Request to focus {view}");
  471. if (!view.CanFocus || !view.Visible || !view.Enabled)
  472. {
  473. return;
  474. }
  475. if (Focused?._hasFocus == true && Focused == view)
  476. {
  477. return;
  478. }
  479. if ((Focused?._hasFocus == true && Focused?.SuperView == view) || view == this)
  480. {
  481. if (!view._hasFocus)
  482. {
  483. view._hasFocus = true;
  484. }
  485. return;
  486. }
  487. // Make sure that this view is a subview
  488. View c;
  489. for (c = view._superView; c != null; c = c._superView)
  490. {
  491. if (c == this)
  492. {
  493. break;
  494. }
  495. }
  496. if (c is null)
  497. {
  498. throw new ArgumentException ("the specified view is not part of the hierarchy of this view");
  499. }
  500. if (Focused is { })
  501. {
  502. Focused.SetHasFocus (false, view);
  503. }
  504. View f = Focused;
  505. Focused = view;
  506. Focused.SetHasFocus (true, f);
  507. Focused.EnsureFocus ();
  508. // Send focus upwards
  509. if (SuperView is { })
  510. {
  511. SuperView.SetFocus (this);
  512. }
  513. else
  514. {
  515. SetFocus (this);
  516. }
  517. }
  518. /// <summary>Causes the specified view and the entire parent hierarchy to have the focused order updated.</summary>
  519. public void SetFocus ()
  520. {
  521. if (!CanBeVisible (this) || !Enabled)
  522. {
  523. if (HasFocus)
  524. {
  525. SetHasFocus (false, this);
  526. }
  527. return;
  528. }
  529. if (SuperView is { })
  530. {
  531. SuperView.SetFocus (this);
  532. }
  533. else
  534. {
  535. SetFocus (this);
  536. }
  537. }
  538. /// <summary>
  539. /// Finds the first view in the hierarchy that wants to get the focus if nothing is currently focused, otherwise,
  540. /// does nothing.
  541. /// </summary>
  542. public void EnsureFocus ()
  543. {
  544. if (Focused is null && _subviews?.Count > 0)
  545. {
  546. if (FocusDirection == NavigationDirection.Forward)
  547. {
  548. FocusFirst ();
  549. }
  550. else
  551. {
  552. FocusLast ();
  553. }
  554. }
  555. }
  556. /// <summary>Focuses the first focusable subview if one exists.</summary>
  557. public void FocusFirst ()
  558. {
  559. if (!CanBeVisible (this))
  560. {
  561. return;
  562. }
  563. if (_tabIndexes is null)
  564. {
  565. SuperView?.SetFocus (this);
  566. return;
  567. }
  568. foreach (View view in _tabIndexes)
  569. {
  570. if (view.CanFocus && view._tabStop && view.Visible && view.Enabled)
  571. {
  572. SetFocus (view);
  573. return;
  574. }
  575. }
  576. }
  577. /// <summary>Focuses the last focusable subview if one exists.</summary>
  578. public void FocusLast ()
  579. {
  580. if (!CanBeVisible (this))
  581. {
  582. return;
  583. }
  584. if (_tabIndexes is null)
  585. {
  586. SuperView?.SetFocus (this);
  587. return;
  588. }
  589. for (int i = _tabIndexes.Count; i > 0;)
  590. {
  591. i--;
  592. View v = _tabIndexes [i];
  593. if (v.CanFocus && v._tabStop && v.Visible && v.Enabled)
  594. {
  595. SetFocus (v);
  596. return;
  597. }
  598. }
  599. }
  600. /// <summary>Focuses the previous view.</summary>
  601. /// <returns><see langword="true"/> if previous was focused, <see langword="false"/> otherwise.</returns>
  602. public bool FocusPrev ()
  603. {
  604. if (!CanBeVisible (this))
  605. {
  606. return false;
  607. }
  608. FocusDirection = NavigationDirection.Backward;
  609. if (_tabIndexes is null || _tabIndexes.Count == 0)
  610. {
  611. return false;
  612. }
  613. if (Focused is null)
  614. {
  615. FocusLast ();
  616. return Focused != null;
  617. }
  618. int focusedIdx = -1;
  619. for (int i = _tabIndexes.Count; i > 0;)
  620. {
  621. i--;
  622. View w = _tabIndexes [i];
  623. if (w.HasFocus)
  624. {
  625. if (w.FocusPrev ())
  626. {
  627. return true;
  628. }
  629. focusedIdx = i;
  630. continue;
  631. }
  632. if (w.CanFocus && focusedIdx != -1 && w._tabStop && w.Visible && w.Enabled)
  633. {
  634. Focused.SetHasFocus (false, w);
  635. if (w.CanFocus && w._tabStop && w.Visible && w.Enabled)
  636. {
  637. w.FocusLast ();
  638. }
  639. SetFocus (w);
  640. return true;
  641. }
  642. }
  643. if (Focused is { })
  644. {
  645. Focused.SetHasFocus (false, this);
  646. Focused = null;
  647. }
  648. return false;
  649. }
  650. /// <summary>Focuses the next view.</summary>
  651. /// <returns><see langword="true"/> if next was focused, <see langword="false"/> otherwise.</returns>
  652. public bool FocusNext ()
  653. {
  654. if (!CanBeVisible (this))
  655. {
  656. return false;
  657. }
  658. FocusDirection = NavigationDirection.Forward;
  659. if (_tabIndexes is null || _tabIndexes.Count == 0)
  660. {
  661. return false;
  662. }
  663. if (Focused is null)
  664. {
  665. FocusFirst ();
  666. return Focused != null;
  667. }
  668. int focusedIdx = -1;
  669. for (var i = 0; i < _tabIndexes.Count; i++)
  670. {
  671. View w = _tabIndexes [i];
  672. if (w.HasFocus)
  673. {
  674. if (w.FocusNext ())
  675. {
  676. return true;
  677. }
  678. focusedIdx = i;
  679. continue;
  680. }
  681. if (w.CanFocus && focusedIdx != -1 && w._tabStop && w.Visible && w.Enabled)
  682. {
  683. Focused.SetHasFocus (false, w);
  684. if (w.CanFocus && w._tabStop && w.Visible && w.Enabled)
  685. {
  686. w.FocusFirst ();
  687. }
  688. SetFocus (w);
  689. return true;
  690. }
  691. }
  692. if (Focused is { })
  693. {
  694. Focused.SetHasFocus (false, this);
  695. Focused = null;
  696. }
  697. return false;
  698. }
  699. private View GetMostFocused (View view)
  700. {
  701. if (view is null)
  702. {
  703. return null;
  704. }
  705. return view.Focused is { } ? GetMostFocused (view.Focused) : view;
  706. }
  707. /// <summary>
  708. /// Positions the cursor in the right position based on the currently focused view in the chain.
  709. /// </summary>
  710. /// <remarks>
  711. /// <para>
  712. /// Views that are focusable and want the cursor visible should override <see cref="PositionCursor"/>,
  713. /// use <see cref="Move"/> to place the cursor in a location that makes sense, use
  714. /// <see cref="ConsoleDriver.SetCursorVisibility"/>
  715. /// to make the cursor visible, and return the position where the cursor was placed.
  716. /// </para>
  717. /// <para>
  718. /// Unix terminals do not have a way of hiding the cursor, so it can be distracting to have the cursor left at
  719. /// the last focused view. Views should make sure that they place the cursor in a visually sensible place.
  720. /// </para>
  721. /// </remarks>
  722. /// <returns>Viewport-relative cursor position. Return <see langword="null"/> to ensure the cursor is not visible.</returns>
  723. public virtual Point? PositionCursor ()
  724. {
  725. if (CanFocus && HasFocus && ContentSize.HasValue)
  726. {
  727. // Base class will position the cursor at the end of the text.
  728. Point location = Viewport.Location;
  729. location.X = TextFormatter.HotKeyPos == -1 ? 0 : TextFormatter.CursorPosition;
  730. location.Y = 0;
  731. Move (location.X, location.Y);
  732. }
  733. // Returning null will hide the cursor.
  734. return null;
  735. }
  736. #endregion Focus
  737. }