Skip to content

Commit c746eaa

Browse files
committed
Shortcut Improvements
Better mechanism for checking for shorcuts. Some code refactoring for ShortcutManager class.
1 parent 3c50d51 commit c746eaa

File tree

6 files changed

+79
-60
lines changed

6 files changed

+79
-60
lines changed

External/Plugins/ProjectManager/Helpers/LineEntryDialog.cs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
using System;
2-
using System.Drawing;
3-
using System.Collections;
41
using System.ComponentModel;
52
using System.Windows.Forms;
63
using PluginCore;
@@ -13,13 +10,11 @@ namespace ProjectManager.Helpers
1310
/// </summary>
1411
public class LineEntryDialog : Form
1512
{
16-
readonly Keys shortcutToLowercase;
17-
readonly Keys shortcutToUppercase;
1813
string line;
1914

2015
#region Form Designer Components
2116

22-
protected System.Windows.Forms.TextBox lineBox;
17+
private System.Windows.Forms.TextBox lineBox;
2318
private System.Windows.Forms.Button btnOK;
2419
private System.Windows.Forms.Button btnCancel;
2520
/// <summary>
@@ -40,8 +35,6 @@ public string Line
4035

4136
public LineEntryDialog(string captionText, string labelText, string defaultLine)
4237
{
43-
shortcutToLowercase = PluginBase.MainForm.GetShortcutItemKeys("EditMenu.ToLowercase");
44-
shortcutToUppercase = PluginBase.MainForm.GetShortcutItemKeys("EditMenu.ToUppercase");
4538
InitializeComponent();
4639
InititalizeLocalization();
4740
this.Font = PluginBase.Settings.DefaultFont;
@@ -174,16 +167,22 @@ private void btnCancel_Click(object sender, System.EventArgs e)
174167

175168
void OnLineBoxOnKeyDown(object sender, KeyEventArgs args)
176169
{
177-
string selectedText = lineBox.SelectedText;
178-
if (string.IsNullOrEmpty(selectedText)) return;
179-
Keys keys = args.KeyData;
180-
if (keys == shortcutToLowercase) selectedText = selectedText.ToLower();
181-
else if (keys == shortcutToUppercase) selectedText = selectedText.ToUpper();
182-
else return;
183-
int selectionStart = lineBox.SelectionStart;
184-
int selectionLength = lineBox.SelectionLength;
185-
lineBox.Paste(selectedText);
186-
SelectRange(selectionStart, selectionLength);
170+
string shortcutId = PluginBase.MainForm.GetShortcutItemId(args.KeyData);
171+
if (string.IsNullOrEmpty(shortcutId)) return;
172+
173+
switch (shortcutId)
174+
{
175+
case "EditMenu.ToLowercase":
176+
case "EditMenu.ToUppercase":
177+
string selectedText = lineBox.SelectedText;
178+
if (string.IsNullOrEmpty(selectedText)) break;
179+
selectedText = shortcutId == "EditMenu.ToLowercase" ? selectedText.ToLower() : selectedText.ToUpper();
180+
int selectionStart = lineBox.SelectionStart;
181+
int selectionLength = lineBox.SelectionLength;
182+
lineBox.Paste(selectedText);
183+
SelectRange(selectionStart, selectionLength);
184+
break;
185+
}
187186
}
188187

189188
public void SelectRange(int start, int length)

External/Plugins/ProjectManager/PluginMain.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,19 +561,21 @@ private bool HandleKeyEvent(KeyEvent ke)
561561
{
562562
if (activeProject == null) return false;
563563

564-
if (ke.Value == PluginBase.MainForm.GetShortcutItemKeys("ProjectMenu.ConfigurationSelector"))
564+
string shortcutId = PluginBase.MainForm.GetShortcutItemId(ke.Value);
565+
566+
if (shortcutId == "ProjectMenu.ConfigurationSelector")
565567
{
566568
pluginUI.menus.ConfigurationSelector.Focus();
567569
}
568-
else if (ke.Value == PluginBase.MainForm.GetShortcutItemKeys("ProjectMenu.ConfigurationSelectorToggle"))
570+
else if (shortcutId == "ProjectMenu.ConfigurationSelectorToggle")
569571
{
570572
pluginUI.menus.ToggleDebugRelease();
571573
}
572-
else if (ke.Value == PluginBase.MainForm.GetShortcutItemKeys("ProjectMenu.TargetBuildSelector"))
574+
else if (shortcutId == "ProjectMenu.TargetBuildSelector")
573575
{
574576
pluginUI.menus.TargetBuildSelector.Focus();
575577
}
576-
else if (ke.Value == PluginBase.MainForm.GetShortcutItemKeys("ProjectTree.LocateActiveFile"))
578+
else if (shortcutId == "ProjectTree.LocateActiveFile")
577579
{
578580
ToggleTrackActiveDocument();
579581
}

FlashDevelop/Dialogs/ShortcutDialog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ private void PopulateListView(String filter, Boolean viewCustom)
284284
this.listView.BeginUpdate();
285285
this.listView.Items.Clear();
286286
this.listView.ListViewItemSorter = new ListViewComparer();
287-
foreach (ShortcutItem item in ShortcutManager.RegisteredItems)
287+
foreach (ShortcutItem item in ShortcutManager.RegisteredItems.Values)
288288
{
289289
if (!this.listView.Items.ContainsKey(item.Id) &&
290290
(item.Id.ToLower().Contains(filter.ToLower()) ||

FlashDevelop/MainForm.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,13 +1781,21 @@ public void AutoUpdateMenuItem(ToolStripItem item, String action)
17811781
}
17821782

17831783
/// <summary>
1784-
/// Gets the specified item's shortcut keys
1784+
/// Gets the specified item's shortcut keys.
17851785
/// </summary>
17861786
public Keys GetShortcutItemKeys(String id)
17871787
{
17881788
ShortcutItem item = ShortcutManager.GetRegisteredItem(id);
1789-
if (item != null) return item.Custom;
1790-
else return Keys.None;
1789+
return item == null ? Keys.None : item.Custom;
1790+
}
1791+
1792+
/// <summary>
1793+
/// Gets the specified item's id.
1794+
/// </summary>
1795+
public String GetShortcutItemId(Keys keys)
1796+
{
1797+
ShortcutItem item = ShortcutManager.GetRegisteredItem(keys);
1798+
return item == null ? string.Empty : item.Id;
17911799
}
17921800

17931801
/// <summary>

FlashDevelop/Managers/ShortcutManager.cs

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,33 @@
1010

1111
namespace FlashDevelop.Managers
1212
{
13-
class ShortcutManager
13+
static class ShortcutManager
1414
{
15-
public static List<Keys> AllShortcuts = new List<Keys>();
16-
public static List<ToolStripItem> SecondaryItems = new List<ToolStripItem>();
17-
public static List<ShortcutItem> RegisteredItems = new List<ShortcutItem>();
15+
public static readonly List<Keys> AllShortcuts;
16+
public static readonly List<ToolStripItem> SecondaryItems;
17+
public static readonly Dictionary<String, ShortcutItem> RegisteredItems;
18+
19+
static ShortcutManager()
20+
{
21+
AllShortcuts = new List<Keys>();
22+
SecondaryItems = new List<ToolStripItem>();
23+
RegisteredItems = new Dictionary<string, ShortcutItem>();
24+
}
1825

1926
/// <summary>
2027
/// Registers a shortcut item
2128
/// </summary>
2229
public static void RegisterItem(String key, Keys keys)
2330
{
24-
ShortcutItem registered = new ShortcutItem(key, keys);
25-
RegisteredItems.Add(registered);
31+
RegisteredItems.Add(key, new ShortcutItem(key, keys));
2632
}
2733

2834
/// <summary>
2935
/// Registers a shortcut item
3036
/// </summary>
3137
public static void RegisterItem(String key, ToolStripMenuItem item)
3238
{
33-
ShortcutItem registered = new ShortcutItem(key, item);
34-
RegisteredItems.Add(registered);
39+
RegisteredItems.Add(key, new ShortcutItem(key, item));
3540
}
3641

3742
/// <summary>
@@ -56,9 +61,19 @@ public static void RegisterSecondaryItem(String id, ToolStripItem item)
5661
/// </summary>
5762
public static ShortcutItem GetRegisteredItem(String id)
5863
{
59-
foreach (ShortcutItem item in RegisteredItems)
64+
ShortcutItem item;
65+
return RegisteredItems.TryGetValue(id, out item) ? item : null;
66+
}
67+
68+
/// <summary>
69+
/// Gets the specified registered shortcut item
70+
/// </summary>
71+
public static ShortcutItem GetRegisteredItem(Keys keys)
72+
{
73+
if (keys == Keys.None) return null;
74+
foreach (ShortcutItem item in RegisteredItems.Values)
6075
{
61-
if (item.Id == id) return item;
76+
if (item.Custom == keys) return item;
6277
}
6378
return null;
6479
}
@@ -70,16 +85,11 @@ public static ToolStripItem GetSecondaryItem(String id)
7085
{
7186
foreach (ToolStripItem item in SecondaryItems)
7287
{
73-
String temp = String.Empty;
74-
String[] ids = ((ItemData)item.Tag).Id.Split(';');
75-
if (ids.Length == 2 && String.IsNullOrEmpty(ids[1]))
76-
{
77-
temp = StripBarManager.GetMenuItemId(item);
78-
}
79-
else if (ids.Length == 2) temp = ids[1];
80-
if (!String.IsNullOrEmpty(temp) && temp == id)
88+
String[] ids = ((ItemData) item.Tag).Id.Split(';');
89+
if (ids.Length == 2)
8190
{
82-
return item;
91+
String temp = String.IsNullOrEmpty(ids[1]) ? StripBarManager.GetMenuItemId(item) : ids[1];
92+
if (temp == id) return item;
8393
}
8494
}
8595
return null;
@@ -90,7 +100,7 @@ public static ToolStripItem GetSecondaryItem(String id)
90100
/// </summary>
91101
public static void UpdateAllShortcuts()
92102
{
93-
foreach (ShortcutItem item in RegisteredItems)
103+
foreach (ShortcutItem item in RegisteredItems.Values)
94104
{
95105
if (!AllShortcuts.Contains(item.Custom))
96106
{
@@ -105,7 +115,7 @@ public static void UpdateAllShortcuts()
105115
public static void ApplyAllShortcuts()
106116
{
107117
UpdateAllShortcuts();
108-
foreach (ShortcutItem item in RegisteredItems)
118+
foreach (ShortcutItem item in RegisteredItems.Values)
109119
{
110120
if (item.Item != null)
111121
{
@@ -134,12 +144,11 @@ public static void ApplySecondaryShortcut(ToolStripItem item)
134144
if (item != null && item.Tag != null)
135145
{
136146
String id = String.Empty;
137-
String[] ids = ((ItemData)item.Tag).Id.Split(';');
138-
if (ids.Length == 2 && String.IsNullOrEmpty(ids[1]))
147+
String[] ids = ((ItemData) item.Tag).Id.Split(';');
148+
if (ids.Length == 2)
139149
{
140-
id = StripBarManager.GetMenuItemId(item);
150+
id = String.IsNullOrEmpty(ids[1]) ? StripBarManager.GetMenuItemId(item) : ids[1];
141151
}
142-
else if (ids.Length == 2) id = ids[1];
143152
else return; // No work for us here...
144153
Keys keys = Globals.MainForm.GetShortcutItemKeys(id);
145154
if (keys != Keys.None)
@@ -165,7 +174,7 @@ public static void ApplySecondaryShortcut(ToolStripItem item)
165174
}
166175

167176
/// <summary>
168-
/// Loads the custom shorcuts from a file
177+
/// Loads the custom shortcuts from a file
169178
/// </summary>
170179
public static void LoadCustomShortcuts()
171180
{
@@ -174,11 +183,11 @@ public static void LoadCustomShortcuts()
174183
if (File.Exists(file))
175184
{
176185
List<Argument> shortcuts = new List<Argument>();
177-
shortcuts = (List<Argument>)ObjectSerializer.Deserialize(file, shortcuts, false);
186+
shortcuts = (List<Argument>) ObjectSerializer.Deserialize(file, shortcuts, false);
178187
foreach (Argument arg in shortcuts)
179188
{
180189
ShortcutItem item = GetRegisteredItem(arg.Key);
181-
if (item != null) item.Custom = (Keys)Enum.Parse(typeof(Keys), arg.Value);
190+
if (item != null) item.Custom = (Keys) Enum.Parse(typeof(Keys), arg.Value);
182191
}
183192
}
184193
}
@@ -189,7 +198,7 @@ public static void LoadCustomShortcuts()
189198
public static void SaveCustomShortcuts()
190199
{
191200
List<Argument> shortcuts = new List<Argument>();
192-
foreach (ShortcutItem item in RegisteredItems)
201+
foreach (ShortcutItem item in RegisteredItems.Values)
193202
{
194203
if (item.Custom != item.Default)
195204
{
@@ -206,10 +215,10 @@ public static void SaveCustomShortcuts()
206215

207216
public class ShortcutItem
208217
{
209-
public Keys Custom = Keys.None;
210-
public Keys Default = Keys.None;
211-
public ToolStripMenuItem Item = null;
212-
public String Id = String.Empty;
218+
public String Id;
219+
public Keys Default;
220+
public Keys Custom;
221+
public ToolStripMenuItem Item;
213222

214223
public ShortcutItem(String id, Keys keys)
215224
{
@@ -224,7 +233,7 @@ public ShortcutItem(String id, ToolStripMenuItem item)
224233
this.Default = this.Custom = item.ShortcutKeys;
225234
}
226235

227-
public override string ToString()
236+
public override String ToString()
228237
{
229238
return Id;
230239
}

PluginCore/PluginCore/Interfaces.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public interface IMainForm : IContainerControl, IWin32Window
122122
ToolStripItem FindMenuItem(String name);
123123
String ProcessArgString(String args);
124124
Keys GetShortcutItemKeys(String id);
125+
String GetShortcutItemId(Keys keys);
125126
String GetThemeValue(String id);
126127
Color GetThemeColor(String id);
127128
Boolean GetThemeFlag(String id);

0 commit comments

Comments
 (0)