浏览代码

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

BDisp 5 年之前
父节点
当前提交
6182eab9e0
共有 2 个文件被更改,包括 38 次插入0 次删除
  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;
 			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>
 		/// <summary>
 		/// Method invoked when a view gets focus.
 		/// Method invoked when a view gets focus.
 		/// </summary>
 		/// </summary>

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

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