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