Improved global hotkey setting
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release macOS / build (Release) (push) Waiting to run
release Windows desktop (Avalonia UI) / build (Release) (push) Waiting to run
release Windows / build (Release) (push) Waiting to run

This commit is contained in:
2dust 2025-02-25 14:26:12 +08:00
parent 166c7cb2f5
commit 6079e76be5
5 changed files with 137 additions and 92 deletions

View file

@ -158,6 +158,7 @@ namespace ServiceLib.Handler
Length = "100-200", Length = "100-200",
Interval = "10-20" Interval = "10-20"
}; };
config.GlobalHotkeys ??= new();
if (config.SystemProxyItem.SystemProxyExceptions.IsNullOrEmpty()) if (config.SystemProxyItem.SystemProxyExceptions.IsNullOrEmpty())
{ {

View file

@ -0,0 +1,67 @@
using System.Reactive;
using System.Text;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
namespace ServiceLib.ViewModels
{
public class GlobalHotkeySettingViewModel : MyReactiveObject
{
private readonly List<KeyEventItem> _globalHotkeys;
public ReactiveCommand<Unit, Unit> SaveCmd { get; }
public GlobalHotkeySettingViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
{
_config = AppHandler.Instance.Config;
_updateView = updateView;
_globalHotkeys = JsonUtils.DeepCopy(_config.GlobalHotkeys);
SaveCmd = ReactiveCommand.CreateFromTask(async () =>
{
await SaveSettingAsync();
});
}
public KeyEventItem GetKeyEventItem(EGlobalHotkey eg)
{
var item = _globalHotkeys.FirstOrDefault((it) => it.EGlobalHotkey == eg);
if (item != null)
{
return item;
}
item = new()
{
EGlobalHotkey = eg,
Control = false,
Alt = false,
Shift = false,
KeyCode = null
};
_globalHotkeys.Add(item);
return item;
}
public void ResetKeyEventItem()
{
_globalHotkeys.Clear();
}
private async Task SaveSettingAsync()
{
_config.GlobalHotkeys = _globalHotkeys;
if (await ConfigHandler.SaveConfig(_config) == 0)
{
_updateView?.Invoke(EViewAction.CloseWindow, null);
}
else
{
NoticeHandler.Instance.Enqueue(ResUI.OperationFailed);
}
}
}
}

View file

@ -29,10 +29,6 @@ namespace v2rayN.Handler
private void Init() private void Init()
{ {
_hotkeyTriggerDic.Clear(); _hotkeyTriggerDic.Clear();
if (AppHandler.Instance.Config.GlobalHotkeys == null)
{
return;
}
foreach (var item in AppHandler.Instance.Config.GlobalHotkeys) foreach (var item in AppHandler.Instance.Config.GlobalHotkeys)
{ {
if (item.KeyCode != null && (Key)item.KeyCode != Key.None) if (item.KeyCode != null && (Key)item.KeyCode != Key.None)

View file

@ -8,10 +8,10 @@
xmlns:reactiveui="http://reactiveui.net" xmlns:reactiveui="http://reactiveui.net"
xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib" xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib"
xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib" xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib"
Title="{x:Static resx:ResUI.menuSetting}" Title="{x:Static resx:ResUI.menuGlobalHotkeySetting}"
Width="700" Width="700"
Height="500" Height="500"
x:TypeArguments="vms:SubEditViewModel" x:TypeArguments="vms:GlobalHotkeySettingViewModel"
ShowInTaskbar="False" ShowInTaskbar="False"
Style="{StaticResource WindowGlobal}" Style="{StaticResource WindowGlobal}"
WindowStartupLocation="CenterScreen" WindowStartupLocation="CenterScreen"
@ -91,7 +91,6 @@
VerticalAlignment="Center" VerticalAlignment="Center"
AcceptsReturn="True" AcceptsReturn="True"
IsReadOnly="True" IsReadOnly="True"
PreviewKeyDown="TxtGlobalHotkey_PreviewKeyDown"
Style="{StaticResource MyOutlinedTextBox}" /> Style="{StaticResource MyOutlinedTextBox}" />
<TextBlock <TextBlock
@ -109,7 +108,6 @@
VerticalAlignment="Center" VerticalAlignment="Center"
AcceptsReturn="True" AcceptsReturn="True"
IsReadOnly="True" IsReadOnly="True"
PreviewKeyDown="TxtGlobalHotkey_PreviewKeyDown"
Style="{StaticResource MyOutlinedTextBox}" /> Style="{StaticResource MyOutlinedTextBox}" />
<TextBlock <TextBlock
@ -127,7 +125,6 @@
VerticalAlignment="Center" VerticalAlignment="Center"
AcceptsReturn="True" AcceptsReturn="True"
IsReadOnly="True" IsReadOnly="True"
PreviewKeyDown="TxtGlobalHotkey_PreviewKeyDown"
Style="{StaticResource MyOutlinedTextBox}" /> Style="{StaticResource MyOutlinedTextBox}" />
<TextBlock <TextBlock
Grid.Row="4" Grid.Row="4"
@ -144,7 +141,6 @@
VerticalAlignment="Center" VerticalAlignment="Center"
AcceptsReturn="True" AcceptsReturn="True"
IsReadOnly="True" IsReadOnly="True"
PreviewKeyDown="TxtGlobalHotkey_PreviewKeyDown"
Style="{StaticResource MyOutlinedTextBox}" /> Style="{StaticResource MyOutlinedTextBox}" />
<TextBlock <TextBlock
Grid.Row="5" Grid.Row="5"
@ -161,7 +157,6 @@
VerticalAlignment="Center" VerticalAlignment="Center"
AcceptsReturn="True" AcceptsReturn="True"
IsReadOnly="True" IsReadOnly="True"
PreviewKeyDown="TxtGlobalHotkey_PreviewKeyDown"
Style="{StaticResource MyOutlinedTextBox}" /> Style="{StaticResource MyOutlinedTextBox}" />
</Grid> </Grid>

View file

@ -1,62 +1,80 @@
using System.Reactive.Disposables;
using System.Text; using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
using ReactiveUI;
using v2rayN.Handler; using v2rayN.Handler;
namespace v2rayN.Views namespace v2rayN.Views
{ {
public partial class GlobalHotkeySettingWindow public partial class GlobalHotkeySettingWindow
{ {
private static Config? _config; private readonly List<object> _textBoxKeyEventItem = new();
private Dictionary<object, KeyEventItem> _textBoxKeyEventItem = new();
public GlobalHotkeySettingWindow() public GlobalHotkeySettingWindow()
{ {
InitializeComponent(); InitializeComponent();
this.Owner = Application.Current.MainWindow; this.Owner = Application.Current.MainWindow;
_config = AppHandler.Instance.Config;
_config.GlobalHotkeys ??= new(); ViewModel = new GlobalHotkeySettingViewModel(UpdateViewHandler);
btnReset.Click += btnReset_Click; btnReset.Click += btnReset_Click;
btnSave.Click += btnSave_ClickAsync;
txtGlobalHotkey0.KeyDown += TxtGlobalHotkey_PreviewKeyDown;
txtGlobalHotkey1.KeyDown += TxtGlobalHotkey_PreviewKeyDown;
txtGlobalHotkey2.KeyDown += TxtGlobalHotkey_PreviewKeyDown;
txtGlobalHotkey3.KeyDown += TxtGlobalHotkey_PreviewKeyDown;
txtGlobalHotkey4.KeyDown += TxtGlobalHotkey_PreviewKeyDown;
HotkeyHandler.Instance.IsPause = true; HotkeyHandler.Instance.IsPause = true;
this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false; this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false;
WindowsUtils.SetDarkBorder(this, _config.UiItem.CurrentTheme);
InitData(); this.WhenActivated(disposables =>
{
this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables);
});
WindowsUtils.SetDarkBorder(this, AppHandler.Instance.Config.UiItem.CurrentTheme);
Init();
BindingData();
} }
private void InitData() private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
{ {
_textBoxKeyEventItem = new() switch (action)
{ {
{ txtGlobalHotkey0,GetKeyEventItemByEGlobalHotkey(_config.GlobalHotkeys,EGlobalHotkey.ShowForm) }, case EViewAction.CloseWindow:
{ txtGlobalHotkey1,GetKeyEventItemByEGlobalHotkey(_config.GlobalHotkeys,EGlobalHotkey.SystemProxyClear) }, this.DialogResult = true;
{ txtGlobalHotkey2,GetKeyEventItemByEGlobalHotkey(_config.GlobalHotkeys,EGlobalHotkey.SystemProxySet) }, break;
{ txtGlobalHotkey3,GetKeyEventItemByEGlobalHotkey(_config.GlobalHotkeys,EGlobalHotkey.SystemProxyUnchanged)}, }
{ txtGlobalHotkey4,GetKeyEventItemByEGlobalHotkey(_config.GlobalHotkeys,EGlobalHotkey.SystemProxyPac)} return await Task.FromResult(true);
}; }
BindingData(); private void Init()
{
_textBoxKeyEventItem.Add(txtGlobalHotkey0);
_textBoxKeyEventItem.Add(txtGlobalHotkey1);
_textBoxKeyEventItem.Add(txtGlobalHotkey2);
_textBoxKeyEventItem.Add(txtGlobalHotkey3);
_textBoxKeyEventItem.Add(txtGlobalHotkey4);
for (var index = 0; index < _textBoxKeyEventItem.Count; index++)
{
var sender = _textBoxKeyEventItem[index];
if (sender is not TextBox txtBox)
{
continue;
}
txtBox.Tag = (EGlobalHotkey)index;
txtBox.PreviewKeyDown += TxtGlobalHotkey_PreviewKeyDown;
}
} }
private void TxtGlobalHotkey_PreviewKeyDown(object? sender, KeyEventArgs e) private void TxtGlobalHotkey_PreviewKeyDown(object? sender, KeyEventArgs e)
{ {
e.Handled = true; e.Handled = true;
if (sender is null) if (sender is not TextBox txtBox)
{ {
return; return;
} }
var item = _textBoxKeyEventItem[sender]; var item = ViewModel?.GetKeyEventItem((EGlobalHotkey)txtBox.Tag);
var modifierKeys = new Key[] { Key.LeftCtrl, Key.RightCtrl, Key.LeftShift, Key.RightShift, Key.LeftAlt, Key.RightAlt, Key.LWin, Key.RWin }; var modifierKeys = new Key[] { Key.LeftCtrl, Key.RightCtrl, Key.LeftShift, Key.RightShift, Key.LeftAlt, Key.RightAlt, Key.LWin, Key.RWin };
item.KeyCode = (int)(e.Key == Key.System ? (modifierKeys.Contains(e.SystemKey) ? Key.None : e.SystemKey) : (modifierKeys.Contains(e.Key) ? Key.None : e.Key)); item.KeyCode = (int)(e.Key == Key.System ? (modifierKeys.Contains(e.SystemKey) ? Key.None : e.SystemKey) : (modifierKeys.Contains(e.Key) ? Key.None : e.Key));
@ -64,38 +82,50 @@ namespace v2rayN.Views
item.Control = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control; item.Control = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control;
item.Shift = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift; item.Shift = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
(sender as TextBox)!.Text = KeyEventItemToString(item); txtBox.Text = KeyEventItemToString(item);
} }
private KeyEventItem GetKeyEventItemByEGlobalHotkey(List<KeyEventItem> lstKey, EGlobalHotkey eg) private void BindingData()
{ {
return JsonUtils.DeepCopy(lstKey.Find((it) => it.EGlobalHotkey == eg) ?? new() foreach (var sender in _textBoxKeyEventItem)
{ {
EGlobalHotkey = eg, if (sender is not TextBox txtBox)
Control = false, {
Alt = false, continue;
Shift = false,
KeyCode = null
});
} }
private string KeyEventItemToString(KeyEventItem item) var item = ViewModel?.GetKeyEventItem((EGlobalHotkey)txtBox.Tag);
txtBox.Text = KeyEventItemToString(item);
}
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{ {
ViewModel?.ResetKeyEventItem();
BindingData();
}
private string KeyEventItemToString(KeyEventItem? item)
{
if (item == null)
{
return string.Empty;
}
var res = new StringBuilder(); var res = new StringBuilder();
if (item.Control) if (item.Control)
{ {
res.Append($"{ModifierKeys.Control}+"); res.Append($"{ModifierKeys.Control} +");
} }
if (item.Shift) if (item.Shift)
{ {
res.Append($"{ModifierKeys.Shift}+"); res.Append($"{ModifierKeys.Shift} +");
} }
if (item.Alt) if (item.Alt)
{ {
res.Append($"{ModifierKeys.Alt}+"); res.Append($"{ModifierKeys.Alt} +");
} }
if (item.KeyCode != null && (Key)item.KeyCode != Key.None) if (item.KeyCode != null && (Key)item.KeyCode != Key.None)
@ -105,49 +135,5 @@ namespace v2rayN.Views
return res.ToString(); return res.ToString();
} }
private void BindingData()
{
foreach (var item in _textBoxKeyEventItem)
{
if (item.Value.KeyCode != null && (Key)item.Value.KeyCode != Key.None)
{
(item.Key as TextBox)!.Text = KeyEventItemToString(item.Value);
}
else
{
(item.Key as TextBox)!.Text = string.Empty;
}
}
}
private async void btnSave_ClickAsync(object sender, RoutedEventArgs e)
{
_config.GlobalHotkeys = _textBoxKeyEventItem.Values.ToList();
if (await ConfigHandler.SaveConfig(_config) == 0)
{
HotkeyHandler.Instance.ReLoad();
this.DialogResult = true;
}
else
{
UI.Show(ResUI.OperationFailed);
}
}
private void btnReset_Click(object sender, RoutedEventArgs e)
{
foreach (var k in _textBoxKeyEventItem.Keys)
{
var item = _textBoxKeyEventItem[k];
item.Alt = false;
item.Control = false;
item.Shift = false;
item.KeyCode = (int)Key.None;
}
BindingData();
}
} }
} }