View.Navigation.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. using System.Diagnostics;
  2. using static Terminal.Gui.FakeDriver;
  3. namespace Terminal.Gui;
  4. public partial class View // Focus and cross-view navigation management (TabStop, TabIndex, etc...)
  5. {
  6. // BUGBUG: This is a poor API design. Automatic behavior like this is non-obvious and should be avoided. Instead, callers to Add should be explicit about what they want.
  7. // Set to true in Add() to indicate that the view being added to a SuperView has CanFocus=true.
  8. // Makes it so CanFocus will update the SuperView's CanFocus property.
  9. internal bool _addingViewSoCanFocusAlsoUpdatesSuperView;
  10. private NavigationDirection _focusDirection;
  11. private bool _hasFocus;
  12. // Used to cache CanFocus on subviews when CanFocus is set to false so that it can be restored when CanFocus is changed back to true
  13. private bool _oldCanFocus;
  14. private bool _canFocus;
  15. /// <summary>
  16. /// Advances the focus to the next or previous view in <see cref="View.TabIndexes"/>, based on
  17. /// <paramref name="direction"/>.
  18. /// itself.
  19. /// </summary>
  20. /// <remarks>
  21. /// <para>
  22. /// If there is no next/previous view, the focus is set to the view itself.
  23. /// </para>
  24. /// </remarks>
  25. /// <param name="direction"></param>
  26. /// <param name="groupOnly">If <see langword="true"/> will advance into ...</param>
  27. /// <returns>
  28. /// <see langword="true"/> if focus was changed to another subview (or stayed on this one), <see langword="false"/>
  29. /// otherwise.
  30. /// </returns>
  31. public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior)
  32. {
  33. if (!CanBeVisible (this))
  34. {
  35. return false;
  36. }
  37. FocusDirection = direction;
  38. if (TabIndexes is null || TabIndexes.Count == 0)
  39. {
  40. return false;
  41. }
  42. if (Focused is null)
  43. {
  44. FocusDeepest (behavior, direction);
  45. return Focused is { };
  46. }
  47. if (Focused is { })
  48. {
  49. if (Focused.AdvanceFocus (direction, behavior))
  50. {
  51. return true;
  52. }
  53. }
  54. var index = GetScopedTabIndexes (behavior, direction);
  55. if (index.Length == 0)
  56. {
  57. return false;
  58. }
  59. var focusedIndex = index.IndexOf (Focused);
  60. int next = 0;
  61. if (focusedIndex < index.Length - 1)
  62. {
  63. next = focusedIndex + 1;
  64. }
  65. else
  66. {
  67. if (behavior == TabBehavior.TabGroup && behavior == TabStop && SuperView?.TabStop == TabBehavior.TabGroup)
  68. {
  69. // Go up the hierarchy
  70. // Leave
  71. Focused.SetHasFocus (false, this);
  72. // Signal that nothing is focused, and callers should try a peer-subview
  73. Focused = null;
  74. return false;
  75. }
  76. Focused.RestoreFocus (TabBehavior.TabStop);
  77. return true;
  78. // Wrap around
  79. //if (SuperView is {})
  80. //{
  81. // if (direction == NavigationDirection.Forward)
  82. // {
  83. // return false;
  84. // }
  85. // else
  86. // {
  87. // return false;
  88. // //SuperView.FocusFirst (groupOnly);
  89. // }
  90. // return true;
  91. //}
  92. //next = index.Length - 1;
  93. }
  94. View view = index [next];
  95. if (view.HasFocus)
  96. {
  97. return true;
  98. }
  99. // The subview does not have focus, but at least one other that can. Can this one be focused?
  100. if (view.CanFocus && view.Visible && view.Enabled)
  101. {
  102. // Make Focused Leave
  103. Focused.SetHasFocus (false, view);
  104. view.FocusDeepest (TabBehavior.TabStop, direction);
  105. return true;
  106. }
  107. if (Focused is { })
  108. {
  109. // Leave
  110. Focused.SetHasFocus (false, this);
  111. // Signal that nothing is focused, and callers should try a peer-subview
  112. Focused = null;
  113. }
  114. return false;
  115. }
  116. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> can be focused.</summary>
  117. /// <remarks>
  118. /// <para>
  119. /// <see cref="SuperView"/> must also have <see cref="CanFocus"/> set to <see langword="true"/>.
  120. /// </para>
  121. /// <para>
  122. /// When set to <see langword="false"/>, if an attempt is made to make this view focused, the focus will be set to
  123. /// the next focusable view.
  124. /// </para>
  125. /// <para>
  126. /// When set to <see langword="false"/>, the <see cref="TabIndex"/> will be set to -1.
  127. /// </para>
  128. /// <para>
  129. /// When set to <see langword="false"/>, the values of <see cref="CanFocus"/> and <see cref="TabIndex"/> for all
  130. /// subviews will be cached so that when <see cref="CanFocus"/> is set back to <see langword="true"/>, the subviews
  131. /// will be restored to their previous values.
  132. /// </para>
  133. /// <para>
  134. /// Changing this peroperty to <see langword="true"/> will cause <see cref="TabStop"/> to be set to
  135. /// <see cref="TabBehavior.TabStop"/>" as a convenience. Changing this peroperty to
  136. /// <see langword="false"/> will have no effect on <see cref="TabStop"/>.
  137. /// </para>
  138. /// </remarks>
  139. public bool CanFocus
  140. {
  141. get => _canFocus;
  142. set
  143. {
  144. if (!_addingViewSoCanFocusAlsoUpdatesSuperView && IsInitialized && SuperView?.CanFocus == false && value)
  145. {
  146. throw new InvalidOperationException ("Cannot set CanFocus to true if the SuperView CanFocus is false!");
  147. }
  148. if (_canFocus == value)
  149. {
  150. return;
  151. }
  152. _canFocus = value;
  153. switch (_canFocus)
  154. {
  155. case false when _tabIndex > -1:
  156. // BUGBUG: This is a poor API design. Automatic behavior like this is non-obvious and should be avoided. Callers should adjust TabIndex explicitly.
  157. //TabIndex = -1;
  158. break;
  159. case true when SuperView?.CanFocus == false && _addingViewSoCanFocusAlsoUpdatesSuperView:
  160. SuperView.CanFocus = true;
  161. break;
  162. }
  163. if (TabStop is null && _canFocus)
  164. {
  165. TabStop = TabBehavior.TabStop;
  166. }
  167. if (!_canFocus && SuperView?.Focused == this)
  168. {
  169. SuperView.Focused = null;
  170. }
  171. if (!_canFocus && HasFocus)
  172. {
  173. SetHasFocus (false, this);
  174. SuperView?.RestoreFocus (null);
  175. // If EnsureFocus () didn't set focus to a view, focus the next focusable view in the application
  176. if (SuperView is { Focused: null })
  177. {
  178. SuperView.AdvanceFocus (NavigationDirection.Forward, null);
  179. if (SuperView.Focused is null && Application.Current is { })
  180. {
  181. Application.Current.AdvanceFocus (NavigationDirection.Forward, null);
  182. }
  183. ApplicationOverlapped.BringOverlappedTopToFront ();
  184. }
  185. }
  186. if (_subviews is { } && IsInitialized)
  187. {
  188. foreach (View view in _subviews)
  189. {
  190. if (view.CanFocus != value)
  191. {
  192. if (!value)
  193. {
  194. // Cache the old CanFocus and TabIndex so that they can be restored when CanFocus is changed back to true
  195. view._oldCanFocus = view.CanFocus;
  196. view._oldTabIndex = view._tabIndex;
  197. view.CanFocus = false;
  198. //view._tabIndex = -1;
  199. }
  200. else
  201. {
  202. if (_addingViewSoCanFocusAlsoUpdatesSuperView)
  203. {
  204. view._addingViewSoCanFocusAlsoUpdatesSuperView = true;
  205. }
  206. // Restore the old CanFocus and TabIndex to the values they held before CanFocus was set to false
  207. view.CanFocus = view._oldCanFocus;
  208. view._tabIndex = view._oldTabIndex;
  209. view._addingViewSoCanFocusAlsoUpdatesSuperView = false;
  210. }
  211. }
  212. }
  213. if (this is Toplevel && Application.Current!.Focused != this)
  214. {
  215. ApplicationOverlapped.BringOverlappedTopToFront ();
  216. }
  217. }
  218. OnCanFocusChanged ();
  219. SetNeedsDisplay ();
  220. }
  221. }
  222. /// <summary>Raised when <see cref="CanFocus"/> has been changed.</summary>
  223. /// <remarks>
  224. /// Raised by the <see cref="OnCanFocusChanged"/> virtual method.
  225. /// </remarks>
  226. public event EventHandler CanFocusChanged;
  227. /// <summary>Raised when the view is gaining (entering) focus. Can be cancelled.</summary>
  228. /// <remarks>
  229. /// Raised by the <see cref="OnEnter"/> virtual method.
  230. /// </remarks>
  231. public event EventHandler<FocusEventArgs> Enter;
  232. /// <summary>Returns the currently focused Subview inside this view, or <see langword="null"/> if nothing is focused.</summary>
  233. /// <value>The currently focused Subview.</value>
  234. public View Focused { get; private set; }
  235. /// <summary>
  236. /// Focuses the deepest focusable view in <see cref="View.TabIndexes"/> if one exists. If there are no views in
  237. /// <see cref="View.TabIndexes"/> then the focus is set to the view itself.
  238. /// </summary>
  239. /// <param name="behavior"></param>
  240. /// <param name="direction"></param>
  241. public void FocusDeepest (TabBehavior? behavior, NavigationDirection direction)
  242. {
  243. if (!CanBeVisible (this))
  244. {
  245. return;
  246. }
  247. View deepest = FindDeepestFocusableView (behavior, direction);
  248. if (deepest is { })
  249. {
  250. deepest.SetFocus ();
  251. }
  252. SetFocus ();
  253. }
  254. [CanBeNull]
  255. private View FindDeepestFocusableView (TabBehavior? behavior, NavigationDirection direction)
  256. {
  257. var indicies = GetScopedTabIndexes (behavior, direction);
  258. foreach (View v in indicies)
  259. {
  260. if (v.TabIndexes.Count == 0)
  261. {
  262. return v;
  263. }
  264. return v.FindDeepestFocusableView (behavior, direction);
  265. }
  266. return null;
  267. }
  268. /// <summary>
  269. /// Gets or sets whether this view has focus.
  270. /// </summary>
  271. /// <remarks>
  272. /// <para>
  273. /// Causes the <see cref="OnEnter"/> and <see cref="OnLeave"/> virtual methods (and <see cref="Enter"/> and
  274. /// <see cref="Leave"/> events to be raised) when the value changes.
  275. /// </para>
  276. /// <para>
  277. /// Setting this property to <see langword="false"/> will recursively set <see cref="HasFocus"/> to
  278. /// <see langword="false"/>
  279. /// for any focused subviews.
  280. /// </para>
  281. /// </remarks>
  282. public bool HasFocus
  283. {
  284. // Force the specified view to have focus
  285. set => SetHasFocus (value, this, true);
  286. get => _hasFocus;
  287. }
  288. /// <summary>Returns a value indicating if this View is currently on Top (Active)</summary>
  289. public bool IsCurrentTop => Application.Current == this;
  290. /// <summary>Raised when the view is losing (leaving) focus. Can be cancelled.</summary>
  291. /// <remarks>
  292. /// Raised by the <see cref="OnLeave"/> virtual method.
  293. /// </remarks>
  294. public event EventHandler<FocusEventArgs> Leave;
  295. /// <summary>
  296. /// Returns the most focused Subview in the chain of subviews (the leaf view that has the focus), or
  297. /// <see langword="null"/> if nothing is focused.
  298. /// </summary>
  299. /// <value>The most focused Subview.</value>
  300. public View MostFocused
  301. {
  302. get
  303. {
  304. if (Focused is null)
  305. {
  306. return null;
  307. }
  308. View most = Focused.MostFocused;
  309. if (most is { })
  310. {
  311. return most;
  312. }
  313. return Focused;
  314. }
  315. }
  316. /// <summary>Invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  317. /// <remarks>
  318. /// Raises the <see cref="CanFocusChanged"/> event.
  319. /// </remarks>
  320. public virtual void OnCanFocusChanged () { CanFocusChanged?.Invoke (this, EventArgs.Empty); }
  321. // BUGBUG: The focus API is poorly defined and implemented. It deeply intertwines the view hierarchy with the tab order.
  322. /// <summary>Invoked when this view is gaining focus (entering).</summary>
  323. /// <param name="leavingView">The view that is leaving focus.</param>
  324. /// <returns> <see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  325. /// <remarks>
  326. /// <para>
  327. /// Overrides must call the base class method to ensure that the <see cref="Enter"/> event is raised. If the event
  328. /// is handled, the method should return <see langword="true"/>.
  329. /// </para>
  330. /// </remarks>
  331. public virtual bool OnEnter (View leavingView)
  332. {
  333. // BUGBUG: _hasFocus should ALWAYS be false when this method is called.
  334. if (_hasFocus)
  335. {
  336. Debug.WriteLine ($"BUGBUG: HasFocus should be false when OnEnter is called - Leaving: {leavingView} Entering: {this}");
  337. // return true;
  338. }
  339. var args = new FocusEventArgs (leavingView, this);
  340. Enter?.Invoke (this, args);
  341. if (args.Handled)
  342. {
  343. return true;
  344. }
  345. return false;
  346. }
  347. /// <summary>Invoked when this view is losing focus (leaving).</summary>
  348. /// <param name="enteringView">The view that is entering focus.</param>
  349. /// <returns> <see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  350. /// <remarks>
  351. /// <para>
  352. /// Overrides must call the base class method to ensure that the <see cref="Leave"/> event is raised. If the event
  353. /// is handled, the method should return <see langword="true"/>.
  354. /// </para>
  355. /// </remarks>
  356. public virtual bool OnLeave (View enteringView)
  357. {
  358. // BUGBUG: _hasFocus should ALWAYS be true when this method is called.
  359. if (!_hasFocus)
  360. {
  361. Debug.WriteLine ($"BUGBUG: HasFocus should be true when OnLeave is called - Leaving: {this} Entering: {enteringView}");
  362. //return true;
  363. }
  364. var args = new FocusEventArgs (this, enteringView);
  365. Leave?.Invoke (this, args);
  366. if (args.Handled)
  367. {
  368. return true;
  369. }
  370. return false;
  371. }
  372. /// <summary>
  373. /// Causes this view to be focused. All focusable views up the Superview hierarchy will also be focused.
  374. /// </summary>
  375. public void SetFocus ()
  376. {
  377. if (!CanBeVisible (this) || !Enabled)
  378. {
  379. if (HasFocus)
  380. {
  381. // If this view is focused, make it leave focus
  382. SetHasFocus (false, this);
  383. }
  384. return;
  385. }
  386. // Recursively set focus upwards in the view hierarchy
  387. if (SuperView is { })
  388. {
  389. SuperView.SetFocus (this);
  390. }
  391. else
  392. {
  393. SetFocus (this);
  394. }
  395. }
  396. /// <summary>
  397. /// INTERNAL API that gets or sets the focus direction for this view and all subviews.
  398. /// Setting this property will set the focus direction for all views up the SuperView hierarchy.
  399. /// </summary>
  400. internal NavigationDirection FocusDirection
  401. {
  402. get => SuperView?.FocusDirection ?? _focusDirection;
  403. set
  404. {
  405. if (SuperView is { })
  406. {
  407. SuperView.FocusDirection = value;
  408. }
  409. else
  410. {
  411. _focusDirection = value;
  412. }
  413. }
  414. }
  415. /// <summary>
  416. /// INTERNAL helper for calling <see cref="FocusDeepest"/> or <see cref="FocusLast"/> based on
  417. /// <see cref="FocusDirection"/>.
  418. /// FocusDirection is not public. This API is thus non-deterministic from a public API perspective.
  419. /// </summary>
  420. internal void RestoreFocus (TabBehavior? behavior)
  421. {
  422. if (Focused is null && _subviews?.Count > 0)
  423. {
  424. FocusDeepest (behavior, FocusDirection);
  425. }
  426. }
  427. /// <summary>
  428. /// Internal API that causes <paramref name="viewToEnterFocus"/> to enter focus.
  429. /// <paramref name="viewToEnterFocus"/> does not need to be a subview.
  430. /// Recursively sets focus DOWN in the view hierarchy.
  431. /// </summary>
  432. /// <param name="viewToEnterFocus"></param>
  433. private void SetFocus (View viewToEnterFocus)
  434. {
  435. if (viewToEnterFocus is null)
  436. {
  437. return;
  438. }
  439. if (!viewToEnterFocus.CanFocus || !viewToEnterFocus.Visible || !viewToEnterFocus.Enabled)
  440. {
  441. return;
  442. }
  443. // If viewToEnterFocus is already the focused view, don't do anything
  444. if (Focused?._hasFocus == true && Focused == viewToEnterFocus)
  445. {
  446. return;
  447. }
  448. // If a subview has focus and viewToEnterFocus is the focused view's superview OR viewToEnterFocus is this view,
  449. // then make viewToEnterFocus.HasFocus = true and return
  450. if ((Focused?._hasFocus == true && Focused?.SuperView == viewToEnterFocus) || viewToEnterFocus == this)
  451. {
  452. if (!viewToEnterFocus._hasFocus)
  453. {
  454. viewToEnterFocus._hasFocus = true;
  455. }
  456. return;
  457. }
  458. // Make sure that viewToEnterFocus is a subview of this view
  459. View c;
  460. for (c = viewToEnterFocus._superView; c != null; c = c._superView)
  461. {
  462. if (c == this)
  463. {
  464. break;
  465. }
  466. }
  467. if (c is null)
  468. {
  469. throw new ArgumentException (@$"The specified view {viewToEnterFocus} is not part of the hierarchy of {this}.");
  470. }
  471. // If a subview has focus, make it leave focus. This will leave focus up the hierarchy.
  472. Focused?.SetHasFocus (false, viewToEnterFocus);
  473. // make viewToEnterFocus Focused and enter focus
  474. View f = Focused;
  475. Focused = viewToEnterFocus;
  476. Focused?.SetHasFocus (true, f, true);
  477. // Recursively set focus down the view hierarchy
  478. if (SuperView is { })
  479. {
  480. SuperView.SetFocus (this);
  481. }
  482. else
  483. {
  484. // If there is no SuperView, then this is a top-level view
  485. SetFocus (this);
  486. }
  487. }
  488. /// <summary>
  489. /// Internal API that sets <see cref="HasFocus"/>. This method is called by <c>HasFocus_set</c> and other methods that
  490. /// need to set or remove focus from a view.
  491. /// </summary>
  492. /// <param name="newHasFocus">The new setting for <see cref="HasFocus"/>.</param>
  493. /// <param name="view">The view that will be gaining or losing focus.</param>
  494. /// <param name="force">
  495. /// <see langword="true"/> to force Enter/Leave on <paramref name="view"/> regardless of whether it
  496. /// already HasFocus or not.
  497. /// </param>
  498. /// <remarks>
  499. /// If <paramref name="newHasFocus"/> is <see langword="false"/> and there is a focused subview (<see cref="Focused"/>
  500. /// is not <see langword="null"/>),
  501. /// this method will recursively remove focus from any focused subviews of <see cref="Focused"/>.
  502. /// </remarks>
  503. private void SetHasFocus (bool newHasFocus, View view, bool force = false)
  504. {
  505. if (HasFocus != newHasFocus || force)
  506. {
  507. if (newHasFocus)
  508. {
  509. Debug.Assert (view is null || ApplicationNavigation.IsInHierarchy (SuperView, view));
  510. OnEnter (view);
  511. ApplicationNavigation.Focused = this;
  512. _hasFocus = true;
  513. }
  514. else
  515. {
  516. OnLeave (view);
  517. _hasFocus = false;
  518. }
  519. SetNeedsDisplay ();
  520. }
  521. // Remove focus down the chain of subviews if focus is removed
  522. if (!newHasFocus && Focused is { })
  523. {
  524. View f = Focused;
  525. //f.OnLeave (view);
  526. f.SetHasFocus (false, view, true);
  527. Focused = null;
  528. }
  529. }
  530. #region Tab/Focus Handling
  531. #nullable enable
  532. private List<View>? _tabIndexes;
  533. // TODO: This should be a get-only property?
  534. // BUGBUG: This returns an AsReadOnly list, but isn't declared as such.
  535. /// <summary>Gets a list of the subviews that are a <see cref="TabStop"/>.</summary>
  536. /// <value>The tabIndexes.</value>
  537. public IList<View> TabIndexes => _tabIndexes?.AsReadOnly () ?? _empty;
  538. /// <summary>
  539. /// Gets TabIndexes that are scoped to the specified behavior and direction. If behavior is null, all TabIndexes are returned.
  540. /// </summary>
  541. /// <param name="behavior"></param>
  542. /// <param name="direction"></param>
  543. /// <returns></returns>GetScopedTabIndexes
  544. private View [] GetScopedTabIndexes (TabBehavior? behavior, NavigationDirection direction)
  545. {
  546. IEnumerable<View>? indicies;
  547. if (behavior.HasValue)
  548. {
  549. indicies = _tabIndexes?.Where (v => v.TabStop == behavior && v is { CanFocus: true, Visible: true, Enabled: true });
  550. }
  551. else
  552. {
  553. indicies = _tabIndexes?.Where (v => v is { CanFocus: true, Visible: true, Enabled: true });
  554. }
  555. if (direction == NavigationDirection.Backward)
  556. {
  557. indicies = indicies?.Reverse ();
  558. }
  559. return indicies?.ToArray () ?? Array.Empty<View> ();
  560. }
  561. private int? _tabIndex; // null indicates the view has not yet been added to TabIndexes
  562. private int? _oldTabIndex;
  563. /// <summary>
  564. /// Indicates the order of the current <see cref="View"/> in <see cref="TabIndexes"/> list.
  565. /// </summary>
  566. /// <remarks>
  567. /// <para>
  568. /// If <see langword="null"/>, the view is not part of the tab order.
  569. /// </para>
  570. /// <para>
  571. /// On set, if <see cref="SuperView"/> is <see langword="null"/> or has not TabStops, <see cref="TabIndex"/> will
  572. /// be set to 0.
  573. /// </para>
  574. /// <para>
  575. /// On set, if <see cref="SuperView"/> has only one TabStop, <see cref="TabIndex"/> will be set to 0.
  576. /// </para>
  577. /// <para>
  578. /// See also <seealso cref="TabStop"/>.
  579. /// </para>
  580. /// </remarks>
  581. public int? TabIndex
  582. {
  583. get => _tabIndex;
  584. // TOOD: This should be a get-only property. Introduce SetTabIndex (int value) (or similar).
  585. set
  586. {
  587. // Once a view is in the tab order, it should not be removed from the tab order; set TabStop to NoStop instead.
  588. Debug.Assert (value >= 0);
  589. Debug.Assert (value is { });
  590. if (SuperView?._tabIndexes is null || SuperView?._tabIndexes.Count == 1)
  591. {
  592. // BUGBUG: Property setters should set the property to the value passed in and not have side effects.
  593. _tabIndex = 0;
  594. return;
  595. }
  596. if (_tabIndex == value && TabIndexes.IndexOf (this) == value)
  597. {
  598. return;
  599. }
  600. _tabIndex = value > SuperView!.TabIndexes.Count - 1 ? SuperView._tabIndexes.Count - 1 :
  601. value < 0 ? 0 : value;
  602. _tabIndex = GetGreatestTabIndexInSuperView ((int)_tabIndex);
  603. if (SuperView._tabIndexes.IndexOf (this) != _tabIndex)
  604. {
  605. // BUGBUG: we have to use _tabIndexes and not TabIndexes because TabIndexes returns is a read-only version of _tabIndexes
  606. SuperView._tabIndexes.Remove (this);
  607. SuperView._tabIndexes.Insert ((int)_tabIndex, this);
  608. ReorderSuperViewTabIndexes ();
  609. }
  610. }
  611. }
  612. /// <summary>
  613. /// Gets the greatest <see cref="TabIndex"/> of the <see cref="SuperView"/>'s <see cref="TabIndexes"/> that is less
  614. /// than or equal to <paramref name="idx"/>.
  615. /// </summary>
  616. /// <param name="idx"></param>
  617. /// <returns>The minimum of <paramref name="idx"/> and the <see cref="SuperView"/>'s <see cref="TabIndexes"/>.</returns>
  618. private int GetGreatestTabIndexInSuperView (int idx)
  619. {
  620. if (SuperView is null)
  621. {
  622. return 0;
  623. }
  624. var i = 0;
  625. foreach (View superViewTabStop in SuperView._tabIndexes)
  626. {
  627. if (superViewTabStop._tabIndex is null || superViewTabStop == this)
  628. {
  629. continue;
  630. }
  631. i++;
  632. }
  633. return Math.Min (i, idx);
  634. }
  635. /// <summary>
  636. /// Re-orders the <see cref="TabIndex"/>s of the views in the <see cref="SuperView"/>'s <see cref="TabIndexes"/>.
  637. /// </summary>
  638. private void ReorderSuperViewTabIndexes ()
  639. {
  640. if (SuperView is null)
  641. {
  642. return;
  643. }
  644. var i = 0;
  645. foreach (View superViewTabStop in SuperView._tabIndexes)
  646. {
  647. if (superViewTabStop._tabIndex is null)
  648. {
  649. continue;
  650. }
  651. superViewTabStop._tabIndex = i;
  652. i++;
  653. }
  654. }
  655. private TabBehavior? _tabStop;
  656. /// <summary>
  657. /// Gets or sets the behavior of <see cref="AdvanceFocus"/> for keyboard navigation.
  658. /// </summary>
  659. /// <remarks>
  660. /// <para>
  661. /// If <see langword="null"/> the tab stop has not been set and setting <see cref="CanFocus"/> to true will set it
  662. /// to
  663. /// <see cref="TabBehavior.TabStop"/>.
  664. /// </para>
  665. /// <para>
  666. /// TabStop is independent of <see cref="CanFocus"/>. If <see cref="CanFocus"/> is <see langword="false"/>, the
  667. /// view will not gain
  668. /// focus even if this property is set and vice-versa.
  669. /// </para>
  670. /// <para>
  671. /// The default <see cref="TabBehavior.TabStop"/> keys are <c>Key.Tab</c> and <c>Key>Tab.WithShift</c>.
  672. /// </para>
  673. /// <para>
  674. /// The default <see cref="TabBehavior.TabGroup"/> keys are <c>Key.Tab.WithCtrl</c> and <c>Key>Key.Tab.WithCtrl.WithShift</c>.
  675. /// </para>
  676. /// </remarks>
  677. public TabBehavior? TabStop
  678. {
  679. get => _tabStop;
  680. set
  681. {
  682. if (_tabStop == value)
  683. {
  684. return;
  685. }
  686. Debug.Assert (value is { });
  687. if (_tabStop is null && TabIndex is null)
  688. {
  689. // This view has not yet been added to TabIndexes (TabStop has not been set previously).
  690. TabIndex = GetGreatestTabIndexInSuperView (SuperView is { } ? SuperView._tabIndexes.Count : 0);
  691. }
  692. _tabStop = value;
  693. }
  694. }
  695. #endregion Tab/Focus Handling
  696. }