Browse Source

Fixes #750. Ensure the correct focus order after call SetFocus.

BDisp 5 years ago
parent
commit
fa992abc6e
1 changed files with 22 additions and 11 deletions
  1. 22 11
      Terminal.Gui/Core/View.cs

+ 22 - 11
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 {
 		}
 
 		/// <summary>
-		/// 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.
 		/// </summary>
 		/// <param name="view">View.</param>
 		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);
 		}
 
+		/// <summary>
+		/// Causes the specified view and the entire parent hierarchy to have focus.
+		/// </summary>
+		public void SetFocus ()
+		{
+			SuperView?.SetFocus (this);
+		}
+
 		/// <summary>
 		/// Defines the event arguments for <see cref="KeyEvent"/>
 		/// </summary>
@@ -1449,11 +1458,13 @@ namespace Terminal.Gui {
 		/// </summary>
 		public void EnsureFocus ()
 		{
-			if (focused == null)
-				if (FocusDirection == Direction.Forward)
+			if (focused == null && subviews?.Count > 0) {
+				if (FocusDirection == Direction.Forward) {
 					FocusFirst ();
-				else
+				} else {
 					FocusLast ();
+				}
+			}
 		}
 
 		/// <summary>