From ed4f8f5b78dc14e1029d40759ca4ed4a7f4877c6 Mon Sep 17 00:00:00 2001 From: celeron533 Date: Mon, 11 Jun 2018 21:55:46 +0800 Subject: [PATCH] Minor updates on Hotkeys form - Naming - Cleanup - Move logic to controller --- .../Controller/System/Hotkeys/Hotkeys.cs | 27 ++++++++- shadowsocks-csharp/View/HotkeySettingsForm.cs | 55 ++++++------------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs b/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs index 2e071b73..45069321 100644 --- a/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs +++ b/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs @@ -36,7 +36,28 @@ namespace Shadowsocks.Controller.Hotkeys if (_keymap.TryGetValue(hotkey, out callback)) callback(); } - + + public static bool RegHotkey(HotKey hotkey, HotKeyCallBackHandler callback) + { + UnregExistingHotkey(callback); + return Register(hotkey, callback); + } + + public static bool UnregExistingHotkey(HotKeys.HotKeyCallBackHandler cb) + { + HotKey existingHotKey; + if (IsCallbackExists(cb, out existingHotKey)) + { + // unregister existing one + Unregister(existingHotKey); + return true; + } + else + { + return false; + } + } + public static bool IsHotkeyExists( HotKey hotKey ) { if (hotKey == null) throw new ArgumentNullException(nameof(hotKey)); @@ -115,7 +136,7 @@ namespace Shadowsocks.Controller.Hotkeys #endregion - public static bool Register(HotKey key, HotKeyCallBackHandler callBack) + private static bool Register(HotKey key, HotKeyCallBackHandler callBack) { if (key == null) throw new ArgumentNullException(nameof(key)); @@ -141,7 +162,7 @@ namespace Shadowsocks.Controller.Hotkeys } } - public static void Unregister(HotKey key) + private static void Unregister(HotKey key) { if (key == null) throw new ArgumentNullException(nameof(key)); diff --git a/shadowsocks-csharp/View/HotkeySettingsForm.cs b/shadowsocks-csharp/View/HotkeySettingsForm.cs index 46e8c2ed..34dc4594 100644 --- a/shadowsocks-csharp/View/HotkeySettingsForm.cs +++ b/shadowsocks-csharp/View/HotkeySettingsForm.cs @@ -21,8 +21,6 @@ namespace Shadowsocks.View // this is a copy of hotkey configuration that we are working on private HotkeyConfig _modifiedHotkeyConfig; - private readonly IEnumerable _allTextBoxes; - public HotkeySettingsForm(ShadowsocksController controller) { InitializeComponent(); @@ -33,10 +31,6 @@ namespace Shadowsocks.View _controller.ConfigChanged += controller_ConfigChanged; LoadCurrentConfiguration(); - - // get all textboxes belong to this form - _allTextBoxes = tableLayoutPanel1.GetChildControls(); - if (!_allTextBoxes.Any()) throw new Exception("Cannot get all textboxes"); } private void UpdateTexts() @@ -117,7 +111,7 @@ namespace Shadowsocks.View sb.Append("Shift+"); } - Keys keyvalue = (Keys) e.KeyValue; + Keys keyvalue = (Keys)e.KeyValue; if ((keyvalue >= Keys.PageUp && keyvalue <= Keys.Down) || (keyvalue >= Keys.A && keyvalue <= Keys.Z) || (keyvalue >= Keys.F1 && keyvalue <= Keys.F12)) @@ -126,14 +120,14 @@ namespace Shadowsocks.View } else if (keyvalue >= Keys.D0 && keyvalue <= Keys.D9) { - sb.Append('D').Append((char) e.KeyValue); + sb.Append('D').Append((char)e.KeyValue); } else if (keyvalue >= Keys.NumPad0 && keyvalue <= Keys.NumPad9) { - sb.Append("NumPad").Append((char) (e.KeyValue - 48)); + sb.Append("NumPad").Append((char)(e.KeyValue - 48)); } } - ((TextBox) sender).Text = sb.ToString(); + ((TextBox)sender).Text = sb.ToString(); } /// @@ -141,7 +135,7 @@ namespace Shadowsocks.View /// private void HotkeyUp(object sender, KeyEventArgs e) { - var tb = (TextBox) sender; + var tb = (TextBox)sender; var content = tb.Text.TrimEnd(); if (content.Length >= 1 && content[content.Length - 1] == '+') { @@ -177,15 +171,15 @@ namespace Shadowsocks.View private bool RegisterAllHotkeys(HotkeyConfig hotkeyConfig) { 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); + RegHotkeyFromString(hotkeyConfig.SwitchSystemProxy, "SwitchSystemProxyCallback", SwitchSystemProxyLabel) + && RegHotkeyFromString(hotkeyConfig.SwitchSystemProxyMode, "SwitchProxyModeCallback", SwitchProxyModeLabel) + && RegHotkeyFromString(hotkeyConfig.SwitchAllowLan, "SwitchAllowLanCallback", SwitchAllowLanLabel) + && RegHotkeyFromString(hotkeyConfig.ShowLogs, "ShowLogsCallback", ShowLogsLabel) + && RegHotkeyFromString(hotkeyConfig.ServerMoveUp, "ServerMoveUpCallback", ServerMoveUpLabel) + && RegHotkeyFromString(hotkeyConfig.ServerMoveDown, "ServerMoveDownCallback", ServerMoveDownLabel); } - private bool TryRegHotkeyFromString(string hotkeyStr, string callbackName, Label indicator = null) + private bool RegHotkeyFromString(string hotkeyStr, string callbackName, Label indicator = null) { var _callback = HotkeyCallbacks.GetCallback(callbackName); if (_callback == null) @@ -197,7 +191,11 @@ namespace Shadowsocks.View if (hotkeyStr.IsNullOrEmpty()) { - UnregPrevHotkey(callback); + HotKeys.UnregExistingHotkey(callback); + if (indicator != null) + { + indicator.ResetBackColor(); + } return true; } else @@ -210,7 +208,7 @@ namespace Shadowsocks.View } else { - bool regResult = (TryRegHotkey(hotkey, callback)); + bool regResult = (HotKeys.RegHotkey(hotkey, callback)); if (indicator != null) { indicator.BackColor = regResult ? Color.Green : Color.Yellow; @@ -220,22 +218,5 @@ namespace Shadowsocks.View } } - - private bool TryRegHotkey(GlobalHotKey.HotKey hotkey, HotKeys.HotKeyCallBackHandler callback) - { - UnregPrevHotkey(callback); - return HotKeys.Register(hotkey, callback); - } - - private static void UnregPrevHotkey(HotKeys.HotKeyCallBackHandler cb) - { - GlobalHotKey.HotKey prevHotKey; - if (HotKeys.IsCallbackExists(cb, out prevHotKey)) - { - // unregister previous one - HotKeys.Unregister(prevHotKey); - } - } - } } \ No newline at end of file