View.Navigation.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. /// <summary>Exposed as `internal` for unit tests. Indicates focus navigation direction.</summary>
  8. public enum NavigationDirection
  9. {
  10. /// <summary>Navigate forward.</summary>
  11. Forward,
  12. /// <summary>Navigate backwards.</summary>
  13. Backward
  14. }
  15. // BUGBUG: The focus API is poorly defined and implemented. It deeply intertwines the view hierarchy with the tab order.
  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>
  286. /// Internal API that causes <paramref name="viewToEnterFocus"/> to enter focus.
  287. /// <paramref name="viewToEnterFocus"/> does not need to be a subview.
  288. /// Recursively sets focus upwards in the view hierarchy.
  289. /// </summary>
  290. /// <param name="viewToEnterFocus"></param>
  291. private void SetFocus (View viewToEnterFocus)
  292. {
  293. if (viewToEnterFocus is null)
  294. {
  295. return;
  296. }
  297. if (!viewToEnterFocus.CanFocus || !viewToEnterFocus.Visible || !viewToEnterFocus.Enabled)
  298. {
  299. return;
  300. }
  301. // If viewToEnterFocus is already the focused view, don't do anything
  302. if (Focused?._hasFocus == true && Focused == viewToEnterFocus)
  303. {
  304. return;
  305. }
  306. // If a subview has focus and viewToEnterFocus is the focused view's superview OR viewToEnterFocus is this view,
  307. // then make viewToEnterFocus.HasFocus = true and return
  308. if ((Focused?._hasFocus == true && Focused?.SuperView == viewToEnterFocus) || viewToEnterFocus == this)
  309. {
  310. if (!viewToEnterFocus._hasFocus)
  311. {
  312. viewToEnterFocus._hasFocus = true;
  313. }
  314. return;
  315. }
  316. // Make sure that viewToEnterFocus is a subview of this view
  317. View c;
  318. for (c = viewToEnterFocus._superView; c != null; c = c._superView)
  319. {
  320. if (c == this)
  321. {
  322. break;
  323. }
  324. }
  325. if (c is null)
  326. {
  327. throw new ArgumentException (@$"The specified view {viewToEnterFocus} is not part of the hierarchy of {this}.");
  328. }
  329. // If a subview has focus, make it leave focus
  330. Focused?.SetHasFocus (false, viewToEnterFocus);
  331. // make viewToEnterFocus Focused and enter focus
  332. View f = Focused;
  333. Focused = viewToEnterFocus;
  334. Focused.SetHasFocus (true, f);
  335. // Ensure on either the first or last focusable subview of Focused
  336. Focused.FocusFirstOrLast ();
  337. // Recursively set focus upwards in the view hierarchy
  338. if (SuperView is { })
  339. {
  340. SuperView.SetFocus (this);
  341. }
  342. else
  343. {
  344. // If there is no SuperView, then this is a top-level view
  345. SetFocus (this);
  346. }
  347. }
  348. /// <summary>
  349. /// Causes this view to be focused. All focusable views up the Superview hierarchy will also be focused.
  350. /// </summary>
  351. public void SetFocus ()
  352. {
  353. if (!CanBeVisible (this) || !Enabled)
  354. {
  355. if (HasFocus)
  356. {
  357. // If this view is focused, make it leave focus
  358. SetHasFocus (false, this);
  359. }
  360. return;
  361. }
  362. // Recursively set focus upwards in the view hierarchy
  363. if (SuperView is { })
  364. {
  365. SuperView.SetFocus (this);
  366. }
  367. else
  368. {
  369. SetFocus (this);
  370. }
  371. }
  372. /// <summary>
  373. /// INTERNAL helper for calling <see cref="FocusFirst"/> or <see cref="FocusLast"/> based on
  374. /// <see cref="FocusDirection"/>.
  375. /// FocusDirection is not public. This API is thus non-deterministic from a public API perspective.
  376. /// </summary>
  377. internal void FocusFirstOrLast ()
  378. {
  379. if (Focused is null && _subviews?.Count > 0)
  380. {
  381. if (FocusDirection == NavigationDirection.Forward)
  382. {
  383. FocusFirst ();
  384. }
  385. else
  386. {
  387. FocusLast ();
  388. }
  389. }
  390. }
  391. // TODO: Combine FocusFirst and FocusLast into a single method that takes a direction parameter for less code duplication and easier testing.
  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 <see cref="ViewArrangement.Overlapped"/> set
  398. /// will be considered.
  399. /// </param>
  400. public void FocusFirst (bool overlappedOnly = false)
  401. {
  402. if (!CanBeVisible (this))
  403. {
  404. return;
  405. }
  406. if (_tabIndexes is null)
  407. {
  408. SuperView?.SetFocus (this);
  409. return;
  410. }
  411. foreach (View view in _tabIndexes.Where (v => !overlappedOnly || v.Arrangement.HasFlag (ViewArrangement.Overlapped)))
  412. {
  413. if (view.CanFocus && view._tabStop && view.Visible && view.Enabled)
  414. {
  415. SetFocus (view);
  416. return;
  417. }
  418. }
  419. }
  420. /// <summary>
  421. /// Focuses the last focusable view in <see cref="View.TabIndexes"/> if one exists. If there are no views in
  422. /// <see cref="View.TabIndexes"/> then the focus is set to the view itself.
  423. /// </summary>
  424. /// <param name="overlappedOnly">
  425. /// If <see langword="true"/>, only subviews where <see cref="Arrangement"/> has <see cref="ViewArrangement.Overlapped"/> set
  426. /// will be considered.
  427. /// </param>
  428. public void FocusLast (bool overlappedOnly = false)
  429. {
  430. if (!CanBeVisible (this))
  431. {
  432. return;
  433. }
  434. if (_tabIndexes is null)
  435. {
  436. SuperView?.SetFocus (this);
  437. return;
  438. }
  439. foreach (View view in _tabIndexes.Where (v => !overlappedOnly || v.Arrangement.HasFlag (ViewArrangement.Overlapped)).Reverse ())
  440. {
  441. if (view.CanFocus && view._tabStop && view.Visible && view.Enabled)
  442. {
  443. SetFocus (view);
  444. return;
  445. }
  446. }
  447. }
  448. /// <summary>
  449. /// Advances the focus to the next or previous view in <see cref="View.TabIndexes"/>, based on <paramref name="direction"/>.
  450. /// itself.
  451. /// </summary>
  452. /// <remarks>
  453. /// <para>
  454. /// If there is no next/previous view, the focus is set to the view itself.
  455. /// </para>
  456. /// </remarks>
  457. /// <param name="direction"></param>
  458. /// <returns><see langword="true"/> if focus was changed to another subview (or stayed on this one), <see langword="false"/> otherwise.</returns>
  459. public bool AdvanceFocus (NavigationDirection direction)
  460. {
  461. return direction switch
  462. {
  463. NavigationDirection.Forward => FocusNext (),
  464. NavigationDirection.Backward => FocusPrev (),
  465. _ => false
  466. };
  467. }
  468. /// <summary>
  469. /// Focuses the next view in <see cref="View.TabIndexes"/>. If there is no next view, the focus is set to the view
  470. /// itself.
  471. /// </summary>
  472. /// <returns><see langword="true"/> if focus was changed to another subview (or stayed on this one), <see langword="false"/> otherwise.</returns>
  473. public bool FocusNext ()
  474. {
  475. if (!CanBeVisible (this))
  476. {
  477. return false;
  478. }
  479. FocusDirection = NavigationDirection.Forward;
  480. if (TabIndexes is null || TabIndexes.Count == 0)
  481. {
  482. return false;
  483. }
  484. if (Focused is null)
  485. {
  486. FocusFirst ();
  487. return Focused is { };
  488. }
  489. int focusedIdx = -1;
  490. for (var i = 0; i < TabIndexes.Count; i++)
  491. {
  492. View w = TabIndexes [i];
  493. if (w.HasFocus)
  494. {
  495. // A subview has focus, tell *it* to FocusNext
  496. if (w.FocusNext ())
  497. {
  498. // The subview changed which of it's subviews had focus
  499. return true;
  500. }
  501. Debug.Assert (w.HasFocus);
  502. // The subview has no subviews that can be next. Cache that we found a focused subview.
  503. focusedIdx = i;
  504. continue;
  505. }
  506. // The subview does not have focus, but at least one other that can. Can this one be focused?
  507. if (focusedIdx != -1 && w.CanFocus && w._tabStop && w.Visible && w.Enabled)
  508. {
  509. // Make Focused Leave
  510. Focused.SetHasFocus (false, w);
  511. //// If the focused view is overlapped don't focus on the next if it's not overlapped.
  512. //if (Focused.Arrangement.HasFlag (ViewArrangement.Overlapped)/* && !w.Arrangement.HasFlag (ViewArrangement.Overlapped)*/)
  513. //{
  514. // return false;
  515. //}
  516. //// If the focused view is not overlapped and the next is, skip it
  517. //if (!Focused.Arrangement.HasFlag (ViewArrangement.Overlapped) && w.Arrangement.HasFlag (ViewArrangement.Overlapped))
  518. //{
  519. // continue;
  520. //}
  521. // QUESTION: Why do we check these again here?
  522. if (w.CanFocus && w._tabStop && w.Visible && w.Enabled)
  523. {
  524. w.FocusFirst ();
  525. }
  526. SetFocus (w);
  527. return true;
  528. }
  529. }
  530. // There's no next view in tab indexes.
  531. if (Focused is { })
  532. {
  533. // Leave
  534. Focused.SetHasFocus (false, this);
  535. //if (Focused.Arrangement.HasFlag (ViewArrangement.Overlapped))
  536. //{
  537. // FocusFirst (true);
  538. // return true;
  539. //}
  540. // Signal to caller no next view was found; this will enable it to make a peer
  541. // or view up the superview hierarchy have focus.
  542. Focused = null;
  543. }
  544. return false;
  545. }
  546. /// <summary>
  547. /// Focuses the previous view in <see cref="View.TabIndexes"/>. If there is no previous view, the focus is set to the
  548. /// view itself.
  549. /// </summary>
  550. /// <returns><see langword="true"/> if previous was focused, <see langword="false"/> otherwise.</returns>
  551. public bool FocusPrev ()
  552. {
  553. if (!CanBeVisible (this))
  554. {
  555. return false;
  556. }
  557. FocusDirection = NavigationDirection.Backward;
  558. if (TabIndexes is null || TabIndexes.Count == 0)
  559. {
  560. return false;
  561. }
  562. if (Focused is null)
  563. {
  564. FocusLast ();
  565. return Focused != null;
  566. }
  567. int focusedIdx = -1;
  568. for (int i = TabIndexes.Count; i > 0;)
  569. {
  570. i--;
  571. View w = TabIndexes [i];
  572. if (w.HasFocus)
  573. {
  574. if (w.FocusPrev ())
  575. {
  576. return true;
  577. }
  578. focusedIdx = i;
  579. continue;
  580. }
  581. if (w.CanFocus && focusedIdx != -1 && w._tabStop && w.Visible && w.Enabled)
  582. {
  583. Focused.SetHasFocus (false, w);
  584. // If the focused view is overlapped don't focus on the next if it's not overlapped.
  585. if (Focused.Arrangement.HasFlag (ViewArrangement.Overlapped) && !w.Arrangement.HasFlag (ViewArrangement.Overlapped))
  586. {
  587. FocusLast (true);
  588. return true;
  589. }
  590. // If the focused view is not overlapped and the next is, skip it
  591. if (!Focused.Arrangement.HasFlag (ViewArrangement.Overlapped) && w.Arrangement.HasFlag (ViewArrangement.Overlapped))
  592. {
  593. continue;
  594. }
  595. if (w.CanFocus && w._tabStop && w.Visible && w.Enabled)
  596. {
  597. w.FocusLast ();
  598. }
  599. SetFocus (w);
  600. return true;
  601. }
  602. }
  603. // There's no prev view in tab indexes.
  604. if (Focused is { })
  605. {
  606. // Leave Focused
  607. Focused.SetHasFocus (false, this);
  608. if (Focused.Arrangement.HasFlag (ViewArrangement.Overlapped))
  609. {
  610. FocusLast (true);
  611. return true;
  612. }
  613. // Signal to caller no next view was found
  614. Focused = null;
  615. }
  616. return false;
  617. }
  618. #region Tab/Focus Handling
  619. private List<View> _tabIndexes;
  620. // TODO: This should be a get-only property?
  621. // BUGBUG: This returns an AsReadOnly list, but isn't declared as such.
  622. /// <summary>Gets a list of the subviews that are a <see cref="TabStop"/>.</summary>
  623. /// <value>The tabIndexes.</value>
  624. public IList<View> TabIndexes => _tabIndexes?.AsReadOnly () ?? _empty;
  625. // TODO: Change this to int? and use null to indicate the view is not in the tab order.
  626. private int _tabIndex = -1;
  627. private int _oldTabIndex;
  628. /// <summary>
  629. /// Indicates the index of the current <see cref="View"/> from the <see cref="TabIndexes"/> list. See also:
  630. /// <seealso cref="TabStop"/>.
  631. /// </summary>
  632. /// <remarks>
  633. /// <para>
  634. /// If the value is -1, the view is not part of the tab order.
  635. /// </para>
  636. /// <para>
  637. /// On set, if <see cref="CanFocus"/> is <see langword="false"/>, <see cref="TabIndex"/> will be set to -1.
  638. /// </para>
  639. /// <para>
  640. /// On set, if <see cref="SuperView"/> is <see langword="null"/> or has not TabStops, <see cref="TabIndex"/> will
  641. /// be set to 0.
  642. /// </para>
  643. /// <para>
  644. /// On set, if <see cref="SuperView"/> has only one TabStop, <see cref="TabIndex"/> will be set to 0.
  645. /// </para>
  646. /// </remarks>
  647. public int TabIndex
  648. {
  649. get => _tabIndex;
  650. set
  651. {
  652. if (!CanFocus)
  653. {
  654. // BUGBUG: Property setters should set the property to the value passed in and not have side effects.
  655. _tabIndex = -1;
  656. return;
  657. }
  658. if (SuperView?._tabIndexes is null || SuperView?._tabIndexes.Count == 1)
  659. {
  660. // BUGBUG: Property setters should set the property to the value passed in and not have side effects.
  661. _tabIndex = 0;
  662. return;
  663. }
  664. if (_tabIndex == value && TabIndexes.IndexOf (this) == value)
  665. {
  666. return;
  667. }
  668. _tabIndex = value > SuperView!.TabIndexes.Count - 1 ? SuperView._tabIndexes.Count - 1 :
  669. value < 0 ? 0 : value;
  670. _tabIndex = GetGreatestTabIndexInSuperView (_tabIndex);
  671. if (SuperView._tabIndexes.IndexOf (this) != _tabIndex)
  672. {
  673. // BUGBUG: we have to use _tabIndexes and not TabIndexes because TabIndexes returns is a read-only version of _tabIndexes
  674. SuperView._tabIndexes.Remove (this);
  675. SuperView._tabIndexes.Insert (_tabIndex, this);
  676. ReorderSuperViewTabIndexes ();
  677. }
  678. }
  679. }
  680. /// <summary>
  681. /// Gets the greatest <see cref="TabIndex"/> of the <see cref="SuperView"/>'s <see cref="TabIndexes"/> that is less
  682. /// than or equal to <paramref name="idx"/>.
  683. /// </summary>
  684. /// <param name="idx"></param>
  685. /// <returns>The minimum of <paramref name="idx"/> and the <see cref="SuperView"/>'s <see cref="TabIndexes"/>.</returns>
  686. private int GetGreatestTabIndexInSuperView (int idx)
  687. {
  688. var i = 0;
  689. foreach (View superViewTabStop in SuperView._tabIndexes)
  690. {
  691. if (superViewTabStop._tabIndex == -1 || superViewTabStop == this)
  692. {
  693. continue;
  694. }
  695. i++;
  696. }
  697. return Math.Min (i, idx);
  698. }
  699. /// <summary>
  700. /// Re-orders the <see cref="TabIndex"/>s of the views in the <see cref="SuperView"/>'s <see cref="TabIndexes"/>.
  701. /// </summary>
  702. private void ReorderSuperViewTabIndexes ()
  703. {
  704. var i = 0;
  705. foreach (View superViewTabStop in SuperView._tabIndexes)
  706. {
  707. if (superViewTabStop._tabIndex == -1)
  708. {
  709. continue;
  710. }
  711. superViewTabStop._tabIndex = i;
  712. i++;
  713. }
  714. }
  715. private bool _tabStop = true;
  716. /// <summary>
  717. /// Gets or sets whether the view is a stop-point for keyboard navigation of focus. Will be <see langword="true"/>
  718. /// only if <see cref="CanFocus"/> is <see langword="true"/>. Set to <see langword="false"/> to prevent the
  719. /// view from being a stop-point for keyboard navigation.
  720. /// </summary>
  721. /// <remarks>
  722. /// The default keyboard navigation keys are <c>Key.Tab</c> and <c>Key>Tab.WithShift</c>. These can be changed by
  723. /// modifying the key bindings (see <see cref="KeyBindings.Add(Key, Command[])"/>) of the SuperView.
  724. /// </remarks>
  725. public bool TabStop
  726. {
  727. get => _tabStop;
  728. set
  729. {
  730. if (_tabStop == value)
  731. {
  732. return;
  733. }
  734. _tabStop = CanFocus && value;
  735. }
  736. }
  737. #endregion Tab/Focus Handling
  738. }