Skip to content

Commit 6df86df

Browse files
committed
Improved ShortcutDialog update 1
Code refactoring and added localization of texts.
1 parent 7302f9f commit 6df86df

File tree

6 files changed

+126
-79
lines changed

6 files changed

+126
-79
lines changed

FlashDevelop/Dialogs/ShortcutDialog.cs

Lines changed: 107 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -245,34 +245,29 @@ private void ApplyScaling()
245245
this.idHeader.Width = ScaleHelper.Scale(this.idHeader.Width);
246246
this.keyHeader.Width = ScaleHelper.Scale(this.keyHeader.Width);
247247
}
248-
249-
/// <summary>
250-
/// Gets the keys as a string
251-
/// </summary>
252-
private static String GetKeysAsString(Keys keys)
253-
{
254-
return DataConverter.KeysToString(keys);
255-
}
256-
248+
257249
/// <summary>
258250
/// Updates the font highlight of the item
259251
/// </summary>
260252
private static void UpdateItemHighlightFont(ShortcutListItem item)
261253
{
262254
item.ForeColor = item.Conflicts == null ? SystemColors.ControlText : Color.DarkRed;
263255
item.SubItems[1].ForeColor = item.KeysString == "None" ? SystemColors.GrayText : SystemColors.ControlText;
264-
if (item.Default == item.Custom)
256+
if (item.IsModified)
265257
{
266-
item.Font = new Font(Globals.Settings.DefaultFont, FontStyle.Regular);
267-
item.UseItemStyleForSubItems = false;
258+
item.Font = new Font(Globals.Settings.DefaultFont, FontStyle.Bold);
259+
item.UseItemStyleForSubItems = true;
268260
}
269261
else
270262
{
271-
item.Font = new Font(Globals.Settings.DefaultFont, FontStyle.Bold);
272-
item.UseItemStyleForSubItems = true;
263+
item.Font = new Font(Globals.Settings.DefaultFont, FontStyle.Regular);
264+
item.UseItemStyleForSubItems = false;
273265
}
274266
}
275267

268+
/// <summary>
269+
/// Initialize the full shortcut list.
270+
/// </summary>
276271
private void InitializeShortcutListItems()
277272
{
278273
Dictionary<String, ShortcutItem>.ValueCollection collection = ShortcutManager.RegisteredItems.Values;
@@ -292,22 +287,8 @@ private void InitializeShortcutListItems()
292287
/// </summary>
293288
private void PopulateListView(String filter)
294289
{
295-
// TODO: Read starting char of filter
296290
Boolean viewCustom = false, viewConflicts = false;
297-
checkSpecialFilterKeys:
298-
if (filter.StartsWith(ViewCustomKey)) // TODO: StartsWithOrdinal
299-
{
300-
filter = filter.Substring(ViewCustomKey.Length);
301-
viewCustom = true;
302-
}
303-
if (!viewConflicts && filter.StartsWith(ViewConflictsKey))
304-
{
305-
filter = filter.Substring(ViewConflictsKey.Length);
306-
viewConflicts = true;
307-
if (!viewCustom) goto checkSpecialFilterKeys;
308-
}
309-
310-
filter = filter.Trim();
291+
filter = ExtractFilterKeywords(filter, ref viewCustom, ref viewConflicts);
311292

312293
this.listView.BeginUpdate();
313294
this.listView.Items.Clear();
@@ -317,7 +298,7 @@ private void PopulateListView(String filter)
317298
item.Id.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0 ||
318299
item.KeysString.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0)
319300
{
320-
if (viewCustom && item.Custom == item.Default) continue;
301+
if (viewCustom && !item.IsModified) continue;
321302
if (viewConflicts && item.Conflicts == null) continue;
322303
this.listView.Items.Add(item);
323304
}
@@ -330,6 +311,27 @@ private void PopulateListView(String filter)
330311
}
331312
}
332313

314+
/// <summary>
315+
/// Reads and removes filter keywords from the start of the filter.
316+
/// Order of the keywords is irrelevant.
317+
/// </summary>
318+
private static String ExtractFilterKeywords(String filter, ref Boolean viewCustom, ref Boolean viewConflicts)
319+
{
320+
if (!viewCustom && filter.StartsWith(ViewCustomKey))
321+
{
322+
filter = filter.Substring(ViewCustomKey.Length);
323+
viewCustom = true;
324+
return ExtractFilterKeywords(filter, ref viewCustom, ref viewConflicts);
325+
}
326+
if (!viewConflicts && filter.StartsWith(ViewConflictsKey))
327+
{
328+
filter = filter.Substring(ViewConflictsKey.Length);
329+
viewConflicts = true;
330+
return ExtractFilterKeywords(filter, ref viewCustom, ref viewConflicts);
331+
}
332+
return filter.Trim();
333+
}
334+
333335
/// <summary>
334336
/// Raised when the context menu for the list view is opening.
335337
/// </summary>
@@ -339,18 +341,17 @@ private void ContextMenuOpening(Object sender, EventArgs e)
339341
{
340342
ShortcutListItem item = (ShortcutListItem) this.listView.SelectedItems[0];
341343
this.removeShortcut.Enabled = item.Custom != Keys.None;
342-
this.revertToDefault.Enabled = this.revertAllToDefault.Enabled = item.Custom != item.Default;
344+
this.revertToDefault.Enabled = this.revertAllToDefault.Enabled = item.IsModified;
345+
if (this.revertAllToDefault.Enabled) return;
343346
}
344-
else
347+
else this.removeShortcut.Enabled = this.revertToDefault.Enabled = false;
348+
349+
foreach (ShortcutListItem item in this.listView.Items)
345350
{
346-
this.removeShortcut.Enabled = this.revertToDefault.Enabled = false;
347-
foreach (ShortcutListItem item in this.listView.Items)
351+
if (item.IsModified)
348352
{
349-
if (item.Custom != item.Default)
350-
{
351-
this.revertAllToDefault.Enabled = true;
352-
break;
353-
}
353+
this.revertAllToDefault.Enabled = true;
354+
break;
354355
}
355356
}
356357
}
@@ -363,8 +364,7 @@ private void ListViewKeyDown(Object sender, KeyEventArgs e)
363364
if (this.listView.SelectedItems.Count == 0) return;
364365

365366
ShortcutListItem item = (ShortcutListItem) this.listView.SelectedItems[0];
366-
if (e.KeyData == Keys.Delete) RemoveShortcut(item);
367-
else this.AssignNewShortcut(item, e.KeyData);
367+
this.AssignNewShortcut(item, e.KeyData);
368368

369369
// Don't trigger list view default shortcuts like Ctrl+Add
370370
if (e.KeyData != Keys.Up && e.KeyData != Keys.Down) e.Handled = true;
@@ -375,44 +375,38 @@ private void ListViewKeyDown(Object sender, KeyEventArgs e)
375375
/// </summary>
376376
private void AssignNewShortcut(ShortcutListItem item, Keys shortcut)
377377
{
378-
if (item.Custom == shortcut || !ToolStripManager.IsValidShortcut(shortcut)) return;
378+
if (shortcut == Keys.None || shortcut == Keys.Delete) shortcut = 0;
379+
else if (!ToolStripManager.IsValidShortcut(shortcut)) return;
379380

381+
if (item.Custom == shortcut) return;
380382
item.Custom = shortcut;
381383
item.Selected = true;
382384
ResetConflicts(item);
383385
List<ShortcutListItem> conflicts = this.GetConflictItems(shortcut);
384-
if (conflicts != null)
386+
if (conflicts == null) UpdateItemHighlightFont(item);
387+
else
385388
{
386389
foreach (ShortcutListItem i in conflicts)
387390
{
388391
i.Conflicts = conflicts;
389392
UpdateItemHighlightFont(i);
390393
}
391-
String message = TextHelper.GetString("Info.ShortcutIsAlreadyUsed");
392-
ErrorManager.ShowWarning(message, null);
394+
ErrorManager.ShowWarning(TextHelper.GetString("Info.ShortcutIsAlreadyUsed"), null);
393395
this.filterTextBox.Focus(); // Set focus to filter...
394-
this.filterTextBox.Text = GetKeysAsString(shortcut);
396+
this.filterTextBox.Text = ViewConflictsKey + item.KeysString;
395397
this.filterTextBox.SelectAll();
396398
}
397399
}
398-
400+
399401
/// <summary>
400-
/// Remove shortcut keys from an item
402+
/// Resets the conflicts status of an item.
401403
/// </summary>
402-
private static void RemoveShortcut(ShortcutListItem item)
403-
{
404-
item.Custom = 0;
405-
item.Selected = true;
406-
ResetConflicts(item);
407-
}
408-
409404
private static void ResetConflicts(ShortcutListItem item)
410405
{
411406
List<ShortcutListItem> conflicts = item.Conflicts;
412407
if (conflicts == null) return;
413408
item.Conflicts = null;
414409
conflicts.Remove(item);
415-
UpdateItemHighlightFont(item);
416410
if (conflicts.Count == 1)
417411
{
418412
item = conflicts[0];
@@ -444,6 +438,7 @@ private void ViewCustomCheckedChanged(Object sender, EventArgs e)
444438
/// </summary>
445439
private List<ShortcutListItem> GetConflictItems(Keys keys)
446440
{
441+
if (keys == Keys.None) return null;
447442
// To prevent creation of unnecessary List<T> objects
448443
ShortcutListItem first = null;
449444
List<ShortcutListItem> items = null;
@@ -494,7 +489,7 @@ private void RemoveShortcutClick(Object sender, EventArgs e)
494489
{
495490
if (this.listView.SelectedItems.Count == 0) return;
496491
ShortcutListItem item = (ShortcutListItem) this.listView.SelectedItems[0];
497-
RemoveShortcut(item);
492+
this.AssignNewShortcut(item, Keys.None);
498493
}
499494

500495
/// <summary>
@@ -511,18 +506,18 @@ private void ClearFilterClick(Object sender, EventArgs e)
511506
/// </summary>
512507
private void SetupUpdateTimer()
513508
{
514-
updateTimer = new Timer();
515-
updateTimer.Enabled = false;
516-
updateTimer.Interval = 100;
517-
updateTimer.Tick += UpdateTimer_Tick;
509+
this.updateTimer = new Timer();
510+
this.updateTimer.Enabled = false;
511+
this.updateTimer.Interval = 100;
512+
this.updateTimer.Tick += this.UpdateTimer_Tick;
518513
}
519514

520515
/// <summary>
521516
/// Update the list with filter.
522517
/// </summary>
523518
private void UpdateTimer_Tick(Object sender, EventArgs e)
524519
{
525-
updateTimer.Enabled = false;
520+
this.updateTimer.Enabled = false;
526521
this.PopulateListView(this.filterTextBox.Text);
527522
}
528523

@@ -531,8 +526,8 @@ private void UpdateTimer_Tick(Object sender, EventArgs e)
531526
/// </summary>
532527
private void FilterTextChanged(Object sender, EventArgs e)
533528
{
534-
updateTimer.Stop();
535-
updateTimer.Start();
529+
this.updateTimer.Stop();
530+
this.updateTimer.Start();
536531
}
537532

538533
/// <summary>
@@ -552,8 +547,7 @@ private void DialogClosing(Object sender, FormClosingEventArgs e)
552547
{
553548
if (item.Conflicts != null)
554549
{
555-
// TODO: Localize the text.
556-
ErrorManager.ShowError("There are one or more conflicts present.", null);
550+
ErrorManager.ShowError(TextHelper.GetString("Info.ShortcutConflictsPresent"), null);
557551
this.filterTextBox.Text = ViewConflictsKey;
558552
e.Cancel = true;
559553
break;
@@ -590,6 +584,9 @@ private void DialogClosed(Object sender, FormClosedEventArgs e)
590584

591585
#region ListViewComparer
592586

587+
/// <summary>
588+
/// Defines a method that compares two <see cref="ShortcutListItem"/> objects.
589+
/// </summary>
593590
class ShorcutListItemComparer : IComparer<ShortcutListItem>
594591
{
595592
Int32 IComparer<ShortcutListItem>.Compare(ShortcutListItem x, ShortcutListItem y)
@@ -600,38 +597,69 @@ Int32 IComparer<ShortcutListItem>.Compare(ShortcutListItem x, ShortcutListItem y
600597

601598
#endregion
602599

600+
/// <summary>
601+
/// Represents a copy of a <see cref="ShortcutItem"/> as well as a visual component.
602+
/// </summary>
603603
class ShortcutListItem : ListViewItem
604604
{
605605
private Keys custom;
606606

607-
public ShortcutItem Item { get; set; }
607+
/// <summary>
608+
/// Gets the associated <see cref="ShortcutItem"/> object.
609+
/// </summary>
610+
public ShortcutItem Item { get; private set; }
611+
/// <summary>
612+
/// Gets or sets a list of <see cref="ShortcutListItem"/> objects that have conflicting keys with this instance.
613+
/// </summary>
608614
public List<ShortcutListItem> Conflicts { get; set; }
609-
public String Id { get { return Item.Id; } }
610-
public Keys Default { get { return Item.Default; } }
615+
/// <summary>
616+
/// Gets the ID of the associated <see cref="ShortcutItem"/>.
617+
/// </summary>
618+
public String Id { get { return this.Item.Id; } }
619+
/// <summary>
620+
/// Gets the default shortcut keys.
621+
/// </summary>
622+
public Keys Default { get { return this.Item.Default; } }
623+
/// <summary>
624+
/// Gets or sets the custom shortcut keys.
625+
/// </summary>
611626
public Keys Custom
612627
{
613-
get { return custom; }
628+
get { return this.custom; }
614629
set
615630
{
616-
custom = value;
617-
KeysString = DataConverter.KeysToString(value);
618-
SubItems[1].Text = KeysString;
631+
this.custom = value;
632+
this.KeysString = DataConverter.KeysToString(value);
633+
this.SubItems[1].Text = this.KeysString;
619634
}
620635
}
636+
/// <summary>
637+
/// Gets the string representation of the custom shortcut keys.
638+
/// </summary>
621639
public String KeysString { get; private set; }
640+
/// <summary>
641+
/// Gets the modification status of the shortcut.
642+
/// </summary>
643+
public Boolean IsModified { get { return this.Custom != this.Default; } }
622644

645+
/// <summary>
646+
/// Creates a new instance of <see cref="ShortcutListItem"/> with an associated <see cref="ShortcutItem"/>.
647+
/// </summary>
623648
public ShortcutListItem(ShortcutItem item)
624649
{
625-
Name = Text = item.Id;
626-
SubItems.Add(string.Empty);
627-
Item = item;
628-
Conflicts = null;
629-
Custom = item.Custom;
650+
this.SubItems.Add(string.Empty);
651+
this.Name = this.Text = item.Id;
652+
this.Item = item;
653+
this.Conflicts = null;
654+
this.Custom = item.Custom;
630655
}
631656

657+
/// <summary>
658+
/// Apply changes made to this instance to the associated <see cref="ShortcutItem"/>.
659+
/// </summary>
632660
public void ApplyChanges()
633661
{
634-
Item.Custom = Custom;
662+
this.Item.Custom = this.Custom;
635663
}
636664
}
637665

PluginCore/PluginCore/Resources/de_DE.resX

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5964,4 +5964,8 @@ Eigene Sprachumgebungen müssen eine Erweiterung der Standard-Sprachumgebung sei
59645964
<value>Highlight word:</value>
59655965
<comment>Added after 5.0.2</comment>
59665966
</data>
5967+
<data name="FlashDevelop.Info.ShortcutConflictsPresent" xml:space="preserve">
5968+
<value>There are one or more conflicts present.</value>
5969+
<comment>REQUIRES TRANSLATION</comment>
5970+
</data>
59675971
</root>

PluginCore/PluginCore/Resources/en_US.resX

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5980,4 +5980,7 @@ Custom locales must be an extension of a default locale, e.g. en-US.</value>
59805980
<value>Highlight word:</value>
59815981
<comment>Added after 5.0.2</comment>
59825982
</data>
5983+
<data name="FlashDevelop.Info.ShortcutConflictsPresent" xml:space="preserve">
5984+
<value>There are one or more conflicts present.</value>
5985+
</data>
59835986
</root>

PluginCore/PluginCore/Resources/eu_ES.resX

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5961,4 +5961,8 @@ Lokalizazio pertsonalizatuek lehenetsiaren luzapen bat izan behar dute, adb. en-
59615961
<value>Highlight word:</value>
59625962
<comment>Added after 5.0.2</comment>
59635963
</data>
5964+
<data name="FlashDevelop.Info.ShortcutConflictsPresent" xml:space="preserve">
5965+
<value>There are one or more conflicts present.</value>
5966+
<comment>REQUIRES TRANSLATION</comment>
5967+
</data>
59645968
</root>

PluginCore/PluginCore/Resources/ja_JP.resX

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6022,4 +6022,8 @@ UseData:"</value>
60226022
<value>Highlight word:</value>
60236023
<comment>Added after 5.0.2</comment>
60246024
</data>
6025+
<data name="FlashDevelop.Info.ShortcutConflictsPresent" xml:space="preserve">
6026+
<value>There are one or more conflicts present.</value>
6027+
<comment>REQUIRES TRANSLATION</comment>
6028+
</data>
60256029
</root>

PluginCore/PluginCore/Resources/zh_CN.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5974,4 +5974,8 @@
59745974
<value>Highlight word:</value>
59755975
<comment>Added after 5.0.2</comment>
59765976
</data>
5977+
<data name="FlashDevelop.Info.ShortcutConflictsPresent" xml:space="preserve">
5978+
<value>There are one or more conflicts present.</value>
5979+
<comment>REQUIRES TRANSLATION</comment>
5980+
</data>
59775981
</root>

0 commit comments

Comments
 (0)