ViewSubViews.cs 25 KB

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