-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAHKHelpWindow.cs
199 lines (161 loc) · 5.33 KB
/
AHKHelpWindow.cs
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Transactions;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Microsoft.VisualBasic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
class Program
{
static void Main(params string[] fileNames)
{
var keyTypes = new Dictionary<string, string>();
keyTypes.Add("Replacers", @"(\:\*\:)");
keyTypes.Add("Win", @"(#.\:\:)");
keyTypes.Add("L-Alt", @"(\<\^\>\!)");
var frm = CreateForm();
foreach (var type in keyTypes)
{
var panel = new Panel { Dock = DockStyle.Top, AutoScroll = true, Height = 250 };
frm.Controls.Add(panel);
var keyInfoCollections = GetAllKeyBindings(type.Value);
var keysCount = (int)Math.Ceiling((decimal)keyInfoCollections.Count / 4);
var height = (keyInfoCollections.Count > keysCount ? keysCount : keyInfoCollections.Count);
panel.Height = (height + 1) * 24;
FillInfo(type.Key, keyInfoCollections, panel, keysCount);
}
frm.ShowDialog();
}
static void FillInfo
(string heading, Dictionary<string, string> keyInfoCollections, Control panel, int itemsCount = 5)
{
var locationX = 0;
var locationY = 0;
var headingLabel = GetNewLable(heading, new System.Drawing.Point(locationX, locationY), 260, ContentAlignment.MiddleCenter);
headingLabel.BackColor = Color.Green;
headingLabel.ForeColor = Color.White;
headingLabel.Dock = DockStyle.Top;
panel.Controls.Add(headingLabel);
var startingLocationY = locationY += headingLabel.Height;
var itemsFilled = 0;
foreach (var key in keyInfoCollections.Keys.OrderBy(x => x))
{
if (itemsFilled != 0 && itemsFilled % itemsCount == 0)
{
locationX += 250;
locationY = startingLocationY;
}
var newKeyControl = GetNewLable(key.ToLower(), new System.Drawing.Point(locationX, locationY), 60, ContentAlignment.MiddleRight);
newKeyControl.Font = new Font(Label.DefaultFont, System.Drawing.FontStyle.Bold);
var newInfoControl = GetNewLable($": {keyInfoCollections[key]}", new System.Drawing.Point(locationX + 60, locationY), 200, ContentAlignment.MiddleLeft);
newKeyControl.Tag = newInfoControl;
newInfoControl.Tag = newKeyControl;
panel.Controls.Add(newKeyControl);
panel.Controls.Add(newInfoControl);
locationY += newKeyControl.Height;
itemsFilled++;
}
}
static Form CreateForm()
{
var frm = new Form() { Height = 580, Width = 1060, MaximizeBox = false, MinimizeBox = false, ShowIcon = false, Text = "AHK Shortcuts" };
frm.Load += new EventHandler((s, e) =>
{
var timer = new System.Windows.Forms.Timer();
timer.Interval = 5000;
timer.Tick += new EventHandler((s, e) =>
{
if (!frm.Focused) frm.Close();
});
timer.Start();
});
frm.KeyDown += new KeyEventHandler((s, e) => { if (e.KeyCode == Keys.Escape) frm.Close(); });
return frm;
}
static Dictionary<string, string> GetAllKeyBindings(string bindingPattern)
{
var keyInfoCollections = new Dictionary<string, string>();
var fileNames = new string[2] {
@"./MyKeys_AllDescktop.ahk",
@"./MyKeys_DesktopFocused.ahk"
};
foreach (var fileName in fileNames)
{
var lines = File.ReadAllLines(fileName);
//To Avoid getting #Include or #If
var regex = new Regex(bindingPattern, RegexOptions.IgnoreCase);
//To Avoid removing keys from the windows shortcuts. ex: removing 2 from #2 for vs22
var regex1 = new Regex(@"(\:\*\:)|(\<\^\>\!)|(#)", RegexOptions.IgnoreCase);
var keyLines = lines.Where(x => regex.Match(x)?.Success == true);
var resultLines = keyLines
.Where(x => !x.Contains("|"))
.Select(x => regex1.Replace(x, "")
.Replace("::", "")
.Split(';'));
var resultLines1 = keyLines
.Where(x => x.Contains("|"))
.Select(x => regex1.Replace(x, "")
.Replace("::", "")
.Split(';'));
//To combine multiple subkeys
foreach (var item in resultLines1)
{
var exKeys = item[1].Split('|');
foreach (var keyLine in exKeys)
{
var x = keyLine.Replace("--", "-");
var key = x.Split('-');
resultLines = resultLines.Append(new string[] { $"{item[0]}{key[0]}".Trim(), key[1].Trim() });
}
}
var dict = resultLines
.ToDictionary(k => k[0],
v => v.Length > 1 ? v[1] : v[0])
.Select(x => x);
keyInfoCollections = keyInfoCollections
.Concat(dict)
.ToDictionary(x => x.Key, v => v.Value);
}
return keyInfoCollections;
}
static Label GetNewLable(string text, System.Drawing.Point location, int Width, ContentAlignment alignment)
{
var label = new Label
{
Text = text,
Location = location,
Width = Width,
TextAlign = alignment
};
label.MouseEnter += new EventHandler((s, x) => { changeColor(s, Color.LightGray).GetAwaiter(); });
label.MouseLeave += new EventHandler((s, x) => { changeColor(s, Control.DefaultBackColor).GetAwaiter(); });
return label;
}
static async Task changeColor(object label, Color color)
{
await Task.Run(() =>
{
var ogLabel = (Label)label;
var coLabel = (Label)ogLabel.Tag;
if (coLabel == null)
{
return;
}
ogLabel.BackColor = coLabel.BackColor = color;
});
}
}