View.Navigation.cs 26 KB

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