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.

HotkeysViewModel.cs 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using ReactiveUI;
  2. using ReactiveUI.Fody.Helpers;
  3. using Shadowsocks.Controller;
  4. using Shadowsocks.Model;
  5. using Shadowsocks.View;
  6. using System.Reactive;
  7. using System.Text;
  8. using System.Windows.Input;
  9. namespace Shadowsocks.WPF.ViewModels
  10. {
  11. public class HotkeysViewModel : ReactiveObject
  12. {
  13. public HotkeysViewModel()
  14. {
  15. _config = Program.MainController.GetCurrentConfiguration();
  16. _controller = Program.MainController;
  17. _menuViewController = Program.MenuController;
  18. HotkeySystemProxy = _config.hotkey.SwitchSystemProxy;
  19. HotkeyProxyMode = _config.hotkey.SwitchSystemProxyMode;
  20. HotkeyAllowLan = _config.hotkey.SwitchAllowLan;
  21. HotkeyOpenLogs = _config.hotkey.ShowLogs;
  22. HotkeySwitchPrev = _config.hotkey.ServerMoveUp;
  23. HotkeySwitchNext = _config.hotkey.ServerMoveDown;
  24. RegisterAtStartup = _config.hotkey.RegHotkeysAtStartup;
  25. HotkeySystemProxyStatus = "✔";
  26. HotkeyProxyModeStatus = "✔";
  27. HotkeyAllowLanStatus = "✔";
  28. HotkeyOpenLogsStatus = "✔";
  29. HotkeySwitchPrevStatus = "✔";
  30. HotkeySwitchNextStatus = "✔";
  31. RegisterAll = ReactiveCommand.Create(() => RegisterAllAndUpdateStatus());
  32. Save = ReactiveCommand.Create(() => RegisterAllAndUpdateStatus(true));
  33. Cancel = ReactiveCommand.Create(_menuViewController.CloseHotkeysWindow);
  34. }
  35. private readonly Configuration _config;
  36. private readonly ShadowsocksController _controller;
  37. private readonly MenuViewController _menuViewController;
  38. public ReactiveCommand<Unit, Unit> RegisterAll { get; }
  39. public ReactiveCommand<Unit, Unit> Save { get; }
  40. public ReactiveCommand<Unit, Unit> Cancel { get; }
  41. [Reactive]
  42. public string HotkeySystemProxy { get; set; }
  43. [Reactive]
  44. public string HotkeyProxyMode { get; set; }
  45. [Reactive]
  46. public string HotkeyAllowLan { get; set; }
  47. [Reactive]
  48. public string HotkeyOpenLogs { get; set; }
  49. [Reactive]
  50. public string HotkeySwitchPrev { get; set; }
  51. [Reactive]
  52. public string HotkeySwitchNext { get; set; }
  53. [Reactive]
  54. public bool RegisterAtStartup { get; set; }
  55. [Reactive]
  56. public string HotkeySystemProxyStatus { get; set; }
  57. [Reactive]
  58. public string HotkeyProxyModeStatus { get; set; }
  59. [Reactive]
  60. public string HotkeyAllowLanStatus { get; set; }
  61. [Reactive]
  62. public string HotkeyOpenLogsStatus { get; set; }
  63. [Reactive]
  64. public string HotkeySwitchPrevStatus { get; set; }
  65. [Reactive]
  66. public string HotkeySwitchNextStatus { get; set; }
  67. public void RecordKeyDown(int hotkeyIndex, KeyEventArgs keyEventArgs)
  68. {
  69. var recordedKeyStringBuilder = new StringBuilder();
  70. // record modifiers
  71. if ((Keyboard.Modifiers & ModifierKeys.Control) > 0)
  72. recordedKeyStringBuilder.Append("Ctrl+");
  73. if ((Keyboard.Modifiers & ModifierKeys.Alt) > 0)
  74. recordedKeyStringBuilder.Append("Alt+");
  75. if ((Keyboard.Modifiers & ModifierKeys.Shift) > 0)
  76. recordedKeyStringBuilder.Append("Shift+");
  77. // record other keys when at least one modifier is pressed
  78. if (recordedKeyStringBuilder.Length > 0 && (keyEventArgs.Key < Key.LeftShift || keyEventArgs.Key > Key.RightAlt))
  79. recordedKeyStringBuilder.Append(keyEventArgs.Key);
  80. switch (hotkeyIndex)
  81. {
  82. case 0:
  83. HotkeySystemProxy = recordedKeyStringBuilder.ToString();
  84. break;
  85. case 1:
  86. HotkeyProxyMode = recordedKeyStringBuilder.ToString();
  87. break;
  88. case 2:
  89. HotkeyAllowLan = recordedKeyStringBuilder.ToString();
  90. break;
  91. case 3:
  92. HotkeyOpenLogs = recordedKeyStringBuilder.ToString();
  93. break;
  94. case 4:
  95. HotkeySwitchPrev = recordedKeyStringBuilder.ToString();
  96. break;
  97. case 5:
  98. HotkeySwitchNext = recordedKeyStringBuilder.ToString();
  99. break;
  100. }
  101. }
  102. public void FinishOnKeyUp(int hotkeyIndex, KeyEventArgs keyEventArgs)
  103. {
  104. switch (hotkeyIndex)
  105. {
  106. case 0:
  107. if (HotkeySystemProxy.EndsWith("+"))
  108. HotkeySystemProxy = "";
  109. break;
  110. case 1:
  111. if (HotkeyProxyMode.EndsWith("+"))
  112. HotkeyProxyMode = "";
  113. break;
  114. case 2:
  115. if (HotkeyAllowLan.EndsWith("+"))
  116. HotkeyAllowLan = "";
  117. break;
  118. case 3:
  119. if (HotkeyOpenLogs.EndsWith("+"))
  120. HotkeyOpenLogs = "";
  121. break;
  122. case 4:
  123. if (HotkeySwitchPrev.EndsWith("+"))
  124. HotkeySwitchPrev = "";
  125. break;
  126. case 5:
  127. if (HotkeySwitchNext.EndsWith("+"))
  128. HotkeySwitchNext = "";
  129. break;
  130. }
  131. }
  132. private void RegisterAllAndUpdateStatus(bool save = false)
  133. {
  134. HotkeySystemProxyStatus = HotkeyReg.RegHotkeyFromString(HotkeySystemProxy, "SwitchSystemProxyCallback") ? "✔" : "❌";
  135. HotkeyProxyModeStatus = HotkeyReg.RegHotkeyFromString(HotkeyProxyMode, "SwitchSystemProxyModeCallback") ? "✔" : "❌";
  136. HotkeyAllowLanStatus = HotkeyReg.RegHotkeyFromString(HotkeyAllowLan, "SwitchAllowLanCallback") ? "✔" : "❌";
  137. HotkeyOpenLogsStatus = HotkeyReg.RegHotkeyFromString(HotkeyOpenLogs, "ShowLogsCallback") ? "✔" : "❌";
  138. HotkeySwitchPrevStatus = HotkeyReg.RegHotkeyFromString(HotkeySwitchPrev, "ServerMoveUpCallback") ? "✔" : "❌";
  139. HotkeySwitchNextStatus = HotkeyReg.RegHotkeyFromString(HotkeySwitchNext, "ServerMoveDownCallback") ? "✔" : "❌";
  140. if (HotkeySystemProxyStatus == "✔" &&
  141. HotkeyProxyModeStatus == "✔" &&
  142. HotkeyAllowLanStatus == "✔" &&
  143. HotkeyOpenLogsStatus == "✔" &&
  144. HotkeySwitchPrevStatus == "✔" &&
  145. HotkeySwitchNextStatus == "✔" && save)
  146. {
  147. _controller.SaveHotkeyConfig(GetHotkeyConfig);
  148. _menuViewController.CloseHotkeysWindow();
  149. }
  150. }
  151. private HotkeyConfig GetHotkeyConfig => new HotkeyConfig()
  152. {
  153. SwitchSystemProxy = HotkeySystemProxy,
  154. SwitchSystemProxyMode = HotkeyProxyMode,
  155. SwitchAllowLan = HotkeyAllowLan,
  156. ShowLogs = HotkeyOpenLogs,
  157. ServerMoveUp = HotkeySwitchPrev,
  158. ServerMoveDown = HotkeySwitchNext,
  159. RegHotkeysAtStartup = RegisterAtStartup
  160. };
  161. }
  162. }