View.Navigation.cs 26 KB

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