fix: 尝试修复更新重启后热键失效

This commit is contained in:
uye
2024-02-01 10:42:25 +08:00
parent a24e0e4bdc
commit 3ebe8d2cc3
4 changed files with 22 additions and 10 deletions

View File

@@ -221,6 +221,7 @@ namespace MaaWpfGui.Main
// MessageBox.Show("O(∩_∩)O 拜拜");
ETagCache.Save();
Instances.SettingsViewModel.Sober();
Instances.MaaHotKeyManager.Release();
// 关闭程序时清理操作中心中的通知
// 使用 handyorg 的 ShowBalloonTip不需要清理

View File

@@ -20,5 +20,7 @@ namespace MaaWpfGui.Services.HotKeys
void UnRegister(MaaHotKeyAction action);
MaaHotKey GetOrNull(MaaHotKeyAction action);
void Release();
}
}

View File

@@ -22,8 +22,7 @@ namespace MaaWpfGui.Services.HotKeys
{
public class MaaHotKeyManager : IMaaHotKeyManager
{
private readonly Dictionary<MaaHotKeyAction, MaaHotKey> _actionHotKeyMapping =
new Dictionary<MaaHotKeyAction, MaaHotKey>();
private readonly Dictionary<MaaHotKeyAction, MaaHotKey> _actionHotKeyMapping = new();
private const string HotKeyConfigName = "HotKeys";
@@ -84,20 +83,28 @@ namespace MaaWpfGui.Services.HotKeys
}
}
public void Release()
{
foreach (var kvPair in _actionHotKeyMapping)
{
InternalUnRegister(kvPair.Key);
}
}
private void InternalUnRegister(MaaHotKeyAction action)
{
if (!_actionHotKeyMapping.ContainsKey(action) || _actionHotKeyMapping[action] == null)
if (!_actionHotKeyMapping.TryGetValue(action, out var value) || value == null)
{
return;
}
Instances.HotKeyManager.Unregister(_actionHotKeyMapping[action]);
Instances.HotKeyManager.Unregister(value);
_actionHotKeyMapping[action] = null;
}
public MaaHotKey GetOrNull(MaaHotKeyAction action)
{
return _actionHotKeyMapping.TryGetValue(action, out var value) ? value : null;
return _actionHotKeyMapping.GetValueOrDefault(action);
}
private void HotKeyManagerPressed(object sender, KeyPressedEventArgs e)
@@ -120,12 +127,10 @@ namespace MaaWpfGui.Services.HotKeys
var hotKeys = new Dictionary<MaaHotKeyAction, MaaHotKey>
{
{
MaaHotKeyAction.ShowGui,
new MaaHotKey(Key.M, ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt)
MaaHotKeyAction.ShowGui, new MaaHotKey(Key.M, ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt)
},
{
MaaHotKeyAction.LinkStart,
new MaaHotKey(Key.L, ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt)
MaaHotKeyAction.LinkStart, new MaaHotKey(Key.L, ModifierKeys.Control | ModifierKeys.Shift | ModifierKeys.Alt)
},
};

View File

@@ -28,7 +28,11 @@ namespace MaaWpfGui.ViewModels.UI
/// <inheritdoc/>
protected override void OnViewLoaded()
{
CheckAndUpdateNow();
if (CheckAndUpdateNow())
{
return;
}
InitViewModels();
InitProxy();
}