Browse Source

Minor updates on Hotkeys form

tags/4.1.0
celeron533 7 years ago
parent
commit
db1bbd8039
3 changed files with 50 additions and 67 deletions
  1. +2
    -26
      shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs
  2. +2
    -0
      shadowsocks-csharp/View/ConfigForm.cs
  3. +46
    -41
      shadowsocks-csharp/View/HotkeySettingsForm.cs

+ 2
- 26
shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs View File

@@ -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) if (key == null)
throw new ArgumentNullException(nameof(key)); 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) if (key == null)
throw new ArgumentNullException(nameof(key)); throw new ArgumentNullException(nameof(key));


+ 2
- 0
shadowsocks-csharp/View/ConfigForm.cs View File

@@ -158,11 +158,13 @@ namespace Shadowsocks.View
{ {
_modifiedConfiguration = controller.GetConfigurationCopy(); _modifiedConfiguration = controller.GetConfigurationCopy();
LoadServerNameListToUI(_modifiedConfiguration); LoadServerNameListToUI(_modifiedConfiguration);
_lastSelectedIndex = _modifiedConfiguration.index; _lastSelectedIndex = _modifiedConfiguration.index;
if (_lastSelectedIndex < 0 || _lastSelectedIndex >= ServersListBox.Items.Count) if (_lastSelectedIndex < 0 || _lastSelectedIndex >= ServersListBox.Items.Count)
{ {
_lastSelectedIndex = 0; _lastSelectedIndex = 0;
} }
ServersListBox.SelectedIndex = _lastSelectedIndex; ServersListBox.SelectedIndex = _lastSelectedIndex;
UpdateButtons(); UpdateButtons();
LoadSelectedServerDetails(); LoadSelectedServerDetails();


+ 46
- 41
shadowsocks-csharp/View/HotkeySettingsForm.cs View File

@@ -18,8 +18,8 @@ namespace Shadowsocks.View
{ {
private readonly ShadowsocksController _controller; 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<TextBox> _allTextBoxes; private readonly IEnumerable<TextBox> _allTextBoxes;
@@ -39,6 +39,21 @@ namespace Shadowsocks.View
if (!_allTextBoxes.Any()) throw new Exception("Cannot get all textboxes"); 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) private void controller_ConfigChanged(object sender, EventArgs e)
{ {
LoadCurrentConfiguration(); LoadCurrentConfiguration();
@@ -46,8 +61,8 @@ namespace Shadowsocks.View
private void LoadCurrentConfiguration() private void LoadCurrentConfiguration()
{ {
_modifiedConfig = _controller.GetConfigurationCopy().hotkey;
LoadConfiguration(_modifiedConfig);
_modifiedHotkeyConfig = _controller.GetConfigurationCopy().hotkey;
LoadConfiguration(_modifiedHotkeyConfig);
} }
private void LoadConfiguration(HotkeyConfig config) private void LoadConfiguration(HotkeyConfig config)
@@ -60,21 +75,6 @@ namespace Shadowsocks.View
ServerMoveDownTextBox.Text = config.ServerMoveDown; 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...");
}
/// <summary> /// <summary>
/// Capture hotkey - Press key /// Capture hotkey - Press key
/// </summary> /// </summary>
@@ -154,40 +154,45 @@ namespace Shadowsocks.View
private void CancelButton_Click(object sender, EventArgs e) private void CancelButton_Click(object sender, EventArgs e)
{ {
Close();
this.Close();
} }
private void OKButton_Click(object sender, EventArgs e) private void OKButton_Click(object sender, EventArgs e)
{ {
// try to register, notify to change settings if failed // 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 // All check passed, saving
SaveConfig(); SaveConfig();
Close();
this.Close();
} }
private void RegisterAllButton_Click(object sender, EventArgs e) 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) foreach (var tb in _allTextBoxes)
{ {
if (tb.Text.IsNullOrEmpty()) if (tb.Text.IsNullOrEmpty())
{ {
continue; continue;
} }
TryRegHotkey(tb);
if (!TryRegHotkey(tb))
{
isSuccess = false;
failureInfo.AppendLine(tb.Text);
}
} }
failureInfoStr = failureInfo.ToString();
return isSuccess;
} }
private bool TryRegHotkey(TextBox tb) private bool TryRegHotkey(TextBox tb)
@@ -218,7 +223,7 @@ namespace Shadowsocks.View
// or change to another one // or change to another one
// Transparent without color: first run or empty config // 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; lb.BackColor = regResult ? Color.Green : Color.Yellow;
return regResult; return regResult;
} }
@@ -229,19 +234,19 @@ namespace Shadowsocks.View
if (HotKeys.IsCallbackExists(cb, out prevHotKey)) if (HotKeys.IsCallbackExists(cb, out prevHotKey))
{ {
// unregister previous one // unregister previous one
HotKeys.UnRegist(prevHotKey);
HotKeys.Unregister(prevHotKey);
} }
} }
private void SaveConfig() 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);
} }


Loading…
Cancel
Save