Pārlūkot izejas kodu

Refactor HotKeyChanged event

tznind 2 gadi atpakaļ
vecāks
revīzija
a949b790ec

+ 29 - 0
Terminal.Gui/Core/EventArgs/HotKeyChangedEventArgs.cs

@@ -0,0 +1,29 @@
+using System;
+
+namespace Terminal.Gui {
+
+	/// <summary>
+	/// Event args for when a <see cref="Key"/> is changed from
+	/// one value to a new value (e.g. in <see cref="View.HotKeyChanged"/>)
+	/// </summary>
+	public class KeyChangedEventArgs : EventArgs {
+
+		/// <summary>
+		/// Gets the old <see cref="Key"/> that was set before the event.
+		/// Use <see cref="Key.Null"/> to check for empty.
+		/// </summary>
+		public Key OldKey { get; }
+
+		/// <summary>
+		/// Gets the new <see cref="Key"/> that is being used.
+		/// Use <see cref="Key.Null"/> to check for empty.
+		/// </summary>
+		public Key NewKey { get; }
+
+		public KeyChangedEventArgs (Key oldKey, Key newKey)
+		{
+			this.OldKey = oldKey;
+			this.NewKey = newKey;
+		}
+	}
+}

+ 24 - 21
Terminal.Gui/Core/EventArgs/TimeoutEventArgs.cs

@@ -2,30 +2,33 @@
 using Terminal.Gui;
 using static Terminal.Gui.MainLoop;
 
-/// <summary>
-/// <see cref="EventArgs"/> for timeout events (e.g. <see cref="MainLoop.TimeoutAdded"/>)
-/// </summary>
-public class TimeoutEventArgs : EventArgs {
+namespace Terminal.Gui {
 	/// <summary>
-	/// Gets the timeout callback handler
+	/// <see cref="EventArgs"/> for timeout events (e.g. <see cref="MainLoop.TimeoutAdded"/>)
 	/// </summary>
-	public Timeout Timeout { get; }
+	public class TimeoutEventArgs : EventArgs {
+		/// <summary>
+		/// Gets the timeout callback handler
+		/// </summary>
+		public Timeout Timeout { get; }
 
-	/// <summary>
-	/// Gets the <see cref="DateTime.Ticks"/> in UTC time when the 
-	/// <see cref="Timeout"/> will next execute after.
-	/// </summary>
-	public long Ticks { get; }
+		/// <summary>
+		/// Gets the <see cref="DateTime.Ticks"/> in UTC time when the 
+		/// <see cref="Timeout"/> will next execute after.
+		/// </summary>
+		public long Ticks { get; }
 
 
-	/// <summary>
-	/// Creates a new instance of the <see cref="TimeoutEventArgs"/> class.
-	/// </summary>
-	/// <param name="timeout"></param>
-	/// <param name="ticks"></param>
-	public TimeoutEventArgs (Timeout timeout, long ticks)
-	{
-		this.Timeout = timeout;
-		this.Ticks = ticks;
+		/// <summary>
+		/// Creates a new instance of the <see cref="TimeoutEventArgs"/> class.
+		/// </summary>
+		/// <param name="timeout"></param>
+		/// <param name="ticks"></param>
+		public TimeoutEventArgs (Timeout timeout, long ticks)
+		{
+			this.Timeout = timeout;
+			this.Ticks = ticks;
+		}
 	}
-}
+}
+

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

@@ -126,7 +126,7 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// Event invoked when the <see cref="HotKey"/> is changed.
 		/// </summary>
-		public event Action<Key> HotKeyChanged;
+		public event EventHandler<KeyChangedEventArgs> HotKeyChanged;
 
 		/// <summary>
 		///   The text to be displayed. This text is never modified.
@@ -288,7 +288,7 @@ namespace Terminal.Gui {
 				if (hotKey != value) {
 					var oldKey = hotKey;
 					hotKey = value;
-					HotKeyChanged?.Invoke (oldKey);
+					HotKeyChanged?.Invoke (this, new KeyChangedEventArgs(oldKey,value));
 				}
 			}
 		}

+ 3 - 3
Terminal.Gui/Core/View.cs

@@ -169,7 +169,7 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// Event invoked when the <see cref="HotKey"/> is changed.
 		/// </summary>
-		public event Action<Key> HotKeyChanged;
+		public event EventHandler<KeyChangedEventArgs> HotKeyChanged;
 
 		Key hotKey = Key.Null;
 
@@ -825,9 +825,9 @@ namespace Terminal.Gui {
 			SetNeedsDisplay ();
 		}
 
-		void TextFormatter_HotKeyChanged (Key obj)
+		void TextFormatter_HotKeyChanged (object sender, KeyChangedEventArgs e)
 		{
-			HotKeyChanged?.Invoke (obj);
+			HotKeyChanged?.Invoke (this,e);
 		}
 
 		/// <summary>

+ 40 - 0
UnitTests/Views/ButtonTests.cs

@@ -584,5 +584,45 @@ namespace Terminal.Gui.ViewTests {
 
 			TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
 		}
+		[Fact, AutoInitShutdown]
+		public void Button_HotKeyChanged_EventFires ()
+		{
+			var btn = new Button ("Yar");
+
+			object sender = null;
+			KeyChangedEventArgs args = null;
+
+			btn.HotKeyChanged += (s, e) =>{
+				sender = s;
+				args = e;
+
+			};
+			
+			btn.HotKey = Key.r;
+			Assert.Same (btn, sender);
+			Assert.Equal (Key.Y, args.OldKey);
+			Assert.Equal (Key.r, args.NewKey);
+
+		}
+		[Fact, AutoInitShutdown]
+		public void Button_HotKeyChanged_EventFires_WithNone ()
+		{
+			var btn = new Button ();
+
+			object sender = null;
+			KeyChangedEventArgs args = null;
+
+			btn.HotKeyChanged += (s, e) => {
+				sender = s;
+				args = e;
+
+			};
+
+			btn.HotKey = Key.r;
+			Assert.Same (btn, sender);
+			Assert.Equal (Key.Null, args.OldKey);
+			Assert.Equal (Key.r, args.NewKey);
+
+		}
 	}
 }