浏览代码

View with a null SuperView should be allowed to set focus.

BDisp 2 年之前
父节点
当前提交
df95acc77d
共有 2 个文件被更改,包括 20 次插入2 次删除
  1. 8 2
      Terminal.Gui/Core/View.cs
  2. 12 0
      UnitTests/Core/ViewTests.cs

+ 8 - 2
Terminal.Gui/Core/View.cs

@@ -1673,7 +1673,9 @@ namespace Terminal.Gui {
 				return;
 			if (focused?.hasFocus == true && focused == view)
 				return;
-			if (focused?.hasFocus == true && focused?.SuperView == view) {
+			if ((focused?.hasFocus == true && focused?.SuperView == view)
+				|| view == this) {
+
 				if (!view.hasFocus) {
 					view.hasFocus = true;
 				}
@@ -1715,7 +1717,11 @@ namespace Terminal.Gui {
 				return;
 			}
 
-			SuperView?.SetFocus (this);
+			if (SuperView != null) {
+				SuperView.SetFocus (this);
+			} else {
+				SetFocus (this);
+			}
 		}
 
 		/// <summary>

+ 12 - 0
UnitTests/Core/ViewTests.cs

@@ -2995,5 +2995,17 @@ At 0,0
 			Assert.Null (child);
 			Assert.False (leave);
 		}
+
+		[Fact, AutoInitShutdown]
+		public void SetFocus_View_With_Null_Superview_Does_Not_Throw_Exception ()
+		{
+			Assert.True (Application.Top.CanFocus);
+			Assert.False (Application.Top.HasFocus);
+
+			var exception = Record.Exception (Application.Top.SetFocus);
+			Assert.Null (exception);
+			Assert.True (Application.Top.CanFocus);
+			Assert.True (Application.Top.HasFocus);
+		}
 	}
 }