View.Navigation.cs 28 KB

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