View.Navigation.cs 27 KB

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