浏览代码

Added SuperViewChangedEventArgs for Added/Removed to clarify situation

tznind 2 年之前
父节点
当前提交
02825d89ba

+ 1 - 1
Terminal.Gui/Core/Autocomplete/Autocomplete.cs

@@ -74,7 +74,7 @@ namespace Terminal.Gui {
 			}
 			}
 		}
 		}
 
 
-		private void Top_Removed (object sender, ViewEventArgs e)
+		private void Top_Removed (object sender, SuperViewChangedEventArgs e)
 		{
 		{
 			Visible = false;
 			Visible = false;
 			ManipulatePopup ();
 			ManipulatePopup ();

+ 1 - 1
Terminal.Gui/Core/Border.cs

@@ -460,7 +460,7 @@ namespace Terminal.Gui {
 			}
 			}
 		}
 		}
 
 
-		private void Parent_Removed (object sender, ViewEventArgs e)
+		private void Parent_Removed (object sender, SuperViewChangedEventArgs e)
 		{
 		{
 			BorderBrush = default;
 			BorderBrush = default;
 			Background = default;
 			Background = default;

+ 33 - 0
Terminal.Gui/Core/EventArgs/SuperViewChangedEventArgs.cs

@@ -0,0 +1,33 @@
+using System;
+
+namespace Terminal.Gui {
+	/// <summary>
+	/// Args for events where the <see cref="View.SuperView"/> of a <see cref="View"/> is changed
+	/// (e.g. <see cref="View.Removed"/> / <see cref="View.Added"/> events).
+	/// </summary>
+	public class SuperViewChangedEventArgs : EventArgs
+	{
+		/// <summary>
+		/// Creates a new instance of the <see cref="SuperViewChangedEventArgs"/> class.
+		/// </summary>
+		/// <param name="parent"></param>
+		/// <param name="child"></param>
+		public SuperViewChangedEventArgs (View parent, View child)
+		{
+			Parent = parent;
+			Child = child;
+		}
+
+		/// <summary>
+		/// The parent.  For <see cref="View.Removed"/> this is the old
+		/// parent (new parent now being null).  For <see cref="View.Added"/>
+		/// it is the new parent to whom view now belongs.
+		/// </summary>
+		public View Parent { get; }
+
+		/// <summary>
+		/// The view that is having it's <see cref="View.SuperView"/> changed
+		/// </summary>
+		public View Child { get; }
+	}
+}

+ 1 - 2
Terminal.Gui/Core/EventArgs/ViewEventArgs.cs

@@ -4,8 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
 
 
-namespace Terminal.Gui{
-
+namespace Terminal.Gui {
 	/// <summary>
 	/// <summary>
 	/// Args for events that relate to specific <see cref="View"/>
 	/// Args for events that relate to specific <see cref="View"/>
 	/// </summary>
 	/// </summary>

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

@@ -117,14 +117,14 @@ namespace Terminal.Gui {
 		ShortcutHelper shortcutHelper;
 		ShortcutHelper shortcutHelper;
 
 
 		/// <summary>
 		/// <summary>
-		/// Event fired when a subview is being added to this view.
+		/// Event fired when this view is added to another.
 		/// </summary>
 		/// </summary>
-		public event EventHandler<ViewEventArgs> Added;
+		public event EventHandler<SuperViewChangedEventArgs> Added;
 
 
 		/// <summary>
 		/// <summary>
-		/// Event fired when a subview is being removed from this view.
+		/// Event fired when this view is removed from another.
 		/// </summary>
 		/// </summary>
-		public event EventHandler<ViewEventArgs> Removed;
+		public event EventHandler<SuperViewChangedEventArgs> Removed;
 
 
 		/// <summary>
 		/// <summary>
 		/// Event fired when the view gets focus.
 		/// Event fired when the view gets focus.
@@ -942,7 +942,7 @@ namespace Terminal.Gui {
 			}
 			}
 			SetNeedsLayout ();
 			SetNeedsLayout ();
 			SetNeedsDisplay ();
 			SetNeedsDisplay ();
-			OnAdded (new ViewEventArgs(view));
+			OnAdded (new SuperViewChangedEventArgs (this,view));
 			if (IsInitialized) {
 			if (IsInitialized) {
 				view.BeginInit ();
 				view.BeginInit ();
 				view.EndInit ();
 				view.EndInit ();
@@ -1002,7 +1002,7 @@ namespace Terminal.Gui {
 				if (v.Frame.IntersectsWith (touched))
 				if (v.Frame.IntersectsWith (touched))
 					view.SetNeedsDisplay ();
 					view.SetNeedsDisplay ();
 			}
 			}
-			OnRemoved (new ViewEventArgs(view));
+			OnRemoved (new SuperViewChangedEventArgs (this, view));
 			if (focused == view) {
 			if (focused == view) {
 				focused = null;
 				focused = null;
 			}
 			}
@@ -1355,9 +1355,9 @@ namespace Terminal.Gui {
 		/// Method invoked when a subview is being added to this view.
 		/// Method invoked when a subview is being added to this view.
 		/// </summary>
 		/// </summary>
 		/// <param name="e">Event where <see cref="ViewEventArgs.View"/> is the subview being added.</param>
 		/// <param name="e">Event where <see cref="ViewEventArgs.View"/> is the subview being added.</param>
-		public virtual void OnAdded (ViewEventArgs e)
+		public virtual void OnAdded (SuperViewChangedEventArgs e)
 		{
 		{
-			var view = e.View;
+			var view = e.Child;
 			view.IsAdded = true;
 			view.IsAdded = true;
 			view.x ??= view.frame.X;
 			view.x ??= view.frame.X;
 			view.y ??= view.frame.Y;
 			view.y ??= view.frame.Y;
@@ -1371,9 +1371,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="e">Event args describing the subview being removed.</param>
 		/// <param name="e">Event args describing the subview being removed.</param>
-		public virtual void OnRemoved (ViewEventArgs e)
+		public virtual void OnRemoved (SuperViewChangedEventArgs e)
 		{
 		{
-			var view = e.View;
+			var view = e.Child;
 			view.IsAdded = false;
 			view.IsAdded = false;
 			view.Removed?.Invoke (this, e);
 			view.Removed?.Invoke (this, e);
 		}
 		}

+ 4 - 1
UICatalog/KeyBindingsDialog.cs

@@ -60,8 +60,11 @@ namespace UICatalog {
 				foreach (var sub in view.Subviews) {
 				foreach (var sub in view.Subviews) {
 					RecordView (sub);
 					RecordView (sub);
 				}
 				}
+				// TODO: BUG: Based on my new understanding of Added event I think this is wrong
+				// (and always was wrong). Parents don't get to be told when new views are added
+				// to them
 
 
-				view.Added += (s,e)=>RecordView(e.View);
+				view.Added += (s,e)=>RecordView(e.Child);
 			}
 			}
 
 
 			internal static void Initialize ()
 			internal static void Initialize ()

+ 13 - 9
UnitTests/Core/ViewTests.cs

@@ -239,10 +239,14 @@ namespace Terminal.Gui.CoreTests {
 			var t = new View ();
 			var t = new View ();
 
 
 			v.Added += (s,e) => {
 			v.Added += (s,e) => {
-				Assert.True (v.SuperView == e.View);
+				Assert.Same (v.SuperView, e.Parent);
+				Assert.Same (t, e.Parent);
+				Assert.Same (v, e.Child);
 			};
 			};
 
 
 			v.Removed += (s, e) => {
 			v.Removed += (s, e) => {
+				Assert.Same (t, e.Parent);
+				Assert.Same (v, e.Child);
 				Assert.True (v.SuperView == null);
 				Assert.True (v.SuperView == null);
 			};
 			};
 
 
@@ -655,20 +659,20 @@ namespace Terminal.Gui.CoreTests {
 			int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
 			int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0;
 
 
 			w.Added += (s,e) => {
 			w.Added += (s,e) => {
-				Assert.Equal (e.View.Frame.Width, w.Frame.Width);
-				Assert.Equal (e.View.Frame.Height, w.Frame.Height);
+				Assert.Equal (e.Parent.Frame.Width, w.Frame.Width);
+				Assert.Equal (e.Parent.Frame.Height, w.Frame.Height);
 			};
 			};
 			v1.Added += (s, e) => {
 			v1.Added += (s, e) => {
-				Assert.Equal (e.View.Frame.Width, v1.Frame.Width);
-				Assert.Equal (e.View.Frame.Height, v1.Frame.Height);
+				Assert.Equal (e.Parent.Frame.Width, v1.Frame.Width);
+				Assert.Equal (e.Parent.Frame.Height, v1.Frame.Height);
 			};
 			};
 			v2.Added += (s, e) => {
 			v2.Added += (s, e) => {
-				Assert.Equal (e.View.Frame.Width, v2.Frame.Width);
-				Assert.Equal (e.View.Frame.Height, v2.Frame.Height);
+				Assert.Equal (e.Parent.Frame.Width, v2.Frame.Width);
+				Assert.Equal (e.Parent.Frame.Height, v2.Frame.Height);
 			};
 			};
 			sv1.Added += (s, e) => {
 			sv1.Added += (s, e) => {
-				Assert.Equal (e.View.Frame.Width, sv1.Frame.Width);
-				Assert.Equal (e.View.Frame.Height, sv1.Frame.Height);
+				Assert.Equal (e.Parent.Frame.Width, sv1.Frame.Width);
+				Assert.Equal (e.Parent.Frame.Height, sv1.Frame.Height);
 			};
 			};
 
 
 			t.Initialized += (s, e) => {
 			t.Initialized += (s, e) => {

+ 1 - 1
UnitTests/TopLevels/ToplevelTests.cs

@@ -587,7 +587,7 @@ namespace Terminal.Gui.TopLevelTests {
 			var view = new View ();
 			var view = new View ();
 			view.Added += View_Added;
 			view.Added += View_Added;
 
 
-			void View_Added (object sender, ViewEventArgs e)
+			void View_Added (object sender, SuperViewChangedEventArgs e)
 			{
 			{
 				Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (s,e) => alternateForwardKey = e.OldKey);
 				Assert.Throws<NullReferenceException> (() => Application.Top.AlternateForwardKeyChanged += (s,e) => alternateForwardKey = e.OldKey);
 				Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (s,e) => alternateBackwardKey = e.OldKey);
 				Assert.Throws<NullReferenceException> (() => Application.Top.AlternateBackwardKeyChanged += (s,e) => alternateBackwardKey = e.OldKey);