diff --git a/Terminal.Gui/Core/EventArgs/HotKeyChangedEventArgs.cs b/Terminal.Gui/Core/EventArgs/HotKeyChangedEventArgs.cs new file mode 100644 index 000000000..83b825b2a --- /dev/null +++ b/Terminal.Gui/Core/EventArgs/HotKeyChangedEventArgs.cs @@ -0,0 +1,29 @@ +using System; + +namespace Terminal.Gui { + + /// + /// Event args for when a is changed from + /// one value to a new value (e.g. in ) + /// + public class KeyChangedEventArgs : EventArgs { + + /// + /// Gets the old that was set before the event. + /// Use to check for empty. + /// + public Key OldKey { get; } + + /// + /// Gets the new that is being used. + /// Use to check for empty. + /// + public Key NewKey { get; } + + public KeyChangedEventArgs (Key oldKey, Key newKey) + { + this.OldKey = oldKey; + this.NewKey = newKey; + } + } +} diff --git a/Terminal.Gui/Core/EventArgs/TimeoutEventArgs.cs b/Terminal.Gui/Core/EventArgs/TimeoutEventArgs.cs index cfb2b3ecd..6a8e04c3f 100644 --- a/Terminal.Gui/Core/EventArgs/TimeoutEventArgs.cs +++ b/Terminal.Gui/Core/EventArgs/TimeoutEventArgs.cs @@ -2,30 +2,33 @@ using Terminal.Gui; using static Terminal.Gui.MainLoop; -/// -/// for timeout events (e.g. ) -/// -public class TimeoutEventArgs : EventArgs { +namespace Terminal.Gui { /// - /// Gets the timeout callback handler + /// for timeout events (e.g. ) /// - public Timeout Timeout { get; } + public class TimeoutEventArgs : EventArgs { + /// + /// Gets the timeout callback handler + /// + public Timeout Timeout { get; } - /// - /// Gets the in UTC time when the - /// will next execute after. - /// - public long Ticks { get; } + /// + /// Gets the in UTC time when the + /// will next execute after. + /// + public long Ticks { get; } - /// - /// Creates a new instance of the class. - /// - /// - /// - public TimeoutEventArgs (Timeout timeout, long ticks) - { - this.Timeout = timeout; - this.Ticks = ticks; + /// + /// Creates a new instance of the class. + /// + /// + /// + public TimeoutEventArgs (Timeout timeout, long ticks) + { + this.Timeout = timeout; + this.Ticks = ticks; + } } -} \ No newline at end of file +} + diff --git a/Terminal.Gui/Core/TextFormatter.cs b/Terminal.Gui/Core/TextFormatter.cs index 5d71066e4..c601970aa 100644 --- a/Terminal.Gui/Core/TextFormatter.cs +++ b/Terminal.Gui/Core/TextFormatter.cs @@ -126,7 +126,7 @@ namespace Terminal.Gui { /// /// Event invoked when the is changed. /// - public event Action HotKeyChanged; + public event EventHandler HotKeyChanged; /// /// 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)); } } } diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 91755ff40..3a0c475ce 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -169,7 +169,7 @@ namespace Terminal.Gui { /// /// Event invoked when the is changed. /// - public event Action HotKeyChanged; + public event EventHandler 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); } /// diff --git a/UnitTests/Views/ButtonTests.cs b/UnitTests/Views/ButtonTests.cs index 01fd330f2..9585b4bba 100644 --- a/UnitTests/Views/ButtonTests.cs +++ b/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); + + } } }