This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathHotkeyControl.cs
More file actions
157 lines (145 loc) · 6.03 KB
/
HotkeyControl.cs
File metadata and controls
157 lines (145 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using SPCode.Utils;
namespace SPCode.Interop
{
public class HotkeyControl
{
public static Dictionary<string, string> DefaultHotkeys = new()
{
{ "New", "Ctrl+N" },
{ "NewTemplate", "Ctrl+Shift+N" },
{ "Open", "Ctrl+O" },
{ "ReopenLastClosedTab", "Ctrl+Shift+T" },
{ "Save", "Ctrl+S" },
{ "SaveAll", "Ctrl+Shift+S" },
{ "SaveAs", "Ctrl+Alt+S" },
{ "Close", "Ctrl+W" },
{ "CloseAll", "Ctrl+Shift+W" },
{ "FoldingsExpand", "Ctrl+P" },
{ "FoldingsCollapse", "Ctrl+Shift+P" },
{ "ReformatCurrent", "Ctrl+R" },
{ "ReformatAll", "Ctrl+Shift+R" },
{ "GoToLine", "Ctrl+G" },
{ "CommentLine", "Ctrl+K" },
{ "UncommentLine", "Ctrl+Shift+K" },
{ "TransformUppercase", "Ctrl+U" },
{ "TransformLowercase", "Ctrl+Shift+U" },
{ "DeleteLine", "Ctrl+D" },
{ "MoveLineDown", "Ctrl+Down" },
{ "MoveLineUp", "Ctrl+Up" },
{ "DupeLineDown", "Ctrl+Alt+Down" },
{ "DupeLineUp", "Ctrl+Alt+Up" },
{ "SearchReplace", "Ctrl+F" },
{ "SearchDefinition", "Ctrl+Shift+F" },
{ "CompileCurrent", "F6" },
{ "CompileAll", "F5" },
{ "CopyPlugins", "F7" },
{ "UploadFTP", "F8" },
{ "StartServer", "F9" },
{ "SendRCON", "F10" },
};
public static Dictionary<string, string> RestrictedHotkeys = new()
{
{ "Paste", "Ctrl+V" },
{ "Copy", "Ctrl+C" },
{ "Cut", "Ctrl+X" },
{ "Undo", "Ctrl+Z" },
{ "Redo", "Ctrl+Y" },
{ "SelectAll", "Ctrl+A" }
};
/// <summary>
/// Checks if there are new Hotkeys that haven't been added to the Hotkeys file <br>
/// and buffers all Hotkeys to an array in memory.
/// </summary>
public static void CheckAndBufferHotkeys()
{
try
{
// Load the current Hotkeys file
Program.HotkeysList = new();
var document = new XmlDocument();
document.Load(Constants.HotkeysFile);
// Compare with the default hotkeys to check for new commands
var xmlNodes = document.ChildNodes[0].ChildNodes.Cast<XmlNode>().ToList();
var defaultHksCount = DefaultHotkeys.Count;
// If the count is not equal, there's a new hotkey to be added to the file
if (xmlNodes.Count != defaultHksCount)
{
// Regular for to get the index
for (var i = 0; i < defaultHksCount; i++)
{
var currentHk = DefaultHotkeys.ElementAt(i);
if (!xmlNodes.Any(x => x.Name.Equals(DefaultHotkeys.ElementAt(i).Key)))
{
var element = document.CreateElement(string.Empty, currentHk.Key, string.Empty);
var text = document.CreateTextNode(currentHk.Value);
element.AppendChild(text);
document.ChildNodes[0].InsertBefore(element, document.ChildNodes[0].ChildNodes[i]);
}
}
document.Save(Constants.HotkeysFile);
xmlNodes = document.ChildNodes[0].ChildNodes.Cast<XmlNode>().ToList();
}
xmlNodes.ForEach(x =>
{
var hki = new HotkeyInfo(new Hotkey(x.InnerText), x.Name);
Program.HotkeysList.Add(hki);
});
}
catch (XmlException ex)
{
var invalidHotkeysFile = Constants.HotkeysFile + ".invalid";
if (File.Exists(invalidHotkeysFile))
{
File.Delete(invalidHotkeysFile);
}
File.Move(Constants.HotkeysFile, invalidHotkeysFile);
CreateDefaultHotkeys();
MessageBox.Show("There was an error parsing the Hotkeys.xml file.\n" +
$"It has been renamed to {invalidHotkeysFile}, and a new one was created.\n" +
$"Details: {ex.Message}", "SPCode Error");
}
catch (Exception ex)
{
throw new Exception("Error while checking and buffering the hotkeys", ex);
}
}
/// <summary>
/// Creates the default hotkeys, stores them in an XML and buffers them.
/// </summary>
public static void CreateDefaultHotkeys()
{
try
{
// Create the XML document
var document = new XmlDocument();
var rootElement = document.CreateElement(string.Empty, "Hotkeys", string.Empty);
document.AppendChild(rootElement);
// Fill it with the default hotkeys from the dictionary
foreach (var item in DefaultHotkeys)
{
var element = document.CreateElement(string.Empty, item.Key, string.Empty);
var text = document.CreateTextNode(item.Value);
element.AppendChild(text);
rootElement.AppendChild(element);
}
// Buffer hotkeys in global HotkeyInfo list
Program.HotkeysList = new();
foreach (XmlNode node in document.ChildNodes[0].ChildNodes)
{
Program.HotkeysList.Add(new HotkeyInfo(new Hotkey(node.InnerText), node.Name));
}
document.Save(Constants.HotkeysFile);
}
catch (Exception ex)
{
throw new Exception("Error while creating the default hotkeys", ex);
}
}
}
}