Update desktop global hotkey setting

This commit is contained in:
2dust 2025-02-25 16:14:50 +08:00
parent 6079e76be5
commit add92cfa7c
2 changed files with 125 additions and 117 deletions

View file

@ -6,17 +6,17 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
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:DataType="vms:SubEditViewModel" x:DataType="vms:GlobalHotkeySettingViewModel"
ShowInTaskbar="False" ShowInTaskbar="False"
WindowStartupLocation="CenterScreen" WindowStartupLocation="CenterScreen"
mc:Ignorable="d"> mc:Ignorable="d">
<DockPanel Margin="{StaticResource Margin8}"> <DockPanel Margin="{StaticResource Margin8}">
<StackPanel <StackPanel
HorizontalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
HorizontalAlignment="Center"
DockPanel.Dock="Bottom" DockPanel.Dock="Bottom"
Orientation="Horizontal"> Orientation="Horizontal">
<Button <Button
@ -55,77 +55,77 @@
<TextBlock <TextBlock
Grid.Row="1" Grid.Row="1"
Grid.Column="0" Grid.Column="0"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbDisplayGUI}" /> Text="{x:Static resx:ResUI.TbDisplayGUI}" />
<TextBox <TextBox
x:Name="txtGlobalHotkey0" x:Name="txtGlobalHotkey0"
Grid.Row="1" Grid.Row="1"
Grid.Column="1" Grid.Column="1"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
IsReadOnly="True" /> IsReadOnly="True" />
<TextBlock <TextBlock
Grid.Row="2" Grid.Row="2"
Grid.Column="0" Grid.Column="0"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbClearSystemProxy}" /> Text="{x:Static resx:ResUI.TbClearSystemProxy}" />
<TextBox <TextBox
x:Name="txtGlobalHotkey1" x:Name="txtGlobalHotkey1"
Grid.Row="2" Grid.Row="2"
Grid.Column="1" Grid.Column="1"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
IsReadOnly="True" /> IsReadOnly="True" />
<TextBlock <TextBlock
Grid.Row="3" Grid.Row="3"
Grid.Column="0" Grid.Column="0"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbSetSystemProxy}" /> Text="{x:Static resx:ResUI.TbSetSystemProxy}" />
<TextBox <TextBox
x:Name="txtGlobalHotkey2" x:Name="txtGlobalHotkey2"
Grid.Row="3" Grid.Row="3"
Grid.Column="1" Grid.Column="1"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
IsReadOnly="True" /> IsReadOnly="True" />
<TextBlock <TextBlock
Grid.Row="4" Grid.Row="4"
Grid.Column="0" Grid.Column="0"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbNotChangeSystemProxy}" /> Text="{x:Static resx:ResUI.TbNotChangeSystemProxy}" />
<TextBox <TextBox
x:Name="txtGlobalHotkey3" x:Name="txtGlobalHotkey3"
Grid.Row="4" Grid.Row="4"
Grid.Column="1" Grid.Column="1"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
IsReadOnly="True" /> IsReadOnly="True" />
<TextBlock <TextBlock
Grid.Row="5" Grid.Row="5"
Grid.Column="0" Grid.Column="0"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbSystemProxyPac}" /> Text="{x:Static resx:ResUI.TbSystemProxyPac}" />
<TextBox <TextBox
x:Name="txtGlobalHotkey4" x:Name="txtGlobalHotkey4"
Grid.Row="5" Grid.Row="5"
Grid.Column="1" Grid.Column="1"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
IsReadOnly="True" /> IsReadOnly="True" />
</Grid> </Grid>
<TextBlock <TextBlock
Grid.Row="1" Grid.Row="1"
VerticalAlignment="Center"
Margin="{StaticResource Margin4}" Margin="{StaticResource Margin4}"
VerticalAlignment="Center"
Text="{x:Static resx:ResUI.TbGlobalHotkeySettingTip}" /> Text="{x:Static resx:ResUI.TbGlobalHotkeySettingTip}" />
</Grid> </Grid>
</ScrollViewer> </ScrollViewer>

View file

@ -1,129 +1,137 @@
using Avalonia.Controls; using System.Reactive.Disposables;
using System.Text;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.ReactiveUI;
using ReactiveUI;
namespace v2rayN.Desktop.Views namespace v2rayN.Desktop.Views
{ {
public partial class GlobalHotkeySettingWindow : Window public partial class GlobalHotkeySettingWindow : ReactiveWindow<GlobalHotkeySettingViewModel>
{ {
private static Config _config = default!; private readonly List<object> _textBoxKeyEventItem = new();
private Dictionary<object, KeyEventItem> _TextBoxKeyEventItem = default!;
public GlobalHotkeySettingWindow() public GlobalHotkeySettingWindow()
{ {
InitializeComponent(); InitializeComponent();
btnCancel.Click += (s, e) => this.Close(); ViewModel = new GlobalHotkeySettingViewModel(UpdateViewHandler);
_config = AppHandler.Instance.Config;
//_config.globalHotkeys ??= new List<KeyEventItem>();
//txtGlobalHotkey0.KeyDown += TxtGlobalHotkey_PreviewKeyDown; btnReset.Click += btnReset_Click;
//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;
//InitData(); btnCancel.Click += (s, e) => this.Close();
this.WhenActivated(disposables =>
{
this.BindCommand(ViewModel, vm => vm.SaveCmd, v => v.btnSave).DisposeWith(disposables);
});
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.Close(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 TxtGlobalHotkey_PreviewKeyDown(object? sender, KeyEventArgs e) private void Init()
//{ {
// e.Handled = true; _textBoxKeyEventItem.Add(txtGlobalHotkey0);
// var _ModifierKeys = new Key[] { Key.LeftCtrl, Key.RightCtrl, Key.LeftShift, _textBoxKeyEventItem.Add(txtGlobalHotkey1);
// Key.RightShift, Key.LeftAlt, Key.RightAlt, Key.LWin, Key.RWin}; _textBoxKeyEventItem.Add(txtGlobalHotkey2);
// _TextBoxKeyEventItem[sender].KeyCode = (int)(e.Key == Key.System ? (_ModifierKeys.Contains(e.SystemKey) ? Key.None : e.SystemKey) : (_ModifierKeys.Contains(e.Key) ? Key.None : e.Key)); _textBoxKeyEventItem.Add(txtGlobalHotkey3);
// _TextBoxKeyEventItem[sender].Alt = (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt; _textBoxKeyEventItem.Add(txtGlobalHotkey4);
// _TextBoxKeyEventItem[sender].Control = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control;
// _TextBoxKeyEventItem[sender].Shift = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
// (sender as TextBox)!.Text = KeyEventItemToString(_TextBoxKeyEventItem[sender]);
//}
//private KeyEventItem GetKeyEventItemByEGlobalHotkey(List<KeyEventItem> KEList, EGlobalHotkey eg) for (var index = 0; index < _textBoxKeyEventItem.Count; index++)
//{ {
// return JsonUtils.DeepCopy(KEList.Find((it) => it.eGlobalHotkey == eg) ?? new() var sender = _textBoxKeyEventItem[index];
// { if (sender is not TextBox txtBox)
// eGlobalHotkey = eg, {
// Control = false, continue;
// Alt = false, }
// Shift = false, txtBox.Tag = (EGlobalHotkey)index;
// KeyCode = null txtBox.KeyDown += TxtGlobalHotkey_PreviewKeyDown;
// }); }
//} }
//private string KeyEventItemToString(KeyEventItem item) private void TxtGlobalHotkey_PreviewKeyDown(object? sender, KeyEventArgs e)
//{ {
// var res = new StringBuilder(); e.Handled = true;
if (sender is not TextBox txtBox)
{
return;
}
// if (item.Control) res.Append($"{ModifierKeys.Control}+"); var item = ViewModel?.GetKeyEventItem((EGlobalHotkey)txtBox.Tag);
// if (item.Shift) res.Append($"{ModifierKeys.Shift}+"); var modifierKeys = new Key[] { Key.LeftCtrl, Key.RightCtrl, Key.LeftShift, Key.RightShift, Key.LeftAlt, Key.RightAlt, Key.LWin, Key.RWin };
// if (item.Alt) res.Append($"{ModifierKeys.Alt}+");
// if (item.KeyCode != null && (Key)item.KeyCode != Key.None)
// res.Append($"{(Key)item.KeyCode}");
// return res.ToString(); item.KeyCode = (int)(e.Key == Key.System ? modifierKeys.Contains(Key.System) ? Key.None : Key.System : modifierKeys.Contains(e.Key) ? Key.None : e.Key);
//} item.Alt = (e.KeyModifiers & KeyModifiers.Alt) == KeyModifiers.Alt;
item.Control = (e.KeyModifiers & KeyModifiers.Control) == KeyModifiers.Control;
item.Shift = (e.KeyModifiers & KeyModifiers.Shift) == KeyModifiers.Shift;
//private void BindingData() txtBox.Text = KeyEventItemToString(item);
//{ }
// 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 void btnSave_Click(object? sender, RoutedEventArgs e) private void BindingData()
//{ {
// _config.globalHotkeys = _TextBoxKeyEventItem.Values.ToList(); foreach (var sender in _textBoxKeyEventItem)
{
if (sender is not TextBox txtBox)
{
continue;
}
// if (ConfigHandler.SaveConfig(_config, false) == 0) var item = ViewModel?.GetKeyEventItem((EGlobalHotkey)txtBox.Tag);
// { txtBox.Text = KeyEventItemToString(item);
// HotkeyHandler.Instance.ReLoad(); }
// this.Close(); }
// }
// else
// {
// UI.Show(ResUI.OperationFailed);
// }
//}
//private void btnReset_Click(object? sender, RoutedEventArgs e) private void btnReset_Click(object sender, RoutedEventArgs e)
//{ {
// foreach (var k in _TextBoxKeyEventItem.Keys) ViewModel?.ResetKeyEventItem();
// { BindingData();
// _TextBoxKeyEventItem[k].Alt = false; }
// _TextBoxKeyEventItem[k].Control = false;
// _TextBoxKeyEventItem[k].Shift = false;
// _TextBoxKeyEventItem[k].KeyCode = (int)Key.None;
// }
// BindingData();
//}
//private void GlobalHotkeySettingWindow_KeyDown(object? sender, KeyEventArgs e) private string KeyEventItemToString(KeyEventItem? item)
//{ {
// if (e.Key == Key.Escape) if (item == null)
// { {
// this.Close(); return string.Empty;
// } }
//} var res = new StringBuilder();
if (item.Control)
{
res.Append($"{KeyModifiers.Control} +");
}
if (item.Shift)
{
res.Append($"{KeyModifiers.Shift} +");
}
if (item.Alt)
{
res.Append($"{KeyModifiers.Alt} +");
}
if (item.KeyCode != null && (Key)item.KeyCode != Key.None)
{
res.Append($"{(Key)item.KeyCode}");
}
return res.ToString();
}
} }
} }