Skip to content

Commit 7b7b6ab

Browse files
committed
More Focus fixes 2
1 parent feee9a0 commit 7b7b6ab

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

Terminal.Gui/Application/ApplicationNavigation.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,19 @@ public ApplicationNavigation ()
2727
/// <summary>
2828
/// Gets the most focused <see cref="View"/> in the application, if there is one.
2929
/// </summary>
30-
public View? GetFocused () { return _focused; }
30+
public View? GetFocused ()
31+
{
32+
return _focused;
33+
34+
if (_focused is { CanFocus: true, HasFocus: true })
35+
{
36+
return _focused;
37+
}
38+
39+
_focused = null;
40+
41+
return null;
42+
}
3143

3244
/// <summary>
3345
/// Gets whether <paramref name="view"/> is in the Subview hierarchy of <paramref name="start"/>.

Terminal.Gui/View/View.Navigation.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ public bool CanFocus
179179

180180
if (!_canFocus && HasFocus)
181181
{
182+
if (Title == "AdornmentsEditor")
183+
{
184+
185+
}
182186
// If CanFocus is set to false and this view has focus, make it leave focus
183187
// Set traverssingdown so we don't go back up the hierachy...
184188
SetHasFocusFalse (null, traversingDown: false);
@@ -381,6 +385,7 @@ public bool HasFocus
381385
{
382386
SetHasFocusFalse (null);
383387

388+
Debug.Assert (!_hasFocus);
384389
if (_hasFocus)
385390
{
386391
// force it.
@@ -502,7 +507,6 @@ public bool SetFocus ()
502507
// Restore focus to the previously focused subview, if any
503508
if (!RestoreFocus ())
504509
{
505-
// Debug.Assert (_previouslyFocused is null);
506510
// Couldn't restore focus, so use Advance to navigate to the next focusable subview, if any
507511
AdvanceFocus (NavigationDirection.Forward, null);
508512
}
@@ -656,9 +660,11 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
656660
{
657661
if (superViewOrParent.AdvanceFocus (NavigationDirection.Forward, TabStop))
658662
{
659-
// The above will cause SetHasFocusFalse, so we can return
660-
Debug.Assert (!_hasFocus);
661-
return;
663+
// The above might have SetHasFocusFalse, so we can return
664+
if (!_hasFocus)
665+
{
666+
return;
667+
}
662668
}
663669

664670
if (superViewOrParent is { HasFocus: true, CanFocus: true })
@@ -709,6 +715,8 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
709715
// No other focusable view to be found. Just "leave" us...
710716
}
711717

718+
Debug.Assert (_hasFocus);
719+
712720
// Before we can leave focus, we need to make sure that all views down the subview-hierarchy have left focus.
713721
View? mostFocused = MostFocused;
714722

@@ -739,10 +747,17 @@ private void SetHasFocusFalse (View? newFocusedView, bool traversingDown = false
739747

740748
bool previousValue = HasFocus;
741749

750+
Debug.Assert (_hasFocus);
751+
742752
// Note, can't be cancelled.
743753
NotifyFocusChanging (HasFocus, !HasFocus, this, newFocusedView);
744754

745-
Debug.Assert (_hasFocus);
755+
// Even though the change can't be cancelled, some listener may have changed the focus to another view.
756+
if (!_hasFocus)
757+
{
758+
// Notify caused HasFocus to change to false.
759+
return;
760+
}
746761

747762
// Get whatever peer has focus, if any so we can update our superview's _previouslyMostFocused
748763
View? focusedPeer = superViewOrParent?.Focused;

0 commit comments

Comments
 (0)