From da45077370cc56d1f33897b1f96a280e1063199e Mon Sep 17 00:00:00 2001 From: Kasbolat Kumakhov Date: Tue, 28 Nov 2023 13:34:00 +0300 Subject: [PATCH] Added a "ToKeys" method to return array of keys instead of formatting them directly into single string line --- HotKeys2.Test/HotKeyEntryTest.cs | 20 ++++++++++++++++++++ HotKeys2/HotKeyEntry.cs | 12 ++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/HotKeys2.Test/HotKeyEntryTest.cs b/HotKeys2.Test/HotKeyEntryTest.cs index e8d59fb..9219732 100644 --- a/HotKeys2.Test/HotKeyEntryTest.cs +++ b/HotKeys2.Test/HotKeyEntryTest.cs @@ -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[] { "?" }); + } } diff --git a/HotKeys2/HotKeyEntry.cs b/HotKeys2/HotKeyEntry.cs index 7f12da6..1b87b63 100644 --- a/HotKeys2/HotKeyEntry.cs +++ b/HotKeys2/HotKeyEntry.cs @@ -115,6 +115,15 @@ protected void CommonProcess(Func action) /// /// {0} will be replaced with key combination text, and {1} will be replaced with description of this hotkey entry object. public string ToString(string format) + { + var keyComboText = string.Join(" + ", this.ToKeys()); + return string.Format(format, keyComboText, this.Description); + } + + /// + /// Returns an array of String formatted keys. + /// + public string[] ToKeys() { var keyCombo = new List(); if (this._Modifiers != 0) @@ -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()