@@ -1,11 +1,11 @@ | |||||
using System.Reflection; | |||||
using System; | |||||
using System.Reflection; | |||||
using Shadowsocks.View; | using Shadowsocks.View; | ||||
namespace Shadowsocks.Controller.Hotkeys | namespace Shadowsocks.Controller.Hotkeys | ||||
{ | { | ||||
public class HotkeyCallbacks | public class HotkeyCallbacks | ||||
{ | { | ||||
public static HotkeyCallbacks Instance { get; private set; } | |||||
public static void InitInstance(ShadowsocksController controller) | public static void InitInstance(ShadowsocksController controller) | ||||
{ | { | ||||
@@ -17,6 +17,23 @@ namespace Shadowsocks.Controller.Hotkeys | |||||
Instance = new HotkeyCallbacks(controller); | Instance = new HotkeyCallbacks(controller); | ||||
} | } | ||||
/// <summary> | |||||
/// Create hotkey callback handler delegate based on callback name | |||||
/// </summary> | |||||
/// <param name="methodname"></param> | |||||
/// <returns></returns> | |||||
public static Delegate GetCallback(string methodname) | |||||
{ | |||||
if (methodname.IsNullOrEmpty()) throw new ArgumentException(nameof(methodname)); | |||||
MethodInfo dynMethod = typeof(HotkeyCallbacks).GetMethod(methodname, | |||||
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase); | |||||
return dynMethod == null ? null : Delegate.CreateDelegate(typeof(HotKeys.HotKeyCallBackHandler), Instance, dynMethod); | |||||
} | |||||
#region Singleton | |||||
private static HotkeyCallbacks Instance { get; set; } | |||||
private readonly ShadowsocksController _controller; | private readonly ShadowsocksController _controller; | ||||
private HotkeyCallbacks(ShadowsocksController controller) | private HotkeyCallbacks(ShadowsocksController controller) | ||||
@@ -24,6 +41,8 @@ namespace Shadowsocks.Controller.Hotkeys | |||||
_controller = controller; | _controller = controller; | ||||
} | } | ||||
#endregion | |||||
#region Callbacks | #region Callbacks | ||||
private void SwitchSystemProxyCallback() | private void SwitchSystemProxyCallback() | ||||
@@ -82,7 +82,7 @@ namespace Shadowsocks | |||||
#endif | #endif | ||||
_controller = new ShadowsocksController(); | _controller = new ShadowsocksController(); | ||||
_viewController = new MenuViewController(_controller); | _viewController = new MenuViewController(_controller); | ||||
HotKeys.Init(); | |||||
HotKeys.Init(_controller); | |||||
_controller.Start(); | _controller.Start(); | ||||
Application.Run(); | Application.Run(); | ||||
} | } | ||||
@@ -270,7 +270,7 @@ namespace Shadowsocks.View | |||||
var labelName = rawName + "Label"; | var labelName = rawName + "Label"; | ||||
var callbackName = rawName + "Callback"; | var callbackName = rawName + "Callback"; | ||||
var callback = GetDelegateViaMethodName(callbackName); | |||||
var callback = HotkeyCallbacks.GetCallback(callbackName); | |||||
if (callback == null) | if (callback == null) | ||||
{ | { | ||||
throw new Exception($"{callbackName} not found"); | throw new Exception($"{callbackName} not found"); | ||||
@@ -302,20 +302,6 @@ namespace Shadowsocks.View | |||||
return fi == null ? null : fi.GetValue(obj); | return fi == null ? null : fi.GetValue(obj); | ||||
} | } | ||||
/// <summary> | |||||
/// Create hotkey callback handler delegate based on callback name | |||||
/// </summary> | |||||
/// <param name="type"></param> | |||||
/// <param name="methodname"></param> | |||||
/// <returns></returns> | |||||
private Delegate GetDelegateViaMethodName(string methodname) | |||||
{ | |||||
if (methodname.IsNullOrEmpty()) throw new ArgumentException(nameof(methodname)); | |||||
MethodInfo dynMethod = typeof(HotkeyCallbacks).GetMethod(methodname, | |||||
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase); | |||||
return dynMethod == null ? null : Delegate.CreateDelegate(typeof(HotKeys.HotKeyCallBackHandler), HotkeyCallbacks.Instance, dynMethod); | |||||
} | |||||
#endregion | #endregion | ||||
} | } | ||||
} | } |