Skip to content

Commit edaaf5b

Browse files
authored
Improve KeyGesture.ToString() output in case when Key is set to Key.None (#18353)
* Improve ToString() output in case when Key is set to Key.None * Update KeyGesture.Parse method to support only modifiers combinations * Update tests with empty combination and modifiers-only combinations
1 parent 8232988 commit edaaf5b

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/Avalonia.Base/Input/KeyGesture.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,10 @@ public static KeyGesture Parse(string gesture)
7979
{
8080
var partSpan = gesture.AsSpan(cstart, c - cstart).Trim();
8181

82-
if (isLast)
83-
{
84-
key = ParseKey(partSpan.ToString());
85-
}
86-
else
82+
if (!TryParseKey(partSpan.ToString(), out key))
8783
{
8884
keyModifiers |= ParseModifier(partSpan);
8985
}
90-
9186
cstart = c + 1;
9287
}
9388
}
@@ -151,8 +146,11 @@ static void Plus(StringBuilder s)
151146
s.Append(formatInfo.Meta);
152147
}
153148

154-
Plus(s);
155-
s.Append(formatInfo.FormatKey(Key));
149+
if ((Key != Key.None) || (KeyModifiers == KeyModifiers.None))
150+
{
151+
Plus(s);
152+
s.Append(formatInfo.FormatKey(Key));
153+
}
156154

157155
return StringBuilderCache.GetStringAndRelease(s);
158156
}
@@ -163,12 +161,16 @@ public bool Matches(KeyEventArgs? keyEvent) =>
163161
ResolveNumPadOperationKey(keyEvent.Key) == ResolveNumPadOperationKey(Key);
164162

165163
// TODO: Move that to external key parser
166-
private static Key ParseKey(string key)
164+
private static bool TryParseKey(string keyStr, out Key key)
167165
{
168-
if (s_keySynonyms.TryGetValue(key.ToLower(CultureInfo.InvariantCulture), out Key rv))
169-
return rv;
166+
key = Key.None;
167+
if (s_keySynonyms.TryGetValue(keyStr.ToLower(CultureInfo.InvariantCulture), out key))
168+
return true;
169+
170+
if (EnumHelper.TryParse(keyStr, true, out key))
171+
return true;
170172

171-
return EnumHelper.Parse<Key>(key, true);
173+
return false;
172174
}
173175

174176
private static KeyModifiers ParseModifier(ReadOnlySpan<char> modifier)

tests/Avalonia.Base.UnitTests/Input/KeyGestureTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class KeyGestureTests
1313
new object[]{"Control++", new KeyGesture(Key.OemPlus, KeyModifiers.Control) },
1414
new object[]{ "Shift+⌘+A", new KeyGesture(Key.A, KeyModifiers.Meta | KeyModifiers.Shift) },
1515
new object[]{ "Shift+Cmd+A", new KeyGesture(Key.A, KeyModifiers.Meta | KeyModifiers.Shift) },
16+
new object[]{"None", new KeyGesture(Key.None)},
17+
new object[]{"Alt+Shift", new KeyGesture(Key.None, KeyModifiers.Alt | KeyModifiers.Shift)},
18+
1619
};
1720

1821
public static readonly IEnumerable<object[]> ToStringData = new object[][]
@@ -23,6 +26,8 @@ public class KeyGestureTests
2326
new object[]{new KeyGesture(Key.A, KeyModifiers.Alt | KeyModifiers.Shift), "Shift+Alt+A"},
2427
new object[]{new KeyGesture(Key.A, KeyModifiers.Control | KeyModifiers.Alt | KeyModifiers.Shift), "Ctrl+Shift+Alt+A"},
2528
new object[]{new KeyGesture(Key.A, KeyModifiers.Meta | KeyModifiers.Shift), "Shift+Cmd+A"},
29+
new object[]{new KeyGesture(Key.None), "None"},
30+
new object[]{new KeyGesture(Key.None, KeyModifiers.Alt | KeyModifiers.Shift), "Shift+Alt"},
2631
};
2732

2833
[Theory]

0 commit comments

Comments
 (0)