BDisp 5 years ago
parent
commit
967d55bee6
2 changed files with 51 additions and 0 deletions
  1. 30 0
      Terminal.Gui/Core/View.cs
  2. 21 0
      UnitTests/ViewTests.cs

+ 30 - 0
Terminal.Gui/Core/View.cs

@@ -124,6 +124,16 @@ namespace Terminal.Gui {
 
 
 		TextFormatter viewText;
 		TextFormatter viewText;
 
 
+		/// <summary>
+		/// Event fired when a subview is being added to this view.
+		/// </summary>
+		public Action<View> Added;
+
+		/// <summary>
+		/// Event fired when a subview is being removed from this view.
+		/// </summary>
+		public Action<View> Removed;
+
 		/// <summary>
 		/// <summary>
 		/// Event fired when the view gets focus.
 		/// Event fired when the view gets focus.
 		/// </summary>
 		/// </summary>
@@ -614,6 +624,7 @@ namespace Terminal.Gui {
 			subviews.Add (view);
 			subviews.Add (view);
 			tabIndexes.Add (view);
 			tabIndexes.Add (view);
 			view.container = this;
 			view.container = this;
+			OnAdded (view);
 			if (view.CanFocus) {
 			if (view.CanFocus) {
 				CanFocus = true;
 				CanFocus = true;
 				view.tabIndex = tabIndexes.IndexOf (view);
 				view.tabIndex = tabIndexes.IndexOf (view);
@@ -668,6 +679,7 @@ namespace Terminal.Gui {
 			subviews.Remove (view);
 			subviews.Remove (view);
 			tabIndexes.Remove (view);
 			tabIndexes.Remove (view);
 			view.container = null;
 			view.container = null;
+			OnRemoved (view);
 			view.tabIndex = -1;
 			view.tabIndex = -1;
 			if (subviews.Count < 1)
 			if (subviews.Count < 1)
 				this.CanFocus = false;
 				this.CanFocus = false;
@@ -1010,6 +1022,24 @@ namespace Terminal.Gui {
 			public View View { get; set; }
 			public View View { get; set; }
 		}
 		}
 
 
+		/// <summary>
+		/// Method invoked  when a subview is being added to this view.
+		/// </summary>
+		/// <param name="view">The subview being added.</param>
+		public virtual void OnAdded (View view)
+		{
+			view.Added?.Invoke (this);
+		}
+
+		/// <summary>
+		/// Method invoked when a subview is being removed from this view.
+		/// </summary>
+		/// <param name="view">The subview being removed.</param>
+		public virtual void OnRemoved (View view)
+		{
+			view.Removed?.Invoke (this);
+		}
+
 		/// <inheritdoc/>
 		/// <inheritdoc/>
 		public override bool OnEnter (View view)
 		public override bool OnEnter (View view)
 		{
 		{

+ 21 - 0
UnitTests/ViewTests.cs

@@ -136,6 +136,27 @@ namespace Terminal.Gui {
 			Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
 			Assert.Throws<InvalidOperationException> (() => root.LayoutSubviews ());
 		}
 		}
 
 
+		[Fact]
+		public void Added_Removed ()
+		{
+			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);
+		}
+
 		[Fact]
 		[Fact]
 		public void Subviews_TabIndexes_AreEqual ()
 		public void Subviews_TabIndexes_AreEqual ()
 		{
 		{