forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortcutManager.cs
More file actions
154 lines (138 loc) · 4.51 KB
/
ShortcutManager.cs
File metadata and controls
154 lines (138 loc) · 4.51 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
using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Collections.Generic;
using PluginCore.Managers;
using PluginCore.Utilities;
using FlashDevelop.Helpers;
using ScintillaNet;
using PluginCore;
namespace FlashDevelop.Managers
{
class ShortcutManager
{
public static List<Keys> AllShortcuts = new List<Keys>();
public static List<ShortcutItem> RegistedItems = new List<ShortcutItem>();
/// <summary>
/// Registers a shortcut item
/// </summary>
public static void RegisterItem(String key, Keys keys)
{
ShortcutItem registered = new ShortcutItem(key, keys);
RegistedItems.Add(registered);
}
/// <summary>
/// Registers a shortcut item
/// </summary>
public static void RegisterItem(String key, ToolStripMenuItem item)
{
ShortcutItem registered = new ShortcutItem(key, item);
RegistedItems.Add(registered);
}
/// <summary>
/// Gets the specified registered shortcut item
/// </summary>
public static ShortcutItem GetRegisteredItem(String id)
{
foreach (ShortcutItem item in RegistedItems)
{
if (item.Id == id) return item;
}
return null;
}
/// <summary>
/// Updates the list of all shortcuts
/// </summary>
public static void UpdateAllShortcuts()
{
foreach (ShortcutItem item in RegistedItems)
{
if (!AllShortcuts.Contains(item.Custom))
{
AllShortcuts.Add(item.Custom);
}
}
}
/// <summary>
/// Applies all shortcuts to the items
/// </summary>
public static void ApplyAllShortcuts()
{
ShortcutManager.UpdateAllShortcuts();
foreach (ShortcutItem item in RegistedItems)
{
if (item.Item != null)
{
item.Item.ShortcutKeys = Keys.None;
item.Item.ShortcutKeys = item.Custom;
}
else if (item.Default != item.Custom)
{
ScintillaControl.UpdateShortcut(item.Id, item.Custom);
DataEvent de = new DataEvent(EventType.Shortcut, item.Id, item.Custom);
EventManager.DispatchEvent(Globals.MainForm, de);
}
}
}
/// <summary>
/// Loads the custom shorcuts from a file
/// </summary>
public static void LoadCustomShortcuts()
{
ScintillaControl.InitShortcuts();
String file = FileNameHelper.ShortcutData;
if (File.Exists(file))
{
List<Argument> shortcuts = new List<Argument>();
shortcuts = (List<Argument>)ObjectSerializer.Deserialize(file, shortcuts, false);
foreach (Argument arg in shortcuts)
{
ShortcutItem item = GetRegisteredItem(arg.Key);
if (item != null) item.Custom = (Keys)Enum.Parse(typeof(Keys), arg.Value);
}
}
}
/// <summary>
/// Saves the custom shorcuts to a file
/// </summary>
public static void SaveCustomShortcuts()
{
List<Argument> shortcuts = new List<Argument>();
foreach (ShortcutItem item in RegistedItems)
{
if (item.Custom != item.Default)
{
shortcuts.Add(new Argument(item.Id, item.Custom.ToString()));
}
}
String file = FileNameHelper.ShortcutData;
ObjectSerializer.Serialize(file, shortcuts);
}
}
#region Helper Classes
public class ShortcutItem
{
public Keys Custom = Keys.None;
public Keys Default = Keys.None;
public ToolStripMenuItem Item = null;
public String Id = String.Empty;
public ShortcutItem(String id, Keys keys)
{
this.Id = id;
this.Default = this.Custom = keys;
}
public ShortcutItem(String id, ToolStripMenuItem item)
{
this.Id = id;
this.Item = item;
this.Default = this.Custom = item.ShortcutKeys;
}
public override string ToString()
{
return Id;
}
}
#endregion
}