mirror of
https://github.com/jie65535/GrasscutterCommandGenerator.git
synced 2025-10-23 20:29:45 +08:00
Add HotKey Page (close #190)
This commit is contained in:
242
Source/GrasscutterTools/Pages/PageHotKey.cs
Normal file
242
Source/GrasscutterTools/Pages/PageHotKey.cs
Normal file
@@ -0,0 +1,242 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Windows.Forms;
|
||||
|
||||
using GrasscutterTools.Properties;
|
||||
using GrasscutterTools.Utils;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace GrasscutterTools.Pages
|
||||
{
|
||||
internal partial class PageHotKey : BasePage
|
||||
{
|
||||
private const string TAG = nameof(PageHotKey);
|
||||
|
||||
public PageHotKey()
|
||||
{
|
||||
InitializeComponent();
|
||||
LvHotKeyList.FullRowSelect = true;
|
||||
if (DesignMode) return;
|
||||
|
||||
InitHotKeys();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 热键保存位置
|
||||
/// </summary>
|
||||
private readonly string HotKeysFilePath = Common.GetAppDataFile("HotKeys.json");
|
||||
|
||||
/// <summary>
|
||||
/// 热键配置是否存在更改
|
||||
/// </summary>
|
||||
private bool HotKeysChanged;
|
||||
|
||||
/// <summary>
|
||||
/// 初始化快捷键
|
||||
/// </summary>
|
||||
private void InitHotKeys()
|
||||
{
|
||||
if (!File.Exists(HotKeysFilePath))
|
||||
return;
|
||||
try
|
||||
{
|
||||
Logger.I(TAG, "Loading HotKey json file from: " + HotKeysFilePath);
|
||||
Common.KeyGo.Items = JsonConvert.DeserializeObject<List<HotKeyItem>>(File.ReadAllText(HotKeysFilePath));
|
||||
LvHotKeyList.Items.AddRange(Common.KeyGo.Items.Select(HotKeyItemToViewItem).ToArray());
|
||||
Logger.I(TAG, "Start Register All HotKeys");
|
||||
Common.KeyGo.RegAllKey();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.W(TAG, "Parsing HotKeys.json failed.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 关闭时触发,取消注册并保存更改
|
||||
/// </summary>
|
||||
public override void OnClosed()
|
||||
{
|
||||
Logger.I(TAG, "Cancel all HotKeys");
|
||||
Common.KeyGo.UnRegAllKey();
|
||||
|
||||
if (!HotKeysChanged) return;
|
||||
Logger.I(TAG, "Save all HotKeys to: " + HotKeysFilePath);
|
||||
File.WriteAllText(HotKeysFilePath, JsonConvert.SerializeObject(Common.KeyGo.Items));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将实体转为视图对象
|
||||
/// </summary>
|
||||
private static ListViewItem HotKeyItemToViewItem(HotKeyItem item) => new ListViewItem(new[]
|
||||
{
|
||||
item.Tag,
|
||||
item.HotKey,
|
||||
item.Commands
|
||||
}) { Checked = item.IsEnabled };
|
||||
|
||||
/// <summary>
|
||||
/// 列表选中项改变时触发
|
||||
/// </summary>
|
||||
private void LvHotKeyList_SelectedIndexChanged(object sender, EventArgs e)
|
||||
{
|
||||
if (LvHotKeyList.SelectedIndices.Count == 0) return;
|
||||
var i = LvHotKeyList.SelectedIndices[0];
|
||||
var hotKeyItem = Common.KeyGo.Items[i];
|
||||
TxtTag.Text = hotKeyItem.Tag;
|
||||
TxtHotKey.Text = hotKeyItem.HotKey;
|
||||
SetCommand(hotKeyItem.Commands);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 点击添加或更新按钮时触发
|
||||
/// </summary>
|
||||
private void BtnAddOrUpdate_Click(object sender, EventArgs e)
|
||||
{
|
||||
var tag = TxtTag.Text.Trim();
|
||||
var commands = GetCommand();
|
||||
var hotKey = TxtHotKey.Text;
|
||||
if (string.IsNullOrEmpty(tag) || string.IsNullOrEmpty(commands) || string.IsNullOrEmpty(hotKey))
|
||||
{
|
||||
MessageBox.Show(Resources.EmptyInputTip, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var i = Common.KeyGo.Items.FindIndex(it => it.Tag == tag);
|
||||
if (i == -1)
|
||||
{
|
||||
var item = new HotKeyItem
|
||||
{
|
||||
Tag = tag,
|
||||
Commands = commands,
|
||||
HotKey = hotKey
|
||||
};
|
||||
Logger.I(TAG, $"New HotKey item [{hotKey}]");
|
||||
Common.KeyGo.AddHotKey(item);
|
||||
LvHotKeyList.Items.Add(HotKeyItemToViewItem(item));
|
||||
}
|
||||
else
|
||||
{
|
||||
var item = Common.KeyGo.Items[i];
|
||||
item.Commands = commands;
|
||||
if (item.HotKey != hotKey)
|
||||
{
|
||||
Logger.I(TAG, $"Update HotKey from [{item.HotKey}] to [{hotKey}]");
|
||||
item.HotKey = hotKey;
|
||||
Common.KeyGo.ChangeHotKey(item);
|
||||
}
|
||||
|
||||
LvHotKeyList.Items[i] = HotKeyItemToViewItem(item);
|
||||
}
|
||||
|
||||
HotKeysChanged = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.E(TAG, "AddOrUpdate HotKey failed", ex);
|
||||
MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 点击移除按钮时触发
|
||||
/// </summary>
|
||||
private void BtnRemove_Click(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
var tag = TxtTag.Text.Trim();
|
||||
var i = Common.KeyGo.Items.FindIndex(it => it.Tag == tag);
|
||||
if (i == -1) return;
|
||||
var item = Common.KeyGo.Items[i];
|
||||
Logger.I(TAG, $"Remove HotKey [{item.HotKey}] \"{item.Tag}\"");
|
||||
Common.KeyGo.DelHotKey(item);
|
||||
LvHotKeyList.Items.RemoveAt(i);
|
||||
HotKeysChanged = true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Logger.E(TAG, "Remove HotKey failed", ex);
|
||||
MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 快捷键选项卡按键按下时触发
|
||||
/// </summary>
|
||||
private void TxtHotKey_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
// ESC键清空当前快捷键
|
||||
if (e.KeyCode == Keys.Escape)
|
||||
{
|
||||
TxtHotKey.Text = "";
|
||||
return;
|
||||
}
|
||||
|
||||
// 必须带功能键
|
||||
if (e.Modifiers == Keys.None)
|
||||
return;
|
||||
|
||||
// 必须是组合键
|
||||
if (e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Menu)
|
||||
return;
|
||||
|
||||
var text = e.KeyCode.ToString();
|
||||
if (e.Control)
|
||||
text = "Ctrl + " + text;
|
||||
if (e.Shift)
|
||||
text = "Shift + " + text;
|
||||
if (e.Alt)
|
||||
text = "Alt + " + text;
|
||||
|
||||
TxtHotKey.Text = text;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 列表中的复选框改变时触发
|
||||
/// </summary>
|
||||
private void LvHotKeyList_ItemCheck(object sender, ItemCheckEventArgs e)
|
||||
{
|
||||
var isEnable = e.NewValue == CheckState.Checked;
|
||||
try
|
||||
{
|
||||
var item = Common.KeyGo.Items[e.Index];
|
||||
if (isEnable)
|
||||
{
|
||||
// 尝试注册快捷键
|
||||
Logger.I(TAG, $"Register hotKey [{item.HotKey}] as \"{item.Tag}\"");
|
||||
Common.KeyGo.RegKey(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 尝试注销快捷键
|
||||
Logger.I(TAG, $"Cancel hotKey [{item.HotKey}]");
|
||||
Common.KeyGo.UnRegKey(item);
|
||||
}
|
||||
// 更新使能状态
|
||||
item.IsEnabled = isEnable;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果操作失败,还原选项,禁止设置
|
||||
e.NewValue = e.CurrentValue;
|
||||
Logger.E(TAG, (isEnable ? "Enable" : "Disable") +" HotKey failed", ex);
|
||||
MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加热键快捷设置方法
|
||||
/// </summary>
|
||||
/// <param name="tag">标签名</param>
|
||||
public void AddNewHotKey(string tag)
|
||||
{
|
||||
TxtHotKey.Tag = "";
|
||||
TxtTag.Text = tag;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user