Browse Source

Add HexViewEditEventArgs

tznind 2 years ago
parent
commit
e347a255b7

+ 32 - 6
Terminal.Gui/Views/HexView.cs

@@ -103,7 +103,7 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// Event to be invoked when an edit is made on the <see cref="Stream"/>.
 		/// </summary>
-		public event Action<KeyValuePair<long, byte>> Edited;
+		public event EventHandler<HexViewEditEventArgs> Edited;
 
 		/// <summary>
 		/// Event to be invoked when the position and cursor position changes.
@@ -458,11 +458,11 @@ namespace Terminal.Gui {
 					firstNibble = false;
 					b = (byte)(b & 0xf | (value << bsize));
 					edits [position] = b;
-					OnEdited (new KeyValuePair<long, byte> (position, edits [position]));
+					OnEdited (new HexViewEditEventArgs (position, edits [position]));
 				} else {
 					b = (byte)(b & 0xf0 | value);
 					edits [position] = b;
-					OnEdited (new KeyValuePair<long, byte> (position, edits [position]));
+					OnEdited (new HexViewEditEventArgs (position, edits [position]));
 					MoveRight ();
 				}
 				return true;
@@ -473,10 +473,10 @@ namespace Terminal.Gui {
 		/// <summary>
 		/// Method used to invoke the <see cref="Edited"/> event passing the <see cref="KeyValuePair{TKey, TValue}"/>.
 		/// </summary>
-		/// <param name="keyValuePair">The key value pair.</param>
-		public virtual void OnEdited (KeyValuePair<long, byte> keyValuePair)
+		/// <param name="e">The key value pair.</param>
+		public virtual void OnEdited (HexViewEditEventArgs e)
 		{
-			Edited?.Invoke (keyValuePair);
+			Edited?.Invoke (this, e);
 		}
 
 		/// <summary>
@@ -632,6 +632,32 @@ namespace Terminal.Gui {
 			return base.OnEnter (view);
 		}
 
+		/// <summary>
+		/// Defines the event arguments for <see cref="Edited"/> event.
+		/// </summary>
+		public class HexViewEditEventArgs : EventArgs {
+
+			/// <summary>
+			/// Creates a new instance of the <see cref="HexViewEditEventArgs"/> class.
+			/// </summary>
+			/// <param name="position"></param>
+			/// <param name="newValue"></param>
+			public HexViewEditEventArgs (long position, byte newValue)
+			{
+				Position = position;
+				NewValue = newValue;
+			}
+
+			/// <summary>
+			/// Gets the location of the edit.
+			/// </summary>
+			public long Position { get; }
+
+			/// <summary>
+			/// Gets the new value for that <see cref="Position"/>.
+			/// </summary>
+			public byte NewValue { get; }
+		}
 		/// <summary>
 		/// Defines the event arguments for <see cref="PositionChanged"/> event.
 		/// </summary>

+ 1 - 1
UICatalog/Scenarios/HexEditor.cs

@@ -75,7 +75,7 @@ namespace UICatalog.Scenarios {
 			_hexView.AllowEdits = (bool)(miAllowEdits.Checked = !miAllowEdits.Checked);
 		}
 
-		private void _hexView_Edited (System.Collections.Generic.KeyValuePair<long, byte> obj)
+		private void _hexView_Edited (object sender, HexView.HexViewEditEventArgs e)
 		{
 			_saved = false;
 		}

+ 1 - 1
UICatalog/Scenarios/Text.cs

@@ -136,7 +136,7 @@ namespace UICatalog.Scenarios {
 			};
 			var array = ((MemoryStream)hexEditor.Source).ToArray ();
 			labelMirroringHexEditor.Text = Encoding.UTF8.GetString (array, 0, array.Length);
-			hexEditor.Edited += (kv) => {
+			hexEditor.Edited += (s,kv) => {
 				hexEditor.ApplyEdits ();
 				var array = ((MemoryStream)hexEditor.Source).ToArray ();
 				labelMirroringHexEditor.Text = Encoding.UTF8.GetString (array, 0, array.Length);

+ 1 - 1
UnitTests/Views/HexViewTests.cs

@@ -106,7 +106,7 @@ namespace Terminal.Gui.ViewTests {
 		{
 			var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 };
 			KeyValuePair<long, byte> keyValuePair = default;
-			hv.Edited += (e) => keyValuePair = e;
+			hv.Edited += (s,e) => keyValuePair = new KeyValuePair<long, byte>(e.Position,e.NewValue);
 
 			Assert.True (hv.ProcessKey (new KeyEvent (Key.D4, new KeyModifiers ())));
 			Assert.True (hv.ProcessKey (new KeyEvent (Key.D6, new KeyModifiers ())));