浏览代码

Added a Unloaded event to the Toplevel class.

BDisp 4 年之前
父节点
当前提交
19c684a3e1
共有 3 个文件被更改,包括 31 次插入1 次删除
  1. 1 0
      Terminal.Gui/Core/Application.cs
  2. 15 1
      Terminal.Gui/Core/Toplevel.cs
  3. 15 0
      UnitTests/ApplicationTests.cs

+ 1 - 0
Terminal.Gui/Core/Application.cs

@@ -484,6 +484,7 @@ namespace Terminal.Gui {
 			if (runState == null)
 			if (runState == null)
 				throw new ArgumentNullException (nameof (runState));
 				throw new ArgumentNullException (nameof (runState));
 
 
+			runState.Toplevel.OnUnloaded ();
 			runState.Dispose ();
 			runState.Dispose ();
 		}
 		}
 
 

+ 15 - 1
Terminal.Gui/Core/Toplevel.cs

@@ -61,6 +61,12 @@ namespace Terminal.Gui {
 		/// </summary>
 		/// </summary>
 		public event Action Ready;
 		public event Action Ready;
 
 
+		/// <summary>
+		/// Fired once the Toplevel's <see cref="Application.RunState"/> has begin unloaded.
+		/// A Unloaded event handler is a good place to disposing after calling `<see cref="Application.End(Application.RunState)"/>.
+		/// </summary>
+		public event Action Unloaded;
+
 		/// <summary>
 		/// <summary>
 		/// Called from <see cref="Application.Begin(Toplevel)"/> before the <see cref="Toplevel"/> is redraws for the first time.
 		/// Called from <see cref="Application.Begin(Toplevel)"/> before the <see cref="Toplevel"/> is redraws for the first time.
 		/// </summary>
 		/// </summary>
@@ -70,13 +76,21 @@ namespace Terminal.Gui {
 		}
 		}
 
 
 		/// <summary>
 		/// <summary>
-		/// Called from <see cref="Application.RunLoop"/> after the <see cref="Toplevel"/> has entered it's first iteration of the loop. 
+		/// Called from <see cref="Application.RunLoop"/> after the <see cref="Toplevel"/> has entered it's first iteration of the loop.
 		/// </summary>
 		/// </summary>
 		internal virtual void OnReady ()
 		internal virtual void OnReady ()
 		{
 		{
 			Ready?.Invoke ();
 			Ready?.Invoke ();
 		}
 		}
 
 
+		/// <summary>
+		/// Called from <see cref="Application.End(Application.RunState)"/> before the <see cref="Toplevel"/> is disposed.
+		/// </summary>
+		internal virtual void OnUnloaded ()
+		{
+			Unloaded?.Invoke ();
+		}
+
 		/// <summary>
 		/// <summary>
 		/// Initializes a new instance of the <see cref="Toplevel"/> class with the specified absolute layout.
 		/// Initializes a new instance of the <see cref="Toplevel"/> class with the specified absolute layout.
 		/// </summary>
 		/// </summary>

+ 15 - 0
UnitTests/ApplicationTests.cs

@@ -212,5 +212,20 @@ namespace Terminal.Gui {
 			Assert.Null (Application.MainLoop);
 			Assert.Null (Application.MainLoop);
 			Assert.Null (Application.Driver);
 			Assert.Null (Application.Driver);
 		}
 		}
+
+		[Fact]
+		public void Loaded_Ready_Unlodaded_Events ()
+		{
+			Init ();
+			var top = Application.Top;
+			var count = 0;
+			top.Loaded += () => count++;
+			top.Ready += () => count++;
+			top.Unloaded += () => count++;
+			Application.Iteration = () => Application.RequestStop ();
+			Application.Run ();
+			Application.Shutdown ();
+			Assert.Equal (3, count);
+		}
 	}
 	}
 }
 }