View.Navigation.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. #nullable enable
  2. using System.Diagnostics;
  3. namespace Terminal.Gui;
  4. public partial class View // Focus and cross-view navigation management (TabStop, TabIndex, etc...)
  5. {
  6. private bool _canFocus;
  7. /// <summary>
  8. /// Advances the focus to the next or previous view in the focus chain, based on
  9. /// <paramref name="direction"/>.
  10. /// itself.
  11. /// </summary>
  12. /// <remarks>
  13. /// <para>
  14. /// If there is no next/previous view to advance to, the focus is set to the view itself.
  15. /// </para>
  16. /// </remarks>
  17. /// <param name="direction"></param>
  18. /// <param name="behavior"></param>
  19. /// <returns>
  20. /// <see langword="true"/> if focus was changed to another subview (or stayed on this one), <see langword="false"/>
  21. /// otherwise.
  22. /// </returns>
  23. public bool AdvanceFocus (NavigationDirection direction, TabBehavior? behavior)
  24. {
  25. if (!CanBeVisible (this)) // TODO: is this check needed?
  26. {
  27. return false;
  28. }
  29. View? focused = Focused;
  30. if (focused is { } && focused.AdvanceFocus (direction, behavior))
  31. {
  32. return true;
  33. }
  34. // AdvanceFocus did not advance - do we wrap, or move up to the superview?
  35. View [] focusChain = GetSubviewFocusChain (direction, behavior);
  36. if (focusChain.Length == 0)
  37. {
  38. return false;
  39. }
  40. // Special case TabGroup
  41. if (behavior == TabBehavior.TabGroup)
  42. {
  43. if (direction == NavigationDirection.Forward && focused == focusChain [^1] && SuperView is null)
  44. {
  45. // We're at the top of the focus chain. Go back down the focus chain and focus the first TabGroup
  46. View [] views = GetSubviewFocusChain (NavigationDirection.Forward, TabBehavior.TabGroup);
  47. if (views.Length > 0)
  48. {
  49. View [] subViews = views [0].GetSubviewFocusChain (NavigationDirection.Forward, TabBehavior.TabStop);
  50. if (subViews.Length > 0)
  51. {
  52. if (subViews [0].SetFocus ())
  53. {
  54. return true;
  55. }
  56. }
  57. }
  58. }
  59. if (direction == NavigationDirection.Backward && focused == focusChain [0])
  60. {
  61. // We're at the bottom of the focus chain
  62. View [] views = GetSubviewFocusChain (NavigationDirection.Forward, TabBehavior.TabGroup);
  63. if (views.Length > 0)
  64. {
  65. View [] subViews = views [^1].GetSubviewFocusChain (NavigationDirection.Forward, TabBehavior.TabStop);
  66. if (subViews.Length > 0)
  67. {
  68. if (subViews [0].SetFocus ())
  69. {
  70. return true;
  71. }
  72. }
  73. }
  74. }
  75. }
  76. int focusedIndex = focusChain.IndexOf (Focused); // Will return -1 if Focused can't be found or is null
  77. var next = 0; // Assume we wrap to start of the focus chain
  78. if (focusedIndex < focusChain.Length - 1)
  79. {
  80. // We're moving w/in the subviews
  81. next = focusedIndex + 1;
  82. }
  83. else
  84. {
  85. // Determine if focus should remain in this focus chain, or move to the superview's focus chain
  86. if (SuperView is { })
  87. {
  88. // If we are TabStop, and we have at least one other focusable peer, move to the SuperView's chain
  89. if (TabStop == TabBehavior.TabStop && SuperView is { } && SuperView.GetSubviewFocusChain (direction, behavior).Length > 1)
  90. {
  91. return false;
  92. }
  93. // TabGroup is special-cased.
  94. if (focused?.TabStop == TabBehavior.TabGroup)
  95. {
  96. if (SuperView?.GetSubviewFocusChain (direction, TabBehavior.TabGroup)?.Length > 0)
  97. {
  98. // Our superview has a TabGroup subview; signal we couldn't move so we nav out to it
  99. return false;
  100. }
  101. }
  102. }
  103. }
  104. View view = focusChain [next];
  105. if (view.HasFocus)
  106. {
  107. // We could not advance
  108. return view == this;
  109. }
  110. // The subview does not have focus, but at least one other that can. Can this one be focused?
  111. (bool focusSet, bool _) = view.SetHasFocusTrue (Focused);
  112. return focusSet;
  113. }
  114. /// <summary>Gets or sets a value indicating whether this <see cref="View"/> can be focused.</summary>
  115. /// <remarks>
  116. /// <para>
  117. /// <see cref="SuperView"/> must also have <see cref="CanFocus"/> set to <see langword="true"/>.
  118. /// </para>
  119. /// <para>
  120. /// When set to <see langword="false"/>, if an attempt is made to make this view focused, the focus will be set to
  121. /// the next focusable view.
  122. /// </para>
  123. /// <para>
  124. /// When set to <see langword="false"/>, the value of <see cref="CanFocus"/> for all
  125. /// subviews will be cached so that when <see cref="CanFocus"/> is set back to <see langword="true"/>, the subviews
  126. /// will be restored to their previous values.
  127. /// </para>
  128. /// <para>
  129. /// Changing this property to <see langword="true"/> will cause <see cref="TabStop"/> to be set to
  130. /// <see cref="TabBehavior.TabStop"/>" as a convenience. Changing this property to
  131. /// <see langword="false"/> will have no effect on <see cref="TabStop"/>.
  132. /// </para>
  133. /// </remarks>
  134. public bool CanFocus
  135. {
  136. get => _canFocus;
  137. set
  138. {
  139. if (_canFocus == value)
  140. {
  141. return;
  142. }
  143. _canFocus = value;
  144. if (TabStop is null && _canFocus)
  145. {
  146. TabStop = TabBehavior.TabStop;
  147. }
  148. if (!_canFocus && HasFocus)
  149. {
  150. // If CanFocus is set to false and this view has focus, make it leave focus
  151. HasFocus = false;
  152. }
  153. if (_canFocus && !HasFocus && Visible && SuperView is { } && SuperView.Focused is null)
  154. {
  155. // If CanFocus is set to true and this view does not have focus, make it enter focus
  156. SetFocus ();
  157. }
  158. OnCanFocusChanged ();
  159. }
  160. }
  161. /// <summary>Raised when <see cref="CanFocus"/> has been changed.</summary>
  162. /// <remarks>
  163. /// Raised by the <see cref="OnCanFocusChanged"/> virtual method.
  164. /// </remarks>
  165. public event EventHandler? CanFocusChanged;
  166. /// <summary>
  167. /// Focuses the deepest focusable Subview if one exists. If there are no focusable Subviews then the focus is set to
  168. /// the view itself.
  169. /// </summary>
  170. /// <param name="direction"></param>
  171. /// <param name="behavior"></param>
  172. /// <returns><see langword="true"/> if a subview other than this was focused.</returns>
  173. public bool FocusDeepest (NavigationDirection direction, TabBehavior? behavior)
  174. {
  175. View? deepest = FindDeepestFocusableView (direction, behavior);
  176. if (deepest is { })
  177. {
  178. return deepest.SetFocus ();
  179. }
  180. return SetFocus ();
  181. }
  182. /// <summary>Gets the currently focused Subview of this view, or <see langword="null"/> if nothing is focused.</summary>
  183. public View? Focused
  184. {
  185. get { return Subviews.FirstOrDefault (v => v.HasFocus); }
  186. }
  187. /// <summary>Returns a value indicating if this View is currently on Top (Active)</summary>
  188. public bool IsCurrentTop => Application.Current == this;
  189. /// <summary>
  190. /// Returns the most focused Subview down the subview-hierarchy.
  191. /// </summary>
  192. /// <value>The most focused Subview, or <see langword="null"/> if no Subview is focused.</value>
  193. public View? MostFocused
  194. {
  195. get
  196. {
  197. // TODO: Remove this API. It's duplicative of Application.Navigation.GetFocused.
  198. if (Focused is null)
  199. {
  200. return null;
  201. }
  202. View? most = Focused.MostFocused;
  203. if (most is { })
  204. {
  205. return most;
  206. }
  207. return Focused;
  208. }
  209. }
  210. /// <summary>Invoked when the <see cref="CanFocus"/> property from a view is changed.</summary>
  211. /// <remarks>
  212. /// Raises the <see cref="CanFocusChanged"/> event.
  213. /// </remarks>
  214. public virtual void OnCanFocusChanged () { CanFocusChanged?.Invoke (this, EventArgs.Empty); }
  215. /// <summary>
  216. /// INTERNAL API to restore focus to the subview that had focus before this view lost focus.
  217. /// </summary>
  218. /// <returns>
  219. /// Returns true if focus was restored to a subview, false otherwise.
  220. /// </returns>
  221. internal bool RestoreFocus ()
  222. {
  223. if (Focused is null && _subviews?.Count > 0)
  224. {
  225. if (_previouslyMostFocused is { })
  226. {
  227. return _previouslyMostFocused.SetFocus ();
  228. }
  229. return false;
  230. }
  231. return false;
  232. }
  233. private View? FindDeepestFocusableView (NavigationDirection direction, TabBehavior? behavior)
  234. {
  235. View [] indicies = GetSubviewFocusChain (direction, behavior);
  236. foreach (View v in indicies)
  237. {
  238. return v.FindDeepestFocusableView (direction, behavior);
  239. }
  240. return null;
  241. }
  242. #region HasFocus
  243. // Backs `HasFocus` and is the ultimate source of truth whether a View has focus or not.
  244. private bool _hasFocus;
  245. /// <summary>
  246. /// Gets or sets whether this view has focus.
  247. /// </summary>
  248. /// <remarks>
  249. /// <para>
  250. /// Only Views that are visible, enabled, and have <see cref="CanFocus"/> set to <see langword="true"/> are
  251. /// focusable. If
  252. /// these conditions are not met when this property is set to <see langword="true"/> <see cref="HasFocus"/> will
  253. /// not change.
  254. /// </para>
  255. /// <para>
  256. /// Setting this property causes the <see cref="OnHasFocusChanging"/> and <see cref="OnHasFocusChanged"/> virtual
  257. /// methods (and <see cref="HasFocusChanging"/> and
  258. /// <see cref="HasFocusChanged"/> events to be raised). If the event is cancelled, <see cref="HasFocus"/> will not
  259. /// be changed.
  260. /// </para>
  261. /// <para>
  262. /// Setting this property to <see langword="true"/> will recursively set <see cref="HasFocus"/> to
  263. /// <see langword="true"/> for all SuperViews up the hierarchy.
  264. /// </para>
  265. /// <para>
  266. /// Setting this property to <see langword="true"/> will cause the subview furthest down the hierarchy that is
  267. /// focusable to also gain focus (as long as <see cref="TabStop"/>
  268. /// </para>
  269. /// <para>
  270. /// Setting this property to <see langword="false"/> will cause <see cref="AdvanceFocus"/> to set
  271. /// the focus on the next view to be focused.
  272. /// </para>
  273. /// </remarks>
  274. public bool HasFocus
  275. {
  276. set
  277. {
  278. if (HasFocus == value)
  279. {
  280. return;
  281. }
  282. if (value)
  283. {
  284. // NOTE: If Application.Navigation is null, we pass null to FocusChanging. For unit tests.
  285. (bool focusSet, bool _) = SetHasFocusTrue (Application.Navigation?.GetFocused ());
  286. if (focusSet)
  287. {
  288. // The change happened
  289. // HasFocus is now true
  290. }
  291. }
  292. else
  293. {
  294. SetHasFocusFalse (null);
  295. }
  296. }
  297. get => _hasFocus;
  298. }
  299. /// <summary>
  300. /// Causes this view to be focused. Calling this method has the same effect as setting <see cref="HasFocus"/> to
  301. /// <see langword="true"/> but with the added benefit of returning a value indicating whether the focus was set.
  302. /// </summary>
  303. public bool SetFocus ()
  304. {
  305. (bool focusSet, bool _) = SetHasFocusTrue (Application.Navigation?.GetFocused ());
  306. return focusSet;
  307. }
  308. /// <summary>
  309. /// Caches the most focused subview when this view is losing focus. This is used by <see cref="RestoreFocus"/>.
  310. /// </summary>
  311. private View? _previouslyMostFocused;
  312. /// <summary>
  313. /// INTERNAL: Called when focus is going to change to this view. This method is called by <see cref="SetFocus"/> and
  314. /// other methods that
  315. /// set or remove focus from a view.
  316. /// </summary>
  317. /// <param name="previousFocusedView">
  318. /// The previously focused view. If <see langword="null"/> there is no previously focused
  319. /// view.
  320. /// </param>
  321. /// <param name="traversingUp"></param>
  322. /// <returns><see langword="true"/> if <see cref="HasFocus"/> was changed to <see langword="true"/>.</returns>
  323. /// <exception cref="InvalidOperationException"></exception>
  324. private (bool focusSet, bool cancelled) SetHasFocusTrue (View? previousFocusedView, bool traversingUp = false)
  325. {
  326. Debug.Assert (ApplicationNavigation.IsInHierarchy (SuperView, this));
  327. // Pre-conditions
  328. if (_hasFocus)
  329. {
  330. return (false, false);
  331. }
  332. if (CanFocus && SuperView is { CanFocus: false })
  333. {
  334. Debug.WriteLine ($@"WARNING: Attempt to FocusChanging where SuperView.CanFocus == false. {this}");
  335. return (false, false);
  336. }
  337. if (!CanBeVisible (this) || !Enabled)
  338. {
  339. return (false, false);
  340. }
  341. if (!CanFocus)
  342. {
  343. return (false, false);
  344. }
  345. bool previousValue = HasFocus;
  346. bool cancelled = NotifyFocusChanging (false, true, previousFocusedView, this);
  347. if (cancelled)
  348. {
  349. return (false, true);
  350. }
  351. // Make sure superviews up the superview hierarchy have focus.
  352. // Any of them may cancel gaining focus. In which case we need to back out.
  353. if (SuperView is { HasFocus: false } sv)
  354. {
  355. (bool focusSet, bool svCancelled) = sv.SetHasFocusTrue (previousFocusedView, true);
  356. if (!focusSet)
  357. {
  358. return (false, svCancelled);
  359. }
  360. }
  361. // Are we an Adornment?
  362. if (this is Adornment adornment)
  363. {
  364. if (adornment.Parent is { HasFocus: false } parent)
  365. {
  366. (bool focusSet, bool parentCancelled) = parent.SetHasFocusTrue (previousFocusedView, true);
  367. if (!focusSet)
  368. {
  369. return (false, parentCancelled);
  370. }
  371. }
  372. }
  373. if (_hasFocus)
  374. {
  375. // Something else beat us to the change (likely a FocusChanged handler).
  376. return (true, false);
  377. }
  378. // By setting _hasFocus to true we definitively change HasFocus for this view.
  379. // Get whatever peer has focus, if any
  380. View? focusedPeer = SuperView?.Focused;
  381. if (focusedPeer is null)
  382. {
  383. // Are we an Adornment?
  384. if (this is Adornment ad)
  385. {
  386. focusedPeer = ad.Parent?.Focused;
  387. }
  388. }
  389. _hasFocus = true;
  390. // Ensure that the peer loses focus
  391. focusedPeer?.SetHasFocusFalse (this, true);
  392. if (!traversingUp)
  393. {
  394. // Restore focus to the previously most focused subview in the subview-hierarchy
  395. if (!RestoreFocus ())
  396. {
  397. // Couldn't restore focus, so use Advance to navigate to the next focusable subview
  398. if (!AdvanceFocus (NavigationDirection.Forward, null))
  399. {
  400. // Couldn't advance, so we're the most focused view in the application
  401. _previouslyMostFocused = null;
  402. Application.Navigation?.SetFocused (this);
  403. }
  404. }
  405. }
  406. if (previousFocusedView is { HasFocus: true } && Subviews.Contains (previousFocusedView))
  407. {
  408. previousFocusedView.SetHasFocusFalse (this);
  409. }
  410. if (Arrangement.HasFlag (ViewArrangement.Overlapped))
  411. {
  412. SuperView?.MoveSubviewToEnd (this);
  413. }
  414. NotifyFocusChanged (HasFocus, previousFocusedView, this);
  415. // Post-conditions - prove correctness
  416. if (HasFocus == previousValue)
  417. {
  418. throw new InvalidOperationException ("NotifyFocusChanging was not cancelled and the HasFocus value did not change.");
  419. }
  420. return (true, false);
  421. }
  422. private bool NotifyFocusChanging (bool currentHasFocus, bool newHasFocus, View? currentFocused, View? newFocused)
  423. {
  424. // Call the virtual method
  425. if (OnHasFocusChanging (currentHasFocus, newHasFocus, currentFocused, newFocused))
  426. {
  427. // The event was cancelled
  428. return true;
  429. }
  430. var args = new HasFocusEventArgs (currentHasFocus, newHasFocus, currentFocused, newFocused);
  431. HasFocusChanging?.Invoke (this, args);
  432. if (args.Cancel)
  433. {
  434. // The event was cancelled
  435. return true;
  436. }
  437. return false;
  438. }
  439. /// <summary>
  440. /// Invoked when <see cref="View.HasFocus"/> is about to change. This method is called before the
  441. /// <see cref="HasFocusChanging"/> event is raised.
  442. /// </summary>
  443. /// <remarks>
  444. /// <para>
  445. /// Use <see cref="OnHasFocusChanged"/> to be notified after the focus has changed.
  446. /// </para>
  447. /// </remarks>
  448. /// <param name="currentHasFocus">The current value of <see cref="View.HasFocus"/>.</param>
  449. /// <param name="newHasFocus">The value <see cref="View.HasFocus"/> will have if the focus change happens.</param>
  450. /// <param name="currentFocused">The view that is currently Focused. May be <see langword="null"/>.</param>
  451. /// <param name="newFocused">The view that will be focused. May be <see langword="null"/>.</param>
  452. /// <returns>
  453. /// <see langword="true"/>, if the change to <see cref="View.HasFocus"/> is to be cancelled, <see langword="false"/>
  454. /// otherwise.
  455. /// </returns>
  456. protected virtual bool OnHasFocusChanging (bool currentHasFocus, bool newHasFocus, View? currentFocused, View? newFocused) { return false; }
  457. /// <summary>
  458. /// Raised when <see cref="View.HasFocus"/> is about to change.
  459. /// </summary>
  460. /// <remarks>
  461. /// <para>
  462. /// Cancel the event to prevent the focus from changing.
  463. /// </para>
  464. /// <para>
  465. /// Use <see cref="HasFocusChanged"/> to be notified after the focus has changed.
  466. /// </para>
  467. /// </remarks>
  468. public event EventHandler<HasFocusEventArgs>? HasFocusChanging;
  469. /// <summary>
  470. /// Called when this view should stop being focused.
  471. /// </summary>
  472. /// <param name="newFocusedView">
  473. /// The new focused view. If <see langword="null"/> it is not known which view will be
  474. /// focused.
  475. /// </param>
  476. /// <param name="traversingDown">
  477. /// Set to true to indicate method is being called recurively, traversing down the focus
  478. /// chain.
  479. /// </param>
  480. /// <exception cref="InvalidOperationException"></exception>
  481. private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false)
  482. {
  483. // Pre-conditions
  484. if (!_hasFocus)
  485. {
  486. throw new InvalidOperationException ("SetHasFocusFalse should not be called if the view does not have focus.");
  487. }
  488. // If newFocusedVew is null, we need to find the view that should get focus, and SetFocus on it.
  489. if (!traversingDown && newFocusedView is null)
  490. {
  491. if (SuperView?._previouslyMostFocused is { })
  492. {
  493. if (SuperView?._previouslyMostFocused != this)
  494. {
  495. SuperView?._previouslyMostFocused?.SetFocus ();
  496. // The above will cause SetHasFocusFalse, so we can return
  497. return;
  498. }
  499. newFocusedView = SuperView?._previouslyMostFocused;
  500. }
  501. if (SuperView is { })
  502. {
  503. if (SuperView.AdvanceFocus (NavigationDirection.Forward, TabStop))
  504. {
  505. // The above will cause SetHasFocusFalse, so we can return
  506. return;
  507. }
  508. newFocusedView = SuperView;
  509. }
  510. // Are we an Adornment?
  511. if (this is Adornment ad)
  512. {
  513. if (ad.Parent is {})
  514. {
  515. if (ad.Parent.RestoreFocus ())
  516. {
  517. // The above will cause SetHasFocusFalse, so we can return
  518. return;
  519. }
  520. newFocusedView = ad.Parent;
  521. }
  522. }
  523. if (Application.Navigation is { } && Application.Current is { })
  524. {
  525. // Temporarily ensure this view can't get focus
  526. bool prevCanFocus = _canFocus;
  527. _canFocus = false;
  528. bool restoredFocus = Application.Current!.RestoreFocus ();
  529. _canFocus = prevCanFocus;
  530. if (restoredFocus)
  531. {
  532. // The above caused SetHasFocusFalse, so we can return
  533. return;
  534. }
  535. }
  536. // No other focusable view to be found. Just "leave" us...
  537. }
  538. // Before we can leave focus, we need to make sure that all views down the subview-hierarchy have left focus.
  539. View? mostFocused = MostFocused;
  540. if (mostFocused is { } && (newFocusedView is null || mostFocused != newFocusedView))
  541. {
  542. // Start at the bottom and work our way up to us
  543. View? bottom = mostFocused;
  544. while (bottom is { } && bottom != this)
  545. {
  546. if (bottom.HasFocus)
  547. {
  548. bottom.SetHasFocusFalse (newFocusedView, true);
  549. }
  550. bottom = bottom.SuperView;
  551. }
  552. _previouslyMostFocused = mostFocused;
  553. }
  554. bool previousValue = HasFocus;
  555. // Note, can't be cancelled.
  556. NotifyFocusChanging (HasFocus, !HasFocus, newFocusedView, this);
  557. // Get whatever peer has focus, if any
  558. View? focusedPeer = SuperView?.Focused;
  559. _hasFocus = false;
  560. if (Application.Navigation is { })
  561. {
  562. View? appFocused = Application.Navigation.GetFocused ();
  563. if (appFocused is { } || appFocused == this)
  564. {
  565. Application.Navigation.SetFocused (newFocusedView ?? SuperView);
  566. }
  567. }
  568. NotifyFocusChanged (HasFocus, this, newFocusedView);
  569. if (_hasFocus)
  570. {
  571. // Notify caused HasFocus to change to true.
  572. return;
  573. }
  574. if (SuperView is { })
  575. {
  576. SuperView._previouslyMostFocused = focusedPeer;
  577. }
  578. // Post-conditions - prove correctness
  579. if (HasFocus == previousValue)
  580. {
  581. throw new InvalidOperationException ("SetHasFocusFalse and the HasFocus value did not change.");
  582. }
  583. SetNeedsDisplay ();
  584. }
  585. private void NotifyFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedVew)
  586. {
  587. // Call the virtual method
  588. OnHasFocusChanged (newHasFocus, previousFocusedView, focusedVew);
  589. // Raise the event
  590. var args = new HasFocusEventArgs (newHasFocus, newHasFocus, previousFocusedView, focusedVew);
  591. HasFocusChanged?.Invoke (this, args);
  592. }
  593. /// <summary>
  594. /// Invoked after <see cref="HasFocus"/> has changed. This method is called before the <see cref="HasFocusChanged"/>
  595. /// event is raised.
  596. /// </summary>
  597. /// <remarks>
  598. /// <para>
  599. /// This event cannot be cancelled.
  600. /// </para>
  601. /// </remarks>
  602. /// <param name="newHasFocus">The new value of <see cref="View.HasFocus"/>.</param>
  603. /// <param name="previousFocusedView"></param>
  604. /// <param name="focusedVew">The view that is now focused. May be <see langword="null"/></param>
  605. protected virtual void OnHasFocusChanged (bool newHasFocus, View? previousFocusedView, View? focusedVew) { }
  606. /// <summary>Raised after <see cref="HasFocus"/> has changed.</summary>
  607. /// <remarks>
  608. /// <para>
  609. /// This event cannot be cancelled.
  610. /// </para>
  611. /// </remarks>
  612. public event EventHandler<HasFocusEventArgs>? HasFocusChanged;
  613. #endregion HasFocus
  614. #region Tab/Focus Handling
  615. /// <summary>
  616. /// Gets TabIndexes that are scoped to the specified behavior and direction. If behavior is null, all TabIndexes are
  617. /// returned.
  618. /// </summary>
  619. /// <param name="direction"></param>
  620. /// <param name="behavior"></param>
  621. /// <returns></returns>
  622. /// GetScopedTabIndexes
  623. internal View [] GetSubviewFocusChain (NavigationDirection direction, TabBehavior? behavior)
  624. {
  625. IEnumerable<View>? fitleredSubviews;
  626. if (behavior.HasValue)
  627. {
  628. fitleredSubviews = _subviews?.Where (v => v.TabStop == behavior && v is { CanFocus: true, Visible: true, Enabled: true });
  629. }
  630. else
  631. {
  632. fitleredSubviews = _subviews?.Where (v => v is { CanFocus: true, Visible: true, Enabled: true });
  633. }
  634. if (direction == NavigationDirection.Backward)
  635. {
  636. fitleredSubviews = fitleredSubviews?.Reverse ();
  637. }
  638. return fitleredSubviews?.ToArray () ?? Array.Empty<View> ();
  639. }
  640. private TabBehavior? _tabStop;
  641. /// <summary>
  642. /// Gets or sets the behavior of <see cref="AdvanceFocus"/> for keyboard navigation.
  643. /// </summary>
  644. /// <remarks>
  645. /// <para>
  646. /// If <see langword="null"/> the tab stop has not been set and setting <see cref="CanFocus"/> to true will set it
  647. /// to
  648. /// <see cref="TabBehavior.TabStop"/>.
  649. /// </para>
  650. /// <para>
  651. /// TabStop is independent of <see cref="CanFocus"/>. If <see cref="CanFocus"/> is <see langword="false"/>, the
  652. /// view will not gain
  653. /// focus even if this property is set and vice-versa.
  654. /// </para>
  655. /// <para>
  656. /// The default <see cref="TabBehavior.TabStop"/> keys are <see cref="Application.NextTabKey"/> (<c>Key.Tab</c>)
  657. /// and <see cref="Application.PrevTabKey"/> (<c>Key>Tab.WithShift</c>).
  658. /// </para>
  659. /// <para>
  660. /// The default <see cref="TabBehavior.TabGroup"/> keys are <see cref="Application.NextTabGroupKey"/> (
  661. /// <c>Key.F6</c>) and <see cref="Application.PrevTabGroupKey"/> (<c>Key>Key.F6.WithShift</c>).
  662. /// </para>
  663. /// </remarks>
  664. public TabBehavior? TabStop
  665. {
  666. get => _tabStop;
  667. set
  668. {
  669. if (_tabStop is { } && _tabStop == value)
  670. {
  671. return;
  672. }
  673. _tabStop = value;
  674. }
  675. }
  676. #endregion Tab/Focus Handling
  677. }