Browse Source

Fixes #723 Views now are notified when they are added or removing.

BDisp 5 năm trước cách đây
mục cha
commit
6182eab9e0
2 tập tin đã thay đổi với 38 bổ sung0 xóa
  1. 12 0
      Terminal.Gui/Core/Responder.cs
  2. 26 0
      Terminal.Gui/Core/View.cs

+ 12 - 0
Terminal.Gui/Core/Responder.cs

@@ -164,6 +164,18 @@ namespace Terminal.Gui {
 			return false;
 		}
 
+		/// <summary>
+		/// Method invoked when a view is added.
+		/// </summary>
+		/// <param name="view">The view added.</param>
+		public virtual void OnAddedView (View view) { }
+
+		/// <summary>
+		/// Method invoked when a view being removing.
+		/// </summary>
+		/// <param name="view">The view being removing.</param>
+		public virtual void OnRemovingView (View view) { }
+
 		/// <summary>
 		/// Method invoked when a view gets focus.
 		/// </summary>

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

@@ -124,6 +124,16 @@ namespace Terminal.Gui {
 
 		TextFormatter viewText;
 
+		/// <summary>
+		/// Event fired when the view is added.
+		/// </summary>
+		public Action<View> AddedView;
+
+		/// <summary>
+		/// Event fired when the view is being removing.
+		/// </summary>
+		public Action<View> RemovingView;
+
 		/// <summary>
 		/// Event fired when the view gets focus.
 		/// </summary>
@@ -552,6 +562,7 @@ namespace Terminal.Gui {
 				subviews = new List<View> ();
 			subviews.Add (view);
 			view.container = this;
+			OnAddedView (view);
 			if (view.CanFocus)
 				CanFocus = true;
 			SetNeedsLayout ();
@@ -596,6 +607,7 @@ namespace Terminal.Gui {
 			if (view == null || subviews == null)
 				return;
 
+			OnRemovingView (view);
 			SetNeedsLayout ();
 			SetNeedsDisplay ();
 			var touched = view.Frame;
@@ -933,6 +945,20 @@ namespace Terminal.Gui {
 			public bool Handled { get; set; }
 		}
 
+		/// <inheritdoc/>
+		public override void OnAddedView (View view)
+		{
+			AddedView?.Invoke (view);
+			base.OnAddedView (view);
+		}
+
+		/// <inheritdoc/>
+		public override void OnRemovingView (View view)
+		{
+			RemovingView?.Invoke (view);
+			base.OnRemovingView (view);
+		}
+
 		/// <inheritdoc/>
 		public override bool OnEnter ()
 		{