EventDispatcher and IEventListener.
This commit is contained in:
42
VAR.Toolbox/Code/EventDispatcher.cs
Normal file
42
VAR.Toolbox/Code/EventDispatcher.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
13
VAR.Toolbox/Code/IEventListener.cs
Normal file
13
VAR.Toolbox/Code/IEventListener.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -67,6 +67,7 @@ namespace VAR.Toolbox.UI
|
||||
Mouse.SetPosition(_mouseX, _mouseY);
|
||||
|
||||
Close();
|
||||
EventDispatcher.EmitEvent(PnlCover.PostCoverEventName, null);
|
||||
}
|
||||
|
||||
private void FrmCover_KeyPress(object sender, KeyPressEventArgs e)
|
||||
@@ -77,6 +78,7 @@ namespace VAR.Toolbox.UI
|
||||
Mouse.SetPosition(_mouseX, _mouseY);
|
||||
|
||||
Close();
|
||||
EventDispatcher.EmitEvent(PnlCover.PostCoverEventName, null);
|
||||
}
|
||||
|
||||
private void Timer_Tick(object sender, EventArgs e)
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using VAR.Toolbox.Code;
|
||||
using VAR.Toolbox.Code.Windows;
|
||||
|
||||
namespace VAR.Toolbox.UI
|
||||
{
|
||||
public partial class PnlCover : UserControl, IToolPanel
|
||||
{
|
||||
public const string PreCoverEventName = "PreCover";
|
||||
public const string PostCoverEventName = "PostCover";
|
||||
|
||||
public PnlCover()
|
||||
{
|
||||
InitializeComponent();
|
||||
@@ -39,6 +43,9 @@ namespace VAR.Toolbox.UI
|
||||
private void CoverScreen()
|
||||
{
|
||||
if (DesignMode) { return; }
|
||||
|
||||
EventDispatcher.EmitEvent(PreCoverEventName, null);
|
||||
|
||||
if (_frmCover != null)
|
||||
{
|
||||
_frmCover.Show();
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using VAR.Toolbox.Code;
|
||||
using VAR.Toolbox.Code.Windows;
|
||||
|
||||
namespace VAR.Toolbox.UI
|
||||
{
|
||||
public partial class PnlSuspension : UserControl, IToolPanel
|
||||
{
|
||||
public const string PreSuspendEventName = "PreSuspend";
|
||||
|
||||
private readonly Random _rnd = new Random();
|
||||
|
||||
public PnlSuspension()
|
||||
@@ -118,6 +121,7 @@ namespace VAR.Toolbox.UI
|
||||
|
||||
private void SuspendSystem()
|
||||
{
|
||||
EventDispatcher.EmitEvent(PreSuspendEventName, null);
|
||||
Win32.SetSuspendState(false, true, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,9 @@
|
||||
<Compile Include="..\VAR.Json\VAR.Json\ParserContext.cs">
|
||||
<Link>Code\Json\ParserContext.cs</Link>
|
||||
</Compile>
|
||||
<Compile Include="Code\EventDispatcher.cs" />
|
||||
<Compile Include="Code\HexUtils.cs" />
|
||||
<Compile Include="Code\IEventListener.cs" />
|
||||
<Compile Include="Code\ProxyCmdExecutors\ProxyCmdExecutorWMIC.cs" />
|
||||
<Compile Include="Code\ReflectionUtils.cs" />
|
||||
<Compile Include="Code\TextCoders\TextCoderBase64Utf8.cs" />
|
||||
|
||||
Reference in New Issue
Block a user