View.Navigation.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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
  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 (TabBehavior? behavior)
  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. if (value)
  278. {
  279. // NOTE: If Application.Navigation is null, we pass null to FocusChanging. For unit tests.
  280. (bool focusSet, bool _) = SetHasFocusTrue (Application.Navigation?.GetFocused ());
  281. if (focusSet)
  282. {
  283. // The change happened
  284. // HasFocus is now true
  285. }
  286. }
  287. else
  288. {
  289. SetHasFocusFalse (null);
  290. }
  291. }
  292. }
  293. get => _hasFocus;
  294. }
  295. /// <summary>
  296. /// Causes this view to be focused. Calling this method has the same effect as setting <see cref="HasFocus"/> to
  297. /// <see langword="true"/> but with the added benefit of returning a value indicating whether the focus was set.
  298. /// </summary>
  299. public bool SetFocus ()
  300. {
  301. (bool focusSet, bool _) = SetHasFocusTrue (Application.Navigation?.GetFocused ());
  302. return focusSet;
  303. }
  304. /// <summary>
  305. /// INTERNAL: Called when focus is going to change to this view. This method is called by <see cref="SetFocus"/> and
  306. /// other methods that
  307. /// set or remove focus from a view.
  308. /// </summary>
  309. /// <param name="previousFocusedView">
  310. /// The previously focused view. If <see langword="null"/> there is no previously focused
  311. /// view.
  312. /// </param>
  313. /// <param name="traversingUp"></param>
  314. /// <returns><see langword="true"/> if <see cref="HasFocus"/> was changed to <see langword="true"/>.</returns>
  315. /// <exception cref="InvalidOperationException"></exception>
  316. private (bool focusSet, bool cancelled) SetHasFocusTrue (View? previousFocusedView, bool traversingUp = false)
  317. {
  318. Debug.Assert (ApplicationNavigation.IsInHierarchy (SuperView, this));
  319. // Pre-conditions
  320. if (_hasFocus)
  321. {
  322. return (false, false);
  323. }
  324. if (CanFocus && SuperView is { CanFocus: false })
  325. {
  326. Debug.WriteLine ($@"WARNING: Attempt to FocusChanging where SuperView.CanFocus == false. {this}");
  327. return (false, false);
  328. }
  329. if (!CanBeVisible (this) || !Enabled)
  330. {
  331. return (false, false);
  332. }
  333. if (!CanFocus)
  334. {
  335. return (false, false);
  336. }
  337. bool previousValue = HasFocus;
  338. bool cancelled = NotifyFocusChanging (false, true, previousFocusedView, this);
  339. if (cancelled)
  340. {
  341. return (false, true);
  342. }
  343. // Make sure superviews up the superview hierarchy have focus.
  344. // Any of them may cancel gaining focus. In which case we need to back out.
  345. if (SuperView is { HasFocus: false } sv)
  346. {
  347. (bool focusSet, bool svCancelled) = sv.SetHasFocusTrue (previousFocusedView, true);
  348. if (!focusSet)
  349. {
  350. return (false, svCancelled);
  351. }
  352. }
  353. if (_hasFocus)
  354. {
  355. // Something else beat us to the change (likely a FocusChanged handler).
  356. return (true, false);
  357. }
  358. // By setting _hasFocus to true we definitively change HasFocus for this view.
  359. // Get whatever peer has focus, if any
  360. View? focusedPeer = SuperView?.Focused;
  361. _hasFocus = true;
  362. // Ensure that the peer loses focus
  363. focusedPeer?.SetHasFocusFalse (this, true);
  364. if (!traversingUp)
  365. {
  366. // Restore focus to the previously most focused subview in the subview-hierarchy
  367. if (!RestoreFocus (TabStop))
  368. {
  369. // Couldn't restore focus, so use Advance to navigate to the next focusable subview
  370. if (!AdvanceFocus (NavigationDirection.Forward, null))
  371. {
  372. // Couldn't advance, so we're the most focused view in the application
  373. _previouslyMostFocused = null;
  374. Application.Navigation?.SetFocused (this);
  375. }
  376. }
  377. }
  378. if (previousFocusedView is { HasFocus: true } && Subviews.Contains (previousFocusedView))
  379. {
  380. previousFocusedView.SetHasFocusFalse (this);
  381. }
  382. if (Arrangement.HasFlag (ViewArrangement.Overlapped))
  383. {
  384. SuperView?.MoveSubviewToStart (this);
  385. }
  386. NotifyFocusChanged (HasFocus, previousFocusedView, this);
  387. // Post-conditions - prove correctness
  388. if (HasFocus == previousValue)
  389. {
  390. throw new InvalidOperationException ("NotifyFocusChanging was not cancelled and the HasFocus value did not change.");
  391. }
  392. return (true, false);
  393. }
  394. private bool NotifyFocusChanging (bool currentHasFocus, bool newHasFocus, View? currentFocused, View? newFocused)
  395. {
  396. // Call the virtual method
  397. if (OnHasFocusChanging (currentHasFocus, newHasFocus, currentFocused, newFocused))
  398. {
  399. // The event was cancelled
  400. return true;
  401. }
  402. var args = new HasFocusEventArgs (currentHasFocus, newHasFocus, currentFocused, newFocused);
  403. HasFocusChanging?.Invoke (this, args);
  404. if (args.Cancel)
  405. {
  406. // The event was cancelled
  407. return true;
  408. }
  409. return false;
  410. }
  411. /// <summary>
  412. /// Invoked when <see cref="View.HasFocus"/> is about to change. This method is called before the
  413. /// <see cref="HasFocusChanging"/> event is raised.
  414. /// </summary>
  415. /// <remarks>
  416. /// <para>
  417. /// Use <see cref="OnHasFocusChanged"/> to be notified after the focus has changed.
  418. /// </para>
  419. /// </remarks>
  420. /// <param name="currentHasFocus">The current value of <see cref="View.HasFocus"/>.</param>
  421. /// <param name="newHasFocus">The value <see cref="View.HasFocus"/> will have if the focus change happens.</param>
  422. /// <param name="currentFocused">The view that is currently Focused. May be <see langword="null"/>.</param>
  423. /// <param name="newFocused">The view that will be focused. May be <see langword="null"/>.</param>
  424. /// <returns>
  425. /// <see langword="true"/>, if the change to <see cref="View.HasFocus"/> is to be cancelled, <see langword="false"/>
  426. /// otherwise.
  427. /// </returns>
  428. protected virtual bool OnHasFocusChanging (bool currentHasFocus, bool newHasFocus, View? currentFocused, View? newFocused) { return false; }
  429. /// <summary>
  430. /// Raised when <see cref="View.HasFocus"/> is about to change.
  431. /// </summary>
  432. /// <remarks>
  433. /// <para>
  434. /// Cancel the event to prevent the focus from changing.
  435. /// </para>
  436. /// <para>
  437. /// Use <see cref="HasFocusChanged"/> to be notified after the focus has changed.
  438. /// </para>
  439. /// </remarks>
  440. public event EventHandler<HasFocusEventArgs>? HasFocusChanging;
  441. /// <summary>
  442. /// Called when this view should stop being focused.
  443. /// </summary>
  444. /// <param name="newFocusedView">
  445. /// The new focused view. If <see langword="null"/> it is not known which view will be
  446. /// focused.
  447. /// </param>
  448. /// <param name="traversingDown">
  449. /// Set to true to indicate method is being called recurively, traversing down the focus
  450. /// chain.
  451. /// </param>
  452. /// <exception cref="InvalidOperationException"></exception>
  453. private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false)
  454. {
  455. // Pre-conditions
  456. if (!_hasFocus)
  457. {
  458. throw new InvalidOperationException ("SetHasFocusFalse should not be called if the view does not have focus.");
  459. }
  460. // If newFocusedVew is null, we need to find the view that should get focus, and SetFocus on it.
  461. if (!traversingDown && newFocusedView is null)
  462. {
  463. if (SuperView?._previouslyMostFocused is { } && SuperView?._previouslyMostFocused != this)
  464. {
  465. SuperView?._previouslyMostFocused?.SetFocus ();
  466. // The above will cause SetHasFocusFalse, so we can return
  467. return;
  468. }
  469. if (SuperView is { } && SuperView.AdvanceFocus (NavigationDirection.Forward, TabStop))
  470. {
  471. // The above will cause SetHasFocusFalse, so we can return
  472. return;
  473. }
  474. if (Application.Navigation is { } && Application.Current is { })
  475. {
  476. // Temporarily ensure this view can't get focus
  477. bool prevCanFocus = _canFocus;
  478. _canFocus = false;
  479. bool restoredFocus = Application.Current!.RestoreFocus (null);
  480. _canFocus = prevCanFocus;
  481. if (restoredFocus)
  482. {
  483. // The above caused SetHasFocusFalse, so we can return
  484. return;
  485. }
  486. }
  487. // No other focusable view to be found. Just "leave" us...
  488. }
  489. // Before we can leave focus, we need to make sure that all views down the subview-hierarchy have left focus.
  490. View? mostFocused = MostFocused;
  491. if (mostFocused is { } && (newFocusedView is null || mostFocused != newFocusedView))
  492. {
  493. // Start at the bottom and work our way up to us
  494. View? bottom = mostFocused;
  495. while (bottom is { } && bottom != this)
  496. {
  497. if (bottom.HasFocus)
  498. {
  499. bottom.SetHasFocusFalse (newFocusedView, true);
  500. }
  501. bottom = bottom.SuperView;
  502. }
  503. _previouslyMostFocused = mostFocused;
  504. }
  505. bool previousValue = HasFocus;
  506. // Note, can't be cancelled.
  507. NotifyFocusChanging (HasFocus, !HasFocus, newFocusedView, this);
  508. // Get whatever peer has focus, if any
  509. View? focusedPeer = SuperView?.Focused;
  510. _hasFocus = false;
  511. if (Application.Navigation is { })
  512. {
  513. View? appFocused = Application.Navigation.GetFocused ();
  514. if (appFocused is { } || appFocused == this)
  515. {
  516. Application.Navigation.SetFocused (newFocusedView ?? SuperView);
  517. }
  518. }
  519. NotifyFocusChanged (HasFocus, this, newFocusedView);
  520. if (_hasFocus)
  521. {
  522. // Notify caused HasFocus to change to true.
  523. return;
  524. }
  525. if (SuperView is { })
  526. {
  527. SuperView._previouslyMostFocused = focusedPeer;
  528. }
  529. // Post-conditions - prove correctness
  530. if (HasFocus == previousValue)
  531. {
  532. throw new InvalidOperationException ("SetHasFocusFalse and the HasFocus value did not change.");
  533. }
  534. SetNeedsDisplay ();
  535. }
  536. /// <summary>
  537. /// Caches the most focused subview when this view is losing focus. This is used by <see cref="RestoreFocus"/>.
  538. /// </summary>
  539. private View? _previouslyMostFocused;
  540. private void NotifyFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedVew)
  541. {
  542. // Call the virtual method
  543. OnHasFocusChanged (newHasFocus, previousFocusedView, focusedVew);
  544. // Raise the event
  545. var args = new HasFocusEventArgs (newHasFocus, newHasFocus, previousFocusedView, focusedVew);
  546. HasFocusChanged?.Invoke (this, args);
  547. }
  548. /// <summary>
  549. /// Invoked after <see cref="HasFocus"/> has changed. This method is called before the <see cref="HasFocusChanged"/>
  550. /// event is raised.
  551. /// </summary>
  552. /// <remarks>
  553. /// <para>
  554. /// This event cannot be cancelled.
  555. /// </para>
  556. /// </remarks>
  557. /// <param name="newHasFocus">The new value of <see cref="View.HasFocus"/>.</param>
  558. /// <param name="previousFocusedView"></param>
  559. /// <param name="focusedVew">The view that is now focused. May be <see langword="null"/></param>
  560. protected virtual void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedVew) { }
  561. /// <summary>Raised after <see cref="HasFocus"/> has changed.</summary>
  562. /// <remarks>
  563. /// <para>
  564. /// This event cannot be cancelled.
  565. /// </para>
  566. /// </remarks>
  567. public event EventHandler<HasFocusEventArgs>? HasFocusChanged;
  568. #endregion HasFocus
  569. #region Tab/Focus Handling
  570. /// <summary>
  571. /// Gets TabIndexes that are scoped to the specified behavior and direction. If behavior is null, all TabIndexes are
  572. /// returned.
  573. /// </summary>
  574. /// <param name="direction"></param>
  575. /// <param name="behavior"></param>
  576. /// <returns></returns>
  577. /// GetScopedTabIndexes
  578. internal View [] GetSubviewFocusChain (NavigationDirection direction, TabBehavior? behavior)
  579. {
  580. IEnumerable<View>? fitleredSubviews;
  581. if (behavior.HasValue)
  582. {
  583. fitleredSubviews = _subviews?.Where (v => v.TabStop == behavior && v is { CanFocus: true, Visible: true, Enabled: true });
  584. }
  585. else
  586. {
  587. fitleredSubviews = _subviews?.Where (v => v is { CanFocus: true, Visible: true, Enabled: true });
  588. }
  589. if (direction == NavigationDirection.Backward)
  590. {
  591. fitleredSubviews = fitleredSubviews?.Reverse ();
  592. }
  593. return fitleredSubviews?.ToArray () ?? Array.Empty<View> ();
  594. }
  595. private TabBehavior? _tabStop;
  596. /// <summary>
  597. /// Gets or sets the behavior of <see cref="AdvanceFocus"/> for keyboard navigation.
  598. /// </summary>
  599. /// <remarks>
  600. /// <para>
  601. /// If <see langword="null"/> the tab stop has not been set and setting <see cref="CanFocus"/> to true will set it
  602. /// to
  603. /// <see cref="TabBehavior.TabStop"/>.
  604. /// </para>
  605. /// <para>
  606. /// TabStop is independent of <see cref="CanFocus"/>. If <see cref="CanFocus"/> is <see langword="false"/>, the
  607. /// view will not gain
  608. /// focus even if this property is set and vice-versa.
  609. /// </para>
  610. /// <para>
  611. /// The default <see cref="TabBehavior.TabStop"/> keys are <see cref="Application.NextTabKey"/> (<c>Key.Tab</c>)
  612. /// and <see cref="Application.PrevTabKey"/> (<c>Key>Tab.WithShift</c>).
  613. /// </para>
  614. /// <para>
  615. /// The default <see cref="TabBehavior.TabGroup"/> keys are <see cref="Application.NextTabGroupKey"/> (
  616. /// <c>Key.F6</c>) and <see cref="Application.PrevTabGroupKey"/> (<c>Key>Key.F6.WithShift</c>).
  617. /// </para>
  618. /// </remarks>
  619. public TabBehavior? TabStop
  620. {
  621. get => _tabStop;
  622. set
  623. {
  624. if (_tabStop is { } && _tabStop == value)
  625. {
  626. return;
  627. }
  628. _tabStop = value;
  629. }
  630. }
  631. #endregion Tab/Focus Handling
  632. }