瀏覽代碼

Update Added/Removed and other events to use `EventHandler

tznind 2 年之前
父節點
當前提交
9c4f3eda16

+ 4 - 4
Terminal.Gui/Configuration/ConfigurationManager.cs

@@ -353,14 +353,14 @@ namespace Terminal.Gui.Configuration {
 		public static void OnUpdated ()
 		public static void OnUpdated ()
 		{
 		{
 			Debug.WriteLine ($"ConfigurationManager.OnApplied()");
 			Debug.WriteLine ($"ConfigurationManager.OnApplied()");
-			Updated?.Invoke (new ConfigurationManagerEventArgs ());
+			Updated?.Invoke (null, new ConfigurationManagerEventArgs ());
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
 		/// Event fired when the configuration has been updated from a configuration source.  
 		/// Event fired when the configuration has been updated from a configuration source.  
 		/// application.
 		/// application.
 		/// </summary>
 		/// </summary>
-		public static event Action<ConfigurationManagerEventArgs>? Updated;
+		public static event EventHandler<ConfigurationManagerEventArgs>? Updated;
 
 
 		/// <summary>
 		/// <summary>
 		/// Resets the state of <see cref="ConfigurationManager"/>. Should be called whenever a new app session
 		/// Resets the state of <see cref="ConfigurationManager"/>. Should be called whenever a new app session
@@ -440,14 +440,14 @@ namespace Terminal.Gui.Configuration {
 		public static void OnApplied ()
 		public static void OnApplied ()
 		{
 		{
 			Debug.WriteLine ($"ConfigurationManager.OnApplied()");
 			Debug.WriteLine ($"ConfigurationManager.OnApplied()");
-			Applied?.Invoke (new ConfigurationManagerEventArgs ());
+			Applied?.Invoke (null, new ConfigurationManagerEventArgs ());
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
 		/// Event fired when an updated configuration has been applied to the  
 		/// Event fired when an updated configuration has been applied to the  
 		/// application.
 		/// application.
 		/// </summary>
 		/// </summary>
-		public static event Action<ConfigurationManagerEventArgs>? Applied;
+		public static event EventHandler<ConfigurationManagerEventArgs>? Applied;
 
 
 		/// <summary>
 		/// <summary>
 		/// Name of the running application. By default this property is set to the application's assembly name.
 		/// Name of the running application. By default this property is set to the application's assembly name.

+ 2 - 2
Terminal.Gui/Core/Application.cs

@@ -257,7 +257,7 @@ namespace Terminal.Gui {
 		///	<see cref="Begin(Toplevel)"/> must also subscribe to <see cref="NotifyStopRunState"/>
 		///	<see cref="Begin(Toplevel)"/> must also subscribe to <see cref="NotifyStopRunState"/>
 		///	and manually dispose of the <see cref="RunState"/> token when the application is done.
 		///	and manually dispose of the <see cref="RunState"/> token when the application is done.
 		/// </remarks>
 		/// </remarks>
-		public static event Action<RunState> NotifyNewRunState;
+		public static event EventHandler<RunStateEventArgs> NotifyNewRunState;
 
 
 		/// <summary>
 		/// <summary>
 		/// Notify that a existent <see cref="RunState"/> is stopping (<see cref="End(RunState)"/> was called).
 		/// Notify that a existent <see cref="RunState"/> is stopping (<see cref="End(RunState)"/> was called).
@@ -1029,7 +1029,7 @@ namespace Terminal.Gui {
 				Driver.Refresh ();
 				Driver.Refresh ();
 			}
 			}
 
 
-			NotifyNewRunState?.Invoke (rs);
+			NotifyNewRunState?.Invoke (toplevel, new RunStateEventArgs(rs));
 			return rs;
 			return rs;
 		}
 		}
 
 

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

@@ -74,7 +74,7 @@ namespace Terminal.Gui {
 			}
 			}
 		}
 		}
 
 
-		private void Top_Removed (View obj)
+		private void Top_Removed (object sender, ViewEventArgs 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 (View obj)
+		private void Parent_Removed (object sender, ViewEventArgs e)
 		{
 		{
 			BorderBrush = default;
 			BorderBrush = default;
 			Background = default;
 			Background = default;

+ 18 - 0
Terminal.Gui/Core/EventArgs/RunStateEventArgs.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using static Terminal.Gui.Application;
+
+namespace Terminal.Gui {
+	public class RunStateEventArgs : EventArgs {
+
+		public RunStateEventArgs (RunState state)
+		{
+			State = state;
+		}
+
+		public RunState State { get; }
+	}
+}

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

@@ -91,7 +91,7 @@ namespace Terminal.Gui {
 		/// Invoked when the Toplevel's <see cref="Application.RunState"/> is being closed by  
 		/// Invoked when the Toplevel's <see cref="Application.RunState"/> is being closed by  
 		/// <see cref="Application.RequestStop(Toplevel)"/>.
 		/// <see cref="Application.RequestStop(Toplevel)"/>.
 		/// </summary>
 		/// </summary>
-		public event Action<ToplevelClosingEventArgs> Closing;
+		public event EventHandler<ToplevelClosingEventArgs> Closing;
 
 
 		/// <summary>
 		/// <summary>
 		/// Invoked when the Toplevel's <see cref="Application.RunState"/> is closed by <see cref="Application.End(Application.RunState)"/>.
 		/// Invoked when the Toplevel's <see cref="Application.RunState"/> is closed by <see cref="Application.End(Application.RunState)"/>.
@@ -135,7 +135,7 @@ namespace Terminal.Gui {
 
 
 		internal virtual bool OnClosing (ToplevelClosingEventArgs ev)
 		internal virtual bool OnClosing (ToplevelClosingEventArgs ev)
 		{
 		{
-			Closing?.Invoke (ev);
+			Closing?.Invoke (this, ev);
 			return ev.Cancel;
 			return ev.Cancel;
 		}
 		}
 
 

+ 11 - 9
Terminal.Gui/Core/View.cs

@@ -119,12 +119,12 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// <summary>
 		/// Event fired when a subview is being added to this view.
 		/// Event fired when a subview is being added to this view.
 		/// </summary>
 		/// </summary>
-		public event Action<View> Added;
+		public event EventHandler<ViewEventArgs> Added;
 
 
 		/// <summary>
 		/// <summary>
 		/// Event fired when a subview is being removed from this view.
 		/// Event fired when a subview is being removed from this view.
 		/// </summary>
 		/// </summary>
-		public event Action<View> Removed;
+		public event EventHandler<ViewEventArgs> 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 (view);
+			OnAdded (new ViewEventArgs(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 (view);
+			OnRemoved (new ViewEventArgs(view));
 			if (focused == view) {
 			if (focused == view) {
 				focused = null;
 				focused = null;
 			}
 			}
@@ -1354,26 +1354,28 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// <summary>
 		/// 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="view">The subview being added.</param>
-		public virtual void OnAdded (View view)
+		/// <param name="e">Event where <see cref="ViewEventArgs.View"/> is the subview being added.</param>
+		public virtual void OnAdded (ViewEventArgs e)
 		{
 		{
+			var view = e.View;
 			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;
 			view.width ??= view.frame.Width;
 			view.width ??= view.frame.Width;
 			view.height ??= view.frame.Height;
 			view.height ??= view.frame.Height;
 
 
-			view.Added?.Invoke (this);
+			view.Added?.Invoke (this,e);
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
 		/// 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="view">The subview being removed.</param>
 		/// <param name="view">The subview being removed.</param>
-		public virtual void OnRemoved (View view)
+		public virtual void OnRemoved (ViewEventArgs e)
 		{
 		{
+			var view = e.View;
 			view.IsAdded = false;
 			view.IsAdded = false;
-			view.Removed?.Invoke (this);
+			view.Removed?.Invoke (this, e);
 		}
 		}
 
 
 		/// <inheritdoc/>
 		/// <inheritdoc/>

+ 1 - 1
Terminal.Gui/Views/ComboBox.cs

@@ -316,7 +316,7 @@ namespace Terminal.Gui {
 				}
 				}
 			};
 			};
 
 
-			Added += (View v) => {
+			Added += (s,  e) => {
 
 
 				// Determine if this view is hosted inside a dialog and is the only control
 				// Determine if this view is hosted inside a dialog and is the only control
 				for (View view = this.SuperView; view != null; view = view.SuperView) {
 				for (View view = this.SuperView; view != null; view = view.SuperView) {

+ 1 - 1
Terminal.Gui/Views/ContextMenu.cs

@@ -152,7 +152,7 @@ namespace Terminal.Gui {
 			}
 			}
 		}
 		}
 
 
-		private void Container_Closing (ToplevelClosingEventArgs obj)
+		private void Container_Closing (object sender, ToplevelClosingEventArgs obj)
 		{
 		{
 			Hide ();
 			Hide ();
 		}
 		}

+ 1 - 1
Terminal.Gui/Windows/Wizard.cs

@@ -392,7 +392,7 @@ namespace Terminal.Gui {
 
 
 		private bool finishedPressed = false;
 		private bool finishedPressed = false;
 
 
-		private void Wizard_Closing (ToplevelClosingEventArgs obj)
+		private void Wizard_Closing (object sender, ToplevelClosingEventArgs obj)
 		{
 		{
 			if (!finishedPressed) {
 			if (!finishedPressed) {
 				var args = new WizardButtonEventArgs ();
 				var args = new WizardButtonEventArgs ();

+ 1 - 1
UICatalog/KeyBindingsDialog.cs

@@ -61,7 +61,7 @@ namespace UICatalog {
 					RecordView (sub);
 					RecordView (sub);
 				}
 				}
 
 
-				view.Added += RecordView;
+				view.Added += (s,e)=>RecordView(e.View);
 			}
 			}
 
 
 			internal static void Initialize ()
 			internal static void Initialize ()

+ 1 - 1
UICatalog/UICatalog.cs

@@ -387,7 +387,7 @@ namespace UICatalog {
 				Unloaded -= UnloadedHandler;
 				Unloaded -= UnloadedHandler;
 			}
 			}
 			
 			
-			void ConfigAppliedHandler (ConfigurationManagerEventArgs a)
+			void ConfigAppliedHandler (object sender, ConfigurationManagerEventArgs a)
 			{
 			{
 				ConfigChanged ();
 				ConfigChanged ();
 			}
 			}

+ 7 - 7
UnitTests/Application/ApplicationTests.cs

@@ -143,9 +143,9 @@ namespace Terminal.Gui.ApplicationTests {
 			};
 			};
 
 
 			Application.RunState runstate = null;
 			Application.RunState runstate = null;
-			Action<Application.RunState> NewRunStateFn = (rs) => {
-				Assert.NotNull (rs);
-				runstate = rs;
+			EventHandler<RunStateEventArgs> NewRunStateFn = (s, e) => {
+				Assert.NotNull (e.State);
+				runstate = e.State;
 			};
 			};
 			Application.NotifyNewRunState += NewRunStateFn;
 			Application.NotifyNewRunState += NewRunStateFn;
 
 
@@ -188,9 +188,9 @@ namespace Terminal.Gui.ApplicationTests {
 			Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ());
 			Application.InternalInit (() => topLevel = new TestToplevel (), new FakeDriver ());
 
 
 			Application.RunState runstate = null;
 			Application.RunState runstate = null;
-			Action<Application.RunState> NewRunStateFn = (rs) => {
-				Assert.NotNull (rs);
-				runstate = rs;
+			EventHandler<RunStateEventArgs> NewRunStateFn = (s, e) => {
+				Assert.NotNull (e.State);
+				runstate = e.State;
 			};
 			};
 			Application.NotifyNewRunState += NewRunStateFn;
 			Application.NotifyNewRunState += NewRunStateFn;
 
 
@@ -730,7 +730,7 @@ namespace Terminal.Gui.ApplicationTests {
 			var top = Application.Top;
 			var top = Application.Top;
 			var isQuiting = false;
 			var isQuiting = false;
 
 
-			top.Closing += (e) => {
+			top.Closing += (s,e) => {
 				isQuiting = true;
 				isQuiting = true;
 				e.Cancel = true;
 				e.Cancel = true;
 			};
 			};

+ 2 - 2
UnitTests/Configuration/ConfigurationMangerTests.cs

@@ -775,7 +775,7 @@ namespace Terminal.Gui.ConfigurationTests {
 
 
 			ConfigurationManager.Updated += ConfigurationManager_Updated;
 			ConfigurationManager.Updated += ConfigurationManager_Updated;
 			bool fired = false;
 			bool fired = false;
-			void ConfigurationManager_Updated (ConfigurationManager.ConfigurationManagerEventArgs obj)
+			void ConfigurationManager_Updated (object sender, ConfigurationManager.ConfigurationManagerEventArgs obj)
 			{
 			{
 				fired = true;
 				fired = true;
 				// assert
 				// assert
@@ -801,7 +801,7 @@ namespace Terminal.Gui.ConfigurationTests {
 			ConfigurationManager.Reset ();
 			ConfigurationManager.Reset ();
 			ConfigurationManager.Applied += ConfigurationManager_Applied;
 			ConfigurationManager.Applied += ConfigurationManager_Applied;
 			bool fired = false;
 			bool fired = false;
-			void ConfigurationManager_Applied (ConfigurationManager.ConfigurationManagerEventArgs obj)
+			void ConfigurationManager_Applied (object sender, ConfigurationManager.ConfigurationManagerEventArgs obj)
 			{
 			{
 				fired = true;
 				fired = true;
 				// assert
 				// assert

+ 15 - 15
UnitTests/Core/ViewTests.cs

@@ -238,11 +238,11 @@ namespace Terminal.Gui.CoreTests {
 			var v = new View (new Rect (0, 0, 10, 24));
 			var v = new View (new Rect (0, 0, 10, 24));
 			var t = new View ();
 			var t = new View ();
 
 
-			v.Added += (View e) => {
-				Assert.True (v.SuperView == e);
+			v.Added += (s,e) => {
+				Assert.True (v.SuperView == e.View);
 			};
 			};
 
 
-			v.Removed += (View e) => {
+			v.Removed += (s, e) => {
 				Assert.True (v.SuperView == null);
 				Assert.True (v.SuperView == null);
 			};
 			};
 
 
@@ -654,21 +654,21 @@ 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 += (e) => {
-				Assert.Equal (e.Frame.Width, w.Frame.Width);
-				Assert.Equal (e.Frame.Height, w.Frame.Height);
+			w.Added += (s,e) => {
+				Assert.Equal (e.View.Frame.Width, w.Frame.Width);
+				Assert.Equal (e.View.Frame.Height, w.Frame.Height);
 			};
 			};
-			v1.Added += (e) => {
-				Assert.Equal (e.Frame.Width, v1.Frame.Width);
-				Assert.Equal (e.Frame.Height, v1.Frame.Height);
+			v1.Added += (s, e) => {
+				Assert.Equal (e.View.Frame.Width, v1.Frame.Width);
+				Assert.Equal (e.View.Frame.Height, v1.Frame.Height);
 			};
 			};
-			v2.Added += (e) => {
-				Assert.Equal (e.Frame.Width, v2.Frame.Width);
-				Assert.Equal (e.Frame.Height, v2.Frame.Height);
+			v2.Added += (s, e) => {
+				Assert.Equal (e.View.Frame.Width, v2.Frame.Width);
+				Assert.Equal (e.View.Frame.Height, v2.Frame.Height);
 			};
 			};
-			sv1.Added += (e) => {
-				Assert.Equal (e.Frame.Width, sv1.Frame.Width);
-				Assert.Equal (e.Frame.Height, sv1.Frame.Height);
+			sv1.Added += (s, e) => {
+				Assert.Equal (e.View.Frame.Width, sv1.Frame.Width);
+				Assert.Equal (e.View.Frame.Height, sv1.Frame.Height);
 			};
 			};
 
 
 			t.Initialized += (s, e) => {
 			t.Initialized += (s, e) => {

+ 4 - 4
UnitTests/TopLevels/ToplevelTests.cs

@@ -163,7 +163,7 @@ namespace Terminal.Gui.TopLevelTests {
 			top.Closed += (s, e) => eventInvoked = "Closed";
 			top.Closed += (s, e) => eventInvoked = "Closed";
 			top.OnClosed (top);
 			top.OnClosed (top);
 			Assert.Equal ("Closed", eventInvoked);
 			Assert.Equal ("Closed", eventInvoked);
-			top.Closing += (e) => eventInvoked = "Closing";
+			top.Closing += (s,e) => eventInvoked = "Closing";
 			top.OnClosing (new ToplevelClosingEventArgs (top));
 			top.OnClosing (new ToplevelClosingEventArgs (top));
 			Assert.Equal ("Closing", eventInvoked);
 			Assert.Equal ("Closing", eventInvoked);
 			top.AllChildClosed += (s, e) => eventInvoked = "AllChildClosed";
 			top.AllChildClosed += (s, e) => eventInvoked = "AllChildClosed";
@@ -356,7 +356,7 @@ namespace Terminal.Gui.TopLevelTests {
 			var top = Application.Top;
 			var top = Application.Top;
 			top.Add (win1, win2);
 			top.Add (win1, win2);
 			top.Loaded += (s, e) => isRunning = true;
 			top.Loaded += (s, e) => isRunning = true;
-			top.Closing += (_) => isRunning = false;
+			top.Closing += (s,e) => isRunning = false;
 			Application.Begin (top);
 			Application.Begin (top);
 			top.Running = true;
 			top.Running = true;
 
 
@@ -465,7 +465,7 @@ namespace Terminal.Gui.TopLevelTests {
 			var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
 			var tf2W2 = new TextField ("Text2 on Win2") { X = Pos.Left (tf1W2), Width = Dim.Fill () };
 			win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
 			win2.Add (lblTf1W2, tf1W2, lblTvW2, tvW2, lblTf2W2, tf2W2);
 
 
-			win1.Closing += (_) => isRunning = false;
+			win1.Closing += (s, e) => isRunning = false;
 			Assert.Null (top.Focused);
 			Assert.Null (top.Focused);
 			Assert.Equal (top, Application.Current);
 			Assert.Equal (top, Application.Current);
 			Assert.True (top.IsCurrentTop);
 			Assert.True (top.IsCurrentTop);
@@ -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 (View obj)
+			void View_Added (object sender, ViewEventArgs 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);