瀏覽代碼

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

Ross Ferguson 5 年之前
父節點
當前提交
6d690996a5
共有 1 個文件被更改,包括 30 次插入0 次删除
  1. 30 0
      Terminal.Gui/Core/View.cs

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

@@ -124,6 +124,16 @@ namespace Terminal.Gui {
 
 		TextFormatter viewText;
 
+		/// <summary>
+		/// Event fired when a subview is being added to this view.
+		/// </summary>
+		public Action<View> Adding;
+
+		/// <summary>
+		/// Event fired when a subview is being removed from this view.
+		/// </summary>
+		public Action<View> Removing;
+
 		/// <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;
+			OnAdding (view);
 			if (view.CanFocus)
 				CanFocus = true;
 			SetNeedsLayout ();
@@ -596,6 +607,7 @@ namespace Terminal.Gui {
 			if (view == null || subviews == null)
 				return;
 
+			OnRemoving (view);
 			SetNeedsLayout ();
 			SetNeedsDisplay ();
 			var touched = view.Frame;
@@ -933,6 +945,24 @@ namespace Terminal.Gui {
 			public bool Handled { 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 OnAdding (View view)
+		{
+			view.Adding?.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 OnRemoving (View view)
+		{
+			view.Removing?.Invoke (this);
+		}
+
 		/// <inheritdoc/>
 		public override bool OnEnter ()
 		{