You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

HotkeyCallbacks.cs 3.4 kB

9 years ago
9 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Reflection;
  3. namespace Shadowsocks.Controller.Hotkeys
  4. {
  5. public class HotkeyCallbacks
  6. {
  7. public static void InitInstance(ShadowsocksController controller)
  8. {
  9. if (Instance != null)
  10. {
  11. return;
  12. }
  13. Instance = new HotkeyCallbacks(controller);
  14. }
  15. /// <summary>
  16. /// Create hotkey callback handler delegate based on callback name
  17. /// </summary>
  18. /// <param name="methodname"></param>
  19. /// <returns></returns>
  20. public static Delegate GetCallback(string methodname)
  21. {
  22. if (methodname.IsNullOrEmpty()) throw new ArgumentException(nameof(methodname));
  23. MethodInfo dynMethod = typeof(HotkeyCallbacks).GetMethod(methodname,
  24. BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase);
  25. return dynMethod == null ? null : Delegate.CreateDelegate(typeof(HotKeys.HotKeyCallBackHandler), Instance, dynMethod);
  26. }
  27. #region Singleton
  28. private static HotkeyCallbacks Instance { get; set; }
  29. private readonly ShadowsocksController _controller;
  30. private HotkeyCallbacks(ShadowsocksController controller)
  31. {
  32. _controller = controller;
  33. }
  34. #endregion
  35. #region Callbacks
  36. private void SwitchSystemProxyCallback()
  37. {
  38. bool enabled = _controller.GetConfigurationCopy().enabled;
  39. _controller.ToggleEnable(!enabled);
  40. }
  41. private void SwitchSystemProxyModeCallback()
  42. {
  43. var config = _controller.GetConfigurationCopy();
  44. if (config.enabled == false) return;
  45. var currStatus = config.global;
  46. _controller.ToggleGlobal(!currStatus);
  47. }
  48. private void SwitchAllowLanCallback()
  49. {
  50. var status = _controller.GetConfigurationCopy().shareOverLan;
  51. _controller.ToggleShareOverLAN(!status);
  52. }
  53. private void ShowLogsCallback()
  54. {
  55. Program.MenuController.ShowLogForm_HotKey();
  56. }
  57. private void ServerMoveUpCallback()
  58. {
  59. int currIndex;
  60. int serverCount;
  61. GetCurrServerInfo(out currIndex, out serverCount);
  62. if (currIndex - 1 < 0)
  63. {
  64. // revert to last server
  65. currIndex = serverCount - 1;
  66. }
  67. else
  68. {
  69. currIndex -= 1;
  70. }
  71. _controller.SelectServerIndex(currIndex);
  72. }
  73. private void ServerMoveDownCallback()
  74. {
  75. int currIndex;
  76. int serverCount;
  77. GetCurrServerInfo(out currIndex, out serverCount);
  78. if (currIndex + 1 == serverCount)
  79. {
  80. // revert to first server
  81. currIndex = 0;
  82. }
  83. else
  84. {
  85. currIndex += 1;
  86. }
  87. _controller.SelectServerIndex(currIndex);
  88. }
  89. private void GetCurrServerInfo(out int currIndex, out int serverCount)
  90. {
  91. var currConfig = _controller.GetCurrentConfiguration();
  92. currIndex = currConfig.index;
  93. serverCount = currConfig.configs.Count;
  94. }
  95. #endregion
  96. }
  97. }