Browse Source

Merge branch 'added-removing-view-events' of https://github.com/BDisp/gui.cs into combobox_fixes2

Ross Ferguson 5 years ago
parent
commit
7d8f3cac9e
2 changed files with 25 additions and 4 deletions
  1. 4 4
      Terminal.Gui/Core/View.cs
  2. 21 0
      UnitTests/ViewTests.cs

+ 4 - 4
Terminal.Gui/Core/View.cs

@@ -132,7 +132,7 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// <summary>
 		/// Event fired when a subview is being removed from this view.
 		/// Event fired when a subview is being removed from this view.
 		/// </summary>
 		/// </summary>
-		public Action<View> Removing;
+		public Action<View> Removed;
 
 
 		/// <summary>
 		/// <summary>
 		/// Event fired when the view gets focus.
 		/// Event fired when the view gets focus.
@@ -607,12 +607,12 @@ namespace Terminal.Gui {
 			if (view == null || subviews == null)
 			if (view == null || subviews == null)
 				return;
 				return;
 
 
-			OnRemoving (view);
 			SetNeedsLayout ();
 			SetNeedsLayout ();
 			SetNeedsDisplay ();
 			SetNeedsDisplay ();
 			var touched = view.Frame;
 			var touched = view.Frame;
 			subviews.Remove (view);
 			subviews.Remove (view);
 			view.container = null;
 			view.container = null;
+			OnRemoved (view);
 
 
 			if (subviews.Count < 1)
 			if (subviews.Count < 1)
 				this.CanFocus = false;
 				this.CanFocus = false;
@@ -958,9 +958,9 @@ namespace Terminal.Gui {
 		/// Method invoked when a subview is being removed from this view.
 		/// Method invoked when a subview is being removed from this view.
 		/// </summary>
 		/// </summary>
 		/// <param name="view">The subview being removed.</param>
 		/// <param name="view">The subview being removed.</param>
-		public virtual void OnRemoving (View view)
+		public virtual void OnRemoved (View view)
 		{
 		{
-			view.Removing?.Invoke (this);
+			view.Removed?.Invoke (this);
 		}
 		}
 
 
 		/// <inheritdoc/>
 		/// <inheritdoc/>

+ 21 - 0
UnitTests/ViewTests.cs

@@ -135,5 +135,26 @@ namespace Terminal.Gui {
 			sub2.Width = Dim.Width (sub2);
 			sub2.Width = Dim.Width (sub2);
 			Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
 			Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
 		}
 		}
+
+		[Fact]
+		public void Added_Removing ()
+		{
+			var v = new View (new Rect (0, 0, 10, 24));
+			var t = new View ();
+
+			v.Added += (View e) => {
+				Assert.True (v.SuperView == e);
+			};
+
+			v.Removed += (View e) => {
+				Assert.True (v.SuperView == null);
+			};
+
+			t.Add (v);
+			Assert.True (t.Subviews.Count == 1);
+
+			t.Remove (v);
+			Assert.True (t.Subviews.Count == 0);
+		}
 	}
 	}
 }
 }