Browse Source

Switch to EventHandler<ToplevelEventArgs> for

- Activate
- Deactivate
- ChildClosed
- Closed
- ChildLoaded
- ChildUnloaded
tznind 2 years ago
parent
commit
bdcbd55856

+ 21 - 0
Terminal.Gui/Core/EventArgs/ToplevelEventArgs.cs

@@ -0,0 +1,21 @@
+using System;
+
+namespace Terminal.Gui {
+	public class ToplevelEventArgs : EventArgs{
+
+		public ToplevelEventArgs (Toplevel toplevel)
+		{
+			Toplevel = toplevel;
+		}
+
+		/// <summary>
+		/// Gets the <see cref="Toplevel"/> that the event is about.
+		/// </summary>
+		/// <remarks>This is usually but may not always be the same as the sender 
+		/// in <see cref="EventHandler"/>.  For example if the reported event
+		/// is about a different  <see cref="Toplevel"/> or the event is
+		/// raised by a separate class.
+		/// </remarks>
+		public Toplevel Toplevel { get; }
+	}
+}

+ 12 - 12
Terminal.Gui/Core/Toplevel.cs

@@ -68,18 +68,18 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// Invoked when the Toplevel <see cref="Application.RunState"/> becomes the <see cref="Application.Current"/> Toplevel.
 		/// </summary>
-		public event Action<Toplevel> Activate;
+		public event EventHandler<ToplevelEventArgs> Activate;
 
 		/// <summary>
 		/// Invoked when the Toplevel<see cref="Application.RunState"/> ceases to be the <see cref="Application.Current"/> Toplevel.
 		/// </summary>
-		public event Action<Toplevel> Deactivate;
+		public event EventHandler<ToplevelEventArgs> Deactivate;
 
 		/// <summary>
 		/// Invoked when a child of the Toplevel <see cref="Application.RunState"/> is closed by  
 		/// <see cref="Application.End(Application.RunState)"/>.
 		/// </summary>
-		public event Action<Toplevel> ChildClosed;
+		public event EventHandler<ToplevelEventArgs> ChildClosed;
 
 		/// <summary>
 		/// Invoked when the last child of the Toplevel <see cref="Application.RunState"/> is closed from 
@@ -96,17 +96,17 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// Invoked when the Toplevel's <see cref="Application.RunState"/> is closed by <see cref="Application.End(Application.RunState)"/>.
 		/// </summary>
-		public event Action<Toplevel> Closed;
+		public event EventHandler<ToplevelEventArgs> Closed;
 
 		/// <summary>
 		/// Invoked when a child Toplevel's <see cref="Application.RunState"/> has been loaded.
 		/// </summary>
-		public event Action<Toplevel> ChildLoaded;
+		public event EventHandler<ToplevelEventArgs> ChildLoaded;
 
 		/// <summary>
 		/// Invoked when a cjhild Toplevel's <see cref="Application.RunState"/> has been unloaded.
 		/// </summary>
-		public event Action<Toplevel> ChildUnloaded;
+		public event EventHandler<ToplevelEventArgs> ChildUnloaded;
 
 		/// <summary>
 		/// Invoked when the terminal has been resized. The new <see cref="Size"/> of the terminal is provided.
@@ -120,17 +120,17 @@ namespace Terminal.Gui {
 
 		internal virtual void OnChildUnloaded (Toplevel top)
 		{
-			ChildUnloaded?.Invoke (top);
+			ChildUnloaded?.Invoke (this, new ToplevelEventArgs(top));
 		}
 
 		internal virtual void OnChildLoaded (Toplevel top)
 		{
-			ChildLoaded?.Invoke (top);
+			ChildLoaded?.Invoke (this, new ToplevelEventArgs (top));
 		}
 
 		internal virtual void OnClosed (Toplevel top)
 		{
-			Closed?.Invoke (top);
+			Closed?.Invoke (this, new ToplevelEventArgs (top));
 		}
 
 		internal virtual bool OnClosing (ToplevelClosingEventArgs ev)
@@ -149,17 +149,17 @@ namespace Terminal.Gui {
 			if (IsMdiContainer) {
 				SetChildNeedsDisplay ();
 			}
-			ChildClosed?.Invoke (top);
+			ChildClosed?.Invoke (this, new ToplevelEventArgs (top));
 		}
 
 		internal virtual void OnDeactivate (Toplevel activated)
 		{
-			Deactivate?.Invoke (activated);
+			Deactivate?.Invoke (this, new ToplevelEventArgs (activated));
 		}
 
 		internal virtual void OnActivate (Toplevel deactivated)
 		{
-			Activate?.Invoke (deactivated);
+			Activate?.Invoke (this, new ToplevelEventArgs(deactivated));
 		}
 
 		/// <summary>

+ 5 - 5
UICatalog/Scenarios/BackgroundWorkerCollection.cs

@@ -63,7 +63,7 @@ namespace UICatalog.Scenarios {
 				};
 			}
 
-			private void MdiMain_Closed (Toplevel obj)
+			private void MdiMain_Closed (object sender, ToplevelEventArgs e)
 			{
 				workerApp.Dispose ();
 				Dispose ();
@@ -82,14 +82,14 @@ namespace UICatalog.Scenarios {
 				}
 			}
 
-			private void MdiMain_Deactivate (Toplevel top)
+			private void MdiMain_Deactivate (object sender, ToplevelEventArgs top)
 			{
-				workerApp.WriteLog ($"{top.Data} deactivate.");
+				workerApp.WriteLog ($"{top.Toplevel.Data} deactivate.");
 			}
 
-			private void MdiMain_Activate (Toplevel top)
+			private void MdiMain_Activate (object sender, ToplevelEventArgs top)
 			{
-				workerApp.WriteLog ($"{top.Data} activate.");
+				workerApp.WriteLog ($"{top.Toplevel.Data} activate.");
 			}
 
 			private MenuBarItem View ()

+ 1 - 1
UICatalog/Scenarios/ContextMenus.cs

@@ -81,7 +81,7 @@ namespace UICatalog.Scenarios {
 
 			Win.WantMousePositionReports = true;
 
-			Application.Top.Closed += (_) => {
+			Application.Top.Closed += (s,e) => {
 				Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US");
 				Application.RootMouseEvent -= Application_RootMouseEvent;
 			};

+ 1 - 1
UICatalog/Scenarios/Dialogs.cs

@@ -226,7 +226,7 @@ namespace UICatalog.Scenarios {
 						}
 						dialog.LayoutSubviews ();
 					};
-					dialog.Closed += (args) => {
+					dialog.Closed += (s,e) => {
 						buttonPressedLabel.Text = $"{clicked}";
 					};
 					dialog.Add (addChar);

+ 1 - 1
UICatalog/Scenarios/Editor.cs

@@ -196,7 +196,7 @@ namespace UICatalog.Scenarios {
 				}
 			};
 
-			Application.Top.Closed += (_) => Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US");
+			Application.Top.Closed += (s,e) => Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US");
 		}
 
 		private void DisposeWinDialog ()

+ 1 - 1
UnitTests/Application/ApplicationTests.cs

@@ -497,7 +497,7 @@ namespace Terminal.Gui.ApplicationTests {
 				t2.RequestStop ();
 			};
 			// Now this will close the MdiContainer when all MdiChildes was closed
-			t2.Closed += (_) => {
+			t2.Closed += (s,_) => {
 				t1.RequestStop ();
 			};
 			Application.Iteration += () => {

+ 8 - 8
UnitTests/TopLevels/MdiTests.cs

@@ -91,7 +91,7 @@ namespace Terminal.Gui.TopLevelTests {
 				Assert.True (Application.Current == d);
 			};
 
-			d.Closed += (e) => Application.RequestStop (top1);
+			d.Closed += (s,e) => Application.RequestStop (top1);
 
 			Application.Iteration += () => {
 				Assert.Null (Application.MdiChildes);
@@ -155,7 +155,7 @@ else 					Assert.True (Application.Current == top1);
 			};
 
 			// Now this will close the MdiContainer propagating through the MdiChildes.
-			d.Closed += (e) => {
+			d.Closed += (s,e) => {
 				mdi.RequestStop ();
 			};
 
@@ -215,7 +215,7 @@ else 					Assert.True (Application.Current == top1);
 			};
 
 			// Now this will close the MdiContainer propagating through the MdiChildes.
-			d.Closed += (e) => Application.RequestStop (mdi);
+			d.Closed += (s,e) => Application.RequestStop (mdi);
 
 			Application.Iteration += () => {
 				if (iterations == 4) {
@@ -273,7 +273,7 @@ else 					Assert.True (Application.Current == top1);
 			};
 
 			// Now this will close the MdiContainer propagating through the MdiChildes.
-			d.Closed += (e) => Application.RequestStop (mdi);
+			d.Closed += (s, e) => Application.RequestStop (mdi);
 
 			Application.Iteration += () => {
 				if (iterations == 4) {
@@ -361,7 +361,7 @@ else 					Assert.True (Application.Current == top1);
 			};
 
 			// Now this will close the MdiContainer propagating through the MdiChildes.
-			d1.Closed += (e) => {
+			d1.Closed += (s, e) => {
 				Assert.True (Application.Current == d1);
 				Assert.False (Application.Current.Running);
 				mdi.RequestStop ();
@@ -434,7 +434,7 @@ else 					Assert.True (Application.Current == top1);
 			};
 
 			// Now this will close the MdiContainer propagating through the MdiChildes.
-			d1.Closed += (e) => {
+			d1.Closed += (s, e) => {
 				mdi.RequestStop ();
 			};
 
@@ -489,7 +489,7 @@ else 					Assert.True (Application.Current == top1);
 				c1.RequestStop ();
 			};
 			// Now this will close the MdiContainer propagating through the MdiChildes.
-			c1.Closed += (e) => {
+			c1.Closed += (s, e) => {
 				mdi.RequestStop ();
 			};
 			Application.Iteration += () => {
@@ -570,7 +570,7 @@ else 					Assert.True (Application.Current == top1);
 						stage.RequestStop ();
 					};
 
-					stage.Closed += (_) => {
+					stage.Closed += (_, _) => {
 						if (iterations == 11) 							allStageClosed = true;
 						Assert.Equal (iterations, Application.MdiChildes.Count);
 						if (running) {

+ 6 - 6
UnitTests/TopLevels/ToplevelTests.cs

@@ -154,13 +154,13 @@ namespace Terminal.Gui.TopLevelTests {
 
 			var eventInvoked = "";
 
-			top.ChildUnloaded += (e) => eventInvoked = "ChildUnloaded";
+			top.ChildUnloaded += (s,e) => eventInvoked = "ChildUnloaded";
 			top.OnChildUnloaded (top);
 			Assert.Equal ("ChildUnloaded", eventInvoked);
-			top.ChildLoaded += (e) => eventInvoked = "ChildLoaded";
+			top.ChildLoaded += (s,e) => eventInvoked = "ChildLoaded";
 			top.OnChildLoaded (top);
 			Assert.Equal ("ChildLoaded", eventInvoked);
-			top.Closed += (e) => eventInvoked = "Closed";
+			top.Closed += (s, e) => eventInvoked = "Closed";
 			top.OnClosed (top);
 			Assert.Equal ("Closed", eventInvoked);
 			top.Closing += (e) => eventInvoked = "Closing";
@@ -169,13 +169,13 @@ namespace Terminal.Gui.TopLevelTests {
 			top.AllChildClosed += (s, e) => eventInvoked = "AllChildClosed";
 			top.OnAllChildClosed ();
 			Assert.Equal ("AllChildClosed", eventInvoked);
-			top.ChildClosed += (e) => eventInvoked = "ChildClosed";
+			top.ChildClosed += (s,e) => eventInvoked = "ChildClosed";
 			top.OnChildClosed (top);
 			Assert.Equal ("ChildClosed", eventInvoked);
-			top.Deactivate += (e) => eventInvoked = "Deactivate";
+			top.Deactivate += (s,e) => eventInvoked = "Deactivate";
 			top.OnDeactivate (top);
 			Assert.Equal ("Deactivate", eventInvoked);
-			top.Activate += (e) => eventInvoked = "Activate";
+			top.Activate += (s,e) => eventInvoked = "Activate";
 			top.OnActivate (top);
 			Assert.Equal ("Activate", eventInvoked);
 			top.Loaded += (s, e) => eventInvoked = "Loaded";

+ 3 - 3
UnitTests/TopLevels/WizardTests.cs

@@ -523,7 +523,7 @@ namespace Terminal.Gui.TopLevelTests {
 			};
 
 			var closedFired = false;
-			wizard.Closed += (args) => {
+			wizard.Closed += (s, e) => {
 				closedFired = true;
 			};
 
@@ -553,7 +553,7 @@ namespace Terminal.Gui.TopLevelTests {
 			};
 
 			closedFired = false;
-			wizard.Closed += (args) => {
+			wizard.Closed += (s,e) => {
 				closedFired = true;
 			};
 
@@ -591,7 +591,7 @@ namespace Terminal.Gui.TopLevelTests {
 			};
 
 			closedFired = false;
-			wizard.Closed += (args) => {
+			wizard.Closed += (s,e) => {
 				closedFired = true;
 			};