소스 검색

Fixes #2970. ScrollView doesn't remove a view that was previously added on both versions. (#2971)

BDisp 1 년 전
부모
커밋
91865eed22
2개의 변경된 파일56개의 추가작업 그리고 10개의 파일을 삭제
  1. 33 10
      Terminal.Gui/Views/ScrollView.cs
  2. 23 0
      UnitTests/Views/ScrollViewTests.cs

+ 33 - 10
Terminal.Gui/Views/ScrollView.cs

@@ -237,6 +237,39 @@ namespace Terminal.Gui {
 			SetNeedsLayout ();
 		}
 
+		/// <summary>
+		/// Removes the view from the scrollview.
+		/// </summary>
+		/// <param name="view">The view to remove from the scrollview.</param>
+		public override void Remove (View view)
+		{
+			if (view == null) {
+				return;
+			}
+
+			SetNeedsDisplay ();
+			var container = view?.SuperView;
+			if (container == this) {
+				base.Remove (view);
+			} else {
+				container?.Remove (view);
+			}
+
+			if (contentView.InternalSubviews.Count < 1) {
+				this.CanFocus = false;
+			}
+		}
+
+		/// <summary>
+		///   Removes all widgets from this container.
+		/// </summary>
+		/// <remarks>
+		/// </remarks>
+		public override void RemoveAll ()
+		{
+			contentView.RemoveAll ();
+		}
+
 		void View_MouseLeave (MouseEventArgs e)
 		{
 			if (Application.MouseGrabView != null && Application.MouseGrabView != vertical && Application.MouseGrabView != horizontal) {
@@ -280,16 +313,6 @@ namespace Terminal.Gui {
 			}
 		}
 
-		/// <summary>
-		///   Removes all widgets from this container.
-		/// </summary>
-		/// <remarks>
-		/// </remarks>
-		public override void RemoveAll ()
-		{
-			contentView.RemoveAll ();
-		}
-
 		/// <summary>
 		/// Gets or sets the visibility for the vertical scroll indicator.
 		/// </summary>

+ 23 - 0
UnitTests/Views/ScrollViewTests.cs

@@ -498,5 +498,28 @@ namespace Terminal.Gui.ViewTests {
 00000000000000000000000
 00000000000000000000000", attributes);
 		}
+
+		[Fact, AutoInitShutdown]
+		public void Remove_Added_View_Is_Allowed ()
+		{
+			var sv = new ScrollView () {
+				Width = 20,
+				Height = 20,
+				ContentSize = new Size (100, 100)
+			};
+			sv.Add (new View () { Width = Dim.Fill (), Height = Dim.Fill (50), Id = "View1" },
+				new View () { Y = 51, Width = Dim.Fill (), Height = Dim.Fill (), Id = "View2" });
+
+			Application.Top.Add (sv);
+			Application.Begin (Application.Top);
+
+			Assert.Equal (3, sv.Subviews.Count);
+			Assert.Equal (2, sv.Subviews [0].Subviews.Count);
+
+			sv.Remove (sv.Subviews [0].Subviews [1]);
+			Assert.Equal (3, sv.Subviews.Count);
+			Assert.Single (sv.Subviews [0].Subviews);
+			Assert.Equal ("View1", sv.Subviews [0].Subviews [0].Id);
+		}
 	}
 }