Skip to content

Commit

Permalink
新增 支持移除快捷键
Browse files Browse the repository at this point in the history
  • Loading branch information
MakesYT committed Nov 10, 2023
1 parent 1f219ed commit 92f3f5c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
24 changes: 23 additions & 1 deletion Core/ViewModel/Pages/SettingPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Windows;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Core.SDKs.HotKey;
using Core.SDKs.Services;
using Core.SDKs.Services.Config;
using log4net;
Expand Down Expand Up @@ -97,8 +98,29 @@ private void EditHotKey(string name)
}

var xx = (Window)hwndSource.RootVisual;
var hotKeyModel = ConfigManger.Config.hotKeys.FirstOrDefault(e =>
{
if ($"{e.MainName}_{e.Name}".Equals(name))
{
return true;
}
((IHotKeyEditor)ServiceManager.Services.GetService(typeof(IHotKeyEditor))!).EditByName(name?.ToString(), xx);
return false;
});
if (hotKeyModel is null)
{
var strings = name.Split("_", 2);
var hotKeyModel2 = new HotKeyModel()
{ MainName = strings[0], Name = strings[1], IsUsable = true };
ConfigManger.Config.hotKeys.Add(hotKeyModel2);
ConfigManger.Save();
((IHotKeyEditor)ServiceManager.Services.GetService(typeof(IHotKeyEditor))!).EditByName(name, xx);
}
else
{
((IHotKeyEditor)ServiceManager.Services.GetService(typeof(IHotKeyEditor))!).EditByHotKeyModel(hotKeyModel,
xx);
}
}

[RelayCommand]
Expand Down
29 changes: 28 additions & 1 deletion uToolkitopia/Controls/HotKeyShow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
using System.Linq;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Core.SDKs.HotKey;
using Core.SDKs.Services;
using Core.SDKs.Services.Config;
using Kitopia.View;

#endregion

Expand Down Expand Up @@ -56,15 +60,20 @@ public enum KeyTypeE
typeof(string), typeof(HotKeyShow),
new PropertyMetadata("空格"));

public static readonly DependencyProperty RemoveHotKeyProperty = DependencyProperty.Register(nameof(RemoveHotKey),
typeof(ICommand), typeof(HotKeyShow), new FrameworkPropertyMetadata(null));

public HotKeyShow()
{
WeakReferenceMessenger.Default.Register<string, string>(this, "hotkey", (_, s) =>
{
if (s == HotKeyModel.SignName)
HotKeyModel ??= ConfigManger.Config.hotKeys.FirstOrDefault(e => e.SignName == s);
if (HotKeyModel != null && s == HotKeyModel.SignName)
{
HotKeyModelChanged(HotKeyModel, this);
}
});
SetValue(RemoveHotKeyProperty, new RelayCommand<HotKeyModel>(Remove));
}

[Bindable(true)]
Expand Down Expand Up @@ -99,6 +108,14 @@ public string KeyName
private set => SetValue(KeyNameProperty, value);
}

[Bindable(true)]
[Category("RemoveHotKey")]
public ICommand RemoveHotKey
{
get => (ICommand)GetValue(RemoveHotKeyProperty);
private set => SetValue(RemoveHotKeyProperty, value);
}

private static void HotKeyModelChanged(HotKeyModel hotKeyModel, HotKeyShow hotKeyShow)
{
var type = 0000;
Expand Down Expand Up @@ -130,4 +147,14 @@ private static void HotKeyModelChanged(HotKeyModel hotKeyModel, HotKeyShow hotKe
hotKeyShow.KeyType = (HotKeyShow.KeyTypeE)type;
hotKeyShow.KeyName = hotKeyModel.SelectKey.ToString();
}

private void Remove(HotKeyModel? hotKeyModel)
{
if (hotKeyModel != null)
{
((MainWindow)ServiceManager.Services.GetService(typeof(MainWindow))!).RemoveHotKey(hotKeyModel);
SetValue(HotKeyModelProperty, null);
SetValue(KeyTypeProperty, KeyTypeE.None);
}
}
}
8 changes: 7 additions & 1 deletion uToolkitopia/Controls/HotKeyShow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,13 @@
Height="40" />
<controls:HotKeyShowItme x:Name="KeyName" Appearance="Primary" FontSize="20"
Foreground="{TemplateBinding Foreground}"
Content="{TemplateBinding KeyName}" Margin="5,0,20,0" Height="40" />
Content="{TemplateBinding KeyName}" Margin="5,0,0,0" Height="40" />
<ui:TextBlock Margin="10" x:Name="None" Text="未设置" VerticalAlignment="Center" HorizontalAlignment="Center" />
<ui:Button Margin="5,0,5,0" Padding="0" Height="32" Width="32" FontSize="24" ToolTip="移除快捷键" Command="{TemplateBinding RemoveHotKey}" CommandParameter="{TemplateBinding HotKeyModel}">
<ui:Button.Icon>
<ui:SymbolIcon FontSize="24" Symbol="Dismiss24" />
</ui:Button.Icon>
</ui:Button>
<ui:SymbolIcon
x:Name="ControlChevronIcon"
Foreground="{TemplateBinding Foreground}"
Expand Down Expand Up @@ -166,6 +171,7 @@
</ControlTemplate>

</Setter.Value>

</Setter>
</Style>

Expand Down
17 changes: 16 additions & 1 deletion uToolkitopia/SDKs/HotKeyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using Core.SDKs.HotKey;
using Core.SDKs.Services.Config;
using log4net;
using Vanara.PInvoke;

Expand Down Expand Up @@ -59,7 +60,7 @@ public static List<HotKeyModel> RegisterGlobalHotKey(IEnumerable<HotKeyModel> ho
private static bool RegisterHotKey(HotKeyModel hotKeyModel, IntPtr hWnd)
{
var fsModifierKey = new User32.HotKeyModifiers();
var hotKeySetting = $"{hotKeyModel.MainName}_{hotKeyModel.Name}";
var hotKeySetting = hotKeyModel.SignName;
if (!hotKeyModel.IsUsable)
{
return true;
Expand Down Expand Up @@ -108,4 +109,18 @@ private static bool RegisterHotKey(HotKeyModel hotKeyModel, IntPtr hWnd)
return User32.RegisterHotKey(hWnd, m_HotKeySettingsDic[hotKeySetting], fsModifierKey,
(uint)hotKeyModel.SelectKey);
}

public static void UnRegisterHotKey(HotKeyModel hotKeyModel, IntPtr hwnd)
{
log.Debug("注销热键:" + hotKeyModel.SignName);
User32.UnregisterHotKey(hwnd, m_HotKeySettingsDic[hotKeyModel.SignName]);
if (!Kernel32.GlobalFindAtom(hotKeyModel.SignName).IsInvalid)
{
Kernel32.GlobalDeleteAtom(Kernel32.GlobalFindAtom(hotKeyModel.SignName));
}

m_HotKeySettingsDic.Remove(hotKeyModel.SignName);
ConfigManger.Config.hotKeys.RemoveAll(e => e.SignName == hotKeyModel.SignName);
ConfigManger.Save();
}
}
5 changes: 5 additions & 0 deletions uToolkitopia/View/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ public bool HotKeySet(HotKeyModel hotKeyModel)
return false;
}

public void RemoveHotKey(HotKeyModel hotKeyModel)
{
HotKeyHelper.UnRegisterHotKey(hotKeyModel, m_Hwnd);
}

public void InitHotKey()
{
var list = ConfigManger.Config.hotKeys;
Expand Down

0 comments on commit 92f3f5c

Please sign in to comment.