View.Navigation.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 an attempt is made to make this view 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. /// <para>
  160. /// Changing this peroperty to <see langword="true"/> will cause <see cref="TabStop"/> to be set to <see cref="TabStop.TabStop"/>
  161. /// as a convenience. Changing this peroperty to <see langword="false"/> will have no effect on <see cref="TabStop"/>.
  162. /// </para>
  163. /// </remarks>
  164. public bool CanFocus
  165. {
  166. get => _canFocus;
  167. set
  168. {
  169. if (!_addingViewSoCanFocusAlsoUpdatesSuperView && IsInitialized && SuperView?.CanFocus == false && value)
  170. {
  171. throw new InvalidOperationException ("Cannot set CanFocus to true if the SuperView CanFocus is false!");
  172. }
  173. if (_canFocus == value)
  174. {
  175. return;
  176. }
  177. _canFocus = value;
  178. switch (_canFocus)
  179. {
  180. case false when _tabIndex > -1:
  181. // BUGBUG: This is a poor API design. Automatic behavior like this is non-obvious and should be avoided. Callers should adjust TabIndex explicitly.
  182. TabIndex = -1;
  183. break;
  184. case true when SuperView?.CanFocus == false && _addingViewSoCanFocusAlsoUpdatesSuperView:
  185. SuperView.CanFocus = true;
  186. break;
  187. }
  188. if (_canFocus && _tabIndex == -1)
  189. {
  190. // BUGBUG: This is a poor API design. Automatic behavior like this is non-obvious and should be avoided. Callers should adjust TabIndex explicitly.
  191. TabIndex = SuperView is { } ? SuperView._tabIndexes.IndexOf (this) : -1;
  192. }
  193. if (TabStop == TabStop.None && _canFocus)
  194. {
  195. TabStop = TabStop.TabStop;
  196. }
  197. if (!_canFocus && SuperView?.Focused == this)
  198. {
  199. SuperView.Focused = null;
  200. }
  201. if (!_canFocus && HasFocus)
  202. {
  203. SetHasFocus (false, this);
  204. SuperView?.FocusFirstOrLast ();
  205. // If EnsureFocus () didn't set focus to a view, focus the next focusable view in the application
  206. if (SuperView is { Focused: null })
  207. {
  208. SuperView.AdvanceFocus (NavigationDirection.Forward);
  209. if (SuperView.Focused is null && Application.Current is { })
  210. {
  211. Application.Current.AdvanceFocus (NavigationDirection.Forward);
  212. }
  213. ApplicationOverlapped.BringOverlappedTopToFront ();
  214. }
  215. }
  216. if (_subviews is { } && IsInitialized)
  217. {
  218. foreach (View view in _subviews)
  219. {
  220. if (view.CanFocus != value)
  221. {
  222. if (!value)
  223. {
  224. // Cache the old CanFocus and TabIndex so that they can be restored when CanFocus is changed back to true
  225. view._oldCanFocus = view.CanFocus;
  226. view._oldTabIndex = view._tabIndex;
  227. view.CanFocus = false;
  228. view._tabIndex = -1;
  229. }
  230. else
  231. {
  232. if (_addingViewSoCanFocusAlsoUpdatesSuperView)
  233. {
  234. view._addingViewSoCanFocusAlsoUpdatesSuperView = true;
  235. }
  236. // Restore the old CanFocus and TabIndex to the values they held before CanFocus was set to false
  237. view.CanFocus = view._oldCanFocus;
  238. view._tabIndex = view._oldTabIndex;
  239. view._addingViewSoCanFocusAlsoUpdatesSuperView = false;
  240. }
  241. }
  242. }
  243. if (this is Toplevel && Application.Current!.Focused != this)
  244. {
  245. ApplicationOverlapped.BringOverlappedTopToFront ();
  246. }
  247. }
  248. OnCanFocusChanged ();
  249. SetNeedsDisplay ();
  250. }
  251. }
  252. /// <summary>Raised when <see cref="CanFocus"/> has been changed.</summary>
  253. /// <remarks>
  254. /// Raised by the <see cref="OnCanFocusChanged"/> virtual method.
  255. /// </remarks>
  256. public event EventHandler CanFocusChanged;
  257. /// <summary>Invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  258. /// <remarks>
  259. /// Raises the <see cref="CanFocusChanged"/> event.
  260. /// </remarks>
  261. public virtual void OnCanFocusChanged () { CanFocusChanged?.Invoke (this, EventArgs.Empty); }
  262. /// <summary>Returns the currently focused Subview inside this view, or <see langword="null"/> if nothing is focused.</summary>
  263. /// <value>The currently focused Subview.</value>
  264. public View Focused { get; private set; }
  265. /// <summary>
  266. /// Returns the most focused Subview in the chain of subviews (the leaf view that has the focus), or
  267. /// <see langword="null"/> if nothing is focused.
  268. /// </summary>
  269. /// <value>The most focused Subview.</value>
  270. public View MostFocused
  271. {
  272. get
  273. {
  274. if (Focused is null)
  275. {
  276. return null;
  277. }
  278. View most = Focused.MostFocused;
  279. if (most is { })
  280. {
  281. return most;
  282. }
  283. return Focused;
  284. }
  285. }
  286. /// <summary>
  287. /// Internal API that causes <paramref name="viewToEnterFocus"/> to enter focus.
  288. /// <paramref name="viewToEnterFocus"/> does not need to be a subview.
  289. /// Recursively sets focus upwards in the view hierarchy.
  290. /// </summary>
  291. /// <param name="viewToEnterFocus"></param>
  292. private void SetFocus (View viewToEnterFocus)
  293. {
  294. if (viewToEnterFocus is null)
  295. {
  296. return;
  297. }
  298. if (!viewToEnterFocus.CanFocus || !viewToEnterFocus.Visible || !viewToEnterFocus.Enabled)
  299. {
  300. return;
  301. }
  302. // If viewToEnterFocus is already the focused view, don't do anything
  303. if (Focused?._hasFocus == true && Focused == viewToEnterFocus)
  304. {
  305. return;
  306. }
  307. // If a subview has focus and viewToEnterFocus is the focused view's superview OR viewToEnterFocus is this view,
  308. // then make viewToEnterFocus.HasFocus = true and return
  309. if ((Focused?._hasFocus == true && Focused?.SuperView == viewToEnterFocus) || viewToEnterFocus == this)
  310. {
  311. if (!viewToEnterFocus._hasFocus)
  312. {
  313. viewToEnterFocus._hasFocus = true;
  314. }
  315. return;
  316. }
  317. // Make sure that viewToEnterFocus is a subview of this view
  318. View c;
  319. for (c = viewToEnterFocus._superView; c != null; c = c._superView)
  320. {
  321. if (c == this)
  322. {
  323. break;
  324. }
  325. }
  326. if (c is null)
  327. {
  328. throw new ArgumentException (@$"The specified view {viewToEnterFocus} is not part of the hierarchy of {this}.");
  329. }
  330. // If a subview has focus, make it leave focus
  331. Focused?.SetHasFocus (false, viewToEnterFocus);
  332. // make viewToEnterFocus Focused and enter focus
  333. View f = Focused;
  334. Focused = viewToEnterFocus;
  335. Focused.SetHasFocus (true, f);
  336. // Ensure on either the first or last focusable subview of Focused
  337. Focused.FocusFirstOrLast ();
  338. // Recursively set focus upwards in the view hierarchy
  339. if (SuperView is { })
  340. {
  341. SuperView.SetFocus (this);
  342. }
  343. else
  344. {
  345. // If there is no SuperView, then this is a top-level view
  346. SetFocus (this);
  347. }
  348. }
  349. /// <summary>
  350. /// Causes this view to be focused. All focusable views up the Superview hierarchy will also be focused.
  351. /// </summary>
  352. public void SetFocus ()
  353. {
  354. if (!CanBeVisible (this) || !Enabled)
  355. {
  356. if (HasFocus)
  357. {
  358. // If this view is focused, make it leave focus
  359. SetHasFocus (false, this);
  360. }
  361. return;
  362. }
  363. // Recursively set focus upwards in the view hierarchy
  364. if (SuperView is { })
  365. {
  366. SuperView.SetFocus (this);
  367. }
  368. else
  369. {
  370. SetFocus (this);
  371. }
  372. }
  373. /// <summary>
  374. /// INTERNAL helper for calling <see cref="FocusFirst"/> or <see cref="FocusLast"/> based on
  375. /// <see cref="FocusDirection"/>.
  376. /// FocusDirection is not public. This API is thus non-deterministic from a public API perspective.
  377. /// </summary>
  378. internal void FocusFirstOrLast ()
  379. {
  380. if (Focused is null && _subviews?.Count > 0)
  381. {
  382. if (FocusDirection == NavigationDirection.Forward)
  383. {
  384. FocusFirst ();
  385. }
  386. else
  387. {
  388. FocusLast ();
  389. }
  390. }
  391. }
  392. /// <summary>
  393. /// Focuses the first focusable view in <see cref="View.TabIndexes"/> if one exists. If there are no views in
  394. /// <see cref="View.TabIndexes"/> then the focus is set to the view itself.
  395. /// </summary>
  396. /// <param name="overlappedOnly">
  397. /// If <see langword="true"/>, only subviews where <see cref="Arrangement"/> has
  398. /// <see cref="ViewArrangement.Overlapped"/> set
  399. /// will be considered.
  400. /// </param>
  401. public void FocusFirst (bool overlappedOnly = false)
  402. {
  403. if (!CanBeVisible (this))
  404. {
  405. return;
  406. }
  407. if (_tabIndexes is null)
  408. {
  409. SuperView?.SetFocus (this);
  410. return;
  411. }
  412. foreach (View view in _tabIndexes.Where (v => !overlappedOnly || v.Arrangement.HasFlag (ViewArrangement.Overlapped)))
  413. {
  414. if (view.CanFocus && view.TabStop.HasFlag (TabStop.TabStop) && view.Visible && view.Enabled)
  415. {
  416. SetFocus (view);
  417. return;
  418. }
  419. }
  420. }
  421. /// <summary>
  422. /// Focuses the last focusable view in <see cref="View.TabIndexes"/> if one exists. If there are no views in
  423. /// <see cref="View.TabIndexes"/> then the focus is set to the view itself.
  424. /// </summary>
  425. /// <param name="overlappedOnly">
  426. /// If <see langword="true"/>, only subviews where <see cref="Arrangement"/> has
  427. /// <see cref="ViewArrangement.Overlapped"/> set
  428. /// will be considered.
  429. /// </param>
  430. public void FocusLast (bool overlappedOnly = false)
  431. {
  432. if (!CanBeVisible (this))
  433. {
  434. return;
  435. }
  436. if (_tabIndexes is null)
  437. {
  438. SuperView?.SetFocus (this);
  439. return;
  440. }
  441. foreach (View view in _tabIndexes.Where (v => !overlappedOnly || v.Arrangement.HasFlag (ViewArrangement.Overlapped)).Reverse ())
  442. {
  443. if (view.CanFocus && view.TabStop.HasFlag (TabStop.TabStop) && view.Visible && view.Enabled)
  444. {
  445. SetFocus (view);
  446. return;
  447. }
  448. }
  449. }
  450. /// <summary>
  451. /// Advances the focus to the next or previous view in <see cref="View.TabIndexes"/>, based on
  452. /// <paramref name="direction"/>.
  453. /// itself.
  454. /// </summary>
  455. /// <remarks>
  456. /// <para>
  457. /// If there is no next/previous view, the focus is set to the view itself.
  458. /// </para>
  459. /// </remarks>
  460. /// <param name="direction"></param>
  461. /// <param name="acrossGroupOrOverlapped">If <see langword="true"/> will advance into ...</param>
  462. /// <returns>
  463. /// <see langword="true"/> if focus was changed to another subview (or stayed on this one), <see langword="false"/>
  464. /// otherwise.
  465. /// </returns>
  466. public bool AdvanceFocus (NavigationDirection direction, bool acrossGroupOrOverlapped = false)
  467. {
  468. if (!CanBeVisible (this))
  469. {
  470. return false;
  471. }
  472. FocusDirection = direction;
  473. if (TabIndexes is null || TabIndexes.Count == 0)
  474. {
  475. return false;
  476. }
  477. if (Focused is null)
  478. {
  479. switch (direction)
  480. {
  481. case NavigationDirection.Forward:
  482. FocusFirst ();
  483. break;
  484. case NavigationDirection.Backward:
  485. FocusLast ();
  486. break;
  487. default:
  488. throw new ArgumentOutOfRangeException (nameof (direction), direction, null);
  489. }
  490. return Focused is { };
  491. }
  492. var focusedFound = false;
  493. foreach (View w in direction == NavigationDirection.Forward
  494. ? TabIndexes.ToArray ()
  495. : TabIndexes.ToArray ().Reverse ())
  496. {
  497. if (w.HasFocus)
  498. {
  499. // A subview has focus, tell *it* to FocusNext
  500. if (w.AdvanceFocus (direction, acrossGroupOrOverlapped))
  501. {
  502. // The subview changed which of it's subviews had focus
  503. return true;
  504. }
  505. else
  506. {
  507. if (acrossGroupOrOverlapped && Arrangement.HasFlag (ViewArrangement.Overlapped))
  508. {
  509. return false;
  510. }
  511. }
  512. //Debug.Assert (w.HasFocus);
  513. if (w.Focused is null)
  514. {
  515. // No next focusable view was found.
  516. if (w.Arrangement.HasFlag (ViewArrangement.Overlapped))
  517. {
  518. // Keep focus w/in w
  519. return false;
  520. }
  521. }
  522. // The subview has no subviews that can be next. Cache that we found a focused subview.
  523. focusedFound = true;
  524. continue;
  525. }
  526. // The subview does not have focus, but at least one other that can. Can this one be focused?
  527. if (focusedFound && w.CanFocus && w.TabStop.HasFlag (TabStop.TabStop) && w.Visible && w.Enabled)
  528. {
  529. // Make Focused Leave
  530. Focused.SetHasFocus (false, w);
  531. // If the focused view is overlapped don't focus on the next if it's not overlapped.
  532. //if (acrossGroupOrOverlapped && Focused.Arrangement.HasFlag (ViewArrangement.Overlapped)/* && !w.Arrangement.HasFlag (ViewArrangement.Overlapped)*/)
  533. //{
  534. // return false;
  535. //}
  536. // If the focused view is not overlapped and the next is, skip it
  537. if (!acrossGroupOrOverlapped && !Focused.Arrangement.HasFlag (ViewArrangement.Overlapped) && w.Arrangement.HasFlag (ViewArrangement.Overlapped))
  538. {
  539. continue;
  540. }
  541. switch (direction)
  542. {
  543. case NavigationDirection.Forward:
  544. w.FocusFirst ();
  545. break;
  546. case NavigationDirection.Backward:
  547. w.FocusLast ();
  548. break;
  549. }
  550. SetFocus (w);
  551. return true;
  552. }
  553. }
  554. if (Focused is { })
  555. {
  556. // Leave
  557. Focused.SetHasFocus (false, this);
  558. // Signal that nothing is focused, and callers should try a peer-subview
  559. Focused = null;
  560. }
  561. return false;
  562. }
  563. #region Tab/Focus Handling
  564. private List<View> _tabIndexes;
  565. // TODO: This should be a get-only property?
  566. // BUGBUG: This returns an AsReadOnly list, but isn't declared as such.
  567. /// <summary>Gets a list of the subviews that are a <see cref="TabStop"/>.</summary>
  568. /// <value>The tabIndexes.</value>
  569. public IList<View> TabIndexes => _tabIndexes?.AsReadOnly () ?? _empty;
  570. // TODO: Change this to int? and use null to indicate the view is not in the tab order.
  571. // BUGBUG: It is confused to have both TabStop and TabIndex = -1.
  572. private int _tabIndex = -1;
  573. private int _oldTabIndex;
  574. /// <summary>
  575. /// Indicates the order of the current <see cref="View"/> in <see cref="TabIndexes"/> list.
  576. /// </summary>
  577. /// <remarks>
  578. /// <para>
  579. /// If the value is -1, the view is not part of the tab order.
  580. /// </para>
  581. /// <para>
  582. /// On set, if <see cref="CanFocus"/> is <see langword="false"/>, <see cref="TabIndex"/> will be set to -1.
  583. /// </para>
  584. /// <para>
  585. /// On set, if <see cref="SuperView"/> is <see langword="null"/> or has not TabStops, <see cref="TabIndex"/> will
  586. /// be set to 0.
  587. /// </para>
  588. /// <para>
  589. /// On set, if <see cref="SuperView"/> has only one TabStop, <see cref="TabIndex"/> will be set to 0.
  590. /// </para>
  591. /// <para>
  592. /// See also <seealso cref="TabStop"/>.
  593. /// </para>
  594. /// </remarks>
  595. public int TabIndex
  596. {
  597. get => _tabIndex;
  598. // TOOD: This should be a get-only property. Introduce SetTabIndex (int value) (or similar).
  599. set
  600. {
  601. // BUGBUG: Property setters should set the property to the value passed in and not have side effects.
  602. if (!CanFocus)
  603. {
  604. // BUGBUG: Property setters should set the property to the value passed in and not have side effects.
  605. // BUGBUG: TabIndex = -1 should not be used to indicate that the view is not in the tab order. That's what TabStop is for.
  606. _tabIndex = -1;
  607. return;
  608. }
  609. // BUGBUG: Property setters should set the property to the value passed in and not have side effects.
  610. if (SuperView?._tabIndexes is null || SuperView?._tabIndexes.Count == 1)
  611. {
  612. // BUGBUG: Property setters should set the property to the value passed in and not have side effects.
  613. _tabIndex = 0;
  614. return;
  615. }
  616. if (_tabIndex == value && TabIndexes.IndexOf (this) == value)
  617. {
  618. return;
  619. }
  620. _tabIndex = value > SuperView!.TabIndexes.Count - 1 ? SuperView._tabIndexes.Count - 1 :
  621. value < 0 ? 0 : value;
  622. _tabIndex = GetGreatestTabIndexInSuperView (_tabIndex);
  623. if (SuperView._tabIndexes.IndexOf (this) != _tabIndex)
  624. {
  625. // BUGBUG: we have to use _tabIndexes and not TabIndexes because TabIndexes returns is a read-only version of _tabIndexes
  626. SuperView._tabIndexes.Remove (this);
  627. SuperView._tabIndexes.Insert (_tabIndex, this);
  628. ReorderSuperViewTabIndexes ();
  629. }
  630. }
  631. }
  632. /// <summary>
  633. /// Gets the greatest <see cref="TabIndex"/> of the <see cref="SuperView"/>'s <see cref="TabIndexes"/> that is less
  634. /// than or equal to <paramref name="idx"/>.
  635. /// </summary>
  636. /// <param name="idx"></param>
  637. /// <returns>The minimum of <paramref name="idx"/> and the <see cref="SuperView"/>'s <see cref="TabIndexes"/>.</returns>
  638. private int GetGreatestTabIndexInSuperView (int idx)
  639. {
  640. var i = 0;
  641. foreach (View superViewTabStop in SuperView._tabIndexes)
  642. {
  643. if (superViewTabStop._tabIndex == -1 || superViewTabStop == this)
  644. {
  645. continue;
  646. }
  647. i++;
  648. }
  649. return Math.Min (i, idx);
  650. }
  651. /// <summary>
  652. /// Re-orders the <see cref="TabIndex"/>s of the views in the <see cref="SuperView"/>'s <see cref="TabIndexes"/>.
  653. /// </summary>
  654. private void ReorderSuperViewTabIndexes ()
  655. {
  656. var i = 0;
  657. foreach (View superViewTabStop in SuperView._tabIndexes)
  658. {
  659. if (superViewTabStop._tabIndex == -1)
  660. {
  661. continue;
  662. }
  663. superViewTabStop._tabIndex = i;
  664. i++;
  665. }
  666. }
  667. /// <summary>
  668. /// Gets or sets whether the view is a stop-point for keyboard navigation.
  669. /// </summary>
  670. /// <remarks>
  671. /// <para>
  672. /// TabStop is independent of <see cref="CanFocus"/>. If <see cref="CanFocus"/> is <see langword="false"/>, the view will not gain
  673. /// focus even if this property is set and vice-versa.
  674. /// </para>
  675. /// <para>
  676. /// The default keyboard navigation keys are <c>Key.Tab</c> and <c>Key>Tab.WithShift</c>. These can be changed by
  677. /// modifying the key bindings (see <see cref="KeyBindings.Add(Key, Command[])"/>) of the SuperView.
  678. /// </para>
  679. /// </remarks>
  680. public TabStop TabStop { get; set; } = TabStop.None;
  681. #endregion Tab/Focus Handling
  682. }