View.Navigation.cs 27 KB

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