Skip to content

Commit f0cf800

Browse files
committed
Fix KeyVisual Issue
1 parent afe1e1b commit f0cf800

3 files changed

Lines changed: 34 additions & 12 deletions

File tree

dev/DevWinUI.Controls/Controls/KeyVisual/KeyVisual.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,24 @@ public object Content
2222
set => SetValue(ContentProperty, value);
2323
}
2424

25-
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register("Content", typeof(object), typeof(KeyVisual), new PropertyMetadata(default(string), OnContentChanged));
25+
public static readonly DependencyProperty ContentProperty =
26+
DependencyProperty.Register(nameof(Content), typeof(object), typeof(KeyVisual), new PropertyMetadata(null, OnContentChanged));
2627

2728
public VisualType VisualType
2829
{
2930
get => (VisualType)GetValue(VisualTypeProperty);
3031
set => SetValue(VisualTypeProperty, value);
3132
}
3233

33-
public static readonly DependencyProperty VisualTypeProperty = DependencyProperty.Register("VisualType", typeof(VisualType), typeof(KeyVisual), new PropertyMetadata(default(VisualType), OnSizeChanged));
34+
public static readonly DependencyProperty VisualTypeProperty = DependencyProperty.Register(nameof(VisualType), typeof(VisualType), typeof(KeyVisual), new PropertyMetadata(default(VisualType), OnSizeChanged));
3435

3536
public bool IsError
3637
{
3738
get => (bool)GetValue(IsErrorProperty);
3839
set => SetValue(IsErrorProperty, value);
3940
}
4041

41-
public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register("IsError", typeof(bool), typeof(KeyVisual), new PropertyMetadata(false, OnIsErrorChanged));
42+
public static readonly DependencyProperty IsErrorProperty = DependencyProperty.Register(nameof(IsError), typeof(bool), typeof(KeyVisual), new PropertyMetadata(false, OnIsErrorChanged));
4243

4344
public KeyVisual()
4445
{
@@ -82,13 +83,21 @@ private void Update()
8283

8384
if (_keyVisual.Content != null)
8485
{
85-
object content = _keyVisual.Content;
86-
VirtualKey key = content switch
86+
VirtualKey key = VirtualKey.None;
87+
88+
var content = _keyVisual.Content;
89+
string keyName = null;
90+
91+
if (content is string strContent)
8792
{
88-
string str => GetVirtualKeyFromString(str),
89-
VirtualKey vk => vk,
90-
_ => (VirtualKey)(int)content
91-
};
93+
key = GetVirtualKeyFromString(strContent);
94+
keyName = strContent;
95+
}
96+
else if (content is KeyVisualInfo keyVisualInfo)
97+
{
98+
key = (VirtualKey)keyVisualInfo.Key;
99+
keyName = keyVisualInfo.KeyName;
100+
}
92101

93102
if (TryGetIconForKey((int)key, out object iconContent))
94103
{
@@ -98,7 +107,7 @@ private void Update()
98107
else
99108
{
100109
_keyVisual.Style = GetStyleSize("TextKeyVisualStyle");
101-
_keyVisual._keyPresenter.Content = content.ToString();
110+
_keyVisual._keyPresenter.Content = keyName;
102111
}
103112
}
104113
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Windows.System;
2+
3+
namespace DevWinUI;
4+
5+
public partial class KeyVisualInfo
6+
{
7+
public VirtualKey? Key { get; set; }
8+
public string KeyName { get; set; }
9+
10+
public override string ToString()
11+
{
12+
return KeyName;
13+
}
14+
}

dev/DevWinUI.Controls/Controls/Shortcut/Shortcut.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ private void OnKeyDown(object sender, KeyRoutedEventArgs e)
261261
_keyOrder.Add(key);
262262
}
263263

264-
// Now use _keyOrder for display — but only include keys still in _pressedKeys
265-
var keyNames = _keyOrder.Where(_pressedKeys.Contains).Select(GetKeyName).ToList();
264+
var keyNames = _keyOrder.Where(_pressedKeys.Contains).Select(key=> new KeyVisualInfo { Key = key, KeyName = GetKeyName(key) }).ToList();
266265
shortcut.Keys = keyNames.Cast<object>().ToList();
267266

268267
int modifierCount = _pressedKeys.Count(IsModifierKey);

0 commit comments

Comments
 (0)