View.Navigation.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. var 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 the view itself.
  165. /// </summary>
  166. /// <param name="direction"></param>
  167. /// <param name="behavior"></param>
  168. /// <returns><see langword="true"/> if a subview other than this was focused.</returns>
  169. public bool FocusDeepest (NavigationDirection direction, TabBehavior? behavior)
  170. {
  171. View? deepest = FindDeepestFocusableView (direction, behavior);
  172. if (deepest is { })
  173. {
  174. return deepest.SetFocus ();
  175. }
  176. return SetFocus ();
  177. }
  178. /// <summary>Gets the currently focused Subview of this view, or <see langword="null"/> if nothing is focused.</summary>
  179. public View? Focused
  180. {
  181. get { return Subviews.FirstOrDefault (v => v.HasFocus); }
  182. }
  183. /// <summary>Returns a value indicating if this View is currently on Top (Active)</summary>
  184. public bool IsCurrentTop => Application.Current == this;
  185. /// <summary>
  186. /// Returns the most focused Subview down the subview-hierarchy.
  187. /// </summary>
  188. /// <value>The most focused Subview, or <see langword="null"/> if no Subview is focused.</value>
  189. public View? MostFocused
  190. {
  191. get
  192. {
  193. // TODO: Remove this API. It's duplicative of Application.Navigation.GetFocused.
  194. if (Focused is null)
  195. {
  196. return null;
  197. }
  198. View? most = Focused.MostFocused;
  199. if (most is { })
  200. {
  201. return most;
  202. }
  203. return Focused;
  204. }
  205. }
  206. /// <summary>Invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  207. /// <remarks>
  208. /// Raises the <see cref="CanFocusChanged"/> event.
  209. /// </remarks>
  210. public virtual void OnCanFocusChanged () { CanFocusChanged?.Invoke (this, EventArgs.Empty); }
  211. /// <summary>
  212. /// INTERNAL API to restore focus to the subview that had focus before this view lost focus.
  213. /// </summary>
  214. /// <returns>
  215. /// Returns true if focus was restored to a subview, false otherwise.
  216. /// </returns>
  217. internal bool RestoreFocus (TabBehavior? behavior)
  218. {
  219. if (Focused is null && _subviews?.Count > 0)
  220. {
  221. if (_previouslyMostFocused is { } /* && (behavior is null || _previouslyMostFocused.TabStop == behavior)*/)
  222. {
  223. return _previouslyMostFocused.SetFocus ();
  224. }
  225. return false;
  226. }
  227. return false;
  228. }
  229. private View? FindDeepestFocusableView (NavigationDirection direction, TabBehavior? behavior)
  230. {
  231. View [] indicies = GetSubviewFocusChain (direction, behavior);
  232. foreach (View v in indicies)
  233. {
  234. return v.FindDeepestFocusableView (direction, behavior);
  235. }
  236. return null;
  237. }
  238. #region HasFocus
  239. // Backs `HasFocus` and is the ultimate source of truth whether a View has focus or not.
  240. private bool _hasFocus;
  241. /// <summary>
  242. /// Gets or sets whether this view has focus.
  243. /// </summary>
  244. /// <remarks>
  245. /// <para>
  246. /// Only Views that are visible, enabled, and have <see cref="CanFocus"/> set to <see langword="true"/> are
  247. /// focusable. If
  248. /// these conditions are not met when this property is set to <see langword="true"/> <see cref="HasFocus"/> will
  249. /// not change.
  250. /// </para>
  251. /// <para>
  252. /// Setting this property causes the <see cref="OnHasFocusChanging"/> and <see cref="OnHasFocusChanged"/> virtual
  253. /// methods (and <see cref="HasFocusChanging"/> and
  254. /// <see cref="HasFocusChanged"/> events to be raised). If the event is cancelled, <see cref="HasFocus"/> will not
  255. /// be changed.
  256. /// </para>
  257. /// <para>
  258. /// Setting this property to <see langword="true"/> will recursively set <see cref="HasFocus"/> to
  259. /// <see langword="true"/> for all SuperViews up the hierarchy.
  260. /// </para>
  261. /// <para>
  262. /// Setting this property to <see langword="true"/> will cause the subview furthest down the hierarchy that is
  263. /// focusable to also gain focus (as long as <see cref="TabStop"/>
  264. /// </para>
  265. /// <para>
  266. /// Setting this property to <see langword="false"/> will cause <see cref="AdvanceFocus"/> to set
  267. /// the focus on the next view to be focused.
  268. /// </para>
  269. /// </remarks>
  270. public bool HasFocus
  271. {
  272. set
  273. {
  274. if (HasFocus != value)
  275. {
  276. if (value)
  277. {
  278. // NOTE: If Application.Navigation is null, we pass null to FocusChanging. For unit tests.
  279. (bool focusSet, bool _) = SetHasFocusTrue (Application.Navigation?.GetFocused ());
  280. if (focusSet)
  281. {
  282. // The change happened
  283. // HasFocus is now true
  284. }
  285. }
  286. else
  287. {
  288. SetHasFocusFalse (null);
  289. }
  290. }
  291. }
  292. get => _hasFocus;
  293. }
  294. /// <summary>
  295. /// Causes this view to be focused. Calling this method has the same effect as setting <see cref="HasFocus"/> to
  296. /// <see langword="true"/> but with the added benefit of returning a value indicating whether the focus was set.
  297. /// </summary>
  298. public bool SetFocus ()
  299. {
  300. (bool focusSet, bool _) = SetHasFocusTrue (Application.Navigation?.GetFocused ());
  301. return focusSet;
  302. }
  303. /// <summary>
  304. /// INTERNAL: Called when focus is going to change to this view. This method is called by <see cref="SetFocus"/> and
  305. /// other methods that
  306. /// set or remove focus from a view.
  307. /// </summary>
  308. /// <param name="previousFocusedView">
  309. /// The previously focused view. If <see langword="null"/> there is no previously focused
  310. /// view.
  311. /// </param>
  312. /// <param name="traversingUp"></param>
  313. /// <returns><see langword="true"/> if <see cref="HasFocus"/> was changed to <see langword="true"/>.</returns>
  314. /// <exception cref="InvalidOperationException"></exception>
  315. private (bool focusSet, bool cancelled) SetHasFocusTrue (View? previousFocusedView, bool traversingUp = false)
  316. {
  317. Debug.Assert (ApplicationNavigation.IsInHierarchy (SuperView, this));
  318. // Pre-conditions
  319. if (_hasFocus)
  320. {
  321. return (false, false);
  322. }
  323. if (CanFocus && SuperView is { CanFocus: false })
  324. {
  325. Debug.WriteLine ($@"WARNING: Attempt to FocusChanging where SuperView.CanFocus == false. {this}");
  326. return (false, false);
  327. }
  328. if (!CanBeVisible (this) || !Enabled)
  329. {
  330. return (false, false);
  331. }
  332. if (!CanFocus)
  333. {
  334. return (false, false);
  335. }
  336. bool previousValue = HasFocus;
  337. bool cancelled = NotifyFocusChanging (false, true, previousFocusedView, this);
  338. if (cancelled)
  339. {
  340. return (false, true);
  341. }
  342. // Make sure superviews up the superview hierarchy have focus.
  343. // Any of them may cancel gaining focus. In which case we need to back out.
  344. if (SuperView is { HasFocus: false } sv)
  345. {
  346. (bool focusSet, bool svCancelled) = sv.SetHasFocusTrue (previousFocusedView, true);
  347. if (!focusSet)
  348. {
  349. return (false, svCancelled);
  350. }
  351. }
  352. if (_hasFocus)
  353. {
  354. // Something else beat us to the change (likely a FocusChanged handler).
  355. return (true, false);
  356. }
  357. // By setting _hasFocus to true we definitively change HasFocus for this view.
  358. // Get whatever peer has focus, if any
  359. View? focusedPeer = SuperView?.Focused;
  360. _hasFocus = true;
  361. // Ensure that the peer loses focus
  362. focusedPeer?.SetHasFocusFalse (this, true);
  363. if (!traversingUp)
  364. {
  365. // Restore focus to the previously most focused subview in the subview-hierarchy
  366. if (!RestoreFocus (TabStop))
  367. {
  368. // Couldn't restore focus, so use Advance to navigate to the next focusable subview
  369. if (!AdvanceFocus (NavigationDirection.Forward, null))
  370. {
  371. // Couldn't advance, so we're the most focused view in the application
  372. _previouslyMostFocused = null;
  373. Application.Navigation?.SetFocused (this);
  374. }
  375. }
  376. }
  377. if (previousFocusedView is { HasFocus: true } && Subviews.Contains (previousFocusedView))
  378. {
  379. previousFocusedView.SetHasFocusFalse (this);
  380. }
  381. NotifyFocusChanged (HasFocus, previousFocusedView, this);
  382. SetNeedsDisplay ();
  383. // Post-conditions - prove correctness
  384. if (HasFocus == previousValue)
  385. {
  386. throw new InvalidOperationException ("NotifyFocusChanging was not cancelled and the HasFocus value did not change.");
  387. }
  388. return (true, false);
  389. }
  390. private bool NotifyFocusChanging (bool currentHasFocus, bool newHasFocus, View? currentFocused, View? newFocused)
  391. {
  392. // Call the virtual method
  393. if (OnHasFocusChanging (currentHasFocus, newHasFocus, currentFocused, newFocused))
  394. {
  395. // The event was cancelled
  396. return true;
  397. }
  398. var args = new HasFocusEventArgs (currentHasFocus, newHasFocus, currentFocused, newFocused);
  399. HasFocusChanging?.Invoke (this, args);
  400. if (args.Cancel)
  401. {
  402. // The event was cancelled
  403. return true;
  404. }
  405. return false;
  406. }
  407. /// <summary>
  408. /// Invoked when <see cref="View.HasFocus"/> is about to change. This method is called before the
  409. /// <see cref="HasFocusChanging"/> event is raised.
  410. /// </summary>
  411. /// <remarks>
  412. /// <para>
  413. /// Use <see cref="OnHasFocusChanged"/> to be notified after the focus has changed.
  414. /// </para>
  415. /// </remarks>
  416. /// <param name="currentHasFocus">The current value of <see cref="View.HasFocus"/>.</param>
  417. /// <param name="newHasFocus">The value <see cref="View.HasFocus"/> will have if the focus change happens.</param>
  418. /// <param name="currentFocused">The view that is currently Focused. May be <see langword="null"/>.</param>
  419. /// <param name="newFocused">The view that will be focused. May be <see langword="null"/>.</param>
  420. /// <returns>
  421. /// <see langword="true"/>, if the change to <see cref="View.HasFocus"/> is to be cancelled, <see langword="false"/>
  422. /// otherwise.
  423. /// </returns>
  424. protected virtual bool OnHasFocusChanging (bool currentHasFocus, bool newHasFocus, View? currentFocused, View? newFocused)
  425. {
  426. return false;
  427. }
  428. /// <summary>
  429. /// Raised when <see cref="View.HasFocus"/> is about to change.
  430. /// </summary>
  431. /// <remarks>
  432. /// <para>
  433. /// Cancel the event to prevent the focus from changing.
  434. /// </para>
  435. /// <para>
  436. /// Use <see cref="HasFocusChanged"/> to be notified after the focus has changed.
  437. /// </para>
  438. /// </remarks>
  439. public event EventHandler<HasFocusEventArgs>? HasFocusChanging;
  440. /// <summary>
  441. /// Called when this view should stop being focused.
  442. /// </summary>
  443. /// <param name="newFocusedView">The new focused view. If <see langword="null"/> it is not known which view will be focused.</param>
  444. /// <param name="traversingDown">Set to true to indicate method is being called recurively, traversing down the focus chain.</param>
  445. /// <exception cref="InvalidOperationException"></exception>
  446. private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false)
  447. {
  448. // Pre-conditions
  449. if (!_hasFocus)
  450. {
  451. throw new InvalidOperationException ("SetHasFocusFalse should not be called if the view does not have focus.");
  452. }
  453. // If newFocusedVew is null, we need to find the view that should get focus, and SetFocus on it.
  454. if (!traversingDown && newFocusedView is null)
  455. {
  456. if (SuperView?._previouslyMostFocused is { } && SuperView?._previouslyMostFocused != this)
  457. {
  458. SuperView?._previouslyMostFocused?.SetFocus ();
  459. // The above will cause SetHasFocusFalse, so we can return
  460. return;
  461. }
  462. if (SuperView is { } && SuperView.AdvanceFocus (NavigationDirection.Forward, TabStop))
  463. {
  464. // The above will cause SetHasFocusFalse, so we can return
  465. return;
  466. }
  467. if (Application.Navigation is { } && Application.Current is { })
  468. {
  469. // Temporarily ensure this view can't get focus
  470. bool prevCanFocus = _canFocus;
  471. _canFocus = false;
  472. bool restoredFocus = Application.Current!.RestoreFocus (null);
  473. _canFocus = prevCanFocus;
  474. if (restoredFocus)
  475. {
  476. // The above caused SetHasFocusFalse, so we can return
  477. return;
  478. }
  479. }
  480. // No other focusable view to be found. Just "leave" us...
  481. }
  482. // Before we can leave focus, we need to make sure that all views down the subview-hierarchy have left focus.
  483. View? mostFocused = MostFocused;
  484. if (mostFocused is { } && (newFocusedView is null || mostFocused != newFocusedView))
  485. {
  486. // Start at the bottom and work our way up to us
  487. View? bottom = mostFocused;
  488. while (bottom is { } && bottom != this)
  489. {
  490. if (bottom.HasFocus)
  491. {
  492. bottom.SetHasFocusFalse (newFocusedView, true);
  493. }
  494. bottom = bottom.SuperView;
  495. }
  496. _previouslyMostFocused = mostFocused;
  497. }
  498. bool previousValue = HasFocus;
  499. // Note, can't be cancelled.
  500. NotifyFocusChanging (HasFocus, !HasFocus, newFocusedView, this);
  501. // Get whatever peer has focus, if any
  502. View? focusedPeer = SuperView?.Focused;
  503. _hasFocus = false;
  504. if (Application.Navigation is { })
  505. {
  506. View? appFocused = Application.Navigation.GetFocused ();
  507. if (appFocused is { } || appFocused == this)
  508. {
  509. Application.Navigation.SetFocused (newFocusedView ?? SuperView);
  510. }
  511. }
  512. NotifyFocusChanged (HasFocus, this, newFocusedView);
  513. if (_hasFocus)
  514. {
  515. // Notify caused HasFocus to change to true.
  516. return;
  517. }
  518. if (SuperView is { })
  519. {
  520. SuperView._previouslyMostFocused = focusedPeer;
  521. }
  522. // Post-conditions - prove correctness
  523. if (HasFocus == previousValue)
  524. {
  525. throw new InvalidOperationException ("SetHasFocusFalse and the HasFocus value did not change.");
  526. }
  527. SetNeedsDisplay ();
  528. }
  529. /// <summary>
  530. /// Caches the most focused subview when this view is losing focus. This is used by <see cref="RestoreFocus"/>.
  531. /// </summary>
  532. private View? _previouslyMostFocused;
  533. private void NotifyFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedVew)
  534. {
  535. // Call the virtual method
  536. OnHasFocusChanged (newHasFocus, previousFocusedView, focusedVew);
  537. // Raise the event
  538. var args = new HasFocusEventArgs (newHasFocus, newHasFocus, previousFocusedView, focusedVew);
  539. HasFocusChanged?.Invoke (this, args);
  540. }
  541. /// <summary>
  542. /// Invoked after <see cref="HasFocus"/> has changed. This method is called before the <see cref="HasFocusChanged"/>
  543. /// event is raised.
  544. /// </summary>
  545. /// <remarks>
  546. /// <para>
  547. /// This event cannot be cancelled.
  548. /// </para>
  549. /// </remarks>
  550. /// <param name="newHasFocus">The new value of <see cref="View.HasFocus"/>.</param>
  551. /// <param name="previousFocusedView"></param>
  552. /// <param name="focusedVew">The view that is now focused. May be <see langword="null"/></param>
  553. protected virtual void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedVew) { }
  554. /// <summary>Raised after <see cref="HasFocus"/> has changed.</summary>
  555. /// <remarks>
  556. /// <para>
  557. /// This event cannot be cancelled.
  558. /// </para>
  559. /// </remarks>
  560. public event EventHandler<HasFocusEventArgs>? HasFocusChanged;
  561. #endregion HasFocus
  562. #region Tab/Focus Handling
  563. /// <summary>
  564. /// Gets TabIndexes that are scoped to the specified behavior and direction. If behavior is null, all TabIndexes are
  565. /// returned.
  566. /// </summary>
  567. /// <param name="direction"></param>
  568. /// <param name="behavior"></param>
  569. /// <returns></returns>
  570. /// GetScopedTabIndexes
  571. internal View [] GetSubviewFocusChain (NavigationDirection direction, TabBehavior? behavior)
  572. {
  573. IEnumerable<View>? fitleredSubviews;
  574. if (behavior.HasValue)
  575. {
  576. fitleredSubviews = _subviews?.Where (v => v.TabStop == behavior && v is { CanFocus: true, Visible: true, Enabled: true });
  577. }
  578. else
  579. {
  580. fitleredSubviews = _subviews?.Where (v => v is { CanFocus: true, Visible: true, Enabled: true });
  581. }
  582. if (direction == NavigationDirection.Backward)
  583. {
  584. fitleredSubviews = fitleredSubviews?.Reverse ();
  585. }
  586. return fitleredSubviews?.ToArray () ?? Array.Empty<View> ();
  587. }
  588. private TabBehavior? _tabStop;
  589. /// <summary>
  590. /// Gets or sets the behavior of <see cref="AdvanceFocus"/> for keyboard navigation.
  591. /// </summary>
  592. /// <remarks>
  593. /// <para>
  594. /// If <see langword="null"/> the tab stop has not been set and setting <see cref="CanFocus"/> to true will set it
  595. /// to
  596. /// <see cref="TabBehavior.TabStop"/>.
  597. /// </para>
  598. /// <para>
  599. /// TabStop is independent of <see cref="CanFocus"/>. If <see cref="CanFocus"/> is <see langword="false"/>, the
  600. /// view will not gain
  601. /// focus even if this property is set and vice-versa.
  602. /// </para>
  603. /// <para>
  604. /// The default <see cref="TabBehavior.TabStop"/> keys are <see cref="Application.NextTabKey"/> (<c>Key.Tab</c>)
  605. /// and <see cref="Application.PrevTabKey"/> (<c>Key>Tab.WithShift</c>).
  606. /// </para>
  607. /// <para>
  608. /// The default <see cref="TabBehavior.TabGroup"/> keys are <see cref="Application.NextTabGroupKey"/> (
  609. /// <c>Key.F6</c>) and <see cref="Application.PrevTabGroupKey"/> (<c>Key>Key.F6.WithShift</c>).
  610. /// </para>
  611. /// </remarks>
  612. public TabBehavior? TabStop
  613. {
  614. get => _tabStop;
  615. set
  616. {
  617. if (_tabStop is { } && _tabStop == value)
  618. {
  619. return;
  620. }
  621. _tabStop = value;
  622. }
  623. }
  624. #endregion Tab/Focus Handling
  625. }