From fa992abc6e8d93d62902e691328348e0bb265a58 Mon Sep 17 00:00:00 2001 From: BDisp Date: Fri, 24 Jul 2020 19:15:28 +0100 Subject: [PATCH] Fixes #750. Ensure the correct focus order after call SetFocus. --- Terminal.Gui/Core/View.cs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 3f2c263c3..d04072b0e 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -1078,13 +1078,13 @@ namespace Terminal.Gui { { if (hasFocus != value) { hasFocus = value; + if (value) { + OnEnter (view); + } else { + OnLeave (view); + } + SetNeedsDisplay (); } - if (value) { - OnEnter (view); - } else { - OnLeave (view); - } - SetNeedsDisplay (); // Remove focus down the chain of subviews if focus is removed if (!value && focused != null) { @@ -1303,7 +1303,8 @@ namespace Terminal.Gui { } /// - /// Causes the specified subview to have focus. + /// Causes the specified subview to have focus. + /// This does not ensures that the entire parent hierarchy can really get focus and thus not updating the focus order. /// /// View. public void SetFocus (View view) @@ -1313,7 +1314,7 @@ namespace Terminal.Gui { //Console.WriteLine ($"Request to focus {view}"); if (!view.CanFocus) return; - if (focused == view) + if (focused?.hasFocus == true && focused == view) return; // Make sure that this view is a subview @@ -1336,6 +1337,14 @@ namespace Terminal.Gui { SuperView?.SetFocus (this); } + /// + /// Causes the specified view and the entire parent hierarchy to have focus. + /// + public void SetFocus () + { + SuperView?.SetFocus (this); + } + /// /// Defines the event arguments for /// @@ -1449,11 +1458,13 @@ namespace Terminal.Gui { /// public void EnsureFocus () { - if (focused == null) - if (FocusDirection == Direction.Forward) + if (focused == null && subviews?.Count > 0) { + if (FocusDirection == Direction.Forward) { FocusFirst (); - else + } else { FocusLast (); + } + } } ///