From db1bbd803977059d6b86808a6b32c55666c52103 Mon Sep 17 00:00:00 2001 From: celeron533 Date: Tue, 5 Jun 2018 22:44:03 +0800 Subject: [PATCH] Minor updates on Hotkeys form --- .../Controller/System/Hotkeys/Hotkeys.cs | 28 +----- shadowsocks-csharp/View/ConfigForm.cs | 2 + shadowsocks-csharp/View/HotkeySettingsForm.cs | 87 ++++++++++--------- 3 files changed, 50 insertions(+), 67 deletions(-) diff --git a/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs b/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs index 4714f2ea..00393c46 100644 --- a/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs +++ b/shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs @@ -112,7 +112,7 @@ namespace Shadowsocks.Controller.Hotkeys } } - public static bool Regist( HotKey key, HotKeyCallBackHandler callBack ) + public static bool Register(HotKey key, HotKeyCallBackHandler callBack) { if (key == null) throw new ArgumentNullException(nameof(key)); @@ -138,31 +138,7 @@ namespace Shadowsocks.Controller.Hotkeys } } - public static bool Regist(Key key, ModifierKeys modifiers, HotKeyCallBackHandler callBack) - { - if (!Enum.IsDefined(typeof(Key), key)) - throw new InvalidEnumArgumentException(nameof(key), (int) key, typeof(Key)); - try - { - var hotkey = _hotKeyManager.Register(key, modifiers); - _keymap[hotkey] = callBack; - return true; - } - catch (ArgumentException) - { - // already called this method with the specific hotkey - // return success silently - return true; - } - catch (Win32Exception) - { - // already registered by other programs - // notify user to change key - return false; - } - } - - public static void UnRegist(HotKey key) + public static void Unregister(HotKey key) { if (key == null) throw new ArgumentNullException(nameof(key)); diff --git a/shadowsocks-csharp/View/ConfigForm.cs b/shadowsocks-csharp/View/ConfigForm.cs index c00560b1..e7c2635e 100755 --- a/shadowsocks-csharp/View/ConfigForm.cs +++ b/shadowsocks-csharp/View/ConfigForm.cs @@ -158,11 +158,13 @@ namespace Shadowsocks.View { _modifiedConfiguration = controller.GetConfigurationCopy(); LoadServerNameListToUI(_modifiedConfiguration); + _lastSelectedIndex = _modifiedConfiguration.index; if (_lastSelectedIndex < 0 || _lastSelectedIndex >= ServersListBox.Items.Count) { _lastSelectedIndex = 0; } + ServersListBox.SelectedIndex = _lastSelectedIndex; UpdateButtons(); LoadSelectedServerDetails(); diff --git a/shadowsocks-csharp/View/HotkeySettingsForm.cs b/shadowsocks-csharp/View/HotkeySettingsForm.cs index 85c90b49..1c75a664 100644 --- a/shadowsocks-csharp/View/HotkeySettingsForm.cs +++ b/shadowsocks-csharp/View/HotkeySettingsForm.cs @@ -18,8 +18,8 @@ namespace Shadowsocks.View { private readonly ShadowsocksController _controller; - // this is a copy of configuration that we are working on - private HotkeyConfig _modifiedConfig; + // this is a copy of hotkey configuration that we are working on + private HotkeyConfig _modifiedHotkeyConfig; private readonly IEnumerable _allTextBoxes; @@ -39,6 +39,21 @@ namespace Shadowsocks.View if (!_allTextBoxes.Any()) throw new Exception("Cannot get all textboxes"); } + private void UpdateTexts() + { + // I18N stuff + SwitchSystemProxyLabel.Text = I18N.GetString("Switch system proxy"); + SwitchProxyModeLabel.Text = I18N.GetString("Switch system proxy mode"); + SwitchAllowLanLabel.Text = I18N.GetString("Switch share over LAN"); + ShowLogsLabel.Text = I18N.GetString("Show Logs"); + ServerMoveUpLabel.Text = I18N.GetString("Switch to prev server"); + ServerMoveDownLabel.Text = I18N.GetString("Switch to next server"); + btnOK.Text = I18N.GetString("OK"); + btnCancel.Text = I18N.GetString("Cancel"); + btnRegisterAll.Text = I18N.GetString("Reg All"); + this.Text = I18N.GetString("Edit Hotkeys..."); + } + private void controller_ConfigChanged(object sender, EventArgs e) { LoadCurrentConfiguration(); @@ -46,8 +61,8 @@ namespace Shadowsocks.View private void LoadCurrentConfiguration() { - _modifiedConfig = _controller.GetConfigurationCopy().hotkey; - LoadConfiguration(_modifiedConfig); + _modifiedHotkeyConfig = _controller.GetConfigurationCopy().hotkey; + LoadConfiguration(_modifiedHotkeyConfig); } private void LoadConfiguration(HotkeyConfig config) @@ -60,21 +75,6 @@ namespace Shadowsocks.View ServerMoveDownTextBox.Text = config.ServerMoveDown; } - private void UpdateTexts() - { - // I18N stuff - SwitchSystemProxyLabel.Text = I18N.GetString("Switch system proxy"); - SwitchProxyModeLabel.Text = I18N.GetString("Switch system proxy mode"); - SwitchAllowLanLabel.Text = I18N.GetString("Switch share over LAN"); - ShowLogsLabel.Text = I18N.GetString("Show Logs"); - ServerMoveUpLabel.Text = I18N.GetString("Switch to prev server"); - ServerMoveDownLabel.Text = I18N.GetString("Switch to next server"); - btnOK.Text = I18N.GetString("OK"); - btnCancel.Text = I18N.GetString("Cancel"); - btnRegisterAll.Text = I18N.GetString("Reg All"); - Text = I18N.GetString("Edit Hotkeys..."); - } - /// /// Capture hotkey - Press key /// @@ -154,40 +154,45 @@ namespace Shadowsocks.View private void CancelButton_Click(object sender, EventArgs e) { - Close(); + this.Close(); } private void OKButton_Click(object sender, EventArgs e) { // try to register, notify to change settings if failed - foreach (var tb in _allTextBoxes) + if (!RegisterAllHotkeys(out _)) // declare out as an inline discard variable { - if (tb.Text.IsNullOrEmpty()) - { - continue; - } - if (!TryRegHotkey(tb)) - { - MessageBox.Show(I18N.GetString("Register hotkey failed")); - return; - } + MessageBox.Show(I18N.GetString("Register hotkey failed")); } // All check passed, saving SaveConfig(); - Close(); + this.Close(); } private void RegisterAllButton_Click(object sender, EventArgs e) { + RegisterAllHotkeys(out _); // declare out as an inline discard variable + } + + private bool RegisterAllHotkeys(out string failureInfoStr) + { + bool isSuccess = true; + StringBuilder failureInfo = new StringBuilder(); foreach (var tb in _allTextBoxes) { if (tb.Text.IsNullOrEmpty()) { continue; } - TryRegHotkey(tb); + if (!TryRegHotkey(tb)) + { + isSuccess = false; + failureInfo.AppendLine(tb.Text); + } } + failureInfoStr = failureInfo.ToString(); + return isSuccess; } private bool TryRegHotkey(TextBox tb) @@ -218,7 +223,7 @@ namespace Shadowsocks.View // or change to another one // Transparent without color: first run or empty config - bool regResult = HotKeys.Regist(hotkey, callBack); + bool regResult = HotKeys.Register(hotkey, callBack); lb.BackColor = regResult ? Color.Green : Color.Yellow; return regResult; } @@ -229,19 +234,19 @@ namespace Shadowsocks.View if (HotKeys.IsCallbackExists(cb, out prevHotKey)) { // unregister previous one - HotKeys.UnRegist(prevHotKey); + HotKeys.Unregister(prevHotKey); } } private void SaveConfig() { - _modifiedConfig.SwitchSystemProxy = SwitchSystemProxyTextBox.Text; - _modifiedConfig.SwitchSystemProxyMode = SwitchProxyModeTextBox.Text; - _modifiedConfig.SwitchAllowLan = SwitchAllowLanTextBox.Text; - _modifiedConfig.ShowLogs = ShowLogsTextBox.Text; - _modifiedConfig.ServerMoveUp = ServerMoveUpTextBox.Text; - _modifiedConfig.ServerMoveDown = ServerMoveDownTextBox.Text; - _controller.SaveHotkeyConfig(_modifiedConfig); + _modifiedHotkeyConfig.SwitchSystemProxy = SwitchSystemProxyTextBox.Text; + _modifiedHotkeyConfig.SwitchSystemProxyMode = SwitchProxyModeTextBox.Text; + _modifiedHotkeyConfig.SwitchAllowLan = SwitchAllowLanTextBox.Text; + _modifiedHotkeyConfig.ShowLogs = ShowLogsTextBox.Text; + _modifiedHotkeyConfig.ServerMoveUp = ServerMoveUpTextBox.Text; + _modifiedHotkeyConfig.ServerMoveDown = ServerMoveDownTextBox.Text; + _controller.SaveHotkeyConfig(_modifiedHotkeyConfig); }