소스 검색

POC of IDisposable support

Charlie Kindel 5 년 전
부모
커밋
3a42318b74
4개의 변경된 파일70개의 추가작업 그리고 2개의 파일을 삭제
  1. 3 0
      Terminal.Gui/Core/Application.cs
  2. 40 1
      Terminal.Gui/Core/Responder.cs
  3. 9 0
      Terminal.Gui/Core/View.cs
  4. 18 1
      Terminal.Gui/Views/StatusBar.cs

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

@@ -241,6 +241,7 @@ namespace Terminal.Gui {
 			{
 				if (Toplevel != null) {
 					End (Toplevel, disposing);
+					Toplevel.Dispose ();
 					Toplevel = null;
 				}
 			}
@@ -500,12 +501,14 @@ namespace Terminal.Gui {
 			// TODO: Some of this state is actually related to Begin/End (not Init/Shutdown) and should be moved to `RunState` (#520)
 			foreach (var t in toplevels) {
 				t.Running = false;
+				t.Dispose ();
 			}
 			toplevels.Clear ();
 			Current = null;
 			CurrentView = null;
 			Top = null;
 
+
 			// Closes the application if it's true.
 			if (closeDriver) {
 				MainLoop = null;

+ 40 - 1
Terminal.Gui/Core/Responder.cs

@@ -13,11 +13,15 @@
 // Optimziations
 //   - Add rendering limitation to the exposed area
 
+using System;
+
 namespace Terminal.Gui {
 	/// <summary>
 	/// Responder base class implemented by objects that want to participate on keyboard and mouse input.
 	/// </summary>
-	public class Responder {
+	public class Responder : IDisposable {
+		bool disposedValue;
+
 		/// <summary>
 		/// Gets or sets a value indicating whether this <see cref="Responder"/> can focus.
 		/// </summary>
@@ -181,5 +185,40 @@ namespace Terminal.Gui {
 		{
 			return false;
 		}
+
+		/// <summary>
+		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
+		/// </summary>
+		/// <remarks>
+		/// If disposing equals true, the method has been called directly
+		/// or indirectly by a user's code. Managed and unmanaged resources
+		/// can be disposed.
+		/// If disposing equals false, the method has been called by the
+		/// runtime from inside the finalizer and you should not reference
+		/// other objects. Only unmanaged resources can be disposed.		
+		/// </remarks>
+		/// <param name="disposing"></param>
+		protected virtual void Dispose (bool disposing)
+		{
+			if (!disposedValue) {
+				if (disposing) {
+					// TODO: dispose managed state (managed objects)
+				}
+
+				// TODO: free unmanaged resources (unmanaged objects) and override finalizer
+				// TODO: set large fields to null
+				disposedValue = true;
+			}
+		}
+
+		/// <summary>
+		/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resource.
+		/// </summary>
+		public void Dispose ()
+		{
+			// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
+			Dispose (disposing: true);
+			GC.SuppressFinalize (this);
+		}
 	}
 }

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

@@ -1684,5 +1684,14 @@ namespace Terminal.Gui {
 
 			return false;
 		}
+
+		protected override void Dispose (bool disposing)
+		{
+			foreach (var subview in InternalSubviews) {
+				subview.Dispose ();
+			}
+
+			base.Dispose (disposing);
+		}
 	}
 }

+ 18 - 1
Terminal.Gui/Views/StatusBar.cs

@@ -63,6 +63,8 @@ namespace Terminal.Gui {
 	/// So for each context must be a new instance of a statusbar.
 	/// </summary>
 	public class StatusBar : View {
+		bool disposedValue;
+
 		/// <summary>
 		/// The items that compose the <see cref="StatusBar"/>
 		/// </summary>
@@ -73,6 +75,8 @@ namespace Terminal.Gui {
 		/// </summary>
 		public StatusBar () : this (items: new StatusItem [] { }) { }
 
+		Action<Application.ResizedEventArgs> cachedResizedHandler;
+
 		/// <summary>
 		/// Initializes a new instance of the <see cref="StatusBar"/> class with the specified set of <see cref="StatusItem"/>s.
 		/// The <see cref="StatusBar"/> will be drawn on the lowest line of the terminal or <see cref="View.SuperView"/> (if not null).
@@ -90,7 +94,7 @@ namespace Terminal.Gui {
 			Width = Dim.Fill ();
 			Height = 1;
 
-			Application.Resized += (e) => {
+			cachedResizedHandler = (action) => {
 				X = 0;
 				Height = 1;
 				if (SuperView == null || SuperView == Application.Top) {
@@ -99,6 +103,8 @@ namespace Terminal.Gui {
 					Y = Pos.Bottom (SuperView);
 				}
 			};
+
+			Application.Resized += cachedResizedHandler;
 		}
 
 		Attribute ToggleScheme (Attribute scheme)
@@ -192,5 +198,16 @@ namespace Terminal.Gui {
 				return false;
 			});
 		}
+
+		/// <inheritdoc/>
+		protected override void Dispose (bool disposing)
+		{
+			if (!disposedValue) {
+				if (disposing) {
+					Application.Resized -= cachedResizedHandler;
+				}
+				disposedValue = true;
+			}
+		}
 	}
 }