Files
v2rayN/v2rayN/ServiceLib/ViewModels/GlobalHotkeySettingViewModel.cs
T
2dust 5292568150 Centralize shared configuration state
Introduce a synchronized ConfigState and make ConfigTextManager static. Update configuration access across desktop and WebAPI, and add an AddServer overload using the current configuration.
2026-11-14 20:18:36 +08:00

65 lines
1.4 KiB
C#

namespace ServiceLib.ViewModels;
public class GlobalHotkeySettingViewModel : MyReactiveObject, ICloseable
{
public event EventHandler? RequestClose;
private readonly List<KeyEventItem> _globalHotkeys;
public ReactiveCommand<RxVoid, RxVoid> SaveCmd { get; }
public GlobalHotkeySettingViewModel()
{
_config = ConfigTextManager.Config;
_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 ConfigTextManager.SaveConfig() == 0)
{
RequestClose?.Invoke(this, EventArgs.Empty);
}
else
{
NoticeManager.Instance.Enqueue(ResUI.OperationFailed);
}
}
}