Skip to content

Commit feee9a0

Browse files
committed
More Focus fixes
1 parent f3bdfc3 commit feee9a0

File tree

1 file changed

+38
-4
lines changed

1 file changed

+38
-4
lines changed

Terminal.Gui/View/View.Navigation.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ public bool SetFocus ()
502502
// Restore focus to the previously focused subview, if any
503503
if (!RestoreFocus ())
504504
{
505-
Debug.Assert (_previouslyFocused is null);
505+
// Debug.Assert (_previouslyFocused is null);
506506
// Couldn't restore focus, so use Advance to navigate to the next focusable subview, if any
507507
AdvanceFocus (NavigationDirection.Forward, null);
508508
}
@@ -563,7 +563,14 @@ private bool NotifyFocusChanging (bool currentHasFocus, bool newHasFocus, View?
563563

564564
if (appFocused == currentFocused)
565565
{
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+
}
567574
}
568575

569576
return false;
@@ -632,8 +639,10 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
632639
// If newFocusedVew is null, we need to find the view that should get focus, and SetFocus on it.
633640
if (!traversingDown && newFocusedView is null)
634641
{
642+
// Restore focus?
635643
if (superViewOrParent?._previouslyFocused is { CanFocus: true })
636644
{
645+
// TODO: Why don't we call RestoreFocus here?
637646
if (superViewOrParent._previouslyFocused != this && superViewOrParent._previouslyFocused.SetFocus ())
638647
{
639648
// The above will cause SetHasFocusFalse, so we can return
@@ -642,6 +651,7 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
642651
}
643652
}
644653

654+
// AdvanceFocus?
645655
if (superViewOrParent is { CanFocus: true })
646656
{
647657
if (superViewOrParent.AdvanceFocus (NavigationDirection.Forward, TabStop))
@@ -657,12 +667,15 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
657667
}
658668
}
659669

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 })
661674
{
662675
// Temporarily ensure this view can't get focus
663676
bool prevCanFocus = _canFocus;
664677
_canFocus = false;
665-
bool restoredFocus = Application.Navigation.GetFocused ()!.RestoreFocus ();
678+
bool restoredFocus = applicationFocused!.RestoreFocus ();
666679
_canFocus = prevCanFocus;
667680

668681
if (restoredFocus)
@@ -672,6 +685,27 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
672685
return;
673686
}
674687
}
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+
}
675709
// No other focusable view to be found. Just "leave" us...
676710
}
677711

0 commit comments

Comments
 (0)