ViewSubViews.cs 25 KB

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