浏览代码

Added support for changing modifier key names

CPKreuz 2 年之前
父节点
当前提交
d502b2934a

+ 3 - 0
src/PixiEditor/Data/Localization/Languages/en.json

@@ -536,6 +536,9 @@
   "WARNING_RESET_SHORTCUTS_DEFAULT": "Are you sure you want to reset all shortcuts to their default value?",
   "PRESS_ANY_KEY": "Press any key",
   "NONE_SHORTCUT": "None",
+  "CTRL_KEY": "Ctrl",
+  "SHIFT_KEY": "Shift",
+  "ALT_KEY": "Alt",
 
   "SUCCESS": "Success",
   "ERROR": "Error",

+ 13 - 14
src/PixiEditor/Helpers/Converters/KeyToStringConverter.cs

@@ -1,24 +1,23 @@
 using System.Globalization;
 using System.Windows.Input;
+using PixiEditor.Localization;
 
 namespace PixiEditor.Helpers.Converters;
 
 internal class KeyToStringConverter
     : SingleInstanceConverter<KeyToStringConverter>
 {
-    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
-    {
-        if (value is Key key)
+    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture) =>
+        value switch
         {
-            return InputKeyHelpers.GetKeyboardKey(key);
-        }
-        else if (value is ModifierKeys)
-        {
-            return value.ToString();
-        }
-        else
-        {
-            return string.Empty;
-        }
-    }
+            Key key => (object)InputKeyHelpers.GetKeyboardKey(key),
+            ModifierKeys modifier => modifier switch
+            {
+                ModifierKeys.Control => new LocalizedString("CTRL_KEY"),
+                ModifierKeys.Shift => new LocalizedString("SHIFT_KEY"),
+                ModifierKeys.Alt => new LocalizedString("ALT_KEY"),
+                _ => modifier.ToString()
+            },
+            _ => string.Empty
+        };
 }

+ 6 - 3
src/PixiEditor/Models/DataHolders/KeyCombination.cs

@@ -4,6 +4,7 @@ using System.Diagnostics;
 using System.Globalization;
 using System.Text;
 using System.Windows.Input;
+using PixiEditor.Localization;
 
 namespace PixiEditor.Models.DataHolders;
 
@@ -18,13 +19,15 @@ public record struct KeyCombination(Key Key, ModifierKeys Modifiers)
     {
         StringBuilder builder = new();
 
-        foreach (ModifierKeys modifier in Modifiers.GetFlags().OrderByDescending(x => x != ModifierKeys.Alt))
+        foreach (var modifier in Modifiers.GetFlags().OrderByDescending(x => x != ModifierKeys.Alt))
         {
             if (modifier == ModifierKeys.None) continue;
 
             string key = modifier switch
             {
-                ModifierKeys.Control => "Ctrl",
+                ModifierKeys.Control => new LocalizedString("CTRL_KEY"),
+                ModifierKeys.Shift => new LocalizedString("SHIFT_KEY"),
+                ModifierKeys.Alt => new LocalizedString("ALT_KEY"),
                 _ => modifier.ToString()
             };
 
@@ -40,4 +43,4 @@ public record struct KeyCombination(Key Key, ModifierKeys Modifiers)
     }
 
     private string GetDebuggerDisplay() => ToString(CultureInfo.InvariantCulture);
-}
+}

+ 1 - 1
src/PixiEditor/Views/Dialogs/ShortcutPopup.xaml

@@ -111,7 +111,7 @@
                                                         <ItemsControl.ItemTemplate>
                                                             <DataTemplate DataType="{x:Type ModifierKeys}">
                                                                 <Border Style="{StaticResource KeyBorder}">
-                                                                    <TextBlock Text="{Binding BindsDirectlyToSource=True, Converter={converters:KeyToStringConverter}}" Style="{StaticResource KeyBorderText}"/>
+                                                                    <TextBlock views:Translator.LocalizedString="{Binding BindsDirectlyToSource=True, Converter={converters:KeyToStringConverter}}" Style="{StaticResource KeyBorderText}"/>
                                                                 </Border>
                                                             </DataTemplate>
                                                         </ItemsControl.ItemTemplate>

+ 21 - 0
src/PixiEditor/Views/Translator.cs

@@ -16,6 +16,12 @@ public class Translator : UIElement
         typeof(Translator),
         new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.AffectsRender, KeyPropertyChangedCallback));
 
+    public static readonly DependencyProperty LocalizedStringProperty = DependencyProperty.RegisterAttached(
+        "LocalizedString",
+        typeof(LocalizedString),
+        typeof(Translator),
+        new FrameworkPropertyMetadata(default(LocalizedString), FrameworkPropertyMetadataOptions.AffectsRender, LocalizedStringPropertyChangedCallback));
+
     public static readonly DependencyProperty TooltipKeyProperty = DependencyProperty.RegisterAttached(
         "TooltipKey", typeof(string), typeof(Translator), 
         new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.AffectsRender, TooltipKeyPropertyChangedCallback));
@@ -90,6 +96,11 @@ public class Translator : UIElement
         }
     }
 
+    private static void LocalizedStringPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
+    {
+        d.SetValue(KeyProperty, ((LocalizedString)e.NewValue).Key);
+    }
+
     private static void UpdateKey(DependencyObject d, string key)
     {
         LocalizedString localizedString = new(key);
@@ -137,6 +148,16 @@ public class Translator : UIElement
         return (string)element.GetValue(KeyProperty);
     }
 
+    public static void SetLocalizedString(DependencyObject element, LocalizedString value)
+    {
+        element.SetValue(LocalizedStringProperty, value);
+    }
+
+    public static string GetLocalizedString(DependencyObject element)
+    {
+        return (string)element.GetValue(LocalizedStringProperty);
+    }
+    
     public static string GetValue(DependencyObject element)
     {
         return (string)element.GetValue(ValueProperty);

+ 2 - 0
src/PixiEditor/Views/UserControls/KeyCombinationBox.xaml.cs

@@ -42,6 +42,8 @@ internal partial class KeyCombinationBox : UserControl
 
         UpdateText();
         UpdateButton();
+
+        ViewModelMain.Current.LocalizationProvider.OnLanguageChanged += _ => UpdateText();
     }
 
     private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)