Add HexViewEditEventArgs

This commit is contained in:
tznind
2023-03-12 02:54:46 +00:00
parent 6f5ef32fb7
commit e347a255b7
4 changed files with 35 additions and 9 deletions

View File

@@ -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>

View File

@@ -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;
}

View File

@@ -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);

View File

@@ -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 ())));