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.

HotkeySettingsForm.cs 7.3 kB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using Shadowsocks.Controller;
  2. using Shadowsocks.Model;
  3. using Shadowsocks.Properties;
  4. using System;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using static Shadowsocks.Controller.HotkeyReg;
  9. namespace Shadowsocks.View
  10. {
  11. public partial class HotkeySettingsForm : Form
  12. {
  13. private readonly ShadowsocksController _controller;
  14. // this is a copy of hotkey configuration that we are working on
  15. private HotkeyConfig _modifiedHotkeyConfig;
  16. public HotkeySettingsForm(ShadowsocksController controller)
  17. {
  18. InitializeComponent();
  19. UpdateTexts();
  20. Icon = Icon.FromHandle(Resources.ssw128.GetHicon());
  21. _controller = controller;
  22. _controller.ConfigChanged += controller_ConfigChanged;
  23. LoadCurrentConfiguration();
  24. }
  25. private void UpdateTexts()
  26. {
  27. I18N.TranslateForm(this);
  28. }
  29. private void controller_ConfigChanged(object sender, EventArgs e)
  30. {
  31. LoadCurrentConfiguration();
  32. }
  33. private void LoadCurrentConfiguration()
  34. {
  35. _modifiedHotkeyConfig = _controller.GetConfigurationCopy().hotkey;
  36. SetConfigToUI(_modifiedHotkeyConfig);
  37. }
  38. private void SetConfigToUI(HotkeyConfig config)
  39. {
  40. SwitchSystemProxyTextBox.Text = config.SwitchSystemProxy;
  41. SwitchProxyModeTextBox.Text = config.SwitchSystemProxyMode;
  42. SwitchAllowLanTextBox.Text = config.SwitchAllowLan;
  43. ShowLogsTextBox.Text = config.ShowLogs;
  44. ServerMoveUpTextBox.Text = config.ServerMoveUp;
  45. ServerMoveDownTextBox.Text = config.ServerMoveDown;
  46. RegHotkeysAtStartupCheckBox.Checked = config.RegHotkeysAtStartup;
  47. }
  48. private void SaveConfig()
  49. {
  50. _controller.SaveHotkeyConfig(_modifiedHotkeyConfig);
  51. }
  52. private HotkeyConfig GetConfigFromUI()
  53. {
  54. return new HotkeyConfig
  55. {
  56. SwitchSystemProxy = SwitchSystemProxyTextBox.Text,
  57. SwitchSystemProxyMode = SwitchProxyModeTextBox.Text,
  58. SwitchAllowLan = SwitchAllowLanTextBox.Text,
  59. ShowLogs = ShowLogsTextBox.Text,
  60. ServerMoveUp = ServerMoveUpTextBox.Text,
  61. ServerMoveDown = ServerMoveDownTextBox.Text,
  62. RegHotkeysAtStartup = RegHotkeysAtStartupCheckBox.Checked
  63. };
  64. }
  65. /// <summary>
  66. /// Capture hotkey - Press key
  67. /// </summary>
  68. private void HotkeyDown(object sender, KeyEventArgs e)
  69. {
  70. StringBuilder sb = new StringBuilder();
  71. //Combination key only
  72. if (e.Modifiers != 0)
  73. {
  74. // XXX: Hotkey parsing depends on the sequence, more specifically, ModifierKeysConverter.
  75. // Windows key is reserved by operating system, we deny this key.
  76. if (e.Control)
  77. {
  78. sb.Append("Ctrl+");
  79. }
  80. if (e.Alt)
  81. {
  82. sb.Append("Alt+");
  83. }
  84. if (e.Shift)
  85. {
  86. sb.Append("Shift+");
  87. }
  88. Keys keyvalue = (Keys)e.KeyValue;
  89. if ((keyvalue >= Keys.PageUp && keyvalue <= Keys.Down) ||
  90. (keyvalue >= Keys.A && keyvalue <= Keys.Z) ||
  91. (keyvalue >= Keys.F1 && keyvalue <= Keys.F12))
  92. {
  93. sb.Append(e.KeyCode);
  94. }
  95. else if (keyvalue >= Keys.D0 && keyvalue <= Keys.D9)
  96. {
  97. sb.Append('D').Append((char)e.KeyValue);
  98. }
  99. else if (keyvalue >= Keys.NumPad0 && keyvalue <= Keys.NumPad9)
  100. {
  101. sb.Append("NumPad").Append((char)(e.KeyValue - 48));
  102. }
  103. }
  104. ((TextBox)sender).Text = sb.ToString();
  105. }
  106. /// <summary>
  107. /// Capture hotkey - Release key
  108. /// </summary>
  109. private void HotkeyUp(object sender, KeyEventArgs e)
  110. {
  111. var tb = (TextBox)sender;
  112. var content = tb.Text.TrimEnd();
  113. if (content.Length >= 1 && content[content.Length - 1] == '+')
  114. {
  115. tb.Text = "";
  116. }
  117. }
  118. private void CancelButton_Click(object sender, EventArgs e)
  119. {
  120. this.Close();
  121. }
  122. private void OKButton_Click(object sender, EventArgs e)
  123. {
  124. _modifiedHotkeyConfig = GetConfigFromUI();
  125. // try to register, notify to change settings if failed
  126. if (!RegisterAllHotkeys(_modifiedHotkeyConfig))
  127. {
  128. MessageBox.Show(I18N.GetString("Register hotkey failed"));
  129. }
  130. // All check passed, saving
  131. SaveConfig();
  132. this.Close();
  133. }
  134. private void RegisterAllButton_Click(object sender, EventArgs e)
  135. {
  136. _modifiedHotkeyConfig = GetConfigFromUI();
  137. RegisterAllHotkeys(_modifiedHotkeyConfig);
  138. }
  139. private bool RegisterAllHotkeys(HotkeyConfig hotkeyConfig)
  140. {
  141. return
  142. RegHotkeyFromString(hotkeyConfig.SwitchSystemProxy, "SwitchSystemProxyCallback", result => HandleRegResult(hotkeyConfig.SwitchSystemProxy, SwitchSystemProxyLabel, result))
  143. && RegHotkeyFromString(hotkeyConfig.SwitchSystemProxyMode, "SwitchSystemProxyModeCallback", result => HandleRegResult(hotkeyConfig.SwitchSystemProxyMode, SwitchProxyModeLabel, result))
  144. && RegHotkeyFromString(hotkeyConfig.SwitchAllowLan, "SwitchAllowLanCallback", result => HandleRegResult(hotkeyConfig.SwitchAllowLan, SwitchAllowLanLabel, result))
  145. && RegHotkeyFromString(hotkeyConfig.ShowLogs, "ShowLogsCallback", result => HandleRegResult(hotkeyConfig.ShowLogs, ShowLogsLabel, result))
  146. && RegHotkeyFromString(hotkeyConfig.ServerMoveUp, "ServerMoveUpCallback", result => HandleRegResult(hotkeyConfig.ServerMoveUp, ServerMoveUpLabel, result))
  147. && RegHotkeyFromString(hotkeyConfig.ServerMoveDown, "ServerMoveDownCallback", result => HandleRegResult(hotkeyConfig.ServerMoveDown, ServerMoveDownLabel, result));
  148. }
  149. private void HandleRegResult(string hotkeyStr, Label label, RegResult result)
  150. {
  151. switch (result)
  152. {
  153. case RegResult.ParseError:
  154. MessageBox.Show(I18N.GetString("Cannot parse hotkey: {0}", hotkeyStr));
  155. break;
  156. case RegResult.UnregSuccess:
  157. label.ResetBackColor();
  158. break;
  159. case RegResult.RegSuccess:
  160. label.BackColor = Color.Green;
  161. break;
  162. case RegResult.RegFailure:
  163. label.BackColor = Color.Red;
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. }
  170. }