View.Navigation.cs 29 KB

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