ViewSubViews.cs 25 KB

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