diff --git a/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs b/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs
index bfb10166..2e071b73 100644
--- a/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs
+++ b/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs
@@ -46,15 +46,13 @@ namespace Shadowsocks.Controller.Hotkeys
public static bool IsCallbackExists( HotKeyCallBackHandler cb, out HotKey hotkey)
{
if (cb == null) throw new ArgumentNullException(nameof(cb));
- try
+ if (_keymap.Any(v => v.Value == cb))
{
- var key = _keymap.First(x => x.Value == cb).Key;
- hotkey = key;
+ hotkey = _keymap.First(v => v.Value == cb).Key;
return true;
}
- catch (InvalidOperationException)
+ else
{
- // not found
hotkey = null;
return false;
}
diff --git a/shadowsocks-csharp/View/HotkeySettingsForm.cs b/shadowsocks-csharp/View/HotkeySettingsForm.cs
index ee3def67..46e8c2ed 100644
--- a/shadowsocks-csharp/View/HotkeySettingsForm.cs
+++ b/shadowsocks-csharp/View/HotkeySettingsForm.cs
@@ -77,7 +77,6 @@ namespace Shadowsocks.View
private void SaveConfig()
{
- _modifiedHotkeyConfig = GetConfigFromUI();
_controller.SaveHotkeyConfig(_modifiedHotkeyConfig);
}
@@ -150,27 +149,6 @@ namespace Shadowsocks.View
}
}
- private void TextBox_TextChanged(object sender, EventArgs e)
- {
- var tb = (TextBox) sender;
-
- if (tb.Text == "")
- {
- // unreg
- UnregHotkey(tb);
- }
- }
-
- private void UnregHotkey(TextBox tb)
- {
- HotKeys.HotKeyCallBackHandler callBack;
- Label lb;
-
- PrepareForHotkey(tb, out callBack, out lb);
-
- UnregPrevHotkey(callBack);
- }
-
private void CancelButton_Click(object sender, EventArgs e)
{
this.Close();
@@ -178,8 +156,9 @@ namespace Shadowsocks.View
private void OKButton_Click(object sender, EventArgs e)
{
+ _modifiedHotkeyConfig = GetConfigFromUI();
// try to register, notify to change settings if failed
- if (!RegisterAllHotkeys())
+ if (!RegisterAllHotkeys(_modifiedHotkeyConfig))
{
MessageBox.Show(I18N.GetString("Register hotkey failed"));
}
@@ -191,57 +170,61 @@ namespace Shadowsocks.View
private void RegisterAllButton_Click(object sender, EventArgs e)
{
- RegisterAllHotkeys();
+ _modifiedHotkeyConfig = GetConfigFromUI();
+ RegisterAllHotkeys(_modifiedHotkeyConfig);
}
- private bool RegisterAllHotkeys()
+ private bool RegisterAllHotkeys(HotkeyConfig hotkeyConfig)
{
- bool isSuccess = true;
- foreach (var tb in _allTextBoxes)
- {
- if (tb.Text.IsNullOrEmpty())
- {
- continue;
- }
- if (!TryRegHotkey(tb))
- {
- isSuccess = false;
- }
- }
- return isSuccess;
+ return
+ TryRegHotkeyFromString(hotkeyConfig.SwitchSystemProxy, "SwitchSystemProxyCallback", SwitchSystemProxyLabel)
+ && TryRegHotkeyFromString(hotkeyConfig.SwitchSystemProxyMode, "SwitchProxyModeCallback", SwitchProxyModeLabel)
+ && TryRegHotkeyFromString(hotkeyConfig.SwitchAllowLan, "SwitchAllowLanCallback", SwitchAllowLanLabel)
+ && TryRegHotkeyFromString(hotkeyConfig.ShowLogs, "ShowLogsCallback", ShowLogsLabel)
+ && TryRegHotkeyFromString(hotkeyConfig.ServerMoveUp, "ServerMoveUpCallback", ServerMoveUpLabel)
+ && TryRegHotkeyFromString(hotkeyConfig.ServerMoveDown, "ServerMoveDownCallback", ServerMoveDownLabel);
}
- private bool TryRegHotkey(TextBox tb)
+ private bool TryRegHotkeyFromString(string hotkeyStr, string callbackName, Label indicator = null)
{
- var hotkey = HotKeys.Str2HotKey(tb.Text);
- if (hotkey == null)
+ var _callback = HotkeyCallbacks.GetCallback(callbackName);
+ if (_callback == null)
{
- MessageBox.Show(string.Format(I18N.GetString("Cannot parse hotkey: {0}"), tb.Text));
- tb.Clear();
- return false;
+ throw new Exception($"{callbackName} not found");
}
- HotKeys.HotKeyCallBackHandler callBack;
- Label lb;
-
- PrepareForHotkey(tb, out callBack, out lb);
-
- UnregPrevHotkey(callBack);
+ var callback = _callback as HotKeys.HotKeyCallBackHandler;
- // try to register keys
- // if already registered by other progs
- // notify to change
+ if (hotkeyStr.IsNullOrEmpty())
+ {
+ UnregPrevHotkey(callback);
+ return true;
+ }
+ else
+ {
+ var hotkey = HotKeys.Str2HotKey(hotkeyStr);
+ if (hotkey == null)
+ {
+ MessageBox.Show(string.Format(I18N.GetString("Cannot parse hotkey: {0}"), hotkeyStr));
+ return false;
+ }
+ else
+ {
+ bool regResult = (TryRegHotkey(hotkey, callback));
+ if (indicator != null)
+ {
+ indicator.BackColor = regResult ? Color.Green : Color.Yellow;
+ }
+ return regResult;
+ }
+ }
- // use the corresponding label color to indicate
- // reg result.
- // Green: not occupied by others and operation succeed
- // Yellow: already registered by other program and need action: disable by clear the content
- // or change to another one
- // Transparent without color: first run or empty config
+ }
- bool regResult = HotKeys.Register(hotkey, callBack);
- lb.BackColor = regResult ? Color.Green : Color.Yellow;
- return regResult;
+ private bool TryRegHotkey(GlobalHotKey.HotKey hotkey, HotKeys.HotKeyCallBackHandler callback)
+ {
+ UnregPrevHotkey(callback);
+ return HotKeys.Register(hotkey, callback);
}
private static void UnregPrevHotkey(HotKeys.HotKeyCallBackHandler cb)
@@ -254,62 +237,5 @@ namespace Shadowsocks.View
}
}
- #region Prepare hotkey
-
- ///
- /// Find correct callback and corresponding label by textBox
- ///
- ///
- ///
- ///
- private void PrepareForHotkey(TextBox tb, out HotKeys.HotKeyCallBackHandler cb, out Label lb)
- {
- /*
- * XXX: The labelName, TextBoxName and callbackName
- * must follow this rule to make use of reflection
- *
- *
- */
- if (tb == null)
- throw new ArgumentNullException(nameof(tb));
-
- var pos = tb.Name.LastIndexOf("TextBox", StringComparison.OrdinalIgnoreCase);
- var rawName = tb.Name.Substring(0, pos);
- var labelName = rawName + "Label";
- var callbackName = rawName + "Callback";
-
- var callback = HotkeyCallbacks.GetCallback(callbackName);
- if (callback == null)
- {
- throw new Exception($"{callbackName} not found");
- }
- cb = callback as HotKeys.HotKeyCallBackHandler;
-
- var label = GetFieldViaName(GetType(), labelName, this);
- if (label == null)
- {
- throw new Exception($"{labelName} not found");
- }
- lb = label as Label;
- }
-
- ///
- ///
- ///
- /// from which type
- /// field name
- /// pass null if static field
- ///
- private static object GetFieldViaName(Type type, string name, object obj)
- {
- if (type == null) throw new ArgumentNullException(nameof(type));
- if (name.IsNullOrEmpty()) throw new ArgumentException(nameof(name));
- // In general, TextBoxes and Labels are private
- FieldInfo fi = type.GetField(name,
- BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Static);
- return fi == null ? null : fi.GetValue(obj);
- }
-
- #endregion
}
}
\ No newline at end of file
diff --git a/shadowsocks-csharp/View/HotkeySettingsForm.designer.cs b/shadowsocks-csharp/View/HotkeySettingsForm.designer.cs
index da745ad4..c8ee4e4a 100644
--- a/shadowsocks-csharp/View/HotkeySettingsForm.designer.cs
+++ b/shadowsocks-csharp/View/HotkeySettingsForm.designer.cs
@@ -212,7 +212,6 @@
this.SwitchSystemProxyTextBox.ReadOnly = true;
this.SwitchSystemProxyTextBox.Size = new System.Drawing.Size(276, 25);
this.SwitchSystemProxyTextBox.TabIndex = 7;
- this.SwitchSystemProxyTextBox.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.SwitchSystemProxyTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.HotkeyDown);
this.SwitchSystemProxyTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.HotkeyUp);
//
@@ -225,7 +224,6 @@
this.SwitchProxyModeTextBox.ReadOnly = true;
this.SwitchProxyModeTextBox.Size = new System.Drawing.Size(276, 25);
this.SwitchProxyModeTextBox.TabIndex = 8;
- this.SwitchProxyModeTextBox.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.SwitchProxyModeTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.HotkeyDown);
this.SwitchProxyModeTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.HotkeyUp);
//
@@ -238,7 +236,6 @@
this.SwitchAllowLanTextBox.ReadOnly = true;
this.SwitchAllowLanTextBox.Size = new System.Drawing.Size(276, 25);
this.SwitchAllowLanTextBox.TabIndex = 10;
- this.SwitchAllowLanTextBox.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.SwitchAllowLanTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.HotkeyDown);
this.SwitchAllowLanTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.HotkeyUp);
//
@@ -251,7 +248,6 @@
this.ShowLogsTextBox.ReadOnly = true;
this.ShowLogsTextBox.Size = new System.Drawing.Size(276, 25);
this.ShowLogsTextBox.TabIndex = 11;
- this.ShowLogsTextBox.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.ShowLogsTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.HotkeyDown);
this.ShowLogsTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.HotkeyUp);
//
@@ -264,7 +260,6 @@
this.ServerMoveUpTextBox.ReadOnly = true;
this.ServerMoveUpTextBox.Size = new System.Drawing.Size(276, 25);
this.ServerMoveUpTextBox.TabIndex = 12;
- this.ServerMoveUpTextBox.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.ServerMoveUpTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.HotkeyDown);
this.ServerMoveUpTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.HotkeyUp);
//
@@ -277,7 +272,6 @@
this.ServerMoveDownTextBox.ReadOnly = true;
this.ServerMoveDownTextBox.Size = new System.Drawing.Size(276, 25);
this.ServerMoveDownTextBox.TabIndex = 13;
- this.ServerMoveDownTextBox.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.ServerMoveDownTextBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.HotkeyDown);
this.ServerMoveDownTextBox.KeyUp += new System.Windows.Forms.KeyEventHandler(this.HotkeyUp);
//