Browse Source

Minor updates on Hotkeys form

- Naming
- Cleanup
- Move logic to controller
tags/4.1.0
celeron533 6 years ago
parent
commit
ed4f8f5b78
2 changed files with 42 additions and 40 deletions
  1. +24
    -3
      shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs
  2. +18
    -37
      shadowsocks-csharp/View/HotkeySettingsForm.cs

+ 24
- 3
shadowsocks-csharp/Controller/System/Hotkeys/Hotkeys.cs View File

@@ -36,7 +36,28 @@ namespace Shadowsocks.Controller.Hotkeys
if (_keymap.TryGetValue(hotkey, out callback)) if (_keymap.TryGetValue(hotkey, out callback))
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 ) public static bool IsHotkeyExists( HotKey hotKey )
{ {
if (hotKey == null) throw new ArgumentNullException(nameof(hotKey)); if (hotKey == null) throw new ArgumentNullException(nameof(hotKey));
@@ -115,7 +136,7 @@ namespace Shadowsocks.Controller.Hotkeys


#endregion #endregion


public static bool Register(HotKey key, HotKeyCallBackHandler callBack)
private static bool Register(HotKey key, HotKeyCallBackHandler callBack)
{ {
if (key == null) if (key == null)
throw new ArgumentNullException(nameof(key)); 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) if (key == null)
throw new ArgumentNullException(nameof(key)); throw new ArgumentNullException(nameof(key));


+ 18
- 37
shadowsocks-csharp/View/HotkeySettingsForm.cs View File

@@ -21,8 +21,6 @@ namespace Shadowsocks.View
// this is a copy of hotkey configuration that we are working on // this is a copy of hotkey configuration that we are working on
private HotkeyConfig _modifiedHotkeyConfig; private HotkeyConfig _modifiedHotkeyConfig;
private readonly IEnumerable<TextBox> _allTextBoxes;
public HotkeySettingsForm(ShadowsocksController controller) public HotkeySettingsForm(ShadowsocksController controller)
{ {
InitializeComponent(); InitializeComponent();
@@ -33,10 +31,6 @@ namespace Shadowsocks.View
_controller.ConfigChanged += controller_ConfigChanged; _controller.ConfigChanged += controller_ConfigChanged;
LoadCurrentConfiguration(); LoadCurrentConfiguration();
// get all textboxes belong to this form
_allTextBoxes = tableLayoutPanel1.GetChildControls<TextBox>();
if (!_allTextBoxes.Any()) throw new Exception("Cannot get all textboxes");
} }
private void UpdateTexts() private void UpdateTexts()
@@ -117,7 +111,7 @@ namespace Shadowsocks.View
sb.Append("Shift+"); sb.Append("Shift+");
} }
Keys keyvalue = (Keys) e.KeyValue;
Keys keyvalue = (Keys)e.KeyValue;
if ((keyvalue >= Keys.PageUp && keyvalue <= Keys.Down) || if ((keyvalue >= Keys.PageUp && keyvalue <= Keys.Down) ||
(keyvalue >= Keys.A && keyvalue <= Keys.Z) || (keyvalue >= Keys.A && keyvalue <= Keys.Z) ||
(keyvalue >= Keys.F1 && keyvalue <= Keys.F12)) (keyvalue >= Keys.F1 && keyvalue <= Keys.F12))
@@ -126,14 +120,14 @@ namespace Shadowsocks.View
} }
else if (keyvalue >= Keys.D0 && keyvalue <= Keys.D9) 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) 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();
} }
/// <summary> /// <summary>
@@ -141,7 +135,7 @@ namespace Shadowsocks.View
/// </summary> /// </summary>
private void HotkeyUp(object sender, KeyEventArgs e) private void HotkeyUp(object sender, KeyEventArgs e)
{ {
var tb = (TextBox) sender;
var tb = (TextBox)sender;
var content = tb.Text.TrimEnd(); var content = tb.Text.TrimEnd();
if (content.Length >= 1 && content[content.Length - 1] == '+') if (content.Length >= 1 && content[content.Length - 1] == '+')
{ {
@@ -177,15 +171,15 @@ namespace Shadowsocks.View
private bool RegisterAllHotkeys(HotkeyConfig hotkeyConfig) private bool RegisterAllHotkeys(HotkeyConfig hotkeyConfig)
{ {
return 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); var _callback = HotkeyCallbacks.GetCallback(callbackName);
if (_callback == null) if (_callback == null)
@@ -197,7 +191,11 @@ namespace Shadowsocks.View
if (hotkeyStr.IsNullOrEmpty()) if (hotkeyStr.IsNullOrEmpty())
{ {
UnregPrevHotkey(callback);
HotKeys.UnregExistingHotkey(callback);
if (indicator != null)
{
indicator.ResetBackColor();
}
return true; return true;
} }
else else
@@ -210,7 +208,7 @@ namespace Shadowsocks.View
} }
else else
{ {
bool regResult = (TryRegHotkey(hotkey, callback));
bool regResult = (HotKeys.RegHotkey(hotkey, callback));
if (indicator != null) if (indicator != null)
{ {
indicator.BackColor = regResult ? Color.Green : Color.Yellow; 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);
}
}
} }
} }

Loading…
Cancel
Save