From bdcbd558560ceac7cfd26d8a133813de89fb93b9 Mon Sep 17 00:00:00 2001 From: tznind Date: Sat, 11 Mar 2023 10:18:39 +0000 Subject: [PATCH] Switch to EventHandler for - Activate - Deactivate - ChildClosed - Closed - ChildLoaded - ChildUnloaded --- .../Core/EventArgs/ToplevelEventArgs.cs | 21 ++++++++++++++++ Terminal.Gui/Core/Toplevel.cs | 24 +++++++++---------- .../Scenarios/BackgroundWorkerCollection.cs | 10 ++++---- UICatalog/Scenarios/ContextMenus.cs | 2 +- UICatalog/Scenarios/Dialogs.cs | 2 +- UICatalog/Scenarios/Editor.cs | 2 +- UnitTests/Application/ApplicationTests.cs | 2 +- UnitTests/TopLevels/MdiTests.cs | 16 ++++++------- UnitTests/TopLevels/ToplevelTests.cs | 12 +++++----- UnitTests/TopLevels/WizardTests.cs | 6 ++--- 10 files changed, 59 insertions(+), 38 deletions(-) create mode 100644 Terminal.Gui/Core/EventArgs/ToplevelEventArgs.cs diff --git a/Terminal.Gui/Core/EventArgs/ToplevelEventArgs.cs b/Terminal.Gui/Core/EventArgs/ToplevelEventArgs.cs new file mode 100644 index 000000000..e1cc2198e --- /dev/null +++ b/Terminal.Gui/Core/EventArgs/ToplevelEventArgs.cs @@ -0,0 +1,21 @@ +using System; + +namespace Terminal.Gui { + public class ToplevelEventArgs : EventArgs{ + + public ToplevelEventArgs (Toplevel toplevel) + { + Toplevel = toplevel; + } + + /// + /// Gets the that the event is about. + /// + /// This is usually but may not always be the same as the sender + /// in . For example if the reported event + /// is about a different or the event is + /// raised by a separate class. + /// + public Toplevel Toplevel { get; } + } +} diff --git a/Terminal.Gui/Core/Toplevel.cs b/Terminal.Gui/Core/Toplevel.cs index 55efac8f6..f59991bef 100644 --- a/Terminal.Gui/Core/Toplevel.cs +++ b/Terminal.Gui/Core/Toplevel.cs @@ -68,18 +68,18 @@ namespace Terminal.Gui { /// /// Invoked when the Toplevel becomes the Toplevel. /// - public event Action Activate; + public event EventHandler Activate; /// /// Invoked when the Toplevel ceases to be the Toplevel. /// - public event Action Deactivate; + public event EventHandler Deactivate; /// /// Invoked when a child of the Toplevel is closed by /// . /// - public event Action ChildClosed; + public event EventHandler ChildClosed; /// /// Invoked when the last child of the Toplevel is closed from @@ -96,17 +96,17 @@ namespace Terminal.Gui { /// /// Invoked when the Toplevel's is closed by . /// - public event Action Closed; + public event EventHandler Closed; /// /// Invoked when a child Toplevel's has been loaded. /// - public event Action ChildLoaded; + public event EventHandler ChildLoaded; /// /// Invoked when a cjhild Toplevel's has been unloaded. /// - public event Action ChildUnloaded; + public event EventHandler ChildUnloaded; /// /// Invoked when the terminal has been resized. The new of the terminal is provided. @@ -120,17 +120,17 @@ namespace Terminal.Gui { internal virtual void OnChildUnloaded (Toplevel top) { - ChildUnloaded?.Invoke (top); + ChildUnloaded?.Invoke (this, new ToplevelEventArgs(top)); } internal virtual void OnChildLoaded (Toplevel top) { - ChildLoaded?.Invoke (top); + ChildLoaded?.Invoke (this, new ToplevelEventArgs (top)); } internal virtual void OnClosed (Toplevel top) { - Closed?.Invoke (top); + Closed?.Invoke (this, new ToplevelEventArgs (top)); } internal virtual bool OnClosing (ToplevelClosingEventArgs ev) @@ -149,17 +149,17 @@ namespace Terminal.Gui { if (IsMdiContainer) { SetChildNeedsDisplay (); } - ChildClosed?.Invoke (top); + ChildClosed?.Invoke (this, new ToplevelEventArgs (top)); } internal virtual void OnDeactivate (Toplevel activated) { - Deactivate?.Invoke (activated); + Deactivate?.Invoke (this, new ToplevelEventArgs (activated)); } internal virtual void OnActivate (Toplevel deactivated) { - Activate?.Invoke (deactivated); + Activate?.Invoke (this, new ToplevelEventArgs(deactivated)); } /// diff --git a/UICatalog/Scenarios/BackgroundWorkerCollection.cs b/UICatalog/Scenarios/BackgroundWorkerCollection.cs index de83d5759..201bb3b63 100644 --- a/UICatalog/Scenarios/BackgroundWorkerCollection.cs +++ b/UICatalog/Scenarios/BackgroundWorkerCollection.cs @@ -63,7 +63,7 @@ namespace UICatalog.Scenarios { }; } - private void MdiMain_Closed (Toplevel obj) + private void MdiMain_Closed (object sender, ToplevelEventArgs e) { workerApp.Dispose (); Dispose (); @@ -82,14 +82,14 @@ namespace UICatalog.Scenarios { } } - private void MdiMain_Deactivate (Toplevel top) + private void MdiMain_Deactivate (object sender, ToplevelEventArgs top) { - workerApp.WriteLog ($"{top.Data} deactivate."); + workerApp.WriteLog ($"{top.Toplevel.Data} deactivate."); } - private void MdiMain_Activate (Toplevel top) + private void MdiMain_Activate (object sender, ToplevelEventArgs top) { - workerApp.WriteLog ($"{top.Data} activate."); + workerApp.WriteLog ($"{top.Toplevel.Data} activate."); } private MenuBarItem View () diff --git a/UICatalog/Scenarios/ContextMenus.cs b/UICatalog/Scenarios/ContextMenus.cs index e45c3e50d..03acd5b26 100644 --- a/UICatalog/Scenarios/ContextMenus.cs +++ b/UICatalog/Scenarios/ContextMenus.cs @@ -81,7 +81,7 @@ namespace UICatalog.Scenarios { Win.WantMousePositionReports = true; - Application.Top.Closed += (_) => { + Application.Top.Closed += (s,e) => { Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US"); Application.RootMouseEvent -= Application_RootMouseEvent; }; diff --git a/UICatalog/Scenarios/Dialogs.cs b/UICatalog/Scenarios/Dialogs.cs index d53977ad3..50d0c3f5e 100644 --- a/UICatalog/Scenarios/Dialogs.cs +++ b/UICatalog/Scenarios/Dialogs.cs @@ -226,7 +226,7 @@ namespace UICatalog.Scenarios { } dialog.LayoutSubviews (); }; - dialog.Closed += (args) => { + dialog.Closed += (s,e) => { buttonPressedLabel.Text = $"{clicked}"; }; dialog.Add (addChar); diff --git a/UICatalog/Scenarios/Editor.cs b/UICatalog/Scenarios/Editor.cs index ec9774776..f451bd37b 100644 --- a/UICatalog/Scenarios/Editor.cs +++ b/UICatalog/Scenarios/Editor.cs @@ -196,7 +196,7 @@ namespace UICatalog.Scenarios { } }; - Application.Top.Closed += (_) => Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US"); + Application.Top.Closed += (s,e) => Thread.CurrentThread.CurrentUICulture = new CultureInfo ("en-US"); } private void DisposeWinDialog () diff --git a/UnitTests/Application/ApplicationTests.cs b/UnitTests/Application/ApplicationTests.cs index 3e6590fe0..222057f72 100644 --- a/UnitTests/Application/ApplicationTests.cs +++ b/UnitTests/Application/ApplicationTests.cs @@ -497,7 +497,7 @@ namespace Terminal.Gui.ApplicationTests { t2.RequestStop (); }; // Now this will close the MdiContainer when all MdiChildes was closed - t2.Closed += (_) => { + t2.Closed += (s,_) => { t1.RequestStop (); }; Application.Iteration += () => { diff --git a/UnitTests/TopLevels/MdiTests.cs b/UnitTests/TopLevels/MdiTests.cs index 59b94dc40..72af89035 100644 --- a/UnitTests/TopLevels/MdiTests.cs +++ b/UnitTests/TopLevels/MdiTests.cs @@ -91,7 +91,7 @@ namespace Terminal.Gui.TopLevelTests { Assert.True (Application.Current == d); }; - d.Closed += (e) => Application.RequestStop (top1); + d.Closed += (s,e) => Application.RequestStop (top1); Application.Iteration += () => { Assert.Null (Application.MdiChildes); @@ -155,7 +155,7 @@ else Assert.True (Application.Current == top1); }; // Now this will close the MdiContainer propagating through the MdiChildes. - d.Closed += (e) => { + d.Closed += (s,e) => { mdi.RequestStop (); }; @@ -215,7 +215,7 @@ else Assert.True (Application.Current == top1); }; // Now this will close the MdiContainer propagating through the MdiChildes. - d.Closed += (e) => Application.RequestStop (mdi); + d.Closed += (s,e) => Application.RequestStop (mdi); Application.Iteration += () => { if (iterations == 4) { @@ -273,7 +273,7 @@ else Assert.True (Application.Current == top1); }; // Now this will close the MdiContainer propagating through the MdiChildes. - d.Closed += (e) => Application.RequestStop (mdi); + d.Closed += (s, e) => Application.RequestStop (mdi); Application.Iteration += () => { if (iterations == 4) { @@ -361,7 +361,7 @@ else Assert.True (Application.Current == top1); }; // Now this will close the MdiContainer propagating through the MdiChildes. - d1.Closed += (e) => { + d1.Closed += (s, e) => { Assert.True (Application.Current == d1); Assert.False (Application.Current.Running); mdi.RequestStop (); @@ -434,7 +434,7 @@ else Assert.True (Application.Current == top1); }; // Now this will close the MdiContainer propagating through the MdiChildes. - d1.Closed += (e) => { + d1.Closed += (s, e) => { mdi.RequestStop (); }; @@ -489,7 +489,7 @@ else Assert.True (Application.Current == top1); c1.RequestStop (); }; // Now this will close the MdiContainer propagating through the MdiChildes. - c1.Closed += (e) => { + c1.Closed += (s, e) => { mdi.RequestStop (); }; Application.Iteration += () => { @@ -570,7 +570,7 @@ else Assert.True (Application.Current == top1); stage.RequestStop (); }; - stage.Closed += (_) => { + stage.Closed += (_, _) => { if (iterations == 11) allStageClosed = true; Assert.Equal (iterations, Application.MdiChildes.Count); if (running) { diff --git a/UnitTests/TopLevels/ToplevelTests.cs b/UnitTests/TopLevels/ToplevelTests.cs index ac08ba23c..7c092fda7 100644 --- a/UnitTests/TopLevels/ToplevelTests.cs +++ b/UnitTests/TopLevels/ToplevelTests.cs @@ -154,13 +154,13 @@ namespace Terminal.Gui.TopLevelTests { var eventInvoked = ""; - top.ChildUnloaded += (e) => eventInvoked = "ChildUnloaded"; + top.ChildUnloaded += (s,e) => eventInvoked = "ChildUnloaded"; top.OnChildUnloaded (top); Assert.Equal ("ChildUnloaded", eventInvoked); - top.ChildLoaded += (e) => eventInvoked = "ChildLoaded"; + top.ChildLoaded += (s,e) => eventInvoked = "ChildLoaded"; top.OnChildLoaded (top); Assert.Equal ("ChildLoaded", eventInvoked); - top.Closed += (e) => eventInvoked = "Closed"; + top.Closed += (s, e) => eventInvoked = "Closed"; top.OnClosed (top); Assert.Equal ("Closed", eventInvoked); top.Closing += (e) => eventInvoked = "Closing"; @@ -169,13 +169,13 @@ namespace Terminal.Gui.TopLevelTests { top.AllChildClosed += (s, e) => eventInvoked = "AllChildClosed"; top.OnAllChildClosed (); Assert.Equal ("AllChildClosed", eventInvoked); - top.ChildClosed += (e) => eventInvoked = "ChildClosed"; + top.ChildClosed += (s,e) => eventInvoked = "ChildClosed"; top.OnChildClosed (top); Assert.Equal ("ChildClosed", eventInvoked); - top.Deactivate += (e) => eventInvoked = "Deactivate"; + top.Deactivate += (s,e) => eventInvoked = "Deactivate"; top.OnDeactivate (top); Assert.Equal ("Deactivate", eventInvoked); - top.Activate += (e) => eventInvoked = "Activate"; + top.Activate += (s,e) => eventInvoked = "Activate"; top.OnActivate (top); Assert.Equal ("Activate", eventInvoked); top.Loaded += (s, e) => eventInvoked = "Loaded"; diff --git a/UnitTests/TopLevels/WizardTests.cs b/UnitTests/TopLevels/WizardTests.cs index b48d306d5..b10d862e8 100644 --- a/UnitTests/TopLevels/WizardTests.cs +++ b/UnitTests/TopLevels/WizardTests.cs @@ -523,7 +523,7 @@ namespace Terminal.Gui.TopLevelTests { }; var closedFired = false; - wizard.Closed += (args) => { + wizard.Closed += (s, e) => { closedFired = true; }; @@ -553,7 +553,7 @@ namespace Terminal.Gui.TopLevelTests { }; closedFired = false; - wizard.Closed += (args) => { + wizard.Closed += (s,e) => { closedFired = true; }; @@ -591,7 +591,7 @@ namespace Terminal.Gui.TopLevelTests { }; closedFired = false; - wizard.Closed += (args) => { + wizard.Closed += (s,e) => { closedFired = true; };