ViewSubViews.cs 25 KB

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