Skip to content

Commit

Permalink
Added a "ToKeys" method to return array of keys instead of formatting…
Browse files Browse the repository at this point in the history
… them directly into single string line
  • Loading branch information
uhfath committed Nov 28, 2023
1 parent b7a4d33 commit da45077
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
20 changes: 20 additions & 0 deletions HotKeys2.Test/HotKeyEntryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,24 @@ public void ToString_for_ByCode_Test()
var u = new HotKeyEntryByCode(null!, ModCode.None, Code.U, _ => ValueTask.CompletedTask, null, new() { Description = "Increment counter." });
u.ToString().Is("U: Increment counter.");
}

[Test]
public void ToKeys_for_ByCode_Test()
{
var ctrl_alt_f12 = new HotKeyEntryByCode(null!, ModCode.Ctrl | ModCode.Alt, Code.F12, _ => ValueTask.CompletedTask, null, new() { Description = "Set the volume level to 10." });
ctrl_alt_f12.ToKeys().IsStructuralEqual(new[] { "Ctrl", "Alt", "F12" });

var meta_one = new HotKeyEntryByCode(null!, ModCode.Meta, Code.Num1, _ => ValueTask.CompletedTask, null, new() { Description = "Launch the notepad." });
meta_one.ToKeys().IsStructuralEqual(new[] { "Meta", "1" });

var u = new HotKeyEntryByCode(null!, ModCode.None, Code.U, _ => ValueTask.CompletedTask, null, new() { Description = "Increment counter." });
u.ToKeys().IsStructuralEqual(new[] { "U" });
}

[Test]
public void ToKeys_for_ByKey_Test()
{
var hotKeyEntry = new HotKeyEntryByKey(null!, ModKey.None, Key.Question, _ => ValueTask.CompletedTask, null, new() { Description = "Show help." });
hotKeyEntry.ToKeys().IsStructuralEqual(new[] { "?" });
}
}
12 changes: 10 additions & 2 deletions HotKeys2/HotKeyEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ protected void CommonProcess(Func<ValueTask> action)
/// </summary>
/// <param name="format">{0} will be replaced with key combination text, and {1} will be replaced with description of this hotkey entry object.</param>
public string ToString(string format)
{
var keyComboText = string.Join(" + ", this.ToKeys());
return string.Format(format, keyComboText, this.Description);
}

/// <summary>
/// Returns an array of String formatted keys.
/// </summary>
public string[] ToKeys()
{
var keyCombo = new List<string>();
if (this._Modifiers != 0)
Expand All @@ -133,8 +142,7 @@ public string ToString(string format)
this._KeyEntry;
keyCombo.Add(keyDisplayName);

var keyComboText = string.Join(" + ", keyCombo);
return string.Format(format, keyComboText, this.Description);
return keyCombo.ToArray();
}

public void Dispose()
Expand Down

0 comments on commit da45077

Please sign in to comment.