Skip to content
This repository was archived by the owner on Sep 11, 2023. It is now read-only.

Commit 46b440c

Browse files
committed
fixed hotkeys file not updating when a new hotkey was implemented
1 parent b6951bc commit 46b440c

File tree

2 files changed

+73
-28
lines changed

2 files changed

+73
-28
lines changed

Interop/HotkeyControl.cs

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
25
using System.Xml;
36
using SPCode.Utils;
47

@@ -52,18 +55,51 @@ public class HotkeyControl
5255
};
5356

5457
/// <summary>
55-
/// Buffers the hotkeys to an array in memory.
58+
/// Checks if there are new Hotkeys that haven't been added to the Hotkeys file <br>
59+
/// and buffers all Hotkeys to an array in memory.
5660
/// </summary>
57-
public static void BufferHotkeys()
61+
public static void CheckAndBufferHotkeys()
5862
{
59-
// Buffer hotkeys in global HotkeyInfo list
60-
Program.HotkeysList = new();
61-
var document = new XmlDocument();
62-
document.Load(Constants.HotkeysFile);
63+
try
64+
{
65+
// Load the current Hotkeys file
66+
Program.HotkeysList = new();
67+
var document = new XmlDocument();
68+
document.Load(Constants.HotkeysFile);
69+
70+
// Compare with the default hotkeys to check for new commands
71+
var xmlNodes = document.ChildNodes[0].ChildNodes.Cast<XmlNode>().ToList();
72+
var defaultHksCount = DefaultHotkeys.Count;
73+
74+
// If the count is not equal, there's a new hotkey to be added to the file
75+
if (xmlNodes.Count != defaultHksCount)
76+
{
77+
// Regular for to get the index
78+
for (var i = 0; i < defaultHksCount; i++)
79+
{
80+
var currentHk = DefaultHotkeys.ElementAt(i);
81+
if (!xmlNodes.Any(x => x.Name.Equals(DefaultHotkeys.ElementAt(i).Key)))
82+
{
83+
var element = document.CreateElement(string.Empty, currentHk.Key, string.Empty);
84+
var text = document.CreateTextNode(currentHk.Value);
85+
element.AppendChild(text);
86+
document.ChildNodes[0].InsertBefore(element, document.ChildNodes[0].ChildNodes[i]);
87+
}
88+
}
89+
document.Save(Constants.HotkeysFile);
90+
xmlNodes = document.ChildNodes[0].ChildNodes.Cast<XmlNode>().ToList();
91+
}
6392

64-
foreach (XmlNode node in document.ChildNodes[0].ChildNodes)
93+
xmlNodes.ForEach(x =>
94+
{
95+
var hki = new HotkeyInfo(new Hotkey(x.InnerText), x.Name);
96+
Program.HotkeysList.Add(hki);
97+
});
98+
99+
}
100+
catch (Exception ex)
65101
{
66-
Program.HotkeysList.Add(new HotkeyInfo(new Hotkey(node.InnerText), node.Name));
102+
throw new Exception("Error while checking and buffering the hotkeys", ex);
67103
}
68104
}
69105

@@ -72,29 +108,36 @@ public static void BufferHotkeys()
72108
/// </summary>
73109
public static void CreateDefaultHotkeys()
74110
{
75-
// Create the XML document
76-
var document = new XmlDocument();
111+
try
112+
{
113+
// Create the XML document
114+
var document = new XmlDocument();
77115

78-
var rootElement = document.CreateElement(string.Empty, "Hotkeys", string.Empty);
79-
document.AppendChild(rootElement);
116+
var rootElement = document.CreateElement(string.Empty, "Hotkeys", string.Empty);
117+
document.AppendChild(rootElement);
80118

81-
// Fill it with the default hotkeys from the dictionary
82-
foreach (var item in DefaultHotkeys)
83-
{
84-
var element = document.CreateElement(string.Empty, item.Key, string.Empty);
85-
var text = document.CreateTextNode(item.Value);
86-
element.AppendChild(text);
87-
rootElement.AppendChild(element);
88-
}
119+
// Fill it with the default hotkeys from the dictionary
120+
foreach (var item in DefaultHotkeys)
121+
{
122+
var element = document.CreateElement(string.Empty, item.Key, string.Empty);
123+
var text = document.CreateTextNode(item.Value);
124+
element.AppendChild(text);
125+
rootElement.AppendChild(element);
126+
}
127+
128+
// Buffer hotkeys in global HotkeyInfo list
129+
Program.HotkeysList = new List<HotkeyInfo>();
130+
foreach (XmlNode node in document.ChildNodes[0].ChildNodes)
131+
{
132+
Program.HotkeysList.Add(new HotkeyInfo(new Hotkey(node.InnerText), node.Name));
133+
}
89134

90-
// Buffer hotkeys in global HotkeyInfo list
91-
Program.HotkeysList = new List<HotkeyInfo>();
92-
foreach (XmlNode node in document.ChildNodes[0].ChildNodes)
135+
document.Save(Constants.HotkeysFile);
136+
}
137+
catch (Exception ex)
93138
{
94-
Program.HotkeysList.Add(new HotkeyInfo(new Hotkey(node.InnerText), node.Name));
139+
throw new Exception("Error while creating the default hotkeys", ex);
95140
}
96-
97-
document.Save(Constants.HotkeysFile);
98141
}
99142
}
100143
}

Program.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Globalization;
45
using System.IO;
56
using System.Reflection;
@@ -81,7 +82,7 @@ public static void Main(string[] args)
8182
}
8283
else
8384
{
84-
HotkeyControl.BufferHotkeys();
85+
HotkeyControl.CheckAndBufferHotkeys();
8586
}
8687

8788
// Delete the default Ctrl+D hotkey to assign manually
@@ -275,6 +276,7 @@ private static void App_Startup(object sender, StartupEventArgs e)
275276
ThemeManager.GetAppTheme("BaseDark")); // or appStyle.Item1
276277
}
277278

279+
[SuppressMessage("CodeQuality", "IDE0051:Remove unused private members")]
278280
private static string BuildExceptionString(Exception e, string SectionName)
279281
{
280282
var outString = new StringBuilder();

0 commit comments

Comments
 (0)