View.Navigation.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. // TODO: Combine FocusFirst and FocusLast into a single method that takes a direction parameter for less code duplication and easier testing.
  384. /// <summary>
  385. /// Focuses the first focusable view in <see cref="View.TabIndexes"/> if one exists. If there are no views in
  386. /// <see cref="View.TabIndexes"/> then the focus is set to the view itself.
  387. /// </summary>
  388. /// <param name="overlappedOnly">
  389. /// If <see langword="true"/>, only subviews where <see cref="Arrangement"/> has
  390. /// <see cref="ViewArrangement.Overlapped"/> set
  391. /// will be considered.
  392. /// </param>
  393. public void FocusFirst (bool overlappedOnly = false)
  394. {
  395. if (!CanBeVisible (this))
  396. {
  397. return;
  398. }
  399. if (_tabIndexes is null)
  400. {
  401. SuperView?.SetFocus (this);
  402. return;
  403. }
  404. foreach (View view in _tabIndexes.Where (v => !overlappedOnly || v.Arrangement.HasFlag (ViewArrangement.Overlapped)))
  405. {
  406. if (view.CanFocus && view._tabStop && view.Visible && view.Enabled)
  407. {
  408. SetFocus (view);
  409. return;
  410. }
  411. }
  412. }
  413. /// <summary>
  414. /// Focuses the last focusable view in <see cref="View.TabIndexes"/> if one exists. If there are no views in
  415. /// <see cref="View.TabIndexes"/> then the focus is set to the view itself.
  416. /// </summary>
  417. /// <param name="overlappedOnly">
  418. /// If <see langword="true"/>, only subviews where <see cref="Arrangement"/> has
  419. /// <see cref="ViewArrangement.Overlapped"/> set
  420. /// will be considered.
  421. /// </param>
  422. public void FocusLast (bool overlappedOnly = false)
  423. {
  424. if (!CanBeVisible (this))
  425. {
  426. return;
  427. }
  428. if (_tabIndexes is null)
  429. {
  430. SuperView?.SetFocus (this);
  431. return;
  432. }
  433. foreach (View view in _tabIndexes.Where (v => !overlappedOnly || v.Arrangement.HasFlag (ViewArrangement.Overlapped)).Reverse ())
  434. {
  435. if (view.CanFocus && view._tabStop && view.Visible && view.Enabled)
  436. {
  437. SetFocus (view);
  438. return;
  439. }
  440. }
  441. }
  442. /// <summary>
  443. /// Advances the focus to the next or previous view in <see cref="View.TabIndexes"/>, based on
  444. /// <paramref name="direction"/>.
  445. /// itself.
  446. /// </summary>
  447. /// <remarks>
  448. /// <para>
  449. /// If there is no next/previous view, the focus is set to the view itself.
  450. /// </para>
  451. /// </remarks>
  452. /// <param name="direction"></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)
  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))
  492. {
  493. // The subview changed which of it's subviews had focus
  494. return true;
  495. }
  496. Debug.Assert (w.HasFocus);
  497. // The subview has no subviews that can be next. Cache that we found a focused subview.
  498. focusedFound = true;
  499. continue;
  500. }
  501. // The subview does not have focus, but at least one other that can. Can this one be focused?
  502. if (focusedFound && w.CanFocus && w._tabStop && w.Visible && w.Enabled)
  503. {
  504. // Make Focused Leave
  505. Focused.SetHasFocus (false, w);
  506. //// If the focused view is overlapped don't focus on the next if it's not overlapped.
  507. //if (Focused.Arrangement.HasFlag (ViewArrangement.Overlapped)/* && !w.Arrangement.HasFlag (ViewArrangement.Overlapped)*/)
  508. //{
  509. // return false;
  510. //}
  511. //// If the focused view is not overlapped and the next is, skip it
  512. //if (!Focused.Arrangement.HasFlag (ViewArrangement.Overlapped) && w.Arrangement.HasFlag (ViewArrangement.Overlapped))
  513. //{
  514. // continue;
  515. //}
  516. switch (direction)
  517. {
  518. case NavigationDirection.Forward:
  519. w.FocusFirst ();
  520. break;
  521. case NavigationDirection.Backward:
  522. w.FocusLast ();
  523. break;
  524. }
  525. SetFocus (w);
  526. return true;
  527. }
  528. }
  529. if (Focused is { })
  530. {
  531. Focused.SetHasFocus (false, this);
  532. Focused = null;
  533. }
  534. return false;
  535. }
  536. #region Tab/Focus Handling
  537. private List<View> _tabIndexes;
  538. // TODO: This should be a get-only property?
  539. // BUGBUG: This returns an AsReadOnly list, but isn't declared as such.
  540. /// <summary>Gets a list of the subviews that are a <see cref="TabStop"/>.</summary>
  541. /// <value>The tabIndexes.</value>
  542. public IList<View> TabIndexes => _tabIndexes?.AsReadOnly () ?? _empty;
  543. // TODO: Change this to int? and use null to indicate the view is not in the tab order.
  544. private int _tabIndex = -1;
  545. private int _oldTabIndex;
  546. /// <summary>
  547. /// Indicates the index of the current <see cref="View"/> from the <see cref="TabIndexes"/> list. See also:
  548. /// <seealso cref="TabStop"/>.
  549. /// </summary>
  550. /// <remarks>
  551. /// <para>
  552. /// If the value is -1, the view is not part of the tab order.
  553. /// </para>
  554. /// <para>
  555. /// On set, if <see cref="CanFocus"/> is <see langword="false"/>, <see cref="TabIndex"/> will be set to -1.
  556. /// </para>
  557. /// <para>
  558. /// On set, if <see cref="SuperView"/> is <see langword="null"/> or has not TabStops, <see cref="TabIndex"/> will
  559. /// be set to 0.
  560. /// </para>
  561. /// <para>
  562. /// On set, if <see cref="SuperView"/> has only one TabStop, <see cref="TabIndex"/> will be set to 0.
  563. /// </para>
  564. /// </remarks>
  565. public int TabIndex
  566. {
  567. get => _tabIndex;
  568. set
  569. {
  570. if (!CanFocus)
  571. {
  572. // BUGBUG: Property setters should set the property to the value passed in and not have side effects.
  573. _tabIndex = -1;
  574. return;
  575. }
  576. if (SuperView?._tabIndexes is null || SuperView?._tabIndexes.Count == 1)
  577. {
  578. // BUGBUG: Property setters should set the property to the value passed in and not have side effects.
  579. _tabIndex = 0;
  580. return;
  581. }
  582. if (_tabIndex == value && TabIndexes.IndexOf (this) == value)
  583. {
  584. return;
  585. }
  586. _tabIndex = value > SuperView!.TabIndexes.Count - 1 ? SuperView._tabIndexes.Count - 1 :
  587. value < 0 ? 0 : value;
  588. _tabIndex = GetGreatestTabIndexInSuperView (_tabIndex);
  589. if (SuperView._tabIndexes.IndexOf (this) != _tabIndex)
  590. {
  591. // BUGBUG: we have to use _tabIndexes and not TabIndexes because TabIndexes returns is a read-only version of _tabIndexes
  592. SuperView._tabIndexes.Remove (this);
  593. SuperView._tabIndexes.Insert (_tabIndex, this);
  594. ReorderSuperViewTabIndexes ();
  595. }
  596. }
  597. }
  598. /// <summary>
  599. /// Gets the greatest <see cref="TabIndex"/> of the <see cref="SuperView"/>'s <see cref="TabIndexes"/> that is less
  600. /// than or equal to <paramref name="idx"/>.
  601. /// </summary>
  602. /// <param name="idx"></param>
  603. /// <returns>The minimum of <paramref name="idx"/> and the <see cref="SuperView"/>'s <see cref="TabIndexes"/>.</returns>
  604. private int GetGreatestTabIndexInSuperView (int idx)
  605. {
  606. var i = 0;
  607. foreach (View superViewTabStop in SuperView._tabIndexes)
  608. {
  609. if (superViewTabStop._tabIndex == -1 || superViewTabStop == this)
  610. {
  611. continue;
  612. }
  613. i++;
  614. }
  615. return Math.Min (i, idx);
  616. }
  617. /// <summary>
  618. /// Re-orders the <see cref="TabIndex"/>s of the views in the <see cref="SuperView"/>'s <see cref="TabIndexes"/>.
  619. /// </summary>
  620. private void ReorderSuperViewTabIndexes ()
  621. {
  622. var i = 0;
  623. foreach (View superViewTabStop in SuperView._tabIndexes)
  624. {
  625. if (superViewTabStop._tabIndex == -1)
  626. {
  627. continue;
  628. }
  629. superViewTabStop._tabIndex = i;
  630. i++;
  631. }
  632. }
  633. private bool _tabStop = true;
  634. /// <summary>
  635. /// Gets or sets whether the view is a stop-point for keyboard navigation of focus. Will be <see langword="true"/>
  636. /// only if <see cref="CanFocus"/> is <see langword="true"/>. Set to <see langword="false"/> to prevent the
  637. /// view from being a stop-point for keyboard navigation.
  638. /// </summary>
  639. /// <remarks>
  640. /// The default keyboard navigation keys are <c>Key.Tab</c> and <c>Key>Tab.WithShift</c>. These can be changed by
  641. /// modifying the key bindings (see <see cref="KeyBindings.Add(Key, Command[])"/>) of the SuperView.
  642. /// </remarks>
  643. public bool TabStop
  644. {
  645. get => _tabStop;
  646. set
  647. {
  648. if (_tabStop == value)
  649. {
  650. return;
  651. }
  652. _tabStop = CanFocus && value;
  653. }
  654. }
  655. #endregion Tab/Focus Handling
  656. }