EventDispatcher and IEventListener.

This commit is contained in:
2019-11-18 18:23:26 +01:00
parent 856396a031
commit c38aa902fe
6 changed files with 70 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
namespace VAR.Toolbox.Code
{
public static class EventDispatcher
{
private static List<IEventListener> _eventListeners = null;
private static IEnumerable<IEventListener> GetEventListeners()
{
if (_eventListeners != null)
{
return _eventListeners;
}
Type iEventListener = typeof(IEventListener);
IEnumerable<Type> eventListeners = ReflectionUtils.GetTypesOfInterface(iEventListener);
_eventListeners = new List<IEventListener>();
foreach (Type eventListener in eventListeners)
{
IEventListener eventListenerInstance = Activator.CreateInstance(eventListener) as IEventListener;
if (eventListenerInstance != null)
{
_eventListeners.Add(eventListenerInstance);
}
}
return _eventListeners;
}
public static void EmitEvent(string eventName, object eventData)
{
IEnumerable<IEventListener> eventListeners = GetEventListeners();
foreach (IEventListener eventListener in eventListeners)
{
eventListener.ProcessEvent(eventName, eventData);
}
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VAR.Toolbox.Code
{
public interface IEventListener
{
void ProcessEvent(string eventName, object eventData);
}
}

View File

@@ -67,6 +67,7 @@ namespace VAR.Toolbox.UI
Mouse.SetPosition(_mouseX, _mouseY); Mouse.SetPosition(_mouseX, _mouseY);
Close(); Close();
EventDispatcher.EmitEvent(PnlCover.PostCoverEventName, null);
} }
private void FrmCover_KeyPress(object sender, KeyPressEventArgs e) private void FrmCover_KeyPress(object sender, KeyPressEventArgs e)
@@ -77,6 +78,7 @@ namespace VAR.Toolbox.UI
Mouse.SetPosition(_mouseX, _mouseY); Mouse.SetPosition(_mouseX, _mouseY);
Close(); Close();
EventDispatcher.EmitEvent(PnlCover.PostCoverEventName, null);
} }
private void Timer_Tick(object sender, EventArgs e) private void Timer_Tick(object sender, EventArgs e)

View File

@@ -1,11 +1,15 @@
using System; using System;
using System.Windows.Forms; using System.Windows.Forms;
using VAR.Toolbox.Code;
using VAR.Toolbox.Code.Windows; using VAR.Toolbox.Code.Windows;
namespace VAR.Toolbox.UI namespace VAR.Toolbox.UI
{ {
public partial class PnlCover : UserControl, IToolPanel public partial class PnlCover : UserControl, IToolPanel
{ {
public const string PreCoverEventName = "PreCover";
public const string PostCoverEventName = "PostCover";
public PnlCover() public PnlCover()
{ {
InitializeComponent(); InitializeComponent();
@@ -39,6 +43,9 @@ namespace VAR.Toolbox.UI
private void CoverScreen() private void CoverScreen()
{ {
if (DesignMode) { return; } if (DesignMode) { return; }
EventDispatcher.EmitEvent(PreCoverEventName, null);
if (_frmCover != null) if (_frmCover != null)
{ {
_frmCover.Show(); _frmCover.Show();

View File

@@ -1,11 +1,14 @@
using System; using System;
using System.Windows.Forms; using System.Windows.Forms;
using VAR.Toolbox.Code;
using VAR.Toolbox.Code.Windows; using VAR.Toolbox.Code.Windows;
namespace VAR.Toolbox.UI namespace VAR.Toolbox.UI
{ {
public partial class PnlSuspension : UserControl, IToolPanel public partial class PnlSuspension : UserControl, IToolPanel
{ {
public const string PreSuspendEventName = "PreSuspend";
private readonly Random _rnd = new Random(); private readonly Random _rnd = new Random();
public PnlSuspension() public PnlSuspension()
@@ -118,6 +121,7 @@ namespace VAR.Toolbox.UI
private void SuspendSystem() private void SuspendSystem()
{ {
EventDispatcher.EmitEvent(PreSuspendEventName, null);
Win32.SetSuspendState(false, true, false); Win32.SetSuspendState(false, true, false);
} }
} }

View File

@@ -73,7 +73,9 @@
<Compile Include="..\VAR.Json\VAR.Json\ParserContext.cs"> <Compile Include="..\VAR.Json\VAR.Json\ParserContext.cs">
<Link>Code\Json\ParserContext.cs</Link> <Link>Code\Json\ParserContext.cs</Link>
</Compile> </Compile>
<Compile Include="Code\EventDispatcher.cs" />
<Compile Include="Code\HexUtils.cs" /> <Compile Include="Code\HexUtils.cs" />
<Compile Include="Code\IEventListener.cs" />
<Compile Include="Code\ProxyCmdExecutors\ProxyCmdExecutorWMIC.cs" /> <Compile Include="Code\ProxyCmdExecutors\ProxyCmdExecutorWMIC.cs" />
<Compile Include="Code\ReflectionUtils.cs" /> <Compile Include="Code\ReflectionUtils.cs" />
<Compile Include="Code\TextCoders\TextCoderBase64Utf8.cs" /> <Compile Include="Code\TextCoders\TextCoderBase64Utf8.cs" />