View.Navigation.cs 28 KB

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