Skip to content

Commit 5ef3872

Browse files
committed
ShortcutDialog update 3
Added loading/saving shortcut sets feature.
1 parent 38375ea commit 5ef3872

File tree

2 files changed

+111
-25
lines changed

2 files changed

+111
-25
lines changed

FlashDevelop/Dialogs/ShortcutDialog.cs

Lines changed: 91 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Drawing;
4+
using System.IO;
45
using System.Windows.Forms;
56
using FlashDevelop.Managers;
67
using PluginCore.Controls;
@@ -30,6 +31,8 @@ public class ShortcutDialog : SmartForm
3031
private System.Windows.Forms.CheckBox viewCustom;
3132
private System.Windows.Forms.Button clearButton;
3233
private System.Windows.Forms.Button closeButton;
34+
private System.Windows.Forms.Button openButton;
35+
private System.Windows.Forms.Button saveButton;
3336
private ShortcutListItem[] shortcutListItems;
3437

3538
public ShortcutDialog()
@@ -65,6 +68,8 @@ private void InitializeComponent()
6568
this.clearButton = new System.Windows.Forms.Button();
6669
this.filterTextBox = new System.Windows.Forms.TextBox();
6770
this.viewCustom = new System.Windows.Forms.CheckBox();
71+
this.openButton = new System.Windows.Forms.Button();
72+
this.saveButton = new System.Windows.Forms.Button();
6873
((System.ComponentModel.ISupportInitialize)(this.pictureBox)).BeginInit();
6974
this.SuspendLayout();
7075
//
@@ -79,15 +84,15 @@ private void InitializeComponent()
7984
this.keyHeader.Width = 208;
8085
//
8186
// listView
82-
//
87+
//
8388
this.listView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
8489
this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {this.idHeader, this.keyHeader});
8590
this.listView.GridLines = true;
8691
this.listView.FullRowSelect = true;
87-
this.listView.Location = new System.Drawing.Point(12, 62);
92+
this.listView.Location = new System.Drawing.Point(12, 87);
8893
this.listView.MultiSelect = false;
8994
this.listView.Name = "listView";
90-
this.listView.Size = new System.Drawing.Size(562, 312);
95+
this.listView.Size = new System.Drawing.Size(562, 287);
9196
this.listView.TabIndex = 4;
9297
this.listView.UseCompatibleStateImageBehavior = false;
9398
this.listView.View = System.Windows.Forms.View.Details;
@@ -128,15 +133,15 @@ private void InitializeComponent()
128133
//
129134
this.searchLabel.AutoSize = true;
130135
this.searchLabel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)));
131-
this.searchLabel.Location = new System.Drawing.Point(10, 10);
136+
this.searchLabel.Location = new System.Drawing.Point(10, 25);
132137
this.searchLabel.Name = "searchLabel";
133138
this.searchLabel.TabIndex = 0;
134139
this.searchLabel.Text = "Search:";
135140
//
136141
// clearButton
137-
//
142+
//
138143
this.clearButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
139-
this.clearButton.Location = new System.Drawing.Point(549, 30);
144+
this.clearButton.Location = new System.Drawing.Point(549, 39);
140145
this.clearButton.Name = "clearButton";
141146
this.clearButton.Size = new System.Drawing.Size(26, 23);
142147
this.clearButton.TabIndex = 2;
@@ -146,21 +151,37 @@ private void InitializeComponent()
146151
// filterTextBox
147152
//
148153
this.filterTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right)));
149-
this.filterTextBox.Location = new System.Drawing.Point(12, 32);
154+
this.filterTextBox.Location = new System.Drawing.Point(12, 41);
150155
this.filterTextBox.Name = "filterTextBox";
151-
this.filterTextBox.Size = new System.Drawing.Size(530, 20);
156+
this.filterTextBox.Size = new System.Drawing.Size(531, 20);
152157
this.filterTextBox.TabIndex = 1;
153158
this.filterTextBox.ForeColor = System.Drawing.SystemColors.GrayText;
154159
this.filterTextBox.TextChanged += new System.EventHandler(this.FilterTextChanged);
155160
//
156161
// viewCustom
157-
//
162+
//
158163
this.viewCustom.AutoSize = true;
159164
this.viewCustom.CheckAlign = ContentAlignment.MiddleRight;
160165
this.viewCustom.Anchor = ((System.Windows.Forms.AnchorStyles)(System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right));
161-
this.viewCustom.Location = new System.Drawing.Point(471, 9);
166+
this.viewCustom.Location = new System.Drawing.Point(471, 67);
162167
this.viewCustom.CheckedChanged += new System.EventHandler(this.ViewCustomCheckedChanged);
163168
//
169+
// openButton
170+
//
171+
this.openButton.Location = new System.Drawing.Point(369, 12);
172+
this.openButton.Name = "openButton";
173+
this.openButton.Size = new System.Drawing.Size(100, 23);
174+
this.openButton.UseVisualStyleBackColor = true;
175+
this.openButton.Click += new System.EventHandler(SelectCustomShortcut);
176+
//
177+
// saveButton
178+
//
179+
this.saveButton.Location = new System.Drawing.Point(475, 12);
180+
this.saveButton.Name = "saveButton";
181+
this.saveButton.Size = new System.Drawing.Size(100, 23);
182+
this.saveButton.UseVisualStyleBackColor = true;
183+
this.saveButton.Click += new System.EventHandler(SaveCustomShortcut);
184+
//
164185
// ShortcutDialog
165186
//
166187
this.ShowIcon = false;
@@ -175,6 +196,8 @@ private void InitializeComponent()
175196
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
176197
this.ClientSize = new System.Drawing.Size(586, 416);
177198
this.MinimumSize = new System.Drawing.Size(400, 250);
199+
this.Controls.Add(this.saveButton);
200+
this.Controls.Add(this.openButton);
178201
this.Controls.Add(this.filterTextBox);
179202
this.Controls.Add(this.clearButton);
180203
this.Controls.Add(this.infoLabel);
@@ -191,7 +214,7 @@ private void InitializeComponent()
191214
this.ResumeLayout(false);
192215
this.PerformLayout();
193216
}
194-
217+
195218
#endregion
196219

197220
#region Methods And Event Handlers
@@ -236,6 +259,8 @@ private void ApplyLocalizedTexts()
236259
this.infoLabel.Text = TextHelper.GetString("Info.ShortcutEditInfo");
237260
this.closeButton.Text = TextHelper.GetString("Label.Close");
238261
this.viewCustom.Text = TextHelper.GetString("Label.ViewCustom");
262+
this.openButton.Text = TextHelper.GetString("Label.Open");
263+
this.saveButton.Text = TextHelper.GetString("Label.SaveAs");
239264
this.searchLabel.Text = TextHelper.GetString("Label.Search").Replace("&", "") + ":";
240265
this.Text = " " + TextHelper.GetString("Title.Shortcuts");
241266
}
@@ -376,7 +401,7 @@ private void ListViewKeyDown(Object sender, KeyEventArgs e)
376401
/// <summary>
377402
/// Assign the new shortcut.
378403
/// </summary>
379-
private void AssignNewShortcut(ShortcutListItem item, Keys shortcut)
404+
private void AssignNewShortcut(ShortcutListItem item, Keys shortcut, Boolean suppressWarning = false)
380405
{
381406
if (shortcut == Keys.None || shortcut == Keys.Delete) shortcut = 0;
382407
else if (!ToolStripManager.IsValidShortcut(shortcut)) return;
@@ -394,6 +419,7 @@ private void AssignNewShortcut(ShortcutListItem item, Keys shortcut)
394419
i.Conflicts = conflicts;
395420
UpdateItemHighlightFont(i);
396421
}
422+
if (suppressWarning) return;
397423
ErrorManager.ShowWarning(TextHelper.GetString("Info.ShortcutIsAlreadyUsed"), null);
398424
this.filterTextBox.Focus(); // Set focus to filter...
399425
this.filterTextBox.Text = ViewConflictsKey + item.KeysString;
@@ -533,6 +559,58 @@ private void FilterTextChanged(Object sender, EventArgs e)
533559
this.updateTimer.Start();
534560
}
535561

562+
/// <summary>
563+
/// Switch to a custom shortcut set.
564+
/// </summary>
565+
private void SelectCustomShortcut(Object sender, EventArgs e)
566+
{
567+
OpenFileDialog dialog = new OpenFileDialog
568+
{
569+
Filter = TextHelper.GetString("Info.ArgumentFilter") + "|*.fda",
570+
InitialDirectory = PathHelper.ShortcutsDir,
571+
Title = " " + TextHelper.GetString("Title.OpenFileDialog")
572+
};
573+
574+
if (dialog.ShowDialog(this) == DialogResult.OK)
575+
{
576+
String extension = Path.GetExtension(dialog.FileName);
577+
if (extension.Equals(".fda", StringComparison.OrdinalIgnoreCase))
578+
{
579+
Keys[] shortcuts = ShortcutManager.LoadCustomShortcuts(dialog.FileName, this.shortcutListItems);
580+
if (shortcuts != null)
581+
{
582+
this.listView.BeginUpdate();
583+
for (int i = 0; i < shortcuts.Length; i++)
584+
{
585+
this.AssignNewShortcut(this.shortcutListItems[i], shortcuts[i], true);
586+
}
587+
this.listView.EndUpdate();
588+
}
589+
}
590+
}
591+
}
592+
593+
/// <summary>
594+
/// Save the current shortcut set to a file.
595+
/// </summary>
596+
private void SaveCustomShortcut(Object sender, EventArgs e)
597+
{
598+
SaveFileDialog dialog = new SaveFileDialog
599+
{
600+
AddExtension = true,
601+
DefaultExt = ".fda",
602+
Filter = TextHelper.GetString("Info.ArgumentFilter") + "|*.fda",
603+
InitialDirectory = PathHelper.ShortcutsDir,
604+
OverwritePrompt = true,
605+
Title = " " + TextHelper.GetString("Title.SaveFileDialog")
606+
};
607+
608+
if (dialog.ShowDialog(this) == DialogResult.OK)
609+
{
610+
ShortcutManager.SaveCustomShortcuts(dialog.FileName, this.shortcutListItems);
611+
}
612+
}
613+
536614
/// <summary>
537615
/// Closes the shortcut dialog
538616
/// </summary>
@@ -603,7 +681,7 @@ Int32 IComparer<ShortcutListItem>.Compare(ShortcutListItem x, ShortcutListItem y
603681
/// <summary>
604682
/// Represents a copy of a <see cref="ShortcutItem"/> as well as a visual component.
605683
/// </summary>
606-
class ShortcutListItem : ListViewItem
684+
class ShortcutListItem : ListViewItem, IShortcutItem
607685
{
608686
private Keys custom;
609687

FlashDevelop/Managers/ShortcutManager.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,35 +210,43 @@ public static void SaveCustomShortcuts()
210210
}
211211

212212
/// <summary>
213-
/// Loads the custom shortcuts from a file to a list.
213+
/// Loads the custom shortcuts from a file and returns an array of new shortcut keys
214+
/// corresponding to the shortcut items in the specified array.
214215
/// </summary>
215-
public static void LoadCustomShortcuts(String file, IEnumerable<IShortcutItem> items)
216+
public static Keys[] LoadCustomShortcuts(String file, IShortcutItem[] items)
216217
{
217218
if (File.Exists(file))
218219
{
219220
try
220221
{
222+
Keys[] newShortcuts = new Keys[items.Length];
223+
221224
List<Argument> customShortcuts = new List<Argument>();
222225
customShortcuts = (List<Argument>) ObjectSerializer.Deserialize(file, customShortcuts, false);
223-
foreach (IShortcutItem item in items)
226+
Int32 customCount = customShortcuts.Count;
227+
228+
for (int i = 0; i < items.Length; i++)
224229
{
225-
Keys newShortcut = item.Default;
226-
foreach (Argument arg in customShortcuts)
230+
IShortcutItem item = items[i];
231+
newShortcuts[i] = item.Default;
232+
for (int j = 0; j < customCount; j++)
227233
{
234+
Argument arg = customShortcuts[j];
228235
if (arg.Key == item.Id)
229236
{
230-
newShortcut = (Keys) Enum.Parse(typeof(Keys), arg.Value);
237+
newShortcuts[i] = (Keys) Enum.Parse(typeof(Keys), arg.Value);
231238
break;
232239
}
233240
}
234-
item.Custom = newShortcut;
235241
}
242+
return newShortcuts;
236243
}
237244
catch (Exception e)
238245
{
239246
ErrorManager.ShowError(e);
240247
}
241248
}
249+
return null;
242250
}
243251

244252
/// <summary>
@@ -291,12 +299,12 @@ public override String ToString()
291299
}
292300
}
293301

294-
public class IShortcutItem
302+
public interface IShortcutItem
295303
{
296-
public String Id { get; }
297-
public Keys Default { get; }
298-
public Keys Custom { get; set; }
299-
public Boolean IsModified { get; }
304+
String Id { get; }
305+
Keys Default { get; }
306+
Keys Custom { get; }
307+
Boolean IsModified { get; }
300308
}
301309

302310
#endregion

0 commit comments

Comments
 (0)