View.Navigation.cs 28 KB

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