@@ -502,7 +502,7 @@ public bool SetFocus ()
502
502
// Restore focus to the previously focused subview, if any
503
503
if ( ! RestoreFocus ( ) )
504
504
{
505
- Debug . Assert ( _previouslyFocused is null ) ;
505
+ // Debug.Assert (_previouslyFocused is null);
506
506
// Couldn't restore focus, so use Advance to navigate to the next focusable subview, if any
507
507
AdvanceFocus ( NavigationDirection . Forward , null ) ;
508
508
}
@@ -563,7 +563,14 @@ private bool NotifyFocusChanging (bool currentHasFocus, bool newHasFocus, View?
563
563
564
564
if ( appFocused == currentFocused )
565
565
{
566
- Application . Navigation ? . SetFocused ( null ) ;
566
+ if ( newFocused is { HasFocus : true } )
567
+ {
568
+ Application . Navigation ? . SetFocused ( newFocused ) ;
569
+ }
570
+ else
571
+ {
572
+ Application . Navigation ? . SetFocused ( null ) ;
573
+ }
567
574
}
568
575
569
576
return false ;
@@ -632,8 +639,10 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
632
639
// If newFocusedVew is null, we need to find the view that should get focus, and SetFocus on it.
633
640
if ( ! traversingDown && newFocusedView is null )
634
641
{
642
+ // Restore focus?
635
643
if ( superViewOrParent ? . _previouslyFocused is { CanFocus : true } )
636
644
{
645
+ // TODO: Why don't we call RestoreFocus here?
637
646
if ( superViewOrParent . _previouslyFocused != this && superViewOrParent . _previouslyFocused . SetFocus ( ) )
638
647
{
639
648
// The above will cause SetHasFocusFalse, so we can return
@@ -642,6 +651,7 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
642
651
}
643
652
}
644
653
654
+ // AdvanceFocus?
645
655
if ( superViewOrParent is { CanFocus : true } )
646
656
{
647
657
if ( superViewOrParent . AdvanceFocus ( NavigationDirection . Forward , TabStop ) )
@@ -657,12 +667,15 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
657
667
}
658
668
}
659
669
660
- if ( Application . Navigation is { } && Application . Navigation . GetFocused ( ) is { CanFocus : true } )
670
+ // Application.Navigation.GetFocused?
671
+ View ? applicationFocused = Application . Navigation ? . GetFocused ( ) ;
672
+
673
+ if ( newFocusedView is null && applicationFocused != this && applicationFocused is { CanFocus : true } )
661
674
{
662
675
// Temporarily ensure this view can't get focus
663
676
bool prevCanFocus = _canFocus ;
664
677
_canFocus = false ;
665
- bool restoredFocus = Application . Navigation . GetFocused ( ) ! . RestoreFocus ( ) ;
678
+ bool restoredFocus = applicationFocused ! . RestoreFocus ( ) ;
666
679
_canFocus = prevCanFocus ;
667
680
668
681
if ( restoredFocus )
@@ -672,6 +685,27 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
672
685
return ;
673
686
}
674
687
}
688
+
689
+ // Application.Top?
690
+ if ( newFocusedView is null && Application . Top is { CanFocus : true , HasFocus : false } )
691
+ {
692
+ // Temporarily ensure this view can't get focus
693
+ bool prevCanFocus = _canFocus ;
694
+ _canFocus = false ;
695
+ bool restoredFocus = Application . Top . RestoreFocus ( ) ;
696
+ _canFocus = prevCanFocus ;
697
+
698
+ if ( Application . Top is { CanFocus : true , HasFocus : true } )
699
+ {
700
+ newFocusedView = Application . Top ;
701
+ }
702
+ else if ( restoredFocus )
703
+ {
704
+ // The above caused SetHasFocusFalse, so we can return
705
+ Debug . Assert ( ! _hasFocus ) ;
706
+ return ;
707
+ }
708
+ }
675
709
// No other focusable view to be found. Just "leave" us...
676
710
}
677
711
0 commit comments