From e347a255b7c3e6fa8c1930d2e0f6dc4d069124d1 Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 12 Mar 2023 02:54:46 +0000 Subject: [PATCH] Add HexViewEditEventArgs --- Terminal.Gui/Views/HexView.cs | 38 +++++++++++++++++++++++++++----- UICatalog/Scenarios/HexEditor.cs | 2 +- UICatalog/Scenarios/Text.cs | 2 +- UnitTests/Views/HexViewTests.cs | 2 +- 4 files changed, 35 insertions(+), 9 deletions(-) diff --git a/Terminal.Gui/Views/HexView.cs b/Terminal.Gui/Views/HexView.cs index 68f724544..910e9e105 100644 --- a/Terminal.Gui/Views/HexView.cs +++ b/Terminal.Gui/Views/HexView.cs @@ -103,7 +103,7 @@ namespace Terminal.Gui { /// /// Event to be invoked when an edit is made on the . /// - public event Action> Edited; + public event EventHandler Edited; /// /// 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 (position, edits [position])); + OnEdited (new HexViewEditEventArgs (position, edits [position])); } else { b = (byte)(b & 0xf0 | value); edits [position] = b; - OnEdited (new KeyValuePair (position, edits [position])); + OnEdited (new HexViewEditEventArgs (position, edits [position])); MoveRight (); } return true; @@ -473,10 +473,10 @@ namespace Terminal.Gui { /// /// Method used to invoke the event passing the . /// - /// The key value pair. - public virtual void OnEdited (KeyValuePair keyValuePair) + /// The key value pair. + public virtual void OnEdited (HexViewEditEventArgs e) { - Edited?.Invoke (keyValuePair); + Edited?.Invoke (this, e); } /// @@ -632,6 +632,32 @@ namespace Terminal.Gui { return base.OnEnter (view); } + /// + /// Defines the event arguments for event. + /// + public class HexViewEditEventArgs : EventArgs { + + /// + /// Creates a new instance of the class. + /// + /// + /// + public HexViewEditEventArgs (long position, byte newValue) + { + Position = position; + NewValue = newValue; + } + + /// + /// Gets the location of the edit. + /// + public long Position { get; } + + /// + /// Gets the new value for that . + /// + public byte NewValue { get; } + } /// /// Defines the event arguments for event. /// diff --git a/UICatalog/Scenarios/HexEditor.cs b/UICatalog/Scenarios/HexEditor.cs index 6784181ee..cce25fd92 100644 --- a/UICatalog/Scenarios/HexEditor.cs +++ b/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 obj) + private void _hexView_Edited (object sender, HexView.HexViewEditEventArgs e) { _saved = false; } diff --git a/UICatalog/Scenarios/Text.cs b/UICatalog/Scenarios/Text.cs index 202e81ee0..7ed983528 100644 --- a/UICatalog/Scenarios/Text.cs +++ b/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); diff --git a/UnitTests/Views/HexViewTests.cs b/UnitTests/Views/HexViewTests.cs index ffa3cfcd9..41f45f845 100644 --- a/UnitTests/Views/HexViewTests.cs +++ b/UnitTests/Views/HexViewTests.cs @@ -106,7 +106,7 @@ namespace Terminal.Gui.ViewTests { { var hv = new HexView (LoadStream (true)) { Width = 20, Height = 20 }; KeyValuePair keyValuePair = default; - hv.Edited += (e) => keyValuePair = e; + hv.Edited += (s,e) => keyValuePair = new KeyValuePair(e.Position,e.NewValue); Assert.True (hv.ProcessKey (new KeyEvent (Key.D4, new KeyModifiers ()))); Assert.True (hv.ProcessKey (new KeyEvent (Key.D6, new KeyModifiers ())));