Browse Source

Added oldTitle to eventargs and fixed title = null

Charlie Kindel 3 years ago
parent
commit
59577feecf

+ 22 - 9
Terminal.Gui/Core/Window.cs

@@ -23,7 +23,7 @@ namespace Terminal.Gui {
 	/// </remarks>
 	public class Window : Toplevel {
 		View contentView;
-		ustring title;
+		ustring title = ustring.Empty;
 
 		/// <summary>
 		/// The title to be displayed for this window.
@@ -32,9 +32,10 @@ namespace Terminal.Gui {
 		public ustring Title {
 			get => title;
 			set {
-				if (!OnTitleChanging (value)) {
+				if (!OnTitleChanging (title, value)) {
+					var old = title;
 					title = value;
-					OnTitleChanged (title);
+					OnTitleChanged (old, title);
 				}
 				SetNeedsDisplay ();
 			}
@@ -174,6 +175,7 @@ namespace Terminal.Gui {
 		{
 			CanFocus = true;
 			ColorScheme = Colors.Base;
+			if (title == null) title = ustring.Empty;
 			Title = title;
 			if (border == null) {
 				Border = new Border () {
@@ -348,26 +350,35 @@ namespace Terminal.Gui {
 			public ustring NewTitle { get; set; }
 
 			/// <summary>
-			/// Flag which allows cancelling changing to the new TItle value.
+			/// The old Window Title.
+			/// </summary>
+			public ustring OldTitle { get; set; }
+
+			/// <summary>
+			/// Flag which allows cancelling the Title change.
 			/// </summary>
 			public bool Cancel { get; set; }
 
 			/// <summary>
 			/// Initializes a new instance of <see cref="TitleEventArgs"/>
 			/// </summary>
+			/// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
 			/// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
-			public TitleEventArgs (ustring newTitle)
+			public TitleEventArgs (ustring oldTitle, ustring newTitle)
 			{
+				OldTitle = oldTitle;
 				NewTitle = newTitle;
 			}
 		}
 		/// <summary>
 		/// Called before the <see cref="Window.Title"/> changes. Invokes the <see cref="TitleChanging"/> event, which can be cancelled.
 		/// </summary>
+		/// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
+		/// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
 		/// <returns>`true` if an event handler cancelled the Title change.</returns>
-		public virtual bool OnTitleChanging (ustring newTitle)
+		public virtual bool OnTitleChanging (ustring oldTitle, ustring newTitle)
 		{
-			var args = new TitleEventArgs (newTitle);
+			var args = new TitleEventArgs (oldTitle, newTitle);
 			TitleChanging?.Invoke (args);
 			return args.Cancel;
 		}
@@ -381,9 +392,11 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// Called when the <see cref="Window.Title"/> has been changed. Invokes the <see cref="TitleChanged"/> event.
 		/// </summary>
-		public virtual void OnTitleChanged (ustring newTitle)
+		/// <param name="oldTitle">The <see cref="Window.Title"/> that is/has been replaced.</param>
+		/// <param name="newTitle">The new <see cref="Window.Title"/> to be replaced.</param>
+		public virtual void OnTitleChanged (ustring oldTitle, ustring newTitle)
 		{
-			var args = new TitleEventArgs (title);
+			var args = new TitleEventArgs (oldTitle, newTitle);
 			TitleChanged?.Invoke (args);
 		}
 

+ 2 - 1
Terminal.Gui/Views/FrameView.cs

@@ -108,7 +108,8 @@ namespace Terminal.Gui {
 
 		void Initialize (Rect frame, ustring title, View [] views = null, Border border = null)
 		{
-			this.title = title;
+			if (title == null) title = ustring.Empty;
+			this.Title = title;
 			if (border == null) {
 				Border = new Border () {
 					BorderStyle = BorderStyle.Single

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

@@ -359,7 +359,7 @@ namespace Terminal.Gui {
 		void SetTitle (ustring title)
 		{
 			if (title == null)
-				title = "";
+				title = ustring.Empty;
 			Title = title;
 		}
 

+ 14 - 4
UnitTests/WindowTests.cs

@@ -5,6 +5,7 @@ using GraphViewTests = Terminal.Gui.Views.GraphViewTests;
 
 // Alias Console to MockConsole so we don't accidentally use Console
 using Console = Terminal.Gui.FakeConsole;
+using NStack;
 
 namespace Terminal.Gui.Core {
 	public class WindowTests {
@@ -21,7 +22,7 @@ namespace Terminal.Gui.Core {
 			// Parameterless
 			var r = new Window ();
 			Assert.NotNull (r);
-			Assert.Null (r.Title);
+			Assert.Equal(ustring.Empty, r.Title);
 			Assert.Equal (LayoutStyle.Computed, r.LayoutStyle);
 			Assert.Equal ("Window()({X=0,Y=0,Width=0,Height=0})", r.ToString ());
 			Assert.True (r.CanFocus);
@@ -100,24 +101,29 @@ namespace Terminal.Gui.Core {
 		public void Set_Title_Fires_TitleChanging ()
 		{
 			var r = new Window ();
-			Assert.Null (r.Title);
+			Assert.Equal (ustring.Empty, r.Title);
 
-			string expectedAfter = null;
+			string expectedOld = null;
 			string expectedDuring = null;
+			string expectedAfter = null;
 			bool cancel = false;
 			r.TitleChanging += (args) => {
+				Assert.Equal (expectedOld, args.OldTitle);
 				Assert.Equal (expectedDuring, args.NewTitle);
 				args.Cancel = cancel;
 			};
 
+			expectedOld = string.Empty;
 			r.Title = expectedDuring = expectedAfter = "title";
 			Assert.Equal (expectedAfter, r.Title.ToString());
 
+			expectedOld = r.Title.ToString();
 			r.Title = expectedDuring = expectedAfter = "a different title";
 			Assert.Equal (expectedAfter, r.Title.ToString ());
 
 			// Now setup cancelling the change and change it back to "title"
 			cancel = true;
+			expectedOld = r.Title.ToString();
 			r.Title = expectedDuring = "title";
 			Assert.Equal (expectedAfter, r.Title.ToString ());
 			r.Dispose ();
@@ -128,18 +134,22 @@ namespace Terminal.Gui.Core {
 		public void Set_Title_Fires_TitleChanged ()
 		{
 			var r = new Window ();
-			Assert.Null (r.Title);
+			Assert.Equal (ustring.Empty, r.Title);
 
+			string expectedOld = null;
 			string expected = null;
 			r.TitleChanged += (args) => {
+				Assert.Equal (expectedOld, args.OldTitle);
 				Assert.Equal (r.Title, args.NewTitle);
 			};
 
 			expected = "title";
+			expectedOld = r.Title.ToString ();
 			r.Title = expected;
 			Assert.Equal (expected, r.Title.ToString ());
 
 			expected = "another title";
+			expectedOld = r.Title.ToString ();
 			r.Title = expected;
 			Assert.Equal (expected, r.Title.ToString ());
 			r.Dispose ();