View.Navigation.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. public partial class View // Focus and cross-view navigation management (TabStop, TabIndex, etc...)
  5. {
  6. private bool _canFocus;
  7. /// <summary>
  8. /// Advances the focus to the next or previous view in the focus chain, based on
  9. /// <paramref name="direction"/>.
  10. /// itself.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// If there is no next/previous view, the focus is set to the view itself.
  15. /// </para>
  16. /// </remarks>
  17. /// <param name="direction"></param>
  18. /// <param name="behavior"></param>
  19. /// <returns>
  20. /// <see langword="true"/> if focus was changed to another subview (or stayed on this one), <see langword="false"/>
  21. /// otherwise.
  22. /// </returns>
  23. public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior)
  24. {
  25. if (!CanBeVisible (this)) // TODO: is this check needed?
  26. {
  27. return false;
  28. }
  29. View? focused = Focused;
  30. if (focused is { } && focused.AdvanceFocus (direction, behavior))
  31. {
  32. return true;
  33. }
  34. // AdvanceFocus did not advance
  35. View [] index = GetSubviewFocusChain (direction, behavior);
  36. if (index.Length == 0)
  37. {
  38. return false;
  39. }
  40. if (behavior == TabBehavior.TabGroup)
  41. {
  42. if (direction == NavigationDirection.Forward && focused == index [^1] && SuperView is null)
  43. {
  44. // We're at the top of the focus chain. Go back down the focus chain and focus the first TabGroup
  45. View [] views = GetSubviewFocusChain (NavigationDirection.Forward, TabBehavior.TabGroup);
  46. if (views.Length > 0)
  47. {
  48. View [] subViews = views [0].GetSubviewFocusChain (NavigationDirection.Forward, TabBehavior.TabStop);
  49. if (subViews.Length > 0)
  50. {
  51. if (subViews [0].SetFocus ())
  52. {
  53. return true;
  54. }
  55. }
  56. }
  57. }
  58. if (direction == NavigationDirection.Backward && focused == index [0])
  59. {
  60. // We're at the bottom of the focus chain
  61. View [] views = GetSubviewFocusChain (NavigationDirection.Forward, TabBehavior.TabGroup);
  62. if (views.Length > 0)
  63. {
  64. View [] subViews = views [^1].GetSubviewFocusChain (NavigationDirection.Forward, TabBehavior.TabStop);
  65. if (subViews.Length > 0)
  66. {
  67. if (subViews [0].SetFocus ())
  68. {
  69. return true;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. int focusedIndex = index.IndexOf (Focused); // Will return -1 if Focused can't be found or is null
  76. int next = 0;
  77. if (focusedIndex < index.Length - 1)
  78. {
  79. // We're moving w/in the subviews
  80. next = focusedIndex + 1;
  81. }
  82. else
  83. {
  84. // We're moving beyond the last subview
  85. // Determine if focus should remain in this focus chain, or move to the superview's focus chain
  86. // If we are TabStop and our SuperView has at least one other TabStop subview, move to the SuperView's chain
  87. if (TabStop == TabBehavior.TabStop && SuperView is { } && SuperView.GetSubviewFocusChain (direction, behavior).Length > 1)
  88. {
  89. return false;
  90. }
  91. // TabGroup is special-cased.
  92. if (focused?.TabStop == TabBehavior.TabGroup)
  93. {
  94. if (SuperView?.GetSubviewFocusChain (direction, TabBehavior.TabGroup)?.Length > 0)
  95. {
  96. // Our superview has a TabGroup subview; signal we couldn't move so we nav out to it
  97. return false;
  98. }
  99. }
  100. }
  101. View view = index [next];
  102. if (view.HasFocus)
  103. {
  104. // We could not advance
  105. return view == this;
  106. }
  107. // The subview does not have focus, but at least one other that can. Can this one be focused?
  108. (bool focusSet, bool _) = view.SetHasFocusTrue (Focused);
  109. return focusSet;
  110. }
  111. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> can be focused.</summary>
  112. /// <remarks>
  113. /// <para>
  114. /// <see cref="SuperView"/> must also have <see cref="CanFocus"/> set to <see langword="true"/>.
  115. /// </para>
  116. /// <para>
  117. /// When set to <see langword="false"/>, if an attempt is made to make this view focused, the focus will be set to
  118. /// the next focusable view.
  119. /// </para>
  120. /// <para>
  121. /// When set to <see langword="false"/>, the value of <see cref="CanFocus"/> for all
  122. /// subviews will be cached so that when <see cref="CanFocus"/> is set back to <see langword="true"/>, the subviews
  123. /// will be restored to their previous values.
  124. /// </para>
  125. /// <para>
  126. /// Changing this property to <see langword="true"/> will cause <see cref="TabStop"/> to be set to
  127. /// <see cref="TabBehavior.TabStop"/>" as a convenience. Changing this property to
  128. /// <see langword="false"/> will have no effect on <see cref="TabStop"/>.
  129. /// </para>
  130. /// </remarks>
  131. public bool CanFocus
  132. {
  133. get => _canFocus;
  134. set
  135. {
  136. if (_canFocus == value)
  137. {
  138. return;
  139. }
  140. _canFocus = value;
  141. if (TabStop is null && _canFocus)
  142. {
  143. TabStop = TabBehavior.TabStop;
  144. }
  145. if (!_canFocus && HasFocus)
  146. {
  147. // If CanFocus is set to false and this view has focus, make it leave focus
  148. HasFocus = false;
  149. }
  150. if (_canFocus && !HasFocus && Visible && SuperView is { } && SuperView.Focused is null)
  151. {
  152. // If CanFocus is set to true and this view does not have focus, make it enter focus
  153. SetFocus ();
  154. }
  155. OnCanFocusChanged ();
  156. }
  157. }
  158. /// <summary>Raised when <see cref="CanFocus"/> has been changed.</summary>
  159. /// <remarks>
  160. /// Raised by the <see cref="OnCanFocusChanged"/> virtual method.
  161. /// </remarks>
  162. public event EventHandler? CanFocusChanged;
  163. /// <summary>
  164. /// Focuses the deepest focusable Subview if one exists. If there are no focusable Subviews then the focus is set to
  165. /// the view itself.
  166. /// </summary>
  167. /// <param name="direction"></param>
  168. /// <param name="behavior"></param>
  169. /// <returns><see langword="true"/> if a subview other than this was focused.</returns>
  170. public bool FocusDeepest (NavigationDirection direction, TabBehavior? behavior)
  171. {
  172. View? deepest = FindDeepestFocusableView (direction, behavior);
  173. if (deepest is { })
  174. {
  175. return deepest.SetFocus ();
  176. }
  177. return SetFocus ();
  178. }
  179. /// <summary>Gets the currently focused Subview of this view, or <see langword="null"/> if nothing is focused.</summary>
  180. public View? Focused
  181. {
  182. get { return Subviews.FirstOrDefault (v => v.HasFocus); }
  183. }
  184. /// <summary>Returns a value indicating if this View is currently on Top (Active)</summary>
  185. public bool IsCurrentTop => Application.Current == this;
  186. /// <summary>
  187. /// Returns the most focused Subview down the subview-hierarchy.
  188. /// </summary>
  189. /// <value>The most focused Subview, or <see langword="null"/> if no Subview is focused.</value>
  190. public View? MostFocused
  191. {
  192. get
  193. {
  194. // TODO: Remove this API. It's duplicative of Application.Navigation.GetFocused.
  195. if (Focused is null)
  196. {
  197. return null;
  198. }
  199. View? most = Focused.MostFocused;
  200. if (most is { })
  201. {
  202. return most;
  203. }
  204. return Focused;
  205. }
  206. }
  207. /// <summary>Invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  208. /// <remarks>
  209. /// Raises the <see cref="CanFocusChanged"/> event.
  210. /// </remarks>
  211. public virtual void OnCanFocusChanged () { CanFocusChanged?.Invoke (this, EventArgs.Empty); }
  212. /// <summary>
  213. /// INTERNAL API to restore focus to the subview that had focus before this view lost focus.
  214. /// </summary>
  215. /// <returns>
  216. /// Returns true if focus was restored to a subview, false otherwise.
  217. /// </returns>
  218. internal bool RestoreFocus ()
  219. {
  220. if (Focused is null && _subviews?.Count > 0)
  221. {
  222. if (_previouslyMostFocused is { } /* && (behavior is null || _previouslyMostFocused.TabStop == behavior)*/)
  223. {
  224. return _previouslyMostFocused.SetFocus ();
  225. }
  226. return false;
  227. }
  228. return false;
  229. }
  230. private View? FindDeepestFocusableView (NavigationDirection direction, TabBehavior? behavior)
  231. {
  232. View [] indicies = GetSubviewFocusChain (direction, behavior);
  233. foreach (View v in indicies)
  234. {
  235. return v.FindDeepestFocusableView (direction, behavior);
  236. }
  237. return null;
  238. }
  239. #region HasFocus
  240. // Backs `HasFocus` and is the ultimate source of truth whether a View has focus or not.
  241. private bool _hasFocus;
  242. /// <summary>
  243. /// Gets or sets whether this view has focus.
  244. /// </summary>
  245. /// <remarks>
  246. /// <para>
  247. /// Only Views that are visible, enabled, and have <see cref="CanFocus"/> set to <see langword="true"/> are
  248. /// focusable. If
  249. /// these conditions are not met when this property is set to <see langword="true"/> <see cref="HasFocus"/> will
  250. /// not change.
  251. /// </para>
  252. /// <para>
  253. /// Setting this property causes the <see cref="OnHasFocusChanging"/> and <see cref="OnHasFocusChanged"/> virtual
  254. /// methods (and <see cref="HasFocusChanging"/> and
  255. /// <see cref="HasFocusChanged"/> events to be raised). If the event is cancelled, <see cref="HasFocus"/> will not
  256. /// be changed.
  257. /// </para>
  258. /// <para>
  259. /// Setting this property to <see langword="true"/> will recursively set <see cref="HasFocus"/> to
  260. /// <see langword="true"/> for all SuperViews up the hierarchy.
  261. /// </para>
  262. /// <para>
  263. /// Setting this property to <see langword="true"/> will cause the subview furthest down the hierarchy that is
  264. /// focusable to also gain focus (as long as <see cref="TabStop"/>
  265. /// </para>
  266. /// <para>
  267. /// Setting this property to <see langword="false"/> will cause <see cref="AdvanceFocus"/> to set
  268. /// the focus on the next view to be focused.
  269. /// </para>
  270. /// </remarks>
  271. public bool HasFocus
  272. {
  273. set
  274. {
  275. if (HasFocus == value)
  276. {
  277. return;
  278. }
  279. if (value)
  280. {
  281. // NOTE: If Application.Navigation is null, we pass null to FocusChanging. For unit tests.
  282. (bool focusSet, bool _) = SetHasFocusTrue (Application.Navigation?.GetFocused ());
  283. if (focusSet)
  284. {
  285. // The change happened
  286. // HasFocus is now true
  287. }
  288. }
  289. else
  290. {
  291. SetHasFocusFalse (null);
  292. }
  293. }
  294. get => _hasFocus;
  295. }
  296. /// <summary>
  297. /// Causes this view to be focused. Calling this method has the same effect as setting <see cref="HasFocus"/> to
  298. /// <see langword="true"/> but with the added benefit of returning a value indicating whether the focus was set.
  299. /// </summary>
  300. public bool SetFocus ()
  301. {
  302. (bool focusSet, bool _) = SetHasFocusTrue (Application.Navigation?.GetFocused ());
  303. return focusSet;
  304. }
  305. /// <summary>
  306. /// INTERNAL: Called when focus is going to change to this view. This method is called by <see cref="SetFocus"/> and
  307. /// other methods that
  308. /// set or remove focus from a view.
  309. /// </summary>
  310. /// <param name="previousFocusedView">
  311. /// The previously focused view. If <see langword="null"/> there is no previously focused
  312. /// view.
  313. /// </param>
  314. /// <param name="traversingUp"></param>
  315. /// <returns><see langword="true"/> if <see cref="HasFocus"/> was changed to <see langword="true"/>.</returns>
  316. /// <exception cref="InvalidOperationException"></exception>
  317. private (bool focusSet, bool cancelled) SetHasFocusTrue (View? previousFocusedView, bool traversingUp = false)
  318. {
  319. Debug.Assert (ApplicationNavigation.IsInHierarchy (SuperView, this));
  320. // Pre-conditions
  321. if (_hasFocus)
  322. {
  323. return (false, false);
  324. }
  325. if (CanFocus && SuperView is { CanFocus: false })
  326. {
  327. Debug.WriteLine ($@"WARNING: Attempt to FocusChanging where SuperView.CanFocus == false. {this}");
  328. return (false, false);
  329. }
  330. if (!CanBeVisible (this) || !Enabled)
  331. {
  332. return (false, false);
  333. }
  334. if (!CanFocus)
  335. {
  336. return (false, false);
  337. }
  338. bool previousValue = HasFocus;
  339. bool cancelled = NotifyFocusChanging (false, true, previousFocusedView, this);
  340. if (cancelled)
  341. {
  342. return (false, true);
  343. }
  344. // Make sure superviews up the superview hierarchy have focus.
  345. // Any of them may cancel gaining focus. In which case we need to back out.
  346. if (SuperView is { HasFocus: false } sv)
  347. {
  348. (bool focusSet, bool svCancelled) = sv.SetHasFocusTrue (previousFocusedView, true);
  349. if (!focusSet)
  350. {
  351. return (false, svCancelled);
  352. }
  353. }
  354. if (_hasFocus)
  355. {
  356. // Something else beat us to the change (likely a FocusChanged handler).
  357. return (true, false);
  358. }
  359. // By setting _hasFocus to true we definitively change HasFocus for this view.
  360. // Get whatever peer has focus, if any
  361. View? focusedPeer = SuperView?.Focused;
  362. _hasFocus = true;
  363. // Ensure that the peer loses focus
  364. focusedPeer?.SetHasFocusFalse (this, true);
  365. if (!traversingUp)
  366. {
  367. // Restore focus to the previously most focused subview in the subview-hierarchy
  368. if (!RestoreFocus ())
  369. {
  370. // Couldn't restore focus, so use Advance to navigate to the next focusable subview
  371. if (!AdvanceFocus (NavigationDirection.Forward, null))
  372. {
  373. // Couldn't advance, so we're the most focused view in the application
  374. _previouslyMostFocused = null;
  375. Application.Navigation?.SetFocused (this);
  376. }
  377. }
  378. }
  379. if (previousFocusedView is { HasFocus: true } && Subviews.Contains (previousFocusedView))
  380. {
  381. previousFocusedView.SetHasFocusFalse (this);
  382. }
  383. if (Arrangement.HasFlag (ViewArrangement.Overlapped))
  384. {
  385. SuperView?.MoveSubviewToStart (this);
  386. }
  387. NotifyFocusChanged (HasFocus, previousFocusedView, this);
  388. // Post-conditions - prove correctness
  389. if (HasFocus == previousValue)
  390. {
  391. throw new InvalidOperationException ("NotifyFocusChanging was not cancelled and the HasFocus value did not change.");
  392. }
  393. return (true, false);
  394. }
  395. private bool NotifyFocusChanging (bool currentHasFocus, bool newHasFocus, View? currentFocused, View? newFocused)
  396. {
  397. // Call the virtual method
  398. if (OnHasFocusChanging (currentHasFocus, newHasFocus, currentFocused, newFocused))
  399. {
  400. // The event was cancelled
  401. return true;
  402. }
  403. var args = new HasFocusEventArgs (currentHasFocus, newHasFocus, currentFocused, newFocused);
  404. HasFocusChanging?.Invoke (this, args);
  405. if (args.Cancel)
  406. {
  407. // The event was cancelled
  408. return true;
  409. }
  410. return false;
  411. }
  412. /// <summary>
  413. /// Invoked when <see cref="View.HasFocus"/> is about to change. This method is called before the
  414. /// <see cref="HasFocusChanging"/> event is raised.
  415. /// </summary>
  416. /// <remarks>
  417. /// <para>
  418. /// Use <see cref="OnHasFocusChanged"/> to be notified after the focus has changed.
  419. /// </para>
  420. /// </remarks>
  421. /// <param name="currentHasFocus">The current value of <see cref="View.HasFocus"/>.</param>
  422. /// <param name="newHasFocus">The value <see cref="View.HasFocus"/> will have if the focus change happens.</param>
  423. /// <param name="currentFocused">The view that is currently Focused. May be <see langword="null"/>.</param>
  424. /// <param name="newFocused">The view that will be focused. May be <see langword="null"/>.</param>
  425. /// <returns>
  426. /// <see langword="true"/>, if the change to <see cref="View.HasFocus"/> is to be cancelled, <see langword="false"/>
  427. /// otherwise.
  428. /// </returns>
  429. protected virtual bool OnHasFocusChanging (bool currentHasFocus, bool newHasFocus, View? currentFocused, View? newFocused) { return false; }
  430. /// <summary>
  431. /// Raised when <see cref="View.HasFocus"/> is about to change.
  432. /// </summary>
  433. /// <remarks>
  434. /// <para>
  435. /// Cancel the event to prevent the focus from changing.
  436. /// </para>
  437. /// <para>
  438. /// Use <see cref="HasFocusChanged"/> to be notified after the focus has changed.
  439. /// </para>
  440. /// </remarks>
  441. public event EventHandler<HasFocusEventArgs>? HasFocusChanging;
  442. /// <summary>
  443. /// Called when this view should stop being focused.
  444. /// </summary>
  445. /// <param name="newFocusedView">
  446. /// The new focused view. If <see langword="null"/> it is not known which view will be
  447. /// focused.
  448. /// </param>
  449. /// <param name="traversingDown">
  450. /// Set to true to indicate method is being called recurively, traversing down the focus
  451. /// chain.
  452. /// </param>
  453. /// <exception cref="InvalidOperationException"></exception>
  454. private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false)
  455. {
  456. // Pre-conditions
  457. if (!_hasFocus)
  458. {
  459. throw new InvalidOperationException ("SetHasFocusFalse should not be called if the view does not have focus.");
  460. }
  461. // If newFocusedVew is null, we need to find the view that should get focus, and SetFocus on it.
  462. if (!traversingDown && newFocusedView is null)
  463. {
  464. if (SuperView?._previouslyMostFocused is { } && SuperView?._previouslyMostFocused != this)
  465. {
  466. SuperView?._previouslyMostFocused?.SetFocus ();
  467. // The above will cause SetHasFocusFalse, so we can return
  468. return;
  469. }
  470. if (SuperView is { } && SuperView.AdvanceFocus (NavigationDirection.Forward, TabStop))
  471. {
  472. // The above will cause SetHasFocusFalse, so we can return
  473. return;
  474. }
  475. if (Application.Navigation is { } && Application.Current is { })
  476. {
  477. // Temporarily ensure this view can't get focus
  478. bool prevCanFocus = _canFocus;
  479. _canFocus = false;
  480. bool restoredFocus = Application.Current!.RestoreFocus ();
  481. _canFocus = prevCanFocus;
  482. if (restoredFocus)
  483. {
  484. // The above caused SetHasFocusFalse, so we can return
  485. return;
  486. }
  487. }
  488. // No other focusable view to be found. Just "leave" us...
  489. }
  490. // Before we can leave focus, we need to make sure that all views down the subview-hierarchy have left focus.
  491. View? mostFocused = MostFocused;
  492. if (mostFocused is { } && (newFocusedView is null || mostFocused != newFocusedView))
  493. {
  494. // Start at the bottom and work our way up to us
  495. View? bottom = mostFocused;
  496. while (bottom is { } && bottom != this)
  497. {
  498. if (bottom.HasFocus)
  499. {
  500. bottom.SetHasFocusFalse (newFocusedView, true);
  501. }
  502. bottom = bottom.SuperView;
  503. }
  504. _previouslyMostFocused = mostFocused;
  505. }
  506. bool previousValue = HasFocus;
  507. // Note, can't be cancelled.
  508. NotifyFocusChanging (HasFocus, !HasFocus, newFocusedView, this);
  509. // Get whatever peer has focus, if any
  510. View? focusedPeer = SuperView?.Focused;
  511. _hasFocus = false;
  512. if (Application.Navigation is { })
  513. {
  514. View? appFocused = Application.Navigation.GetFocused ();
  515. if (appFocused is { } || appFocused == this)
  516. {
  517. Application.Navigation.SetFocused (newFocusedView ?? SuperView);
  518. }
  519. }
  520. NotifyFocusChanged (HasFocus, this, newFocusedView);
  521. if (_hasFocus)
  522. {
  523. // Notify caused HasFocus to change to true.
  524. return;
  525. }
  526. if (SuperView is { })
  527. {
  528. SuperView._previouslyMostFocused = focusedPeer;
  529. }
  530. // Post-conditions - prove correctness
  531. if (HasFocus == previousValue)
  532. {
  533. throw new InvalidOperationException ("SetHasFocusFalse and the HasFocus value did not change.");
  534. }
  535. SetNeedsDisplay ();
  536. }
  537. /// <summary>
  538. /// Caches the most focused subview when this view is losing focus. This is used by <see cref="RestoreFocus"/>.
  539. /// </summary>
  540. private View? _previouslyMostFocused;
  541. private void NotifyFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedVew)
  542. {
  543. // Call the virtual method
  544. OnHasFocusChanged (newHasFocus, previousFocusedView, focusedVew);
  545. // Raise the event
  546. var args = new HasFocusEventArgs (newHasFocus, newHasFocus, previousFocusedView, focusedVew);
  547. HasFocusChanged?.Invoke (this, args);
  548. }
  549. /// <summary>
  550. /// Invoked after <see cref="HasFocus"/> has changed. This method is called before the <see cref="HasFocusChanged"/>
  551. /// event is raised.
  552. /// </summary>
  553. /// <remarks>
  554. /// <para>
  555. /// This event cannot be cancelled.
  556. /// </para>
  557. /// </remarks>
  558. /// <param name="newHasFocus">The new value of <see cref="View.HasFocus"/>.</param>
  559. /// <param name="previousFocusedView"></param>
  560. /// <param name="focusedVew">The view that is now focused. May be <see langword="null"/></param>
  561. protected virtual void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedVew) { }
  562. /// <summary>Raised after <see cref="HasFocus"/> has changed.</summary>
  563. /// <remarks>
  564. /// <para>
  565. /// This event cannot be cancelled.
  566. /// </para>
  567. /// </remarks>
  568. public event EventHandler<HasFocusEventArgs>? HasFocusChanged;
  569. #endregion HasFocus
  570. #region Tab/Focus Handling
  571. /// <summary>
  572. /// Gets TabIndexes that are scoped to the specified behavior and direction. If behavior is null, all TabIndexes are
  573. /// returned.
  574. /// </summary>
  575. /// <param name="direction"></param>
  576. /// <param name="behavior"></param>
  577. /// <returns></returns>
  578. /// GetScopedTabIndexes
  579. internal View [] GetSubviewFocusChain (NavigationDirection direction, TabBehavior? behavior)
  580. {
  581. IEnumerable<View>? fitleredSubviews;
  582. if (behavior.HasValue)
  583. {
  584. fitleredSubviews = _subviews?.Where (v => v.TabStop == behavior && v is { CanFocus: true, Visible: true, Enabled: true });
  585. }
  586. else
  587. {
  588. fitleredSubviews = _subviews?.Where (v => v is { CanFocus: true, Visible: true, Enabled: true });
  589. }
  590. if (direction == NavigationDirection.Backward)
  591. {
  592. fitleredSubviews = fitleredSubviews?.Reverse ();
  593. }
  594. return fitleredSubviews?.ToArray () ?? Array.Empty<View> ();
  595. }
  596. private TabBehavior? _tabStop;
  597. /// <summary>
  598. /// Gets or sets the behavior of <see cref="AdvanceFocus"/> for keyboard navigation.
  599. /// </summary>
  600. /// <remarks>
  601. /// <para>
  602. /// If <see langword="null"/> the tab stop has not been set and setting <see cref="CanFocus"/> to true will set it
  603. /// to
  604. /// <see cref="TabBehavior.TabStop"/>.
  605. /// </para>
  606. /// <para>
  607. /// TabStop is independent of <see cref="CanFocus"/>. If <see cref="CanFocus"/> is <see langword="false"/>, the
  608. /// view will not gain
  609. /// focus even if this property is set and vice-versa.
  610. /// </para>
  611. /// <para>
  612. /// The default <see cref="TabBehavior.TabStop"/> keys are <see cref="Application.NextTabKey"/> (<c>Key.Tab</c>)
  613. /// and <see cref="Application.PrevTabKey"/> (<c>Key>Tab.WithShift</c>).
  614. /// </para>
  615. /// <para>
  616. /// The default <see cref="TabBehavior.TabGroup"/> keys are <see cref="Application.NextTabGroupKey"/> (
  617. /// <c>Key.F6</c>) and <see cref="Application.PrevTabGroupKey"/> (<c>Key>Key.F6.WithShift</c>).
  618. /// </para>
  619. /// </remarks>
  620. public TabBehavior? TabStop
  621. {
  622. get => _tabStop;
  623. set
  624. {
  625. if (_tabStop is { } && _tabStop == value)
  626. {
  627. return;
  628. }
  629. _tabStop = value;
  630. }
  631. }
  632. #endregion Tab/Focus Handling
  633. }