ViewSubViews.cs 26 KB

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