View.Navigation.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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 (groupOnly);
  47. break;
  48. case NavigationDirection.Backward:
  49. FocusLast (groupOnly);
  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. // Wrap around
  77. if (SuperView is {})
  78. {
  79. if (direction == NavigationDirection.Forward)
  80. {
  81. return false;
  82. }
  83. else
  84. {
  85. return false;
  86. //SuperView.FocusFirst (groupOnly);
  87. }
  88. return true;
  89. }
  90. //next = index.Length - 1;
  91. }
  92. View view = index [next];
  93. // The subview does not have focus, but at least one other that can. Can this one be focused?
  94. if (view.CanFocus && view.Visible && view.Enabled)
  95. {
  96. // Make Focused Leave
  97. Focused.SetHasFocus (false, view);
  98. switch (direction)
  99. {
  100. case NavigationDirection.Forward:
  101. view.FocusFirst (false);
  102. break;
  103. case NavigationDirection.Backward:
  104. view.FocusLast (false);
  105. break;
  106. }
  107. SetFocus (view);
  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?.FocusFirstOrLast ();
  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);
  182. if (SuperView.Focused is null && Application.Current is { })
  183. {
  184. Application.Current.AdvanceFocus (NavigationDirection.Forward);
  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 first 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="groupOnly">
  243. /// If <see langword="true"/>, only subviews where <see cref="TabStop"/> is
  244. /// <see cref="TabBehavior.TabGroup"/> set
  245. /// will be considered.
  246. /// </param>
  247. public void FocusFirst (bool groupOnly = false)
  248. {
  249. if (!CanBeVisible (this))
  250. {
  251. return;
  252. }
  253. if (_tabIndexes is null)
  254. {
  255. SuperView?.SetFocus (this);
  256. return;
  257. }
  258. var indicies = GetScopedTabIndexes (groupOnly ? TabBehavior.TabGroup : TabBehavior.TabStop, NavigationDirection.Forward);
  259. if (indicies.Length > 0)
  260. {
  261. SetFocus (indicies [0]);
  262. }
  263. }
  264. /// <summary>
  265. /// Focuses the last focusable view in <see cref="View.TabIndexes"/> if one exists. If there are no views in
  266. /// <see cref="View.TabIndexes"/> then the focus is set to the view itself.
  267. /// </summary>
  268. /// <param name="groupOnly">
  269. /// If <see langword="true"/>, only subviews where <see cref="TabStop"/> is
  270. /// <see cref="TabBehavior.TabGroup"/> set
  271. /// will be considered.
  272. /// </param>
  273. public void FocusLast (bool groupOnly = false)
  274. {
  275. if (!CanBeVisible (this))
  276. {
  277. return;
  278. }
  279. if (_tabIndexes is null)
  280. {
  281. SuperView?.SetFocus (this);
  282. return;
  283. }
  284. var indicies = GetScopedTabIndexes (groupOnly ? TabBehavior.TabGroup : TabBehavior.TabStop, NavigationDirection.Forward);
  285. if (indicies.Length > 0)
  286. {
  287. SetFocus (indicies [^1]);
  288. }
  289. }
  290. /// <summary>
  291. /// Gets or sets whether this view has focus.
  292. /// </summary>
  293. /// <remarks>
  294. /// <para>
  295. /// Causes the <see cref="OnEnter"/> and <see cref="OnLeave"/> virtual methods (and <see cref="Enter"/> and
  296. /// <see cref="Leave"/> events to be raised) when the value changes.
  297. /// </para>
  298. /// <para>
  299. /// Setting this property to <see langword="false"/> will recursively set <see cref="HasFocus"/> to
  300. /// <see langword="false"/>
  301. /// for any focused subviews.
  302. /// </para>
  303. /// </remarks>
  304. public bool HasFocus
  305. {
  306. // Force the specified view to have focus
  307. set => SetHasFocus (value, this, true);
  308. get => _hasFocus;
  309. }
  310. /// <summary>Returns a value indicating if this View is currently on Top (Active)</summary>
  311. public bool IsCurrentTop => Application.Current == this;
  312. /// <summary>Raised when the view is losing (leaving) focus. Can be cancelled.</summary>
  313. /// <remarks>
  314. /// Raised by the <see cref="OnLeave"/> virtual method.
  315. /// </remarks>
  316. public event EventHandler<FocusEventArgs> Leave;
  317. /// <summary>
  318. /// Returns the most focused Subview in the chain of subviews (the leaf view that has the focus), or
  319. /// <see langword="null"/> if nothing is focused.
  320. /// </summary>
  321. /// <value>The most focused Subview.</value>
  322. public View MostFocused
  323. {
  324. get
  325. {
  326. if (Focused is null)
  327. {
  328. return null;
  329. }
  330. View most = Focused.MostFocused;
  331. if (most is { })
  332. {
  333. return most;
  334. }
  335. return Focused;
  336. }
  337. }
  338. /// <summary>Invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  339. /// <remarks>
  340. /// Raises the <see cref="CanFocusChanged"/> event.
  341. /// </remarks>
  342. public virtual void OnCanFocusChanged () { CanFocusChanged?.Invoke (this, EventArgs.Empty); }
  343. // BUGBUG: The focus API is poorly defined and implemented. It deeply intertwines the view hierarchy with the tab order.
  344. /// <summary>Invoked when this view is gaining focus (entering).</summary>
  345. /// <param name="leavingView">The view that is leaving focus.</param>
  346. /// <returns> <see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
  347. /// <remarks>
  348. /// <para>
  349. /// Overrides must call the base class method to ensure that the <see cref="Enter"/> event is raised. If the event
  350. /// is handled, the method should return <see langword="true"/>.
  351. /// </para>
  352. /// </remarks>
  353. public virtual bool OnEnter (View leavingView)
  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. var args = new FocusEventArgs (this, enteringView);
  375. Leave?.Invoke (this, args);
  376. if (args.Handled)
  377. {
  378. return true;
  379. }
  380. return false;
  381. }
  382. /// <summary>
  383. /// Causes this view to be focused. All focusable views up the Superview hierarchy will also be focused.
  384. /// </summary>
  385. public void SetFocus ()
  386. {
  387. if (!CanBeVisible (this) || !Enabled)
  388. {
  389. if (HasFocus)
  390. {
  391. // If this view is focused, make it leave focus
  392. SetHasFocus (false, this);
  393. }
  394. return;
  395. }
  396. // Recursively set focus upwards in the view hierarchy
  397. if (SuperView is { })
  398. {
  399. SuperView.SetFocus (this);
  400. }
  401. else
  402. {
  403. SetFocus (this);
  404. }
  405. }
  406. /// <summary>
  407. /// INTERNAL API that gets or sets the focus direction for this view and all subviews.
  408. /// Setting this property will set the focus direction for all views up the SuperView hierarchy.
  409. /// </summary>
  410. internal NavigationDirection FocusDirection
  411. {
  412. get => SuperView?.FocusDirection ?? _focusDirection;
  413. set
  414. {
  415. if (SuperView is { })
  416. {
  417. SuperView.FocusDirection = value;
  418. }
  419. else
  420. {
  421. _focusDirection = value;
  422. }
  423. }
  424. }
  425. /// <summary>
  426. /// INTERNAL helper for calling <see cref="FocusFirst"/> or <see cref="FocusLast"/> based on
  427. /// <see cref="FocusDirection"/>.
  428. /// FocusDirection is not public. This API is thus non-deterministic from a public API perspective.
  429. /// </summary>
  430. internal void FocusFirstOrLast ()
  431. {
  432. if (Focused is null && _subviews?.Count > 0)
  433. {
  434. if (FocusDirection == NavigationDirection.Forward)
  435. {
  436. FocusFirst ();
  437. }
  438. else
  439. {
  440. FocusLast ();
  441. }
  442. }
  443. }
  444. /// <summary>
  445. /// Internal API that causes <paramref name="viewToEnterFocus"/> to enter focus.
  446. /// <paramref name="viewToEnterFocus"/> does not need to be a subview.
  447. /// Recursively sets focus upwards in the view hierarchy.
  448. /// </summary>
  449. /// <param name="viewToEnterFocus"></param>
  450. private void SetFocus (View viewToEnterFocus)
  451. {
  452. if (viewToEnterFocus is null)
  453. {
  454. return;
  455. }
  456. if (!viewToEnterFocus.CanFocus || !viewToEnterFocus.Visible || !viewToEnterFocus.Enabled)
  457. {
  458. return;
  459. }
  460. // If viewToEnterFocus is already the focused view, don't do anything
  461. if (Focused?._hasFocus == true && Focused == viewToEnterFocus)
  462. {
  463. return;
  464. }
  465. // If a subview has focus and viewToEnterFocus is the focused view's superview OR viewToEnterFocus is this view,
  466. // then make viewToEnterFocus.HasFocus = true and return
  467. if ((Focused?._hasFocus == true && Focused?.SuperView == viewToEnterFocus) || viewToEnterFocus == this)
  468. {
  469. if (!viewToEnterFocus._hasFocus)
  470. {
  471. viewToEnterFocus._hasFocus = true;
  472. }
  473. return;
  474. }
  475. // Make sure that viewToEnterFocus is a subview of this view
  476. View c;
  477. for (c = viewToEnterFocus._superView; c != null; c = c._superView)
  478. {
  479. if (c == this)
  480. {
  481. break;
  482. }
  483. }
  484. if (c is null)
  485. {
  486. throw new ArgumentException (@$"The specified view {viewToEnterFocus} is not part of the hierarchy of {this}.");
  487. }
  488. // If a subview has focus, make it leave focus
  489. Focused?.SetHasFocus (false, viewToEnterFocus);
  490. // make viewToEnterFocus Focused and enter focus
  491. View f = Focused;
  492. Focused = viewToEnterFocus;
  493. Focused.SetHasFocus (true, f);
  494. // Ensure on either the first or last focusable subview of Focused
  495. // BUGBUG: With Groups, this means the previous focus is lost
  496. Focused.FocusFirstOrLast ();
  497. // Recursively set focus upwards in the view hierarchy
  498. if (SuperView is { })
  499. {
  500. SuperView.SetFocus (this);
  501. }
  502. else
  503. {
  504. // If there is no SuperView, then this is a top-level view
  505. SetFocus (this);
  506. }
  507. }
  508. /// <summary>
  509. /// Internal API that sets <see cref="HasFocus"/>. This method is called by <c>HasFocus_set</c> and other methods that
  510. /// need to set or remove focus from a view.
  511. /// </summary>
  512. /// <param name="newHasFocus">The new setting for <see cref="HasFocus"/>.</param>
  513. /// <param name="view">The view that will be gaining or losing focus.</param>
  514. /// <param name="force">
  515. /// <see langword="true"/> to force Enter/Leave on <paramref name="view"/> regardless of whether it
  516. /// already HasFocus or not.
  517. /// </param>
  518. /// <remarks>
  519. /// If <paramref name="newHasFocus"/> is <see langword="false"/> and there is a focused subview (<see cref="Focused"/>
  520. /// is not <see langword="null"/>),
  521. /// this method will recursively remove focus from any focused subviews of <see cref="Focused"/>.
  522. /// </remarks>
  523. private void SetHasFocus (bool newHasFocus, View view, bool force = false)
  524. {
  525. if (HasFocus != newHasFocus || force)
  526. {
  527. _hasFocus = newHasFocus;
  528. if (newHasFocus)
  529. {
  530. OnEnter (view);
  531. }
  532. else
  533. {
  534. OnLeave (view);
  535. }
  536. SetNeedsDisplay ();
  537. }
  538. // Remove focus down the chain of subviews if focus is removed
  539. if (!newHasFocus && Focused is { })
  540. {
  541. View f = Focused;
  542. f.OnLeave (view);
  543. f.SetHasFocus (false, view);
  544. Focused = null;
  545. }
  546. }
  547. #region Tab/Focus Handling
  548. #nullable enable
  549. private List<View> _tabIndexes;
  550. // TODO: This should be a get-only property?
  551. // BUGBUG: This returns an AsReadOnly list, but isn't declared as such.
  552. /// <summary>Gets a list of the subviews that are a <see cref="TabStop"/>.</summary>
  553. /// <value>The tabIndexes.</value>
  554. public IList<View> TabIndexes => _tabIndexes?.AsReadOnly () ?? _empty;
  555. private View [] GetScopedTabIndexes (TabBehavior behavior, NavigationDirection direction)
  556. {
  557. var indicies = _tabIndexes.Where (v => v.TabStop == behavior && v is { CanFocus: true, Visible: true, Enabled: true });
  558. if (direction == NavigationDirection.Backward)
  559. {
  560. indicies = indicies.Reverse ();
  561. }
  562. return indicies.ToArray ();
  563. }
  564. private int? _tabIndex; // null indicates the view has not yet been added to TabIndexes
  565. private int? _oldTabIndex;
  566. /// <summary>
  567. /// Indicates the order of the current <see cref="View"/> in <see cref="TabIndexes"/> list.
  568. /// </summary>
  569. /// <remarks>
  570. /// <para>
  571. /// If <see langword="null"/>, the view is not part of the tab order.
  572. /// </para>
  573. /// <para>
  574. /// On set, if <see cref="SuperView"/> is <see langword="null"/> or has not TabStops, <see cref="TabIndex"/> will
  575. /// be set to 0.
  576. /// </para>
  577. /// <para>
  578. /// On set, if <see cref="SuperView"/> has only one TabStop, <see cref="TabIndex"/> will be set to 0.
  579. /// </para>
  580. /// <para>
  581. /// See also <seealso cref="TabStop"/>.
  582. /// </para>
  583. /// </remarks>
  584. public int? TabIndex
  585. {
  586. get => _tabIndex;
  587. // TOOD: This should be a get-only property. Introduce SetTabIndex (int value) (or similar).
  588. set
  589. {
  590. // Once a view is in the tab order, it should not be removed from the tab order; set TabStop to NoStop instead.
  591. Debug.Assert (value >= 0);
  592. Debug.Assert (value is { });
  593. if (SuperView?._tabIndexes is null || SuperView?._tabIndexes.Count == 1)
  594. {
  595. // BUGBUG: Property setters should set the property to the value passed in and not have side effects.
  596. _tabIndex = 0;
  597. return;
  598. }
  599. if (_tabIndex == value && TabIndexes.IndexOf (this) == value)
  600. {
  601. return;
  602. }
  603. _tabIndex = value > SuperView!.TabIndexes.Count - 1 ? SuperView._tabIndexes.Count - 1 :
  604. value < 0 ? 0 : value;
  605. _tabIndex = GetGreatestTabIndexInSuperView ((int)_tabIndex);
  606. if (SuperView._tabIndexes.IndexOf (this) != _tabIndex)
  607. {
  608. // BUGBUG: we have to use _tabIndexes and not TabIndexes because TabIndexes returns is a read-only version of _tabIndexes
  609. SuperView._tabIndexes.Remove (this);
  610. SuperView._tabIndexes.Insert ((int)_tabIndex, this);
  611. ReorderSuperViewTabIndexes ();
  612. }
  613. }
  614. }
  615. /// <summary>
  616. /// Gets the greatest <see cref="TabIndex"/> of the <see cref="SuperView"/>'s <see cref="TabIndexes"/> that is less
  617. /// than or equal to <paramref name="idx"/>.
  618. /// </summary>
  619. /// <param name="idx"></param>
  620. /// <returns>The minimum of <paramref name="idx"/> and the <see cref="SuperView"/>'s <see cref="TabIndexes"/>.</returns>
  621. private int GetGreatestTabIndexInSuperView (int idx)
  622. {
  623. if (SuperView is null)
  624. {
  625. return 0;
  626. }
  627. var i = 0;
  628. foreach (View superViewTabStop in SuperView._tabIndexes)
  629. {
  630. if (superViewTabStop._tabIndex is null || superViewTabStop == this)
  631. {
  632. continue;
  633. }
  634. i++;
  635. }
  636. return Math.Min (i, idx);
  637. }
  638. /// <summary>
  639. /// Re-orders the <see cref="TabIndex"/>s of the views in the <see cref="SuperView"/>'s <see cref="TabIndexes"/>.
  640. /// </summary>
  641. private void ReorderSuperViewTabIndexes ()
  642. {
  643. if (SuperView is null)
  644. {
  645. return;
  646. }
  647. var i = 0;
  648. foreach (View superViewTabStop in SuperView._tabIndexes)
  649. {
  650. if (superViewTabStop._tabIndex is null)
  651. {
  652. continue;
  653. }
  654. superViewTabStop._tabIndex = i;
  655. i++;
  656. }
  657. }
  658. private TabBehavior? _tabStop;
  659. /// <summary>
  660. /// Gets or sets the behavior of <see cref="AdvanceFocus"/> for keyboard navigation.
  661. /// </summary>
  662. /// <remarks>
  663. /// <para>
  664. /// If <see langword="null"/> the tab stop has not been set and setting <see cref="CanFocus"/> to true will set it
  665. /// to
  666. /// <see cref="TabBehavior.TabStop"/>.
  667. /// </para>
  668. /// <para>
  669. /// TabStop is independent of <see cref="CanFocus"/>. If <see cref="CanFocus"/> is <see langword="false"/>, the
  670. /// view will not gain
  671. /// focus even if this property is set and vice-versa.
  672. /// </para>
  673. /// <para>
  674. /// The default <see cref="TabBehavior.TabStop"/> keys are <c>Key.Tab</c> and <c>Key>Tab.WithShift</c>.
  675. /// </para>
  676. /// <para>
  677. /// The default <see cref="TabBehavior.TabGroup"/> keys are <c>Key.Tab.WithCtrl</c> and <c>Key>Key.Tab.WithCtrl.WithShift</c>.
  678. /// </para>
  679. /// </remarks>
  680. public TabBehavior? TabStop
  681. {
  682. get => _tabStop;
  683. set
  684. {
  685. if (_tabStop == value)
  686. {
  687. return;
  688. }
  689. Debug.Assert (value is { });
  690. if (_tabStop is null && TabIndex is null)
  691. {
  692. // This view has not yet been added to TabIndexes (TabStop has not been set previously).
  693. TabIndex = GetGreatestTabIndexInSuperView (SuperView is { } ? SuperView._tabIndexes.Count : 0);
  694. }
  695. _tabStop = value;
  696. }
  697. }
  698. #endregion Tab/Focus Handling
  699. }