ViewSubViews.cs 25 KB

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