View.Navigation.cs 24 KB

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